Skip to content

Commit

Permalink
FEAT: Rename swap_remove_pair to swap_remove_full
Browse files Browse the repository at this point in the history
Also return the index, hence the name full
  • Loading branch information
bluss committed Sep 24, 2017
1 parent 21f4dfd commit 32cd0e4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
35 changes: 19 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::fmt;
use std::mem::{replace};
use std::marker::PhantomData;

use util::{second, third, ptrdistance, enumerate};
use util::{third, ptrdistance, enumerate};
pub use equivalent::Equivalent;
pub use mutable_keys::MutableKeys;

Expand Down Expand Up @@ -866,6 +866,15 @@ impl<K, V, S> OrderMap<K, V, S>
self.find_using(h, move |entry| { Q::equivalent(key, &entry.key) })
}

/// FIXME Same as .swap_remove
///
/// Computes in **O(1)** time (average).
pub fn remove<Q: ?Sized>(&mut self, key: &Q) -> Option<V>
where Q: Hash + Equivalent<K>,
{
self.swap_remove(key)
}

/// Remove the key-value pair equivalent to `key` and return
/// its value.
///
Expand All @@ -879,33 +888,26 @@ impl<K, V, S> OrderMap<K, V, S>
pub fn swap_remove<Q: ?Sized>(&mut self, key: &Q) -> Option<V>
where Q: Hash + Equivalent<K>,
{
self.swap_remove_pair(key).map(second)
self.swap_remove_full(key).map(third)
}

/// FIXME Same as .swap_remove
///
/// Computes in **O(1)** time (average).
pub fn remove<Q: ?Sized>(&mut self, key: &Q) -> Option<V>
where Q: Hash + Equivalent<K>,
{
self.swap_remove(key)
}

/// Remove the key-value pair equivalent to `key` and return it.
/// Remove the key-value pair equivalent to `key` and return it and
/// the index it had.
///
/// Like `Vec::swap_remove`, the pair is removed by swapping it with the
/// last element of the map and popping it off. **This perturbs
/// the postion of what used to be the last element!**
///
/// Return `None` if `key` is not in map.
pub fn swap_remove_pair<Q: ?Sized>(&mut self, key: &Q) -> Option<(K, V)>
pub fn swap_remove_full<Q: ?Sized>(&mut self, key: &Q) -> Option<(usize, K, V)>
where Q: Hash + Equivalent<K>,
{
let (probe, found) = match self.find(key) {
None => return None,
Some(t) => t,
};
Some(self.remove_found(probe, found))
let (k, v) = self.remove_found(probe, found);
Some((found, k, v))
}

/// Remove the last key-value pair
Expand Down Expand Up @@ -1663,12 +1665,13 @@ mod tests {
let remove = [4, 12, 8, 7];

for &key in &remove_fail {
assert!(map.swap_remove_pair(&key).is_none());
assert!(map.swap_remove_full(&key).is_none());
}
println!("{:?}", map);
for &key in &remove {
//println!("{:?}", map);
assert_eq!(map.swap_remove_pair(&key), Some((key, key)));
let index = map.get_full(&key).unwrap().0;
assert_eq!(map.swap_remove_full(&key), Some((index, key, key)));
}
println!("{:?}", map);

Expand Down
1 change: 0 additions & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
use std::iter::Enumerate;
use std::mem::size_of;

pub fn second<A, B>(t: (A, B)) -> B { t.1 }
pub fn third<A, B, C>(t: (A, B, C)) -> C { t.2 }

pub fn enumerate<I>(iterable: I) -> Enumerate<I::IntoIter>
Expand Down
2 changes: 1 addition & 1 deletion tests/quick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ quickcheck! {
map.insert(key, ());
}
for &key in &remove {
map.swap_remove_pair(&key);
map.swap_remove(&key);
}
let elements = &set(&insert) - &set(&remove);
map.len() == elements.len() && map.iter().count() == elements.len() &&
Expand Down

0 comments on commit 32cd0e4

Please sign in to comment.