# SnapshotSet

Defined in: [packages/snapshot-set/src/snapshot-set.ts:57](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/snapshot-set/src/snapshot-set.ts#L57)

A mutable set backed by per-value toggle-version history.

Values are compared with `SameValueZero`, the same equality that `Map` and `Set` use. `NaN` equals itself, and `0` equals `-0`.

`values()` walks values in first-registration order — the order a value was first added, not `Set`’s reinsertion order. A value that is removed and later re-added keeps its original position, because its registry index never changes.

`createSnapshot()` mints an id denoting the set’s state at the moment of the call. Later mutations never change any answer derived from an id minted at or before that call — that is the one invariant the whole design exists to hold. Only ids `createSnapshot()` returned are valid; `hasAt` and `valuesAt` throw for anything else, including a version number the structure passed through but never handed out as a snapshot id.

All implementations mutate in place.

## Type Parameters

### T

`T`

## Properties

### kind

> `readonly` **kind**: `"version-chain"`

Defined in: [packages/snapshot-set/src/snapshot-set.ts:59](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/snapshot-set/src/snapshot-set.ts#L59)

Stable identifier for the implementation’s storage layout.

***

### size

> `readonly` **size**: `number`

Defined in: [packages/snapshot-set/src/snapshot-set.ts:61](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/snapshot-set/src/snapshot-set.ts#L61)

Number of values currently present. O(1).

***

### has

> `readonly` **has**: (`value`) => `boolean`

Defined in: [packages/snapshot-set/src/snapshot-set.ts:63](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/snapshot-set/src/snapshot-set.ts#L63)

Return `true` when the value is currently present. O(1) average.

#### Parameters

##### value

`T`

#### Returns

`boolean`

***

### add

> `readonly` **add**: (`value`) => `boolean`

Defined in: [packages/snapshot-set/src/snapshot-set.ts:71](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/snapshot-set/src/snapshot-set.ts#L71)

Insert a value. O(1) amortized.

A no-op when the value is already present.

#### Parameters

##### value

`T`

#### Returns

`boolean`

`true` when the value was not already present.

***

### delete

> `readonly` **delete**: (`value`) => `boolean`

Defined in: [packages/snapshot-set/src/snapshot-set.ts:79](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/snapshot-set/src/snapshot-set.ts#L79)

Delete a value. O(1) average.

A no-op when the value is not present.

#### Parameters

##### value

`T`

#### Returns

`boolean`

`true` when the value was present and is now removed.

***

### values

> `readonly` **values**: () => `IterableIterator`<`T`>

Defined in: [packages/snapshot-set/src/snapshot-set.ts:87](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/snapshot-set/src/snapshot-set.ts#L87)

Iterate over the values present when the call was made, in first-registration order. O(1) per step, O(n) total.

The iterator covers the values present at call time; mutating the set while iterating does not extend the walk.

#### Returns

`IterableIterator`<`T`>

***

### createSnapshot

> `readonly` **createSnapshot**: () => `number`

Defined in: [packages/snapshot-set/src/snapshot-set.ts:97](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/snapshot-set/src/snapshot-set.ts#L97)

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.

#### Returns

`number`

***

### hasAt

> `readonly` **hasAt**: (`id`, `value`) => `boolean`

Defined in: [packages/snapshot-set/src/snapshot-set.ts:104](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/snapshot-set/src/snapshot-set.ts#L104)

Return `true` when the value was present at the given snapshot. O(log h), where h is the number of times that value has toggled.

#### Parameters

##### id

`number`

##### value

`T`

#### Returns

`boolean`

#### Throws

When `id` was never returned by `createSnapshot()`.

***

### valuesAt

> `readonly` **valuesAt**: (`id`) => `IterableIterator`<`T`>

Defined in: [packages/snapshot-set/src/snapshot-set.ts:122](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/snapshot-set/src/snapshot-set.ts#L122)

Iterate over the values present at the given snapshot, in first-registration order. O(r \* log h) total, where r is the number of values registered at or before the snapshot and h is a given value’s toggle count.

This cost is proportional to how many values had been *registered* by that snapshot, not to how many the snapshot’s result actually contains — an old snapshot on a set that has since churned through many distinct values still pays for every value registered before it, including ones long removed by then.

The iterator covers the registry prefix that existed when the call was made; mutating the set while iterating does not change the walk.

#### Parameters

##### id

`number`

#### Returns

`IterableIterator`<`T`>

#### Throws

When `id` was never returned by `createSnapshot()`.
