Struct HashMap

Source
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>
where K: Debug + Eq + Hash + PartialEq, V: Debug,

Source

pub fn new() -> Self

Constructor for an empty map with a default capacity of 2.

Source

pub fn new_with_capacity(size: usize) -> Self

Constructor for an empty map with a given capacity.

Source

pub fn len(&self) -> usize

Returns the number of active entries in the map in O(1) time.

Source

pub fn occupied(&self) -> usize

Returns the total number of entries in the map (active + deleted entries) in O(n) time.

Source

pub fn deleted(&self) -> usize

Returns the total number of deleted entries in the map in O(n) time.

Source

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().

Source

pub fn is_empty(&self) -> bool

Returns true if the map is either empty or contains only empty slots and deleted entries.

Source

pub fn contents(&self)

Pretty-prints the map’s contents.

Source

pub fn contains<Q>(&self, key: &Q) -> bool
where K: Borrow<Q>, Q: Debug + Hash + Eq + ?Sized,

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.

Source

pub fn get<Q>(&self, key: &Q) -> Option<&V>
where K: Borrow<Q>, Q: Debug + Hash + Eq + ?Sized,

Returns a reference to the value associated with a key, if the key exists.

Source

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.

Source

pub fn try_put(&mut self, key: K, value: V)

Source

pub fn mut_val_or<F>(&mut self, key: K, f: F, default: V)
where F: FnOnce(&mut V), K: Default,

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);
}
Source

pub fn remove<Q>(&mut self, key: &Q) -> Option<Entry<K, V>>
where K: Borrow<Q>, Q: Debug + Hash + Eq + ?Sized,

Removes and returns an entry from the map for a given key, if it exists in the map.

Source

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.

Source

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.

Source

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);
}
Source

pub fn keys(&self) -> Keys<'_, K, V>

Returns an iterator over borrowed keys only. The keys appear in random order.

Example use:

for e in map.keys() {
    println!("{e}");
}
Source

pub fn values(&self) -> Values<'_, K, V>

Returns an iterator over borrowed values only. The values appear in random order.

Example use:

for e in map.values() {
    println!("{e}");
}

Trait Implementations§

Source§

impl<K: Debug, V: Debug> Debug for HashMap<K, V>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K, V> Default for HashMap<K, V>
where K: Debug + Default + Eq + Hash + PartialEq, V: Default + PartialEq + Debug,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<K, V> IntoIterator for HashMap<K, V>

Source§

type Item = (K, V)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<K, V>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<K, V> Freeze for HashMap<K, V>

§

impl<K, V> RefUnwindSafe for HashMap<K, V>

§

impl<K, V> Send for HashMap<K, V>
where K: Send, V: Send,

§

impl<K, V> Sync for HashMap<K, V>
where K: Sync, V: Sync,

§

impl<K, V> Unpin for HashMap<K, V>
where K: Unpin, V: Unpin,

§

impl<K, V> UnwindSafe for HashMap<K, V>
where K: UnwindSafe, V: 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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V