BitSet
class BitSet Import
Source: use collections::bitset::{BitSet}; src/collections/bitset.hy A set of non-negative ints stored as bits.
Much smaller than a HashSet<int> when the values are dense, such as indices
or flags. Storage grows to fit the largest value inserted; capacities are
counted in bits. Each int word holds 63 bits.
Associated functions#
| Name | Description |
|---|---|
with_capacity | Empty set with storage for at least bits indices (0 → no words). |
new | Empty set with no word storage (grows on insert). |
Methods#
| Name | Description |
|---|---|
size | Number of set bits (cardinality). |
is_empty | Returns true when no bit is set. |
capacity | Current bit capacity (words.len() * 63); grows when inserting past it. |
insert | Insert index i; returns true when newly set. |
contains | True when index i is set. |
remove | Clear index i; returns true when a bit was unset. |
clear | Unset every bit; keeps allocated word capacity. |
Invariants#
Checked according to --contracts (see Contracts).
invariant self.count >= 0 && self.count <= self.words.len() * WORD_BITS
Examples#
use collections::bitset::{BitSet};
use tests::assert;
let seen = BitSet::new();
seen.insert(3);
seen.insert(200);
assert!(seen.contains(200));
assert!(!seen.contains(4));
assert!(seen.size() == 2);