data-structure-typed 1.47.4 → 1.47.5
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/CHANGELOG.md +1 -1
- package/benchmark/report.html +2 -2
- package/benchmark/report.json +12 -18
- package/dist/cjs/data-structures/hash/hash-map.d.ts +3 -3
- package/dist/cjs/data-structures/hash/hash-map.js +10 -4
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-table.d.ts +9 -4
- package/dist/cjs/data-structures/hash/hash-table.js +50 -5
- package/dist/cjs/data-structures/hash/hash-table.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +6 -1
- package/dist/cjs/data-structures/heap/heap.js +51 -9
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +56 -56
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +117 -119
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +36 -36
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +58 -60
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +49 -49
- package/dist/cjs/data-structures/queue/deque.js +79 -72
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +45 -0
- package/dist/cjs/data-structures/queue/queue.js +77 -0
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +18 -5
- package/dist/cjs/data-structures/stack/stack.js +56 -7
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +5 -0
- package/dist/cjs/data-structures/trie/trie.js +47 -0
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/mjs/data-structures/hash/hash-map.d.ts +3 -3
- package/dist/mjs/data-structures/hash/hash-map.js +10 -4
- package/dist/mjs/data-structures/hash/hash-table.d.ts +9 -4
- package/dist/mjs/data-structures/hash/hash-table.js +50 -5
- package/dist/mjs/data-structures/heap/heap.d.ts +6 -1
- package/dist/mjs/data-structures/heap/heap.js +51 -9
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +56 -56
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +117 -119
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +36 -36
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +58 -60
- package/dist/mjs/data-structures/queue/deque.d.ts +49 -49
- package/dist/mjs/data-structures/queue/deque.js +79 -72
- package/dist/mjs/data-structures/queue/queue.d.ts +45 -0
- package/dist/mjs/data-structures/queue/queue.js +77 -0
- package/dist/mjs/data-structures/stack/stack.d.ts +18 -5
- package/dist/mjs/data-structures/stack/stack.js +56 -7
- package/dist/mjs/data-structures/trie/trie.d.ts +5 -0
- package/dist/mjs/data-structures/trie/trie.js +47 -0
- package/dist/umd/data-structure-typed.js +545 -276
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/hash/hash-map.ts +13 -7
- package/src/data-structures/hash/hash-table.ts +59 -9
- package/src/data-structures/heap/heap.ts +60 -9
- package/src/data-structures/linked-list/doubly-linked-list.ts +129 -129
- package/src/data-structures/linked-list/singly-linked-list.ts +65 -65
- package/src/data-structures/queue/deque.ts +84 -77
- package/src/data-structures/queue/queue.ts +84 -0
- package/src/data-structures/stack/stack.ts +64 -8
- package/src/data-structures/trie/trie.ts +53 -0
- package/test/integration/conversion.test.ts +0 -0
- package/test/performance/data-structures/heap/heap.test.ts +13 -4
- package/test/unit/data-structures/hash/hash-table.test.ts +58 -2
- package/test/unit/data-structures/heap/min-heap.test.ts +48 -0
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +2 -2
- package/test/unit/data-structures/queue/queue.test.ts +37 -0
- package/test/unit/data-structures/stack/stack.test.ts +55 -5
- package/test/unit/data-structures/trie/trie.test.ts +33 -0
|
@@ -182,7 +182,7 @@ var dataStructureTyped = (() => {
|
|
|
182
182
|
__publicField(this, "next");
|
|
183
183
|
this.key = key;
|
|
184
184
|
this.value = value;
|
|
185
|
-
this.next =
|
|
185
|
+
this.next = void 0;
|
|
186
186
|
}
|
|
187
187
|
};
|
|
188
188
|
var _HashTable = class _HashTable {
|
|
@@ -194,7 +194,7 @@ var dataStructureTyped = (() => {
|
|
|
194
194
|
this._hashFn = hashFn || this._defaultHashFn;
|
|
195
195
|
this._capacity = Math.max(capacity, _HashTable.DEFAULT_CAPACITY);
|
|
196
196
|
this._size = 0;
|
|
197
|
-
this._buckets = new Array(this._capacity).fill(
|
|
197
|
+
this._buckets = new Array(this._capacity).fill(void 0);
|
|
198
198
|
}
|
|
199
199
|
get capacity() {
|
|
200
200
|
return this._capacity;
|
|
@@ -269,7 +269,7 @@ var dataStructureTyped = (() => {
|
|
|
269
269
|
delete(key) {
|
|
270
270
|
const index = this._hash(key);
|
|
271
271
|
let currentNode = this._buckets[index];
|
|
272
|
-
let prevNode =
|
|
272
|
+
let prevNode = void 0;
|
|
273
273
|
while (currentNode) {
|
|
274
274
|
if (currentNode.key === key) {
|
|
275
275
|
if (prevNode) {
|
|
@@ -278,13 +278,58 @@ var dataStructureTyped = (() => {
|
|
|
278
278
|
this._buckets[index] = currentNode.next;
|
|
279
279
|
}
|
|
280
280
|
this._size--;
|
|
281
|
-
currentNode.next =
|
|
281
|
+
currentNode.next = void 0;
|
|
282
282
|
return;
|
|
283
283
|
}
|
|
284
284
|
prevNode = currentNode;
|
|
285
285
|
currentNode = currentNode.next;
|
|
286
286
|
}
|
|
287
287
|
}
|
|
288
|
+
*[Symbol.iterator]() {
|
|
289
|
+
for (const bucket of this._buckets) {
|
|
290
|
+
let currentNode = bucket;
|
|
291
|
+
while (currentNode) {
|
|
292
|
+
yield [currentNode.key, currentNode.value];
|
|
293
|
+
currentNode = currentNode.next;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
forEach(callback) {
|
|
298
|
+
let index = 0;
|
|
299
|
+
for (const entry of this) {
|
|
300
|
+
callback(entry, index, this);
|
|
301
|
+
index++;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
filter(predicate) {
|
|
305
|
+
const newTable = new _HashTable();
|
|
306
|
+
let index = 0;
|
|
307
|
+
for (const [key, value] of this) {
|
|
308
|
+
if (predicate([key, value], index, this)) {
|
|
309
|
+
newTable.set(key, value);
|
|
310
|
+
}
|
|
311
|
+
index++;
|
|
312
|
+
}
|
|
313
|
+
return newTable;
|
|
314
|
+
}
|
|
315
|
+
map(callback) {
|
|
316
|
+
const newTable = new _HashTable();
|
|
317
|
+
let index = 0;
|
|
318
|
+
for (const [key, value] of this) {
|
|
319
|
+
newTable.set(key, callback([key, value], index, this));
|
|
320
|
+
index++;
|
|
321
|
+
}
|
|
322
|
+
return newTable;
|
|
323
|
+
}
|
|
324
|
+
reduce(callback, initialValue) {
|
|
325
|
+
let accumulator = initialValue;
|
|
326
|
+
let index = 0;
|
|
327
|
+
for (const entry of this) {
|
|
328
|
+
accumulator = callback(accumulator, entry, index, this);
|
|
329
|
+
index++;
|
|
330
|
+
}
|
|
331
|
+
return accumulator;
|
|
332
|
+
}
|
|
288
333
|
/**
|
|
289
334
|
* The function `_defaultHashFn` calculates the hash value of a given key and returns the remainder when divided by the
|
|
290
335
|
* capacity of the data structure.
|
|
@@ -370,7 +415,7 @@ var dataStructureTyped = (() => {
|
|
|
370
415
|
*/
|
|
371
416
|
_expand() {
|
|
372
417
|
const newCapacity = this._capacity * 2;
|
|
373
|
-
const newBuckets = new Array(newCapacity).fill(
|
|
418
|
+
const newBuckets = new Array(newCapacity).fill(void 0);
|
|
374
419
|
for (const bucket of this._buckets) {
|
|
375
420
|
let currentNode = bucket;
|
|
376
421
|
while (currentNode) {
|
|
@@ -739,10 +784,12 @@ var dataStructureTyped = (() => {
|
|
|
739
784
|
*/
|
|
740
785
|
filter(predicate) {
|
|
741
786
|
const filteredMap = new _HashMap();
|
|
787
|
+
let index = 0;
|
|
742
788
|
for (const [key, value] of this) {
|
|
743
|
-
if (predicate([key, value], this)) {
|
|
789
|
+
if (predicate([key, value], index, this)) {
|
|
744
790
|
filteredMap.set(key, value);
|
|
745
791
|
}
|
|
792
|
+
index++;
|
|
746
793
|
}
|
|
747
794
|
return filteredMap;
|
|
748
795
|
}
|
|
@@ -755,9 +802,11 @@ var dataStructureTyped = (() => {
|
|
|
755
802
|
*/
|
|
756
803
|
map(callback) {
|
|
757
804
|
const mappedMap = new _HashMap();
|
|
805
|
+
let index = 0;
|
|
758
806
|
for (const [key, value] of this) {
|
|
759
|
-
const newValue = callback([key, value], this);
|
|
807
|
+
const newValue = callback([key, value], index, this);
|
|
760
808
|
mappedMap.set(key, newValue);
|
|
809
|
+
index++;
|
|
761
810
|
}
|
|
762
811
|
return mappedMap;
|
|
763
812
|
}
|
|
@@ -775,8 +824,10 @@ var dataStructureTyped = (() => {
|
|
|
775
824
|
*/
|
|
776
825
|
reduce(callback, initialValue) {
|
|
777
826
|
let accumulator = initialValue;
|
|
778
|
-
|
|
779
|
-
|
|
827
|
+
let index = 0;
|
|
828
|
+
for (const entry of this) {
|
|
829
|
+
accumulator = callback(accumulator, entry, index, this);
|
|
830
|
+
index++;
|
|
780
831
|
}
|
|
781
832
|
return accumulator;
|
|
782
833
|
}
|
|
@@ -1413,58 +1464,42 @@ var dataStructureTyped = (() => {
|
|
|
1413
1464
|
return count;
|
|
1414
1465
|
}
|
|
1415
1466
|
/**
|
|
1416
|
-
*
|
|
1417
|
-
* Space Complexity: O(1) - Constant space.
|
|
1418
|
-
*/
|
|
1419
|
-
/**
|
|
1420
|
-
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
1421
|
-
* Space Complexity: O(1) - Constant space.
|
|
1422
|
-
*
|
|
1423
|
-
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
1424
|
-
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
|
|
1425
|
-
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
1426
|
-
* current node in the linked list.
|
|
1467
|
+
* The function returns an iterator that iterates over the values of a linked list.
|
|
1427
1468
|
*/
|
|
1428
|
-
|
|
1469
|
+
*[Symbol.iterator]() {
|
|
1429
1470
|
let current = this.head;
|
|
1430
|
-
let index = 0;
|
|
1431
1471
|
while (current) {
|
|
1432
|
-
|
|
1472
|
+
yield current.value;
|
|
1433
1473
|
current = current.next;
|
|
1434
|
-
index++;
|
|
1435
1474
|
}
|
|
1436
1475
|
}
|
|
1437
1476
|
/**
|
|
1438
|
-
* Time Complexity: O(n)
|
|
1439
|
-
* Space Complexity: O(
|
|
1477
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
1478
|
+
* Space Complexity: O(1)
|
|
1440
1479
|
*/
|
|
1441
1480
|
/**
|
|
1442
|
-
* Time Complexity: O(n)
|
|
1443
|
-
* Space Complexity: O(
|
|
1481
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
1482
|
+
* Space Complexity: O(1)
|
|
1444
1483
|
*
|
|
1445
|
-
* The `
|
|
1446
|
-
*
|
|
1447
|
-
*
|
|
1448
|
-
*
|
|
1449
|
-
* SinglyLinkedList).
|
|
1450
|
-
* @returns The `map` function is returning a new instance of `SinglyLinkedList<U>` that contains the mapped values.
|
|
1484
|
+
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
1485
|
+
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
|
|
1486
|
+
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
1487
|
+
* current node in the linked list.
|
|
1451
1488
|
*/
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
current = current.next;
|
|
1489
|
+
forEach(callback) {
|
|
1490
|
+
let index = 0;
|
|
1491
|
+
for (const el of this) {
|
|
1492
|
+
callback(el, index, this);
|
|
1493
|
+
index++;
|
|
1458
1494
|
}
|
|
1459
|
-
return mappedList;
|
|
1460
1495
|
}
|
|
1461
1496
|
/**
|
|
1462
|
-
* Time Complexity: O(n)
|
|
1463
|
-
* Space Complexity: O(n)
|
|
1497
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
1498
|
+
* Space Complexity: O(n)
|
|
1464
1499
|
*/
|
|
1465
1500
|
/**
|
|
1466
|
-
* Time Complexity: O(n)
|
|
1467
|
-
* Space Complexity: O(n)
|
|
1501
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
1502
|
+
* Space Complexity: O(n)
|
|
1468
1503
|
*
|
|
1469
1504
|
* The `filter` function iterates through a SinglyLinkedList and returns a new SinglyLinkedList containing only the
|
|
1470
1505
|
* elements that satisfy the given callback function.
|
|
@@ -1474,51 +1509,65 @@ var dataStructureTyped = (() => {
|
|
|
1474
1509
|
*/
|
|
1475
1510
|
filter(callback) {
|
|
1476
1511
|
const filteredList = new _SinglyLinkedList();
|
|
1477
|
-
let
|
|
1478
|
-
|
|
1479
|
-
if (callback(current
|
|
1480
|
-
filteredList.push(current
|
|
1512
|
+
let index = 0;
|
|
1513
|
+
for (const current of this) {
|
|
1514
|
+
if (callback(current, index, this)) {
|
|
1515
|
+
filteredList.push(current);
|
|
1481
1516
|
}
|
|
1482
|
-
|
|
1517
|
+
index++;
|
|
1483
1518
|
}
|
|
1484
1519
|
return filteredList;
|
|
1485
1520
|
}
|
|
1486
1521
|
/**
|
|
1487
|
-
* Time Complexity: O(n)
|
|
1488
|
-
* Space Complexity: O(n)
|
|
1522
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
1523
|
+
* Space Complexity: O(n)
|
|
1489
1524
|
*/
|
|
1490
1525
|
/**
|
|
1491
|
-
* Time Complexity: O(n)
|
|
1492
|
-
* Space Complexity: O(n)
|
|
1526
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
1527
|
+
* Space Complexity: O(n)
|
|
1528
|
+
*
|
|
1529
|
+
* The `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new
|
|
1530
|
+
* SinglyLinkedList with the transformed values.
|
|
1531
|
+
* @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
|
|
1532
|
+
* the original SinglyLinkedList) and returns a value of type T (the type of values that will be stored in the mapped
|
|
1533
|
+
* SinglyLinkedList).
|
|
1534
|
+
* @returns The `map` function is returning a new instance of `SinglyLinkedList<T>` that contains the mapped values.
|
|
1535
|
+
*/
|
|
1536
|
+
map(callback) {
|
|
1537
|
+
const mappedList = new _SinglyLinkedList();
|
|
1538
|
+
let index = 0;
|
|
1539
|
+
for (const current of this) {
|
|
1540
|
+
mappedList.push(callback(current, index, this));
|
|
1541
|
+
index++;
|
|
1542
|
+
}
|
|
1543
|
+
return mappedList;
|
|
1544
|
+
}
|
|
1545
|
+
/**
|
|
1546
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
1547
|
+
* Space Complexity: O(n)
|
|
1548
|
+
*/
|
|
1549
|
+
/**
|
|
1550
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
1551
|
+
* Space Complexity: O(n)
|
|
1493
1552
|
*
|
|
1494
1553
|
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
|
|
1495
1554
|
* single value.
|
|
1496
1555
|
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
|
|
1497
1556
|
* used to perform a specific operation on each element of the linked list.
|
|
1498
|
-
* @param {
|
|
1557
|
+
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
1499
1558
|
* point for the reduction operation.
|
|
1500
1559
|
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
1501
1560
|
* elements in the linked list.
|
|
1502
1561
|
*/
|
|
1503
1562
|
reduce(callback, initialValue) {
|
|
1504
1563
|
let accumulator = initialValue;
|
|
1505
|
-
let
|
|
1506
|
-
|
|
1507
|
-
accumulator = callback(accumulator, current
|
|
1508
|
-
|
|
1564
|
+
let index = 0;
|
|
1565
|
+
for (const current of this) {
|
|
1566
|
+
accumulator = callback(accumulator, current, index, this);
|
|
1567
|
+
index++;
|
|
1509
1568
|
}
|
|
1510
1569
|
return accumulator;
|
|
1511
1570
|
}
|
|
1512
|
-
/**
|
|
1513
|
-
* The function returns an iterator that iterates over the values of a linked list.
|
|
1514
|
-
*/
|
|
1515
|
-
*[Symbol.iterator]() {
|
|
1516
|
-
let current = this.head;
|
|
1517
|
-
while (current) {
|
|
1518
|
-
yield current.value;
|
|
1519
|
-
current = current.next;
|
|
1520
|
-
}
|
|
1521
|
-
}
|
|
1522
1571
|
};
|
|
1523
1572
|
|
|
1524
1573
|
// src/data-structures/linked-list/doubly-linked-list.ts
|
|
@@ -1917,6 +1966,45 @@ var dataStructureTyped = (() => {
|
|
|
1917
1966
|
}
|
|
1918
1967
|
return false;
|
|
1919
1968
|
}
|
|
1969
|
+
/**
|
|
1970
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
1971
|
+
* Space Complexity: O(1)
|
|
1972
|
+
*/
|
|
1973
|
+
/**
|
|
1974
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
1975
|
+
* Space Complexity: O(1)
|
|
1976
|
+
*
|
|
1977
|
+
* The `insertAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
|
|
1978
|
+
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
1979
|
+
* after which the new value will be inserted. It can be either the value of the existing node or the existing node
|
|
1980
|
+
* itself.
|
|
1981
|
+
* @param {E} newValue - The value that you want to insert into the doubly linked list.
|
|
1982
|
+
* @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the
|
|
1983
|
+
* existing value or node is not found in the doubly linked list.
|
|
1984
|
+
*/
|
|
1985
|
+
insertAfter(existingValueOrNode, newValue) {
|
|
1986
|
+
let existingNode;
|
|
1987
|
+
if (existingValueOrNode instanceof DoublyLinkedListNode) {
|
|
1988
|
+
existingNode = existingValueOrNode;
|
|
1989
|
+
} else {
|
|
1990
|
+
existingNode = this.getNode(existingValueOrNode);
|
|
1991
|
+
}
|
|
1992
|
+
if (existingNode) {
|
|
1993
|
+
const newNode = new DoublyLinkedListNode(newValue);
|
|
1994
|
+
newNode.next = existingNode.next;
|
|
1995
|
+
if (existingNode.next) {
|
|
1996
|
+
existingNode.next.prev = newNode;
|
|
1997
|
+
}
|
|
1998
|
+
newNode.prev = existingNode;
|
|
1999
|
+
existingNode.next = newNode;
|
|
2000
|
+
if (existingNode === this.tail) {
|
|
2001
|
+
this._tail = newNode;
|
|
2002
|
+
}
|
|
2003
|
+
this._length++;
|
|
2004
|
+
return true;
|
|
2005
|
+
}
|
|
2006
|
+
return false;
|
|
2007
|
+
}
|
|
1920
2008
|
/**
|
|
1921
2009
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
1922
2010
|
* Space Complexity: O(1)
|
|
@@ -1983,26 +2071,6 @@ var dataStructureTyped = (() => {
|
|
|
1983
2071
|
}
|
|
1984
2072
|
return false;
|
|
1985
2073
|
}
|
|
1986
|
-
/**
|
|
1987
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
1988
|
-
* Space Complexity: O(n)
|
|
1989
|
-
*/
|
|
1990
|
-
/**
|
|
1991
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
1992
|
-
* Space Complexity: O(n)
|
|
1993
|
-
*
|
|
1994
|
-
* The `toArray` function converts a linked list into an array.
|
|
1995
|
-
* @returns The `toArray()` method is returning an array of type `E[]`.
|
|
1996
|
-
*/
|
|
1997
|
-
toArray() {
|
|
1998
|
-
const array = [];
|
|
1999
|
-
let current = this.head;
|
|
2000
|
-
while (current) {
|
|
2001
|
-
array.push(current.value);
|
|
2002
|
-
current = current.next;
|
|
2003
|
-
}
|
|
2004
|
-
return array;
|
|
2005
|
-
}
|
|
2006
2074
|
/**
|
|
2007
2075
|
* The function checks if a variable has a length greater than zero and returns a boolean value.
|
|
2008
2076
|
* @returns A boolean value is being returned.
|
|
@@ -2093,6 +2161,25 @@ var dataStructureTyped = (() => {
|
|
|
2093
2161
|
}
|
|
2094
2162
|
return null;
|
|
2095
2163
|
}
|
|
2164
|
+
/**
|
|
2165
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
2166
|
+
* Space Complexity: O(1)
|
|
2167
|
+
*/
|
|
2168
|
+
/**
|
|
2169
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
2170
|
+
* Space Complexity: O(1)
|
|
2171
|
+
*
|
|
2172
|
+
* The `reverse` function reverses the order of the elements in a doubly linked list.
|
|
2173
|
+
*/
|
|
2174
|
+
reverse() {
|
|
2175
|
+
let current = this.head;
|
|
2176
|
+
[this._head, this._tail] = [this.tail, this.head];
|
|
2177
|
+
while (current) {
|
|
2178
|
+
const next = current.next;
|
|
2179
|
+
[current.prev, current.next] = [current.next, current.prev];
|
|
2180
|
+
current = next;
|
|
2181
|
+
}
|
|
2182
|
+
}
|
|
2096
2183
|
/**
|
|
2097
2184
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
2098
2185
|
* Space Complexity: O(n)
|
|
@@ -2101,35 +2188,46 @@ var dataStructureTyped = (() => {
|
|
|
2101
2188
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
2102
2189
|
* Space Complexity: O(n)
|
|
2103
2190
|
*
|
|
2104
|
-
* The `
|
|
2105
|
-
* @returns The `
|
|
2191
|
+
* The `toArray` function converts a linked list into an array.
|
|
2192
|
+
* @returns The `toArray()` method is returning an array of type `E[]`.
|
|
2106
2193
|
*/
|
|
2107
|
-
|
|
2194
|
+
toArray() {
|
|
2108
2195
|
const array = [];
|
|
2109
|
-
let current = this.
|
|
2196
|
+
let current = this.head;
|
|
2110
2197
|
while (current) {
|
|
2111
2198
|
array.push(current.value);
|
|
2112
|
-
current = current.
|
|
2199
|
+
current = current.next;
|
|
2113
2200
|
}
|
|
2114
2201
|
return array;
|
|
2115
2202
|
}
|
|
2116
2203
|
/**
|
|
2117
2204
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
2118
|
-
* Space Complexity: O(
|
|
2205
|
+
* Space Complexity: O(n)
|
|
2119
2206
|
*/
|
|
2120
2207
|
/**
|
|
2121
2208
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
2122
|
-
* Space Complexity: O(
|
|
2209
|
+
* Space Complexity: O(n)
|
|
2123
2210
|
*
|
|
2124
|
-
* The `
|
|
2211
|
+
* The `toReversedArray` function converts a doubly linked list into an array in reverse order.
|
|
2212
|
+
* @returns The `toReversedArray()` function returns an array of type `E[]`.
|
|
2125
2213
|
*/
|
|
2126
|
-
|
|
2214
|
+
toReversedArray() {
|
|
2215
|
+
const array = [];
|
|
2216
|
+
let current = this.tail;
|
|
2217
|
+
while (current) {
|
|
2218
|
+
array.push(current.value);
|
|
2219
|
+
current = current.prev;
|
|
2220
|
+
}
|
|
2221
|
+
return array;
|
|
2222
|
+
}
|
|
2223
|
+
/**
|
|
2224
|
+
* The function returns an iterator that iterates over the values of a linked list.
|
|
2225
|
+
*/
|
|
2226
|
+
*[Symbol.iterator]() {
|
|
2127
2227
|
let current = this.head;
|
|
2128
|
-
[this._head, this._tail] = [this.tail, this.head];
|
|
2129
2228
|
while (current) {
|
|
2130
|
-
|
|
2131
|
-
|
|
2132
|
-
current = next;
|
|
2229
|
+
yield current.value;
|
|
2230
|
+
current = current.next;
|
|
2133
2231
|
}
|
|
2134
2232
|
}
|
|
2135
2233
|
/**
|
|
@@ -2146,11 +2244,9 @@ var dataStructureTyped = (() => {
|
|
|
2146
2244
|
* current node in the linked list.
|
|
2147
2245
|
*/
|
|
2148
2246
|
forEach(callback) {
|
|
2149
|
-
let current = this.head;
|
|
2150
2247
|
let index = 0;
|
|
2151
|
-
|
|
2152
|
-
callback(
|
|
2153
|
-
current = current.next;
|
|
2248
|
+
for (const el of this) {
|
|
2249
|
+
callback(el, index, this);
|
|
2154
2250
|
index++;
|
|
2155
2251
|
}
|
|
2156
2252
|
}
|
|
@@ -2162,21 +2258,22 @@ var dataStructureTyped = (() => {
|
|
|
2162
2258
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
2163
2259
|
* Space Complexity: O(n)
|
|
2164
2260
|
*
|
|
2165
|
-
* The `
|
|
2166
|
-
*
|
|
2167
|
-
* @param callback - The callback parameter is a function that takes a value of type E
|
|
2168
|
-
*
|
|
2169
|
-
* DoublyLinkedList
|
|
2170
|
-
* @returns The `map` function is returning a new instance of `DoublyLinkedList<U>` that contains the mapped values.
|
|
2261
|
+
* The `filter` function iterates through a DoublyLinkedList and returns a new DoublyLinkedList containing only the
|
|
2262
|
+
* elements that satisfy the given callback function.
|
|
2263
|
+
* @param callback - The `callback` parameter is a function that takes a value of type `E` and returns a boolean value.
|
|
2264
|
+
* It is used to determine whether a value should be included in the filtered list or not.
|
|
2265
|
+
* @returns The filtered list, which is an instance of the DoublyLinkedList class.
|
|
2171
2266
|
*/
|
|
2172
|
-
|
|
2173
|
-
const
|
|
2174
|
-
let
|
|
2175
|
-
|
|
2176
|
-
|
|
2177
|
-
|
|
2267
|
+
filter(callback) {
|
|
2268
|
+
const filteredList = new _DoublyLinkedList();
|
|
2269
|
+
let index = 0;
|
|
2270
|
+
for (const current of this) {
|
|
2271
|
+
if (callback(current, index, this)) {
|
|
2272
|
+
filteredList.push(current);
|
|
2273
|
+
}
|
|
2274
|
+
index++;
|
|
2178
2275
|
}
|
|
2179
|
-
return
|
|
2276
|
+
return filteredList;
|
|
2180
2277
|
}
|
|
2181
2278
|
/**
|
|
2182
2279
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
@@ -2186,22 +2283,21 @@ var dataStructureTyped = (() => {
|
|
|
2186
2283
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
2187
2284
|
* Space Complexity: O(n)
|
|
2188
2285
|
*
|
|
2189
|
-
* The `
|
|
2190
|
-
*
|
|
2191
|
-
* @param callback - The
|
|
2192
|
-
*
|
|
2193
|
-
*
|
|
2286
|
+
* The `map` function takes a callback function and applies it to each element in the DoublyLinkedList, returning a new
|
|
2287
|
+
* DoublyLinkedList with the transformed values.
|
|
2288
|
+
* @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
|
|
2289
|
+
* the original DoublyLinkedList) and returns a value of type T (the type of values that will be stored in the mapped
|
|
2290
|
+
* DoublyLinkedList).
|
|
2291
|
+
* @returns The `map` function is returning a new instance of `DoublyLinkedList<T>` that contains the mapped values.
|
|
2194
2292
|
*/
|
|
2195
|
-
|
|
2196
|
-
const
|
|
2197
|
-
let
|
|
2198
|
-
|
|
2199
|
-
|
|
2200
|
-
|
|
2201
|
-
}
|
|
2202
|
-
current = current.next;
|
|
2293
|
+
map(callback) {
|
|
2294
|
+
const mappedList = new _DoublyLinkedList();
|
|
2295
|
+
let index = 0;
|
|
2296
|
+
for (const current of this) {
|
|
2297
|
+
mappedList.push(callback(current, index, this));
|
|
2298
|
+
index++;
|
|
2203
2299
|
}
|
|
2204
|
-
return
|
|
2300
|
+
return mappedList;
|
|
2205
2301
|
}
|
|
2206
2302
|
/**
|
|
2207
2303
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
@@ -2215,69 +2311,20 @@ var dataStructureTyped = (() => {
|
|
|
2215
2311
|
* single value.
|
|
2216
2312
|
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
|
|
2217
2313
|
* used to perform a specific operation on each element of the linked list.
|
|
2218
|
-
* @param {
|
|
2314
|
+
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
2219
2315
|
* point for the reduction operation.
|
|
2220
2316
|
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
2221
2317
|
* elements in the linked list.
|
|
2222
2318
|
*/
|
|
2223
2319
|
reduce(callback, initialValue) {
|
|
2224
2320
|
let accumulator = initialValue;
|
|
2225
|
-
let
|
|
2226
|
-
|
|
2227
|
-
accumulator = callback(accumulator, current
|
|
2228
|
-
|
|
2321
|
+
let index = 0;
|
|
2322
|
+
for (const current of this) {
|
|
2323
|
+
accumulator = callback(accumulator, current, index, this);
|
|
2324
|
+
index++;
|
|
2229
2325
|
}
|
|
2230
2326
|
return accumulator;
|
|
2231
2327
|
}
|
|
2232
|
-
/**
|
|
2233
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
2234
|
-
* Space Complexity: O(1)
|
|
2235
|
-
*/
|
|
2236
|
-
/**
|
|
2237
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
2238
|
-
* Space Complexity: O(1)
|
|
2239
|
-
*
|
|
2240
|
-
* The `insertAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
|
|
2241
|
-
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
2242
|
-
* after which the new value will be inserted. It can be either the value of the existing node or the existing node
|
|
2243
|
-
* itself.
|
|
2244
|
-
* @param {E} newValue - The value that you want to insert into the doubly linked list.
|
|
2245
|
-
* @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the
|
|
2246
|
-
* existing value or node is not found in the doubly linked list.
|
|
2247
|
-
*/
|
|
2248
|
-
insertAfter(existingValueOrNode, newValue) {
|
|
2249
|
-
let existingNode;
|
|
2250
|
-
if (existingValueOrNode instanceof DoublyLinkedListNode) {
|
|
2251
|
-
existingNode = existingValueOrNode;
|
|
2252
|
-
} else {
|
|
2253
|
-
existingNode = this.getNode(existingValueOrNode);
|
|
2254
|
-
}
|
|
2255
|
-
if (existingNode) {
|
|
2256
|
-
const newNode = new DoublyLinkedListNode(newValue);
|
|
2257
|
-
newNode.next = existingNode.next;
|
|
2258
|
-
if (existingNode.next) {
|
|
2259
|
-
existingNode.next.prev = newNode;
|
|
2260
|
-
}
|
|
2261
|
-
newNode.prev = existingNode;
|
|
2262
|
-
existingNode.next = newNode;
|
|
2263
|
-
if (existingNode === this.tail) {
|
|
2264
|
-
this._tail = newNode;
|
|
2265
|
-
}
|
|
2266
|
-
this._length++;
|
|
2267
|
-
return true;
|
|
2268
|
-
}
|
|
2269
|
-
return false;
|
|
2270
|
-
}
|
|
2271
|
-
/**
|
|
2272
|
-
* The function returns an iterator that iterates over the values of a linked list.
|
|
2273
|
-
*/
|
|
2274
|
-
*[Symbol.iterator]() {
|
|
2275
|
-
let current = this.head;
|
|
2276
|
-
while (current) {
|
|
2277
|
-
yield current.value;
|
|
2278
|
-
current = current.next;
|
|
2279
|
-
}
|
|
2280
|
-
}
|
|
2281
2328
|
};
|
|
2282
2329
|
|
|
2283
2330
|
// src/data-structures/linked-list/skip-linked-list.ts
|
|
@@ -2547,6 +2594,13 @@ var dataStructureTyped = (() => {
|
|
|
2547
2594
|
* Time Complexity: O(n), where n is the number of elements in the input array. Similar to the constructor, it requires iterating through each element.
|
|
2548
2595
|
* Space Complexity: O(n), as it creates a new stack with the elements from the input array.
|
|
2549
2596
|
*/
|
|
2597
|
+
/**
|
|
2598
|
+
* The size() function returns the number of elements in an array.
|
|
2599
|
+
* @returns The size of the elements array.
|
|
2600
|
+
*/
|
|
2601
|
+
get size() {
|
|
2602
|
+
return this.elements.length;
|
|
2603
|
+
}
|
|
2550
2604
|
/**
|
|
2551
2605
|
* Time Complexity: O(n), where n is the number of elements in the input array. Similar to the constructor, it requires iterating through each element.
|
|
2552
2606
|
* Space Complexity: O(n), as it creates a new stack with the elements from the input array.
|
|
@@ -2566,13 +2620,6 @@ var dataStructureTyped = (() => {
|
|
|
2566
2620
|
isEmpty() {
|
|
2567
2621
|
return this.elements.length === 0;
|
|
2568
2622
|
}
|
|
2569
|
-
/**
|
|
2570
|
-
* The size() function returns the number of elements in an array.
|
|
2571
|
-
* @returns The size of the elements array.
|
|
2572
|
-
*/
|
|
2573
|
-
size() {
|
|
2574
|
-
return this.elements.length;
|
|
2575
|
-
}
|
|
2576
2623
|
/**
|
|
2577
2624
|
* Time Complexity: O(1), as it only involves accessing the last element of the array.
|
|
2578
2625
|
* Space Complexity: O(1), as it does not use any additional space.
|
|
@@ -2656,6 +2703,55 @@ var dataStructureTyped = (() => {
|
|
|
2656
2703
|
clone() {
|
|
2657
2704
|
return new _Stack(this.elements.slice());
|
|
2658
2705
|
}
|
|
2706
|
+
/**
|
|
2707
|
+
* Custom iterator for the Stack class.
|
|
2708
|
+
* @returns An iterator object.
|
|
2709
|
+
*/
|
|
2710
|
+
*[Symbol.iterator]() {
|
|
2711
|
+
for (let i = this.elements.length - 1; i >= 0; i--) {
|
|
2712
|
+
yield this.elements[i];
|
|
2713
|
+
}
|
|
2714
|
+
}
|
|
2715
|
+
/**
|
|
2716
|
+
* Applies a function to each element of the stack.
|
|
2717
|
+
* @param {function(E): void} callback - A function to apply to each element.
|
|
2718
|
+
*/
|
|
2719
|
+
forEach(callback) {
|
|
2720
|
+
let index = 0;
|
|
2721
|
+
for (const el of this) {
|
|
2722
|
+
callback(el, index, this);
|
|
2723
|
+
index++;
|
|
2724
|
+
}
|
|
2725
|
+
}
|
|
2726
|
+
filter(predicate) {
|
|
2727
|
+
const newStack = new _Stack();
|
|
2728
|
+
let index = 0;
|
|
2729
|
+
for (const el of this) {
|
|
2730
|
+
if (predicate(el, index, this)) {
|
|
2731
|
+
newStack.push(el);
|
|
2732
|
+
}
|
|
2733
|
+
index++;
|
|
2734
|
+
}
|
|
2735
|
+
return newStack;
|
|
2736
|
+
}
|
|
2737
|
+
map(callback) {
|
|
2738
|
+
const newStack = new _Stack();
|
|
2739
|
+
let index = 0;
|
|
2740
|
+
for (const el of this) {
|
|
2741
|
+
newStack.push(callback(el, index, this));
|
|
2742
|
+
index++;
|
|
2743
|
+
}
|
|
2744
|
+
return newStack;
|
|
2745
|
+
}
|
|
2746
|
+
reduce(callback, initialValue) {
|
|
2747
|
+
let accumulator = initialValue;
|
|
2748
|
+
let index = 0;
|
|
2749
|
+
for (const el of this) {
|
|
2750
|
+
accumulator = callback(accumulator, el, index, this);
|
|
2751
|
+
index++;
|
|
2752
|
+
}
|
|
2753
|
+
return accumulator;
|
|
2754
|
+
}
|
|
2659
2755
|
};
|
|
2660
2756
|
|
|
2661
2757
|
// src/data-structures/queue/queue.ts
|
|
@@ -2921,6 +3017,83 @@ var dataStructureTyped = (() => {
|
|
|
2921
3017
|
yield item;
|
|
2922
3018
|
}
|
|
2923
3019
|
}
|
|
3020
|
+
/**
|
|
3021
|
+
* Time Complexity: O(n)
|
|
3022
|
+
* Space Complexity: O(1)
|
|
3023
|
+
*/
|
|
3024
|
+
/**
|
|
3025
|
+
* Time Complexity: O(n)
|
|
3026
|
+
* Space Complexity: O(1)
|
|
3027
|
+
*
|
|
3028
|
+
* The `forEach` function iterates over each element in a deque and applies a callback function to
|
|
3029
|
+
* each element.
|
|
3030
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
3031
|
+
* deque. It takes three parameters:
|
|
3032
|
+
*/
|
|
3033
|
+
forEach(callback) {
|
|
3034
|
+
let index = 0;
|
|
3035
|
+
for (const el of this) {
|
|
3036
|
+
callback(el, index, this);
|
|
3037
|
+
index++;
|
|
3038
|
+
}
|
|
3039
|
+
}
|
|
3040
|
+
/**
|
|
3041
|
+
* Time Complexity: O(n)
|
|
3042
|
+
* Space Complexity: O(n)
|
|
3043
|
+
*/
|
|
3044
|
+
/**
|
|
3045
|
+
* Time Complexity: O(n)
|
|
3046
|
+
* Space Complexity: O(n)
|
|
3047
|
+
*
|
|
3048
|
+
* The `filter` function creates a new deque containing only the elements that satisfy the given
|
|
3049
|
+
* predicate function.
|
|
3050
|
+
* @param predicate - The `predicate` parameter is a function that takes three arguments: `element`,
|
|
3051
|
+
* `index`, and `deque`.
|
|
3052
|
+
* @returns The `filter` method is returning a new `Queue` object that contains only the elements
|
|
3053
|
+
* that satisfy the given `predicate` function.
|
|
3054
|
+
*/
|
|
3055
|
+
filter(predicate) {
|
|
3056
|
+
const newDeque = new _Queue([]);
|
|
3057
|
+
let index = 0;
|
|
3058
|
+
for (const el of this) {
|
|
3059
|
+
if (predicate(el, index, this)) {
|
|
3060
|
+
newDeque.push(el);
|
|
3061
|
+
}
|
|
3062
|
+
index++;
|
|
3063
|
+
}
|
|
3064
|
+
return newDeque;
|
|
3065
|
+
}
|
|
3066
|
+
/**
|
|
3067
|
+
* Time Complexity: O(n)
|
|
3068
|
+
* Space Complexity: O(n)
|
|
3069
|
+
*/
|
|
3070
|
+
/**
|
|
3071
|
+
* Time Complexity: O(n)
|
|
3072
|
+
* Space Complexity: O(n)
|
|
3073
|
+
*
|
|
3074
|
+
* The `map` function takes a callback function and applies it to each element in the deque,
|
|
3075
|
+
* returning a new deque with the results.
|
|
3076
|
+
* @param callback - The `callback` parameter is a function that takes three arguments:
|
|
3077
|
+
* @returns The `map` method is returning a new `Queue` object with the transformed elements.
|
|
3078
|
+
*/
|
|
3079
|
+
map(callback) {
|
|
3080
|
+
const newDeque = new _Queue([]);
|
|
3081
|
+
let index = 0;
|
|
3082
|
+
for (const el of this) {
|
|
3083
|
+
newDeque.push(callback(el, index, this));
|
|
3084
|
+
index++;
|
|
3085
|
+
}
|
|
3086
|
+
return newDeque;
|
|
3087
|
+
}
|
|
3088
|
+
reduce(callback, initialValue) {
|
|
3089
|
+
let accumulator = initialValue;
|
|
3090
|
+
let index = 0;
|
|
3091
|
+
for (const el of this) {
|
|
3092
|
+
accumulator = callback(accumulator, el, index, this);
|
|
3093
|
+
index++;
|
|
3094
|
+
}
|
|
3095
|
+
return accumulator;
|
|
3096
|
+
}
|
|
2924
3097
|
};
|
|
2925
3098
|
|
|
2926
3099
|
// src/data-structures/queue/deque.ts
|
|
@@ -3505,15 +3678,21 @@ var dataStructureTyped = (() => {
|
|
|
3505
3678
|
* Time Complexity: O(n)
|
|
3506
3679
|
* Space Complexity: O(1)
|
|
3507
3680
|
*
|
|
3508
|
-
* The `
|
|
3509
|
-
*
|
|
3510
|
-
* @param callback -
|
|
3511
|
-
*
|
|
3681
|
+
* The `find` function iterates over the elements in a deque and returns the first element for which
|
|
3682
|
+
* the callback function returns true, or undefined if no such element is found.
|
|
3683
|
+
* @param callback - A function that takes three parameters: element, index, and deque. It should
|
|
3684
|
+
* return a boolean value indicating whether the element satisfies a certain condition.
|
|
3685
|
+
* @returns The method `find` returns the first element in the deque that satisfies the condition
|
|
3686
|
+
* specified by the callback function. If no element satisfies the condition, it returns `undefined`.
|
|
3512
3687
|
*/
|
|
3513
|
-
|
|
3688
|
+
find(callback) {
|
|
3514
3689
|
for (let i = 0; i < this.size; ++i) {
|
|
3515
|
-
|
|
3690
|
+
const element = this.getAt(i);
|
|
3691
|
+
if (callback(element, i, this)) {
|
|
3692
|
+
return element;
|
|
3693
|
+
}
|
|
3516
3694
|
}
|
|
3695
|
+
return void 0;
|
|
3517
3696
|
}
|
|
3518
3697
|
/**
|
|
3519
3698
|
* Time Complexity: O(n)
|
|
@@ -3523,21 +3702,20 @@ var dataStructureTyped = (() => {
|
|
|
3523
3702
|
* Time Complexity: O(n)
|
|
3524
3703
|
* Space Complexity: O(1)
|
|
3525
3704
|
*
|
|
3526
|
-
* The
|
|
3527
|
-
*
|
|
3528
|
-
* @param
|
|
3529
|
-
*
|
|
3530
|
-
* @returns The
|
|
3531
|
-
*
|
|
3705
|
+
* The function "indexOf" returns the index of the first occurrence of a given element in an array,
|
|
3706
|
+
* or -1 if the element is not found.
|
|
3707
|
+
* @param {E} element - The "element" parameter represents the element that you want to find the
|
|
3708
|
+
* index of in the data structure.
|
|
3709
|
+
* @returns The indexOf function returns the index of the first occurrence of the specified element
|
|
3710
|
+
* in the data structure. If the element is not found, it returns -1.
|
|
3532
3711
|
*/
|
|
3533
|
-
|
|
3712
|
+
indexOf(element) {
|
|
3534
3713
|
for (let i = 0; i < this.size; ++i) {
|
|
3535
|
-
|
|
3536
|
-
|
|
3537
|
-
return element;
|
|
3714
|
+
if (this.getAt(i) === element) {
|
|
3715
|
+
return i;
|
|
3538
3716
|
}
|
|
3539
3717
|
}
|
|
3540
|
-
return
|
|
3718
|
+
return -1;
|
|
3541
3719
|
}
|
|
3542
3720
|
/**
|
|
3543
3721
|
* Time Complexity: O(n)
|
|
@@ -3559,23 +3737,39 @@ var dataStructureTyped = (() => {
|
|
|
3559
3737
|
}
|
|
3560
3738
|
/**
|
|
3561
3739
|
* Time Complexity: O(n)
|
|
3562
|
-
* Space Complexity: O(
|
|
3740
|
+
* Space Complexity: O(1)
|
|
3563
3741
|
*/
|
|
3564
3742
|
/**
|
|
3565
3743
|
* Time Complexity: O(n)
|
|
3566
|
-
* Space Complexity: O(
|
|
3744
|
+
* Space Complexity: O(1)
|
|
3567
3745
|
*
|
|
3568
|
-
* The
|
|
3569
|
-
*
|
|
3570
|
-
* @param callback - The `callback` parameter is a function that takes three arguments:
|
|
3571
|
-
* @returns The `map` method is returning a new `Deque` object with the transformed elements.
|
|
3746
|
+
* The above function is an implementation of the iterator protocol in TypeScript, allowing the
|
|
3747
|
+
* object to be iterated over using a for...of loop.
|
|
3572
3748
|
*/
|
|
3573
|
-
|
|
3574
|
-
const newDeque = new _Deque([], this._bucketSize);
|
|
3749
|
+
*[Symbol.iterator]() {
|
|
3575
3750
|
for (let i = 0; i < this.size; ++i) {
|
|
3576
|
-
|
|
3751
|
+
yield this.getAt(i);
|
|
3752
|
+
}
|
|
3753
|
+
}
|
|
3754
|
+
/**
|
|
3755
|
+
* Time Complexity: O(n)
|
|
3756
|
+
* Space Complexity: O(1)
|
|
3757
|
+
*/
|
|
3758
|
+
/**
|
|
3759
|
+
* Time Complexity: O(n)
|
|
3760
|
+
* Space Complexity: O(1)
|
|
3761
|
+
*
|
|
3762
|
+
* The `forEach` function iterates over each element in a deque and applies a callback function to
|
|
3763
|
+
* each element.
|
|
3764
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
3765
|
+
* deque. It takes three parameters:
|
|
3766
|
+
*/
|
|
3767
|
+
forEach(callback) {
|
|
3768
|
+
let index = 0;
|
|
3769
|
+
for (const el of this) {
|
|
3770
|
+
callback(el, index, this);
|
|
3771
|
+
index++;
|
|
3577
3772
|
}
|
|
3578
|
-
return newDeque;
|
|
3579
3773
|
}
|
|
3580
3774
|
/**
|
|
3581
3775
|
* Time Complexity: O(n)
|
|
@@ -3594,11 +3788,34 @@ var dataStructureTyped = (() => {
|
|
|
3594
3788
|
*/
|
|
3595
3789
|
filter(predicate) {
|
|
3596
3790
|
const newDeque = new _Deque([], this._bucketSize);
|
|
3597
|
-
|
|
3598
|
-
|
|
3599
|
-
if (predicate(
|
|
3600
|
-
newDeque.push(
|
|
3791
|
+
let index = 0;
|
|
3792
|
+
for (const el of this) {
|
|
3793
|
+
if (predicate(el, index, this)) {
|
|
3794
|
+
newDeque.push(el);
|
|
3601
3795
|
}
|
|
3796
|
+
index++;
|
|
3797
|
+
}
|
|
3798
|
+
return newDeque;
|
|
3799
|
+
}
|
|
3800
|
+
/**
|
|
3801
|
+
* Time Complexity: O(n)
|
|
3802
|
+
* Space Complexity: O(n)
|
|
3803
|
+
*/
|
|
3804
|
+
/**
|
|
3805
|
+
* Time Complexity: O(n)
|
|
3806
|
+
* Space Complexity: O(n)
|
|
3807
|
+
*
|
|
3808
|
+
* The `map` function takes a callback function and applies it to each element in the deque,
|
|
3809
|
+
* returning a new deque with the results.
|
|
3810
|
+
* @param callback - The `callback` parameter is a function that takes three arguments:
|
|
3811
|
+
* @returns The `map` method is returning a new `Deque` object with the transformed elements.
|
|
3812
|
+
*/
|
|
3813
|
+
map(callback) {
|
|
3814
|
+
const newDeque = new _Deque([], this._bucketSize);
|
|
3815
|
+
let index = 0;
|
|
3816
|
+
for (const el of this) {
|
|
3817
|
+
newDeque.push(callback(el, index, this));
|
|
3818
|
+
index++;
|
|
3602
3819
|
}
|
|
3603
3820
|
return newDeque;
|
|
3604
3821
|
}
|
|
@@ -3621,50 +3838,13 @@ var dataStructureTyped = (() => {
|
|
|
3621
3838
|
*/
|
|
3622
3839
|
reduce(callback, initialValue) {
|
|
3623
3840
|
let accumulator = initialValue;
|
|
3624
|
-
|
|
3625
|
-
|
|
3841
|
+
let index = 0;
|
|
3842
|
+
for (const el of this) {
|
|
3843
|
+
accumulator = callback(accumulator, el, index, this);
|
|
3844
|
+
index++;
|
|
3626
3845
|
}
|
|
3627
3846
|
return accumulator;
|
|
3628
3847
|
}
|
|
3629
|
-
/**
|
|
3630
|
-
* Time Complexity: O(n)
|
|
3631
|
-
* Space Complexity: O(1)
|
|
3632
|
-
*/
|
|
3633
|
-
/**
|
|
3634
|
-
* Time Complexity: O(n)
|
|
3635
|
-
* Space Complexity: O(1)
|
|
3636
|
-
*
|
|
3637
|
-
* The function "indexOf" returns the index of the first occurrence of a given element in an array,
|
|
3638
|
-
* or -1 if the element is not found.
|
|
3639
|
-
* @param {E} element - The "element" parameter represents the element that you want to find the
|
|
3640
|
-
* index of in the data structure.
|
|
3641
|
-
* @returns The indexOf function returns the index of the first occurrence of the specified element
|
|
3642
|
-
* in the data structure. If the element is not found, it returns -1.
|
|
3643
|
-
*/
|
|
3644
|
-
indexOf(element) {
|
|
3645
|
-
for (let i = 0; i < this.size; ++i) {
|
|
3646
|
-
if (this.getAt(i) === element) {
|
|
3647
|
-
return i;
|
|
3648
|
-
}
|
|
3649
|
-
}
|
|
3650
|
-
return -1;
|
|
3651
|
-
}
|
|
3652
|
-
/**
|
|
3653
|
-
* Time Complexity: O(n)
|
|
3654
|
-
* Space Complexity: O(1)
|
|
3655
|
-
*/
|
|
3656
|
-
/**
|
|
3657
|
-
* Time Complexity: O(n)
|
|
3658
|
-
* Space Complexity: O(1)
|
|
3659
|
-
*
|
|
3660
|
-
* The above function is an implementation of the iterator protocol in TypeScript, allowing the
|
|
3661
|
-
* object to be iterated over using a for...of loop.
|
|
3662
|
-
*/
|
|
3663
|
-
*[Symbol.iterator]() {
|
|
3664
|
-
for (let i = 0; i < this.size; ++i) {
|
|
3665
|
-
yield this.getAt(i);
|
|
3666
|
-
}
|
|
3667
|
-
}
|
|
3668
3848
|
/**
|
|
3669
3849
|
* Time Complexity: O(n)
|
|
3670
3850
|
* Space Complexity: O(n)
|
|
@@ -4090,26 +4270,27 @@ var dataStructureTyped = (() => {
|
|
|
4090
4270
|
* @param order - Traverse order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order).
|
|
4091
4271
|
* @returns An array containing elements traversed in the specified order.
|
|
4092
4272
|
*/
|
|
4093
|
-
dfs(order) {
|
|
4273
|
+
dfs(order = "pre") {
|
|
4094
4274
|
const result = [];
|
|
4095
|
-
const
|
|
4275
|
+
const _dfs = (index) => {
|
|
4276
|
+
const left = 2 * index + 1, right = left + 1;
|
|
4096
4277
|
if (index < this.size) {
|
|
4097
4278
|
if (order === "in") {
|
|
4098
|
-
|
|
4279
|
+
_dfs(left);
|
|
4099
4280
|
result.push(this.elements[index]);
|
|
4100
|
-
|
|
4281
|
+
_dfs(right);
|
|
4101
4282
|
} else if (order === "pre") {
|
|
4102
4283
|
result.push(this.elements[index]);
|
|
4103
|
-
|
|
4104
|
-
|
|
4284
|
+
_dfs(left);
|
|
4285
|
+
_dfs(right);
|
|
4105
4286
|
} else if (order === "post") {
|
|
4106
|
-
|
|
4107
|
-
|
|
4287
|
+
_dfs(left);
|
|
4288
|
+
_dfs(right);
|
|
4108
4289
|
result.push(this.elements[index]);
|
|
4109
4290
|
}
|
|
4110
4291
|
}
|
|
4111
4292
|
};
|
|
4112
|
-
|
|
4293
|
+
_dfs(0);
|
|
4113
4294
|
return result;
|
|
4114
4295
|
}
|
|
4115
4296
|
/**
|
|
@@ -4177,6 +4358,47 @@ var dataStructureTyped = (() => {
|
|
|
4177
4358
|
for (let i = Math.floor(this.size / 2); i >= 0; i--)
|
|
4178
4359
|
this._sinkDown(i, this.elements.length >> 1);
|
|
4179
4360
|
}
|
|
4361
|
+
*[Symbol.iterator]() {
|
|
4362
|
+
for (const element of this.elements) {
|
|
4363
|
+
yield element;
|
|
4364
|
+
}
|
|
4365
|
+
}
|
|
4366
|
+
forEach(callback) {
|
|
4367
|
+
let index = 0;
|
|
4368
|
+
for (const el of this) {
|
|
4369
|
+
callback(el, index, this);
|
|
4370
|
+
index++;
|
|
4371
|
+
}
|
|
4372
|
+
}
|
|
4373
|
+
filter(predicate) {
|
|
4374
|
+
const filteredHeap = new _Heap({ comparator: this.comparator });
|
|
4375
|
+
let index = 0;
|
|
4376
|
+
for (const el of this) {
|
|
4377
|
+
if (predicate(el, index, this)) {
|
|
4378
|
+
filteredHeap.push(el);
|
|
4379
|
+
}
|
|
4380
|
+
index++;
|
|
4381
|
+
}
|
|
4382
|
+
return filteredHeap;
|
|
4383
|
+
}
|
|
4384
|
+
map(callback, comparator) {
|
|
4385
|
+
const mappedHeap = new _Heap({ comparator });
|
|
4386
|
+
let index = 0;
|
|
4387
|
+
for (const el of this) {
|
|
4388
|
+
mappedHeap.add(callback(el, index, this));
|
|
4389
|
+
index++;
|
|
4390
|
+
}
|
|
4391
|
+
return mappedHeap;
|
|
4392
|
+
}
|
|
4393
|
+
reduce(callback, initialValue) {
|
|
4394
|
+
let accumulator = initialValue;
|
|
4395
|
+
let index = 0;
|
|
4396
|
+
for (const el of this) {
|
|
4397
|
+
accumulator = callback(accumulator, el, index, this);
|
|
4398
|
+
index++;
|
|
4399
|
+
}
|
|
4400
|
+
return accumulator;
|
|
4401
|
+
}
|
|
4180
4402
|
/**
|
|
4181
4403
|
* Time Complexity: O(log n)
|
|
4182
4404
|
* Space Complexity: O(1)
|
|
@@ -11114,7 +11336,7 @@ var dataStructureTyped = (() => {
|
|
|
11114
11336
|
this.children = /* @__PURE__ */ new Map();
|
|
11115
11337
|
}
|
|
11116
11338
|
};
|
|
11117
|
-
var Trie = class {
|
|
11339
|
+
var Trie = class _Trie {
|
|
11118
11340
|
constructor(words, caseSensitive = true) {
|
|
11119
11341
|
__publicField(this, "_caseSensitive");
|
|
11120
11342
|
__publicField(this, "_root");
|
|
@@ -11397,6 +11619,53 @@ var dataStructureTyped = (() => {
|
|
|
11397
11619
|
dfs(startNode, prefix);
|
|
11398
11620
|
return words;
|
|
11399
11621
|
}
|
|
11622
|
+
*[Symbol.iterator]() {
|
|
11623
|
+
function* _dfs(node, path) {
|
|
11624
|
+
if (node.isEnd) {
|
|
11625
|
+
yield path;
|
|
11626
|
+
}
|
|
11627
|
+
for (const [char, childNode] of node.children) {
|
|
11628
|
+
yield* __yieldStar(_dfs(childNode, path + char));
|
|
11629
|
+
}
|
|
11630
|
+
}
|
|
11631
|
+
yield* __yieldStar(_dfs(this.root, ""));
|
|
11632
|
+
}
|
|
11633
|
+
forEach(callback) {
|
|
11634
|
+
let index = 0;
|
|
11635
|
+
for (const word of this) {
|
|
11636
|
+
callback(word, index, this);
|
|
11637
|
+
index++;
|
|
11638
|
+
}
|
|
11639
|
+
}
|
|
11640
|
+
filter(predicate) {
|
|
11641
|
+
const results = [];
|
|
11642
|
+
let index = 0;
|
|
11643
|
+
for (const word of this) {
|
|
11644
|
+
if (predicate(word, index, this)) {
|
|
11645
|
+
results.push(word);
|
|
11646
|
+
}
|
|
11647
|
+
index++;
|
|
11648
|
+
}
|
|
11649
|
+
return results;
|
|
11650
|
+
}
|
|
11651
|
+
map(callback) {
|
|
11652
|
+
const newTrie = new _Trie();
|
|
11653
|
+
let index = 0;
|
|
11654
|
+
for (const word of this) {
|
|
11655
|
+
newTrie.add(callback(word, index, this));
|
|
11656
|
+
index++;
|
|
11657
|
+
}
|
|
11658
|
+
return newTrie;
|
|
11659
|
+
}
|
|
11660
|
+
reduce(callback, initialValue) {
|
|
11661
|
+
let accumulator = initialValue;
|
|
11662
|
+
let index = 0;
|
|
11663
|
+
for (const word of this) {
|
|
11664
|
+
accumulator = callback(accumulator, word, index, this);
|
|
11665
|
+
index++;
|
|
11666
|
+
}
|
|
11667
|
+
return accumulator;
|
|
11668
|
+
}
|
|
11400
11669
|
/**
|
|
11401
11670
|
* Time Complexity: O(M), where M is the length of the input string.
|
|
11402
11671
|
* Space Complexity: O(1) - Constant space.
|