Data.BitSet

lib/Data/BitSet.gob

A set of the integers 0 .. capacity - 1, one bit each, in 64-bit words.

The capacity is fixed when the set is made, and the set costs a bit for every integer it could hold whether or not it holds it. That is the right trade where the elements are dense small indices -- the ids of one function's values -- and the wrong one for a sparse or unbounded set, which is what Data.Set is for. An element outside the capacity panics, rounded up to a whole word: the words are all there is to check against.

Bit n % 64 of word n / 64 holds n, so bit 63 is a word's sign bit. Nothing here reads a word as a number, only as bits, and the one arithmetic shift is masked back to six bits.

type BitSet
fun new : fun(Int) -> BitSet
fun add : fun(BitSet, Int) -> Unit
fun remove : fun(BitSet, Int) -> Bool

Returns True if the element was present.

fun contains : fun(BitSet, Int) -> Bool
fun isEmpty : fun(BitSet) -> Bool
fun clear : fun(BitSet) -> Unit
fun unionInto : fun(BitSet, BitSet) -> Bool

Adds every element of from to into, and says whether that added any. The two must have the same capacity.

fun copyFrom : fun(BitSet, BitSet) -> Unit

Makes into hold exactly the elements of from, of the same capacity.

fun equal : fun(BitSet, BitSet) -> Bool

Of two sets of the same capacity.

fun elements : fun(BitSet) -> Array Int

Ascending. Each word is walked by its lowest set bit, so the cost is per element and per word, not per bit.

Counted first so the answer is allocated once at its size: a caller that snapshots a set at every instruction otherwise pays for the array doubling its way up, which cost more allocations than the set itself saved.

instance Eq BitSet

Equal when they hold the same elements, whatever their capacities: a word one set has and the other does not must be empty.