| 1 | type Key = string | number | symbol;
|
|---|
| 2 |
|
|---|
| 3 | /**
|
|---|
| 4 | * SetArray acts like a `Set` (allowing only one occurrence of a string `key`), but provides the
|
|---|
| 5 | * index of the `key` in the backing array.
|
|---|
| 6 | *
|
|---|
| 7 | * This is designed to allow synchronizing a second array with the contents of the backing array,
|
|---|
| 8 | * like how in a sourcemap `sourcesContent[i]` is the source content associated with `source[i]`,
|
|---|
| 9 | * and there are never duplicates.
|
|---|
| 10 | */
|
|---|
| 11 | export class SetArray<T extends Key = Key> {
|
|---|
| 12 | declare private _indexes: Record<T, number | undefined>;
|
|---|
| 13 | declare array: readonly T[];
|
|---|
| 14 |
|
|---|
| 15 | constructor() {
|
|---|
| 16 | this._indexes = { __proto__: null } as any;
|
|---|
| 17 | this.array = [];
|
|---|
| 18 | }
|
|---|
| 19 | }
|
|---|
| 20 |
|
|---|
| 21 | interface PublicSet<T extends Key> {
|
|---|
| 22 | array: T[];
|
|---|
| 23 | _indexes: SetArray<T>['_indexes'];
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | /**
|
|---|
| 27 | * Typescript doesn't allow friend access to private fields, so this just casts the set into a type
|
|---|
| 28 | * with public access modifiers.
|
|---|
| 29 | */
|
|---|
| 30 | function cast<T extends Key>(set: SetArray<T>): PublicSet<T> {
|
|---|
| 31 | return set as any;
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | /**
|
|---|
| 35 | * Gets the index associated with `key` in the backing array, if it is already present.
|
|---|
| 36 | */
|
|---|
| 37 | export function get<T extends Key>(setarr: SetArray<T>, key: T): number | undefined {
|
|---|
| 38 | return cast(setarr)._indexes[key];
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | /**
|
|---|
| 42 | * Puts `key` into the backing array, if it is not already present. Returns
|
|---|
| 43 | * the index of the `key` in the backing array.
|
|---|
| 44 | */
|
|---|
| 45 | export function put<T extends Key>(setarr: SetArray<T>, key: T): number {
|
|---|
| 46 | // The key may or may not be present. If it is present, it's a number.
|
|---|
| 47 | const index = get(setarr, key);
|
|---|
| 48 | if (index !== undefined) return index;
|
|---|
| 49 |
|
|---|
| 50 | const { array, _indexes: indexes } = cast(setarr);
|
|---|
| 51 |
|
|---|
| 52 | const length = array.push(key);
|
|---|
| 53 | return (indexes[key] = length - 1);
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | /**
|
|---|
| 57 | * Pops the last added item out of the SetArray.
|
|---|
| 58 | */
|
|---|
| 59 | export function pop<T extends Key>(setarr: SetArray<T>): void {
|
|---|
| 60 | const { array, _indexes: indexes } = cast(setarr);
|
|---|
| 61 | if (array.length === 0) return;
|
|---|
| 62 |
|
|---|
| 63 | const last = array.pop()!;
|
|---|
| 64 | indexes[last] = undefined;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | /**
|
|---|
| 68 | * Removes the key, if it exists in the set.
|
|---|
| 69 | */
|
|---|
| 70 | export function remove<T extends Key>(setarr: SetArray<T>, key: T): void {
|
|---|
| 71 | const index = get(setarr, key);
|
|---|
| 72 | if (index === undefined) return;
|
|---|
| 73 |
|
|---|
| 74 | const { array, _indexes: indexes } = cast(setarr);
|
|---|
| 75 | for (let i = index + 1; i < array.length; i++) {
|
|---|
| 76 | const k = array[i];
|
|---|
| 77 | array[i - 1] = k;
|
|---|
| 78 | indexes[k]!--;
|
|---|
| 79 | }
|
|---|
| 80 | indexes[key] = undefined;
|
|---|
| 81 | array.pop();
|
|---|
| 82 | }
|
|---|