#key #own #associative #element #remove #chooses

colony

A fast associative data-structure that chooses its own keys

3 releases (breaking)

0.3.0 Aug 4, 2023
0.2.0 Aug 4, 2023
0.1.0 Jul 31, 2023

#1914 in Data structures

MIT license

61KB
1.5K SLoC

Colony

Tests Crates.io Docs

An unordered data-structure with O(1) lookup, removal, iteration and O(1) amortized insertion. Like a faster HashMap that chooses its own keys. Also similar to a Vec<Option<T>>, where instead of calling Vec::remove elements are removed by setting the element to None.

See the documentation for more information.

This crate is partly a port of plf::colony, which is a proposed addition to the C++ standard library under the name std::hive.

Example

let mut colony = Colony::new();

// Insert
let foo_handle = colony.insert("foo");
let bar_handle = colony.insert("bar");

// Remove
assert_eq!(colony.remove(foo_handle), Some("foo"));

// Lookup
assert_eq!(colony.get(foo_handle), None);
assert_eq!(colony.get(bar_handle), Some(&"bar"));

// Iteration
for (key, &value) in colony.iter() {
    assert_eq!((key, value), (bar_handle, "bar"));
}

No runtime deps