Class in collections::bitset coil-stdlib

BitSet

class BitSet
Import
use collections::bitset::{BitSet};
Source: 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#

NameDescription
with_capacityEmpty set with storage for at least bits indices (0 → no words).
newEmpty set with no word storage (grows on insert).

Methods#

NameDescription
sizeNumber of set bits (cardinality).
is_emptyReturns true when no bit is set.
capacityCurrent bit capacity (words.len() * 63); grows when inserting past it.
insertInsert index i; returns true when newly set.
containsTrue when index i is set.
removeClear index i; returns true when a bit was unset.
clearUnset 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);

See also#

↑ ↓ navigate ↵ open