pub struct Hmap<KT, VT> {
pub table: Vec<Option<(KT, VT)>>,
pub count: usize,
pub maxhashes: Vec<usize>,
pub keylocs: Vec<usize>,
pub mask: usize,
pub hashstate: RandomState,
}Expand description
Structure of a HashMap, built from scratch, but which uses
Rust’s built-in implmenetations of the Hash trait.
This is a closed hash table with a linear probing rehash function.
The table is a vector of Option
Fields§
§table: Vec<Option<(KT, VT)>>§count: usize§maxhashes: Vec<usize>§keylocs: Vec<usize>§mask: usize§hashstate: RandomStateImplementations§
Source§impl<KT: Hash + Eq, VT> Hmap<KT, VT>
impl<KT: Hash + Eq, VT> Hmap<KT, VT>
Sourcepub fn with_capacity(requested_cap: usize) -> Self
pub fn with_capacity(requested_cap: usize) -> Self
Creates new Hmap with requested capacity. The actual capacity will be the nearest power of two that’s greater than or equal to the requested capacity.
Sourcepub fn current_capacity(&self) -> usize
pub fn current_capacity(&self) -> usize
returns the current capacity of the map
Sourcepub fn get(&self, key: &KT) -> Option<&VT>
pub fn get(&self, key: &KT) -> Option<&VT>
retrieves a borrow of the value associated with the given key,
if it exists. Look at the source code to understand how it’s defined.
Given an opt:Option
Sourcepub fn get_mut(&mut self, key: &KT) -> Option<&mut VT>
pub fn get_mut(&mut self, key: &KT) -> Option<&mut VT>
retrieves a mutable borrow of the value associated with the given key,
if it exists. Look at the source code. Given a mutable opt:Option
Sourcepub fn set(&mut self, key: KT, val: VT) -> Option<(KT, VT)>
pub fn set(&mut self, key: KT, val: VT) -> Option<(KT, VT)>
add or change the value associated with the key, returns the previous key and value, if it existed. Look at the source code. Note that core::mem::swap is used to assign to the vector while taking the previous value out of the vector.
Sourcepub fn remove(&mut self, key: &KT) -> Option<(KT, VT)>
pub fn remove(&mut self, key: &KT) -> Option<(KT, VT)>
removes the key-value pair given the key, returns the pair if it exists. Note that core::mem::swap is used to take the pair out of the vector.
Sourcepub fn add(&mut self, key: KT, val: VT)
pub fn add(&mut self, key: KT, val: VT)
Internal function called by resize, assumes that key-value pair is new and that table has enough capacity.
Sourcepub fn resize(&mut self, upsize: bool) -> bool
pub fn resize(&mut self, upsize: bool) -> bool
Resizes the hashmap by doubling or halfing the capacity.
Sourcepub fn hash(&self, key: &KT) -> usize
pub fn hash(&self, key: &KT) -> usize
hash function steals the Hash trait implementations of Rust
Sourcepub fn find_new_slot(&mut self, key: &KT) -> (usize, bool)
pub fn find_new_slot(&mut self, key: &KT) -> (usize, bool)
This function finds the location where a key is found, or where a new key-value pair can be inserted. It returns an index, paired with a boolean indicating whether the key was found at that index.