StackedMap.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. } else {
  28. return /** @type {[K, Cell<V>]} */ (pair);
  29. }
  30. };
  31. /**
  32. * @template K
  33. * @template V
  34. */
  35. class StackedMap {
  36. /**
  37. * @param {Map<K, InternalCell<V>>[]=} parentStack an optional parent
  38. */
  39. constructor(parentStack) {
  40. /** @type {Map<K, InternalCell<V>>} */
  41. this.map = new Map();
  42. /** @type {Map<K, InternalCell<V>>[]} */
  43. this.stack = parentStack === undefined ? [] : parentStack.slice();
  44. this.stack.push(this.map);
  45. }
  46. /**
  47. * @param {K} item the key of the element to add
  48. * @param {V} value the value of the element to add
  49. * @returns {void}
  50. */
  51. set(item, value) {
  52. this.map.set(item, value === undefined ? UNDEFINED_MARKER : value);
  53. }
  54. /**
  55. * @param {K} item the item to delete
  56. * @returns {void}
  57. */
  58. delete(item) {
  59. if (this.stack.length > 1) {
  60. this.map.set(item, TOMBSTONE);
  61. } else {
  62. this.map.delete(item);
  63. }
  64. }
  65. /**
  66. * @param {K} item the item to test
  67. * @returns {boolean} true if the item exists in this set
  68. */
  69. has(item) {
  70. const topValue = this.map.get(item);
  71. if (topValue !== undefined) {
  72. return topValue !== TOMBSTONE;
  73. }
  74. if (this.stack.length > 1) {
  75. for (let i = this.stack.length - 2; i >= 0; i--) {
  76. const value = this.stack[i].get(item);
  77. if (value !== undefined) {
  78. this.map.set(item, value);
  79. return value !== TOMBSTONE;
  80. }
  81. }
  82. this.map.set(item, TOMBSTONE);
  83. }
  84. return false;
  85. }
  86. /**
  87. * @param {K} item the key of the element to return
  88. * @returns {Cell<V>} the value of the element
  89. */
  90. get(item) {
  91. const topValue = this.map.get(item);
  92. if (topValue !== undefined) {
  93. return topValue === TOMBSTONE || topValue === UNDEFINED_MARKER
  94. ? undefined
  95. : topValue;
  96. }
  97. if (this.stack.length > 1) {
  98. for (let i = this.stack.length - 2; i >= 0; i--) {
  99. const value = this.stack[i].get(item);
  100. if (value !== undefined) {
  101. this.map.set(item, value);
  102. return value === TOMBSTONE || value === UNDEFINED_MARKER
  103. ? undefined
  104. : value;
  105. }
  106. }
  107. this.map.set(item, TOMBSTONE);
  108. }
  109. return undefined;
  110. }
  111. _compress() {
  112. if (this.stack.length === 1) return;
  113. this.map = new Map();
  114. for (const data of this.stack) {
  115. for (const pair of data) {
  116. if (pair[1] === TOMBSTONE) {
  117. this.map.delete(pair[0]);
  118. } else {
  119. this.map.set(pair[0], pair[1]);
  120. }
  121. }
  122. }
  123. this.stack = [this.map];
  124. }
  125. asArray() {
  126. this._compress();
  127. return Array.from(this.map.keys());
  128. }
  129. asSet() {
  130. this._compress();
  131. return new Set(this.map.keys());
  132. }
  133. asPairArray() {
  134. this._compress();
  135. return Array.from(this.map.entries(), extractPair);
  136. }
  137. asMap() {
  138. return new Map(this.asPairArray());
  139. }
  140. get size() {
  141. this._compress();
  142. return this.map.size;
  143. }
  144. createChild() {
  145. return new StackedMap(this.stack);
  146. }
  147. }
  148. module.exports = StackedMap;