SortableSet.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. * @param {T} value value to add to set
  47. * @returns {this} returns itself
  48. */
  49. add(value) {
  50. this._lastActiveSortFn = NONE;
  51. this._invalidateCache();
  52. this._invalidateOrderedCache();
  53. super.add(value);
  54. return this;
  55. }
  56. /**
  57. * @param {T} value value to delete
  58. * @returns {boolean} true if value existed in set, false otherwise
  59. */
  60. delete(value) {
  61. this._invalidateCache();
  62. this._invalidateOrderedCache();
  63. return super.delete(value);
  64. }
  65. /**
  66. * @returns {void}
  67. */
  68. clear() {
  69. this._invalidateCache();
  70. this._invalidateOrderedCache();
  71. return super.clear();
  72. }
  73. /**
  74. * Sort with a comparer function
  75. * @param {SortFunction<T> | undefined} sortFn Sorting comparer function
  76. * @returns {void}
  77. */
  78. sortWith(sortFn) {
  79. if (this.size <= 1 || sortFn === this._lastActiveSortFn) {
  80. // already sorted - nothing to do
  81. return;
  82. }
  83. /** @type {T[]} */
  84. const sortedArray = [...this].sort(sortFn);
  85. super.clear();
  86. for (let i = 0; i < sortedArray.length; i += 1) {
  87. super.add(sortedArray[i]);
  88. }
  89. this._lastActiveSortFn = sortFn;
  90. this._invalidateCache();
  91. }
  92. sort() {
  93. this.sortWith(this._sortFn);
  94. return this;
  95. }
  96. /**
  97. * Get data from cache
  98. * @template {EXPECTED_ANY} R
  99. * @param {(set: SortableSet<T>) => R} fn function to calculate value
  100. * @returns {R} returns result of fn(this), cached until set changes
  101. */
  102. getFromCache(fn) {
  103. if (this._cache === undefined) {
  104. this._cache = new Map();
  105. } else {
  106. const result = this._cache.get(fn);
  107. const data = /** @type {R} */ (result);
  108. if (data !== undefined) {
  109. return data;
  110. }
  111. }
  112. const newData = fn(this);
  113. this._cache.set(fn, newData);
  114. return newData;
  115. }
  116. /**
  117. * Get data from cache (ignoring sorting)
  118. * @template R
  119. * @param {(set: SortableSet<T>) => R} fn function to calculate value
  120. * @returns {R} returns result of fn(this), cached until set changes
  121. */
  122. getFromUnorderedCache(fn) {
  123. if (this._cacheOrderIndependent === undefined) {
  124. this._cacheOrderIndependent = new Map();
  125. } else {
  126. const result = this._cacheOrderIndependent.get(fn);
  127. const data = /** @type {R} */ (result);
  128. if (data !== undefined) {
  129. return data;
  130. }
  131. }
  132. const newData = fn(this);
  133. this._cacheOrderIndependent.set(fn, newData);
  134. return newData;
  135. }
  136. /**
  137. * @private
  138. * @returns {void}
  139. */
  140. _invalidateCache() {
  141. if (this._cache !== undefined) {
  142. this._cache.clear();
  143. }
  144. }
  145. /**
  146. * @private
  147. * @returns {void}
  148. */
  149. _invalidateOrderedCache() {
  150. if (this._cacheOrderIndependent !== undefined) {
  151. this._cacheOrderIndependent.clear();
  152. }
  153. }
  154. /**
  155. * @returns {T[]} the raw array
  156. */
  157. toJSON() {
  158. return [...this];
  159. }
  160. }
  161. module.exports = SortableSet;