Skip to content

Save a structure and load it back

Serialization lives in a /wire subpath. It writes versioned bytes and restores the same layout.

import {
function roaringBitset(options?: BitsetOptions): 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

const seen = roaringBitset({ items: [3, 65_536] })
seen.has(3) // true
seen.size // 2

roaringBitset
} from 'knotkit/bitset'
import {
function deserializeBitset(bytes: Uint8Array): Bitset

Decode bytes written by

serializeBitset

, 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
,
function serializeBitset(set: Bitset, options?: SerializeBitsetOptions): Uint8Array

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'
const
const ids: Bitset
ids
=
function roaringBitset(options?: BitsetOptions): 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

const seen = 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
: [7, 42, 2 ** 31] })
const
const bytes: Uint8Array<ArrayBufferLike>
bytes
=
function serializeBitset(set: Bitset, options?: SerializeBitsetOptions): Uint8Array

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
(
const ids: Bitset
ids
, {
SerializeBitsetOptions.integrity?: boolean | undefined

Include a CRC-32 integrity section for accidental corruption detection.

integrity
: true }) // a Uint8Array to store anywhere
const
const restored: Bitset
restored
=
function deserializeBitset(bytes: Uint8Array): Bitset

Decode bytes written by

serializeBitset

, 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
(
const bytes: Uint8Array<ArrayBufferLike>
bytes
)
const restored: Bitset
restored
.
Bitset.kind: "roaring" | "dense"

The container layout this bitset uses: 'roaring' or 'dense'.

kind
// 'roaring'
const restored: 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
const restored: Bitset
restored
.
Bitset.size: number

Number of stored ids. O(1). The bitset is empty when this is 0.

size
// 3

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.

Modules: knotkit/bitset · API reference