Skip to content

Commit 1112578

Browse files
committed
Use insert_before for "new" entries in insert_sorted
The only difference compared to using `shift_insert` is when the binary-searched key isn't *actually* new to the map, just not found in properly sorted order. In this case we can't guarantee a sorted result either, but it will at least behave better about the new position, especially if that's the end. (cherry picked from commit 8ca01b0)
1 parent 8038705 commit 1112578

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

src/map.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ where
393393
///
394394
/// This is equivalent to finding the position with
395395
/// [`binary_search_keys`][Self::binary_search_keys], then either updating
396-
/// it or calling [`shift_insert`][Self::shift_insert] for a new key.
396+
/// it or calling [`insert_before`][Self::insert_before] for a new key.
397397
///
398398
/// If the sorted key is found in the map, its corresponding value is
399399
/// updated with `value`, and the older value is returned inside

src/map/tests.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,34 @@ fn shift_insert() {
136136
}
137137
}
138138

139+
#[test]
140+
fn insert_sorted_bad() {
141+
let mut map = OrderMap::new();
142+
map.insert(10, ());
143+
for i in 0..10 {
144+
map.insert(i, ());
145+
}
146+
147+
// The binary search will want to insert this at the end (index == len()),
148+
// but that's only possible for *new* inserts. It should still be handled
149+
// without panicking though, and in this case it's simple enough that we
150+
// know the exact result. (But don't read this as an API guarantee!)
151+
assert_eq!(map.first(), Some((&10, &())));
152+
map.insert_sorted(10, ());
153+
assert_eq!(map.last(), Some((&10, &())));
154+
assert!(map.keys().copied().eq(0..=10));
155+
156+
// Other out-of-order entries can also "insert" to a binary-searched
157+
// position, moving in either direction.
158+
map.move_index(5, 0);
159+
map.move_index(6, 10);
160+
assert_eq!(map.first(), Some((&5, &())));
161+
assert_eq!(map.last(), Some((&6, &())));
162+
map.insert_sorted(5, ()); // moves back up
163+
map.insert_sorted(6, ()); // moves back down
164+
assert!(map.keys().copied().eq(0..=10));
165+
}
166+
139167
#[test]
140168
fn grow() {
141169
let insert = [0, 4, 2, 12, 8, 7, 11];

src/set.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ where
335335
///
336336
/// This is equivalent to finding the position with
337337
/// [`binary_search`][Self::binary_search], and if needed calling
338-
/// [`shift_insert`][Self::shift_insert] for a new value.
338+
/// [`insert_before`][Self::insert_before] for a new value.
339339
///
340340
/// If the sorted item is found in the set, it returns the index of that
341341
/// existing item and `false`, without any change. Otherwise, it inserts the

0 commit comments

Comments
 (0)