SortableSet.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. * @param {Iterable<T>=} initialIterable The initial iterable value
  16. * @typedef {function(T, T): number} SortFunction
  17. * @param {SortFunction=} defaultSort Default sorting function
  18. */
  19. constructor(initialIterable, defaultSort) {
  20. super(initialIterable);
  21. /** @private @type {undefined | function(T, T): number}} */
  22. this._sortFn = defaultSort;
  23. /** @private @type {typeof NONE | undefined | function(T, T): number}} */
  24. this._lastActiveSortFn = NONE;
  25. /** @private @type {Map<Function, any> | undefined} */
  26. this._cache = undefined;
  27. /** @private @type {Map<Function, any> | undefined} */
  28. this._cacheOrderIndependent = undefined;
  29. }
  30. /**
  31. * @param {T} value value to add to set
  32. * @returns {this} returns itself
  33. */
  34. add(value) {
  35. this._lastActiveSortFn = NONE;
  36. this._invalidateCache();
  37. this._invalidateOrderedCache();
  38. super.add(value);
  39. return this;
  40. }
  41. /**
  42. * @param {T} value value to delete
  43. * @returns {boolean} true if value existed in set, false otherwise
  44. */
  45. delete(value) {
  46. this._invalidateCache();
  47. this._invalidateOrderedCache();
  48. return super.delete(value);
  49. }
  50. /**
  51. * @returns {void}
  52. */
  53. clear() {
  54. this._invalidateCache();
  55. this._invalidateOrderedCache();
  56. return super.clear();
  57. }
  58. /**
  59. * Sort with a comparer function
  60. * @param {SortFunction} sortFn Sorting comparer function
  61. * @returns {void}
  62. */
  63. sortWith(sortFn) {
  64. if (this.size <= 1 || sortFn === this._lastActiveSortFn) {
  65. // already sorted - nothing to do
  66. return;
  67. }
  68. const sortedArray = Array.from(this).sort(sortFn);
  69. super.clear();
  70. for (let i = 0; i < sortedArray.length; i += 1) {
  71. super.add(sortedArray[i]);
  72. }
  73. this._lastActiveSortFn = sortFn;
  74. this._invalidateCache();
  75. }
  76. sort() {
  77. this.sortWith(this._sortFn);
  78. return this;
  79. }
  80. /**
  81. * Get data from cache
  82. * @template R
  83. * @param {function(SortableSet<T>): R} fn function to calculate value
  84. * @returns {R} returns result of fn(this), cached until set changes
  85. */
  86. getFromCache(fn) {
  87. if (this._cache === undefined) {
  88. this._cache = new Map();
  89. } else {
  90. const result = this._cache.get(fn);
  91. const data = /** @type {R} */ (result);
  92. if (data !== undefined) {
  93. return data;
  94. }
  95. }
  96. const newData = fn(this);
  97. this._cache.set(fn, newData);
  98. return newData;
  99. }
  100. /**
  101. * Get data from cache (ignoring sorting)
  102. * @template R
  103. * @param {function(SortableSet<T>): R} fn function to calculate value
  104. * @returns {R} returns result of fn(this), cached until set changes
  105. */
  106. getFromUnorderedCache(fn) {
  107. if (this._cacheOrderIndependent === undefined) {
  108. this._cacheOrderIndependent = new Map();
  109. } else {
  110. const result = this._cacheOrderIndependent.get(fn);
  111. const data = /** @type {R} */ (result);
  112. if (data !== undefined) {
  113. return data;
  114. }
  115. }
  116. const newData = fn(this);
  117. this._cacheOrderIndependent.set(fn, newData);
  118. return newData;
  119. }
  120. /**
  121. * @private
  122. * @returns {void}
  123. */
  124. _invalidateCache() {
  125. if (this._cache !== undefined) {
  126. this._cache.clear();
  127. }
  128. }
  129. /**
  130. * @private
  131. * @returns {void}
  132. */
  133. _invalidateOrderedCache() {
  134. if (this._cacheOrderIndependent !== undefined) {
  135. this._cacheOrderIndependent.clear();
  136. }
  137. }
  138. /**
  139. * @returns {T[]} the raw array
  140. */
  141. toJSON() {
  142. return Array.from(this);
  143. }
  144. }
  145. module.exports = SortableSet;