StackedMap.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const TOMBSTONE = Symbol("tombstone");
  7. const UNDEFINED_MARKER = Symbol("undefined");
  8. /**
  9. * @template T
  10. * @typedef {T | undefined} Cell<T>
  11. */
  12. /**
  13. * @template T
  14. * @typedef {T | typeof TOMBSTONE | typeof UNDEFINED_MARKER} InternalCell<T>
  15. */
  16. /**
  17. * @template K
  18. * @template V
  19. * @param {[K, InternalCell<V>]} pair the internal cell
  20. * @returns {[K, Cell<V>]} its “safe” representation
  21. */
  22. const extractPair = pair => {
  23. const key = pair[0];
  24. const val = pair[1];
  25. if (val === UNDEFINED_MARKER || val === TOMBSTONE) {
  26. return [key, undefined];
  27. }
  28. return /** @type {[K, Cell<V>]} */ (pair);
  29. };
  30. /**
  31. * @template K
  32. * @template V
  33. */
  34. class StackedMap {
  35. /**
  36. * @param {Map<K, InternalCell<V>>[]=} parentStack an optional parent
  37. */
  38. constructor(parentStack) {
  39. /** @type {Map<K, InternalCell<V>>} */
  40. this.map = new Map();
  41. /** @type {Map<K, InternalCell<V>>[]} */
  42. this.stack = parentStack === undefined ? [] : parentStack.slice();
  43. this.stack.push(this.map);
  44. }
  45. /**
  46. * @param {K} item the key of the element to add
  47. * @param {V} value the value of the element to add
  48. * @returns {void}
  49. */
  50. set(item, value) {
  51. this.map.set(item, value === undefined ? UNDEFINED_MARKER : value);
  52. }
  53. /**
  54. * @param {K} item the item to delete
  55. * @returns {void}
  56. */
  57. delete(item) {
  58. if (this.stack.length > 1) {
  59. this.map.set(item, TOMBSTONE);
  60. } else {
  61. this.map.delete(item);
  62. }
  63. }
  64. /**
  65. * @param {K} item the item to test
  66. * @returns {boolean} true if the item exists in this set
  67. */
  68. has(item) {
  69. const topValue = this.map.get(item);
  70. if (topValue !== undefined) {
  71. return topValue !== TOMBSTONE;
  72. }
  73. if (this.stack.length > 1) {
  74. for (let i = this.stack.length - 2; i >= 0; i--) {
  75. const value = this.stack[i].get(item);
  76. if (value !== undefined) {
  77. this.map.set(item, value);
  78. return value !== TOMBSTONE;
  79. }
  80. }
  81. this.map.set(item, TOMBSTONE);
  82. }
  83. return false;
  84. }
  85. /**
  86. * @param {K} item the key of the element to return
  87. * @returns {Cell<V>} the value of the element
  88. */
  89. get(item) {
  90. const topValue = this.map.get(item);
  91. if (topValue !== undefined) {
  92. return topValue === TOMBSTONE || topValue === UNDEFINED_MARKER
  93. ? undefined
  94. : topValue;
  95. }
  96. if (this.stack.length > 1) {
  97. for (let i = this.stack.length - 2; i >= 0; i--) {
  98. const value = this.stack[i].get(item);
  99. if (value !== undefined) {
  100. this.map.set(item, value);
  101. return value === TOMBSTONE || value === UNDEFINED_MARKER
  102. ? undefined
  103. : value;
  104. }
  105. }
  106. this.map.set(item, TOMBSTONE);
  107. }
  108. }
  109. _compress() {
  110. if (this.stack.length === 1) return;
  111. this.map = new Map();
  112. for (const data of this.stack) {
  113. for (const pair of data) {
  114. if (pair[1] === TOMBSTONE) {
  115. this.map.delete(pair[0]);
  116. } else {
  117. this.map.set(pair[0], pair[1]);
  118. }
  119. }
  120. }
  121. this.stack = [this.map];
  122. }
  123. asArray() {
  124. this._compress();
  125. return Array.from(this.map.keys());
  126. }
  127. asSet() {
  128. this._compress();
  129. return new Set(this.map.keys());
  130. }
  131. asPairArray() {
  132. this._compress();
  133. return Array.from(this.map.entries(), extractPair);
  134. }
  135. asMap() {
  136. return new Map(this.asPairArray());
  137. }
  138. get size() {
  139. this._compress();
  140. return this.map.size;
  141. }
  142. createChild() {
  143. return new StackedMap(this.stack);
  144. }
  145. }
  146. module.exports = StackedMap;