Crates.io | implhm |
lib.rs | implhm |
version | 1.0.8 |
source | src |
created_at | 2022-05-01 06:01:53.41759 |
updated_at | 2022-05-19 18:43:47.026661 |
description | Simplified library of collision-handling HashMaps |
homepage | |
repository | https://github.com/splurf/implhm |
max_upload_size | |
id | 578484 |
size | 44,898 |
Place implhm in your Cargo.toml
:
[dependencies]
implhm = "1.0.8"
There are several different methods for handling collision. implhm provides the most basic implementations. The following features are available:
Here is an example of using a single feature:
[dependencies]
implhm = { version = "1.0.8", default-features = false, features = ["quadratic-probing"] }
A basic example of hash collision using two strings:
use std::{
collections::hash_map::DefaultHasher,
hash::{Hash, Hasher},
};
fn hash<T: Hash>(key: T) -> u64 {
let mut state = DefaultHasher::new();
key.hash(&mut state);
state.finish() % 17
}
fn main() {
/*
When passed through the `hash` function,
`orange` and `blueberry` both equal `8`
*/
let a = hash("orange");
let b = hash("blueberry");
/*
If *collision* isn't handled, then the *value*
("orange") at the location of the *key* (`8`)
would be replaced with the *value* ("blueberry")
*/
assert_eq!(a, b)
}
Here, collision is completely handled by separate chaining:
use implhm::{Map, MapMut, SCHashMap};
fn main() {
let mut map = SCHashMap::default();
map.insert("orange", "ORANGE");
map.insert("blueberry", "BLUEBERRY");
/*
In the case of *separate chaining*, collision is
handled by placing any key-pairs that calculate to
the same hash into an ordered list at that index.
*/
assert_eq!(map.get("orange"), Some(&"ORANGE"));
assert_eq!(map.get("blueberry"), Some(&"BLUEBERRY"));
}