data-structure-typed 1.54.3 → 2.0.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/CHANGELOG.md +1 -1
- package/benchmark/report.html +26 -2
- package/benchmark/report.json +292 -42
- package/dist/cjs/data-structures/base/iterable-element-base.d.ts +14 -40
- package/dist/cjs/data-structures/base/iterable-element-base.js +14 -11
- package/dist/cjs/data-structures/base/iterable-element-base.js.map +1 -1
- package/dist/cjs/data-structures/base/linear-base.d.ts +277 -0
- package/dist/cjs/data-structures/base/linear-base.js +553 -0
- package/dist/cjs/data-structures/base/linear-base.js.map +1 -0
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +12 -8
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +50 -37
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +64 -0
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +64 -0
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +62 -0
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +67 -5
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +3 -3
- package/dist/cjs/data-structures/binary-tree/bst.js +14 -14
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/red-black-tree.d.ts +1 -7
- package/dist/cjs/data-structures/binary-tree/red-black-tree.js +1 -7
- package/dist/cjs/data-structures/binary-tree/red-black-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +175 -14
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +210 -40
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js +16 -16
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +46 -0
- package/dist/cjs/data-structures/hash/hash-map.js +46 -0
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +3 -11
- package/dist/cjs/data-structures/heap/heap.js +0 -10
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.d.ts +2 -2
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.d.ts +2 -2
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +65 -94
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +131 -146
- 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 +145 -75
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +283 -169
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -2
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -2
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +2 -2
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +130 -91
- package/dist/cjs/data-structures/queue/deque.js +269 -169
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +131 -40
- package/dist/cjs/data-structures/queue/queue.js +181 -50
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +124 -11
- package/dist/cjs/data-structures/stack/stack.js +121 -10
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +4 -3
- package/dist/cjs/data-structures/trie/trie.js +3 -0
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/types/data-structures/base/base.d.ts +9 -4
- package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
- package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
- package/dist/cjs/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
- package/dist/cjs/types/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/cjs/types/data-structures/queue/deque.d.ts +2 -3
- package/dist/cjs/types/data-structures/queue/queue.d.ts +2 -2
- package/dist/esm/data-structures/base/iterable-element-base.d.ts +14 -40
- package/dist/esm/data-structures/base/iterable-element-base.js +14 -11
- package/dist/esm/data-structures/base/iterable-element-base.js.map +1 -1
- package/dist/esm/data-structures/base/linear-base.d.ts +277 -0
- package/dist/esm/data-structures/base/linear-base.js +549 -0
- package/dist/esm/data-structures/base/linear-base.js.map +1 -0
- package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.d.ts +12 -8
- package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js +50 -36
- package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/avl-tree.d.ts +64 -0
- package/dist/esm/data-structures/binary-tree/avl-tree.js +64 -0
- package/dist/esm/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/binary-tree.d.ts +62 -0
- package/dist/esm/data-structures/binary-tree/binary-tree.js +67 -5
- package/dist/esm/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/bst.d.ts +3 -3
- package/dist/esm/data-structures/binary-tree/bst.js +14 -12
- package/dist/esm/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/red-black-tree.d.ts +1 -7
- package/dist/esm/data-structures/binary-tree/red-black-tree.js +1 -7
- package/dist/esm/data-structures/binary-tree/red-black-tree.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/tree-multi-map.d.ts +175 -14
- package/dist/esm/data-structures/binary-tree/tree-multi-map.js +210 -39
- package/dist/esm/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/esm/data-structures/graph/abstract-graph.js +16 -16
- package/dist/esm/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/esm/data-structures/hash/hash-map.d.ts +46 -0
- package/dist/esm/data-structures/hash/hash-map.js +46 -0
- package/dist/esm/data-structures/hash/hash-map.js.map +1 -1
- package/dist/esm/data-structures/heap/heap.d.ts +3 -11
- package/dist/esm/data-structures/heap/heap.js +0 -10
- package/dist/esm/data-structures/heap/heap.js.map +1 -1
- package/dist/esm/data-structures/heap/max-heap.d.ts +2 -2
- package/dist/esm/data-structures/heap/max-heap.js.map +1 -1
- package/dist/esm/data-structures/heap/min-heap.d.ts +2 -2
- package/dist/esm/data-structures/heap/min-heap.js.map +1 -1
- package/dist/esm/data-structures/linked-list/doubly-linked-list.d.ts +65 -94
- package/dist/esm/data-structures/linked-list/doubly-linked-list.js +132 -148
- package/dist/esm/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/esm/data-structures/linked-list/singly-linked-list.d.ts +145 -75
- package/dist/esm/data-structures/linked-list/singly-linked-list.js +283 -170
- package/dist/esm/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/esm/data-structures/priority-queue/max-priority-queue.d.ts +2 -2
- package/dist/esm/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/esm/data-structures/priority-queue/min-priority-queue.d.ts +2 -2
- package/dist/esm/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/esm/data-structures/priority-queue/priority-queue.d.ts +2 -2
- package/dist/esm/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/esm/data-structures/queue/deque.d.ts +130 -91
- package/dist/esm/data-structures/queue/deque.js +269 -169
- package/dist/esm/data-structures/queue/deque.js.map +1 -1
- package/dist/esm/data-structures/queue/queue.d.ts +131 -40
- package/dist/esm/data-structures/queue/queue.js +182 -51
- package/dist/esm/data-structures/queue/queue.js.map +1 -1
- package/dist/esm/data-structures/stack/stack.d.ts +124 -11
- package/dist/esm/data-structures/stack/stack.js +121 -10
- package/dist/esm/data-structures/stack/stack.js.map +1 -1
- package/dist/esm/data-structures/trie/trie.d.ts +4 -3
- package/dist/esm/data-structures/trie/trie.js +3 -0
- package/dist/esm/data-structures/trie/trie.js.map +1 -1
- package/dist/esm/types/data-structures/base/base.d.ts +9 -4
- package/dist/esm/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
- package/dist/esm/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
- package/dist/esm/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
- package/dist/esm/types/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/esm/types/data-structures/queue/deque.d.ts +2 -3
- package/dist/esm/types/data-structures/queue/queue.d.ts +2 -2
- package/dist/individuals/binary-tree/avl-tree-counter.mjs +4701 -0
- package/dist/individuals/binary-tree/avl-tree-multi-map.mjs +4514 -0
- package/dist/individuals/binary-tree/avl-tree.mjs +4321 -0
- package/dist/individuals/binary-tree/binary-tree.mjs +3097 -0
- package/dist/individuals/binary-tree/bst.mjs +3858 -0
- package/dist/individuals/binary-tree/red-black-tree.mjs +4391 -0
- package/dist/individuals/binary-tree/tree-counter.mjs +4806 -0
- package/dist/individuals/binary-tree/tree-multi-map.mjs +4582 -0
- package/dist/individuals/graph/directed-graph.mjs +2910 -0
- package/dist/individuals/graph/undirected-graph.mjs +2745 -0
- package/dist/individuals/hash/hash-map.mjs +1040 -0
- package/dist/individuals/heap/heap.mjs +909 -0
- package/dist/individuals/heap/max-heap.mjs +671 -0
- package/dist/individuals/heap/min-heap.mjs +659 -0
- package/dist/individuals/linked-list/doubly-linked-list.mjs +1495 -0
- package/dist/individuals/linked-list/singly-linked-list.mjs +1479 -0
- package/dist/individuals/priority-queue/max-priority-queue.mjs +768 -0
- package/dist/individuals/priority-queue/min-priority-queue.mjs +757 -0
- package/dist/individuals/priority-queue/priority-queue.mjs +670 -0
- package/dist/individuals/queue/deque.mjs +1262 -0
- package/dist/individuals/queue/queue.mjs +1865 -0
- package/dist/individuals/stack/stack.mjs +415 -0
- package/dist/individuals/trie/trie.mjs +687 -0
- package/dist/umd/data-structure-typed.js +1260 -649
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +7 -7
- package/src/data-structures/base/iterable-element-base.ts +29 -20
- package/src/data-structures/base/linear-base.ts +649 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +51 -36
- package/src/data-structures/binary-tree/avl-tree.ts +64 -0
- package/src/data-structures/binary-tree/binary-tree.ts +5 -5
- package/src/data-structures/binary-tree/bst.ts +9 -9
- package/src/data-structures/binary-tree/tree-multi-map.ts +214 -40
- package/src/data-structures/graph/abstract-graph.ts +16 -16
- package/src/data-structures/hash/hash-map.ts +46 -0
- package/src/data-structures/heap/heap.ts +3 -14
- package/src/data-structures/heap/max-heap.ts +2 -2
- package/src/data-structures/heap/min-heap.ts +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +144 -160
- package/src/data-structures/linked-list/singly-linked-list.ts +307 -185
- package/src/data-structures/priority-queue/max-priority-queue.ts +2 -5
- package/src/data-structures/priority-queue/min-priority-queue.ts +2 -5
- package/src/data-structures/priority-queue/priority-queue.ts +2 -2
- package/src/data-structures/queue/deque.ts +286 -183
- package/src/data-structures/queue/queue.ts +196 -63
- package/src/data-structures/stack/stack.ts +124 -18
- package/src/data-structures/trie/trie.ts +7 -3
- package/src/types/data-structures/base/base.ts +17 -8
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +1 -1
- package/src/types/data-structures/linked-list/doubly-linked-list.ts +2 -2
- package/src/types/data-structures/linked-list/singly-linked-list.ts +2 -2
- package/src/types/data-structures/queue/deque.ts +2 -3
- package/src/types/data-structures/queue/queue.ts +2 -2
- package/test/integration/all-in-one.test.ts +1 -1
- package/test/integration/avl-tree.test.ts +1 -1
- package/test/integration/bst.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +168 -0
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +15 -14
- package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +165 -7
- package/test/unit/data-structures/graph/directed-graph.test.ts +37 -37
- package/test/unit/data-structures/graph/undirected-graph.test.ts +2 -2
- package/test/unit/data-structures/hash/hash-map.test.ts +135 -0
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +135 -27
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +183 -12
- package/test/unit/data-structures/queue/deque.test.ts +241 -60
- package/test/unit/data-structures/queue/queue.test.ts +332 -19
- package/test/unit/data-structures/stack/stack.test.ts +165 -0
- package/test/unit/unrestricted-interconversion.test.ts +1 -1
|
@@ -0,0 +1,415 @@
|
|
|
1
|
+
// src/data-structures/base/iterable-element-base.ts
|
|
2
|
+
var IterableElementBase = class {
|
|
3
|
+
/**
|
|
4
|
+
* The protected constructor initializes the options for the IterableElementBase class, including the
|
|
5
|
+
* toElementFn function.
|
|
6
|
+
* @param [options] - An optional object that contains the following properties:
|
|
7
|
+
*/
|
|
8
|
+
constructor(options) {
|
|
9
|
+
if (options) {
|
|
10
|
+
const { toElementFn } = options;
|
|
11
|
+
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
12
|
+
else if (toElementFn) throw new TypeError("toElementFn must be a function type");
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
_toElementFn;
|
|
16
|
+
get toElementFn() {
|
|
17
|
+
return this._toElementFn;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Time Complexity: O(n)
|
|
21
|
+
* Space Complexity: O(1)
|
|
22
|
+
*
|
|
23
|
+
* The function is an implementation of the Symbol.iterator method that returns an IterableIterator.
|
|
24
|
+
* @param {any[]} args - The `args` parameter in the code snippet represents a rest parameter. It
|
|
25
|
+
* allows the function to accept any number of arguments as an array. In this case, the `args`
|
|
26
|
+
* parameter is used to pass any number of arguments to the `_getIterator` method.
|
|
27
|
+
*/
|
|
28
|
+
*[Symbol.iterator](...args) {
|
|
29
|
+
yield* this._getIterator(...args);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Time Complexity: O(n)
|
|
33
|
+
* Space Complexity: O(n)
|
|
34
|
+
*
|
|
35
|
+
* The function returns an iterator that yields all the values in the object.
|
|
36
|
+
*/
|
|
37
|
+
*values() {
|
|
38
|
+
for (const item of this) {
|
|
39
|
+
yield item;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Time Complexity: O(n)
|
|
44
|
+
* Space Complexity: O(1)
|
|
45
|
+
*
|
|
46
|
+
* The `every` function checks if every element in the array satisfies a given predicate.
|
|
47
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
48
|
+
* the current element being processed, its index, and the array it belongs to. It should return a
|
|
49
|
+
* boolean value indicating whether the element satisfies a certain condition or not.
|
|
50
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
51
|
+
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
52
|
+
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
53
|
+
* @returns The `every` method is returning a boolean value. It returns `true` if every element in
|
|
54
|
+
* the array satisfies the provided predicate function, and `false` otherwise.
|
|
55
|
+
*/
|
|
56
|
+
every(predicate, thisArg) {
|
|
57
|
+
let index = 0;
|
|
58
|
+
for (const item of this) {
|
|
59
|
+
if (!predicate.call(thisArg, item, index++, this)) {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Time Complexity: O(n)
|
|
67
|
+
* Space Complexity: O(1)
|
|
68
|
+
*
|
|
69
|
+
* The "some" function checks if at least one element in a collection satisfies a given predicate.
|
|
70
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
71
|
+
* `value`, `index`, and `array`. It should return a boolean value indicating whether the current
|
|
72
|
+
* element satisfies the condition.
|
|
73
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
74
|
+
* to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
|
|
75
|
+
* it will be passed as the `this` value to the `predicate` function. If `thisArg
|
|
76
|
+
* @returns a boolean value. It returns true if the predicate function returns true for any element
|
|
77
|
+
* in the collection, and false otherwise.
|
|
78
|
+
*/
|
|
79
|
+
some(predicate, thisArg) {
|
|
80
|
+
let index = 0;
|
|
81
|
+
for (const item of this) {
|
|
82
|
+
if (predicate.call(thisArg, item, index++, this)) {
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Time Complexity: O(n)
|
|
90
|
+
* Space Complexity: O(1)
|
|
91
|
+
*
|
|
92
|
+
* The `forEach` function iterates over each element in an array-like object and calls a callback
|
|
93
|
+
* function for each element.
|
|
94
|
+
* @param callbackfn - The callbackfn parameter is a function that will be called for each element in
|
|
95
|
+
* the array. It takes three arguments: the current element being processed, the index of the current
|
|
96
|
+
* element, and the array that forEach was called upon.
|
|
97
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
98
|
+
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
|
|
99
|
+
* be passed as the `this` value to the `callbackfn` function. If `thisArg
|
|
100
|
+
*/
|
|
101
|
+
forEach(callbackfn, thisArg) {
|
|
102
|
+
let index = 0;
|
|
103
|
+
for (const item of this) {
|
|
104
|
+
callbackfn.call(thisArg, item, index++, this);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Time Complexity: O(n)
|
|
109
|
+
* Space Complexity: O(1)
|
|
110
|
+
*
|
|
111
|
+
* The `find` function iterates over the elements of an array-like object and returns the first
|
|
112
|
+
* element that satisfies the provided callback function.
|
|
113
|
+
* @param predicate - The predicate parameter is a function that will be called for each element in
|
|
114
|
+
* the array. It takes three arguments: the current element being processed, the index of the current
|
|
115
|
+
* element, and the array itself. The function should return a boolean value indicating whether the
|
|
116
|
+
* current element matches the desired condition.
|
|
117
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
118
|
+
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
|
|
119
|
+
* be passed as the `this` value to the `callbackfn` function. If `thisArg
|
|
120
|
+
* @returns The `find` method returns the first element in the array that satisfies the provided
|
|
121
|
+
* callback function. If no element satisfies the callback function, `undefined` is returned.
|
|
122
|
+
*/
|
|
123
|
+
find(predicate, thisArg) {
|
|
124
|
+
let index = 0;
|
|
125
|
+
for (const item of this) {
|
|
126
|
+
if (predicate.call(thisArg, item, index++, this)) return item;
|
|
127
|
+
}
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Time Complexity: O(n)
|
|
132
|
+
* Space Complexity: O(1)
|
|
133
|
+
*
|
|
134
|
+
* The function checks if a given element exists in a collection.
|
|
135
|
+
* @param {E} element - The parameter "element" is of type E, which means it can be any type. It
|
|
136
|
+
* represents the element that we want to check for existence in the collection.
|
|
137
|
+
* @returns a boolean value. It returns true if the element is found in the collection, and false
|
|
138
|
+
* otherwise.
|
|
139
|
+
*/
|
|
140
|
+
has(element) {
|
|
141
|
+
for (const ele of this) {
|
|
142
|
+
if (ele === element) return true;
|
|
143
|
+
}
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Time Complexity: O(n)
|
|
148
|
+
* Space Complexity: O(1)
|
|
149
|
+
*
|
|
150
|
+
* The `reduce` function iterates over the elements of an array-like object and applies a callback
|
|
151
|
+
* function to reduce them into a single value.
|
|
152
|
+
* @param callbackfn - The callbackfn parameter is a function that will be called for each element in
|
|
153
|
+
* the array. It takes four arguments:
|
|
154
|
+
* @param {U} initialValue - The initialValue parameter is the initial value of the accumulator. It
|
|
155
|
+
* is the value that the accumulator starts with before the reduction operation begins.
|
|
156
|
+
* @returns The `reduce` method is returning the final value of the accumulator after iterating over
|
|
157
|
+
* all the elements in the array and applying the callback function to each element.
|
|
158
|
+
*/
|
|
159
|
+
reduce(callbackfn, initialValue) {
|
|
160
|
+
let accumulator = initialValue ?? 0;
|
|
161
|
+
let index = 0;
|
|
162
|
+
for (const item of this) {
|
|
163
|
+
accumulator = callbackfn(accumulator, item, index++, this);
|
|
164
|
+
}
|
|
165
|
+
return accumulator;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Time Complexity: O(n)
|
|
169
|
+
* Space Complexity: O(n)
|
|
170
|
+
*
|
|
171
|
+
* The `toArray` function converts a linked list into an array.
|
|
172
|
+
* @returns The `toArray()` method is returning an array of type `E[]`.
|
|
173
|
+
*/
|
|
174
|
+
toArray() {
|
|
175
|
+
return [...this];
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Time Complexity: O(n)
|
|
179
|
+
* Space Complexity: O(n)
|
|
180
|
+
*
|
|
181
|
+
* The print function logs the elements of an array to the console.
|
|
182
|
+
*/
|
|
183
|
+
toVisual() {
|
|
184
|
+
return [...this];
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Time Complexity: O(n)
|
|
188
|
+
* Space Complexity: O(n)
|
|
189
|
+
*
|
|
190
|
+
* The print function logs the elements of an array to the console.
|
|
191
|
+
*/
|
|
192
|
+
print() {
|
|
193
|
+
console.log(this.toVisual());
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
// src/data-structures/stack/stack.ts
|
|
198
|
+
var Stack = class _Stack extends IterableElementBase {
|
|
199
|
+
constructor(elements = [], options) {
|
|
200
|
+
super(options);
|
|
201
|
+
this.pushMany(elements);
|
|
202
|
+
}
|
|
203
|
+
_elements = [];
|
|
204
|
+
/**
|
|
205
|
+
* The elements function returns the elements of this set.
|
|
206
|
+
* @return An array of elements
|
|
207
|
+
*/
|
|
208
|
+
get elements() {
|
|
209
|
+
return this._elements;
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* The size() function returns the number of elements in an array.
|
|
213
|
+
* @returns The size of the elements array.
|
|
214
|
+
*/
|
|
215
|
+
get size() {
|
|
216
|
+
return this.elements.length;
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Time Complexity: O(n)
|
|
220
|
+
* Space Complexity: O(n)
|
|
221
|
+
*
|
|
222
|
+
* The function "fromArray" creates a new Stack object from an array of elements.
|
|
223
|
+
* @param {E[]} elements - The `elements` parameter is an array of elements of type `E`.
|
|
224
|
+
* @returns {Stack} The method is returning a new instance of the Stack class, initialized with the elements from the input
|
|
225
|
+
* array.
|
|
226
|
+
*/
|
|
227
|
+
static fromArray(elements) {
|
|
228
|
+
return new _Stack(elements);
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Time Complexity: O(1)
|
|
232
|
+
* Space Complexity: O(1)
|
|
233
|
+
*
|
|
234
|
+
* The function checks if an array is empty and returns a boolean value.
|
|
235
|
+
* @returns A boolean value indicating whether the `_elements` array is empty or not.
|
|
236
|
+
*/
|
|
237
|
+
isEmpty() {
|
|
238
|
+
return this.elements.length === 0;
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Time Complexity: O(1)
|
|
242
|
+
* Space Complexity: O(1)
|
|
243
|
+
*
|
|
244
|
+
* The `peek` function returns the last element of an array, or undefined if the array is empty.
|
|
245
|
+
* @returns The `peek()` function returns the last element of the `_elements` array, or `undefined` if the array is empty.
|
|
246
|
+
*/
|
|
247
|
+
peek() {
|
|
248
|
+
if (this.isEmpty()) return void 0;
|
|
249
|
+
return this.elements[this.elements.length - 1];
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Time Complexity: O(1)
|
|
253
|
+
* Space Complexity: O(1)
|
|
254
|
+
*
|
|
255
|
+
* The push function adds an element to the stack and returns the updated stack.
|
|
256
|
+
* @param {E} element - The parameter "element" is of type E, which means it can be any data type.
|
|
257
|
+
* @returns The `push` method is returning the updated `Stack<E>` object.
|
|
258
|
+
*/
|
|
259
|
+
push(element) {
|
|
260
|
+
this.elements.push(element);
|
|
261
|
+
return true;
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Time Complexity: O(1)
|
|
265
|
+
* Space Complexity: O(1)
|
|
266
|
+
*
|
|
267
|
+
* The `pop` function removes and returns the last element from an array, or returns undefined if the array is empty.
|
|
268
|
+
* @returns The `pop()` method is returning the last element of the array `_elements` if the array is not empty. If the
|
|
269
|
+
* array is empty, it returns `undefined`.
|
|
270
|
+
*/
|
|
271
|
+
pop() {
|
|
272
|
+
if (this.isEmpty()) return;
|
|
273
|
+
return this.elements.pop();
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Time Complexity: O(k)
|
|
277
|
+
* Space Complexity: O(1)
|
|
278
|
+
*
|
|
279
|
+
* The function `pushMany` iterates over elements and pushes them into an array after applying a
|
|
280
|
+
* transformation function if provided.
|
|
281
|
+
* @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the `pushMany` function
|
|
282
|
+
* is an iterable containing elements of type `E` or `R`. The function iterates over each element in
|
|
283
|
+
* the iterable and pushes it into the data structure. If a transformation function `toElementFn` is
|
|
284
|
+
* provided, it is used to
|
|
285
|
+
* @returns The `pushMany` function is returning an array of boolean values indicating whether each
|
|
286
|
+
* element was successfully pushed into the data structure.
|
|
287
|
+
*/
|
|
288
|
+
pushMany(elements) {
|
|
289
|
+
const ans = [];
|
|
290
|
+
for (const el of elements) {
|
|
291
|
+
if (this.toElementFn) {
|
|
292
|
+
ans.push(this.push(this.toElementFn(el)));
|
|
293
|
+
} else {
|
|
294
|
+
ans.push(this.push(el));
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
return ans;
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Time Complexity: O(n)
|
|
301
|
+
* Space Complexity: O(1)
|
|
302
|
+
*
|
|
303
|
+
* The toArray function returns a copy of the elements in an array.
|
|
304
|
+
* @returns An array of type E.
|
|
305
|
+
*/
|
|
306
|
+
delete(element) {
|
|
307
|
+
const index = this.elements.indexOf(element);
|
|
308
|
+
return this.deleteAt(index);
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Time Complexity: O(n)
|
|
312
|
+
* Space Complexity: O(1)
|
|
313
|
+
*
|
|
314
|
+
* The toArray function returns a copy of the elements in an array.
|
|
315
|
+
* @returns An array of type E.
|
|
316
|
+
*/
|
|
317
|
+
deleteAt(index) {
|
|
318
|
+
const spliced = this.elements.splice(index, 1);
|
|
319
|
+
return spliced.length === 1;
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Time Complexity: O(1)
|
|
323
|
+
* Space Complexity: O(1)
|
|
324
|
+
*
|
|
325
|
+
* The clear function clears the elements array.
|
|
326
|
+
*/
|
|
327
|
+
clear() {
|
|
328
|
+
this._elements = [];
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Time Complexity: O(n)
|
|
332
|
+
* Space Complexity: O(n)
|
|
333
|
+
*
|
|
334
|
+
* The `clone()` function returns a new `Stack` object with the same elements as the original stack.
|
|
335
|
+
* @returns The `clone()` method is returning a new `Stack` object with a copy of the `_elements` array.
|
|
336
|
+
*/
|
|
337
|
+
clone() {
|
|
338
|
+
return new _Stack(this, { toElementFn: this.toElementFn });
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Time Complexity: O(n)
|
|
342
|
+
* Space Complexity: O(n)
|
|
343
|
+
*
|
|
344
|
+
* The `filter` function creates a new stack containing elements from the original stack that satisfy
|
|
345
|
+
* a given predicate function.
|
|
346
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
347
|
+
* the current element being iterated over, the index of the current element, and the stack itself.
|
|
348
|
+
* It should return a boolean value indicating whether the element should be included in the filtered
|
|
349
|
+
* stack or not.
|
|
350
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
351
|
+
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
352
|
+
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
353
|
+
* @returns The `filter` method is returning a new `Stack` object that contains the elements that
|
|
354
|
+
* satisfy the given predicate function.
|
|
355
|
+
*/
|
|
356
|
+
filter(predicate, thisArg) {
|
|
357
|
+
const newStack = new _Stack([], { toElementFn: this.toElementFn });
|
|
358
|
+
let index = 0;
|
|
359
|
+
for (const el of this) {
|
|
360
|
+
if (predicate.call(thisArg, el, index, this)) {
|
|
361
|
+
newStack.push(el);
|
|
362
|
+
}
|
|
363
|
+
index++;
|
|
364
|
+
}
|
|
365
|
+
return newStack;
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* Time Complexity: O(n)
|
|
369
|
+
* Space Complexity: O(n)
|
|
370
|
+
*
|
|
371
|
+
* The `map` function takes a callback function and applies it to each element in the stack,
|
|
372
|
+
* returning a new stack with the results.
|
|
373
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
374
|
+
* stack. It takes three arguments: the current element, the index of the element, and the stack
|
|
375
|
+
* itself. It should return a new value that will be added to the new stack.
|
|
376
|
+
* @param [toElementFn] - The `toElementFn` parameter is an optional function that can be used to
|
|
377
|
+
* transform the raw element (`RM`) into a new element (`EM`) before pushing it into the new stack.
|
|
378
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
379
|
+
* specify the value of `this` within the callback function. It is used to set the context or scope
|
|
380
|
+
* in which the callback function will be executed. If `thisArg` is provided, it will be used as the
|
|
381
|
+
* value of
|
|
382
|
+
* @returns a new Stack object with elements of type EM and raw elements of type RM.
|
|
383
|
+
*/
|
|
384
|
+
map(callback, toElementFn, thisArg) {
|
|
385
|
+
const newStack = new _Stack([], { toElementFn });
|
|
386
|
+
let index = 0;
|
|
387
|
+
for (const el of this) {
|
|
388
|
+
newStack.push(callback.call(thisArg, el, index, this));
|
|
389
|
+
index++;
|
|
390
|
+
}
|
|
391
|
+
return newStack;
|
|
392
|
+
}
|
|
393
|
+
/**
|
|
394
|
+
* Time Complexity: O(n)
|
|
395
|
+
* Space Complexity: O(n)
|
|
396
|
+
*
|
|
397
|
+
* Custom iterator for the Stack class.
|
|
398
|
+
* @returns An iterator object.
|
|
399
|
+
*/
|
|
400
|
+
*_getIterator() {
|
|
401
|
+
for (let i = 0; i < this.elements.length; i++) {
|
|
402
|
+
yield this.elements[i];
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
};
|
|
406
|
+
export {
|
|
407
|
+
Stack
|
|
408
|
+
};
|
|
409
|
+
/**
|
|
410
|
+
* data-structure-typed
|
|
411
|
+
*
|
|
412
|
+
* @author Pablo Zeng
|
|
413
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
414
|
+
* @license MIT License
|
|
415
|
+
*/
|