LazyBucketSortedSet.js 6.1 KB

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