LazyBucketSortedSet.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { first } = require("./SetHelpers");
  7. const SortableSet = require("./SortableSet");
  8. /**
  9. * @template T
  10. * @typedef {LazyBucketSortedSet<T, any> | SortableSet<T>} Entry
  11. */
  12. /**
  13. * @template T
  14. * @typedef {(function(T): any) | (function(any, any): number)} Arg
  15. */
  16. /**
  17. * Multi layer bucket sorted set:
  18. * Supports adding non-existing items (DO NOT ADD ITEM TWICE),
  19. * Supports removing exiting items (DO NOT REMOVE ITEM NOT IN SET),
  20. * Supports popping the first items according to defined order,
  21. * Supports iterating all items without order,
  22. * Supports updating an item in an efficient way,
  23. * Supports size property, which is the number of items,
  24. * Items are lazy partially sorted when needed
  25. * @template T
  26. * @template K
  27. */
  28. class LazyBucketSortedSet {
  29. /**
  30. * @param {function(T): K} getKey function to get key from item
  31. * @param {function(K, K): number} comparator comparator to sort keys
  32. * @param {...Arg<T>} args more pairs of getKey and comparator plus optional final comparator for the last layer
  33. */
  34. constructor(getKey, comparator, ...args) {
  35. this._getKey = getKey;
  36. /** @type {Arg<T>[]} */
  37. this._innerArgs = args;
  38. this._leaf = args.length <= 1;
  39. this._keys = new SortableSet(undefined, comparator);
  40. /** @type {Map<K, Entry<T>>} */
  41. this._map = new Map();
  42. this._unsortedItems = new Set();
  43. this.size = 0;
  44. }
  45. /**
  46. * @param {T} item an item
  47. * @returns {void}
  48. */
  49. add(item) {
  50. this.size++;
  51. this._unsortedItems.add(item);
  52. }
  53. /**
  54. * @param {K} key key of item
  55. * @param {T} item the item
  56. * @returns {void}
  57. */
  58. _addInternal(key, item) {
  59. let entry = this._map.get(key);
  60. if (entry === undefined) {
  61. entry =
  62. /** @type {Entry<T>} */
  63. (
  64. this._leaf
  65. ? new SortableSet(undefined, this._innerArgs[0])
  66. : new /** @type {TODO} */ (LazyBucketSortedSet)(...this._innerArgs)
  67. );
  68. this._keys.add(key);
  69. this._map.set(key, entry);
  70. }
  71. /** @type {Entry<T>} */
  72. (entry).add(item);
  73. }
  74. /**
  75. * @param {T} item an item
  76. * @returns {void}
  77. */
  78. delete(item) {
  79. this.size--;
  80. if (this._unsortedItems.has(item)) {
  81. this._unsortedItems.delete(item);
  82. return;
  83. }
  84. const key = this._getKey(item);
  85. const entry = /** @type {Entry<T>} */ (this._map.get(key));
  86. entry.delete(item);
  87. if (entry.size === 0) {
  88. this._deleteKey(key);
  89. }
  90. }
  91. /**
  92. * @param {K} key key to be removed
  93. * @returns {void}
  94. */
  95. _deleteKey(key) {
  96. this._keys.delete(key);
  97. this._map.delete(key);
  98. }
  99. /**
  100. * @returns {T | undefined} an item
  101. */
  102. popFirst() {
  103. if (this.size === 0) return;
  104. this.size--;
  105. if (this._unsortedItems.size > 0) {
  106. for (const item of this._unsortedItems) {
  107. const key = this._getKey(item);
  108. this._addInternal(key, item);
  109. }
  110. this._unsortedItems.clear();
  111. }
  112. this._keys.sort();
  113. const key = /** @type {K} */ (first(this._keys));
  114. const entry = this._map.get(key);
  115. if (this._leaf) {
  116. const leafEntry = /** @type {SortableSet<T>} */ (entry);
  117. leafEntry.sort();
  118. const item = /** @type {T} */ (first(leafEntry));
  119. leafEntry.delete(item);
  120. if (leafEntry.size === 0) {
  121. this._deleteKey(key);
  122. }
  123. return item;
  124. }
  125. const nodeEntry = /** @type {LazyBucketSortedSet<T, any>} */ (entry);
  126. const item = nodeEntry.popFirst();
  127. if (nodeEntry.size === 0) {
  128. this._deleteKey(key);
  129. }
  130. return item;
  131. }
  132. /**
  133. * @param {T} item to be updated item
  134. * @returns {function(true=): void} finish update
  135. */
  136. startUpdate(item) {
  137. if (this._unsortedItems.has(item)) {
  138. return remove => {
  139. if (remove) {
  140. this._unsortedItems.delete(item);
  141. this.size--;
  142. }
  143. };
  144. }
  145. const key = this._getKey(item);
  146. if (this._leaf) {
  147. const oldEntry = /** @type {SortableSet<T>} */ (this._map.get(key));
  148. return remove => {
  149. if (remove) {
  150. this.size--;
  151. oldEntry.delete(item);
  152. if (oldEntry.size === 0) {
  153. this._deleteKey(key);
  154. }
  155. return;
  156. }
  157. const newKey = this._getKey(item);
  158. if (key === newKey) {
  159. // This flags the sortable set as unordered
  160. oldEntry.add(item);
  161. } else {
  162. oldEntry.delete(item);
  163. if (oldEntry.size === 0) {
  164. this._deleteKey(key);
  165. }
  166. this._addInternal(newKey, item);
  167. }
  168. };
  169. }
  170. const oldEntry = /** @type {LazyBucketSortedSet<T, any>} */ (
  171. this._map.get(key)
  172. );
  173. const finishUpdate = oldEntry.startUpdate(item);
  174. return remove => {
  175. if (remove) {
  176. this.size--;
  177. finishUpdate(true);
  178. if (oldEntry.size === 0) {
  179. this._deleteKey(key);
  180. }
  181. return;
  182. }
  183. const newKey = this._getKey(item);
  184. if (key === newKey) {
  185. finishUpdate();
  186. } else {
  187. finishUpdate(true);
  188. if (oldEntry.size === 0) {
  189. this._deleteKey(key);
  190. }
  191. this._addInternal(newKey, item);
  192. }
  193. };
  194. }
  195. /**
  196. * @param {Iterator<T>[]} iterators list of iterators to append to
  197. * @returns {void}
  198. */
  199. _appendIterators(iterators) {
  200. if (this._unsortedItems.size > 0)
  201. iterators.push(this._unsortedItems[Symbol.iterator]());
  202. for (const key of this._keys) {
  203. const entry = this._map.get(key);
  204. if (this._leaf) {
  205. const leafEntry = /** @type {SortableSet<T>} */ (entry);
  206. const iterator = leafEntry[Symbol.iterator]();
  207. iterators.push(iterator);
  208. } else {
  209. const nodeEntry = /** @type {LazyBucketSortedSet<T, any>} */ (entry);
  210. nodeEntry._appendIterators(iterators);
  211. }
  212. }
  213. }
  214. /**
  215. * @returns {Iterator<T>} the iterator
  216. */
  217. [Symbol.iterator]() {
  218. /** @type {Iterator<T>[]} */
  219. const iterators = [];
  220. this._appendIterators(iterators);
  221. iterators.reverse();
  222. let currentIterator =
  223. /** @type {Iterator<T>} */
  224. (iterators.pop());
  225. return {
  226. next: () => {
  227. const res = currentIterator.next();
  228. if (res.done) {
  229. if (iterators.length === 0) return res;
  230. currentIterator = /** @type {Iterator<T>} */ (iterators.pop());
  231. return currentIterator.next();
  232. }
  233. return res;
  234. }
  235. };
  236. }
  237. }
  238. module.exports = LazyBucketSortedSet;