StackedMap.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. * Public cell value exposed by `StackedMap`, where `undefined` is preserved as
  10. * a valid stored result.
  11. * @template T
  12. * @typedef {T | undefined} Cell<T>
  13. */
  14. /**
  15. * Internal cell value used to distinguish deleted entries and explicit
  16. * `undefined` assignments while traversing stacked scopes.
  17. * @template T
  18. * @typedef {T | typeof TOMBSTONE | typeof UNDEFINED_MARKER} InternalCell<T>
  19. */
  20. /**
  21. * Converts an internal key/value pair into the external representation returned
  22. * by iteration helpers.
  23. * @template K
  24. * @template V
  25. * @param {[K, InternalCell<V>]} pair the internal cell
  26. * @returns {[K, Cell<V>]} its “safe” representation
  27. */
  28. const extractPair = (pair) => {
  29. const key = pair[0];
  30. const val = pair[1];
  31. if (val === UNDEFINED_MARKER || val === TOMBSTONE) {
  32. return [key, undefined];
  33. }
  34. return /** @type {[K, Cell<V>]} */ (pair);
  35. };
  36. /**
  37. * Layered map that supports child scopes while memoizing lookups from parent
  38. * scopes into the current layer.
  39. * @template K
  40. * @template V
  41. */
  42. class StackedMap {
  43. /**
  44. * Creates a new map layer on top of an optional parent stack.
  45. * @param {Map<K, InternalCell<V>>[]=} parentStack an optional parent
  46. */
  47. constructor(parentStack) {
  48. /** @type {Map<K, InternalCell<V>>} */
  49. this.map = new Map();
  50. /** @type {Map<K, InternalCell<V>>[]} */
  51. this.stack = parentStack === undefined ? [] : [...parentStack];
  52. this.stack.push(this.map);
  53. }
  54. /**
  55. * Stores a value in the current layer, preserving explicit `undefined`
  56. * values with an internal marker.
  57. * @param {K} item the key of the element to add
  58. * @param {V} value the value of the element to add
  59. * @returns {void}
  60. */
  61. set(item, value) {
  62. this.map.set(item, value === undefined ? UNDEFINED_MARKER : value);
  63. }
  64. /**
  65. * Deletes a key from the current view, either by removing it outright in the
  66. * root layer or by recording a tombstone in child layers.
  67. * @param {K} item the item to delete
  68. * @returns {void}
  69. */
  70. delete(item) {
  71. if (this.stack.length > 1) {
  72. this.map.set(item, TOMBSTONE);
  73. } else {
  74. this.map.delete(item);
  75. }
  76. }
  77. /**
  78. * Checks whether a key exists in the current scope chain, caching any parent
  79. * lookup result in the current layer.
  80. * @param {K} item the item to test
  81. * @returns {boolean} true if the item exists in this set
  82. */
  83. has(item) {
  84. const topValue = this.map.get(item);
  85. if (topValue !== undefined) {
  86. return topValue !== TOMBSTONE;
  87. }
  88. if (this.stack.length > 1) {
  89. for (let i = this.stack.length - 2; i >= 0; i--) {
  90. const value = this.stack[i].get(item);
  91. if (value !== undefined) {
  92. this.map.set(item, value);
  93. return value !== TOMBSTONE;
  94. }
  95. }
  96. this.map.set(item, TOMBSTONE);
  97. }
  98. return false;
  99. }
  100. /**
  101. * Returns the visible value for a key, caching parent hits and misses in the
  102. * current layer.
  103. * @param {K} item the key of the element to return
  104. * @returns {Cell<V>} the value of the element
  105. */
  106. get(item) {
  107. const topValue = this.map.get(item);
  108. if (topValue !== undefined) {
  109. return topValue === TOMBSTONE || topValue === UNDEFINED_MARKER
  110. ? undefined
  111. : topValue;
  112. }
  113. if (this.stack.length > 1) {
  114. for (let i = this.stack.length - 2; i >= 0; i--) {
  115. const value = this.stack[i].get(item);
  116. if (value !== undefined) {
  117. this.map.set(item, value);
  118. return value === TOMBSTONE || value === UNDEFINED_MARKER
  119. ? undefined
  120. : value;
  121. }
  122. }
  123. this.map.set(item, TOMBSTONE);
  124. }
  125. }
  126. /**
  127. * Collapses the stacked layers into a single concrete map.
  128. */
  129. _compress() {
  130. if (this.stack.length === 1) return;
  131. this.map = new Map();
  132. for (const data of this.stack) {
  133. for (const pair of data) {
  134. if (pair[1] === TOMBSTONE) {
  135. this.map.delete(pair[0]);
  136. } else {
  137. this.map.set(pair[0], pair[1]);
  138. }
  139. }
  140. }
  141. this.stack = [this.map];
  142. }
  143. /**
  144. * Returns the visible keys as an array after collapsing the stack.
  145. * @returns {K[]} array of keys
  146. */
  147. asArray() {
  148. this._compress();
  149. return [...this.map.keys()];
  150. }
  151. /**
  152. * Returns the visible keys as a `Set` after collapsing the stack.
  153. * @returns {Set<K>} set of keys
  154. */
  155. asSet() {
  156. this._compress();
  157. return new Set(this.map.keys());
  158. }
  159. /**
  160. * Returns visible key/value pairs using the external representation.
  161. * @returns {[K, Cell<V>][]} array of key/value pairs
  162. */
  163. asPairArray() {
  164. this._compress();
  165. return Array.from(this.map.entries(), extractPair);
  166. }
  167. /**
  168. * Returns the visible contents as a plain `Map`.
  169. * @returns {Map<K, Cell<V>>} materialized map
  170. */
  171. asMap() {
  172. return new Map(this.asPairArray());
  173. }
  174. /**
  175. * Returns the number of visible keys after collapsing the stack.
  176. * @returns {number} number of keys
  177. */
  178. get size() {
  179. this._compress();
  180. return this.map.size;
  181. }
  182. /**
  183. * Creates a child `StackedMap` that sees the current layers as its parent
  184. * scope.
  185. * @returns {StackedMap<K, V>} child map
  186. */
  187. createChild() {
  188. return new StackedMap(this.stack);
  189. }
  190. }
  191. module.exports = StackedMap;