max-priority-queue-typed 2.4.5 → 2.5.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.
- package/README.md +63 -0
- package/dist/cjs/index.cjs +694 -119
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +693 -118
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +694 -119
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +693 -118
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
- package/dist/types/data-structures/base/linear-base.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +380 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +487 -147
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +956 -80
- package/dist/types/data-structures/binary-tree/bst.d.ts +816 -29
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +610 -31
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +326 -135
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +3781 -6
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3607 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2874 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +3528 -6
- package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
- package/dist/types/data-structures/graph/directed-graph.d.ts +429 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +393 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +473 -89
- package/dist/types/data-structures/heap/heap.d.ts +581 -99
- package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +646 -47
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +596 -68
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +793 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +499 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
- package/dist/types/data-structures/queue/deque.d.ts +593 -71
- package/dist/types/data-structures/queue/queue.d.ts +463 -42
- package/dist/types/data-structures/stack/stack.d.ts +384 -32
- package/dist/types/data-structures/trie/trie.d.ts +470 -48
- package/dist/types/interfaces/graph.d.ts +1 -1
- package/dist/types/types/common.d.ts +2 -2
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
- package/dist/types/types/utils/validate-type.d.ts +4 -4
- package/dist/umd/max-priority-queue-typed.js +691 -116
- package/dist/umd/max-priority-queue-typed.js.map +1 -1
- package/dist/umd/max-priority-queue-typed.min.js +1 -1
- package/dist/umd/max-priority-queue-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +1 -0
- package/src/data-structures/base/iterable-element-base.ts +4 -5
- package/src/data-structures/base/iterable-entry-base.ts +8 -8
- package/src/data-structures/base/linear-base.ts +3 -3
- package/src/data-structures/binary-tree/avl-tree.ts +386 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +596 -247
- package/src/data-structures/binary-tree/binary-tree.ts +956 -81
- package/src/data-structures/binary-tree/bst.ts +840 -35
- package/src/data-structures/binary-tree/red-black-tree.ts +689 -97
- package/src/data-structures/binary-tree/segment-tree.ts +498 -249
- package/src/data-structures/binary-tree/tree-map.ts +3784 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +3614 -211
- package/src/data-structures/binary-tree/tree-multi-set.ts +2874 -65
- package/src/data-structures/binary-tree/tree-set.ts +3531 -10
- package/src/data-structures/graph/abstract-graph.ts +4 -4
- package/src/data-structures/graph/directed-graph.ts +429 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +393 -59
- package/src/data-structures/hash/hash-map.ts +476 -92
- package/src/data-structures/heap/heap.ts +581 -99
- package/src/data-structures/heap/max-heap.ts +46 -0
- package/src/data-structures/heap/min-heap.ts +59 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +646 -47
- package/src/data-structures/linked-list/singly-linked-list.ts +596 -68
- package/src/data-structures/linked-list/skip-linked-list.ts +1067 -90
- package/src/data-structures/matrix/matrix.ts +584 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
- package/src/data-structures/priority-queue/priority-queue.ts +60 -0
- package/src/data-structures/queue/deque.ts +592 -70
- package/src/data-structures/queue/queue.ts +463 -42
- package/src/data-structures/stack/stack.ts +384 -32
- package/src/data-structures/trie/trie.ts +470 -48
- package/src/interfaces/graph.ts +1 -1
- package/src/types/common.ts +2 -2
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/heap/heap.ts +1 -0
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
- package/src/types/utils/validate-type.ts +4 -4
|
@@ -113,32 +113,6 @@ export class TrieNode {
|
|
|
113
113
|
* 10. IP Routing: Used in certain types of IP routing algorithms.
|
|
114
114
|
* 11. Text Word Frequency Count: Counting and storing the frequency of words in a large amount of text data.
|
|
115
115
|
* @example
|
|
116
|
-
* // basic Trie creation and add words
|
|
117
|
-
* // Create a simple Trie with initial words
|
|
118
|
-
* const trie = new Trie(['apple', 'app', 'apply']);
|
|
119
|
-
*
|
|
120
|
-
* // Verify size
|
|
121
|
-
* console.log(trie.size); // 3;
|
|
122
|
-
*
|
|
123
|
-
* // Check if words exist
|
|
124
|
-
* console.log(trie.has('apple')); // true;
|
|
125
|
-
* console.log(trie.has('app')); // true;
|
|
126
|
-
*
|
|
127
|
-
* // Add a new word
|
|
128
|
-
* trie.add('application');
|
|
129
|
-
* console.log(trie.size); // 4;
|
|
130
|
-
* @example
|
|
131
|
-
* // Trie getWords and prefix search
|
|
132
|
-
* const trie = new Trie(['apple', 'app', 'apply', 'application', 'apricot']);
|
|
133
|
-
*
|
|
134
|
-
* // Get all words with prefix 'app'
|
|
135
|
-
* const appWords = trie.getWords('app');
|
|
136
|
-
* console.log(appWords); // contains 'app';
|
|
137
|
-
* console.log(appWords); // contains 'apple';
|
|
138
|
-
* console.log(appWords); // contains 'apply';
|
|
139
|
-
* console.log(appWords); // contains 'application';
|
|
140
|
-
* expect(appWords).not.toContain('apricot');
|
|
141
|
-
* @example
|
|
142
116
|
* // Trie isPrefix and isAbsolutePrefix checks
|
|
143
117
|
* const trie = new Trie(['tree', 'trial', 'trick', 'trip', 'trie']);
|
|
144
118
|
*
|
|
@@ -154,23 +128,6 @@ export class TrieNode {
|
|
|
154
128
|
* // Verify size
|
|
155
129
|
* console.log(trie.size); // 5;
|
|
156
130
|
* @example
|
|
157
|
-
* // Trie delete and iteration
|
|
158
|
-
* const trie = new Trie(['car', 'card', 'care', 'careful', 'can', 'cat']);
|
|
159
|
-
*
|
|
160
|
-
* // Delete a word
|
|
161
|
-
* trie.delete('card');
|
|
162
|
-
* console.log(trie.has('card')); // false;
|
|
163
|
-
*
|
|
164
|
-
* // Word with same prefix still exists
|
|
165
|
-
* console.log(trie.has('care')); // true;
|
|
166
|
-
*
|
|
167
|
-
* // Size decreased
|
|
168
|
-
* console.log(trie.size); // 5;
|
|
169
|
-
*
|
|
170
|
-
* // Iterate through all words
|
|
171
|
-
* const allWords = [...trie];
|
|
172
|
-
* console.log(allWords.length); // 5;
|
|
173
|
-
* @example
|
|
174
131
|
* // Trie for autocomplete search index
|
|
175
132
|
* // Trie is perfect for autocomplete: O(m + k) where m is prefix length, k is results
|
|
176
133
|
* const searchIndex = new Trie(['typescript', 'javascript', 'python', 'java', 'rust', 'ruby', 'golang', 'kotlin']);
|
|
@@ -327,6 +284,53 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
327
284
|
* @remarks Time O(L), Space O(L)
|
|
328
285
|
* @param word - Word to insert.
|
|
329
286
|
* @returns True if the word was newly added.
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
* @example
|
|
320
|
+
* // basic Trie creation and add words
|
|
321
|
+
* // Create a simple Trie with initial words
|
|
322
|
+
* const trie = new Trie(['apple', 'app', 'apply']);
|
|
323
|
+
*
|
|
324
|
+
* // Verify size
|
|
325
|
+
* console.log(trie.size); // 3;
|
|
326
|
+
*
|
|
327
|
+
* // Check if words exist
|
|
328
|
+
* console.log(trie.has('apple')); // true;
|
|
329
|
+
* console.log(trie.has('app')); // true;
|
|
330
|
+
*
|
|
331
|
+
* // Add a new word
|
|
332
|
+
* trie.add('application');
|
|
333
|
+
* console.log(trie.size); // 4;
|
|
330
334
|
*/
|
|
331
335
|
|
|
332
336
|
add(word: string): boolean {
|
|
@@ -354,6 +358,42 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
354
358
|
* @remarks Time O(ΣL), Space O(ΣL)
|
|
355
359
|
* @param words - Iterable of strings (or raw records if toElementFn is provided).
|
|
356
360
|
* @returns Array of per-word 'added' flags.
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
* @example
|
|
391
|
+
* // Add multiple words
|
|
392
|
+
* const trie = new Trie();
|
|
393
|
+
* trie.addMany(['cat', 'car', 'card']);
|
|
394
|
+
* console.log(trie.has('cat')); // true;
|
|
395
|
+
* console.log(trie.has('car')); // true;
|
|
396
|
+
* console.log(trie.size); // 3;
|
|
357
397
|
*/
|
|
358
398
|
|
|
359
399
|
addMany(words: Iterable<string> | Iterable<R>): boolean[] {
|
|
@@ -373,6 +413,45 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
373
413
|
* @remarks Time O(L), Space O(1)
|
|
374
414
|
* @param word - Word to search for.
|
|
375
415
|
* @returns True if present.
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
|
|
436
|
+
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
|
|
448
|
+
* @example
|
|
449
|
+
* // Check if a word exists
|
|
450
|
+
* const dict = new Trie(['apple', 'app', 'application']);
|
|
451
|
+
*
|
|
452
|
+
* console.log(dict.has('app')); // true;
|
|
453
|
+
* console.log(dict.has('apple')); // true;
|
|
454
|
+
* console.log(dict.has('ap')); // false;
|
|
376
455
|
*/
|
|
377
456
|
|
|
378
457
|
override has(word: string): boolean {
|
|
@@ -390,6 +469,41 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
390
469
|
* Check whether the trie is empty.
|
|
391
470
|
* @remarks Time O(1), Space O(1)
|
|
392
471
|
* @returns True if size is 0.
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
|
|
479
|
+
|
|
480
|
+
|
|
481
|
+
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
|
|
485
|
+
|
|
486
|
+
|
|
487
|
+
|
|
488
|
+
|
|
489
|
+
|
|
490
|
+
|
|
491
|
+
|
|
492
|
+
|
|
493
|
+
|
|
494
|
+
|
|
495
|
+
|
|
496
|
+
|
|
497
|
+
|
|
498
|
+
|
|
499
|
+
|
|
500
|
+
|
|
501
|
+
* @example
|
|
502
|
+
* // Check if empty
|
|
503
|
+
* const trie = new Trie();
|
|
504
|
+
* console.log(trie.isEmpty()); // true;
|
|
505
|
+
* trie.add('word');
|
|
506
|
+
* console.log(trie.isEmpty()); // false;
|
|
393
507
|
*/
|
|
394
508
|
|
|
395
509
|
isEmpty(): boolean {
|
|
@@ -400,6 +514,40 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
400
514
|
* Remove all words and reset to a fresh root.
|
|
401
515
|
* @remarks Time O(1), Space O(1)
|
|
402
516
|
* @returns void
|
|
517
|
+
|
|
518
|
+
|
|
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
|
+
|
|
544
|
+
|
|
545
|
+
|
|
546
|
+
* @example
|
|
547
|
+
* // Remove all words
|
|
548
|
+
* const trie = new Trie(['a', 'b', 'c']);
|
|
549
|
+
* trie.clear();
|
|
550
|
+
* console.log(trie.isEmpty()); // true;
|
|
403
551
|
*/
|
|
404
552
|
|
|
405
553
|
clear(): void {
|
|
@@ -412,6 +560,55 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
412
560
|
* @remarks Time O(L), Space O(1)
|
|
413
561
|
* @param word - Word to delete.
|
|
414
562
|
* @returns True if a word was removed.
|
|
563
|
+
|
|
564
|
+
|
|
565
|
+
|
|
566
|
+
|
|
567
|
+
|
|
568
|
+
|
|
569
|
+
|
|
570
|
+
|
|
571
|
+
|
|
572
|
+
|
|
573
|
+
|
|
574
|
+
|
|
575
|
+
|
|
576
|
+
|
|
577
|
+
|
|
578
|
+
|
|
579
|
+
|
|
580
|
+
|
|
581
|
+
|
|
582
|
+
|
|
583
|
+
|
|
584
|
+
|
|
585
|
+
|
|
586
|
+
|
|
587
|
+
|
|
588
|
+
|
|
589
|
+
|
|
590
|
+
|
|
591
|
+
|
|
592
|
+
|
|
593
|
+
|
|
594
|
+
|
|
595
|
+
* @example
|
|
596
|
+
* // Trie delete and iteration
|
|
597
|
+
* const trie = new Trie(['car', 'card', 'care', 'careful', 'can', 'cat']);
|
|
598
|
+
*
|
|
599
|
+
* // Delete a word
|
|
600
|
+
* trie.delete('card');
|
|
601
|
+
* console.log(trie.has('card')); // false;
|
|
602
|
+
*
|
|
603
|
+
* // Word with same prefix still exists
|
|
604
|
+
* console.log(trie.has('care')); // true;
|
|
605
|
+
*
|
|
606
|
+
* // Size decreased
|
|
607
|
+
* console.log(trie.size); // 5;
|
|
608
|
+
*
|
|
609
|
+
* // Iterate through all words
|
|
610
|
+
* const allWords = [...trie];
|
|
611
|
+
* console.log(allWords.length); // 5;
|
|
415
612
|
*/
|
|
416
613
|
|
|
417
614
|
delete(word: string): boolean {
|
|
@@ -499,6 +696,45 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
499
696
|
* @remarks Time O(L), Space O(1)
|
|
500
697
|
* @param input - String to test as prefix.
|
|
501
698
|
* @returns True if input matches a path from root.
|
|
699
|
+
|
|
700
|
+
|
|
701
|
+
|
|
702
|
+
|
|
703
|
+
|
|
704
|
+
|
|
705
|
+
|
|
706
|
+
|
|
707
|
+
|
|
708
|
+
|
|
709
|
+
|
|
710
|
+
|
|
711
|
+
|
|
712
|
+
|
|
713
|
+
|
|
714
|
+
|
|
715
|
+
|
|
716
|
+
|
|
717
|
+
|
|
718
|
+
|
|
719
|
+
|
|
720
|
+
|
|
721
|
+
|
|
722
|
+
|
|
723
|
+
|
|
724
|
+
|
|
725
|
+
|
|
726
|
+
|
|
727
|
+
|
|
728
|
+
|
|
729
|
+
|
|
730
|
+
|
|
731
|
+
* @example
|
|
732
|
+
* // Check if a prefix exists
|
|
733
|
+
* const trie = new Trie(['hello', 'help', 'world']);
|
|
734
|
+
*
|
|
735
|
+
* console.log(trie.hasPrefix('hel')); // true;
|
|
736
|
+
* console.log(trie.hasPrefix('wor')); // true;
|
|
737
|
+
* console.log(trie.hasPrefix('xyz')); // false;
|
|
502
738
|
*/
|
|
503
739
|
|
|
504
740
|
hasPrefix(input: string): boolean {
|
|
@@ -537,6 +773,43 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
537
773
|
* Return the longest common prefix among all words.
|
|
538
774
|
* @remarks Time O(H), Space O(1)
|
|
539
775
|
* @returns The longest common prefix string.
|
|
776
|
+
|
|
777
|
+
|
|
778
|
+
|
|
779
|
+
|
|
780
|
+
|
|
781
|
+
|
|
782
|
+
|
|
783
|
+
|
|
784
|
+
|
|
785
|
+
|
|
786
|
+
|
|
787
|
+
|
|
788
|
+
|
|
789
|
+
|
|
790
|
+
|
|
791
|
+
|
|
792
|
+
|
|
793
|
+
|
|
794
|
+
|
|
795
|
+
|
|
796
|
+
|
|
797
|
+
|
|
798
|
+
|
|
799
|
+
|
|
800
|
+
|
|
801
|
+
|
|
802
|
+
|
|
803
|
+
|
|
804
|
+
|
|
805
|
+
|
|
806
|
+
|
|
807
|
+
|
|
808
|
+
* @example
|
|
809
|
+
* // Find shared prefix
|
|
810
|
+
* const trie = new Trie(['flower', 'flow', 'flight']);
|
|
811
|
+
*
|
|
812
|
+
* console.log(trie.getLongestCommonPrefix()); // 'fl';
|
|
540
813
|
*/
|
|
541
814
|
|
|
542
815
|
getLongestCommonPrefix(): string {
|
|
@@ -558,6 +831,49 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
558
831
|
* @param [max] - Maximum number of words to return; default is Number.MAX_SAFE_INTEGER.
|
|
559
832
|
* @param [isAllWhenEmptyPrefix] - When true, collect from root even if prefix is empty.
|
|
560
833
|
* @returns Array of collected words (at most max).
|
|
834
|
+
|
|
835
|
+
|
|
836
|
+
|
|
837
|
+
|
|
838
|
+
|
|
839
|
+
|
|
840
|
+
|
|
841
|
+
|
|
842
|
+
|
|
843
|
+
|
|
844
|
+
|
|
845
|
+
|
|
846
|
+
|
|
847
|
+
|
|
848
|
+
|
|
849
|
+
|
|
850
|
+
|
|
851
|
+
|
|
852
|
+
|
|
853
|
+
|
|
854
|
+
|
|
855
|
+
|
|
856
|
+
|
|
857
|
+
|
|
858
|
+
|
|
859
|
+
|
|
860
|
+
|
|
861
|
+
|
|
862
|
+
|
|
863
|
+
|
|
864
|
+
|
|
865
|
+
|
|
866
|
+
* @example
|
|
867
|
+
* // Trie getWords and prefix search
|
|
868
|
+
* const trie = new Trie(['apple', 'app', 'apply', 'application', 'apricot']);
|
|
869
|
+
*
|
|
870
|
+
* // Get all words with prefix 'app'
|
|
871
|
+
* const appWords = trie.getWords('app');
|
|
872
|
+
* console.log(appWords); // contains 'app';
|
|
873
|
+
* console.log(appWords); // contains 'apple';
|
|
874
|
+
* console.log(appWords); // contains 'apply';
|
|
875
|
+
* console.log(appWords); // contains 'application';
|
|
876
|
+
* expect(appWords).not.toContain('apricot');
|
|
561
877
|
*/
|
|
562
878
|
|
|
563
879
|
getWords(prefix = '', max = Number.MAX_SAFE_INTEGER, isAllWhenEmptyPrefix = false): string[] {
|
|
@@ -599,6 +915,41 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
599
915
|
* Deep clone this trie by iterating and inserting all words.
|
|
600
916
|
* @remarks Time O(ΣL), Space O(ΣL)
|
|
601
917
|
* @returns A new trie with the same words and options.
|
|
918
|
+
|
|
919
|
+
|
|
920
|
+
|
|
921
|
+
|
|
922
|
+
|
|
923
|
+
|
|
924
|
+
|
|
925
|
+
|
|
926
|
+
|
|
927
|
+
|
|
928
|
+
|
|
929
|
+
|
|
930
|
+
|
|
931
|
+
|
|
932
|
+
|
|
933
|
+
|
|
934
|
+
|
|
935
|
+
|
|
936
|
+
|
|
937
|
+
|
|
938
|
+
|
|
939
|
+
|
|
940
|
+
|
|
941
|
+
|
|
942
|
+
|
|
943
|
+
|
|
944
|
+
|
|
945
|
+
|
|
946
|
+
|
|
947
|
+
* @example
|
|
948
|
+
* // Create independent copy
|
|
949
|
+
* const trie = new Trie(['hello', 'world']);
|
|
950
|
+
* const copy = trie.clone();
|
|
951
|
+
* copy.delete('hello');
|
|
952
|
+
* console.log(trie.has('hello')); // true;
|
|
602
953
|
*/
|
|
603
954
|
|
|
604
955
|
clone(): this {
|
|
@@ -613,9 +964,43 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
613
964
|
* @param predicate - Predicate (word, index, trie) → boolean to keep word.
|
|
614
965
|
* @param [thisArg] - Value for `this` inside the predicate.
|
|
615
966
|
* @returns A new trie containing words that satisfy the predicate.
|
|
967
|
+
|
|
968
|
+
|
|
969
|
+
|
|
970
|
+
|
|
971
|
+
|
|
972
|
+
|
|
973
|
+
|
|
974
|
+
|
|
975
|
+
|
|
976
|
+
|
|
977
|
+
|
|
978
|
+
|
|
979
|
+
|
|
980
|
+
|
|
981
|
+
|
|
982
|
+
|
|
983
|
+
|
|
984
|
+
|
|
985
|
+
|
|
986
|
+
|
|
987
|
+
|
|
988
|
+
|
|
989
|
+
|
|
990
|
+
|
|
991
|
+
|
|
992
|
+
|
|
993
|
+
|
|
994
|
+
|
|
995
|
+
|
|
996
|
+
* @example
|
|
997
|
+
* // Filter words
|
|
998
|
+
* const trie = new Trie(['cat', 'car', 'dog', 'card']);
|
|
999
|
+
* const result = trie.filter(w => w.startsWith('ca'));
|
|
1000
|
+
* console.log(result.size); // 3;
|
|
616
1001
|
*/
|
|
617
1002
|
|
|
618
|
-
filter(predicate: ElementCallback<string, R, boolean>, thisArg?:
|
|
1003
|
+
filter(predicate: ElementCallback<string, R, boolean>, thisArg?: unknown): this {
|
|
619
1004
|
const results = this._createInstance();
|
|
620
1005
|
let index = 0;
|
|
621
1006
|
for (const word of this) {
|
|
@@ -627,7 +1012,44 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
627
1012
|
return results;
|
|
628
1013
|
}
|
|
629
1014
|
|
|
630
|
-
|
|
1015
|
+
/**
|
|
1016
|
+
* Transform words
|
|
1017
|
+
|
|
1018
|
+
|
|
1019
|
+
|
|
1020
|
+
|
|
1021
|
+
|
|
1022
|
+
|
|
1023
|
+
|
|
1024
|
+
|
|
1025
|
+
|
|
1026
|
+
|
|
1027
|
+
|
|
1028
|
+
|
|
1029
|
+
|
|
1030
|
+
|
|
1031
|
+
|
|
1032
|
+
|
|
1033
|
+
|
|
1034
|
+
|
|
1035
|
+
|
|
1036
|
+
|
|
1037
|
+
|
|
1038
|
+
|
|
1039
|
+
|
|
1040
|
+
|
|
1041
|
+
|
|
1042
|
+
|
|
1043
|
+
|
|
1044
|
+
|
|
1045
|
+
|
|
1046
|
+
* @example
|
|
1047
|
+
* // Transform words
|
|
1048
|
+
* const trie = new Trie(['hello', 'world']);
|
|
1049
|
+
* const upper = trie.map(w => w.toUpperCase());
|
|
1050
|
+
* console.log(upper.has('HELLO')); // true;
|
|
1051
|
+
*/
|
|
1052
|
+
map<RM>(callback: ElementCallback<string, R, string>, options?: TrieOptions<RM>, thisArg?: unknown): Trie<RM>;
|
|
631
1053
|
|
|
632
1054
|
/**
|
|
633
1055
|
* Map words into a new trie (possibly different record type).
|
|
@@ -643,10 +1065,10 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
643
1065
|
map<EM, RM>(
|
|
644
1066
|
callback: ElementCallback<string, R, EM>,
|
|
645
1067
|
options?: TrieOptions<RM>,
|
|
646
|
-
thisArg?:
|
|
1068
|
+
thisArg?: unknown
|
|
647
1069
|
): IterableElementBase<EM, RM>;
|
|
648
1070
|
|
|
649
|
-
map<EM, RM>(callback: ElementCallback<string, R, EM>, options?: TrieOptions<RM>, thisArg?:
|
|
1071
|
+
map<EM, RM>(callback: ElementCallback<string, R, EM>, options?: TrieOptions<RM>, thisArg?: unknown): any {
|
|
650
1072
|
const newTrie = this._createLike<RM>([], options);
|
|
651
1073
|
let i = 0;
|
|
652
1074
|
for (const x of this) {
|
|
@@ -667,7 +1089,7 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
667
1089
|
* @returns A new trie with mapped words.
|
|
668
1090
|
*/
|
|
669
1091
|
|
|
670
|
-
mapSame(callback: ElementCallback<string, R, string>, thisArg?:
|
|
1092
|
+
mapSame(callback: ElementCallback<string, R, string>, thisArg?: unknown): this {
|
|
671
1093
|
const next = this._createInstance();
|
|
672
1094
|
let i = 0;
|
|
673
1095
|
for (const key of this) {
|
package/src/interfaces/graph.ts
CHANGED
package/src/types/common.ts
CHANGED
|
@@ -11,11 +11,11 @@ export type DFSOrderPattern = 'PRE' | 'IN' | 'POST';
|
|
|
11
11
|
export type NodeDisplayLayout = [string[], number, number, number];
|
|
12
12
|
|
|
13
13
|
export interface IterableWithSize<T> extends Iterable<T> {
|
|
14
|
-
size: number | ((...args:
|
|
14
|
+
size: number | ((...args: unknown[]) => number);
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
export interface IterableWithLength<T> extends Iterable<T> {
|
|
18
|
-
length: number | ((...args:
|
|
18
|
+
length: number | ((...args: unknown[]) => number);
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
export type OptValue<V> = V | undefined;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export
|
|
1
|
+
export {};
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
// SkipList types are co-located with the implementation in src/data-structures/linked-list/skip-linked-list.ts
|
|
2
|
+
export {};
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
export type KeyValueObject = { [key: string]:
|
|
1
|
+
export type KeyValueObject = { [key: string]: unknown };
|
|
2
2
|
|
|
3
|
-
export type KeyValueObjectWithKey = { [key: string]:
|
|
3
|
+
export type KeyValueObjectWithKey = { [key: string]: unknown; key: string | number | symbol };
|
|
4
4
|
|
|
5
5
|
export type NonNumberNonObjectButDefined = string | boolean | symbol | null;
|
|
6
6
|
|
|
7
7
|
export type ObjectWithoutKey = Omit<KeyValueObject, 'key'>;
|
|
8
8
|
|
|
9
9
|
export type ObjectWithNonNumberKey = {
|
|
10
|
-
[key: string]:
|
|
10
|
+
[key: string]: unknown;
|
|
11
11
|
key: string | boolean | symbol | null | object | undefined;
|
|
12
12
|
};
|
|
13
13
|
|
|
14
14
|
export type ObjectWithNumberKey = {
|
|
15
|
-
[key: string]:
|
|
15
|
+
[key: string]: unknown;
|
|
16
16
|
key: number;
|
|
17
17
|
};
|
|
18
18
|
|