pub struct SlabMap<T> { /* private fields */ }
Expand description
A fast HashMap-like collection that automatically determines the key.
Implementations§
source§impl<T> SlabMap<T>
impl<T> SlabMap<T>
sourcepub const fn new() -> Self
pub const fn new() -> Self
Constructs a new, empty SlabMap<T>
.
The SlabMap will not allocate until elements are pushed onto it.
sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Constructs a new, empty SlabMap<T>
with the specified capacity.
sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of elements the SlabMap can hold without reallocating.
sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional more elements to be inserted in the given SlabMap<T>
.
Panics
Panics if the new capacity overflows usize.
sourcepub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>
pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>
Try to reserve capacity for at least additional more elements to be inserted in the given SlabMap<T>
.
sourcepub fn reserve_exact(&mut self, additional: usize)
pub fn reserve_exact(&mut self, additional: usize)
Reserves the minimum capacity for exactly additional more elements to be inserted in the given SlabMap<T>
.
Panics
Panics if the new capacity overflows usize.
sourcepub fn try_reserve_exact(
&mut self,
additional: usize
) -> Result<(), TryReserveError>
pub fn try_reserve_exact( &mut self, additional: usize ) -> Result<(), TryReserveError>
Try to reserve the minimum capacity for exactly additional more elements to be inserted in the given SlabMap<T>
.
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the SlabMap.
Examples
use slabmap::SlabMap;
let mut s = SlabMap::new();
assert_eq!(s.len(), 0);
let key1 = s.insert(10);
let key2 = s.insert(15);
assert_eq!(s.len(), 2);
s.remove(key1);
assert_eq!(s.len(), 1);
s.remove(key2);
assert_eq!(s.len(), 0);
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the SlabMap contains no elements.
Examples
use slabmap::SlabMap;
let mut s = SlabMap::new();
assert_eq!(s.is_empty(), true);
let key = s.insert("a");
assert_eq!(s.is_empty(), false);
s.remove(key);
assert_eq!(s.is_empty(), true);
sourcepub fn get(&self, key: usize) -> Option<&T>
pub fn get(&self, key: usize) -> Option<&T>
Returns a reference to the value corresponding to the key.
Examples
use slabmap::SlabMap;
let mut s = SlabMap::new();
let key = s.insert(100);
assert_eq!(s.get(key), Some(&100));
assert_eq!(s.get(key + 1), None);
sourcepub fn get_mut(&mut self, key: usize) -> Option<&mut T>
pub fn get_mut(&mut self, key: usize) -> Option<&mut T>
Returns a mutable reference to the value corresponding to the key.
sourcepub fn contains_key(&self, key: usize) -> bool
pub fn contains_key(&self, key: usize) -> bool
Returns true if the SlabMap contains a value for the specified key.
Examples
use slabmap::SlabMap;
let mut s = SlabMap::new();
let key = s.insert(100);
assert_eq!(s.contains_key(key), true);
assert_eq!(s.contains_key(key + 1), false);
sourcepub fn insert(&mut self, value: T) -> usize
pub fn insert(&mut self, value: T) -> usize
Inserts a value into the SlabMap.
Returns the key associated with the value.
Examples
use slabmap::SlabMap;
let mut s = SlabMap::new();
let key_abc = s.insert("abc");
let key_xyz = s.insert("xyz");
assert_eq!(s[key_abc], "abc");
assert_eq!(s[key_xyz], "xyz");
sourcepub fn insert_with_key(&mut self, f: impl FnOnce(usize) -> T) -> usize
pub fn insert_with_key(&mut self, f: impl FnOnce(usize) -> T) -> usize
Inserts a value given by f
into the SlabMap. The key to be associated with the value is passed to f
.
Returns the key associated with the value.
Examples
use slabmap::SlabMap;
let mut s = SlabMap::new();
let key = s.insert_with_key(|key| format!("my key is {}", key));
assert_eq!(s[key], format!("my key is {}", key));
sourcepub fn remove(&mut self, key: usize) -> Option<T>
pub fn remove(&mut self, key: usize) -> Option<T>
Removes a key from the SlabMap, returning the value at the key if the key was previously in the SlabMap.
Examples
use slabmap::SlabMap;
let mut s = SlabMap::new();
let key = s.insert("a");
assert_eq!(s.remove(key), Some("a"));
assert_eq!(s.remove(key), None);
sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the SlabMap, removing all values and optimize free spaces.
Examples
use slabmap::SlabMap;
let mut s = SlabMap::new();
s.insert(1);
s.insert(2);
s.clear();
assert_eq!(s.is_empty(), true);
sourcepub fn drain(&mut self) -> Drain<'_, T> ⓘ
pub fn drain(&mut self) -> Drain<'_, T> ⓘ
Clears the SlabMap, returning all values as an iterator and optimize free spaces.
Examples
use slabmap::SlabMap;
let mut s = SlabMap::new();
let k0 = s.insert(10);
let k1 = s.insert(20);
let d: Vec<_> = s.drain().collect();
let mut e = vec![(k0, 10), (k1, 20)];
e.sort();
assert_eq!(s.is_empty(), true);
assert_eq!(d, e);
sourcepub fn retain(&mut self, f: impl FnMut(usize, &mut T) -> bool)
pub fn retain(&mut self, f: impl FnMut(usize, &mut T) -> bool)
Retains only the elements specified by the predicate and optimize free spaces.
Examples
use slabmap::SlabMap;
let mut s = SlabMap::new();
s.insert(10);
s.insert(15);
s.insert(20);
s.insert(25);
s.retain(|_idx, value| *value % 2 == 0);
let value: Vec<_> = s.values().cloned().collect();
assert_eq!(value, vec![10, 20]);
sourcepub fn optimize(&mut self)
pub fn optimize(&mut self)
Optimizing the free space for speeding up iterations.
If the free space has already been optimized, this method does nothing and completes with O(1).
Examples
use slabmap::SlabMap;
use std::time::Instant;
let mut s = SlabMap::new();
const COUNT: usize = 1000000;
for i in 0..COUNT {
s.insert(i);
}
let keys: Vec<_> = s.keys().take(COUNT - 1).collect();
for key in keys {
s.remove(key);
}
s.optimize(); // if comment out this line, `s.values().sum()` to be slow.
let begin = Instant::now();
let sum: usize = s.values().sum();
println!("sum : {}", sum);
println!("duration : {} ms", (Instant::now() - begin).as_millis());