Skip to content

Commit d18bdf9

Browse files
committed
Add shift_take and deprecate take
1 parent ddd9e0f commit d18bdf9

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/set.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ impl<T, S> IndexSet<T, S>
349349
/// FIXME Same as .swap_take
350350
///
351351
/// Computes in **O(1)** time (average).
352+
#[deprecated(note = "use `swap_take` or `shift_take`")]
352353
pub fn take<Q: ?Sized>(&mut self, value: &Q) -> Option<T>
353354
where Q: Hash + Equivalent<T>,
354355
{
@@ -371,6 +372,22 @@ impl<T, S> IndexSet<T, S>
371372
self.map.swap_remove_full(value).map(|(_, x, ())| x)
372373
}
373374

375+
/// Removes and returns the value in the set, if any, that is equal to the
376+
/// given one.
377+
///
378+
/// Like `Vec::remove`, the value is removed by shifting all of the
379+
/// elements that follow it, preserving their relative order.
380+
/// **This perturbs the index of all of those elements!**
381+
///
382+
/// Return `None` if `value` was not in the set.
383+
///
384+
/// Computes in **O(n)** time (average).
385+
pub fn shift_take<Q: ?Sized>(&mut self, value: &Q) -> Option<T>
386+
where Q: Hash + Equivalent<T>,
387+
{
388+
self.map.shift_remove_full(value).map(|(_, x, ())| x)
389+
}
390+
374391
/// Remove the value from the set return it and the index it had.
375392
///
376393
/// Like `Vec::swap_remove`, the value is removed by swapping it with the

0 commit comments

Comments
 (0)