Skip to main content

BumpMap

Struct BumpMap 

Source
pub struct BumpMap<K, V, S = RandomState> { /* private fields */ }
Expand description

A hash map that uses bump allocation inside.

Read this module documentation for more details.

Implementations§

Source§

impl<K, V> BumpMap<K, V, RandomState>

Source

pub fn new() -> Self

Creates an empty BumpMap.

The new map will not allocate until it is first pair inserted into.

§Examples
let mut map: BumpMap<&str, i32> = BumpMap::new();
assert_eq!(map.capacity(), 0);
Source

pub fn with_capacity(capacity: usize) -> Self

Creates an empty BumpMap with at least the specified capacity.

The hash map will be able to hold at least capacity elements without reallocating. This method is allowed to allocate for more elements than capacity. If capacity is zero, the hash map will not allocate.

§Examples
let map: BumpMap<&str, i32> = BumpMap::with_capacity(10);
assert!(map.capacity() >= 10);
Source§

impl<K, V, S> BumpMap<K, V, S>

Source

pub const fn with_hasher(hasher: S) -> Self

Creates an empty BumpMap which will use the given hash builder to hash keys.

The new map will not allocate until it is first pair inserted into.

§Examples
use bumpish::BumpMap;
use std::hash::RandomState;

let s = RandomState::new();
let mut map = BumpMap::with_hasher(s);
map.insert(1, 2);
Source

pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self

Creates an empty BumpMap with at least the specified capacity, using hasher to hash the keys.

The map will be able to hold at least capacity elements without reallocating. This method is allowed to allocate for more elements than capacity. If capacity is zero, the hash map will not allocate.

§Examples
use bumpish::BumpMap;
use std::hash::RandomState;

let s = RandomState::new();
let mut map = BumpMap::with_capacity_and_hasher(10, s);
map.insert(1, 2);
assert!(map.capacity() >= 10);
Source

pub fn capacity(&self) -> usize

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

§Examples
let map: BumpMap<i32, i32> = BumpMap::with_capacity(100);
assert!(map.capacity() >= 100);
Source

pub fn len(&self) -> usize

Returns the number of elements in the map.

§Examples
let mut a = BumpMap::new();
assert_eq!(a.len(), 0);
a.insert(1, "a");
assert_eq!(a.len(), 1);
Source

pub fn is_empty(&self) -> bool

Returns true if the map contains no elements.

§Examples
let mut a = BumpMap::new();
assert!(a.is_empty());
a.insert(1, "a");
assert!(!a.is_empty());
Source

pub fn clear(&mut self)

Clears the map, removing all key-value pairs. Can keep some of the allocated memory for reuse.

§Examples
let mut a = BumpMap::new();
a.insert(1, "a");
a.clear();
assert!(a.is_empty());
Source

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

An iterator visiting all key-value pairs in insertion order. The iterator element type is (&'a K, &'a V).

§Examples
use bumpish::BumpMap;

let map = BumpMap::from([
    ("a", 1),
    ("b", 2),
    ("c", 3),
]);

assert_eq!(
    map.iter().collect::<Vec<(_, _)>>(),
    [(&"a", &1), (&"b", &2), (&"c", &3)]
);
Source

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

An iterator visiting all key-value pairs in insertion order, with mutable references to the values. The iterator element type is (&'a K, &'a mut V).

§Examples
use bumpish::BumpMap;

let mut map = BumpMap::from([
    ("a", 1),
    ("b", 2),
    ("c", 3),
]);

// Update all values
for (_, val) in map.iter_mut() {
    *val *= 2;
}

assert_eq!(
    map.iter().collect::<Vec<(_, _)>>(),
    [(&"a", &2), (&"b", &4), (&"c", &6)]
);
Source

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

An iterator visiting all keys in insertion order. The iterator element type is &'a K.

§Examples
use bumpish::BumpMap;

let map = BumpMap::from([
    ("a", 1),
    ("b", 2),
    ("c", 3),
]);

for key in map.keys() {
    println!("{key}");
}
Source

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

An iterator visiting all values in insertion order. The iterator element type is &'a V.

§Examples
use bumpish::BumpMap;

let map = BumpMap::from([
    ("a", 1),
    ("b", 2),
    ("c", 3),
]);

for val in map.values() {
    println!("{val}");
}
Source

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

An iterator visiting all values mutably in insertion order. The iterator element type is &'a mut V.

§Examples
use bumpish::BumpMap;

let mut map = BumpMap::from([
    ("a", 1),
    ("b", 2),
    ("c", 3),
]);

for val in map.values_mut() {
    *val = *val + 10;
}

for val in map.values() {
    println!("{val}");
}
Source§

impl<K, V, S> BumpMap<K, V, S>
where S: BuildHasher,

Source

pub fn hasher(&self) -> &S

Returns a reference to the map’s BuildHasher.

§Examples
use bumpish::BumpMap;
use std::hash::RandomState;

let hasher = RandomState::new();
let map: BumpMap<i32, i32> = BumpMap::with_hasher(hasher);
let hasher: &RandomState = map.hasher();
Source

pub fn contains_key<Q>(&self, key: &Q) -> bool
where Q: ?Sized + Hash + Equivalent<K>,

Returns true if the map contains a value with an equivalent key to key.

The Q type must hash like K.

§Examples
let mut map = BumpMap::new();
map.insert("a".to_owned(), 1);
assert_eq!(map.contains_key("a"), true);
assert_eq!(map.contains_key(&"a".to_owned()), true);
assert_eq!(map.contains_key("b"), false);
Source

pub fn get<Q>(&self, key: &Q) -> Option<&V>
where Q: ?Sized + Hash + Equivalent<K>,

Returns a reference to the value corresponding to the key.

The Q type must hash like K.

§Examples
let mut map = BumpMap::new();
map.insert(1, "a");
assert_eq!(map.get(&1), Some(&"a"));
assert_eq!(map.get(&2), None);
Source

pub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)>
where Q: ?Sized + Hash + Equivalent<K>,

Returns the key-value pair corresponding to the supplied key. This is potentially useful:

  • for key types where non-identical keys can be considered equal;
  • for getting the &K stored key value from an equivalent &Q lookup key; or
  • for getting a reference to a key with the same lifetime as the collection.

The Q type must hash like K.

§Examples
use bumpish::BumpMap;
use std::hash::{Hash, Hasher};

#[derive(Clone, Copy, Debug)]
struct S {
    id: u32,
    name: &'static str, // ignored by equality and hashing operations
}

impl PartialEq for S {
    fn eq(&self, other: &S) -> bool {
        self.id == other.id
    }
}

impl Eq for S {}

impl Hash for S {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.id.hash(state);
    }
}

let j_a = S { id: 1, name: "Jessica" };
let j_b = S { id: 1, name: "Jess" };
let p = S { id: 2, name: "Paul" };
assert_eq!(j_a, j_b);

let mut map = BumpMap::new();
map.insert(j_a, "Paris");
assert_eq!(map.get_key_value(&j_a), Some((&j_a, &"Paris")));
assert_eq!(map.get_key_value(&j_b), Some((&j_a, &"Paris"))); // the notable case
assert_eq!(map.get_key_value(&p), None);
Source

pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
where Q: ?Sized + Hash + Equivalent<K>,

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

The Q type must hash like K.

§Examples

let mut map = BumpMap::new();
map.insert(1, "a");
if let Some(x) = map.get_mut(&1) {
    *x = "b";
}
assert_eq!(map[&1], "b");
Source§

impl<K, V, S> BumpMap<K, V, S>
where K: Hash + Eq, S: BuildHasher,

Source

pub fn insert(&self, key: K, value: V) -> Option<&V>

Inserts a key-value pair into the map if an equivalent key is not present.

If the new key-value pair is inserted, it returns a shared reference to the value. Otherwise, it returns None.

§Examples
let mut map = BumpMap::new();

assert_eq!(map.insert(37, "a"), Some(&"a"));
assert_eq!(map.insert(37, "b"), None);
assert_eq!(map[&37], "a");
Source

pub fn insert_full(&self, key: K, value: V) -> Option<(&K, &V)>

Inserts a key-value pair into the map if an equivalent key is not present.

If the new key-value pair is inserted, it returns a tuple of shared references to both key and value. Otherwise, it returns None.

§Examples
let mut map = BumpMap::new();

assert_eq!(map.insert_full(37, "a"), Some((&37, &"a")));
assert_eq!(map.insert_full(37, "b"), None);
assert_eq!(map[&37], "a");
Source

pub fn insert_mut(&mut self, key: K, value: V) -> Option<&mut V>

Inserts a key-value pair into the map if an equivalent key is not present.

If the new key-value pair is inserted, it returns a mutable reference to the value. Otherwise, it returns None.

§Examples
let mut map = BumpMap::new();

assert_eq!(map.insert_mut(37, "a"), Some(&mut "a"));
assert_eq!(map.insert_mut(37, "b"), None);
assert_eq!(map[&37], "a");
Source

pub fn insert_full_mut(&mut self, key: K, value: V) -> Option<(&K, &mut V)>

Inserts a key-value pair into the map if an equivalent key is not present.

If the new key-value pair is inserted, it returns a tuple of shared reference to key and mutable reference to value. Otherwise, it returns None.

§Examples
let mut map = BumpMap::new();

assert_eq!(map.insert_full(37, "a"), Some((&37, &"a")));
assert_eq!(map.insert_full(37, "b"), None);
assert_eq!(map[&37], "a");
Source

pub fn replace(&mut self, key: K, value: V) -> Option<V>

Replaces the value associated with the given key. If an equivalent key is not present, inserts a new key-value pair.

Returns the old value if it existed, otherwise returns None.

§Examples
let mut map = BumpMap::new();
assert_eq!(map.replace(37, "a"), None);
assert_eq!(map.replace(37, "b"), Some("a"));
Source

pub fn replace_full(&mut self, key: K, value: V) -> Option<(K, V)>

Replaces both key and value associated with the given key. If an equivalent key is not present, inserts a new key-value pair.

Returns the old key and value if they existed, otherwise returns None.

§Examples
let mut map = BumpMap::new();
assert_eq!(map.replace_full(37, "a"), None);
assert_eq!(map.replace_full(37, "b"), Some((37, "a")));
Source

pub fn entry(&self, key: K) -> Entry<'_, K, V>

Gets the given key’s corresponding entry in the map.

As distinct from standard HashMap::entry, this method returns an Entry that can’t be used to modify the value associated with the key. That allows to call entry method with a shared reference to the map.

But this still allows to insert a new key-value pair into the vacant entry.

§Examples
use bumpish::BumpMap;

let mut letters = BumpMap::new();

for ch in "a short treatise on fungi".chars() {
    letters.entry(ch).or_insert(true);
}

assert_eq!(letters[&'s'], true);
assert_eq!(letters[&'t'], true);
assert_eq!(letters[&'u'], true);
assert_eq!(letters.get(&'y'), None);
Source§

impl<K, V, S> BumpMap<K, V, S>
where K: Eq + Hash, S: BuildHasher,

Source

pub fn augment<I>(&self, iter: I)
where I: IntoIterator<Item = (K, V)>,

Augment the map with the contents of an iterator. If a key (the first element of a tuple) already exists in this map, the key and corresponding value are dropped.

Unlike Extend trait, this method does not replace existing entries, and requires a shared reference to the map.

§Examples
let ages = BumpMap::new();

ages.augment([("John", 18), ("Anna", 24), ("Lily", 99)]);

assert_eq!(ages[&"John"], 18);
assert_eq!(ages[&"Anna"], 24);
assert_eq!(ages[&"Lily"], 99);

ages.augment([("John", 25), ("Anna", 3), ("Will", 54)]);

assert_eq!(ages[&"John"], 18); // keeps the old value
assert_eq!(ages[&"Anna"], 24); // keeps the old value
assert_eq!(ages[&"Lily"], 99);
assert_eq!(ages[&"Will"], 54);

Trait Implementations§

Source§

impl<K, V, S> Clone for BumpMap<K, V, S>
where K: Clone, V: Clone, S: Clone,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<K, V, S> Debug for BumpMap<K, V, S>
where K: Debug, V: Debug,

Source§

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

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

impl<K, V, S> Default for BumpMap<K, V, S>
where S: Default,

Source§

fn default() -> Self

Creates an empty BumpMap<K, V, S>, with the Default value for the hasher.

Source§

impl<'a, K, V, S> Extend<(&'a K, &'a V)> for BumpMap<K, V, S>
where K: Eq + Hash + Copy, V: Copy, S: BuildHasher,

Source§

fn extend<T: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<K, V, S> Extend<(K, V)> for BumpMap<K, V, S>
where K: Eq + Hash, S: BuildHasher,

Source§

fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<K, V, const N: usize> From<[(K, V); N]> for BumpMap<K, V, RandomState>
where K: Eq + Hash,

Available on crate feature std only.
Source§

fn from(arr: [(K, V); N]) -> Self

Converts a [(K, V); N] into a BumpMap<K, V>.

If any entries in the array have equal keys, all but one of the corresponding values will be dropped.

§Examples
let map1 = BumpMap::from([(1, 2), (3, 4)]);
let map2: BumpMap<_, _> = [(1, 2), (3, 4)].into();
assert_eq!(map1, map2);
Source§

impl<K, V, S> FromIterator<(K, V)> for BumpMap<K, V, S>
where K: Eq + Hash, S: BuildHasher + Default,

Source§

fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self

Constructs a BumpMap<K, V> from an iterator of key-value pairs.

If the iterator produces any pairs with equal keys, all but one of the corresponding values will be dropped.

Source§

impl<K, Q, V, S> Index<&Q> for BumpMap<K, V, S>
where K: Eq + Hash, Q: Eq + Hash + Equivalent<K> + ?Sized, S: BuildHasher,

Source§

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

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

§Panics

Panics if the key is not present in the BumpMap.

Source§

type Output = V

The returned type after indexing.
Source§

impl<'a, K, V, S> IntoIterator for &'a BumpMap<K, V, S>

Source§

type Item = (&'a K, &'a V)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, K, V>

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

fn into_iter(self) -> Iter<'a, K, V>

Creates an iterator from a value. Read more
Source§

impl<'a, K, V, S> IntoIterator for &'a mut BumpMap<K, V, S>

Source§

type Item = (&'a K, &'a mut V)

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, K, V>

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

fn into_iter(self) -> IterMut<'a, K, V>

Creates an iterator from a value. Read more
Source§

impl<K, V, S> IntoIterator for BumpMap<K, V, S>

Source§

type Item = (K, V)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<K, V>

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

fn into_iter(self) -> IntoIter<K, V>

Creates an iterator from a value. Read more
Source§

impl<K, V, S> PartialEq for BumpMap<K, V, S>
where K: Eq + Hash, V: PartialEq, S: BuildHasher,

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<K, V, S> Eq for BumpMap<K, V, S>
where K: Eq + Hash, V: Eq, S: BuildHasher,

Auto Trait Implementations§

§

impl<K, V, S = RandomState> !Freeze for BumpMap<K, V, S>

§

impl<K, V, S = RandomState> !RefUnwindSafe for BumpMap<K, V, S>

§

impl<K, V, S = RandomState> !Send for BumpMap<K, V, S>

§

impl<K, V, S = RandomState> !Sync for BumpMap<K, V, S>

§

impl<K, V, S> Unpin for BumpMap<K, V, S>
where S: Unpin, K: Unpin, V: Unpin,

§

impl<K, V, S> UnsafeUnpin for BumpMap<K, V, S>

§

impl<K, V, S> UnwindSafe for BumpMap<K, V, S>

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. 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 T
where 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 T
where T: Clone,

Source§

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 T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.