pub struct ArrayHash<H, K, V>{ /* private fields */ }Expand description
An implementation of ArrayHash in pure Rust.
ArrayHash is a data structure where the index is determined by hash and
each entry in array is a Vec that store data and all it collision.
Oritinal paper can be found [here](Askitis, N. & Zobel, J. (2005), Cache-conscious collision resolution for string hash tables, in ‘Proc. SPIRE String Processing and Information Retrieval Symp.’, Springer-Verlag, pp. 92–104)
In this implementation, user can supplied their own choice of hasher but it need to implement Clone.
The data can be anything that implement Hash.
The default Hasher, if not provided, will be XxHash64.
Implementations§
Source§impl<H, K, V> ArrayHash<H, K, V>
impl<H, K, V> ArrayHash<H, K, V>
Sourcepub fn to_builder(&self) -> ArrayHashBuilder<H>
pub fn to_builder(&self) -> ArrayHashBuilder<H>
Make a builder with default specification equals to current specification of this array.
Note: The current specification of array may be different from the spec used to create
the array. For example, if original max_load_factor is 2 but 3 elements were put
into array, the max_load_factor will be 4 and the bucket_size will be double of
the original bucket_size.
Sourcepub fn is_hasher_eq<K2, V2>(&self, rhs: &ArrayHash<H, K2, V2>) -> bool
pub fn is_hasher_eq<K2, V2>(&self, rhs: &ArrayHash<H, K2, V2>) -> bool
Check if two array use the same hasher with exactly same seed.
This mean that value A == B on these two array will have exactly the same hash value.
Sourcepub fn try_put(&mut self, key: K, value: V) -> Result<&V, (K, V, &V)>
pub fn try_put(&mut self, key: K, value: V) -> Result<&V, (K, V, &V)>
Try to put value into this ArrayHash.
If the given key is already in used, leave old entry as is
and return key/value along with current reference to value associated with the key.
Otherwise, add entry to this ArrayHash and return reference to current value.
§Parameter
entry- A tuple of (key, value) to be add to this.
§Return
It return Ok(&V) if key/value were put into this collection.
Otherwise, it return Err((K, V, &V)) where K is given key,
V is given value, and &V is reference to current value associated
with given key
Sourcepub fn get<T>(&self, key: &T) -> Option<&V>
pub fn get<T>(&self, key: &T) -> Option<&V>
Get a value of given key from this ArrayHash relying on contractual
implementation of PartialEq and Hash where following contract applied:
- for any
A == BthenB == Athen hash ofA== hash ofB - for any
A == BandB == CthenA == Cthen hash ofA== hash ofB== hash ofC
§Parameter
key- A key to look for.
§Return
An Option contains a value or None if it is not found.
Sourcepub fn smart_get<T, Q>(&self, key: Q) -> Option<&V>
pub fn smart_get<T, Q>(&self, key: Q) -> Option<&V>
Get a value using deref type.
This is usable only if the key is a type of smart pointer that can be deref into another type
which implement Hash and PartialEq.
For example, if K is Box<[u8]>, you can use &[u8] to query for a value
§Parameter
key - Any type that implement Deref where type after Deref is that same type
as actual type of key beneath type K.
§Return
Some(&V) if key exist in this table. Otherwise None.
Sourcepub fn coerce_get<T>(&self, key: &T) -> Option<&V>
pub fn coerce_get<T>(&self, key: &T) -> Option<&V>
Get a value of given key from this ArrayHash relying on contractual
implementation of PartialEq and Hash where following contract applied:
Bcan be borrowed as typeA- for any
A == BthenB == Athen hash ofA== hash ofB - for any
A == BandB == CthenA == Cthen hash ofA== hash ofB== hash ofC - for any
&A == &BthenA == B
It is useful for case where the stored key and query key is different type but the stored key
can be borrow into the same type as query. For example, stored Vec<T> but query with &[T].
It isn’t possible to use get method as [T]
isn’t implement PartialEq<Vec<T>>.
§Parameter
key- A key to look for.
§Return
An Option contains a value or None if it is not found.
Sourcepub fn remove<T>(&mut self, key: &T) -> Option<(K, V)>
pub fn remove<T>(&mut self, key: &T) -> Option<(K, V)>
Attempt to remove entry with given key from this ArrayHash relying on contractual
implementation of PartialEq and Hash where following contract applied:
- for any
A == BthenB == Athen hash ofA== hash ofB - for any
A == BandB == CthenA == Cthen hash ofA== hash ofB== hash ofC
§Parameter
key- A key of entry to be remove.
§Return
Option that contain tuple of (key, value) or None of key is not found
Sourcepub fn smart_remove<Q, T>(&mut self, key: Q) -> Option<(K, V)>
pub fn smart_remove<Q, T>(&mut self, key: Q) -> Option<(K, V)>
Attempt to remove entry with given key from this ArrayHash.
This is usable only if the key is a type of smart pointer that can be deref into another type
which implement Hash and PartialEq.
For example, if K is Box<[u8]>, you can use &[u8] to remove it.
§Parameter
key- A key of entry to be remove.
§Return
Option that contain tuple of (key, value) or None of key is not found
Sourcepub fn coerce_remove<T>(&mut self, key: &T) -> Option<(K, V)>
pub fn coerce_remove<T>(&mut self, key: &T) -> Option<(K, V)>
Attempt to remove entry with given key from this ArrayHash relying on contractual
implementation of PartialEq and Hash where following contract applied:
Bcan be borrowed as typeA- for any
A == BthenB == Athen hash ofA== hash ofB - for any
A == BandB == CthenA == Cthen hash ofA== hash ofB== hash ofC - for any
&A == &BthenA == B
It is useful for case where the stored key and query key is different type but the stored key
can be borrow into the same type as query. For example, stored Vec<T> but query with &[T].
It isn’t possible to use remove method as [T]
isn’t implement PartialEq<Vec<T>>.
§Parameter
key- A key of entry to be remove.
§Return
Option that contain tuple of (key, value) or None of key is not found
Sourcepub fn contains_iter<'a, I>(&self, other: I) -> bool
pub fn contains_iter<'a, I>(&self, other: I) -> bool
Sourcepub fn iter(&self) -> ArrayHashIterator<'_, K, V> ⓘ
pub fn iter(&self) -> ArrayHashIterator<'_, K, V> ⓘ
Get an iterator over this ArrayHash.
§Return
ArrayHashIterator that return reference
to each entry in this ArrayHash
Sourcepub fn iter_mut(&mut self) -> ArrayHashIterMut<'_, K, V> ⓘ
pub fn iter_mut(&mut self) -> ArrayHashIterMut<'_, K, V> ⓘ
Get a mutable iterator over this ArrayHash.
Warning, you shall not modify the key part of entry. If you do, it might end up accessible only by iterator but not with get.
§Return
ArrayHashIterMut that return mutably reference
to each entry in this ArrayHash
Sourcepub fn drain_with<F>(&mut self, pred: F) -> DrainWithIter<'_, F, K, V> ⓘ
pub fn drain_with<F>(&mut self, pred: F) -> DrainWithIter<'_, F, K, V> ⓘ
Return an iterator that drain some entry out of this ArrayHash.
After the iterator is done, this ArrayHash size will be shrink.
This method will return an iterator where each element it drain will cause a size deduction on this ArrayHash.
§Return
DrainWithIter - An iterator that will drain all element out of this ArrayHash.
Sourcepub fn split_by<F>(&mut self, pred: F) -> ArrayHash<H, K, V>
pub fn split_by<F>(&mut self, pred: F) -> ArrayHash<H, K, V>
Split this ArrayHash by given predicate closure. Every element that closure evaluate to true will be remove from this ArrayHash and return in new instance of ArrayHash.
This is different from using drain_with to drain some element into another ArrayHash by this method will return ArrayHash with exactly identical property, i.e. Hasher, buckets_size, and max_load_factor, whereas drain_with let caller instantiate ArrayHash yourself.
Since the instant it returns, use the same Hasher. It is safe to assume that all elements shall reside in the same bucket number thus this method speed up the split by ignore hashing altogether and store the entry directly into the same bucket number as in this ArrayHash
§Parameter
pred - A closure that evaluate an entry. If it return true, the entry shall be moved into a new
ArrayHash.
§Return
An ArrayHash that contains all entry that pred evaluate to true.
Trait Implementations§
Source§impl<'a, H, K, V> From<&'a ArrayHash<H, K, V>> for ArrayHashBuilder<H>
Make ArrayHashBuilder with spec from existing ArrayHash.
impl<'a, H, K, V> From<&'a ArrayHash<H, K, V>> for ArrayHashBuilder<H>
Make ArrayHashBuilder with spec from existing ArrayHash.
This is useful for creating an object of array hash that may be later compare to baseline array for equality.
Source§fn from(ah: &'a ArrayHash<H, K, V>) -> ArrayHashBuilder<H>
fn from(ah: &'a ArrayHash<H, K, V>) -> ArrayHashBuilder<H>
Source§impl<H, K, V> Hash for ArrayHash<H, K, V>
Implement hash by using only K and V pair to calculate hash.
It will still conform to following contract:
For any AH1 == AH2, hash(AH1) == hash(AH2).
impl<H, K, V> Hash for ArrayHash<H, K, V>
Implement hash by using only K and V pair to calculate hash.
It will still conform to following contract:
For any AH1 == AH2, hash(AH1) == hash(AH2).
This is because the PartialEq implementation check if both side have
the same size, same hasher, same number of items, and exactly the same
key and value pair returned by each yield of both AH1’s and AH2’s iterator.
However, hasher contract need no symmetric property.
It mean that hash(AH1) == hash(AH2) doesn’t imply that AH1 == AH2.
Thus, we will have the same hash if we iterate through array and hash both key and value
for each yield on both AH1 and AH2.
Source§impl<H, K, V> IntoIterator for ArrayHash<H, K, V>
impl<H, K, V> IntoIterator for ArrayHash<H, K, V>
Source§impl<'a, H, K, V> IntoIterator for &'a ArrayHash<H, K, V>
impl<'a, H, K, V> IntoIterator for &'a ArrayHash<H, K, V>
Source§impl<'a, H, K, V> IntoIterator for &'a mut ArrayHash<H, K, V>
impl<'a, H, K, V> IntoIterator for &'a mut ArrayHash<H, K, V>
Source§impl<H, K1, V1, K2, V2> PartialEq<ArrayHash<H, K1, V1>> for ArrayHash<H, K2, V2>
Generalize implementation that let two array hash of different type of key and value to be
comparable.
It requires that both side must use the same hasher with exactly same seed.
It rely on a contract that if any K1 == K2 then it mean those two hash is also
equals.
impl<H, K1, V1, K2, V2> PartialEq<ArrayHash<H, K1, V1>> for ArrayHash<H, K2, V2>
Generalize implementation that let two array hash of different type of key and value to be
comparable.
It requires that both side must use the same hasher with exactly same seed.
It rely on a contract that if any K1 == K2 then it mean those two hash is also
equals.