SortableSet.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const NONE = Symbol("not sorted");
  7. /**
  8. * A subset of Set that offers sorting functionality
  9. * @template T item type in set
  10. * @extends {Set<T>}
  11. */
  12. class SortableSet extends Set {
  13. /**
  14. * Create a new sortable set
  15. * @template T
  16. * @typedef {(a: T, b: T) => number} SortFunction
  17. * @param {Iterable<T>=} initialIterable The initial iterable value
  18. * @param {SortFunction<T>=} defaultSort Default sorting function
  19. */
  20. constructor(initialIterable, defaultSort) {
  21. super(initialIterable);
  22. /**
  23. * @private
  24. * @type {undefined | SortFunction<T>}
  25. */
  26. this._sortFn = defaultSort;
  27. /**
  28. * @private
  29. * @type {typeof NONE | undefined | ((a: T, b: T) => number)}}
  30. */
  31. this._lastActiveSortFn = NONE;
  32. /**
  33. * @private
  34. * @template R
  35. * @type {Map<(set: SortableSet<T>) => EXPECTED_ANY, EXPECTED_ANY> | undefined}
  36. */
  37. this._cache = undefined;
  38. /**
  39. * @private
  40. * @template R
  41. * @type {Map<(set: SortableSet<T>) => EXPECTED_ANY, EXPECTED_ANY> | undefined}
  42. */
  43. this._cacheOrderIndependent = undefined;
  44. }
  45. /**
  46. * Returns itself.
  47. * @param {T} value value to add to set
  48. * @returns {this} returns itself
  49. */
  50. add(value) {
  51. this._lastActiveSortFn = NONE;
  52. this._invalidateCache();
  53. this._invalidateOrderedCache();
  54. super.add(value);
  55. return this;
  56. }
  57. /**
  58. * Returns true if value existed in set, false otherwise.
  59. * @param {T} value value to delete
  60. * @returns {boolean} true if value existed in set, false otherwise
  61. */
  62. delete(value) {
  63. this._invalidateCache();
  64. this._invalidateOrderedCache();
  65. return super.delete(value);
  66. }
  67. /**
  68. * Describes how this clear operation behaves.
  69. * @returns {void}
  70. */
  71. clear() {
  72. this._invalidateCache();
  73. this._invalidateOrderedCache();
  74. return super.clear();
  75. }
  76. /**
  77. * Sort with a comparer function
  78. * @param {SortFunction<T> | undefined} sortFn Sorting comparer function
  79. * @returns {void}
  80. */
  81. sortWith(sortFn) {
  82. if (this.size <= 1 || sortFn === this._lastActiveSortFn) {
  83. // already sorted - nothing to do
  84. return;
  85. }
  86. /** @type {T[]} */
  87. const sortedArray = [...this].sort(sortFn);
  88. super.clear();
  89. for (let i = 0; i < sortedArray.length; i += 1) {
  90. super.add(sortedArray[i]);
  91. }
  92. this._lastActiveSortFn = sortFn;
  93. this._invalidateCache();
  94. }
  95. sort() {
  96. this.sortWith(this._sortFn);
  97. return this;
  98. }
  99. /**
  100. * Get data from cache
  101. * @template {EXPECTED_ANY} R
  102. * @param {(set: SortableSet<T>) => R} fn function to calculate value
  103. * @returns {R} returns result of fn(this), cached until set changes
  104. */
  105. getFromCache(fn) {
  106. if (this._cache === undefined) {
  107. this._cache = new Map();
  108. } else {
  109. const result = this._cache.get(fn);
  110. const data = /** @type {R} */ (result);
  111. if (data !== undefined) {
  112. return data;
  113. }
  114. }
  115. const newData = fn(this);
  116. this._cache.set(fn, newData);
  117. return newData;
  118. }
  119. /**
  120. * Get data from cache (ignoring sorting)
  121. * @template R
  122. * @param {(set: SortableSet<T>) => R} fn function to calculate value
  123. * @returns {R} returns result of fn(this), cached until set changes
  124. */
  125. getFromUnorderedCache(fn) {
  126. if (this._cacheOrderIndependent === undefined) {
  127. this._cacheOrderIndependent = new Map();
  128. } else {
  129. const result = this._cacheOrderIndependent.get(fn);
  130. const data = /** @type {R} */ (result);
  131. if (data !== undefined) {
  132. return data;
  133. }
  134. }
  135. const newData = fn(this);
  136. this._cacheOrderIndependent.set(fn, newData);
  137. return newData;
  138. }
  139. /**
  140. * Invalidates the cached state associated with this value.
  141. * @private
  142. * @returns {void}
  143. */
  144. _invalidateCache() {
  145. if (this._cache !== undefined) {
  146. this._cache.clear();
  147. }
  148. }
  149. /**
  150. * Invalidate ordered cache.
  151. * @private
  152. * @returns {void}
  153. */
  154. _invalidateOrderedCache() {
  155. if (this._cacheOrderIndependent !== undefined) {
  156. this._cacheOrderIndependent.clear();
  157. }
  158. }
  159. /**
  160. * Returns the raw array.
  161. * @returns {T[]} the raw array
  162. */
  163. toJSON() {
  164. return [...this];
  165. }
  166. }
  167. module.exports = SortableSet;