binary-tree-typed 2.5.0 → 2.5.2

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 (90) hide show
  1. package/dist/cjs/index.cjs +771 -68
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +771 -68
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +771 -69
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +771 -69
  8. package/dist/esm-legacy/index.mjs.map +1 -1
  9. package/dist/types/common/error.d.ts +9 -0
  10. package/dist/types/common/index.d.ts +1 -1
  11. package/dist/types/data-structures/base/index.d.ts +1 -0
  12. package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
  13. package/dist/types/data-structures/base/linear-base.d.ts +3 -3
  14. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +288 -0
  15. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +336 -0
  16. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +618 -18
  17. package/dist/types/data-structures/binary-tree/bst.d.ts +676 -1
  18. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +456 -0
  19. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +144 -1
  20. package/dist/types/data-structures/binary-tree/tree-map.d.ts +3307 -399
  21. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3285 -360
  22. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2674 -325
  23. package/dist/types/data-structures/binary-tree/tree-set.d.ts +3072 -287
  24. package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
  25. package/dist/types/data-structures/graph/directed-graph.d.ts +240 -0
  26. package/dist/types/data-structures/graph/undirected-graph.d.ts +216 -0
  27. package/dist/types/data-structures/hash/hash-map.d.ts +274 -10
  28. package/dist/types/data-structures/heap/heap.d.ts +336 -0
  29. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +411 -3
  30. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +363 -3
  31. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +434 -2
  32. package/dist/types/data-structures/matrix/matrix.d.ts +192 -0
  33. package/dist/types/data-structures/queue/deque.d.ts +364 -4
  34. package/dist/types/data-structures/queue/queue.d.ts +288 -0
  35. package/dist/types/data-structures/stack/stack.d.ts +240 -0
  36. package/dist/types/data-structures/trie/trie.d.ts +292 -4
  37. package/dist/types/interfaces/graph.d.ts +1 -1
  38. package/dist/types/types/common.d.ts +2 -2
  39. package/dist/types/types/data-structures/binary-tree/bst.d.ts +1 -0
  40. package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +5 -0
  41. package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +4 -0
  42. package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +4 -0
  43. package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
  44. package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
  45. package/dist/types/types/utils/validate-type.d.ts +4 -4
  46. package/dist/umd/binary-tree-typed.js +773 -71
  47. package/dist/umd/binary-tree-typed.js.map +1 -1
  48. package/dist/umd/binary-tree-typed.min.js +5 -5
  49. package/dist/umd/binary-tree-typed.min.js.map +1 -1
  50. package/package.json +2 -2
  51. package/src/common/error.ts +19 -1
  52. package/src/common/index.ts +1 -1
  53. package/src/data-structures/base/index.ts +1 -0
  54. package/src/data-structures/base/iterable-element-base.ts +3 -2
  55. package/src/data-structures/base/iterable-entry-base.ts +8 -8
  56. package/src/data-structures/base/linear-base.ts +3 -3
  57. package/src/data-structures/binary-tree/avl-tree.ts +299 -0
  58. package/src/data-structures/binary-tree/binary-indexed-tree.ts +341 -5
  59. package/src/data-structures/binary-tree/binary-tree.ts +606 -6
  60. package/src/data-structures/binary-tree/bst.ts +946 -7
  61. package/src/data-structures/binary-tree/red-black-tree.ts +472 -0
  62. package/src/data-structures/binary-tree/segment-tree.ts +145 -2
  63. package/src/data-structures/binary-tree/tree-map.ts +3423 -499
  64. package/src/data-structures/binary-tree/tree-multi-map.ts +3537 -596
  65. package/src/data-structures/binary-tree/tree-multi-set.ts +2855 -495
  66. package/src/data-structures/binary-tree/tree-set.ts +3209 -413
  67. package/src/data-structures/graph/abstract-graph.ts +6 -6
  68. package/src/data-structures/graph/directed-graph.ts +240 -0
  69. package/src/data-structures/graph/undirected-graph.ts +216 -0
  70. package/src/data-structures/hash/hash-map.ts +281 -19
  71. package/src/data-structures/heap/heap.ts +340 -4
  72. package/src/data-structures/heap/max-heap.ts +2 -2
  73. package/src/data-structures/linked-list/doubly-linked-list.ts +411 -3
  74. package/src/data-structures/linked-list/singly-linked-list.ts +363 -3
  75. package/src/data-structures/linked-list/skip-linked-list.ts +439 -7
  76. package/src/data-structures/matrix/matrix.ts +202 -10
  77. package/src/data-structures/priority-queue/max-priority-queue.ts +2 -2
  78. package/src/data-structures/queue/deque.ts +365 -5
  79. package/src/data-structures/queue/queue.ts +288 -0
  80. package/src/data-structures/stack/stack.ts +240 -0
  81. package/src/data-structures/trie/trie.ts +295 -7
  82. package/src/interfaces/graph.ts +1 -1
  83. package/src/types/common.ts +2 -2
  84. package/src/types/data-structures/binary-tree/bst.ts +1 -0
  85. package/src/types/data-structures/binary-tree/tree-map.ts +6 -0
  86. package/src/types/data-structures/binary-tree/tree-multi-set.ts +5 -0
  87. package/src/types/data-structures/binary-tree/tree-set.ts +5 -0
  88. package/src/types/data-structures/heap/heap.ts +1 -0
  89. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
  90. package/src/types/utils/validate-type.ts +4 -4
@@ -15,7 +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
+ import { ERR, raise } from '../../common';
19
19
 
20
20
  /**
21
21
  * Hash-based map. Supports object keys and custom hashing; offers O(1) average set/get/has.
@@ -180,6 +180,30 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
180
180
 
181
181
 
182
182
 
183
+
184
+
185
+
186
+
187
+
188
+
189
+
190
+
191
+
192
+
193
+
194
+
195
+
196
+
197
+
198
+
199
+
200
+
201
+
202
+
203
+
204
+
205
+
206
+
183
207
  * @example
184
208
  * // Check if empty
185
209
  * const map = new HashMap();
@@ -202,6 +226,30 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
202
226
 
203
227
 
204
228
 
229
+
230
+
231
+
232
+
233
+
234
+
235
+
236
+
237
+
238
+
239
+
240
+
241
+
242
+
243
+
244
+
245
+
246
+
247
+
248
+
249
+
250
+
251
+
252
+
205
253
  * @example
206
254
  * // Remove all entries
207
255
  * const map = new HashMap<string, number>([['a', 1], ['b', 2]]);
@@ -219,7 +267,7 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
219
267
  * @remarks Time O(1), Space O(1)
220
268
  * @returns True if the value is a 2-tuple.
221
269
  */
222
- isEntry(rawElement: any): rawElement is [K, V] {
270
+ isEntry(rawElement: unknown): rawElement is [K, V] {
223
271
  return Array.isArray(rawElement) && rawElement.length === 2;
224
272
  }
225
273
 
@@ -249,6 +297,54 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
249
297
 
250
298
 
251
299
 
300
+
301
+
302
+
303
+
304
+
305
+
306
+
307
+
308
+
309
+
310
+
311
+
312
+
313
+
314
+
315
+
316
+
317
+
318
+
319
+
320
+
321
+
322
+
323
+
324
+
325
+
326
+
327
+
328
+
329
+
330
+
331
+
332
+
333
+
334
+
335
+
336
+
337
+
338
+
339
+
340
+
341
+
342
+
343
+
344
+
345
+
346
+
347
+
252
348
 
253
349
 
254
350
  * @example
@@ -296,6 +392,30 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
296
392
 
297
393
 
298
394
 
395
+
396
+
397
+
398
+
399
+
400
+
401
+
402
+
403
+
404
+
405
+
406
+
407
+
408
+
409
+
410
+
411
+
412
+
413
+
414
+
415
+
416
+
417
+
418
+
299
419
  * @example
300
420
  * // Add multiple entries
301
421
  * const map = new HashMap<string, number>();
@@ -329,6 +449,30 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
329
449
 
330
450
 
331
451
 
452
+
453
+
454
+
455
+
456
+
457
+
458
+
459
+
460
+
461
+
462
+
463
+
464
+
465
+
466
+
467
+
468
+
469
+
470
+
471
+
472
+
473
+
474
+
475
+
332
476
  * @example
333
477
  * // HashMap get and has operations
334
478
  * const map = new HashMap<string, number>([
@@ -373,6 +517,30 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
373
517
 
374
518
 
375
519
 
520
+
521
+
522
+
523
+
524
+
525
+
526
+
527
+
528
+
529
+
530
+
531
+
532
+
533
+
534
+
535
+
536
+
537
+
538
+
539
+
540
+
541
+
542
+
543
+
376
544
  * @example
377
545
  * // Check key existence
378
546
  * const map = new HashMap<string, number>([['a', 1], ['b', 2]]);
@@ -402,6 +570,30 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
402
570
 
403
571
 
404
572
 
573
+
574
+
575
+
576
+
577
+
578
+
579
+
580
+
581
+
582
+
583
+
584
+
585
+
586
+
587
+
588
+
589
+
590
+
591
+
592
+
593
+
594
+
595
+
596
+
405
597
  * @example
406
598
  * // Remove entries by key
407
599
  * const map = new HashMap<string, number>([['x', 10], ['y', 20], ['z', 30]]);
@@ -450,6 +642,30 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
450
642
 
451
643
 
452
644
 
645
+
646
+
647
+
648
+
649
+
650
+
651
+
652
+
653
+
654
+
655
+
656
+
657
+
658
+
659
+
660
+
661
+
662
+
663
+
664
+
665
+
666
+
667
+
668
+
453
669
  * @example
454
670
  * // Create independent copy
455
671
  * const map = new HashMap<string, number>([['a', 1]]);
@@ -480,6 +696,30 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
480
696
 
481
697
 
482
698
 
699
+
700
+
701
+
702
+
703
+
704
+
705
+
706
+
707
+
708
+
709
+
710
+
711
+
712
+
713
+
714
+
715
+
716
+
717
+
718
+
719
+
720
+
721
+
722
+
483
723
  * @example
484
724
  * // Transform all values
485
725
  * const prices = new HashMap<string, number>([['apple', 1], ['banana', 2]]);
@@ -488,8 +728,8 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
488
728
  * console.log(doubled.get('apple')); // 2;
489
729
  * console.log(doubled.get('banana')); // 4;
490
730
  */
491
- map<VM>(callbackfn: EntryCallback<K, V, VM>, thisArg?: any): any {
492
- const out = this._createLike<K, VM, [K, VM]>();
731
+ map<VM>(callbackfn: EntryCallback<K, V, VM>, thisArg?: unknown): HashMap<K, VM> {
732
+ const out = this._createLike<K, VM, [K, VM]>() as unknown as HashMap<K, VM>;
493
733
  let index = 0;
494
734
  for (const [key, value] of this) out.set(key, callbackfn.call(thisArg, value, key, index++, this));
495
735
  return out;
@@ -512,6 +752,30 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
512
752
 
513
753
 
514
754
 
755
+
756
+
757
+
758
+
759
+
760
+
761
+
762
+
763
+
764
+
765
+
766
+
767
+
768
+
769
+
770
+
771
+
772
+
773
+
774
+
775
+
776
+
777
+
778
+
515
779
  * @example
516
780
  * // HashMap iteration and filter operations
517
781
  * const map = new HashMap<number, string>([
@@ -539,7 +803,7 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
539
803
  * console.log(values); // contains 7;
540
804
  */
541
805
 
542
- filter(predicate: EntryCallback<K, V, boolean>, thisArg?: any): any {
806
+ filter(predicate: EntryCallback<K, V, boolean>, thisArg?: unknown): any {
543
807
  const out = this._createLike<K, V, [K, V]>();
544
808
  let index = 0;
545
809
  for (const [key, value] of this) if (predicate.call(thisArg, value, key, index++, this)) out.set(key, value);
@@ -556,8 +820,8 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
556
820
  * @param [options] - Options forwarded to the constructor.
557
821
  * @returns A like-kind map instance.
558
822
  */
559
- protected _createLike<TK = K, TV = V, TR = [TK, TV]>(entries: Iterable<[TK, TV] | TR> = [], options?: any): any {
560
- const Ctor = this.constructor as new (e?: Iterable<[TK, TV] | TR>, o?: any) => any;
823
+ protected _createLike<TK = K, TV = V, TR = [TK, TV]>(entries: Iterable<[TK, TV] | TR> = [], options?: Record<string, unknown>): this {
824
+ const Ctor = this.constructor as new (e?: Iterable<[TK, TV] | TR>, o?: Record<string, unknown>) => this;
561
825
  return new Ctor(entries, options);
562
826
  }
563
827
 
@@ -575,7 +839,7 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
575
839
  for (const node of this.objMap) yield node as [K, V];
576
840
  }
577
841
 
578
- protected _isObjKey(key: any): key is object | ((...args: any[]) => any) {
842
+ protected _isObjKey(key: unknown): key is object | ((...args: unknown[]) => unknown) {
579
843
  const keyType = typeof key;
580
844
  return (keyType === 'object' || keyType === 'function') && key !== null;
581
845
  }
@@ -688,9 +952,7 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
688
952
  if (this.isEntry(rawElement)) {
689
953
  return rawElement;
690
954
  }
691
- throw new TypeError(
692
- ERR.invalidArgument('If elements do not adhere to [key, value], provide options.toEntryFn to transform raw records.', 'HashMap')
693
- );
955
+ raise(TypeError, ERR.invalidArgument('If elements do not adhere to [key, value], provide options.toEntryFn to transform raw records.', 'HashMap'));
694
956
  };
695
957
 
696
958
  get toEntryFn() {
@@ -897,7 +1159,7 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
897
1159
  return this._size === 0;
898
1160
  }
899
1161
 
900
- isEntry(rawElement: any): rawElement is [K, V] {
1162
+ isEntry(rawElement: unknown): rawElement is [K, V] {
901
1163
  return Array.isArray(rawElement) && rawElement.length === 2;
902
1164
  }
903
1165
 
@@ -907,12 +1169,12 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
907
1169
  this._head = this._tail = this._sentinel.prev = this._sentinel.next = this._sentinel;
908
1170
  }
909
1171
 
910
- clone(): any {
1172
+ clone(): this {
911
1173
  const opts = { hashFn: this._hashFn, objHashFn: this._objHashFn };
912
- return this._createLike<[K, V], [K, V], [K, V]>(this, opts);
1174
+ return this._createLike<K, V, [K, V]>(this, opts);
913
1175
  }
914
1176
 
915
- filter(predicate: EntryCallback<K, V, boolean>, thisArg?: any): any {
1177
+ filter(predicate: EntryCallback<K, V, boolean>, thisArg?: unknown): this {
916
1178
  const out = this._createLike<K, V, [K, V]>();
917
1179
  let index = 0;
918
1180
  for (const [key, value] of this) {
@@ -931,8 +1193,8 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
931
1193
  * @param [thisArg] - Value for `this` inside the callback.
932
1194
  * @returns A new map of the same class with transformed entries.
933
1195
  */
934
- map<MK, MV>(callback: EntryCallback<K, V, [MK, MV]>, thisArg?: any): any {
935
- const out = this._createLike<MK, MV, [MK, MV]>();
1196
+ map<MK, MV>(callback: EntryCallback<K, V, [MK, MV]>, thisArg?: unknown): LinkedHashMap<MK, MV> {
1197
+ const out = this._createLike<MK, MV, [MK, MV]>() as unknown as LinkedHashMap<MK, MV>;
936
1198
  let index = 0;
937
1199
  for (const [key, value] of this) {
938
1200
  const [newKey, newValue] = callback.call(thisArg, value, key, index, this);
@@ -972,8 +1234,8 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
972
1234
  return true;
973
1235
  }
974
1236
 
975
- protected _createLike<TK = K, TV = V, TR = [TK, TV]>(entries: Iterable<[TK, TV] | TR> = [], options?: any): any {
976
- const Ctor = this.constructor as new (e?: Iterable<[TK, TV] | TR>, o?: any) => any;
1237
+ protected _createLike<TK = K, TV = V, TR = [TK, TV]>(entries: Iterable<[TK, TV] | TR> = [], options?: Record<string, unknown>): this {
1238
+ const Ctor = this.constructor as new (e?: Iterable<[TK, TV] | TR>, o?: Record<string, unknown>) => this;
977
1239
  return new Ctor(entries, options);
978
1240
  }
979
1241
  }