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