Skip to content

Commit

Permalink
Add IndexSet::shift_insert based on map's method
Browse files Browse the repository at this point in the history
  • Loading branch information
cuviper committed Feb 11, 2024
1 parent 3b217ca commit 4572493
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,20 @@ where
(index, existing.is_none())
}

/// Insert the value into the set at the given index.
///
/// If an equivalent item already exists in the set, it returns
/// `false` leaving the original value in the set, but moving it to
/// the new position in the set. Otherwise, it inserts the new
/// item at the given index and returns `true`.
///
/// ***Panics*** if `index` is out of bounds.
///
/// Computes in **O(n)** time (average).
pub fn shift_insert(&mut self, index: usize, value: T) -> bool {
self.map.shift_insert(index, value, ()).is_none()
}

/// Adds a value to the set, replacing the existing value, if any, that is
/// equal to the given one, without altering its insertion order. Returns
/// the replaced value.
Expand Down
19 changes: 19 additions & 0 deletions src/set/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,25 @@ fn insert_order() {
}
}

#[test]
fn shift_insert() {
let insert = [0, 4, 2, 12, 8, 7, 11, 5, 3, 17, 19, 22, 23];
let mut set = IndexSet::new();

for &elt in &insert {
set.shift_insert(0, elt);
}

assert_eq!(set.iter().count(), set.len());
assert_eq!(set.iter().count(), insert.len());
for (a, b) in insert.iter().rev().zip(set.iter()) {
assert_eq!(a, b);
}
for (i, v) in (0..insert.len()).zip(set.iter()) {
assert_eq!(set.get_index(i).unwrap(), v);
}
}

#[test]
fn replace() {
let replace = [0, 4, 2, 12, 8, 7, 11, 5];
Expand Down

0 comments on commit 4572493

Please sign in to comment.