pub struct HashMap<K, V> {
pub data: Vec<Option<Entry<K, V>>>,
pub ctrl: Vec<u8>,
/* private fields */
}Fields§
§data: Vec<Option<Entry<K, V>>>§ctrl: Vec<u8>Implementations§
Source§impl<K, V> HashMap<K, V>
impl<K, V> HashMap<K, V>
Sourcepub fn new_with_capacity(size: usize) -> Self
pub fn new_with_capacity(size: usize) -> Self
Constructor for an empty map with a given capacity.
Sourcepub fn occupied(&self) -> usize
pub fn occupied(&self) -> usize
Returns the total number of entries in the map (active + deleted entries) in O(n) time.
Sourcepub fn deleted(&self) -> usize
pub fn deleted(&self) -> usize
Returns the total number of deleted entries in the map in O(n) time.
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the total number of available slots in the map in O(1) time.
NOTE: The load factor is the quotient of len() / capacity().
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the map is either empty or contains only empty slots and deleted entries.
Sourcepub fn contains<Q>(&self, key: &Q) -> bool
pub fn contains<Q>(&self, key: &Q) -> bool
Takes a key as a reference and returns a Boolean indicating whether its in the map. The expected temporal complexity is O(1), as the map maintains a laod factor of <=.5.
Sourcepub fn get<Q>(&self, key: &Q) -> Option<&V>
pub fn get<Q>(&self, key: &Q) -> Option<&V>
Returns a reference to the value associated with a key, if the key exists.
Sourcepub fn put(&mut self, key: K, value: V) -> Option<Entry<K, V>>
pub fn put(&mut self, key: K, value: V) -> Option<Entry<K, V>>
Adds entry (k, v), overwriting any value v associated with an
existing key k, returns old value. If a new addition increases
the map’s load factor above the designated threshhold of 0.5
the map resizes.
pub fn try_put(&mut self, key: K, value: V)
Sourcepub fn mut_val_or<F>(&mut self, key: K, f: F, default: V)
pub fn mut_val_or<F>(&mut self, key: K, f: F, default: V)
Takes a key, a closure, and a default value. If the key is in the map, applies the closure to the corresponding entry’s value. If the key is not in the map, creates a new entry with the provided value.
Example:
use dsa_rust::associative::probing_hash_table::HashMap;
let mut count: HashMap<char, u32> = HashMap::new();
let word: &str = "Hello";
for k in word.chars() {
count.mut_val_or(k, |x| *x += 1, 1);
}Sourcepub fn remove<Q>(&mut self, key: &Q) -> Option<Entry<K, V>>
pub fn remove<Q>(&mut self, key: &Q) -> Option<Entry<K, V>>
Removes and returns an entry from the map for a given key, if it exists in the map.
Sourcepub fn rehash(self) -> Self
pub fn rehash(self) -> Self
Consumes self and returns a new map of the same size, but without any tombstones.
Works like a cleanup operation in O(capacity) time because into_iter checks all positions.
Sourcepub fn shrink_to_fit(self) -> Self
pub fn shrink_to_fit(self) -> Self
Rebuilds the map to eliminate accumulated tombstones thereby reducing spatial bloat. This operation runs in O(n) time and is intended for long-lived maps that have undergone many deletions.
The operation consumes the existing map and returns a new HashMap
with the same live entries.
Sourcepub fn iter(&self) -> Iter<'_, K, V> ⓘ
pub fn iter(&self) -> Iter<'_, K, V> ⓘ
Returns an iterator over borrowed values. The resulting values appear in random order.
Example use:
use dsa_rust::associative::probing_hash_table::HashMap;
let mut count: HashMap<char, u8> = HashMap::new();
let mut v = Vec::new();
for e in count.iter() {
v.push(*e.0);
}