data-structure-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 (80) hide show
  1. package/CHANGELOG.md +22 -1
  2. package/README.md +34 -1
  3. package/dist/cjs/index.cjs +10639 -2151
  4. package/dist/cjs-legacy/index.cjs +10694 -2195
  5. package/dist/esm/index.mjs +10639 -2150
  6. package/dist/esm-legacy/index.mjs +10694 -2194
  7. package/dist/types/common/error.d.ts +23 -0
  8. package/dist/types/common/index.d.ts +1 -0
  9. package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
  10. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
  11. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
  12. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +439 -78
  13. package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
  14. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +217 -31
  15. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
  16. package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
  17. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
  18. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
  19. package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
  20. package/dist/types/data-structures/graph/abstract-graph.d.ts +44 -0
  21. package/dist/types/data-structures/graph/directed-graph.d.ts +220 -47
  22. package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
  23. package/dist/types/data-structures/graph/undirected-graph.d.ts +218 -59
  24. package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
  25. package/dist/types/data-structures/heap/heap.d.ts +287 -99
  26. package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
  27. package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
  28. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +286 -44
  29. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
  30. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
  31. package/dist/types/data-structures/matrix/matrix.d.ts +331 -0
  32. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
  33. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
  34. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
  35. package/dist/types/data-structures/queue/deque.d.ts +313 -66
  36. package/dist/types/data-structures/queue/queue.d.ts +211 -42
  37. package/dist/types/data-structures/stack/stack.d.ts +174 -32
  38. package/dist/types/data-structures/trie/trie.d.ts +213 -43
  39. package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
  40. package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
  41. package/dist/types/types/data-structures/queue/deque.d.ts +6 -0
  42. package/dist/umd/data-structure-typed.js +10725 -2221
  43. package/dist/umd/data-structure-typed.min.js +4 -2
  44. package/package.json +5 -4
  45. package/src/common/error.ts +60 -0
  46. package/src/common/index.ts +2 -0
  47. package/src/data-structures/base/iterable-element-base.ts +2 -2
  48. package/src/data-structures/binary-tree/avl-tree.ts +146 -51
  49. package/src/data-structures/binary-tree/binary-indexed-tree.ts +317 -247
  50. package/src/data-structures/binary-tree/binary-tree.ts +567 -121
  51. package/src/data-structures/binary-tree/bst.ts +370 -37
  52. package/src/data-structures/binary-tree/red-black-tree.ts +328 -96
  53. package/src/data-structures/binary-tree/segment-tree.ts +378 -248
  54. package/src/data-structures/binary-tree/tree-map.ts +1411 -13
  55. package/src/data-structures/binary-tree/tree-multi-map.ts +1218 -215
  56. package/src/data-structures/binary-tree/tree-multi-set.ts +959 -69
  57. package/src/data-structures/binary-tree/tree-set.ts +1257 -15
  58. package/src/data-structures/graph/abstract-graph.ts +106 -1
  59. package/src/data-structures/graph/directed-graph.ts +233 -47
  60. package/src/data-structures/graph/map-graph.ts +59 -1
  61. package/src/data-structures/graph/undirected-graph.ts +308 -59
  62. package/src/data-structures/hash/hash-map.ts +254 -79
  63. package/src/data-structures/heap/heap.ts +305 -102
  64. package/src/data-structures/heap/max-heap.ts +48 -3
  65. package/src/data-structures/heap/min-heap.ts +59 -0
  66. package/src/data-structures/linked-list/doubly-linked-list.ts +303 -44
  67. package/src/data-structures/linked-list/singly-linked-list.ts +293 -65
  68. package/src/data-structures/linked-list/skip-linked-list.ts +707 -90
  69. package/src/data-structures/matrix/matrix.ts +433 -22
  70. package/src/data-structures/priority-queue/max-priority-queue.ts +59 -3
  71. package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
  72. package/src/data-structures/priority-queue/priority-queue.ts +60 -0
  73. package/src/data-structures/queue/deque.ts +358 -68
  74. package/src/data-structures/queue/queue.ts +223 -42
  75. package/src/data-structures/stack/stack.ts +184 -32
  76. package/src/data-structures/trie/trie.ts +227 -44
  77. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
  78. package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
  79. package/src/types/data-structures/queue/deque.ts +7 -0
  80. package/src/utils/utils.ts +4 -2
@@ -8,6 +8,7 @@
8
8
 
9
9
  import type { ElementCallback, TrieOptions } from '../../types';
10
10
  import { IterableElementBase } from '../base';
11
+ import { ERR } from '../../common';
11
12
 
12
13
  /**
13
14
  * Node used by Trie to store one character and its children.
@@ -112,32 +113,6 @@ export class TrieNode {
112
113
  * 10. IP Routing: Used in certain types of IP routing algorithms.
113
114
  * 11. Text Word Frequency Count: Counting and storing the frequency of words in a large amount of text data.
114
115
  * @example
115
- * // basic Trie creation and add words
116
- * // Create a simple Trie with initial words
117
- * const trie = new Trie(['apple', 'app', 'apply']);
118
- *
119
- * // Verify size
120
- * console.log(trie.size); // 3;
121
- *
122
- * // Check if words exist
123
- * console.log(trie.has('apple')); // true;
124
- * console.log(trie.has('app')); // true;
125
- *
126
- * // Add a new word
127
- * trie.add('application');
128
- * console.log(trie.size); // 4;
129
- * @example
130
- * // Trie getWords and prefix search
131
- * const trie = new Trie(['apple', 'app', 'apply', 'application', 'apricot']);
132
- *
133
- * // Get all words with prefix 'app'
134
- * const appWords = trie.getWords('app');
135
- * console.log(appWords); // contains 'app';
136
- * console.log(appWords); // contains 'apple';
137
- * console.log(appWords); // contains 'apply';
138
- * console.log(appWords); // contains 'application';
139
- * expect(appWords).not.toContain('apricot');
140
- * @example
141
116
  * // Trie isPrefix and isAbsolutePrefix checks
142
117
  * const trie = new Trie(['tree', 'trial', 'trick', 'trip', 'trie']);
143
118
  *
@@ -153,23 +128,6 @@ export class TrieNode {
153
128
  * // Verify size
154
129
  * console.log(trie.size); // 5;
155
130
  * @example
156
- * // Trie delete and iteration
157
- * const trie = new Trie(['car', 'card', 'care', 'careful', 'can', 'cat']);
158
- *
159
- * // Delete a word
160
- * trie.delete('card');
161
- * console.log(trie.has('card')); // false;
162
- *
163
- * // Word with same prefix still exists
164
- * console.log(trie.has('care')); // true;
165
- *
166
- * // Size decreased
167
- * console.log(trie.size); // 5;
168
- *
169
- * // Iterate through all words
170
- * const allWords = [...trie];
171
- * console.log(allWords.length); // 5;
172
- * @example
173
131
  * // Trie for autocomplete search index
174
132
  * // Trie is perfect for autocomplete: O(m + k) where m is prefix length, k is results
175
133
  * const searchIndex = new Trie(['typescript', 'javascript', 'python', 'java', 'rust', 'ruby', 'golang', 'kotlin']);
@@ -326,6 +284,33 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
326
284
  * @remarks Time O(L), Space O(L)
327
285
  * @param word - Word to insert.
328
286
  * @returns True if the word was newly added.
287
+
288
+
289
+
290
+
291
+
292
+
293
+
294
+
295
+
296
+
297
+
298
+
299
+ * @example
300
+ * // basic Trie creation and add words
301
+ * // Create a simple Trie with initial words
302
+ * const trie = new Trie(['apple', 'app', 'apply']);
303
+ *
304
+ * // Verify size
305
+ * console.log(trie.size); // 3;
306
+ *
307
+ * // Check if words exist
308
+ * console.log(trie.has('apple')); // true;
309
+ * console.log(trie.has('app')); // true;
310
+ *
311
+ * // Add a new word
312
+ * trie.add('application');
313
+ * console.log(trie.size); // 4;
329
314
  */
330
315
 
331
316
  add(word: string): boolean {
@@ -353,6 +338,22 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
353
338
  * @remarks Time O(ΣL), Space O(ΣL)
354
339
  * @param words - Iterable of strings (or raw records if toElementFn is provided).
355
340
  * @returns Array of per-word 'added' flags.
341
+
342
+
343
+
344
+
345
+
346
+
347
+
348
+
349
+
350
+ * @example
351
+ * // Add multiple words
352
+ * const trie = new Trie();
353
+ * trie.addMany(['cat', 'car', 'card']);
354
+ * console.log(trie.has('cat')); // true;
355
+ * console.log(trie.has('car')); // true;
356
+ * console.log(trie.size); // 3;
356
357
  */
357
358
 
358
359
  addMany(words: Iterable<string> | Iterable<R>): boolean[] {
@@ -372,6 +373,25 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
372
373
  * @remarks Time O(L), Space O(1)
373
374
  * @param word - Word to search for.
374
375
  * @returns True if present.
376
+
377
+
378
+
379
+
380
+
381
+
382
+
383
+
384
+
385
+
386
+
387
+
388
+ * @example
389
+ * // Check if a word exists
390
+ * const dict = new Trie(['apple', 'app', 'application']);
391
+ *
392
+ * console.log(dict.has('app')); // true;
393
+ * console.log(dict.has('apple')); // true;
394
+ * console.log(dict.has('ap')); // false;
375
395
  */
376
396
 
377
397
  override has(word: string): boolean {
@@ -389,6 +409,21 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
389
409
  * Check whether the trie is empty.
390
410
  * @remarks Time O(1), Space O(1)
391
411
  * @returns True if size is 0.
412
+
413
+
414
+
415
+
416
+
417
+
418
+
419
+
420
+
421
+ * @example
422
+ * // Check if empty
423
+ * const trie = new Trie();
424
+ * console.log(trie.isEmpty()); // true;
425
+ * trie.add('word');
426
+ * console.log(trie.isEmpty()); // false;
392
427
  */
393
428
 
394
429
  isEmpty(): boolean {
@@ -399,6 +434,20 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
399
434
  * Remove all words and reset to a fresh root.
400
435
  * @remarks Time O(1), Space O(1)
401
436
  * @returns void
437
+
438
+
439
+
440
+
441
+
442
+
443
+
444
+
445
+
446
+ * @example
447
+ * // Remove all words
448
+ * const trie = new Trie(['a', 'b', 'c']);
449
+ * trie.clear();
450
+ * console.log(trie.isEmpty()); // true;
402
451
  */
403
452
 
404
453
  clear(): void {
@@ -411,6 +460,35 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
411
460
  * @remarks Time O(L), Space O(1)
412
461
  * @param word - Word to delete.
413
462
  * @returns True if a word was removed.
463
+
464
+
465
+
466
+
467
+
468
+
469
+
470
+
471
+
472
+
473
+
474
+
475
+ * @example
476
+ * // Trie delete and iteration
477
+ * const trie = new Trie(['car', 'card', 'care', 'careful', 'can', 'cat']);
478
+ *
479
+ * // Delete a word
480
+ * trie.delete('card');
481
+ * console.log(trie.has('card')); // false;
482
+ *
483
+ * // Word with same prefix still exists
484
+ * console.log(trie.has('care')); // true;
485
+ *
486
+ * // Size decreased
487
+ * console.log(trie.size); // 5;
488
+ *
489
+ * // Iterate through all words
490
+ * const allWords = [...trie];
491
+ * console.log(allWords.length); // 5;
414
492
  */
415
493
 
416
494
  delete(word: string): boolean {
@@ -498,6 +576,25 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
498
576
  * @remarks Time O(L), Space O(1)
499
577
  * @param input - String to test as prefix.
500
578
  * @returns True if input matches a path from root.
579
+
580
+
581
+
582
+
583
+
584
+
585
+
586
+
587
+
588
+
589
+
590
+
591
+ * @example
592
+ * // Check if a prefix exists
593
+ * const trie = new Trie(['hello', 'help', 'world']);
594
+ *
595
+ * console.log(trie.hasPrefix('hel')); // true;
596
+ * console.log(trie.hasPrefix('wor')); // true;
597
+ * console.log(trie.hasPrefix('xyz')); // false;
501
598
  */
502
599
 
503
600
  hasPrefix(input: string): boolean {
@@ -536,6 +633,23 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
536
633
  * Return the longest common prefix among all words.
537
634
  * @remarks Time O(H), Space O(1)
538
635
  * @returns The longest common prefix string.
636
+
637
+
638
+
639
+
640
+
641
+
642
+
643
+
644
+
645
+
646
+
647
+
648
+ * @example
649
+ * // Find shared prefix
650
+ * const trie = new Trie(['flower', 'flow', 'flight']);
651
+ *
652
+ * console.log(trie.getLongestCommonPrefix()); // 'fl';
539
653
  */
540
654
 
541
655
  getLongestCommonPrefix(): string {
@@ -557,6 +671,29 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
557
671
  * @param [max] - Maximum number of words to return; default is Number.MAX_SAFE_INTEGER.
558
672
  * @param [isAllWhenEmptyPrefix] - When true, collect from root even if prefix is empty.
559
673
  * @returns Array of collected words (at most max).
674
+
675
+
676
+
677
+
678
+
679
+
680
+
681
+
682
+
683
+
684
+
685
+
686
+ * @example
687
+ * // Trie getWords and prefix search
688
+ * const trie = new Trie(['apple', 'app', 'apply', 'application', 'apricot']);
689
+ *
690
+ * // Get all words with prefix 'app'
691
+ * const appWords = trie.getWords('app');
692
+ * console.log(appWords); // contains 'app';
693
+ * console.log(appWords); // contains 'apple';
694
+ * console.log(appWords); // contains 'apply';
695
+ * console.log(appWords); // contains 'application';
696
+ * expect(appWords).not.toContain('apricot');
560
697
  */
561
698
 
562
699
  getWords(prefix = '', max = Number.MAX_SAFE_INTEGER, isAllWhenEmptyPrefix = false): string[] {
@@ -598,6 +735,21 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
598
735
  * Deep clone this trie by iterating and inserting all words.
599
736
  * @remarks Time O(ΣL), Space O(ΣL)
600
737
  * @returns A new trie with the same words and options.
738
+
739
+
740
+
741
+
742
+
743
+
744
+
745
+
746
+
747
+ * @example
748
+ * // Create independent copy
749
+ * const trie = new Trie(['hello', 'world']);
750
+ * const copy = trie.clone();
751
+ * copy.delete('hello');
752
+ * console.log(trie.has('hello')); // true;
601
753
  */
602
754
 
603
755
  clone(): this {
@@ -612,6 +764,20 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
612
764
  * @param predicate - Predicate (word, index, trie) → boolean to keep word.
613
765
  * @param [thisArg] - Value for `this` inside the predicate.
614
766
  * @returns A new trie containing words that satisfy the predicate.
767
+
768
+
769
+
770
+
771
+
772
+
773
+
774
+
775
+
776
+ * @example
777
+ * // Filter words
778
+ * const trie = new Trie(['cat', 'car', 'dog', 'card']);
779
+ * const result = trie.filter(w => w.startsWith('ca'));
780
+ * console.log(result.size); // 3;
615
781
  */
616
782
 
617
783
  filter(predicate: ElementCallback<string, R, boolean>, thisArg?: any): this {
@@ -626,6 +792,23 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
626
792
  return results;
627
793
  }
628
794
 
795
+ /**
796
+ * Transform words
797
+
798
+
799
+
800
+
801
+
802
+
803
+
804
+
805
+
806
+ * @example
807
+ * // Transform words
808
+ * const trie = new Trie(['hello', 'world']);
809
+ * const upper = trie.map(w => w.toUpperCase());
810
+ * console.log(upper.has('HELLO')); // true;
811
+ */
629
812
  map<RM>(callback: ElementCallback<string, R, string>, options?: TrieOptions<RM>, thisArg?: any): Trie<RM>;
630
813
 
631
814
  /**
@@ -651,7 +834,7 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
651
834
  for (const x of this) {
652
835
  const v = thisArg === undefined ? callback(x, i++, this) : callback.call(thisArg, x, i++, this);
653
836
  if (typeof v !== 'string') {
654
- throw new TypeError(`Trie.map callback must return string; got ${typeof v}`);
837
+ throw new TypeError(ERR.callbackReturnType('string', typeof v, 'Trie.map'));
655
838
  }
656
839
  newTrie.add(v);
657
840
  }
@@ -1 +1 @@
1
- export type SegmentTreeNodeVal = number;
1
+ export {};
@@ -1 +1,2 @@
1
- export type SkipLinkedListOptions = { maxLevel?: number; probability?: number };
1
+ // SkipList types are co-located with the implementation in src/data-structures/linked-list/skip-linked-list.ts
2
+ export {};
@@ -2,4 +2,11 @@ import { LinearBaseOptions } from '../base';
2
2
 
3
3
  export type DequeOptions<E, R> = {
4
4
  bucketSize?: number;
5
+
6
+ /**
7
+ * When the ratio of used buckets to total buckets falls below this threshold
8
+ * after a shift/pop, auto-compact is triggered. Set to 0 to disable.
9
+ * Default: 0.5 (compact when less than half the buckets are in use).
10
+ */
11
+ autoCompactRatio?: number;
5
12
  } & LinearBaseOptions<E, R>;
@@ -77,8 +77,10 @@ export const getMSB = (value: number): number => {
77
77
  * error message to be thrown if the index is out of bounds. By default, if no message is provided when
78
78
  * calling the `rangeCheck` function, the message "Index out of bounds." will be used.
79
79
  */
80
- export const rangeCheck = (index: number, min: number, max: number, message = 'Index out of bounds.'): void => {
81
- if (index < min || index > max) throw new RangeError(message);
80
+ export const rangeCheck = (index: number, min: number, max: number, message?: string): void => {
81
+ if (index < min || index > max) {
82
+ throw new RangeError(message ?? `Index ${index} is out of range [${min}, ${max}].`);
83
+ }
82
84
  };
83
85
 
84
86
  /**