BitSet
UpdatedBitSetimport { BitSet } from "@alloy/std/collections" local visited = new BitSet(1024) visited:add(7):add(40) if visited:has(7) then end for i in visited do print(i) end
A set of whole numbers from 0 up, from the std, with one bit per number in a buffer. A Set<number> spends a table slot on each member. A BitSet spends one bit on each number up to its largest member, and add and has run about 2x faster. It suits dense numbers: tile indices, entity ids, visited flags. new BitSet(size) makes room for the numbers below size, and a larger number grows the buffer. for i in bits walks the members in rising order.
Statics
BitSet.new
BitSet.new(size: number?): BitSet
An empty set with room for the numbers below size, or below 256 without it. A larger number grows the buffer, so size only saves the copies.
import { BitSet } from "@alloy/std/collections" local visited = new BitSet(1024) visited:add(7)
BitSet.from
BitSet.from(indices: { number }): BitSet
A new set of the numbers in an array. A repeated number joins once.
import { BitSet } from "@alloy/std/collections" local bits = BitSet.from({ 1, 2, 2, 9 }) print(bits:len())
Methods
BitSet:add
BitSet:add(index: number): BitSet
Adds index and returns the set, so calls chain. The number must be whole and 0 or more, or the call raises.
import { BitSet } from "@alloy/std/collections" local bits = new BitSet() bits:add(3):add(5) print(bits:has(5))
BitSet:has
BitSet:has(index: number): boolean
Whether index is a member. A number below 0, past the buffer, or with a fraction is not one.
import { BitSet } from "@alloy/std/collections" local bits = BitSet.from({ 4 }) print(bits:has(4))
BitSet:remove
BitSet:remove(index: number): boolean
Removes index and says whether it was there.
import { BitSet } from "@alloy/std/collections" local bits = BitSet.from({ 4 }) print(bits:remove(4))
BitSet:len
BitSet:len(): number
The number of members. The set counts as members come and go, so this costs the same at any size.
import { BitSet } from "@alloy/std/collections" local bits = BitSet.from({ 1, 2, 3 }) print(bits:len())
BitSet:is_empty
BitSet:is_empty(): boolean
Whether the set holds no members.
import { BitSet } from "@alloy/std/collections" local bits = new BitSet() print(bits:is_empty())
BitSet:clear
BitSet:clear()
Removes every member. The buffer keeps its size.
import { BitSet } from "@alloy/std/collections" local bits = BitSet.from({ 1, 2 }) bits:clear()
BitSet:union
BitSet:union(other: BitSet): BitSet
A new set of the members of both, built 32 bits at a time. Neither input changes.
import { BitSet } from "@alloy/std/collections" local a = BitSet.from({ 1, 2 }) local b = BitSet.from({ 2, 3 }) print(a:union(b):len())
BitSet:intersection
BitSet:intersection(other: BitSet): BitSet
A new set of the members that both hold.
import { BitSet } from "@alloy/std/collections" local a = BitSet.from({ 1, 2 }) local b = BitSet.from({ 2, 3 }) print(a:intersection(b):len())
BitSet:difference
BitSet:difference(other: BitSet): BitSet
A new set of this set's members that other does not hold.
import { BitSet } from "@alloy/std/collections" local a = BitSet.from({ 1, 2 }) local b = BitSet.from({ 2, 3 }) print(a:difference(b):len())
BitSet:to_array
BitSet:to_array(): number[]
The members as an array, in rising order.
import { BitSet } from "@alloy/std/collections" local bits = BitSet.from({ 9, 2 }) print(bits:to_array():join(", "))
