natbitset/lib.rs
1//! A super-lightweight bitset implementation for positive integers.
2//!
3//! ## Rationale
4//!
5//! You might need this struct if:
6//!
7//! - You need to efficiently represent *all* integers in a range `1..=N`
8//! - You need to perform set-like operations on those integers
9//! - You need better efficiency than `HashSet<usize>`, `HashSet<u8>`, etc.
10//!
11//! An example use case will illustrate most clearly.
12//!
13//! When solving a Sudoku puzzle, we usually ‘mark’ possible digits for a cell, e.g. “This cell could contain a 1 or 3, but nothing else.” The most intuitive way to represent this would be a `Hashset<usize>` or `Hashset<u8>`, but we can do better.
14//!
15//! [`Bitset(z)`](Bitset) is a thin-as-possible abstraction that behaves exactly like a `Hashset<usize>`, but behind-the-scenes only needs to store a *single* number `z`. When written in binary, the bits of `z` represent bitflags for each of the integers in `1..=N` – `1` if the integer is present in the set, `0` if not.
16//!
17//! For instance, looking at `Bitset(0b_0000_1011)`, the `1` bits are in positions 1, 2 and 4:
18//!
19//! ```text
20//! # | 8765 4321
21//! 0b | 0000 1011
22//! ^ ^^
23//! ```
24//!
25//! This means this `Bitset` represents the set {1, 2, 4}.
26//!
27//! If you’ve used enum bitflags in C#, TypeScript, etc. this is intended to work exactly like those, but specifically for a range of integers `1..=N`.
28//!
29//! ## Usage
30//!
31//! See [`Bitset`](Bitset#usage) for guidance on how to use the struct.
32
33#[allow(dead_code)]
34#[allow(unused_parens)]
35
36mod natbitset;
37pub use natbitset::{
38 Bitset,
39};