You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SortableSet.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. "use strict";
  2. /**
  3. * A subset of Set that offers sorting functionality
  4. * @template T item type in set
  5. * @extends {Set<T>}
  6. */
  7. class SortableSet extends Set {
  8. /**
  9. * Create a new sortable set
  10. * @param {Iterable<T>=} initialIterable The initial iterable value
  11. * @typedef {function(T, T): number} SortFunction
  12. * @param {SortFunction=} defaultSort Default sorting function
  13. */
  14. constructor(initialIterable, defaultSort) {
  15. super(initialIterable);
  16. /** @private @type {function(T, T): number}} */
  17. this._sortFn = defaultSort;
  18. /** @private @type {function(T, T): number} | null} */
  19. this._lastActiveSortFn = null;
  20. /** @private @type {Map<Function, T[]> | undefined} */
  21. this._cache = undefined;
  22. /** @private @type {Map<Function, T[]|string|number> | undefined} */
  23. this._cacheOrderIndependent = undefined;
  24. }
  25. /**
  26. * @param {T} value value to add to set
  27. * @returns {this} returns itself
  28. */
  29. add(value) {
  30. this._lastActiveSortFn = null;
  31. this._invalidateCache();
  32. this._invalidateOrderedCache();
  33. super.add(value);
  34. return this;
  35. }
  36. /**
  37. * @param {T} value value to delete
  38. * @returns {boolean} true if value existed in set, false otherwise
  39. */
  40. delete(value) {
  41. this._invalidateCache();
  42. this._invalidateOrderedCache();
  43. return super.delete(value);
  44. }
  45. /**
  46. * @returns {void}
  47. */
  48. clear() {
  49. this._invalidateCache();
  50. this._invalidateOrderedCache();
  51. return super.clear();
  52. }
  53. /**
  54. * Sort with a comparer function
  55. * @param {SortFunction} sortFn Sorting comparer function
  56. * @returns {void}
  57. */
  58. sortWith(sortFn) {
  59. if (this.size <= 1 || sortFn === this._lastActiveSortFn) {
  60. // already sorted - nothing to do
  61. return;
  62. }
  63. const sortedArray = Array.from(this).sort(sortFn);
  64. super.clear();
  65. for (let i = 0; i < sortedArray.length; i += 1) {
  66. super.add(sortedArray[i]);
  67. }
  68. this._lastActiveSortFn = sortFn;
  69. this._invalidateCache();
  70. }
  71. sort() {
  72. this.sortWith(this._sortFn);
  73. }
  74. /**
  75. * Get data from cache
  76. * @param {function(SortableSet<T>): T[]} fn function to calculate value
  77. * @returns {T[]} returns result of fn(this), cached until set changes
  78. */
  79. getFromCache(fn) {
  80. if (this._cache === undefined) {
  81. this._cache = new Map();
  82. } else {
  83. const data = this._cache.get(fn);
  84. if (data !== undefined) {
  85. return data;
  86. }
  87. }
  88. const newData = fn(this);
  89. this._cache.set(fn, newData);
  90. return newData;
  91. }
  92. /**
  93. * @param {function(SortableSet<T>): string|number|T[]} fn function to calculate value
  94. * @returns {any} returns result of fn(this), cached until set changes
  95. */
  96. getFromUnorderedCache(fn) {
  97. if (this._cacheOrderIndependent === undefined) {
  98. this._cacheOrderIndependent = new Map();
  99. } else {
  100. const data = this._cacheOrderIndependent.get(fn);
  101. if (data !== undefined) {
  102. return data;
  103. }
  104. }
  105. const newData = fn(this);
  106. this._cacheOrderIndependent.set(fn, newData);
  107. return newData;
  108. }
  109. /**
  110. * @private
  111. * @returns {void}
  112. */
  113. _invalidateCache() {
  114. if (this._cache !== undefined) {
  115. this._cache.clear();
  116. }
  117. }
  118. /**
  119. * @private
  120. * @returns {void}
  121. */
  122. _invalidateOrderedCache() {
  123. if (this._cacheOrderIndependent !== undefined) {
  124. this._cacheOrderIndependent.clear();
  125. }
  126. }
  127. }
  128. module.exports = SortableSet;