HashTrieMap

Struct HashTrieMap 

Source
pub struct HashTrieMap<H: Hashword, F: Flagword<H>, K: Key, V: Value, M: HasherBv<H, K>>
where <F as TryFrom<<H as BitAnd>::Output>>::Error: Debug,
{ /* private fields */ }
Expand description

HashTrieMap implements a hash map using a hash array mapped trie (HAMT).

§Example Usage

use fnv::FnvHasher;
use hash_trie::{HashTrieMap, traits::HashLike};
use std::string::String;
 
#[derive(Clone,Debug,Eq,Hash,PartialEq)]
struct Str<'a> {
    s: &'a str
}
 
impl <'a> Str<'a> {
    fn new(s: &'a str) -> Self {
        Self {s}
    }
}
 
impl <'a> Default for Str<'a> {
    fn default() -> Self {
        Self {s: ""}
    }
}
impl <'a> From<Str<'a>> for String {
    fn from(s: Str<'a>) -> String {
        s.s.into()
    }
}
impl <'a> PartialEq<Str<'a>> for String {
    fn eq(&self, other: &Str<'a>) -> bool {
        *self == other.s
    }
}
impl <'a> HashLike<String> for Str<'a> {}
impl <'a> HashLike<Str<'a>> for String {}
unsafe impl <'a> Send for Str<'a> {}
unsafe impl <'a> Sync for Str<'a> {}
 
let mut hash_map: HashTrieMap<u64, u32, String, String, FnvHasher> = HashTrieMap::new();
let hello = "Hello,";
let world = "world!,";
 
hash_map = hash_map.insert(Str::new(hello), world, false).unwrap().0;
 
// Inserting an already-inserted key returns references to the key and value in the map...
assert!(hash_map.insert(Str::new(hello), "?", false).map(|_| ())
    .map_err(|key_value| *key_value.0 == hello && *key_value.1 == world).unwrap_err());
// ... unless you enable replacement.
assert!(hash_map.insert(Str::new(hello), "?", true).is_ok());
 
assert!(hash_map.find(&Str::new(hello)).map(|key_value| *key_value.0 == hello && *key_value.1 == world).unwrap());
 
match hash_map.remove(&Str::new(hello)) {
    Ok((mutated, key, value)) => {
        // Removing a key returns references to the key and
        // value in the set in addition to the mutated map.
        println!("Value stored in hash_map: ({}, {})", key, value);
        hash_map = mutated;
    },
    Err(_) => panic!(),
}

Implementations§

Source§

impl<H: Hashword, F: Flagword<H>, K: Key, V: Value, M: HasherBv<H, K>> HashTrieMap<H, F, K, V, M>
where <F as TryFrom<<H as BitAnd>::Output>>::Error: Debug,

Source

pub fn new() -> Self

Get a new, empty HashTrieMap.

Source

pub fn size(&self) -> usize

Get the total number of entries in the map.

Source

pub fn find<'a, L: Key + HashLike<K>>( &'a self, key: &L, ) -> Result<(&'a K, &'a V), HashTrieError>
where K: PartialEq<L>, M: HasherBv<H, L>,

Search the HashTrieMap for the given key and return references if found, or HashTrieError::NotFound if not found.

Source

pub fn insert<'a, L: Key + HashLike<K> + Into<K>, W: Into<V>>( &'a self, key: L, value: W, replace: bool, ) -> Result<(Self, *const K, *const V, Option<(&'a K, &'a V)>), (&'a K, &'a V)>
where K: HashLike<L> + PartialEq<L>, M: HasherBv<H, L>,

Search the HashTrieMap for the spot to insert the key and return both a mutated map and, if applicable, references to the replaced values. If found and replacement is disabled, references to the existing values are returned.

Source

pub fn remove<'a, L: Key + HashLike<K>>( &'a self, key: &L, ) -> Result<(Self, &'a K, &'a V), HashTrieError>
where K: PartialEq<L>, M: HasherBv<H, L>,

Search the HashTrieMap for the given key to remove and return a mutated map, or HashTrieError::NotFound if not found.

Source

pub fn visit<Op>(&self, op: Op)
where Op: Fn(&K, &V) + Clone,

Run an operation on each entry in the map.

Source

pub fn transform<ReduceT, ReduceOp, Op>( &self, reduce_op: ReduceOp, op: Op, ) -> (Self, ReduceT)
where Self: Sized, ReduceT: Default, ReduceOp: Fn(&ReduceT, &ReduceT) -> ReduceT + Clone, Op: Fn(&K, &V) -> MapTransformResult<V, ReduceT> + Clone,

Run a transform operation on each entry in the map. Returns the transformed map and a reduction of the secondary returns of the transform operations.

Source

pub unsafe fn transmute<S: Key + HashLike<K>, X: Value, ReduceT, ReduceOp, Op>( &self, reduce_op: ReduceOp, op: Op, ) -> (HashTrieMap<H, F, S, X, M>, ReduceT)
where Self: Sized, ReduceT: Default, ReduceOp: Fn(&ReduceT, &ReduceT) -> ReduceT + Clone, Op: Fn(&K, &V) -> MapTransmuteResult<S, X, ReduceT> + Clone, K: HashLike<S> + PartialEq<S>, M: HasherBv<H, S>,

Run a transmute operation on each entry in the map. Returns the transmuted map and a reduction of the secondary returns of the transmute operations.

Source

pub fn transform_with_transformed<ReduceT, ReduceOp, BothOp, LeftOp, RightOp>( &self, right: &Self, reduce_op: ReduceOp, both_op: BothOp, left_op: LeftOp, right_op: RightOp, ) -> (Self, ReduceT)
where Self: Sized, ReduceT: Default, ReduceOp: Fn(&ReduceT, &ReduceT) -> ReduceT + Clone, BothOp: Fn(&K, &V, &K, &V) -> MapJointTransformResult<V, ReduceT> + Clone, LeftOp: Fn(&K, &V) -> MapTransformResult<V, ReduceT> + Clone, RightOp: Fn(&K, &V) -> MapTransformResult<V, ReduceT> + Clone,

Run a transform operation on each entry or pair of entries in the maps. Returns the transformed map and a reduction of the secondary returns of the transmute operations. Can reuse nodes from either map.

Source

pub fn transform_with_transfuted<W: Value, ReduceT, ReduceOp, BothOp, LeftOp, RightOp>( &self, right: &HashTrieMap<H, F, K, W, M>, reduce_op: ReduceOp, both_op: BothOp, left_op: LeftOp, right_op: RightOp, ) -> (Self, ReduceT)
where Self: Sized, ReduceT: Default, ReduceOp: Fn(&ReduceT, &ReduceT) -> ReduceT + Clone, BothOp: Fn(&K, &V, &K, &W) -> MapTransformResult<V, ReduceT> + Clone, LeftOp: Fn(&K, &V) -> MapTransformResult<V, ReduceT> + Clone, RightOp: Fn(&K, &W) -> SetTransmuteResult<V, ReduceT> + Clone,

Run a transform/transmute operation on each entry or pair of entries in the maps. Returns the transmuted map and a reduction of the secondary returns of the transmute operations. Can reuse nodes from the transformed map. Like transform_with_transmuted but enforces identity transformations on keys.

Source

pub unsafe fn transform_with_transmuted<L, W: Value, ReduceT, ReduceOp, BothOp, LeftOp, RightOp>( &self, right: &HashTrieMap<H, F, L, W, M>, reduce_op: ReduceOp, both_op: BothOp, left_op: LeftOp, right_op: RightOp, ) -> (Self, ReduceT)
where Self: Sized, ReduceT: Default, ReduceOp: Fn(&ReduceT, &ReduceT) -> ReduceT + Clone, BothOp: Fn(&K, &V, &L, &W) -> MapTransformResult<V, ReduceT> + Clone, LeftOp: Fn(&K, &V) -> MapTransformResult<V, ReduceT> + Clone, RightOp: Fn(&L, &W) -> MapTransmuteResult<K, V, ReduceT> + Clone, K: HashLike<L> + PartialEq<L>, L: HashLike<K> + PartialEq<K> + Key, M: HasherBv<H, L>,

Run a transform/transmute operation on each entry or pair of entries in the maps. Returns the transmuted map and a reduction of the secondary returns of the transmute operations. Can reuse nodes from the transformed map.

Source

pub fn transfute_with_transformed<W: Value, ReduceT, ReduceOp, BothOp, LeftOp, RightOp>( &self, right: &HashTrieMap<H, F, K, W, M>, reduce_op: ReduceOp, both_op: BothOp, left_op: LeftOp, right_op: RightOp, ) -> (HashTrieMap<H, F, K, W, M>, ReduceT)
where Self: Sized, ReduceT: Default, ReduceOp: Fn(&ReduceT, &ReduceT) -> ReduceT + Clone, BothOp: Fn(&K, &V, &K, &W) -> MapTransformResult<W, ReduceT> + Clone, LeftOp: Fn(&K, &V) -> SetTransmuteResult<W, ReduceT> + Clone, RightOp: Fn(&K, &W) -> MapTransformResult<W, ReduceT> + Clone,

Run a transmute/transform operation on each entry or pair of entries in the maps. Returns the transmuted map and a reduction of the secondary returns of the transmute operations. Can reuse nodes from the transformed map. Like transmute_with_transformed but enforces identity transformations on keys.

Source

pub unsafe fn transmute_with_transformed<L, W: Value, ReduceT, ReduceOp, BothOp, LeftOp, RightOp>( &self, right: &HashTrieMap<H, F, L, W, M>, reduce_op: ReduceOp, both_op: BothOp, left_op: LeftOp, right_op: RightOp, ) -> (HashTrieMap<H, F, L, W, M>, ReduceT)
where Self: Sized, ReduceT: Default, ReduceOp: Fn(&ReduceT, &ReduceT) -> ReduceT + Clone, BothOp: Fn(&K, &V, &L, &W) -> MapTransformResult<W, ReduceT> + Clone, LeftOp: Fn(&K, &V) -> MapTransmuteResult<L, W, ReduceT> + Clone, RightOp: Fn(&L, &W) -> MapTransformResult<W, ReduceT> + Clone, K: HashLike<L> + PartialEq<L>, L: HashLike<K> + PartialEq<K> + Key, M: HasherBv<H, L>,

Run a transmute/transform operation on each entry or pair of entries in the maps. Returns the transmuted map and a reduction of the secondary returns of the transmute operations. Can reuse nodes from the transformed map.

Source

pub unsafe fn transmute_with_transmuted<L, W: Value, S: Key + HashLike<K>, X: Value, ReduceT, ReduceOp, BothOp, LeftOp, RightOp>( &self, right: &HashTrieMap<H, F, L, W, M>, reduce_op: ReduceOp, both_op: BothOp, left_op: LeftOp, right_op: RightOp, ) -> (HashTrieMap<H, F, S, X, M>, ReduceT)
where Self: Sized, ReduceT: Default, ReduceOp: Fn(&ReduceT, &ReduceT) -> ReduceT + Clone, BothOp: Fn(&K, &V, &L, &W) -> MapTransmuteResult<S, X, ReduceT> + Clone, LeftOp: Fn(&K, &V) -> MapTransmuteResult<S, X, ReduceT> + Clone, RightOp: Fn(&L, &W) -> MapTransmuteResult<S, X, ReduceT> + Clone, K: HashLike<L> + PartialEq<L> + HashLike<S> + PartialEq<S>, L: HashLike<K> + PartialEq<K> + HashLike<S> + PartialEq<S> + Key, M: HasherBv<H, L> + HasherBv<H, S>,

Run a transmute operation on each entry or pair of entries in the maps. Returns the transmuted map and a reduction of the secondary returns of the transmute operations.

Trait Implementations§

Source§

impl<H: Clone + Hashword, F: Clone + Flagword<H>, K: Clone + Key, V: Clone + Value, M: Clone + HasherBv<H, K>> Clone for HashTrieMap<H, F, K, V, M>
where <F as TryFrom<<H as BitAnd>::Output>>::Error: Debug,

Source§

fn clone(&self) -> HashTrieMap<H, F, K, V, M>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<H: Debug + Hashword, F: Debug + Flagword<H>, K: Debug + Key, V: Debug + Value, M: Debug + HasherBv<H, K>> Debug for HashTrieMap<H, F, K, V, M>
where <F as TryFrom<<H as BitAnd>::Output>>::Error: Debug,

Source§

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

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

impl<H: Hashword, F: Flagword<H>, K: Key, V: Value, M: HasherBv<H, K>> Default for HashTrieMap<H, F, K, V, M>
where <F as TryFrom<<H as BitAnd>::Output>>::Error: Debug,

Source§

fn default() -> Self

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

impl<H: Hashword, F: Flagword<H>, K: Key, V: Value, M: HasherBv<H, K>> PartialEq for HashTrieMap<H, F, K, V, M>
where <F as TryFrom<<H as BitAnd>::Output>>::Error: Debug,

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<H: Hashword, F: Flagword<H>, K: Key, V: Value, M: HasherBv<H, K>> Eq for HashTrieMap<H, F, K, V, M>
where <F as TryFrom<<H as BitAnd>::Output>>::Error: Debug,

Auto Trait Implementations§

§

impl<H, F, K, V, M> Freeze for HashTrieMap<H, F, K, V, M>

§

impl<H, F, K, V, M> !RefUnwindSafe for HashTrieMap<H, F, K, V, M>

§

impl<H, F, K, V, M> !Send for HashTrieMap<H, F, K, V, M>

§

impl<H, F, K, V, M> !Sync for HashTrieMap<H, F, K, V, M>

§

impl<H, F, K, V, M> Unpin for HashTrieMap<H, F, K, V, M>

§

impl<H, F, K, V, M> !UnwindSafe for HashTrieMap<H, F, K, V, M>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> HashLike<T> for T