SortableSet.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. const sortedArray = Array.from(this).sort(sortFn);
  84. super.clear();
  85. for (let i = 0; i < sortedArray.length; i += 1) {
  86. super.add(sortedArray[i]);
  87. }
  88. this._lastActiveSortFn = sortFn;
  89. this._invalidateCache();
  90. }
  91. sort() {
  92. this.sortWith(this._sortFn);
  93. return this;
  94. }
  95. /**
  96. * Get data from cache
  97. * @template {EXPECTED_ANY} R
  98. * @param {(set: SortableSet<T>) => R} fn function to calculate value
  99. * @returns {R} returns result of fn(this), cached until set changes
  100. */
  101. getFromCache(fn) {
  102. if (this._cache === undefined) {
  103. this._cache = new Map();
  104. } else {
  105. const result = this._cache.get(fn);
  106. const data = /** @type {R} */ (result);
  107. if (data !== undefined) {
  108. return data;
  109. }
  110. }
  111. const newData = fn(this);
  112. this._cache.set(fn, newData);
  113. return newData;
  114. }
  115. /**
  116. * Get data from cache (ignoring sorting)
  117. * @template R
  118. * @param {(set: SortableSet<T>) => R} fn function to calculate value
  119. * @returns {R} returns result of fn(this), cached until set changes
  120. */
  121. getFromUnorderedCache(fn) {
  122. if (this._cacheOrderIndependent === undefined) {
  123. this._cacheOrderIndependent = new Map();
  124. } else {
  125. const result = this._cacheOrderIndependent.get(fn);
  126. const data = /** @type {R} */ (result);
  127. if (data !== undefined) {
  128. return data;
  129. }
  130. }
  131. const newData = fn(this);
  132. this._cacheOrderIndependent.set(fn, newData);
  133. return newData;
  134. }
  135. /**
  136. * @private
  137. * @returns {void}
  138. */
  139. _invalidateCache() {
  140. if (this._cache !== undefined) {
  141. this._cache.clear();
  142. }
  143. }
  144. /**
  145. * @private
  146. * @returns {void}
  147. */
  148. _invalidateOrderedCache() {
  149. if (this._cacheOrderIndependent !== undefined) {
  150. this._cacheOrderIndependent.clear();
  151. }
  152. }
  153. /**
  154. * @returns {T[]} the raw array
  155. */
  156. toJSON() {
  157. return Array.from(this);
  158. }
  159. }
  160. module.exports = SortableSet;