schm

Crates.ioschm
lib.rsschm
version0.1.2
sourcesrc
created_at2022-04-30 21:52:44.495983
updated_at2022-04-30 22:45:10.937798
descriptionSimplified HashMap implementation handling collision by separate chaining
homepage
repositoryhttps://github.com/splurf/schm
max_upload_size
id578360
size7,494
Evan Schwartzentruber (splurf)

documentation

README

schm

A basic example of hash collision using two strings:

use std::{
    collections::hash_map::DefaultHasher,
    hash::{Hash, Hasher},
};

const DEFAULT_CAPACITY: u64 = 17;

fn hash_to_index<T: Hash>(key: T) -> u64 {
    let mut state = DefaultHasher::new();
    key.hash(&mut state);
    state.finish() % DEFAULT_CAPACITY
}

fn main() {
    let a = hash_to_index("orange"); //  Calculates to '8'
    let b = hash_to_index("blueberry"); //  Calculates to '8'

    assert_eq!(a, b)
}

Here, collision is completely handled due to separate chaining:

use schm::HashMap;

fn main() {
    let mut map = HashMap::new();

    map.insert("orange", "ORANGE");
    map.insert("blueberry", "BLUEBERRY");

    assert_eq!(map.get("orange"), Some("ORANGE"));
    assert_eq!(map.get("blueberry"), Some("BLUEBERRY"));
}
Commit count: 0

cargo fmt