ahtable
An array hash data structure where array is allocated up front with specific size and each element is assigned to each index based on hash function. Each element in an array is a Vec. Each collision of hash will be push into Vec. When number of added element reach certain size, it will scale the size of array by 2x and all old entry will be moved from old array to new one. All of this happen behind the scene. User will not effect by such operation.
How to use
- Use
ArrayHashBuilder::default() - Config all required specification of array hash with the builder
- Use
build()method on the instance to obtainArrayHash - Use following method to operate on the array
put()to put new data into array. This method always put or replace existing value.try_put()to put new data into array if it is not already exist.get()to retrieve a value that was put into it by a key of a type that is hashable and comparable to key.smart_get()to retrieve a value whenkeyis of smart pointer type. This will help reduce time processor required to construct a smart pointer of the same type as key itself.coerce_get()to retrieve a value whenkeyis of a type that can be borrowed as another type which isn't implementPartialEq<K>to the original type.remove()to remove a value out of this array by a key of a type that is hashable and comparable to key.smart_remove()to retrieve a value whenkeyis of smart pointer type. This will help reduce time processor required to construct a smart pointer of the same type as key itself.coerce_remove()to remove a value whenkeyis of a type that can be borrowed as another type which isn't implementPartialEq<K>to the original type.contains_iterto test whether current array have every entry that the iterator yield.to_builderto obtainArrayHashBuilderwith current spec out of existing array.iter()to iterate over the array entry.iter_mut()to iterate and mutate the array entry. This iterator shall not be used to mutate the entry key.drain()to obtain an iterator that keep draining all data from this array out.drain_with()to obtain an iterator that drain some specific entry out when predicate return true.split_by()which return another array and move all value that satisfy the predicate into new array.is_hasher_eq()to check whether two array have equivalence hasher.
Important notes
PartialEqofArrayHashneed both comparator and comparatee to be exactly the same. This includingHasherwhich must be seed by exactly the same number. The ideal usage is to fill largest array first then useArrayHash::to_builderto build second array. If it is impossible, consider construct anArrayHashthat is large enough to stored everything without reachingmax_load_factorthen useArrayHash::to_builderor clone thatArrayHashBuilderto build every array.- There's method
ArrayHash::is_hasher_eqto test whether two array can be compared. If two arrays are using different type of hasher, it will immediately yield compile error. If it use the same type of hasher but it use different seed, it will returnfalse. Otherwise, it is comparable via==operator. - Directly using
==operator on two arrays are safe. It will compile error similar toArrayHash::is_hasher_eq. In fact, inPartialEqimplementation, it useArrayHash::is_hasher_eqto check first if it is comparable. However, it will always return false if two array use different seed even if both array have exactly the same elements in it. - It is much faster to use
==operator to compare two arrays than usingArrayHash::contains_iterascontains_iterwill need to hash every key return by iter. Thecontains_itermethod is suitable in case where two arrays are using different hasher type or built from different ``ArrayHashBuilder`.
What's new
0.1.4
ArrayHashandArrayHashBuilderare now implementsHashandPartialEqArrayHash::is_hash_eqArrayHash::coerce_getandArrayHash::coerce_removethat accept a borrowed type that doesn't implementPartialEq<K>with the stored entryArrayHash::smart_removewhich is counterpart ofArrayHash::smart_getthat is usable when both storedkeyand query can be deref into the same type.- impl
core::convert::From<ArrayHash>forArrayHashBuilder. ArrayHash::to_builderto retrieve aArrayHashBuilderthat can build anArrayHashwith exactly same spec as currentArrayHash.ArrayHash::contains_iterthat check if this array contain every entry that given iter yield.
0.1.3
ArrayHash::getandArrayHash::removeparameter is now generic instead of fixing it to be the same type as the one being key. Now any types that implementsPartialEq<K>+Hashcan be used as parameter.ArrayHashkey and value no longer need to implement clone. The reason behind this is because there are two place where it need to clone key and value. Both of it is for purpose of allocatingVec. However, in both place, it need no actual clone on key nor value. It allocate an emptyVec. Therefore, cloning emptyVecwould have no different impact on performance comparing to looping to construct an emptyVec. With this reason, it would be easier for library user to have lesser number of constraint on key/value.