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.
@example
constseen=roaringBitset({ items: [3, 65_536] })
seen.has(3) // true
seen.size // 2
roaringBitset()
const
constunique:any[]
unique= []
for (const
constrow: {
id:number;
}
rowof
constrows: {
id:number;
}[]
rows) {
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(
constrow: {
id:number;
}
row.
id: number
id)) {
continue
}
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(
constrow: {
id:number;
}
row.
id: number
id)
constunique:any[]
unique.
Array<any>.push(...items: any[]): number
Appends new elements to the end of an array, and returns the new length of the array.
@param ― items New elements to add to the array.
push(
constrow: {
id:number;
}
row)
}
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 one flat bitmap.
One bit per possible id up to the largest stored id, with no chunk directory,
so it is the faster layout for a set whose ids fill a compact range — and the
layout whose memory follows the span rather than the content. Choose it only
when the largest id is known and per-id traffic dominates.
@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.
@throws ― {BitsetInvalidInputError} With code MAX_CAPACITY_EXCEEDED when the bitmap the items span cannot be allocated.
A bitset of unsigned 32-bit ids over one flat bitmap.
One bit per possible id up to the largest stored id, with no chunk directory,
so it is the faster layout for a set whose ids fill a compact range — and the
layout whose memory follows the span rather than the content. Choose it only
when the largest id is known and per-id traffic dominates.
@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.
@throws ― {BitsetInvalidInputError} With code MAX_CAPACITY_EXCEEDED when the bitmap the items span cannot be allocated.
@example
constvisited=denseBitset({ items: [0, 2, 4] })
visited.size // 3
visited.rank(2) // 2
denseBitset({
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.
A bitset of unsigned 32-bit ids over one flat bitmap.
One bit per possible id up to the largest stored id, with no chunk directory,
so it is the faster layout for a set whose ids fill a compact range — and the
layout whose memory follows the span rather than the content. Choose it only
when the largest id is known and per-id traffic dominates.
@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.
@throws ― {BitsetInvalidInputError} With code MAX_CAPACITY_EXCEEDED when the bitmap the items span cannot be allocated.
@example
constvisited=denseBitset({ items: [0, 2, 4] })
visited.size // 3
visited.rank(2) // 2
denseBitset({
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.
items:
constyesterdayIds:number[]
yesterdayIds })
consttoday:Bitset
today.
Bitset.addAll: (ids:Iterable<number>) => number
Add every id of ids.
The operand is read completely and validated before anything is added, so
the call applies completely or not at all. An operand that is this
package's own Bitset is copied container by container, which makes
set.addAll(set) a no-op.
A generic operand is sorted and applied a chunk at a time, so input order
does not change the cost: O(k log k) to stage plus one merge per touched chunk.
@returns ― The number of ids whose membership changed.
@throws ― {BitsetInvalidInputError} With code INVALID_ID when the operand yields a value outside 0 … BITSET_MAX_ID.
@throws ― {BitsetInvalidInputError} With code MAX_CAPACITY_EXCEEDED when a dense bitset cannot grow its bitmap.
addAll(
constyesterday:Bitset
yesterday) // union: ids added
consttoday:Bitset
today.
Bitset.retainAll: (ids:Iterable<number>) => number
Keep only the ids ids contains. The operand is read completely first, so
the call applies completely or not at all.
@returns ― The number of ids whose membership changed.
@throws ― {BitsetInvalidInputError} With code INVALID_ID when the operand yields a value outside 0 … BITSET_MAX_ID.
retainAll(
constyesterday:Bitset
yesterday) // intersection: ids deleted
consttoday:Bitset
today.
Bitset.deleteAll: (ids:Iterable<number>) => number
Remove every id of ids.
@returns ― The number of ids whose membership changed.
@throws ― {BitsetInvalidInputError} With code INVALID_ID when the operand yields a value outside 0 … BITSET_MAX_ID.
deleteAll(
constyesterday:Bitset
yesterday) // difference: ids deleted
consttoday:Bitset
today.
Bitset.intersectionSize: (other:BitsetOperand) => number
The number of ids this bitset shares with other, without allocating a third
set. A generic set-like is walked from whichever side is smaller.
@throws ― {BitsetInvalidInputError} With code INVALID_OPERAND when other is not a set-like.
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.
@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.
items: [2, 5, 9] })
constids:Bitset
ids.
Bitset.at: (position:number) => number
Return the id at position: the position-th smallest for
0 <= position < size, and the -position-th largest for
-size <= position < 0, so at(0) is the smallest id and at(-1) the largest.
The walk starts from the nearer end, so both are cheap; the cost grows with
the distance from that end.
@throws ― {BitsetInvalidInputError} With code INVALID_INDEX when position
is not an integer or falls outside -size … size - 1.
at(0) // 2, the smallest
constids:Bitset
ids.
Bitset.at: (position:number) => number
Return the id at position: the position-th smallest for
0 <= position < size, and the -position-th largest for
-size <= position < 0, so at(0) is the smallest id and at(-1) the largest.
The walk starts from the nearer end, so both are cheap; the cost grows with
the distance from that end.
@throws ― {BitsetInvalidInputError} With code INVALID_INDEX when position
is not an integer or falls outside -size … size - 1.
at(-1) // 9, the largest
constids:Bitset
ids.
Bitset.rank: (id:number) => number
Return the number of stored ids at or below id, matching Roaring's
convention: rank(BITSET_MAX_ID) === size, and at(rank(id) - 1) === id
for every stored id.
@throws ― {BitsetInvalidInputError} With code INVALID_ID when id is not in 0 … BITSET_MAX_ID.
rank(3) // 1, ids at or below 3
constids:Bitset
ids.
Bitset.ceiling: (bound:number) => number |undefined
Return the smallest stored id at or above bound, or undefined when none
exists. Accepts the whole bound domain, so ceiling(2 ** 32) is always
undefined and the scan idiom ceiling(id + 1) never throws.
@throws ― {BitsetInvalidInputError} With code INVALID_ID when bound is not in 0 … 2 ** 32.
ceiling(3) // 5, the next id at or above 3
constids:Bitset
ids.
Bitset.ceiling: (bound:number) => number |undefined
Return the smallest stored id at or above bound, or undefined when none
exists. Accepts the whole bound domain, so ceiling(2 ** 32) is always
undefined and the scan idiom ceiling(id + 1) never throws.
@throws ― {BitsetInvalidInputError} With code INVALID_ID when bound is not in 0 … 2 ** 32.
ceiling(10) // undefined
constids:Bitset
ids.
Bitset.floor: (id:number) => number |undefined
Return the largest stored id at or below id, or undefined when none
exists: the mirror of
ceiling
. floor(BITSET_MAX_ID) is the largest id.
@throws ― {BitsetInvalidInputError} With code INVALID_ID when id is not in 0 … BITSET_MAX_ID.
floor(8) // 5, the previous id at or below 8
constids:Bitset
ids.
Bitset.floor: (id:number) => number |undefined
Return the largest stored id at or below id, or undefined when none
exists: the mirror of
ceiling
. floor(BITSET_MAX_ID) is the largest id.
@throws ― {BitsetInvalidInputError} With code INVALID_ID when id is not in 0 … BITSET_MAX_ID.
floor(1) // undefined
constids:Bitset
ids.
Bitset.countRange: (start:number, end:number) => number
Count the stored ids in [start, end), over the same bound domain as
addRange
, so countRange(0, 2 ** 32) is size and an empty range is
0. O((end - start) / 32) for dense; O(log c + chunks in span) for roaring.
@throws ― {BitsetInvalidInputError} With code INVALID_ID when an argument is
not in 0 … 2 ** 32, or INVALID_RANGE when start > end.
A bitset of unsigned 32-bit ids over one flat bitmap.
One bit per possible id up to the largest stored id, with no chunk directory,
so it is the faster layout for a set whose ids fill a compact range — and the
layout whose memory follows the span rather than the content. Choose it only
when the largest id is known and per-id traffic dominates.
@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.
@throws ― {BitsetInvalidInputError} With code MAX_CAPACITY_EXCEEDED when the bitmap the items span cannot be allocated.
A bitset of unsigned 32-bit ids over one flat bitmap.
One bit per possible id up to the largest stored id, with no chunk directory,
so it is the faster layout for a set whose ids fill a compact range — and the
layout whose memory follows the span rather than the content. Choose it only
when the largest id is known and per-id traffic dominates.
@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.
@throws ― {BitsetInvalidInputError} With code MAX_CAPACITY_EXCEEDED when the bitmap the items span cannot be allocated.
@example
constvisited=denseBitset({ items: [0, 2, 4] })
visited.size // 3
visited.rank(2) // 2
denseBitset()
constslots:Bitset
slots.
Bitset.addRange: (start:number, end:number) => number
Add every id in [start, end). O((end - start) / 32) for dense; O(chunks in span) for roaring.
@returns ― The number of ids whose membership changed.
@throws ― {BitsetInvalidInputError} With code INVALID_ID when an argument is
not in 0 … 2 ** 32, or INVALID_RANGE when start > end.
@throws ― {BitsetInvalidInputError} With code MAX_CAPACITY_EXCEEDED when a dense bitset cannot grow its bitmap.
addRange(0, 1_000) // 1,000 ids added
constslots:Bitset
slots.
Bitset.deleteRange: (start:number, end:number) => number
Remove every id in [start, end), as
addRange
costs.
@returns ― The number of ids whose membership changed.
@throws ― {BitsetInvalidInputError} With code INVALID_ID when an argument is
not in 0 … 2 ** 32, or INVALID_RANGE when start > end.
deleteRange(500, 600) // 100 ids deleted
import { roaringBitset } from'knotkit/bitset'
constcached=roaringBitset({ items: [1, 2, 3] })
constrequested=newSet([2, 3, 9])
// A native Set, built from the bitset, without materializing its ids first.
newSet(requested.union(cached)) // Set { 2, 3, 9, 1 }
requested.isSubsetOf(cached) // false
newSet(cached) // Set { 1, 2, 3 }
// And the other way round: a bitset compares against any set-like.
Every container layout this package constructs, keyed by the kind it
reports.
Exported so a caller, a test, or a benchmark can sweep every layout without
naming one, which is how the property suites prove the two containers agree.
roaring is first for the same reason it is first in BITSET_KINDS.
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.
@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.
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.
Every container layout this package constructs, keyed by the kind it
reports.
Exported so a caller, a test, or a benchmark can sweep every layout without
naming one, which is how the property suites prove the two containers agree.
roaring is first for the same reason it is first in BITSET_KINDS.
__BITSET_LAYOUTS[
consta:Bitset
a.
Bitset.kind: "roaring"|"dense"
The container layout this bitset uses: 'roaring' or 'dense'.
kind]({
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.
items:
consta:Bitset
a })
constunion:Bitset
union.
Bitset.addAll: (ids:Iterable<number>) => number
Add every id of ids.
The operand is read completely and validated before anything is added, so
the call applies completely or not at all. An operand that is this
package's own Bitset is copied container by container, which makes
set.addAll(set) a no-op.
A generic operand is sorted and applied a chunk at a time, so input order
does not change the cost: O(k log k) to stage plus one merge per touched chunk.
@returns ― The number of ids whose membership changed.
@throws ― {BitsetInvalidInputError} With code INVALID_ID when the operand yields a value outside 0 … BITSET_MAX_ID.
@throws ― {BitsetInvalidInputError} With code MAX_CAPACITY_EXCEEDED when a dense bitset cannot grow its bitmap.
you cannot name the largest id you will store, or fewer than one id in 64 of the range you can name holds a stored id
two bytes per stored id while a chunk is an array and one bit per possible id once it becomes a bitmap, plus a directory entry per occupied chunk and, once the set holds at least as many ids as its highest chunk key, a two-byte key-table entry per key; about 1.7 times slower than dense on has at half density of a 2^24 universe and about 4.7 times slower at one id in 16, and the only layout that stays small when one id is far from the rest
denseBitset
kind dense
you can name the largest id, it sits within 64 MiB of bitmap, and per-id has and add traffic dominates the operation mix
one bit per possible id up to the largest id it has held until clear, so a single far id pays for the whole span; measured about 1.7 times faster than roaring on has at half density of a 2^24 universe, about 4.7 times faster at one id in 16, and faster at every density measured
Bitset over 65,536-id chunks, each a sorted Uint16Array below the Roaring crossover and an 8 KB bitmap above it, so storage follows the ids stored rather than the span they occupy.
Bitset over one flat Uint32Array bitmap that doubles on growth and keeps its high-water capacity until clear, so its storage follows the largest id it has held.
Cost per member. Open a row for every operation.
roaringBitset22 members
Member
Cost
size
O(1)
has
O(log c + log n) array container, O(log c) bitmap container; the O(log c) lookup is one indexed read once the key table is built
add
O(log c + n) array container, O(log c) bitmap container
delete
O(log c + n) array container, O(log c) bitmap container
addRange
O(chunks in span)
deleteRange
O(chunks in span)
countRange
O(log c + chunks in span)
addAll
O(k log k) to stage a generic operand, skipped when it ascends, then one merge per touched chunk; O(min c) chunk copies for a Bitset operand
deleteAll
O(k log k) to stage a generic operand, then one pass per touched chunk
retainAll
O(c + k log k)
equals
O(min c)
isSubsetOf
O(min c)
isDisjointFrom
O(min c), stops at the first shared chunk
intersectionSize
O(min c)
at
O(c), from the nearer end
rank
O(c), from the nearer end
ceiling
O(c)
floor
O(log c)
values
O(size), O(1) per step
keys
O(size), O(1) per step
[Symbol.iterator]
O(size), O(1) per step
clear
O(1)
denseBitset22 members
Member
Cost
size
O(1)
has
O(1)
add
O(1) amortized
delete
O(1); the trailing-word trim is deferred to the next read that walks the span
Every error this module throws is BitsetInvalidInputError or BitsetParseError, with a code from a closed, exported list. Branch on the code; the message is for people.
Use knotkit/bitset for this task. If Knotkit is missing, follow https://knotkit.jkomyno.dev/getting-started.md#install-from-source. Import from 'knotkit/bitset'.
Exports:
- `roaringBitset` → `Bitset`: Bitset over 65,536-id chunks, each a sorted Uint16Array below the Roaring crossover and an 8 KB bitmap above it, so storage follows the ids stored rather than the span they occupy.
- `denseBitset` → `Bitset`: Bitset over one flat Uint32Array bitmap that doubles on growth and keeps its high-water capacity until clear, so its storage follows the largest id it has held.
Pick the layout by these rules:
- `roaringBitset` (kind 'roaring') when you cannot name the largest id you will store, or fewer than one id in 64 of the range you can name holds a stored id
- `denseBitset` (kind 'dense') when you can name the largest id, it sits within 64 MiB of bitmap, and per-id has and add traffic dominates the operation mix
Use something else when:
- a plain set over arbitrary keys, or over ids beyond 2^32 - 1 → Set or Map
- membership over unbounded values where a small false-positive rate is acceptable → knotkit/scalar-sketch
- read a set as it was at an earlier point while it keeps changing → knotkit/snapshot-set
- a boolean flag per vertex you already index by number, where membership is the only question → a pre-sized Uint8Array, as problems/mst and problems/tsp do
- track which elements are connected and merge groups → knotkit/disjoint-set
- persist the set in a format another implementation reads → your own encoder: the binary artifact is Knotkit's own and is not RoaringFormatSpec-compatible
Errors are BitsetInvalidInputError or BitsetParseError with a `code` property. Branch on `error.code`, never on the message. Save and load through `knotkit/bitset/wire` (serializeBitset, deserializeBitset).
Full page: https://knotkit.jkomyno.dev/modules/bitset.md
Types: node_modules/knotkit/bitset/ and https://knotkit.jkomyno.dev/reference/knotkit/bitset/