max-priority-queue-typed 2.3.0 → 2.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/dist/types/data-structures/base/linear-base.d.ts +6 -6
  2. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +6 -6
  3. package/dist/types/data-structures/binary-tree/bst.d.ts +2 -1
  4. package/dist/types/data-structures/binary-tree/index.d.ts +3 -3
  5. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +150 -20
  6. package/dist/types/data-structures/binary-tree/tree-map.d.ts +188 -0
  7. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +238 -147
  8. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +270 -0
  9. package/dist/types/data-structures/binary-tree/tree-set.d.ts +181 -0
  10. package/dist/types/interfaces/binary-tree.d.ts +2 -2
  11. package/dist/types/types/data-structures/binary-tree/index.d.ts +3 -3
  12. package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +33 -0
  13. package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +16 -0
  14. package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +33 -0
  15. package/package.json +2 -2
  16. package/src/data-structures/base/linear-base.ts +2 -12
  17. package/src/data-structures/binary-tree/avl-tree.ts +1 -1
  18. package/src/data-structures/binary-tree/binary-tree.ts +45 -21
  19. package/src/data-structures/binary-tree/bst.ts +85 -10
  20. package/src/data-structures/binary-tree/index.ts +3 -3
  21. package/src/data-structures/binary-tree/red-black-tree.ts +568 -76
  22. package/src/data-structures/binary-tree/tree-map.ts +439 -0
  23. package/src/data-structures/binary-tree/tree-multi-map.ts +488 -325
  24. package/src/data-structures/binary-tree/tree-multi-set.ts +502 -0
  25. package/src/data-structures/binary-tree/tree-set.ts +407 -0
  26. package/src/data-structures/queue/deque.ts +10 -0
  27. package/src/interfaces/binary-tree.ts +2 -2
  28. package/src/types/data-structures/binary-tree/index.ts +3 -3
  29. package/src/types/data-structures/binary-tree/tree-map.ts +45 -0
  30. package/src/types/data-structures/binary-tree/tree-multi-set.ts +19 -0
  31. package/src/types/data-structures/binary-tree/tree-set.ts +39 -0
  32. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +0 -236
  33. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -197
  34. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +0 -243
  35. package/dist/types/types/data-structures/binary-tree/avl-tree-counter.d.ts +0 -2
  36. package/dist/types/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -2
  37. package/dist/types/types/data-structures/binary-tree/tree-counter.d.ts +0 -2
  38. package/src/data-structures/binary-tree/avl-tree-counter.ts +0 -539
  39. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +0 -438
  40. package/src/data-structures/binary-tree/tree-counter.ts +0 -575
  41. package/src/types/data-structures/binary-tree/avl-tree-counter.ts +0 -3
  42. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +0 -3
  43. package/src/types/data-structures/binary-tree/tree-counter.ts +0 -3
@@ -2,118 +2,28 @@
2
2
  * data-structure-typed
3
3
  *
4
4
  * @author Pablo Zeng
5
- * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
5
+ * @copyright Copyright (c) 2022 Pablo Zeng
6
6
  * @license MIT License
7
7
  */
8
- import type { ElemOf, EntryCallback, FamilyPosition, RBTNColor, TreeMultiMapOptions } from '../../types';
9
- import { RedBlackTree, RedBlackTreeNode } from './red-black-tree';
10
- import { IBinaryTree } from '../../interfaces';
8
+ import type { Comparator, TreeMultiMapOptions } from '../../types';
9
+ import { Range } from '../../common';
10
+ import { RedBlackTreeNode } from './red-black-tree';
11
11
  /**
12
- * Node used by TreeMultiMap; stores the key with a bucket of values (array).
13
- * @remarks Time O(1), Space O(1)
14
- * @template K
15
- * @template V
12
+ * Node type used by TreeMultiMap (alias to RedBlackTreeNode for backward compatibility).
13
+ *
14
+ * @deprecated Direct node manipulation is discouraged. Use TreeMultiMap methods instead.
16
15
  */
17
- export declare class TreeMultiMapNode<K = any, V = any> {
18
- key: K;
19
- value?: V[];
20
- parent?: TreeMultiMapNode<K, V>;
21
- /**
22
- * Create a TreeMultiMap node with an optional value bucket.
23
- * @remarks Time O(1), Space O(1)
24
- * @param key - Key of the node.
25
- * @param [value] - Initial array of values.
26
- * @returns New TreeMultiMapNode instance.
27
- */
28
- constructor(key: K, value?: V[], color?: RBTNColor);
29
- _left?: TreeMultiMapNode<K, V> | null | undefined;
30
- /**
31
- * Get the left child pointer.
32
- * @remarks Time O(1), Space O(1)
33
- * @returns Left child node, or null/undefined.
34
- */
35
- get left(): TreeMultiMapNode<K, V> | null | undefined;
36
- /**
37
- * Set the left child and update its parent pointer.
38
- * @remarks Time O(1), Space O(1)
39
- * @param v - New left child node, or null/undefined.
40
- * @returns void
41
- */
42
- set left(v: TreeMultiMapNode<K, V> | null | undefined);
43
- _right?: TreeMultiMapNode<K, V> | null | undefined;
44
- /**
45
- * Get the right child pointer.
46
- * @remarks Time O(1), Space O(1)
47
- * @returns Right child node, or null/undefined.
48
- */
49
- get right(): TreeMultiMapNode<K, V> | null | undefined;
50
- /**
51
- * Set the right child and update its parent pointer.
52
- * @remarks Time O(1), Space O(1)
53
- * @param v - New right child node, or null/undefined.
54
- * @returns void
55
- */
56
- set right(v: TreeMultiMapNode<K, V> | null | undefined);
57
- _height: number;
58
- /**
59
- * Gets the height of the node (used in self-balancing trees).
60
- * @remarks Time O(1), Space O(1)
61
- *
62
- * @returns The height.
63
- */
64
- get height(): number;
65
- /**
66
- * Sets the height of the node.
67
- * @remarks Time O(1), Space O(1)
68
- *
69
- * @param value - The new height.
70
- */
71
- set height(value: number);
72
- _color: RBTNColor;
73
- /**
74
- * Gets the color of the node (used in Red-Black trees).
75
- * @remarks Time O(1), Space O(1)
76
- *
77
- * @returns The node's color.
78
- */
79
- get color(): RBTNColor;
80
- /**
81
- * Sets the color of the node.
82
- * @remarks Time O(1), Space O(1)
83
- *
84
- * @param value - The new color.
85
- */
86
- set color(value: RBTNColor);
87
- _count: number;
88
- /**
89
- * Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
90
- * @remarks Time O(1), Space O(1)
91
- *
92
- * @returns The subtree node count.
93
- */
94
- get count(): number;
95
- /**
96
- * Sets the count of nodes in the subtree.
97
- * @remarks Time O(1), Space O(1)
98
- *
99
- * @param value - The new count.
100
- */
101
- set count(value: number);
102
- /**
103
- * Gets the position of the node relative to its parent.
104
- * @remarks Time O(1), Space O(1)
105
- *
106
- * @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
107
- */
108
- get familyPosition(): FamilyPosition;
16
+ export declare class TreeMultiMapNode<K = any, V = any> extends RedBlackTreeNode<K, V[]> {
17
+ constructor(key: K, value?: V[]);
109
18
  }
110
19
  /**
111
- * Red-Black Tree–based multimap (key → array of values). Preserves O(log N) updates and supports map-like mode.
112
- * @remarks Time O(1), Space O(1)
113
- * @template K
114
- * @template V
115
- * @template R
20
+ * TreeMultiMap (ordered MultiMap) key → bucket (Array of values).
116
21
  *
22
+ * Semantics (RFC):
23
+ * - Bucketed design: each key appears once; duplicates live in the bucket.
24
+ * - `get(key)` returns a **live** bucket reference.
25
+ * - Default iteration yields bucket entries: `[K, V[]]`.
26
+ * - Navigable operations (`first/last/ceiling/...`) return entry tuples like TreeMap.
117
27
  * @example
118
28
  * // players ranked by score with their equipment
119
29
  * type Equipment = {
@@ -241,7 +151,7 @@ export declare class TreeMultiMapNode<K = any, V = any> {
241
151
  * isMapMode: false
242
152
  * });
243
153
  *
244
- * const topPlayersEquipments = playerRankings.rangeSearch([8900, 10000], node => playerRankings.get(node));
154
+ * const topPlayersEquipments = playerRankings.rangeSearch([8900, 10000], node => playerRankings.get(node.key));
245
155
  * console.log(topPlayersEquipments); // [
246
156
  * // [
247
157
  * // {
@@ -279,55 +189,236 @@ export declare class TreeMultiMapNode<K = any, V = any> {
279
189
  * // ]
280
190
  * // ];
281
191
  */
282
- export declare class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTree<K, V[], R> implements IBinaryTree<K, V[], R> {
192
+ export declare class TreeMultiMap<K = any, V = any, R = any> implements Iterable<[K, V[]]> {
193
+ #private;
283
194
  /**
284
- * Create a TreeMultiMap and optionally bulk-insert items.
285
- * @remarks Time O(N log N), Space O(N)
286
- * @param [keysNodesEntriesOrRaws] - Iterable of keys/nodes/entries/raw items to insert.
287
- * @param [options] - Options for TreeMultiMap (comparator, reverse, map mode).
288
- * @returns New TreeMultiMap instance.
195
+ * Creates a new TreeMultiMap.
196
+ * @param keysNodesEntriesOrRaws - Initial entries, or raw elements if `toEntryFn` is provided.
197
+ * @param options - Configuration options including optional `toEntryFn` to transform raw elements.
198
+ * @remarks Time O(m log m), Space O(m) where m is the number of initial entries
199
+ * @example
200
+ * // Standard usage with entries
201
+ * const mmap = new TreeMultiMap([['a', ['x', 'y']], ['b', ['z']]]);
202
+ *
203
+ * // Using toEntryFn to transform raw objects
204
+ * const players = [{ score: 100, items: ['sword'] }, { score: 200, items: ['shield', 'bow'] }];
205
+ * const mmap = new TreeMultiMap(players, { toEntryFn: p => [p.score, p.items] });
289
206
  */
290
- constructor(keysNodesEntriesOrRaws?: Iterable<K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | R>, options?: TreeMultiMapOptions<K, V[], R>);
291
- createNode(key: K, value?: V[]): TreeMultiMapNode<K, V>;
207
+ constructor(keysNodesEntriesOrRaws?: Iterable<K | [K | null | undefined, V[] | undefined] | null | undefined | R>, options?: TreeMultiMapOptions<K, V[], R>);
292
208
  /**
293
- * Checks if the given item is a `TreeMultiMapNode` instance.
209
+ * Validates the key against the default comparator rules.
294
210
  * @remarks Time O(1), Space O(1)
295
- *
296
- * @param keyNodeOrEntry - The item to check.
297
- * @returns True if it's a TreeMultiMapNode, false otherwise.
298
211
  */
299
- isNode(keyNodeOrEntry: K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined): keyNodeOrEntry is TreeMultiMapNode<K, V>;
300
- set(keyNodeOrEntry: K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined): boolean;
212
+ private _validateKey;
213
+ /**
214
+ * Number of distinct keys.
215
+ * @remarks Time O(1), Space O(1)
216
+ */
217
+ get size(): number;
218
+ /**
219
+ * Whether the map is empty.
220
+ * @remarks Time O(1), Space O(1)
221
+ */
222
+ isEmpty(): boolean;
223
+ /**
224
+ * Removes all entries from the map.
225
+ * @remarks Time O(1), Space O(1)
226
+ */
227
+ clear(): void;
228
+ /**
229
+ * Bucket length for a key (missing => 0).
230
+ * @remarks Time O(log n), Space O(1)
231
+ */
232
+ count(key: K): number;
233
+ /**
234
+ * Total number of values across all buckets (Σ bucket.length).
235
+ * @remarks Time O(n), Space O(1)
236
+ */
237
+ get totalSize(): number;
238
+ /**
239
+ * Whether the map contains the given key.
240
+ * @remarks Time O(log n), Space O(1)
241
+ */
242
+ has(key: K): boolean;
243
+ /**
244
+ * Live bucket reference (do not auto-delete key if bucket becomes empty via mutation).
245
+ * @remarks Time O(log n), Space O(1)
246
+ */
247
+ get(key: K): V[] | undefined;
248
+ /**
249
+ * Append a single value.
250
+ * @remarks Time O(log n), Space O(1)
251
+ */
252
+ add(key: K, value: V): boolean;
253
+ /**
254
+ * Alias for compatibility with existing TreeMultiMap semantics.
255
+ * @remarks Time O(log n), Space O(1) for single value; O(log n + m) for bucket append
256
+ */
257
+ set(entry: [K | null | undefined, V[] | undefined] | K | null | undefined, value?: V): boolean;
301
258
  set(key: K, value: V): boolean;
302
259
  /**
303
- * Delete a single value from the bucket at a given key. Removes the key if the bucket becomes empty.
304
- * @remarks Time O(log N), Space O(1)
305
- * @param keyNodeOrEntry - Key, node, or [key, values] entry to locate the bucket.
306
- * @param value - Value to remove from the bucket.
307
- * @returns True if the value was removed; false if not found.
260
+ * Deletes a key and its entire bucket.
261
+ * @remarks Time O(log n), Space O(1)
262
+ */
263
+ delete(key: K): boolean;
264
+ /**
265
+ * Check if a specific value exists in a key's bucket.
266
+ * @remarks Time O(log n + m), Space O(1) where m is bucket size
267
+ */
268
+ hasEntry(key: K, value: V, eq?: (a: V, b: V) => boolean): boolean;
269
+ /**
270
+ * Delete a single occurrence of a value from a key's bucket.
271
+ * @remarks Time O(log n + m), Space O(1) where m is bucket size
272
+ */
273
+ deleteValue(key: K, value: V, eq?: (a: V, b: V) => boolean): boolean;
274
+ /**
275
+ * Delete all occurrences of a value from a key's bucket.
276
+ * @remarks Time O(log n + m), Space O(1) where m is bucket size
277
+ */
278
+ deleteValues(key: K, value: V, eq?: (a: V, b: V) => boolean): number;
279
+ /**
280
+ * Iterates over all entries as [key, bucket] pairs.
281
+ * @remarks Time O(n), Space O(1)
282
+ */
283
+ [Symbol.iterator](): Iterator<[K, V[]]>;
284
+ /**
285
+ * Iterates over all keys.
286
+ * @remarks Time O(n), Space O(1)
287
+ */
288
+ keys(): IterableIterator<K>;
289
+ /**
290
+ * Iterates over all buckets.
291
+ * @remarks Time O(n), Space O(1)
292
+ */
293
+ values(): IterableIterator<V[]>;
294
+ /**
295
+ * Iterates over all entries for a specific key.
296
+ * @remarks Time O(log n + m), Space O(1) where m is bucket size
297
+ */
298
+ entriesOf(key: K): IterableIterator<[K, V]>;
299
+ /**
300
+ * Iterates over all values for a specific key.
301
+ * @remarks Time O(log n + m), Space O(1) where m is bucket size
302
+ */
303
+ valuesOf(key: K): IterableIterator<V>;
304
+ /**
305
+ * Iterates over all [key, value] pairs (flattened from buckets).
306
+ * @remarks Time O(T), Space O(1) where T is totalSize
307
+ */
308
+ flatEntries(): IterableIterator<[K, V]>;
309
+ /**
310
+ * Returns the entry with the smallest key.
311
+ * @remarks Time O(log n), Space O(1)
312
+ * @example
313
+ * const map = new TreeMultiMap([[1, ['a']], [2, ['b']]]);
314
+ * map.first(); // [1, ['a']]
315
+ */
316
+ first(): [K, V[]] | undefined;
317
+ /**
318
+ * Returns the entry with the largest key.
319
+ * @remarks Time O(log n), Space O(1)
320
+ * @example
321
+ * const map = new TreeMultiMap([[1, ['a']], [2, ['b']]]);
322
+ * map.last(); // [2, ['b']]
308
323
  */
309
- deleteValue(keyNodeOrEntry: K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined, value: V): boolean;
310
- map<MK = K, MVArr extends unknown[] = V[], MR = any>(callback: EntryCallback<K, V[] | undefined, [MK, MVArr]>, options?: Partial<TreeMultiMapOptions<MK, MVArr, MR>>, thisArg?: unknown): TreeMultiMap<MK, ElemOf<MVArr>, MR>;
311
- map<MK = K, MV = V[], MR = any>(callback: EntryCallback<K, V[] | undefined, [MK, MV]>, options?: Partial<TreeMultiMapOptions<MK, MV, MR>>, thisArg?: unknown): RedBlackTree<MK, MV, MR>;
324
+ last(): [K, V[]] | undefined;
312
325
  /**
313
- * (Protected) Create an empty instance of the same concrete class.
326
+ * Removes and returns the entry with the smallest key.
327
+ * @remarks Time O(log n), Space O(1)
328
+ * @example
329
+ * const map = new TreeMultiMap([[1, ['a']], [2, ['b']]]);
330
+ * map.pollFirst(); // [1, ['a']]
331
+ * map.has(1); // false
332
+ */
333
+ pollFirst(): [K, V[]] | undefined;
334
+ /**
335
+ * Removes and returns the entry with the largest key.
336
+ * @remarks Time O(log n), Space O(1)
337
+ * @example
338
+ * const map = new TreeMultiMap([[1, ['a']], [2, ['b']]]);
339
+ * map.pollLast(); // [2, ['b']]
340
+ * map.has(2); // false
341
+ */
342
+ pollLast(): [K, V[]] | undefined;
343
+ /**
344
+ * Returns the entry with the smallest key >= given key.
345
+ * @remarks Time O(log n), Space O(1)
346
+ * @example
347
+ * const map = new TreeMultiMap([[10, ['a']], [20, ['b']], [30, ['c']]]);
348
+ * map.ceiling(15); // [20, ['b']]
349
+ * map.ceiling(20); // [20, ['b']]
350
+ */
351
+ ceiling(key: K): [K, V[]] | undefined;
352
+ /**
353
+ * Returns the entry with the largest key <= given key.
354
+ * @remarks Time O(log n), Space O(1)
355
+ * @example
356
+ * const map = new TreeMultiMap([[10, ['a']], [20, ['b']], [30, ['c']]]);
357
+ * map.floor(25); // [20, ['b']]
358
+ * map.floor(20); // [20, ['b']]
359
+ */
360
+ floor(key: K): [K, V[]] | undefined;
361
+ /**
362
+ * Returns the entry with the smallest key > given key.
363
+ * @remarks Time O(log n), Space O(1)
364
+ * @example
365
+ * const map = new TreeMultiMap([[10, ['a']], [20, ['b']], [30, ['c']]]);
366
+ * map.higher(10); // [20, ['b']]
367
+ * map.higher(15); // [20, ['b']]
368
+ */
369
+ higher(key: K): [K, V[]] | undefined;
370
+ /**
371
+ * Returns the entry with the largest key < given key.
372
+ * @remarks Time O(log n), Space O(1)
373
+ * @example
374
+ * const map = new TreeMultiMap([[10, ['a']], [20, ['b']], [30, ['c']]]);
375
+ * map.lower(20); // [10, ['a']]
376
+ * map.lower(15); // [10, ['a']]
377
+ */
378
+ lower(key: K): [K, V[]] | undefined;
379
+ /**
380
+ * Prints the internal tree structure (for debugging).
381
+ * @remarks Time O(n), Space O(n)
382
+ */
383
+ print(): void;
384
+ /**
385
+ * Executes a callback for each entry.
386
+ * @remarks Time O(n), Space O(1)
387
+ */
388
+ forEach(callback: (value: V[], key: K, map: this) => void): void;
389
+ /**
390
+ * Creates a new map with entries that pass the predicate.
391
+ * @remarks Time O(n), Space O(n)
392
+ */
393
+ filter(predicate: (value: V[], key: K, map: this) => boolean): TreeMultiMap<K, V, R>;
394
+ /**
395
+ * Creates a new map by transforming each entry.
396
+ * @remarks Time O(n log n), Space O(n)
397
+ */
398
+ map<V2>(mapper: (value: V[], key: K, map: this) => [K, V2[]]): TreeMultiMap<K, V2, R>;
399
+ /**
400
+ * Reduces all entries to a single value.
401
+ * @remarks Time O(n), Space O(1)
402
+ */
403
+ reduce<U>(callback: (accumulator: U, value: V[], key: K, map: this) => U, initialValue: U): U;
404
+ /**
405
+ * Sets multiple entries at once.
406
+ * @remarks Time O(m log n), Space O(m) where m is input size
407
+ */
408
+ setMany(keysNodesEntriesOrRaws: Iterable<K | [K | null | undefined, V[] | undefined]>): boolean[];
409
+ /**
410
+ * Searches for entries within a key range.
411
+ * @remarks Time O(log n + k), Space O(k) where k is result size
412
+ */
413
+ rangeSearch<C extends (node: RedBlackTreeNode<K, V[]>) => unknown>(range: Range<K> | [K, K], callback?: C): ReturnType<C>[];
414
+ /**
415
+ * Creates a shallow clone of this map.
416
+ * @remarks Time O(n log n), Space O(n)
417
+ */
418
+ clone(): TreeMultiMap<K, V, R>;
419
+ /**
420
+ * Expose comparator for advanced usage/testing (read-only).
314
421
  * @remarks Time O(1), Space O(1)
315
- * @template TK
316
- * @template TV
317
- * @template TR
318
- * @param [options] - Optional constructor options for the like-kind instance.
319
- * @returns An empty like-kind instance.
320
- */
321
- protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<TreeMultiMapOptions<TK, TV, TR>>): this;
322
- /**
323
- * (Protected) Create a like-kind instance and seed it from an iterable.
324
- * @remarks Time O(N log N), Space O(N)
325
- * @template TK
326
- * @template TV
327
- * @template TR
328
- * @param iter - Iterable used to seed the new tree.
329
- * @param [options] - Options merged with the current snapshot.
330
- * @returns A like-kind RedBlackTree built from the iterable.
331
- */
332
- protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | RedBlackTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<TreeMultiMapOptions<TK, TV, TR>>): RedBlackTree<TK, TV, TR>;
422
+ */
423
+ get comparator(): Comparator<K>;
333
424
  }
@@ -0,0 +1,270 @@
1
+ /**
2
+ * TreeMultiSet (ordered multiset) — a restricted, native-like API backed by RedBlackTree.
3
+ *
4
+ * Design goals:
5
+ * - No node exposure
6
+ * - Strict default comparator (number/string/Date), otherwise require comparator
7
+ * - Default iteration is expanded (each element yielded `count(x)` times)
8
+ * - `delete(x)` removes one occurrence by default
9
+ */
10
+ import type { Comparator, TreeMultiSetOptions } from '../../types';
11
+ export declare class TreeMultiSet<K = any, R = K> implements Iterable<K> {
12
+ #private;
13
+ private _size;
14
+ /**
15
+ * Creates a new TreeMultiSet.
16
+ * @param elements - Initial elements to add, or raw elements if `toElementFn` is provided.
17
+ * @param options - Configuration options including optional `toElementFn` to transform raw elements.
18
+ * @remarks Time O(m log m), Space O(m) where m is the number of initial elements
19
+ * @example
20
+ * // Standard usage with elements
21
+ * const mset = new TreeMultiSet([1, 2, 2, 3]);
22
+ *
23
+ * // Using toElementFn to transform raw objects
24
+ * const items = [{ score: 100 }, { score: 200 }, { score: 100 }];
25
+ * const mset = new TreeMultiSet<number, Item>(items, { toElementFn: item => item.score });
26
+ */
27
+ constructor(elements?: Iterable<R> | Iterable<K>, options?: TreeMultiSetOptions<K, R>);
28
+ /**
29
+ * Validates the key against the default comparator rules.
30
+ * @remarks Time O(1), Space O(1)
31
+ */
32
+ private _validateKey;
33
+ /**
34
+ * Validates that count is a non-negative safe integer.
35
+ * @remarks Time O(1), Space O(1)
36
+ */
37
+ private _validateCount;
38
+ /**
39
+ * Total occurrences (sumCounts).
40
+ * @remarks Time O(1), Space O(1)
41
+ */
42
+ get size(): number;
43
+ /**
44
+ * Number of distinct keys.
45
+ * @remarks Time O(1), Space O(1)
46
+ */
47
+ get distinctSize(): number;
48
+ /**
49
+ * Whether the multiset is empty.
50
+ * @remarks Time O(1), Space O(1)
51
+ */
52
+ isEmpty(): boolean;
53
+ /**
54
+ * Whether the multiset contains the given key.
55
+ * @remarks Time O(log n), Space O(1)
56
+ */
57
+ has(key: K): boolean;
58
+ /**
59
+ * Returns the count of occurrences for the given key.
60
+ * @remarks Time O(log n), Space O(1)
61
+ */
62
+ count(key: K): number;
63
+ /**
64
+ * Add `n` occurrences of `key`.
65
+ * @returns True if the multiset changed.
66
+ * @remarks Time O(log n), Space O(1)
67
+ */
68
+ add(key: K, n?: number): boolean;
69
+ /**
70
+ * Set count for `key` to exactly `n`.
71
+ * @returns True if changed.
72
+ * @remarks Time O(log n), Space O(1)
73
+ */
74
+ setCount(key: K, n: number): boolean;
75
+ /**
76
+ * Delete `n` occurrences of `key` (default 1).
77
+ * @returns True if any occurrence was removed.
78
+ * @remarks Time O(log n), Space O(1)
79
+ */
80
+ delete(key: K, n?: number): boolean;
81
+ /**
82
+ * Delete all occurrences of the given key.
83
+ * @returns True if any occurrence was removed.
84
+ * @remarks Time O(log n), Space O(1)
85
+ */
86
+ deleteAll(key: K): boolean;
87
+ /**
88
+ * Iterates over distinct keys (each key yielded once).
89
+ * @remarks Time O(n), Space O(1)
90
+ */
91
+ keysDistinct(): IterableIterator<K>;
92
+ /**
93
+ * Iterates over entries as [key, count] pairs.
94
+ * @remarks Time O(n), Space O(1)
95
+ */
96
+ entries(): IterableIterator<[K, number]>;
97
+ /**
98
+ * Expanded iteration (default). Each key is yielded `count(key)` times.
99
+ * @remarks Time O(size), Space O(1) where size is total occurrences
100
+ */
101
+ [Symbol.iterator](): Iterator<K>;
102
+ /**
103
+ * Returns an array with all elements (expanded).
104
+ * @remarks Time O(size), Space O(size)
105
+ */
106
+ toArray(): K[];
107
+ /**
108
+ * Returns an array with distinct keys only.
109
+ * @remarks Time O(n), Space O(n)
110
+ */
111
+ toDistinctArray(): K[];
112
+ /**
113
+ * Returns an array of [key, count] entries.
114
+ * @remarks Time O(n), Space O(n)
115
+ */
116
+ toEntries(): Array<[K, number]>;
117
+ /**
118
+ * Expose comparator for advanced usage/testing (read-only).
119
+ * @remarks Time O(1), Space O(1)
120
+ */
121
+ get comparator(): Comparator<K>;
122
+ /**
123
+ * Remove all elements from the multiset.
124
+ * @remarks Time O(1), Space O(1)
125
+ * @example
126
+ * const ms = new TreeMultiSet([1, 2, 2, 3]);
127
+ * ms.clear();
128
+ * ms.size; // 0
129
+ */
130
+ clear(): void;
131
+ /**
132
+ * Returns the smallest key, or undefined if empty.
133
+ * @remarks Time O(log n), Space O(1)
134
+ * @example
135
+ * const ms = new TreeMultiSet([3, 1, 4]);
136
+ * ms.first(); // 1
137
+ */
138
+ first(): K | undefined;
139
+ /**
140
+ * Returns the largest key, or undefined if empty.
141
+ * @remarks Time O(log n), Space O(1)
142
+ * @example
143
+ * const ms = new TreeMultiSet([3, 1, 4]);
144
+ * ms.last(); // 4
145
+ */
146
+ last(): K | undefined;
147
+ /**
148
+ * Removes all occurrences of the smallest key and returns it.
149
+ * @remarks Time O(log n), Space O(1)
150
+ * @example
151
+ * const ms = new TreeMultiSet([1, 1, 2, 3]);
152
+ * ms.pollFirst(); // 1
153
+ * ms.has(1); // false
154
+ */
155
+ pollFirst(): K | undefined;
156
+ /**
157
+ * Removes all occurrences of the largest key and returns it.
158
+ * @remarks Time O(log n), Space O(1)
159
+ * @example
160
+ * const ms = new TreeMultiSet([1, 2, 3, 3]);
161
+ * ms.pollLast(); // 3
162
+ * ms.has(3); // false
163
+ */
164
+ pollLast(): K | undefined;
165
+ /**
166
+ * Returns the smallest key >= given key, or undefined.
167
+ * @remarks Time O(log n), Space O(1)
168
+ * @example
169
+ * const ms = new TreeMultiSet([10, 20, 30]);
170
+ * ms.ceiling(15); // 20
171
+ * ms.ceiling(20); // 20
172
+ */
173
+ ceiling(key: K): K | undefined;
174
+ /**
175
+ * Returns the largest key <= given key, or undefined.
176
+ * @remarks Time O(log n), Space O(1)
177
+ * @example
178
+ * const ms = new TreeMultiSet([10, 20, 30]);
179
+ * ms.floor(25); // 20
180
+ * ms.floor(20); // 20
181
+ */
182
+ floor(key: K): K | undefined;
183
+ /**
184
+ * Returns the smallest key > given key, or undefined.
185
+ * @remarks Time O(log n), Space O(1)
186
+ * @example
187
+ * const ms = new TreeMultiSet([10, 20, 30]);
188
+ * ms.higher(10); // 20
189
+ * ms.higher(15); // 20
190
+ */
191
+ higher(key: K): K | undefined;
192
+ /**
193
+ * Returns the largest key < given key, or undefined.
194
+ * @remarks Time O(log n), Space O(1)
195
+ * @example
196
+ * const ms = new TreeMultiSet([10, 20, 30]);
197
+ * ms.lower(20); // 10
198
+ * ms.lower(15); // 10
199
+ */
200
+ lower(key: K): K | undefined;
201
+ /**
202
+ * Iterates over distinct keys with their counts.
203
+ * @remarks Time O(n), Space O(1)
204
+ * @example
205
+ * const ms = new TreeMultiSet([1, 1, 2, 3, 3, 3]);
206
+ * ms.forEach((key, count) => console.log(`${key}: ${count}`));
207
+ * // 1: 2, 2: 1, 3: 3
208
+ */
209
+ forEach(callback: (key: K, count: number) => void): void;
210
+ /**
211
+ * Creates a new TreeMultiSet with entries that match the predicate.
212
+ * @remarks Time O(n log n), Space O(n)
213
+ * @example
214
+ * const ms = new TreeMultiSet([1, 1, 2, 3, 3, 3]);
215
+ * const filtered = ms.filter((key, count) => count >= 2);
216
+ * // TreeMultiSet { 1: 2, 3: 3 }
217
+ */
218
+ filter(predicate: (key: K, count: number) => boolean): TreeMultiSet<K>;
219
+ /**
220
+ * Reduces the multiset to a single value.
221
+ * @remarks Time O(n), Space O(1)
222
+ * @example
223
+ * const ms = new TreeMultiSet([1, 1, 2, 3, 3, 3]);
224
+ * const total = ms.reduce((acc, key, count) => acc + count, 0); // 6
225
+ */
226
+ reduce<U>(callback: (accumulator: U, key: K, count: number) => U, initialValue: U): U;
227
+ /**
228
+ * Maps keys and counts to a new TreeMultiSet.
229
+ * When multiple keys map to the same new key, counts are merged (added).
230
+ * @remarks Time O(n log n), Space O(n)
231
+ * @example
232
+ * const ms = new TreeMultiSet([1, 1, 2, 3, 3, 3]);
233
+ * const mapped = ms.map((key, count) => [key * 10, count]);
234
+ * // TreeMultiSet { 10: 2, 20: 1, 30: 3 }
235
+ * @example
236
+ * // Collision: counts merge
237
+ * const ms = new TreeMultiSet([1, 2, 3]);
238
+ * const merged = ms.map((key, count) => [key % 2, count]);
239
+ * // { 0: 1, 1: 2 } (1 and 3 both map to 1, counts add)
240
+ */
241
+ map<K2>(mapper: (key: K, count: number) => [K2, number], options?: {
242
+ comparator?: Comparator<K2>;
243
+ }): TreeMultiSet<K2>;
244
+ /**
245
+ * Creates an independent copy of this multiset.
246
+ * @remarks Time O(n log n), Space O(n)
247
+ * @example
248
+ * const ms = new TreeMultiSet([1, 1, 2]);
249
+ * const copy = ms.clone();
250
+ * copy.add(3);
251
+ * ms.has(3); // false (original unchanged)
252
+ */
253
+ clone(): TreeMultiSet<K>;
254
+ /**
255
+ * Returns keys within the given range.
256
+ * @remarks Time O(log n + k), Space O(k) where k is result size
257
+ * @example
258
+ * const ms = new TreeMultiSet([10, 20, 30, 40, 50]);
259
+ * ms.rangeSearch([15, 45]); // [20, 30, 40]
260
+ */
261
+ rangeSearch<C extends (key: K) => any>(range: [K, K], callback?: C): (C extends undefined ? K : ReturnType<C>)[];
262
+ /**
263
+ * Prints the internal tree structure (for debugging).
264
+ * @remarks Time O(n), Space O(n)
265
+ * @example
266
+ * const ms = new TreeMultiSet([1, 2, 3]);
267
+ * ms.print();
268
+ */
269
+ print(): void;
270
+ }