Struct slabmap::SlabMap

source ·
pub struct SlabMap<T> { /* private fields */ }
Expand description

A fast HashMap-like collection that automatically determines the key.

Implementations§

source§

impl<T> SlabMap<T>

source

pub const fn new() -> Self

Constructs a new, empty SlabMap<T>. The SlabMap will not allocate until elements are pushed onto it.

source

pub fn with_capacity(capacity: usize) -> Self

Constructs a new, empty SlabMap<T> with the specified capacity.

source

pub fn capacity(&self) -> usize

Returns the number of elements the SlabMap can hold without reallocating.

source

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.

source

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>.

source

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.

source

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>.

source

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);
source

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);
source

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);
source

pub fn get_mut(&mut self, key: usize) -> Option<&mut T>

Returns a mutable reference to the value corresponding to the key.

source

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);
source

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");
source

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));
source

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);
source

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);
source

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);
source

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]);
source

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());
source

pub fn iter(&self) -> Iter<'_, T>

Gets an iterator over the entries of the SlabMap, sorted by key.

If you make a large number of remove calls, optimize should be called before calling this function.

source

pub fn iter_mut(&mut self) -> IterMut<'_, T>

Gets a mutable iterator over the entries of the slab, sorted by key.

If you make a large number of remove calls, optimize should be called before calling this function.

source

pub fn keys(&self) -> Keys<'_, T>

Gets an iterator over the keys of the SlabMap, in sorted order.

If you make a large number of remove calls, optimize should be called before calling this function.

source

pub fn values(&self) -> Values<'_, T>

Gets an iterator over the values of the SlabMap.

If you make a large number of remove calls, optimize should be called before calling this function.

source

pub fn values_mut(&mut self) -> ValuesMut<'_, T>

Gets a mutable iterator over the values of the SlabMap.

If you make a large number of remove calls, optimize should be called before calling this function.

Trait Implementations§

source§

impl<T: Clone> Clone for SlabMap<T>

source§

fn clone(&self) -> SlabMap<T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Debug> Debug for SlabMap<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T> Default for SlabMap<T>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<T> Index<usize> for SlabMap<T>

§

type Output = T

The returned type after indexing.
source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl<T> IndexMut<usize> for SlabMap<T>

source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
source§

impl<'a, T> IntoIterator for &'a SlabMap<T>

§

type Item = (usize, &'a T)

The type of the elements being iterated over.
§

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<'a, T> IntoIterator for &'a mut SlabMap<T>

§

type Item = (usize, &'a mut T)

The type of the elements being iterated over.
§

type IntoIter = IterMut<'a, T>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<T> IntoIterator for SlabMap<T>

§

type Item = (usize, T)

The type of the elements being iterated over.
§

type IntoIter = IntoIter<T>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for SlabMap<T>where T: RefUnwindSafe,

§

impl<T> Send for SlabMap<T>where T: Send,

§

impl<T> Sync for SlabMap<T>where T: Sync,

§

impl<T> Unpin for SlabMap<T>where T: Unpin,

§

impl<T> UnwindSafe for SlabMap<T>where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.