max-priority-queue-typed 2.4.4 → 2.5.0

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 (85) hide show
  1. package/README.md +63 -0
  2. package/dist/cjs/index.cjs +403 -98
  3. package/dist/cjs/index.cjs.map +1 -1
  4. package/dist/cjs-legacy/index.cjs +402 -97
  5. package/dist/cjs-legacy/index.cjs.map +1 -1
  6. package/dist/esm/index.mjs +403 -99
  7. package/dist/esm/index.mjs.map +1 -1
  8. package/dist/esm-legacy/index.mjs +402 -98
  9. package/dist/esm-legacy/index.mjs.map +1 -1
  10. package/dist/types/common/error.d.ts +23 -0
  11. package/dist/types/common/index.d.ts +1 -0
  12. package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
  13. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
  14. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
  15. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +439 -78
  16. package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
  17. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +217 -31
  18. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
  19. package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
  20. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
  21. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
  22. package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
  23. package/dist/types/data-structures/graph/abstract-graph.d.ts +44 -0
  24. package/dist/types/data-structures/graph/directed-graph.d.ts +220 -47
  25. package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
  26. package/dist/types/data-structures/graph/undirected-graph.d.ts +218 -59
  27. package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
  28. package/dist/types/data-structures/heap/heap.d.ts +287 -99
  29. package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
  30. package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
  31. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +286 -44
  32. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
  33. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
  34. package/dist/types/data-structures/matrix/matrix.d.ts +331 -0
  35. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
  36. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
  37. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
  38. package/dist/types/data-structures/queue/deque.d.ts +313 -66
  39. package/dist/types/data-structures/queue/queue.d.ts +211 -42
  40. package/dist/types/data-structures/stack/stack.d.ts +174 -32
  41. package/dist/types/data-structures/trie/trie.d.ts +213 -43
  42. package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
  43. package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
  44. package/dist/types/types/data-structures/queue/deque.d.ts +6 -0
  45. package/dist/umd/max-priority-queue-typed.js +400 -95
  46. package/dist/umd/max-priority-queue-typed.js.map +1 -1
  47. package/dist/umd/max-priority-queue-typed.min.js +1 -1
  48. package/dist/umd/max-priority-queue-typed.min.js.map +1 -1
  49. package/package.json +2 -2
  50. package/src/common/error.ts +60 -0
  51. package/src/common/index.ts +2 -0
  52. package/src/data-structures/base/iterable-element-base.ts +2 -2
  53. package/src/data-structures/binary-tree/avl-tree.ts +134 -51
  54. package/src/data-structures/binary-tree/binary-indexed-tree.ts +303 -247
  55. package/src/data-structures/binary-tree/binary-tree.ts +542 -121
  56. package/src/data-structures/binary-tree/bst.ts +346 -37
  57. package/src/data-structures/binary-tree/red-black-tree.ts +309 -96
  58. package/src/data-structures/binary-tree/segment-tree.ts +372 -248
  59. package/src/data-structures/binary-tree/tree-map.ts +1292 -13
  60. package/src/data-structures/binary-tree/tree-multi-map.ts +1098 -215
  61. package/src/data-structures/binary-tree/tree-multi-set.ts +863 -69
  62. package/src/data-structures/binary-tree/tree-set.ts +1143 -15
  63. package/src/data-structures/graph/abstract-graph.ts +106 -1
  64. package/src/data-structures/graph/directed-graph.ts +223 -47
  65. package/src/data-structures/graph/map-graph.ts +59 -1
  66. package/src/data-structures/graph/undirected-graph.ts +299 -59
  67. package/src/data-structures/hash/hash-map.ts +243 -79
  68. package/src/data-structures/heap/heap.ts +291 -102
  69. package/src/data-structures/heap/max-heap.ts +48 -3
  70. package/src/data-structures/heap/min-heap.ts +59 -0
  71. package/src/data-structures/linked-list/doubly-linked-list.ts +286 -44
  72. package/src/data-structures/linked-list/singly-linked-list.ts +278 -65
  73. package/src/data-structures/linked-list/skip-linked-list.ts +689 -90
  74. package/src/data-structures/matrix/matrix.ts +425 -22
  75. package/src/data-structures/priority-queue/max-priority-queue.ts +59 -3
  76. package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
  77. package/src/data-structures/priority-queue/priority-queue.ts +60 -0
  78. package/src/data-structures/queue/deque.ts +343 -68
  79. package/src/data-structures/queue/queue.ts +211 -42
  80. package/src/data-structures/stack/stack.ts +174 -32
  81. package/src/data-structures/trie/trie.ts +215 -44
  82. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
  83. package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
  84. package/src/types/data-structures/queue/deque.ts +7 -0
  85. package/src/utils/utils.ts +4 -2
@@ -15,6 +15,7 @@ import type {
15
15
  } from '../../types';
16
16
  import { IterableEntryBase } from '../base';
17
17
  import { isWeakKey, rangeCheck } from '../../utils';
18
+ import { ERR } from '../../common';
18
19
 
19
20
  /**
20
21
  * Hash-based map. Supports object keys and custom hashing; offers O(1) average set/get/has.
@@ -28,83 +29,6 @@ import { isWeakKey, rangeCheck } from '../../utils';
28
29
  * If you try to insert another entry with the same key, the new one will replace the old entry.
29
30
  * 4. Unordered Collection: HashMap does not guarantee the order of entries, and the order may change over time.
30
31
  * @example
31
- * // should maintain insertion order
32
- * const linkedHashMap = new LinkedHashMap<number, string>();
33
- * linkedHashMap.set(1, 'A');
34
- * linkedHashMap.set(2, 'B');
35
- * linkedHashMap.set(3, 'C');
36
- *
37
- * const result = Array.from(linkedHashMap);
38
- * console.log(result); // [
39
- * // [1, 'A'],
40
- * // [2, 'B'],
41
- * // [3, 'C']
42
- * // ];
43
- * @example
44
- * // basic HashMap creation and set operation
45
- * // Create a simple HashMap with key-value pairs
46
- * const map = new HashMap<number, string>([
47
- * [1, 'one'],
48
- * [2, 'two'],
49
- * [3, 'three']
50
- * ]);
51
- *
52
- * // Verify size
53
- * console.log(map.size); // 3;
54
- *
55
- * // Set a new key-value pair
56
- * map.set(4, 'four');
57
- * console.log(map.size); // 4;
58
- *
59
- * // Verify entries
60
- * console.log([...map.entries()]); // length: 4;
61
- * @example
62
- * // HashMap get and has operations
63
- * const map = new HashMap<string, number>([
64
- * ['apple', 1],
65
- * ['banana', 2],
66
- * ['cherry', 3]
67
- * ]);
68
- *
69
- * // Check if key exists
70
- * console.log(map.has('apple')); // true;
71
- * console.log(map.has('date')); // false;
72
- *
73
- * // Get value by key
74
- * console.log(map.get('banana')); // 2;
75
- * console.log(map.get('grape')); // undefined;
76
- *
77
- * // Get all keys and values
78
- * const keys = [...map.keys()];
79
- * const values = [...map.values()];
80
- * console.log(keys); // contains 'apple';
81
- * console.log(values); // contains 3;
82
- * @example
83
- * // HashMap iteration and filter operations
84
- * const map = new HashMap<number, string>([
85
- * [1, 'Alice'],
86
- * [2, 'Bob'],
87
- * [3, 'Charlie'],
88
- * [4, 'Diana'],
89
- * [5, 'Eve']
90
- * ]);
91
- *
92
- * // Iterate through entries
93
- * const entries: [number, string][] = [];
94
- * for (const [key, value] of map) {
95
- * entries.push([key, value]);
96
- * }
97
- * console.log(entries); // length: 5;
98
- *
99
- * // Filter operation (for iteration with collection methods)
100
- * const filtered = [...map].filter(([key]) => key > 2);
101
- * console.log(filtered.length); // 3;
102
- *
103
- * // Map operation
104
- * const values = [...map.values()].map(v => v.length);
105
- * console.log(values); // contains 3; // 'Bob', 'Eve'
106
- * console.log(values); // contains 7;
107
- * @example
108
32
  * // HashMap for user session caching O(1) performance
109
33
  * interface UserSession {
110
34
  * userId: number;
@@ -156,6 +80,19 @@ import { isWeakKey, rangeCheck } from '../../utils';
156
80
  * // Get all active sessions
157
81
  * const activeCount = [...sessionCache.values()].length;
158
82
  * console.log(activeCount); // 2;
83
+ * @example
84
+ * // Aggregate values
85
+ * const counts = new HashMap<string, number>([['a', 5], ['b', 3], ['c', 8]]);
86
+ *
87
+ * const total = counts.reduce((sum, v) => sum + (v ?? 0), 0);
88
+ * console.log(total); // 16;
89
+ * @example
90
+ * // Iterate over entries
91
+ * const map = new HashMap<string, number>([['x', 1], ['y', 2]]);
92
+ * const keys: string[] = [];
93
+ *
94
+ * map.forEach((v, k) => keys.push(k));
95
+ * console.log(keys.sort()); // ['x', 'y'];
159
96
  */
160
97
  export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K, V> {
161
98
  /**
@@ -234,6 +171,19 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
234
171
  * Check whether the map is empty.
235
172
  * @remarks Time O(1), Space O(1)
236
173
  * @returns True if size is 0.
174
+
175
+
176
+
177
+
178
+
179
+
180
+
181
+
182
+
183
+ * @example
184
+ * // Check if empty
185
+ * const map = new HashMap();
186
+ * console.log(map.isEmpty()); // true;
237
187
  */
238
188
  isEmpty(): boolean {
239
189
  return this._size === 0;
@@ -243,6 +193,20 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
243
193
  * Remove all entries and reset counters.
244
194
  * @remarks Time O(N), Space O(1)
245
195
  * @returns void
196
+
197
+
198
+
199
+
200
+
201
+
202
+
203
+
204
+
205
+ * @example
206
+ * // Remove all entries
207
+ * const map = new HashMap<string, number>([['a', 1], ['b', 2]]);
208
+ * map.clear();
209
+ * console.log(map.isEmpty()); // true;
246
210
  */
247
211
  clear(): void {
248
212
  this._store = {};
@@ -265,6 +229,46 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
265
229
  * @param key - Key.
266
230
  * @param value - Value.
267
231
  * @returns True when the operation succeeds.
232
+
233
+
234
+
235
+
236
+
237
+
238
+
239
+
240
+
241
+
242
+
243
+
244
+
245
+
246
+
247
+
248
+
249
+
250
+
251
+
252
+
253
+
254
+ * @example
255
+ * // basic HashMap creation and set operation
256
+ * // Create a simple HashMap with key-value pairs
257
+ * const map = new HashMap<number, string>([
258
+ * [1, 'one'],
259
+ * [2, 'two'],
260
+ * [3, 'three']
261
+ * ]);
262
+ *
263
+ * // Verify size
264
+ * console.log(map.size); // 3;
265
+ *
266
+ * // Set a new key-value pair
267
+ * map.set(4, 'four');
268
+ * console.log(map.size); // 4;
269
+ *
270
+ * // Verify entries
271
+ * console.log([...map.entries()]); // length: 4;
268
272
  */
269
273
  set(key: K, value: V): boolean {
270
274
  if (this._isObjKey(key)) {
@@ -283,6 +287,20 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
283
287
  * @remarks Time O(N), Space O(N)
284
288
  * @param entryOrRawElements - Iterable of entries or raw elements to insert.
285
289
  * @returns Array of per-entry results.
290
+
291
+
292
+
293
+
294
+
295
+
296
+
297
+
298
+
299
+ * @example
300
+ * // Add multiple entries
301
+ * const map = new HashMap<string, number>();
302
+ * map.setMany([['a', 1], ['b', 2], ['c', 3]]);
303
+ * console.log(map.size); // 3;
286
304
  */
287
305
  setMany(entryOrRawElements: Iterable<R | [K, V]>): boolean[] {
288
306
  const results: boolean[] = [];
@@ -300,6 +318,38 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
300
318
  * @remarks Time O(1), Space O(1)
301
319
  * @param key - Key to look up.
302
320
  * @returns Value or undefined.
321
+
322
+
323
+
324
+
325
+
326
+
327
+
328
+
329
+
330
+
331
+
332
+ * @example
333
+ * // HashMap get and has operations
334
+ * const map = new HashMap<string, number>([
335
+ * ['apple', 1],
336
+ * ['banana', 2],
337
+ * ['cherry', 3]
338
+ * ]);
339
+ *
340
+ * // Check if key exists
341
+ * console.log(map.has('apple')); // true;
342
+ * console.log(map.has('date')); // false;
343
+ *
344
+ * // Get value by key
345
+ * console.log(map.get('banana')); // 2;
346
+ * console.log(map.get('grape')); // undefined;
347
+ *
348
+ * // Get all keys and values
349
+ * const keys = [...map.keys()];
350
+ * const values = [...map.values()];
351
+ * console.log(keys); // contains 'apple';
352
+ * console.log(values); // contains 3;
303
353
  */
304
354
  override get(key: K): V | undefined {
305
355
  if (this._isObjKey(key)) return this.objMap.get(key);
@@ -312,6 +362,23 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
312
362
  * @remarks Time O(1), Space O(1)
313
363
  * @param key - Key to test.
314
364
  * @returns True if present.
365
+
366
+
367
+
368
+
369
+
370
+
371
+
372
+
373
+
374
+
375
+
376
+ * @example
377
+ * // Check key existence
378
+ * const map = new HashMap<string, number>([['a', 1], ['b', 2]]);
379
+ *
380
+ * console.log(map.has('a')); // true;
381
+ * console.log(map.has('z')); // false;
315
382
  */
316
383
  override has(key: K): boolean {
317
384
  if (this._isObjKey(key)) return this.objMap.has(key);
@@ -324,6 +391,24 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
324
391
  * @remarks Time O(1), Space O(1)
325
392
  * @param key - Key to delete.
326
393
  * @returns True if the key was found and removed.
394
+
395
+
396
+
397
+
398
+
399
+
400
+
401
+
402
+
403
+
404
+
405
+ * @example
406
+ * // Remove entries by key
407
+ * const map = new HashMap<string, number>([['x', 10], ['y', 20], ['z', 30]]);
408
+ *
409
+ * console.log(map.delete('y')); // true;
410
+ * console.log(map.has('y')); // false;
411
+ * console.log(map.size); // 2;
327
412
  */
328
413
  delete(key: K): boolean {
329
414
  if (this._isObjKey(key)) {
@@ -356,6 +441,21 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
356
441
  * Deep clone this map, preserving hashing behavior.
357
442
  * @remarks Time O(N), Space O(N)
358
443
  * @returns A new map with the same content.
444
+
445
+
446
+
447
+
448
+
449
+
450
+
451
+
452
+
453
+ * @example
454
+ * // Create independent copy
455
+ * const map = new HashMap<string, number>([['a', 1]]);
456
+ * const copy = map.clone();
457
+ * copy.set('a', 99);
458
+ * console.log(map.get('a')); // 1;
359
459
  */
360
460
  clone(): this {
361
461
  const opts = { hashFn: this._hashFn, toEntryFn: this._toEntryFn };
@@ -369,6 +469,24 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
369
469
  * @param callbackfn - Mapping function (key, value, index, map) → newValue.
370
470
  * @param [thisArg] - Value for `this` inside the callback.
371
471
  * @returns A new map with transformed values.
472
+
473
+
474
+
475
+
476
+
477
+
478
+
479
+
480
+
481
+
482
+
483
+ * @example
484
+ * // Transform all values
485
+ * const prices = new HashMap<string, number>([['apple', 1], ['banana', 2]]);
486
+ *
487
+ * const doubled = prices.map(v => (v ?? 0) * 2);
488
+ * console.log(doubled.get('apple')); // 2;
489
+ * console.log(doubled.get('banana')); // 4;
372
490
  */
373
491
  map<VM>(callbackfn: EntryCallback<K, V, VM>, thisArg?: any): any {
374
492
  const out = this._createLike<K, VM, [K, VM]>();
@@ -383,6 +501,42 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
383
501
  * @param predicate - Predicate (key, value, index, map) → boolean.
384
502
  * @param [thisArg] - Value for `this` inside the predicate.
385
503
  * @returns A new map containing entries that satisfied the predicate.
504
+
505
+
506
+
507
+
508
+
509
+
510
+
511
+
512
+
513
+
514
+
515
+ * @example
516
+ * // HashMap iteration and filter operations
517
+ * const map = new HashMap<number, string>([
518
+ * [1, 'Alice'],
519
+ * [2, 'Bob'],
520
+ * [3, 'Charlie'],
521
+ * [4, 'Diana'],
522
+ * [5, 'Eve']
523
+ * ]);
524
+ *
525
+ * // Iterate through entries
526
+ * const entries: [number, string][] = [];
527
+ * for (const [key, value] of map) {
528
+ * entries.push([key, value]);
529
+ * }
530
+ * console.log(entries); // length: 5;
531
+ *
532
+ * // Filter operation (for iteration with collection methods)
533
+ * const filtered = [...map].filter(([key]) => key > 2);
534
+ * console.log(filtered.length); // 3;
535
+ *
536
+ * // Map operation
537
+ * const values = [...map.values()].map(v => v.length);
538
+ * console.log(values); // contains 3; // 'Bob', 'Eve'
539
+ * console.log(values); // contains 7;
386
540
  */
387
541
 
388
542
  filter(predicate: EntryCallback<K, V, boolean>, thisArg?: any): any {
@@ -534,8 +688,8 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
534
688
  if (this.isEntry(rawElement)) {
535
689
  return rawElement;
536
690
  }
537
- throw new Error(
538
- 'If `entryOrRawElements` does not adhere to [key,value], provide `options.toEntryFn` to transform raw records.'
691
+ throw new TypeError(
692
+ ERR.invalidArgument('If elements do not adhere to [key, value], provide options.toEntryFn to transform raw records.', 'HashMap')
539
693
  );
540
694
  };
541
695
 
@@ -797,6 +951,16 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
797
951
  }
798
952
 
799
953
  protected _deleteNode(node: HashMapLinkedNode<K, V | undefined>): boolean {
954
+ // Remove from hash table
955
+ const key: unknown = node.key;
956
+ if (isWeakKey(key)) {
957
+ this._objMap.delete(key);
958
+ } else {
959
+ const hash = this._hashFn(key as K);
960
+ delete this._noObjMap[hash];
961
+ }
962
+
963
+ // Remove from linked list
800
964
  const { prev, next } = node;
801
965
  prev.next = next;
802
966
  next.prev = prev;