docs

You are reading the docs for v0.1.0. Nabi 0.1.0 is no longer supported. Latest release: v0.1.3 →

core::collections::dict

On this page

Dict<K, V, S>, a hash map hashing with S (the process-keyed RandomState by default), compact: entries are appended to dense key / value / hash arrays, and a power-of-two index of linear-probed slots maps a hash to its entry. A table of at most SMALL entries has no index: lookup scans the hashes. remove leaves a hole; holes are squeezed out when the entries are full. One object holds the counts and the current arrays, swapped on growth. Iteration follows the entries, so it is in insertion order: a present key re-inserted keeps its place, a removed one re-inserted goes last.

Structs

DictHeadSource

pub struct DictHead<K, V, S>

The dict's object. keys / vals have one more slot that stays zero: a hole's key and value are overwritten with it, so a removed entry keeps nothing alive. meta holds cap entry hashes (0: a hole), then slots index slots (0: empty, else entry + 1). An index slot of a removed entry stays, pointing at the hole: a tombstone, passed over because no lookup hash is 0. Index slots in use never exceed cap, at most 2/3 of slots, so probing always ends.

DictSource

pub struct Dict<K, V, S = RandomState>

S builds the hashers (RandomState: the process key).

  • pub fn freeze(self) -> FrozenDict<K, V, S>§

    Source

    The dict as a FrozenDict, which threads share without a lock or a copy. O(1) (O(n) once for a dict built at compile time): the frozen dict takes the entries over, and using self after is a compile error (to_dict() gives a dict to change again; freeze a clone() to keep this one).

  • pub const fn new() -> Dict<K, V>§

    Source

    An empty dict; its storage is allocated at the first insert.

  • pub const fn with_capacity(cap: usize) -> Dict<K, V>§

    Source

    Room for cap entries before the first growth.

  • pub const fn with_hasher(state: S) -> Dict<K, V, S>§

    Source

    An empty dict hashing with state's hashers; its storage is allocated at the first insert.

  • pub const fn with_capacity_and_hasher(cap: usize, state: S) -> Dict<K, V, S>§

    Source

    Room for cap entries, hashing with state's hashers.

  • pub const fn hasher(self) -> S§

    Source

    What builds the dict's hashers.

  • pub const fn len(self) -> usize§

    Source

    The number of entries.

  • pub const fn is_empty(self) -> bool§

    Source

    true when the dict has no entry.

  • pub const fn insert(self, k: K, v: V) -> V | none§

    Source

    Insert or replace; the old value, or none.

  • pub const fn get(self, k: K) -> V | none§

    Source

    The value under k; none when k is absent.

  • pub const fn get_or(self, k: K, default: V) -> V§

    Source

    The value under k; default when k is absent. The dict is not changed. Python: d.get(k, default).

  • pub const fn get_or_insert(self, k: K, default: V) -> V§

    Source

    The value under k, first inserting default when k is absent. d[k, default: v] as an assignment target uses it.

  • pub const fn contains_key(self, k: K) -> bool§

    Source

    true when k has an entry.

  • pub const fn remove(self, k: K) -> V | none§

    Source

    Remove k's entry and return its value; none when k is absent.

  • pub const fn remove_entry(self, k: K) -> (K, V) | none§

    Source

    Remove k; the stored key and its value, or none when absent.

  • pub const fn pop_last(self) -> (K, V) | none§

    Source

    Remove and return the entry inserted last, or none when the dict is empty. Python: d.popitem().

    nabi
    let d = {"a": 1, "b": 2, "c": 3};
    let (k, v) = d.pop_last() ?? ("", 0);
    assert(k == "c" && v == 3);
    let (k1, _) = d.pop_first() ?? ("", 0);
    let (k2, _) = d.remove_entry("b") ?? ("", 0);
    assert(k1 == "a" && k2 == "b" && d.is_empty() && d.pop_last() is none);
    
  • pub const fn pop_first(self) -> (K, V) | none§

    Source

    Remove and return the entry inserted first, or none when the dict is empty.

  • pub const fn drain(self) -> List<(K, V)>§

    Source

    Remove every entry and return them, in insertion order.

    nabi
    let d = {"x": 1, "y": 2};
    assert(d.drain() == [("x", 1), ("y", 2)] && d.is_empty());
    
  • pub const fn retain(self, keep: fn(K, V) -> bool)§

    Source

    Keep only the entries keep accepts, in place; O(n).

  • pub const fn extend<I: IntoIterator<Item = (K, V)>>(self, pairs: I)§

    Source

    Insert every (key, value) pair of pairs, in order; a later pair's value wins.

  • pub const fn clear(self)§

    Source

    Drop every entry (a fresh empty table, allocated at the next insert).

  • pub const fn keys(self) -> Keys<K, V, S>§

    Source

    The keys, lazily, in insertion order.

  • pub const fn values(self) -> Values<K, V, S>§

    Source

    The values, lazily.

  • pub const fn iter(self) -> DictIter<K, V, S>§

    Source

    (key, value) pairs, lazily (insertion order).

  • pub const fn entry(self, k: K) -> Entry<K, V, S>§

    Source

    k's place in the dict, present or not.

  • pub const fn most_common(self, n: usize | none = none) -> List<(K, usize)>§

    Source

    The n largest counts (all when n is none) as (key, count), largest first; equal counts keep first-seen order. Python: Counter.most_common(n); build the counts with xs.counts().

Trait implementations 14

EntrySource

pub struct Entry<K, V, S = RandomState>

d.entry(k): read or fill one key's slot. Each call looks the key up again.

  • pub fn key(self) -> K§

    Source

    The entry's key.

  • pub const fn or_insert(self, v: V) -> V§

    Source

    The value, after inserting v if the key is absent.

  • pub const fn or_insert_with(self, f: fn() -> V) -> V§

    Source

    The value, after inserting f() if the key is absent.

  • pub const fn and_modify(self, f: fn(V) -> V) -> Entry<K, V, S>§

    Source

    A present value replaced by f(value); an absent key is left alone.

  • pub const fn or_default(self) -> V§

    Source

    The value, after inserting V::default() if the key is absent.

Trait implementations 1
  • impl<K, V, S> Debug for Entry<K, V, S>

KeysSource

pub struct Keys<K, V, S = RandomState>

What d.keys() returns: the keys, in insertion order. Changing the dict while iterating gives unspecified (memory-safe) results.

Trait implementations 2

ValuesSource

pub struct Values<K, V, S = RandomState>

What d.values() returns: the values, in insertion order. Changing the dict while iterating gives unspecified (memory-safe) results.

Trait implementations 2

DictIterSource

pub struct DictIter<K, V, S = RandomState>

What d.iter() returns: (key, value) pairs, in insertion order. Changing the dict while iterating gives unspecified (memory-safe) results.

Trait implementations 2