deque-typed 2.5.2 → 2.6.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.
- package/dist/cjs/index.cjs +223 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +223 -0
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +223 -0
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +223 -0
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/base/iterable-element-base.d.ts +17 -0
- package/dist/types/data-structures/base/linear-base.d.ts +6 -0
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +86 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +98 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +191 -15
- package/dist/types/data-structures/binary-tree/bst.d.ts +171 -3
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +136 -8
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +42 -0
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +1061 -167
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1232 -355
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +916 -194
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +1078 -141
- package/dist/types/data-structures/graph/directed-graph.d.ts +70 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +63 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +84 -6
- package/dist/types/data-structures/heap/heap.d.ts +140 -12
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +150 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +106 -1
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +126 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +56 -0
- package/dist/types/data-structures/queue/deque.d.ts +171 -0
- package/dist/types/data-structures/queue/queue.d.ts +97 -0
- package/dist/types/data-structures/stack/stack.d.ts +72 -2
- package/dist/types/data-structures/trie/trie.d.ts +84 -0
- package/dist/types/interfaces/binary-tree.d.ts +2 -3
- package/dist/umd/deque-typed.js +223 -0
- package/dist/umd/deque-typed.js.map +1 -1
- package/dist/umd/deque-typed.min.js +1 -1
- package/dist/umd/deque-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/base/iterable-element-base.ts +32 -0
- package/src/data-structures/base/linear-base.ts +11 -0
- package/src/data-structures/binary-tree/avl-tree.ts +88 -5
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +98 -0
- package/src/data-structures/binary-tree/binary-tree.ts +242 -81
- package/src/data-structures/binary-tree/bst.ts +173 -7
- package/src/data-structures/binary-tree/red-black-tree.ts +139 -15
- package/src/data-structures/binary-tree/segment-tree.ts +42 -0
- package/src/data-structures/binary-tree/tree-map.ts +948 -36
- package/src/data-structures/binary-tree/tree-multi-map.ts +893 -13
- package/src/data-structures/binary-tree/tree-multi-set.ts +761 -33
- package/src/data-structures/binary-tree/tree-set.ts +1260 -251
- package/src/data-structures/graph/directed-graph.ts +71 -1
- package/src/data-structures/graph/undirected-graph.ts +64 -1
- package/src/data-structures/hash/hash-map.ts +100 -12
- package/src/data-structures/heap/heap.ts +149 -19
- package/src/data-structures/linked-list/doubly-linked-list.ts +178 -2
- package/src/data-structures/linked-list/singly-linked-list.ts +106 -1
- package/src/data-structures/linked-list/skip-linked-list.ts +126 -0
- package/src/data-structures/matrix/matrix.ts +56 -0
- package/src/data-structures/queue/deque.ts +187 -0
- package/src/data-structures/queue/queue.ts +109 -0
- package/src/data-structures/stack/stack.ts +75 -5
- package/src/data-structures/trie/trie.ts +84 -0
- package/src/interfaces/binary-tree.ts +1 -9
package/dist/esm/index.mjs
CHANGED
|
@@ -216,6 +216,35 @@ var IterableElementBase = class {
|
|
|
216
216
|
for (const ele of this) if (ele === element) return true;
|
|
217
217
|
return false;
|
|
218
218
|
}
|
|
219
|
+
/**
|
|
220
|
+
* Check whether a value exists (Array-compatible alias for `has`).
|
|
221
|
+
* @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1).
|
|
222
|
+
* @param element - Element to search for (uses `===`).
|
|
223
|
+
* @returns `true` if found.
|
|
224
|
+
*/
|
|
225
|
+
includes(element) {
|
|
226
|
+
return this.has(element);
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Return an iterator of `[index, value]` pairs (Array-compatible).
|
|
230
|
+
* @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1) per step.
|
|
231
|
+
*/
|
|
232
|
+
*entries() {
|
|
233
|
+
let index = 0;
|
|
234
|
+
for (const value of this) {
|
|
235
|
+
yield [index++, value];
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Return an iterator of numeric indices (Array-compatible).
|
|
240
|
+
* @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1) per step.
|
|
241
|
+
*/
|
|
242
|
+
*keys() {
|
|
243
|
+
let index = 0;
|
|
244
|
+
for (const _ of this) {
|
|
245
|
+
yield index++;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
219
248
|
/**
|
|
220
249
|
* Reduces all elements to a single accumulated value.
|
|
221
250
|
*
|
|
@@ -478,6 +507,16 @@ var LinearBase = class _LinearBase extends IterableElementBase {
|
|
|
478
507
|
}
|
|
479
508
|
return this;
|
|
480
509
|
}
|
|
510
|
+
/**
|
|
511
|
+
* Return a new instance of the same type with elements in reverse order (non-mutating).
|
|
512
|
+
* @remarks Provided for familiarity when migrating from Array (ES2023 `toReversed`). Time O(n), Space O(n).
|
|
513
|
+
* @returns A new reversed instance.
|
|
514
|
+
*/
|
|
515
|
+
toReversed() {
|
|
516
|
+
const cloned = this.clone();
|
|
517
|
+
cloned.reverse();
|
|
518
|
+
return cloned;
|
|
519
|
+
}
|
|
481
520
|
};
|
|
482
521
|
|
|
483
522
|
// src/data-structures/queue/deque.ts
|
|
@@ -639,8 +678,39 @@ var Deque = class extends LinearBase {
|
|
|
639
678
|
|
|
640
679
|
|
|
641
680
|
|
|
681
|
+
|
|
682
|
+
|
|
683
|
+
|
|
642
684
|
|
|
643
685
|
|
|
686
|
+
|
|
687
|
+
|
|
688
|
+
* @example
|
|
689
|
+
* // Deque peek at both ends
|
|
690
|
+
* const deque = new Deque<number>([10, 20, 30, 40, 50]);
|
|
691
|
+
*
|
|
692
|
+
* // Get first element without removing
|
|
693
|
+
* const first = deque.at(0);
|
|
694
|
+
* console.log(first); // 10;
|
|
695
|
+
*
|
|
696
|
+
* // Get last element without removing
|
|
697
|
+
* const last = deque.at(deque.length - 1);
|
|
698
|
+
* console.log(last); // 50;
|
|
699
|
+
*
|
|
700
|
+
* // Length unchanged
|
|
701
|
+
* console.log(deque.length); // 5;
|
|
702
|
+
*/
|
|
703
|
+
/**
|
|
704
|
+
* Peek at the front element without removing it (alias for `first`).
|
|
705
|
+
* @remarks Time O(1), Space O(1)
|
|
706
|
+
* @returns Front element or undefined.
|
|
707
|
+
*/
|
|
708
|
+
peek() {
|
|
709
|
+
return this.first;
|
|
710
|
+
}
|
|
711
|
+
/**
|
|
712
|
+
* Deque peek at both ends
|
|
713
|
+
|
|
644
714
|
|
|
645
715
|
|
|
646
716
|
* @example
|
|
@@ -693,6 +763,13 @@ var Deque = class extends LinearBase {
|
|
|
693
763
|
|
|
694
764
|
|
|
695
765
|
|
|
766
|
+
|
|
767
|
+
|
|
768
|
+
|
|
769
|
+
|
|
770
|
+
|
|
771
|
+
|
|
772
|
+
|
|
696
773
|
|
|
697
774
|
|
|
698
775
|
|
|
@@ -756,6 +833,13 @@ var Deque = class extends LinearBase {
|
|
|
756
833
|
|
|
757
834
|
|
|
758
835
|
|
|
836
|
+
|
|
837
|
+
|
|
838
|
+
|
|
839
|
+
|
|
840
|
+
|
|
841
|
+
|
|
842
|
+
|
|
759
843
|
|
|
760
844
|
|
|
761
845
|
|
|
@@ -832,6 +916,13 @@ var Deque = class extends LinearBase {
|
|
|
832
916
|
|
|
833
917
|
|
|
834
918
|
|
|
919
|
+
|
|
920
|
+
|
|
921
|
+
|
|
922
|
+
|
|
923
|
+
|
|
924
|
+
|
|
925
|
+
|
|
835
926
|
|
|
836
927
|
|
|
837
928
|
|
|
@@ -895,6 +986,13 @@ var Deque = class extends LinearBase {
|
|
|
895
986
|
|
|
896
987
|
|
|
897
988
|
|
|
989
|
+
|
|
990
|
+
|
|
991
|
+
|
|
992
|
+
|
|
993
|
+
|
|
994
|
+
|
|
995
|
+
|
|
898
996
|
|
|
899
997
|
|
|
900
998
|
|
|
@@ -959,6 +1057,13 @@ var Deque = class extends LinearBase {
|
|
|
959
1057
|
|
|
960
1058
|
|
|
961
1059
|
|
|
1060
|
+
|
|
1061
|
+
|
|
1062
|
+
|
|
1063
|
+
|
|
1064
|
+
|
|
1065
|
+
|
|
1066
|
+
|
|
962
1067
|
|
|
963
1068
|
|
|
964
1069
|
|
|
@@ -1064,6 +1169,13 @@ var Deque = class extends LinearBase {
|
|
|
1064
1169
|
|
|
1065
1170
|
|
|
1066
1171
|
|
|
1172
|
+
|
|
1173
|
+
|
|
1174
|
+
|
|
1175
|
+
|
|
1176
|
+
|
|
1177
|
+
|
|
1178
|
+
|
|
1067
1179
|
|
|
1068
1180
|
|
|
1069
1181
|
|
|
@@ -1109,6 +1221,13 @@ var Deque = class extends LinearBase {
|
|
|
1109
1221
|
|
|
1110
1222
|
|
|
1111
1223
|
|
|
1224
|
+
|
|
1225
|
+
|
|
1226
|
+
|
|
1227
|
+
|
|
1228
|
+
|
|
1229
|
+
|
|
1230
|
+
|
|
1112
1231
|
|
|
1113
1232
|
|
|
1114
1233
|
|
|
@@ -1158,6 +1277,13 @@ var Deque = class extends LinearBase {
|
|
|
1158
1277
|
|
|
1159
1278
|
|
|
1160
1279
|
|
|
1280
|
+
|
|
1281
|
+
|
|
1282
|
+
|
|
1283
|
+
|
|
1284
|
+
|
|
1285
|
+
|
|
1286
|
+
|
|
1161
1287
|
|
|
1162
1288
|
|
|
1163
1289
|
|
|
@@ -1358,6 +1484,13 @@ var Deque = class extends LinearBase {
|
|
|
1358
1484
|
|
|
1359
1485
|
|
|
1360
1486
|
|
|
1487
|
+
|
|
1488
|
+
|
|
1489
|
+
|
|
1490
|
+
|
|
1491
|
+
|
|
1492
|
+
|
|
1493
|
+
|
|
1361
1494
|
|
|
1362
1495
|
|
|
1363
1496
|
|
|
@@ -1448,10 +1581,72 @@ var Deque = class extends LinearBase {
|
|
|
1448
1581
|
|
|
1449
1582
|
|
|
1450
1583
|
|
|
1584
|
+
|
|
1585
|
+
|
|
1586
|
+
|
|
1587
|
+
|
|
1451
1588
|
|
|
1452
1589
|
|
|
1453
1590
|
|
|
1454
1591
|
|
|
1592
|
+
|
|
1593
|
+
* @example
|
|
1594
|
+
* // Deque for...of iteration and reverse
|
|
1595
|
+
* const deque = new Deque<string>(['A', 'B', 'C', 'D']);
|
|
1596
|
+
*
|
|
1597
|
+
* // Iterate forward
|
|
1598
|
+
* const forward: string[] = [];
|
|
1599
|
+
* for (const item of deque) {
|
|
1600
|
+
* forward.push(item);
|
|
1601
|
+
* }
|
|
1602
|
+
* console.log(forward); // ['A', 'B', 'C', 'D'];
|
|
1603
|
+
*
|
|
1604
|
+
* // Reverse the deque
|
|
1605
|
+
* deque.reverse();
|
|
1606
|
+
* const backward: string[] = [];
|
|
1607
|
+
* for (const item of deque) {
|
|
1608
|
+
* backward.push(item);
|
|
1609
|
+
* }
|
|
1610
|
+
* console.log(backward); // ['D', 'C', 'B', 'A'];
|
|
1611
|
+
*/
|
|
1612
|
+
/**
|
|
1613
|
+
* Find the last value matching a predicate (scans back-to-front).
|
|
1614
|
+
* @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1).
|
|
1615
|
+
* @param predicate - Function called with (value, index, deque).
|
|
1616
|
+
* @returns Matching value or undefined.
|
|
1617
|
+
* @example
|
|
1618
|
+
* // Find last matching value
|
|
1619
|
+
* const d = new Deque([1, 2, 3, 4, 5]);
|
|
1620
|
+
* console.log(d.findLast(v => v > 2)); // 5;
|
|
1621
|
+
* console.log(d.findLast(v => v % 2 === 0)); // 4;
|
|
1622
|
+
*/
|
|
1623
|
+
findLast(predicate) {
|
|
1624
|
+
for (let i = this.length - 1; i >= 0; i--) {
|
|
1625
|
+
const val = this.at(i);
|
|
1626
|
+
if (predicate(val, i, this)) return val;
|
|
1627
|
+
}
|
|
1628
|
+
return void 0;
|
|
1629
|
+
}
|
|
1630
|
+
/**
|
|
1631
|
+
* Find the index of the last value matching a predicate (scans back-to-front).
|
|
1632
|
+
* @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1).
|
|
1633
|
+
* @param predicate - Function called with (value, index, deque).
|
|
1634
|
+
* @returns Matching index, or -1 if not found.
|
|
1635
|
+
* @example
|
|
1636
|
+
* // Find last matching index
|
|
1637
|
+
* const d = new Deque([10, 20, 30, 20, 10]);
|
|
1638
|
+
* console.log(d.findLastIndex(v => v === 20)); // 3;
|
|
1639
|
+
* console.log(d.findLastIndex(v => v === 10)); // 4;
|
|
1640
|
+
*/
|
|
1641
|
+
findLastIndex(predicate) {
|
|
1642
|
+
for (let i = this.length - 1; i >= 0; i--) {
|
|
1643
|
+
if (predicate(this.at(i), i, this)) return i;
|
|
1644
|
+
}
|
|
1645
|
+
return -1;
|
|
1646
|
+
}
|
|
1647
|
+
/**
|
|
1648
|
+
* Deque for...of iteration and reverse
|
|
1649
|
+
|
|
1455
1650
|
|
|
1456
1651
|
* @example
|
|
1457
1652
|
* // Deque for...of iteration and reverse
|
|
@@ -1557,6 +1752,13 @@ var Deque = class extends LinearBase {
|
|
|
1557
1752
|
|
|
1558
1753
|
|
|
1559
1754
|
|
|
1755
|
+
|
|
1756
|
+
|
|
1757
|
+
|
|
1758
|
+
|
|
1759
|
+
|
|
1760
|
+
|
|
1761
|
+
|
|
1560
1762
|
|
|
1561
1763
|
|
|
1562
1764
|
|
|
@@ -1628,6 +1830,13 @@ var Deque = class extends LinearBase {
|
|
|
1628
1830
|
|
|
1629
1831
|
|
|
1630
1832
|
|
|
1833
|
+
|
|
1834
|
+
|
|
1835
|
+
|
|
1836
|
+
|
|
1837
|
+
|
|
1838
|
+
|
|
1839
|
+
|
|
1631
1840
|
|
|
1632
1841
|
|
|
1633
1842
|
|
|
@@ -1682,6 +1891,13 @@ var Deque = class extends LinearBase {
|
|
|
1682
1891
|
|
|
1683
1892
|
|
|
1684
1893
|
|
|
1894
|
+
|
|
1895
|
+
|
|
1896
|
+
|
|
1897
|
+
|
|
1898
|
+
|
|
1899
|
+
|
|
1900
|
+
|
|
1685
1901
|
|
|
1686
1902
|
|
|
1687
1903
|
|
|
@@ -1756,6 +1972,13 @@ var Deque = class extends LinearBase {
|
|
|
1756
1972
|
|
|
1757
1973
|
|
|
1758
1974
|
|
|
1975
|
+
|
|
1976
|
+
|
|
1977
|
+
|
|
1978
|
+
|
|
1979
|
+
|
|
1980
|
+
|
|
1981
|
+
|
|
1759
1982
|
|
|
1760
1983
|
|
|
1761
1984
|
|