set-array.mjs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. */
  9. class 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. */
  19. function cast(set) {
  20. return set;
  21. }
  22. /**
  23. * Gets the index associated with `key` in the backing array, if it is already present.
  24. */
  25. function 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. */
  32. function 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. */
  44. function 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. */
  54. function 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. export { SetArray, get, pop, put, remove };
  68. //# sourceMappingURL=set-array.mjs.map