source: imaps-frontend/node_modules/@jridgewell/set-array/dist/set-array.mjs

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 2.0 KB
Line 
1/**
2 * SetArray acts like a `Set` (allowing only one occurrence of a string `key`), but provides the
3 * index of the `key` in the backing array.
4 *
5 * This is designed to allow synchronizing a second array with the contents of the backing array,
6 * like how in a sourcemap `sourcesContent[i]` is the source content associated with `source[i]`,
7 * and there are never duplicates.
8 */
9class SetArray {
10 constructor() {
11 this._indexes = { __proto__: null };
12 this.array = [];
13 }
14}
15/**
16 * Typescript doesn't allow friend access to private fields, so this just casts the set into a type
17 * with public access modifiers.
18 */
19function cast(set) {
20 return set;
21}
22/**
23 * Gets the index associated with `key` in the backing array, if it is already present.
24 */
25function get(setarr, key) {
26 return cast(setarr)._indexes[key];
27}
28/**
29 * Puts `key` into the backing array, if it is not already present. Returns
30 * the index of the `key` in the backing array.
31 */
32function put(setarr, key) {
33 // The key may or may not be present. If it is present, it's a number.
34 const index = get(setarr, key);
35 if (index !== undefined)
36 return index;
37 const { array, _indexes: indexes } = cast(setarr);
38 const length = array.push(key);
39 return (indexes[key] = length - 1);
40}
41/**
42 * Pops the last added item out of the SetArray.
43 */
44function pop(setarr) {
45 const { array, _indexes: indexes } = cast(setarr);
46 if (array.length === 0)
47 return;
48 const last = array.pop();
49 indexes[last] = undefined;
50}
51/**
52 * Removes the key, if it exists in the set.
53 */
54function remove(setarr, key) {
55 const index = get(setarr, key);
56 if (index === undefined)
57 return;
58 const { array, _indexes: indexes } = cast(setarr);
59 for (let i = index + 1; i < array.length; i++) {
60 const k = array[i];
61 array[i - 1] = k;
62 indexes[k]--;
63 }
64 indexes[key] = undefined;
65 array.pop();
66}
67
68export { SetArray, get, pop, put, remove };
69//# sourceMappingURL=set-array.mjs.map
Note: See TracBrowser for help on using the repository browser.