weak-table: weak hash maps and sets for Rust
This crate defines several kinds of weak hash maps and sets.
-
For a hash map where the keys are held by weak pointers and compared by key value, see
WeakKeyHashMap. -
For a hash map where the keys are held by weak pointers and compared by pointer, see
PtrWeakKeyHashMap. -
For a hash map where the values are held by weak pointers, see
WeakValueHashMap. -
For a hash map where the keys and values are both held by weak pointers and the keys are compared by value, see
WeakWeakHashMap. -
For a hash map where the keys and values are both held by weak pointers and the keys are compared by pointer, see
PtrWeakWeakHashMap. -
For a hash set where the elements are held by weak pointers and compared by element value, see
WeakHashSet. -
For a hash set where the elements are held by weak pointers and compared by pointer, see
PtrWeakHashSet.
To add support for your own weak pointers, see
the traits WeakElement and WeakKey.
Rust version support
This crate supports Rust version 1.46 and later.
Crate features
weak-table is built with the std feature, which enables
functionality dependent on the std library, enabled by default.
Optionally, the following dependency may be enabled:
ahash: useahash’s hasher by default rather than thestdhasher. (This feature may be disabled in the future for security reasons.)
If the std feature is disabled (for no_std)
then the ahash dependency must be enabled.
Asymptotic complexity
Most operations have documented asymptotic time complexities. When time complexities are given in big-O notation, the following parameters are used consistently:
- n: the capacity of the map or set being accessed or constructed
- m: the capacity of a second map/set involved in a submap/subset operation
- p: the length of the probe sequence for the key in question
Note that p ∈ O(n), but we expect it to be O(1).
Time complexities for insertion operations are amortized. (This means that when we say that insertions run in O(p) time, we do not preclude the possibility that some insertions (ones where the table needs to be resized) will take longer: we only guarantee that such insertions are infrequent enough that the average insertion will take only O(p).)
Examples
Here we create a weak hash table mapping strings to integers.
Note that after dropping one, the key "one" is no longer present in the map.
This is because the map holds the strings as std::sync::Weak<str>s.
use WeakKeyHashMap;
use ;
let mut table = new;
let one = from;
let two = from;
table.insert;
assert_eq!;
assert_eq!;
table.insert;
*table.get_mut.unwrap += 10;
assert_eq!;
assert_eq!;
drop;
assert_eq!;
assert_eq!;
Here we use a weak hash set to implement a simple string interning facility:
use WeakHashSet;
use Deref;
use ;
;
;
# interning_test;