A super-lightweight set implementation for consecutive natural numbers 1..=N.
Bitset is a drop-in replacement for HashSet<u8>. You might want this if you need:
- To efficiently represent integers in a range
1..=N - To perform set-like operations on those integers
- Higher speeds and lower memory usage than
HashSet<u8>
natbitset is available on crates.io↗! Add to your Rust project:
> cargo add natbitsetImport the Bitset struct:
use natbitset::Bitset;
let bitset = Bitset::<9, u16>::from([1, 3, 7]);For guidance on how to use the struct, and the details behind its implementation, please visit the documentation on docs.rs↗.
Flexible instantiation:
let left = Bitset::<4>::none();
let right = Bitset::<4>::all();Implements the same methods as HashSet, and then some:
left.insert(1);
right.remove(4);
let _ = left.intersect_nonempty(right);
let _ = left.maximum();
let _ = left.is_single();Supports bitwise operations for concise syntax:
left & right // intersection
left | right // union
left / right // differenceEnforce domain with const generic type parameter, and specify backing type for different use cases:
let bitset = Bitset::<9, u16>::from([1, 2, 4]);- Working on a growable set structure!
Bitset(z) represents the set with a single integer z. The whole data structure is 1 number! This makes it much faster and lighter than a HashSet<u8>, especially at scale.
Warning
These benchmarks are purely illustrative – results will of course vary hugely depending on environment, background processes, etc. Please use them only as a reference to the differences in magnitude of performance.
| operation | N | bitset | hashset | unit | description |
|---|---|---|---|---|---|
| construction | 9 | ~100 | ~100,000 | ps | Constructing a set with members 1..=N using FromIterator |
| 65536 | ~10 | ~1,000 | μs | ||
| intersection | 3 | ~100 | ~100,000 | ps | Intersecting 2 sets of size N |
| 42069 | ~100 ps | ~1 ms |