Skip to main content

Hmap

Struct Hmap 

Source
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. The vector maxhashes tracks the maximum number of hashes and rehashes required for a given hash slot. keylocs tracks locations of possible keys in the vector, which is important for quick iteration over the structure. The size/capacity of the vector is always set to a power of 2: this means instead of % capacity we can calculate & mask, where mask is always capacity-1; The hashstate is necessary for writing the hash function.

Fields§

§table: Vec<Option<(KT, VT)>>§count: usize§maxhashes: Vec<usize>§keylocs: Vec<usize>§mask: usize§hashstate: RandomState

Implementations§

Source§

impl<KT: Hash + Eq, VT> Hmap<KT, VT>

Source

pub fn new() -> Self

Creates new Hmap with default capacity 16

Source

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.

Source

pub fn len(&self) -> usize

returns the number of key-value pairs inside the map

Source

pub fn current_capacity(&self) -> usize

returns the current capacity of the map

Source

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, opt.as_ref() will return an Option<&T>.

Source

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, opt.as_mut() will return an Option<&mut T>.

Source

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.

Source

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.

Source

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.

Source

pub fn resize(&mut self, upsize: bool) -> bool

Resizes the hashmap by doubling or halfing the capacity.

Source

pub fn hash(&self, key: &KT) -> usize

hash function steals the Hash trait implementations of Rust

Source

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.

Source

pub fn find_slot(&self, key: &KT) -> Option<usize>

This function finds the index of the internal vector where a key is located, if it exists.

Source

pub fn iter<'t>(&'t self) -> HmapIter<'t, KT, VT>

Creates iterator over all key-value pairs

Trait Implementations§

Source§

impl<KT: Hash + Eq, VT> Index<&KT> for Hmap<KT, VT>

Source§

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

Provides ability to use syntax such as map[&key]. Operation will panic if key does not exist

Source§

type Output = VT

The returned type after indexing.
Source§

impl<KT: Hash + Eq, VT> Index<KT> for Hmap<KT, VT>

Source§

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

Provides ability to use syntax such as map[key]. Operation will panic if key does not exist.

Source§

type Output = VT

The returned type after indexing.
Source§

impl<KT: Hash + Eq, VT: Default> IndexMut<KT> for Hmap<KT, VT>

Source§

fn index_mut(&mut self, key: KT) -> &mut Self::Output

Provides ability to use syntax such as map[key] = val. This function will insert a new value into the table if necessary and assumes that the value type VT implements the Default trait.

Auto Trait Implementations§

§

impl<KT, VT> Freeze for Hmap<KT, VT>

§

impl<KT, VT> RefUnwindSafe for Hmap<KT, VT>

§

impl<KT, VT> Send for Hmap<KT, VT>
where KT: Send, VT: Send,

§

impl<KT, VT> Sync for Hmap<KT, VT>
where KT: Sync, VT: Sync,

§

impl<KT, VT> Unpin for Hmap<KT, VT>
where KT: Unpin, VT: Unpin,

§

impl<KT, VT> UnsafeUnpin for Hmap<KT, VT>

§

impl<KT, VT> UnwindSafe for Hmap<KT, VT>
where KT: UnwindSafe, VT: UnwindSafe,

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> 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, 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.