Skip to content

Commit 354345b

Browse files
committed
Take *two* entries in insert_sorted_by
1 parent 314ec7d commit 354345b

File tree

5 files changed

+15
-13
lines changed

5 files changed

+15
-13
lines changed

src/map.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -505,12 +505,12 @@ where
505505
/// pair is moved to or inserted at that position regardless.
506506
///
507507
/// Computes in **O(n)** time (average).
508-
pub fn insert_sorted_by<F>(&mut self, key: K, value: V, cmp: F) -> (usize, Option<V>)
508+
pub fn insert_sorted_by<F>(&mut self, key: K, value: V, mut cmp: F) -> (usize, Option<V>)
509509
where
510510
K: Ord,
511-
F: FnMut(&K, &V) -> Ordering,
511+
F: FnMut(&K, &V, &K, &V) -> Ordering,
512512
{
513-
let (Ok(i) | Err(i)) = self.binary_search_by(cmp);
513+
let (Ok(i) | Err(i)) = self.binary_search_by(|k, v| cmp(k, v, &key, &value));
514514
self.insert_before(i, key, value)
515515
}
516516

src/map/core/entry.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,13 +412,13 @@ impl<'a, K, V> VacantEntry<'a, K, V> {
412412
/// pair is inserted at that position regardless.
413413
///
414414
/// Computes in **O(n)** time (average).
415-
pub fn insert_sorted_by<F>(self, value: V, cmp: F) -> (usize, &'a mut V)
415+
pub fn insert_sorted_by<F>(self, value: V, mut cmp: F) -> (usize, &'a mut V)
416416
where
417417
K: Ord,
418-
F: FnMut(&K, &V) -> Ordering,
418+
F: FnMut(&K, &V, &K, &V) -> Ordering,
419419
{
420420
let slice = crate::map::Slice::from_slice(self.map.entries);
421-
let (Ok(i) | Err(i)) = slice.binary_search_by(cmp);
421+
let (Ok(i) | Err(i)) = slice.binary_search_by(|k, v| cmp(k, v, &self.key, &value));
422422
(i, self.shift_insert(i, value))
423423
}
424424

src/map/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,14 +1251,14 @@ fn insert_sorted_by() {
12511251
let mut values = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)];
12521252
let mut map: IndexMap<i32, i32> = IndexMap::new();
12531253
for (key, value) in values {
1254-
let (_, old) = map.insert_sorted_by(key, value, |probe, _| key.cmp(probe));
1254+
let (_, old) = map.insert_sorted_by(key, value, |key1, _, key2, _| key2.cmp(key1));
12551255
assert_eq!(old, None);
12561256
}
12571257
values.reverse();
12581258
assert_eq!(values, *map.as_slice());
12591259

12601260
for (key, value) in &mut values {
1261-
let (_, old) = map.insert_sorted_by(*key, -*value, |probe, _| (*key).cmp(probe));
1261+
let (_, old) = map.insert_sorted_by(*key, -*value, |key1, _, key2, _| key2.cmp(key1));
12621262
assert_eq!(old, Some(*value));
12631263
*value = -*value;
12641264
}

src/set.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -437,9 +437,11 @@ where
437437
pub fn insert_sorted_by<F>(&mut self, value: T, mut cmp: F) -> (usize, bool)
438438
where
439439
T: Ord,
440-
F: FnMut(&T) -> Ordering,
440+
F: FnMut(&T, &T) -> Ordering,
441441
{
442-
let (index, existing) = self.map.insert_sorted_by(value, (), |k, _| cmp(k));
442+
let (index, existing) = self
443+
.map
444+
.insert_sorted_by(value, (), |a, (), b, ()| cmp(a, b));
443445
(index, existing.is_none())
444446
}
445447

@@ -938,7 +940,7 @@ impl<T, S> IndexSet<T, S> {
938940
where
939941
F: FnMut(&T, &T) -> Ordering,
940942
{
941-
self.map.sort_by(move |a, _, b, _| cmp(a, b));
943+
self.map.sort_by(move |a, (), b, ()| cmp(a, b));
942944
}
943945

944946
/// Sort the values of the set and return a by-value iterator of

tests/quick.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,11 @@ quickcheck_limit! {
136136
let mut map2 = IndexMap::new();
137137
for &(key, value) in &insert {
138138
hmap.insert(key, value);
139-
map.insert_sorted_by(key, value, |other, _| key.cmp(other));
139+
map.insert_sorted_by(key, value, |key1, _, key2, _| key2.cmp(key1));
140140
match map2.entry(key) {
141141
Entry::Occupied(e) => *e.into_mut() = value,
142142
Entry::Vacant(e) => {
143-
e.insert_sorted_by(value, |other, _| key.cmp(other));
143+
e.insert_sorted_by(value, |key1, _, key2, _| key2.cmp(key1));
144144
}
145145
}
146146
}

0 commit comments

Comments
 (0)