[][src]Struct slabmap::SlabMap

pub struct SlabMap<T> { /* fields omitted */ }

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

Implementations

impl<T> SlabMap<T>[src]

pub fn new() -> Self[src]

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

pub fn with_capacity(capacity: usize) -> Self[src]

Constructs a new, empty SlabMap with the specified capacity.

pub fn capacity(&self) -> usize[src]

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

pub fn reserve(&mut self, additional: usize)[src]

Reserves capacity for at least additional more elements to be inserted in the given SlabMap.

Panics

Panics if the new capacity overflows usize.

pub fn reserve_exact(&mut self, additional: usize)[src]

Reserves the minimum capacity for exactly additional more elements to be inserted in the given SlabMap.

Panics

Panics if the new capacity overflows usize.

pub fn len(&self) -> usize[src]

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

pub fn is_empty(&self) -> bool[src]

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

pub fn get(&self, key: usize) -> Option<&T>[src]

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

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

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

pub fn contains_key(&self, key: usize) -> bool[src]

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

pub fn insert(&mut self, value: T) -> usize[src]

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

pub fn insert_with_key(&mut self, f: impl FnOnce(usize) -> T) -> usize[src]

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

pub fn remove(&mut self, key: usize) -> Option<T>[src]

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

pub fn clear(&mut self)[src]

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

pub fn drain(&mut self) -> Drain<T>[src]

Clears the SlabMap, returning all values as an iterator and optimize free spaces.

Examples

use slabmap::SlabMap;

let mut s = SlabMap::new();
s.insert(10);
s.insert(20);

let d: Vec<_> = s.drain().collect();

assert_eq!(s.is_empty(), true);
assert_eq!(d, vec![10, 20]);

pub fn retain(&mut self, f: impl FnMut(usize, &mut T) -> bool)[src]

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

pub fn optimize(&mut self)[src]

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

pub fn iter(&self) -> Iter<T>[src]

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.

pub fn iter_mut(&mut self) -> IterMut<T>[src]

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.

pub fn keys(&self) -> Keys<T>[src]

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.

pub fn values(&self) -> Values<T>[src]

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.

pub fn values_mut(&mut self) -> ValuesMut<T>[src]

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

impl<T: Clone> Clone for SlabMap<T>[src]

impl<T: Debug> Debug for SlabMap<T>[src]

impl<T> Default for SlabMap<T>[src]

impl<T> Index<usize> for SlabMap<T>[src]

type Output = T

The returned type after indexing.

impl<T> IndexMut<usize> for SlabMap<T>[src]

impl<T> IntoIterator for SlabMap<T>[src]

type Item = T

The type of the elements being iterated over.

type IntoIter = IntoIter<T>

Which kind of iterator are we turning this into?

impl<'a, T> IntoIterator for &'a SlabMap<T>[src]

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?

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

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?

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

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<I> IntoIterator for I where
    I: Iterator
[src]

type Item = <I as Iterator>::Item

The type of the elements being iterated over.

type IntoIter = I

Which kind of iterator are we turning this into?

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.