| | |
| | | const SortableSet = require("./SortableSet"); |
| | | |
| | | /** |
| | | * Callback that extracts the grouping key for an item at one bucket layer. |
| | | * @template T |
| | | * @template K |
| | | * @typedef {(item: T) => K} GetKey |
| | | */ |
| | | |
| | | /** |
| | | * Comparison function used to order keys or leaf items. |
| | | * @template T |
| | | * @typedef {(a: T, n: T) => number} Comparator |
| | | */ |
| | | |
| | | /** |
| | | * Internal bucket entry, either another nested bucket set or a sorted leaf set. |
| | | * @template T |
| | | * @template K |
| | | * @typedef {LazyBucketSortedSet<T, K> | SortableSet<T>} Entry |
| | | */ |
| | | |
| | | /** |
| | | * Constructor argument accepted for nested bucket layers or the final leaf |
| | | * comparator. |
| | | * @template T |
| | | * @template K |
| | | * @typedef {GetKey<T, K> | Comparator<K> | Comparator<T>} Arg |
| | |
| | | */ |
| | | class LazyBucketSortedSet { |
| | | /** |
| | | * Creates a lazily sorted, potentially multi-level bucket structure whose |
| | | * order is only fully resolved when items are popped. |
| | | * @param {GetKey<T, K>} getKey function to get key from item |
| | | * @param {Comparator<K>=} comparator comparator to sort keys |
| | | * @param {...Arg<T, K>} args more pairs of getKey and comparator plus optional final comparator for the last layer |
| | |
| | | this._keys = new SortableSet(undefined, comparator); |
| | | /** @type {Map<K, Entry<T, K>>} */ |
| | | this._map = new Map(); |
| | | /** @type {Set<T>} */ |
| | | this._unsortedItems = new Set(); |
| | | this.size = 0; |
| | | } |
| | | |
| | | /** |
| | | * Adds an item to the unsorted staging area so sorting can be deferred until |
| | | * an ordered pop is requested. |
| | | * @param {T} item an item |
| | | * @returns {void} |
| | | */ |
| | |
| | | } |
| | | |
| | | /** |
| | | * Inserts an item into the correct nested bucket, creating intermediate |
| | | * bucket structures on demand. |
| | | * @param {K} key key of item |
| | | * @param {T} item the item |
| | | * @returns {void} |
| | |
| | | } |
| | | |
| | | /** |
| | | * Removes an item from either the unsorted staging area or its resolved |
| | | * bucket and prunes empty buckets as needed. |
| | | * @param {T} item an item |
| | | * @returns {void} |
| | | */ |
| | |
| | | } |
| | | |
| | | /** |
| | | * Removes an empty bucket key and its corresponding nested entry. |
| | | * @param {K} key key to be removed |
| | | * @returns {void} |
| | | */ |
| | |
| | | } |
| | | |
| | | /** |
| | | * Removes and returns the smallest item according to the configured bucket |
| | | * order, sorting only the portions of the structure that are needed. |
| | | * @returns {T | undefined} an item |
| | | */ |
| | | popFirst() { |
| | |
| | | } |
| | | |
| | | /** |
| | | * Begins an in-place update for an item and returns a completion callback |
| | | * that can either reinsert it under a new key or remove it entirely. |
| | | * @param {T} item to be updated item |
| | | * @returns {(remove?: true) => void} finish update |
| | | */ |
| | |
| | | } |
| | | |
| | | /** |
| | | * Appends iterators for every stored bucket and leaf to support unordered |
| | | * traversal across the entire structure. |
| | | * @param {Iterator<T>[]} iterators list of iterators to append to |
| | | * @returns {void} |
| | | */ |
| | |
| | | } |
| | | |
| | | /** |
| | | * Iterates over all stored items without imposing bucket sort order. |
| | | * @returns {Iterator<T>} the iterator |
| | | */ |
| | | [Symbol.iterator]() { |