Uniquevec 0.1.0

A Vec-like datastructure which only contains unique entries. It is no_std and provides optional serde support.

// Create a new empty UniqueVec
use uniquevec::UniqueVec;
let mut uvec = UniqueVec::new();

// Fill it with contents
uvec.push("cellular");
uvec.push("_");
uvec.push("raza");

// The next entry is already contained so it will not be added again.
// We can also check the returned value
let r = uvec.push("cellular");
assert_eq!(r, Some("cellular"));

// Otherwise we can use it similarly to a Vec
assert_eq!(uvec[0], "cellular");
assert_eq!(uvec[1], "_");
assert_eq!(uvec[2], "raza");
assert_eq!(uvec.len(), 3);

for (n, entry) in uvec.into_iter().enumerate() {
    println!("{n}th entry: {entry}");
}

https://crates.io/crates/uniquevec

Looks like your implementation takes O(n2) time to process n elements. I'd expect this to use a HashSet for deduplication rather than linear search.

2 Likes

A HashSet requires Hash and Eq to be implemented which I would like to avoid. But Yes I could probably improve the performance in other ways.