A bitset of unsigned 32-bit ids over 65,536-id chunks, each a sorted id array
or an 8 KB bitmap.
This is the recommended layout: memory follows the ids actually stored, so a
caller that does not yet know how far its ids range does not pay for the span.
Chunked containers also make isDisjointFrom and intersectionSize stop at
the first chunk that decides them.
@param ― options - Optional items, added as if by add.
@throws ― {BitsetInvalidInputError} With code INVALID_ID when an item is not in 0 … BITSET_MAX_ID.
, restoring the layout the
artifact was serialized from.
@throws ― {BitsetParseError} With a code from BITSET_PARSE_ERROR_CODES when the
payload is malformed, truncated, unsupported, over budget, or internally
inconsistent.
Serialize a bitset into canonical binary bytes that restore the same layout.
The payload is the Roaring format's no-run layout — sorted chunk keys, a
cardinality per chunk, then each chunk as a sorted 16-bit id run or a bitmap —
inside a knotkit/wire envelope. The bytes are Knotkit's own framing and are
not RoaringFormatSpec-compatible.
@throws ― {BitsetInvalidInputError} With code FOREIGN_BITSET when set was not
produced by this package, or SERIALIZATION_LIMIT_EXCEEDED when a dense
bitset's bitmap would exceed BITSET_MAX_DECODE_DENSE_BYTES.
A bitset of unsigned 32-bit ids over 65,536-id chunks, each a sorted id array
or an 8 KB bitmap.
This is the recommended layout: memory follows the ids actually stored, so a
caller that does not yet know how far its ids range does not pay for the span.
Chunked containers also make isDisjointFrom and intersectionSize stop at
the first chunk that decides them.
@param ― options - Optional items, added as if by add.
@throws ― {BitsetInvalidInputError} With code INVALID_ID when an item is not in 0 … BITSET_MAX_ID.
@example
constseen=roaringBitset({ items: [3, 65_536] })
seen.has(3) // true
seen.size // 2
roaringBitset({
BitsetOptions.items?: Iterable<number>|undefined
Ids to add in iteration order as the bitset is built, as if by add.
A Bitset from this package is copied container by container instead of id
by id, which also makes it the way to take an untouched copy of another
bitset.
Serialize a bitset into canonical binary bytes that restore the same layout.
The payload is the Roaring format's no-run layout — sorted chunk keys, a
cardinality per chunk, then each chunk as a sorted 16-bit id run or a bitmap —
inside a knotkit/wire envelope. The bytes are Knotkit's own framing and are
not RoaringFormatSpec-compatible.
@throws ― {BitsetInvalidInputError} With code FOREIGN_BITSET when set was not
produced by this package, or SERIALIZATION_LIMIT_EXCEEDED when a dense
bitset's bitmap would exceed BITSET_MAX_DECODE_DENSE_BYTES.
, restoring the layout the
artifact was serialized from.
@throws ― {BitsetParseError} With a code from BITSET_PARSE_ERROR_CODES when the
payload is malformed, truncated, unsupported, over budget, or internally
inconsistent.
deserializeBitset(
constbytes:Uint8Array<ArrayBufferLike>
bytes)
constrestored:Bitset
restored.
Bitset.kind: "roaring"|"dense"
The container layout this bitset uses: 'roaring' or 'dense'.
kind// 'roaring'
constrestored:Bitset
restored.
Bitset.has: (id:number) => boolean
Whether id is stored. Total: returns false for any value that is not an
id (-1, 1.5, NaN, a string, 2 ** 32), as Set.prototype.has does
for a value it does not hold. O(1) for dense; O(log c + log n) for a
roaring array container and O(log c) for a bitmap container, where c
is the number of occupied chunks and n a container's cardinality.
has(42) // true
constrestored:Bitset
restored.
Bitset.size: number
Number of stored ids. O(1). The bitset is empty when this is 0.
A bitset of unsigned 32-bit ids over 65,536-id chunks, each a sorted id array
or an 8 KB bitmap.
This is the recommended layout: memory follows the ids actually stored, so a
caller that does not yet know how far its ids range does not pay for the span.
Chunked containers also make isDisjointFrom and intersectionSize stop at
the first chunk that decides them.
@param ― options - Optional items, added as if by add.
@throws ― {BitsetInvalidInputError} With code INVALID_ID when an item is not in 0 … BITSET_MAX_ID.
, restoring the layout the
artifact was serialized from.
@throws ― {BitsetParseError} With a code from BITSET_PARSE_ERROR_CODES when the
payload is malformed, truncated, unsupported, over budget, or internally
inconsistent.
Serialize a bitset into canonical binary bytes that restore the same layout.
The payload is the Roaring format's no-run layout — sorted chunk keys, a
cardinality per chunk, then each chunk as a sorted 16-bit id run or a bitmap —
inside a knotkit/wire envelope. The bytes are Knotkit's own framing and are
not RoaringFormatSpec-compatible.
@throws ― {BitsetInvalidInputError} With code FOREIGN_BITSET when set was not
produced by this package, or SERIALIZATION_LIMIT_EXCEEDED when a dense
bitset's bitmap would exceed BITSET_MAX_DECODE_DENSE_BYTES.
serializeBitset } from'knotkit/bitset/wire'
import {
functionsplitmix32(seed:number): () =>number
Creates a deterministic 32-bit pseudorandom number generator.
Each call advances a private state by 0x9e3779b9. This constant equals
floor(2^32 / phi), where phi is the golden ratio. Addition wraps modulo
2^32. Because the increment is odd, it is coprime to 2^32. The resulting
Weyl sequence visits every 32-bit state once before it repeats.
The mixing function uses the constants 0x21f0aaad and 0x735a2d97 from
the
search
for low avalanche bias. Its XOR-shifts and multiplications by odd integers
are invertible over 32-bit values. The mixer therefore preserves the full
period while diffusing nearby states across the output bits.
Math.imul and the bitwise operators keep the mixing steps in 32-bit
arithmetic. The final unsigned conversion produces an integer from 0 to
2^32 - 1. Dividing by 2^32 maps that integer to [0, 1).
The SplitMix family was introduced by Steele, Lea, and Flood in
Return a uniformly shuffled copy of values using Fisher-Yates.
Runs in O(values.length) time and uses O(values.length) space for the copy.
The input is never mutated.
@throws ― {PrngInvalidInputError} With code INVALID_SOURCE_VALUE when the
random source returns a value outside [0, 1).
shuffled(
functionsplitmix32(seed:number): () =>number
Creates a deterministic 32-bit pseudorandom number generator.
Each call advances a private state by 0x9e3779b9. This constant equals
floor(2^32 / phi), where phi is the golden ratio. Addition wraps modulo
2^32. Because the increment is odd, it is coprime to 2^32. The resulting
Weyl sequence visits every 32-bit state once before it repeats.
The mixing function uses the constants 0x21f0aaad and 0x735a2d97 from
the
search
for low avalanche bias. Its XOR-shifts and multiplications by odd integers
are invertible over 32-bit values. The mixer therefore preserves the full
period while diffusing nearby states across the output bits.
Math.imul and the bitwise operators keep the mixing steps in 32-bit
arithmetic. The final unsigned conversion produces an integer from 0 to
2^32 - 1. Dividing by 2^32 maps that integer to [0, 1).
The SplitMix family was introduced by Steele, Lea, and Flood in
A bitset of unsigned 32-bit ids over 65,536-id chunks, each a sorted id array
or an 8 KB bitmap.
This is the recommended layout: memory follows the ids actually stored, so a
caller that does not yet know how far its ids range does not pay for the span.
Chunked containers also make isDisjointFrom and intersectionSize stop at
the first chunk that decides them.
@param ― options - Optional items, added as if by add.
@throws ― {BitsetInvalidInputError} With code INVALID_ID when an item is not in 0 … BITSET_MAX_ID.
@example
constseen=roaringBitset({ items: [3, 65_536] })
seen.has(3) // true
seen.size // 2
roaringBitset({
BitsetOptions.items?: Iterable<number>|undefined
Ids to add in iteration order as the bitset is built, as if by add.
A Bitset from this package is copied container by container instead of id
by id, which also makes it the way to take an untouched copy of another
bitset.
Serialize a bitset into canonical binary bytes that restore the same layout.
The payload is the Roaring format's no-run layout — sorted chunk keys, a
cardinality per chunk, then each chunk as a sorted 16-bit id run or a bitmap —
inside a knotkit/wire envelope. The bytes are Knotkit's own framing and are
not RoaringFormatSpec-compatible.
@throws ― {BitsetInvalidInputError} With code FOREIGN_BITSET when set was not
produced by this package, or SERIALIZATION_LIMIT_EXCEEDED when a dense
bitset's bitmap would exceed BITSET_MAX_DECODE_DENSE_BYTES.
, restoring the layout the
artifact was serialized from.
@throws ― {BitsetParseError} With a code from BITSET_PARSE_ERROR_CODES when the
payload is malformed, truncated, unsupported, over budget, or internally
inconsistent.
deserializeBitset(
constbytes:Uint8Array<ArrayBufferLike>
bytes)
constrestored:Bitset
restored.
Bitset.kind: "roaring"|"dense"
The container layout this bitset uses: 'roaring' or 'dense'.
kind// 'roaring'
constrestored:Bitset
restored.
Bitset.size: number
Number of stored ids. O(1). The bitset is empty when this is 0.
size// 50000
constrestored:Bitset
restored.
Bitset.has: (id:number) => boolean
Whether id is stored. Total: returns false for any value that is not an
id (-1, 1.5, NaN, a string, 2 ** 32), as Set.prototype.has does
for a value it does not hold. O(1) for dense; O(log c + log n) for a
roaring array container and O(log c) for a bitmap container, where c
is the number of occupied chunks and n a container's cardinality.
has(49_999) // true
constbytes:Uint8Array<ArrayBufferLike>
bytes.
Uint8Array<ArrayBufferLike>.byteLength: number
The length in bytes of the array.
byteLength// 8288, the ids fill one dense chunk: a bitmap of 8 KiB for each 65,536 possible ids
With integrity set, a corrupted artifact fails with a parse error code instead of loading. Code that never saves a structure never imports the parser.