backed by one toggle-version history per value,
optionally pre-populated from an iterable.
Diverges from knotkit/disjoint-set's growable factories: a duplicate
value in the initial iterable is a silent no-op, matching add's
no-op-on-present rule, rather than a throw.
@param ― values - Values to insert, in iteration order.
backed by one toggle-version history per value,
optionally pre-populated from an iterable.
Diverges from knotkit/disjoint-set's growable factories: a duplicate
value in the initial iterable is a silent no-op, matching add's
no-op-on-present rule, rather than a throw.
@param ― values - Values to insert, in iteration order.
snapshotSet(['a'])
const
consts1:number
s1=
constset:SnapshotSet<string>
set.
SnapshotSet<string>.createSnapshot: () => number
Mint an id denoting the set's state at this moment. O(1); nothing is
copied.
Shares its counter with add/delete, so two createSnapshot() calls with no
intervening mutation return different ids that answer identically for
every value. Ids are strictly increasing across the structure's
lifetime.
the caller mints snapshots and later mutations must not change any answer derived from an id already minted, and mutation plus snapshot cost must stay O(1) rather than cloning the set per snapshot
one immutable version entry per effective toggle and one Set insert per minted id; `hasAt` binary-searches that value's toggles, and `valuesAt` pays O(r log h) over every value registered by the snapshot, not just the ones it returns
Original checkpoint has key 0: true. Current set has key 0: false.
After 12 checkpoints, full copies write 1536 memberships. The version chain records 24 membership changes.
The work model counts membership history after initialization.
Hover or focus a chart for exact values. Each change reruns demo.ts against knotkit/snapshot-set in your browser; the defaults reproduce the README figure.
Every error this module throws is SnapshotSetInvalidInputError or SnapshotSetParseError, with a code from a closed, exported list. Branch on the code; the message is for people.
Use knotkit/snapshot-set for this task. If Knotkit is missing, follow https://knotkit.jkomyno.dev/getting-started.md#install-from-source. Import from 'knotkit/snapshot-set'.
Exports:
- `snapshotSet` → `SnapshotSet<T>`: Mutable set whose snapshots are O(1) ids over a per-value toggle-version history, so a minted id never changes its answer.
Pick the layout by these rules:
- `snapshotSet` (kind 'version-chain') when the caller mints snapshots and later mutations must not change any answer derived from an id already minted, and mutation plus snapshot cost must stay O(1) rather than cloning the set per snapshot
Inputs and their sources:
- value encoding for a binary artifact, which no artifact carries back: `ElementCodec<T>` from `knotkit/wire`
- value validation for a JSON artifact, whose projection carries no value types: `(value: unknown) => value is T` from `the caller`
Use something else when:
- a plain set with no history → Set
- keep every version of a keyed value, not just membership → a persistent data-structure library such as immutable.js, or structuredClone per version
- record which elements belong to the same group, and merge groups → knotkit/disjoint-set
- reclaim the memory a snapshot holds once nobody reads it → drop the id and rebuild the set from your own change log, because a snapshot lives for the set's lifetime
- serialize identically-behaving sets to identical bytes → your own canonical projection, because a snapshot-set artifact records the mutation path it took
Errors are SnapshotSetInvalidInputError or SnapshotSetParseError with a `code` property. Branch on `error.code`, never on the message. Save and load through `knotkit/snapshot-set/wire` (serializeSnapshotSet, deserializeSnapshotSet, serializeSnapshotSetJson, deserializeSnapshotSetJson).
Full page: https://knotkit.jkomyno.dev/modules/snapshot-set.md
Types: node_modules/knotkit/snapshot-set/ and https://knotkit.jkomyno.dev/reference/knotkit/snapshot-set/