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.
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.
Returns the elements of an array that meet the condition specified in a callback function.
@param ― predicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array.
@param ― thisArg An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.
filter((
row: {
id: number;
name: string;
}
row) => {
if (
constseen:Bitset
seen.
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(
row: {
id: number;
name: string;
}
row.
id: number
id)) {
returnfalse
}
constseen:Bitset
seen.
Bitset.add: (id:number) => boolean
Add id. O(1) amortized for dense; O(log c + n) for a roaring array
container, which shifts its tail.
@returns ― Whether the id was absent.
@throws ― {BitsetInvalidInputError} With code INVALID_ID when id is not in 0 … BITSET_MAX_ID.
@throws ― {BitsetInvalidInputError} With code MAX_CAPACITY_EXCEEDED when a dense bitset cannot grow its bitmap.
Calls a defined callback function on each element of an array, and returns an array that contains the results.
@param ― callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.
@param ― thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
map((
row: {
id: number;
name: string;
}
row) =>
row: {
id: number;
name: string;
}
row.
name: string
name) // ['ada', 'grace']
constseen:Bitset
seen.
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.
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
@param ― iterable An iterable object to convert to an array.
@param ― mapfn A mapping function to call on every element of the array.
@param ― thisArg Value of 'this' used to invoke the mapfn.
from({
ArrayLike<T>.length: number
length: 20_000 }, (
_: unknown
_,
index: number
index) => ({
id: number
id:
index: number
index%10_000,
name: string
name: `row-${
index: number
index}` }))
const
constarrivals:readonly {
id:number;
name:string;
}[]
arrivals=
shuffled<{
id:number;
name:string;
}>(random:RandomSource, values:readonly {
id:number;
name:string;
}[]):readonly {
id: number;
name: string;
}[]
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.
Returns the elements of an array that meet the condition specified in a callback function.
@param ― predicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array.
@param ― thisArg An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.
filter((
row: {
id: number;
name: string;
}
row) => {
if (
constseen:Bitset
seen.
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(
row: {
id: number;
name: string;
}
row.
id: number
id)) {
returnfalse
}
constseen:Bitset
seen.
Bitset.add: (id:number) => boolean
Add id. O(1) amortized for dense; O(log c + n) for a roaring array
container, which shifts its tail.
@returns ― Whether the id was absent.
@throws ― {BitsetInvalidInputError} With code INVALID_ID when id is not in 0 … BITSET_MAX_ID.
@throws ― {BitsetInvalidInputError} With code MAX_CAPACITY_EXCEEDED when a dense bitset cannot grow its bitmap.
add(
row: {
id: number;
name: string;
}
row.
id: number
id)
returntrue
})
constunique: {
id:number;
name:string;
}[]
unique.
Array<{ id: number; name: string; }>.length: number
Gets or sets the length of the array. This is a number one higher than the highest index in the array.
length// 10000
constseen:Bitset
seen.
Bitset.size: number
Number of stored ids. O(1). The bitset is empty when this is 0.
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(
constseen:Bitset
seen).
Uint8Array<ArrayBufferLike>.byteLength: number
The length in bytes of the array.
byteLength// 8272, one bit per possible id from 0 to 65,535 rather than one object per row
Ids must be integers from 0 to 2³² − 1. For other keys, a Set is the right tool.