import { Node, PresentNode, SparseItems, deserializeItems, } from "./sparse_items"; /** * Serialized form of a `SparseArray`. * * The serialized form uses a compact JSON representation with run-length encoded deletions. It alternates between: * - arrays of present values, and * - numbers, representing that number of deleted indices (empty slots). * * For example, the sparse array `["foo", "bar", , , , "X", "yy"]` serializes to * `[["foo", "bar"], 3, ["X", "yy"]]`. * * Trivial entries (empty arrays, 0s, & trailing deletions) are always omitted. * For example, the sparse array `[, , "biz", "baz"]` serializes to `[2, ["biz", "baz"]]`. */ export type SerializedSparseArray = (T[] | number)[]; /** * Iterator-like object returned by SparseArray.newSlicer(). * * Call nextSlice repeatedly to enumerate the slices in order. */ export interface ArraySlicer { /** * Returns an array of items in the next slice, * continuing from the previous index (inclusive) to endIndex (exclusive). * * Each item [index, values] indicates a run of present values starting at index, * ending at either endIndex or a deleted index. * * The first call starts at index 0. To end at the end of the array, * set `endIndex = null`. * * @throws If endIndex is less than the previous index. */ nextSlice(endIndex: number | null): Array<[index: number, values: T[]]>; } /** * A sparse array with values of type `T`. * * `SparseArray` behaves similarly to an ordinary `Array` used in sparse mode. * (Note: Use set/get/delete instead of indexed access.) * However, it is additionally optimized for the following tasks: * 1. Convert between the array and a compact JSON representation * with run-length encoded deletions (`SerializedSparseArray`). * 2. Iterate over present values only. * 3. Convert between a count `c` and the `c`-th present entry. * * For ordinary array tasks, SparseArray aims to have comparable * memory usage and acceptable speed relative to an ordinary Array. * However, indexed accesses are slower, due to internal searches. * * To construct a SparseArray, use the static `new` or `deserialize` methods. * * @see SparseString For a memory-optimized array of chars and (optional) embedded objects. * @see SparseIndices To track a sparse array's present indices independent of its values. */ export class SparseArray extends SparseItems { // So list-positions can refer to unbound versions, we avoid using // "this" in static methods. /** * Returns a new, empty SparseArray. */ static new(): SparseArray { return new SparseArray(null); } /** * Returns a new SparseArray by deserializing the given state * from `SparseArray.serialize`. * * @throws If the serialized form is invalid (see `SparseArray.serialize`). */ static deserialize(serialized: SerializedSparseArray): SparseArray { return new SparseArray( deserializeItems(serialized, (allegedItem) => { if (!Array.isArray(allegedItem)) { throw new Error(`Invalid entry in serialized state: ${allegedItem}`); } return new ArrayNode((allegedItem as T[]).slice()); }) ); } /** * Returns a compact JSON representation of our state. * * See SerializedSparseArray for a description of the format. */ serialize(): SerializedSparseArray { return super.serialize(); } /** * Returns the value at index, or undefined if not present. * * @throws If `index < 0`. (It is okay for index to exceed `this.length`.) */ get(index: number): T | undefined { const located = this._get(index); if (located === null) return undefined; const [item, offset] = located; return item[offset]; } newSlicer(): ArraySlicer { return super.newSlicer(); } /** * Iterates over the present [index, value] pairs, in order. */ *entries(): IterableIterator<[index: number, value: T]> { for (const [index, item] of this.items()) { for (let j = 0; j < item.length; j++) { yield [index + j, item[j]]; } } } /** * Iterates over the present items, in order. * * Each item [index, values] indicates a run of present values starting at index, * ending at a deleted index. */ items(): IterableIterator<[index: number, values: T[]]> { return super.items(); } /** * Sets values starting at index. * * That is, sets all values in the range [index, index + values.length) to the * given values. * * @returns A sparse array of the previous values. * Index 0 in the returned array corresponds to `index` in this array. */ set(index: number, ...values: T[]): SparseArray { return this._set(index, values); } protected construct(start: Node | null): this { return new SparseArray(start) as this; } protected newNode(item: T[]): PresentNode { return new ArrayNode(item); } } class ArrayNode extends PresentNode { constructor(public item: T[]) { super(); } get length(): number { return this.item.length; } splitContent(index: number): PresentNode { const after = new ArrayNode(this.item.slice(index)); this.item.length = index; return after; } tryMergeContent(other: PresentNode): boolean { this.item.push(...(other as ArrayNode).item); return true; } sliceItem(start?: number, end?: number): T[] { return this.item.slice(start, end); } }