Unverified Commit 1ed253e0 authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

auto-hash-map: add swap_remove to maps and sets

Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent e3b14676
...@@ -204,6 +204,25 @@ where ...@@ -204,6 +204,25 @@ where
} }
} }
/// Removes `key` by swapping the last entry into its place, like
/// [IndexMap::swap_remove](https://docs.rs/indexmap/latest/indexmap/map/struct.IndexMap.html#method.swap_remove).
///
/// This always swaps, also for ordered storages, so it's O(1) there but changes the order of
/// the remaining entries. For unordered storages it's the same as [`Self::remove`].
pub fn swap_remove(&mut self, key: &K) -> Option<V> {
match self {
RawAutoMap::List(list, _) => {
for i in 0..list.len() {
if list[i].0 == *key {
return Some(list.swap_remove(i).1);
}
}
None
}
RawAutoMap::Map(map) => map.swap_remove(key),
}
}
/// see [HashMap::extend](https://doc.rust-lang.org/std/collections/struct.HashMap.html#method.extend) /// see [HashMap::extend](https://doc.rust-lang.org/std/collections/struct.HashMap.html#method.extend)
pub fn extend(&mut self, iter: impl IntoIterator<Item = (K, V)>) { pub fn extend(&mut self, iter: impl IntoIterator<Item = (K, V)>) {
let iter = iter.into_iter(); let iter = iter.into_iter();
...@@ -1355,6 +1374,44 @@ mod tests { ...@@ -1355,6 +1374,44 @@ mod tests {
assert_eq!(keys(&map), expected); assert_eq!(keys(&map), expected);
} }
#[test]
fn test_auto_index_map_swap_remove() {
// List mode: the last entry moves into the removed slot.
let mut map: AutoIndexMap<usize, usize> = (0..5).map(|i| (i, i * 10)).collect();
assert!(matches!(map, RawAutoMap::List(..)));
assert_eq!(map.swap_remove(&1), Some(10));
assert_eq!(map.swap_remove(&1), None);
assert_eq!(map.swap_remove(&99), None);
assert_eq!(keys(&map), vec![0, 4, 2, 3]);
// Removing the last entry doesn't move anything.
assert_eq!(map.swap_remove(&3), Some(30));
assert_eq!(keys(&map), vec![0, 4, 2]);
// Map mode: same semantics as `IndexMap::swap_remove`.
let n = MAX_USEFUL_LINEAR_SCAN * 2;
let mut map: AutoIndexMap<usize, usize> = (0..n).map(|i| (i, i * 10)).collect();
assert!(matches!(map, RawAutoMap::Map(..)));
assert_eq!(map.swap_remove(&0), Some(0));
assert_eq!(map.swap_remove(&0), None);
let mut expected: Vec<usize> = (0..n).collect();
expected.swap_remove(0);
assert_eq!(keys(&map), expected);
assert_eq!(map.get(&(n - 1)), Some(&((n - 1) * 10)));
}
#[test]
fn test_auto_map_swap_remove_is_remove() {
for n in [5, MAX_USEFUL_LINEAR_SCAN * 2] {
let mut swapped: AutoMap<usize, usize> = (0..n).map(|i| (i, i * 10)).collect();
let mut removed = swapped.clone();
for key in [1, 3, n - 1, n + 7] {
assert_eq!(swapped.swap_remove(&key), removed.remove(&key));
}
assert_eq!(keys(&swapped), keys(&removed));
assert_eq!(swapped, removed);
}
}
#[test] #[test]
fn test_auto_index_map_entry_order_across_conversion() { fn test_auto_index_map_entry_order_across_conversion() {
let n = MAX_USEFUL_LINEAR_SCAN * 2; let n = MAX_USEFUL_LINEAR_SCAN * 2;
......
...@@ -120,6 +120,15 @@ where ...@@ -120,6 +120,15 @@ where
self.map.remove(key).is_some() self.map.remove(key).is_some()
} }
/// Removes `key` by swapping the last item into its place, like
/// [IndexSet::swap_remove](https://docs.rs/indexmap/latest/indexmap/set/struct.IndexSet.html#method.swap_remove).
///
/// This always swaps, also for ordered storages, so it's O(1) there but changes the order of
/// the remaining items. For unordered storages it's the same as [`Self::remove`].
pub fn swap_remove(&mut self, key: &K) -> bool {
self.map.swap_remove(key).is_some()
}
/// see [HashSet::extend](https://doc.rust-lang.org/std/collections/hash_set/struct.HashSet.html#method.extend) /// see [HashSet::extend](https://doc.rust-lang.org/std/collections/hash_set/struct.HashSet.html#method.extend)
pub fn extend(&mut self, iter: impl IntoIterator<Item = K>) { pub fn extend(&mut self, iter: impl IntoIterator<Item = K>) {
self.map.extend(iter.into_iter().map(|item| (item, ()))) self.map.extend(iter.into_iter().map(|item| (item, ())))
...@@ -473,6 +482,28 @@ mod tests { ...@@ -473,6 +482,28 @@ mod tests {
assert_eq!(set.into_iter().collect::<Vec<_>>(), expected[1..]); assert_eq!(set.into_iter().collect::<Vec<_>>(), expected[1..]);
} }
#[test]
fn test_swap_remove() {
for n in [5, MAX_USEFUL_LINEAR_SCAN * 2] {
// Ordered: the last item moves into the removed slot, in list and map mode.
let mut set: AutoIndexSet<usize> = (0..n).collect();
assert!(set.swap_remove(&1));
assert!(!set.swap_remove(&1));
assert!(!set.swap_remove(&(n + 7)));
let mut expected: Vec<usize> = (0..n).collect();
expected.swap_remove(1);
assert_eq!(set.iter().copied().collect::<Vec<_>>(), expected);
// Unordered: the same as `remove`.
let mut swapped: AutoSet<usize> = (0..n).collect();
let mut removed = swapped.clone();
for key in [1, 3, n - 1, n + 7] {
assert_eq!(swapped.swap_remove(&key), removed.remove(&key));
}
assert!(swapped.iter().eq(removed.iter()));
}
}
#[test] #[test]
fn test_auto_index_set_serialization_preserves_order() { fn test_auto_index_set_serialization_preserves_order() {
for n in [5, MAX_USEFUL_LINEAR_SCAN * 2] { for n in [5, MAX_USEFUL_LINEAR_SCAN * 2] {
......
...@@ -295,6 +295,12 @@ pub trait BackingMapStorage<K, V>: Sized { ...@@ -295,6 +295,12 @@ pub trait BackingMapStorage<K, V>: Sized {
Self::Hasher: BuildHasher; Self::Hasher: BuildHasher;
/// Removes `key`. Ordered storages preserve the order of the other entries. /// Removes `key`. Ordered storages preserve the order of the other entries.
fn remove(&mut self, key: &K) -> Option<V> fn remove(&mut self, key: &K) -> Option<V>
where
K: Eq + Hash,
Self::Hasher: BuildHasher;
/// Removes `key` by swapping the last entry into its place. This is O(1) for ordered storages
/// too, but changes their order. Unordered storages remove the same way as [`Self::remove`].
fn swap_remove(&mut self, key: &K) -> Option<V>
where where
K: Eq + Hash, K: Eq + Hash,
Self::Hasher: BuildHasher; Self::Hasher: BuildHasher;
...@@ -481,6 +487,13 @@ impl<K, V, H> BackingMapStorage<K, V> for HashMap<K, V, H> { ...@@ -481,6 +487,13 @@ impl<K, V, H> BackingMapStorage<K, V> for HashMap<K, V, H> {
{ {
HashMap::remove(self, key) HashMap::remove(self, key)
} }
fn swap_remove(&mut self, key: &K) -> Option<V>
where
K: Eq + Hash,
H: BuildHasher,
{
HashMap::remove(self, key)
}
fn extend(&mut self, iter: impl Iterator<Item = (K, V)>) fn extend(&mut self, iter: impl Iterator<Item = (K, V)>)
where where
K: Eq + Hash, K: Eq + Hash,
...@@ -735,6 +748,13 @@ impl<K, V, H> BackingMapStorage<K, V> for IndexMap<K, V, H> { ...@@ -735,6 +748,13 @@ impl<K, V, H> BackingMapStorage<K, V> for IndexMap<K, V, H> {
{ {
IndexMap::shift_remove(self, key) IndexMap::shift_remove(self, key)
} }
fn swap_remove(&mut self, key: &K) -> Option<V>
where
K: Eq + Hash,
H: BuildHasher,
{
IndexMap::swap_remove(self, key)
}
fn extend(&mut self, iter: impl Iterator<Item = (K, V)>) fn extend(&mut self, iter: impl Iterator<Item = (K, V)>)
where where
K: Eq + Hash, K: Eq + Hash,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment