core:collections
Maps and sets that iterate in insertion order. HashMap<K, V> and
HashSet<T> find a key by hashing it under a HashSeed; TreeMap<K, V> and
TreeSet<T> by ordering it, for a key that is Ord but not Hash.
map[key] = value overwrites; try_insert / get_or_insert insert only
when the key is absent.
HashIndex is the table alone, for an interner whose keys already sit in
rows of its own: it holds a row number per key, and the caller supplies
each key's hash and says which row equals it. It gives up HashMap's
protections for speed. Nothing seeds the hash, and a hash whose top bits
do not differ between keys turns every lookup into a walk of the table.
Synopsis
let mut sizes = { small: 1, medium: 2, large: 3 } as HashMap<String, i32>;
assert sizes.len() == 3;
assert sizes["medium"] == 2;
assert sizes.get("small") matches { Some(v) && v == 1 };
sizes["xlarge"] = 4;
assert sizes.len() == 4;
let primes = [7, 2, 5, 2, 3] as HashSet<i32>;
assert primes.len() == 4;
assert primes.contains(5);
assert !primes.contains(4);
let mut seen = HashSet::<String>::new();
assert seen.insert("b");
assert seen.insert("a");
assert !seen.insert("b");
assert seen.iter_value().collect() == ["b", "a"];
Structs
pub struct MapKeysRefIter<K, V>
A map's keys, or a set's elements, by reference.
Fields are private.
pub fn iter_value(&self) -> MapKeysValueIter<K, V>
impl Iterator for MapKeysRefIter<K, V>
fn next(&mut self) -> Option<&K>
pub struct MapKeysValueIter<K, V>
A map's keys, or a set's elements, by value.
Fields are private.
impl Iterator for MapKeysValueIter<K, V>
fn next(&mut self) -> Option<K>
pub struct MapValuesRefIter<K, V>
A map's values, by reference.
Fields are private.
pub fn iter_value(&self) -> MapValuesValueIter<K, V>
impl Iterator for MapValuesRefIter<K, V>
fn next(&mut self) -> Option<&V>
pub struct MapValuesValueIter<K, V>
A map's values, by value. The doubled word is the axis meeting a
projection already named values.
Fields are private.
impl Iterator for MapValuesValueIter<K, V>
fn next(&mut self) -> Option<V>
pub struct MapEntriesRefIter<K, V>
A map's key-value pairs, by reference.
Fields are private.
pub fn iter_value(&self) -> MapEntriesValueIter<K, V>
impl Iterator for MapEntriesRefIter<K, V>
fn next(&mut self) -> Option<[&K, &V]>
#[compiler_item("map_entries_iter_next")]
pub struct MapEntriesValueIter<K, V>
A map's key-value pairs, by value.
Fields are private.
impl Iterator for MapEntriesValueIter<K, V>
fn next(&mut self) -> Option<[K, V]>
pub struct TreeMap<K, V>
A map that iterates in insertion order.
Keys are stored in a tree for O(log n) lookups, while key-value pairs are stored in a dense array for fast, insertion-order iteration.
This is similar to Rust's indexmap::IndexMap but uses a tree instead
of a hash table for key lookups.
Fields are private.
pub fn new() -> TreeMap<K, V>
Creates a new empty TreeMap.
pub fn len(&self) -> i32
Returns the number of key-value pairs in the map.
pub fn is_empty(&self) -> bool
Returns true if the map contains no elements.
pub fn try_insert(&mut self, key: K, value: V) -> bool
Inserts only if key is absent, and reports whether it was inserted.
The losing insert keeps the existing value, unlike map[key] = value.
pub fn get_or_insert(&mut self, key: K, value: V) -> V
The value already stored under key, or value inserted and returned.
The shape of TC39's Map.prototype.getOrInsert; for whether it
inserted, use try_insert.
pub fn update(&mut self, key: K, init: V, mut f: fn mut(&mut V) with _) with _
Hands f the value stored under key, init inserted first where the
key is absent, to write: one lookup for what Rust writes
*map.entry(k).or_insert(0) += 1. A value written in place is written
there. One replaced on assignment is stored under key once f
returns, over any write f made to that entry through the map, and
nowhere where f removed it.
pub fn contains_key(&self, key: K) -> bool
Returns true if the map contains a value for the specified key.
pub fn get(&self, key: K) -> Option<V>
Returns the value corresponding to the key, or null if not found.
pub fn remove(&mut self, key: K) -> bool
Removes a key from the map and returns true if the key was present. Preserves insertion order of remaining elements.
pub fn keys(&self) -> MapKeysRefIter<K, V>
The keys, in insertion order. A map traversal yields references and
carries no axis suffix: keys already says what it yields, and a
caller wanting owned keys takes iter_value() off the result.
pub fn values(&self) -> MapValuesRefIter<K, V>
The values, in insertion order.
pub fn entries(&self) -> MapEntriesRefIter<K, V>
The key-value pairs, in insertion order.
pub fn clear(&mut self)
Removes all key-value pairs from the map, keeping the capacity.
pub fn shrink_to_fit(&mut self)
Shrinks the capacity to match the current length.
pub fn get_str<S: AsStrSlice>(&self, key: S) -> Option<V>
Returns the value stored under text equal to key, or null.
pub fn contains_key_str<S: AsStrSlice>(&self, key: S) -> bool
Returns true if some key spells the same text as key.
pub fn get_ref_str<S: AsStrSlice>(&self, key: S) -> Option<&V>
Returns a reference to the value stored under text equal to key.
impl Eq for TreeMap<K, V>
fn eq(&self, other: &TreeMap<K, V>) -> bool
impl Hash for TreeMap<K, V>
fn hash<H: Hasher>(&self, h: &mut H)
impl Ord for TreeMap<K, V>
fn cmp(&self, other: &TreeMap<K, V>) -> Ordering
impl IndexAssign<K> for TreeMap<K, V>
fn index_assign(&mut self, key: K, value: Self::Output)
impl IndexValue<K> for TreeMap<K, V>
fn index_value(&self, key: K) -> Self::Output
impl IndexSwap<K> for TreeMap<K, V>
fn index_swap(&mut self, a: K, b: K)
impl IndexRef<K> for TreeMap<K, V>
fn index_ref(&self, key: K) -> &V
impl IndexRefMut<K> for TreeMap<K, V>
fn index_ref_mut(&mut self, key: K) -> &mut V
impl From<Array<[String, V]>> for TreeMap<String, V>
fn from(entries: Array<[String, V]>) -> TreeMap<String, V>
impl LiteralSpread for TreeMap<String, V>
fn spread_literal(&mut self, base: TreeMap<String, V>)
impl Serialize for TreeMap<K, V>
fn serialize<affine S: Serializer>(&self, s: &mut S) -> Result<(), SerializeError>
impl Deserialize for TreeMap<K, V>
fn deserialize<affine D: Deserializer>(d: &mut D) -> Result<TreeMap<K, V>, DeserializeError>
impl Inspect for TreeMap<K, V>
pub fn inspect(&self, f: &mut Formatter)
impl Default for TreeMap<K, V>
pub fn default() -> TreeMap<K, V>
pub struct TreeSet<T>
A set that iterates in insertion order.
Backed by a TreeMap<T, ()>, which keeps a B-tree for O(log n) membership
tests and a dense array for O(1) insertion-order iteration.
Duplicate inserts are silently ignored (preserving the original position). Removing an element and re-inserting it appends it at the end.
T must implement Ord to use any of the methods.
Fields are private.
pub fn new() -> TreeSet<T>
Creates a new empty TreeSet.
pub fn len(&self) -> i32
Returns the number of elements in the set.
pub fn is_empty(&self) -> bool
Returns true if the set contains no elements.
pub fn insert(&mut self, value: T) -> bool
Inserts a value into the set.
Returns true if the value was newly inserted, false if it was already present.
pub fn contains(&self, value: T) -> bool
Returns true if the set contains the given value.
pub fn remove(&mut self, value: T) -> bool
Removes a value from the set.
Returns true if the value was present, false otherwise.
pub fn clear(&mut self)
Removes all elements from the set, keeping the capacity.
pub fn shrink_to_fit(&mut self)
Shrinks the capacity to match the current length.
pub fn iter_ref(&self) -> MapKeysRefIter<T, ()>
The elements, in insertion order.
pub fn iter_value(&self) -> MapKeysValueIter<T, ()>
The elements, in insertion order.
pub fn contains_str<S: AsStrSlice>(&self, value: S) -> bool
Returns true if some element spells the same text as value.
impl IntoIterator for TreeSet<T>
fn into_iter(&self) -> MapKeysValueIter<T, ()>
impl From<Array<T>> for TreeSet<T>
fn from(elements: Array<T>) -> TreeSet<T>
impl Serialize for TreeSet<T>
fn serialize<affine S: Serializer>(&self, s: &mut S) -> Result<(), SerializeError>
impl Deserialize for TreeSet<T>
fn deserialize<affine D: Deserializer>(d: &mut D) -> Result<TreeSet<T>, DeserializeError>
impl Inspect for TreeSet<T>
pub fn inspect(&self, f: &mut Formatter)
impl Default for TreeSet<T>
pub fn default() -> TreeSet<T>
pub struct HashSeed
The key to a HashMap's hash function. A seed nobody outside the program
can predict keeps keys an attacker chooses from colliding.
Fields are private.
pub fn fixed(k0: u64, k1: u64) -> HashSeed
A seed of the caller's choosing, for a map whose keys the program trusts and whose hashing must be the same on every run.
pub struct HashMap<K, V>
A map that iterates in insertion order, found by hashing its keys.
Its hash function is keyed by a HashSeed: the host's under new(), which
holds up against keys an attacker chooses, or the caller's under
with_seed.
Fields are private.
pub fn new() -> HashMap<K, V>
#[compiler_item("hash_map_new")]
An empty map hashing under the host's seed.
pub fn with_seed(seed: HashSeed) -> HashMap<K, V>
An empty map hashing with seed.
pub fn len(&self) -> i32
#[compiler_item("hash_map_len")]
Returns the number of key-value pairs in the map.
pub fn is_empty(&self) -> bool
Returns true if the map contains no elements.
pub fn try_insert(&mut self, key: K, value: V) -> bool
Inserts only if key is absent, and reports whether it was inserted.
The losing insert keeps the existing value, unlike map[key] = value.
pub fn get_or_insert(&mut self, key: K, value: V) -> V
The value already stored under key, or value inserted and returned.
pub fn update(&mut self, key: K, init: V, mut f: fn mut(&mut V) with _) with _
Hands f the value stored under key, init inserted first where the
key is absent, to write: one lookup for what Rust writes
*map.entry(k).or_insert(0) += 1. A value written in place is written
there. One replaced on assignment is stored under key once f
returns, over any write f made to that entry through the map, and
nowhere where f removed it.
pub fn contains_key(&self, key: K) -> bool
Returns true if the map contains a value for the specified key.
pub fn get(&self, key: K) -> Option<V>
Returns the value corresponding to the key, or null if not found.
pub fn remove(&mut self, key: K) -> bool
Removes a key from the map and returns true if the key was present. Preserves insertion order of remaining elements.
pub fn keys(&self) -> MapKeysRefIter<K, V>
The keys, in insertion order.
pub fn values(&self) -> MapValuesRefIter<K, V>
The values, in insertion order.
pub fn entries(&self) -> MapEntriesRefIter<K, V>
#[compiler_item("hash_map_entries")]
The key-value pairs, in insertion order.
pub fn clear(&mut self)
Removes all key-value pairs from the map, keeping the capacity. The seed stays.
pub fn shrink_to_fit(&mut self)
Shrinks the capacity to match the current length.
pub fn get_str<S: AsStrSlice>(&self, key: S) -> Option<V>
Returns the value stored under text equal to key, or null.
pub fn contains_key_str<S: AsStrSlice>(&self, key: S) -> bool
Returns true if some key spells the same text as key.
pub fn get_ref_str<S: AsStrSlice>(&self, key: S) -> Option<&V>
Returns a reference to the value stored under text equal to key.
impl Eq for HashMap<K, V>
fn eq(&self, other: &HashMap<K, V>) -> bool
impl Hash for HashMap<K, V>
fn hash<H: Hasher>(&self, h: &mut H)
impl Ord for HashMap<K, V>
fn cmp(&self, other: &HashMap<K, V>) -> Ordering
impl Inspect for HashMap<K, V>
fn inspect(&self, f: &mut Formatter)
impl IndexAssign<K> for HashMap<K, V>
fn index_assign(&mut self, key: K, value: Self::Output)
#[compiler_item("hash_map_index_assign")]
impl IndexValue<K> for HashMap<K, V>
fn index_value(&self, key: K) -> Self::Output
impl IndexSwap<K> for HashMap<K, V>
fn index_swap(&mut self, a: K, b: K)
impl IndexRef<K> for HashMap<K, V>
fn index_ref(&self, key: K) -> &V
impl IndexRefMut<K> for HashMap<K, V>
fn index_ref_mut(&mut self, key: K) -> &mut V
impl From<Array<[String, V]>> for HashMap<String, V>
fn from(entries: Array<[String, V]>) -> HashMap<String, V>
impl LiteralSpread for HashMap<String, V>
fn spread_literal(&mut self, base: HashMap<String, V>)
impl Default for HashMap<K, V>
fn default() -> HashMap<K, V>
impl Serialize for HashMap<K, V>
fn serialize<affine S: Serializer>(&self, s: &mut S) -> Result<(), SerializeError>
impl Deserialize for HashMap<K, V>
fn deserialize<affine D: Deserializer>(d: &mut D) -> Result<HashMap<K, V>, DeserializeError>
pub struct HashSet<T>
A set that iterates in insertion order, found by hashing its elements.
Fields are private.
pub fn new() -> HashSet<T>
An empty set hashing under the host's seed.
pub fn with_seed(seed: HashSeed) -> HashSet<T>
An empty set hashing with seed.
pub fn len(&self) -> i32
Returns the number of elements in the set.
pub fn is_empty(&self) -> bool
Returns true if the set contains no elements.
pub fn insert(&mut self, value: T) -> bool
Inserts a value, and reports whether it was absent.
pub fn contains(&self, value: T) -> bool
Returns true if the set contains the given value.
pub fn remove(&mut self, value: T) -> bool
Removes a value, and reports whether it was present.
pub fn clear(&mut self)
Removes all elements from the set, keeping the capacity.
pub fn shrink_to_fit(&mut self)
Shrinks the capacity to match the current length.
pub fn iter_ref(&self) -> MapKeysRefIter<T, ()>
The elements, in insertion order.
pub fn iter_value(&self) -> MapKeysValueIter<T, ()>
The elements, in insertion order.
pub fn contains_str<S: AsStrSlice>(&self, value: S) -> bool
Returns true if some element spells the same text as value.
impl Inspect for HashSet<T>
fn inspect(&self, f: &mut Formatter)
impl IntoIterator for HashSet<T>
fn into_iter(&self) -> MapKeysValueIter<T, ()>
impl From<Array<T>> for HashSet<T>
fn from(elements: Array<T>) -> HashSet<T>
impl Default for HashSet<T>
fn default() -> HashSet<T>
impl Serialize for HashSet<T>
fn serialize<affine S: Serializer>(&self, s: &mut S) -> Result<(), SerializeError>
impl Deserialize for HashSet<T>
fn deserialize<affine D: Deserializer>(d: &mut D) -> Result<HashSet<T>, DeserializeError>
pub struct HashIndex
An index over rows the caller keeps elsewhere: it holds a row number per key and never the key. The caller hashes a key and says which row equals it. It keeps no order and removes nothing but everything.
A hash is the caller's, used as it comes: its top bits pick the slot, so
they must differ between keys. HashIndex::spread makes them, of a hash
whose bits differ anywhere.
Fields are private.
pub fn new() -> HashIndex
An empty index.
pub fn spread(hash: u64) -> u64
hash with a difference in any bit carried into the top ones. A
multiply carries a difference only upward, so a hash built of
multiplies alone, as FNV is, leaves its top bits blind to the low bits
of the last word and its low bits blind to the high bits of every
word. Folding the high half into the low and multiplying once more
closes both.
pub fn len(&self) -> i32
The number of rows indexed.
pub fn find(&self, hash: u64, is_row: fn(i32) -> bool) -> i32
The row is_row accepts among those indexed under hash, or -1.
pub fn find_or_insert(&mut self, hash: u64, is_row: fn(i32) -> bool, row: i32) -> i32
contract row >= 0
The row is_row accepts among those indexed under hash, or row
once it is indexed there.
pub fn insert(&mut self, hash: u64, row: i32)
contract row >= 0
Indexes row under hash. No row indexed there may equal it.
pub fn clear(&mut self)
Forgets every row. A table far wider than what it held is cut to fit
that, so one large fill does not make every later clear pay for its
width.
