data-structure-typed 2.4.5 → 2.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.vitepress/cache/deps_temp_51f5f1b0/chunk-7OIKW5WK.js +12984 -0
- package/.vitepress/cache/deps_temp_51f5f1b0/package.json +3 -0
- package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vue_devtools-api.js +4505 -0
- package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vueuse_core.js +9731 -0
- package/.vitepress/cache/deps_temp_51f5f1b0/vue.js +347 -0
- package/CHANGELOG.md +3 -1
- package/README.md +78 -31
- package/dist/cjs/binary-tree.cjs +23698 -0
- package/dist/cjs/graph.cjs +5236 -0
- package/dist/cjs/hash.cjs +1262 -0
- package/dist/cjs/heap.cjs +1540 -0
- package/dist/cjs/index.cjs +24509 -2899
- package/dist/cjs/linked-list.cjs +4370 -0
- package/dist/cjs/matrix.cjs +1042 -0
- package/dist/cjs/priority-queue.cjs +1314 -0
- package/dist/cjs/queue.cjs +4090 -0
- package/dist/cjs/stack.cjs +861 -0
- package/dist/cjs/trie.cjs +1173 -0
- package/dist/cjs-legacy/binary-tree.cjs +23730 -0
- package/dist/cjs-legacy/graph.cjs +5234 -0
- package/dist/cjs-legacy/hash.cjs +1262 -0
- package/dist/cjs-legacy/heap.cjs +1537 -0
- package/dist/cjs-legacy/index.cjs +32555 -10936
- package/dist/cjs-legacy/linked-list.cjs +4376 -0
- package/dist/cjs-legacy/matrix.cjs +1045 -0
- package/dist/cjs-legacy/priority-queue.cjs +1312 -0
- package/dist/cjs-legacy/queue.cjs +4088 -0
- package/dist/cjs-legacy/stack.cjs +861 -0
- package/dist/cjs-legacy/trie.cjs +1172 -0
- package/dist/esm/binary-tree.mjs +23683 -0
- package/dist/esm/graph.mjs +5223 -0
- package/dist/esm/hash.mjs +1259 -0
- package/dist/esm/heap.mjs +1534 -0
- package/dist/esm/index.mjs +24507 -2898
- package/dist/esm/linked-list.mjs +4363 -0
- package/dist/esm/matrix.mjs +1038 -0
- package/dist/esm/priority-queue.mjs +1310 -0
- package/dist/esm/queue.mjs +4086 -0
- package/dist/esm/stack.mjs +859 -0
- package/dist/esm/trie.mjs +1170 -0
- package/dist/esm-legacy/binary-tree.mjs +23715 -0
- package/dist/esm-legacy/graph.mjs +5221 -0
- package/dist/esm-legacy/hash.mjs +1259 -0
- package/dist/esm-legacy/heap.mjs +1531 -0
- package/dist/esm-legacy/index.mjs +32553 -10935
- package/dist/esm-legacy/linked-list.mjs +4369 -0
- package/dist/esm-legacy/matrix.mjs +1041 -0
- package/dist/esm-legacy/priority-queue.mjs +1308 -0
- package/dist/esm-legacy/queue.mjs +4084 -0
- package/dist/esm-legacy/stack.mjs +859 -0
- package/dist/esm-legacy/trie.mjs +1169 -0
- package/dist/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
- package/dist/types/data-structures/base/linear-base.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +368 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +473 -147
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +931 -80
- package/dist/types/data-structures/binary-tree/bst.d.ts +792 -29
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +592 -32
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +320 -135
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +3662 -6
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3487 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2778 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +3414 -6
- package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
- package/dist/types/data-structures/graph/directed-graph.d.ts +419 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +384 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +462 -89
- package/dist/types/data-structures/heap/heap.d.ts +567 -99
- package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +631 -49
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +581 -68
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +775 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +491 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
- package/dist/types/data-structures/queue/deque.d.ts +578 -71
- package/dist/types/data-structures/queue/queue.d.ts +451 -42
- package/dist/types/data-structures/stack/stack.d.ts +374 -32
- package/dist/types/data-structures/trie/trie.d.ts +458 -48
- package/dist/types/interfaces/graph.d.ts +1 -1
- package/dist/types/types/common.d.ts +2 -2
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
- package/dist/types/types/utils/validate-type.d.ts +4 -4
- package/dist/umd/data-structure-typed.js +32432 -10808
- package/dist/umd/data-structure-typed.min.js +10 -4
- package/docs-site-docusaurus/README.md +41 -0
- package/docs-site-docusaurus/docs/api/README.md +52 -0
- package/docs-site-docusaurus/docs/api/classes/AVLTree.md +6130 -0
- package/docs-site-docusaurus/docs/api/classes/AVLTreeNode.md +282 -0
- package/docs-site-docusaurus/docs/api/classes/AbstractGraph.md +2266 -0
- package/docs-site-docusaurus/docs/api/classes/BST.md +5831 -0
- package/docs-site-docusaurus/docs/api/classes/BSTNode.md +333 -0
- package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +455 -0
- package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +4647 -0
- package/docs-site-docusaurus/docs/api/classes/BinaryTreeNode.md +331 -0
- package/docs-site-docusaurus/docs/api/classes/Deque.md +2767 -0
- package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +2999 -0
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +2685 -0
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedListNode.md +221 -0
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +253 -0
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +21 -0
- package/docs-site-docusaurus/docs/api/classes/HashMap.md +1333 -0
- package/docs-site-docusaurus/docs/api/classes/Heap.md +1881 -0
- package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +800 -0
- package/docs-site-docusaurus/docs/api/classes/IterableEntryBase.md +644 -0
- package/docs-site-docusaurus/docs/api/classes/LinearBase.md +1632 -0
- package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +1853 -0
- package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +1108 -0
- package/docs-site-docusaurus/docs/api/classes/LinkedListNode.md +156 -0
- package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +2824 -0
- package/docs-site-docusaurus/docs/api/classes/MapGraph.md +2929 -0
- package/docs-site-docusaurus/docs/api/classes/Matrix.md +1026 -0
- package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +1866 -0
- package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +1883 -0
- package/docs-site-docusaurus/docs/api/classes/MinHeap.md +1879 -0
- package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +1882 -0
- package/docs-site-docusaurus/docs/api/classes/Navigator.md +109 -0
- package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +1839 -0
- package/docs-site-docusaurus/docs/api/classes/Queue.md +2244 -0
- package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +6374 -0
- package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +372 -0
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +2897 -0
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedListNode.md +169 -0
- package/docs-site-docusaurus/docs/api/classes/SkipList.md +1229 -0
- package/docs-site-docusaurus/docs/api/classes/Stack.md +1573 -0
- package/docs-site-docusaurus/docs/api/classes/TreeMap.md +1257 -0
- package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +1475 -0
- package/docs-site-docusaurus/docs/api/classes/TreeSet.md +1117 -0
- package/docs-site-docusaurus/docs/api/classes/Trie.md +1708 -0
- package/docs-site-docusaurus/docs/api/classes/TrieNode.md +199 -0
- package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +2979 -0
- package/docs-site-docusaurus/docs/guide/_category_.json +6 -0
- package/docs-site-docusaurus/docs/guide/architecture.md +613 -0
- package/docs-site-docusaurus/docs/guide/concepts.md +420 -0
- package/docs-site-docusaurus/docs/guide/guides.md +611 -0
- package/docs-site-docusaurus/docs/guide/installation.md +60 -0
- package/docs-site-docusaurus/docs/guide/integrations.md +823 -0
- package/docs-site-docusaurus/docs/guide/overview.md +638 -0
- package/docs-site-docusaurus/docs/guide/performance.md +833 -0
- package/docs-site-docusaurus/docs/guide/quick-start.md +73 -0
- package/docs-site-docusaurus/docusaurus.config.ts +159 -0
- package/docs-site-docusaurus/fix-mdx-generics.mjs +75 -0
- package/docs-site-docusaurus/package-lock.json +18667 -0
- package/docs-site-docusaurus/package.json +50 -0
- package/docs-site-docusaurus/prefix-class-to-methods.mjs +48 -0
- package/docs-site-docusaurus/sidebars.ts +23 -0
- package/docs-site-docusaurus/sort-protected.mjs +87 -0
- package/docs-site-docusaurus/src/css/custom.css +96 -0
- package/docs-site-docusaurus/src/pages/index.module.css +13 -0
- package/docs-site-docusaurus/src/pages/index.tsx +71 -0
- package/docs-site-docusaurus/src/pages/markdown-page.md +7 -0
- package/docs-site-docusaurus/src/theme/TOCItems/index.tsx +34 -0
- package/docs-site-docusaurus/static/.nojekyll +0 -0
- package/docs-site-docusaurus/static/img/docusaurus-social-card.jpg +0 -0
- package/docs-site-docusaurus/static/img/docusaurus.png +0 -0
- package/docs-site-docusaurus/static/img/favicon.ico +0 -0
- package/docs-site-docusaurus/static/img/favicon.png +0 -0
- package/docs-site-docusaurus/static/img/logo-180.png +0 -0
- package/docs-site-docusaurus/static/img/logo.jpg +0 -0
- package/docs-site-docusaurus/static/img/logo.png +0 -0
- package/docs-site-docusaurus/static/img/logo.svg +1 -0
- package/docs-site-docusaurus/static/img/og-image.png +0 -0
- package/docs-site-docusaurus/static/img/undraw_docusaurus_mountain.svg +171 -0
- package/docs-site-docusaurus/static/img/undraw_docusaurus_react.svg +170 -0
- package/docs-site-docusaurus/static/img/undraw_docusaurus_tree.svg +40 -0
- package/docs-site-docusaurus/static/robots.txt +4 -0
- package/docs-site-docusaurus/typedoc.json +23 -0
- package/package.json +109 -12
- package/src/data-structures/base/index.ts +1 -0
- package/src/data-structures/base/iterable-element-base.ts +4 -5
- package/src/data-structures/base/iterable-entry-base.ts +8 -8
- package/src/data-structures/base/linear-base.ts +3 -3
- package/src/data-structures/binary-tree/avl-tree.ts +386 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +596 -247
- package/src/data-structures/binary-tree/binary-tree.ts +956 -81
- package/src/data-structures/binary-tree/bst.ts +840 -35
- package/src/data-structures/binary-tree/red-black-tree.ts +689 -97
- package/src/data-structures/binary-tree/segment-tree.ts +498 -249
- package/src/data-structures/binary-tree/tree-map.ts +3784 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +3614 -211
- package/src/data-structures/binary-tree/tree-multi-set.ts +2874 -65
- package/src/data-structures/binary-tree/tree-set.ts +3531 -10
- package/src/data-structures/graph/abstract-graph.ts +4 -4
- package/src/data-structures/graph/directed-graph.ts +429 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +393 -59
- package/src/data-structures/hash/hash-map.ts +476 -92
- package/src/data-structures/heap/heap.ts +581 -99
- package/src/data-structures/heap/max-heap.ts +46 -0
- package/src/data-structures/heap/min-heap.ts +59 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +646 -47
- package/src/data-structures/linked-list/singly-linked-list.ts +596 -68
- package/src/data-structures/linked-list/skip-linked-list.ts +1067 -90
- package/src/data-structures/matrix/matrix.ts +584 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
- package/src/data-structures/priority-queue/priority-queue.ts +60 -0
- package/src/data-structures/queue/deque.ts +592 -70
- package/src/data-structures/queue/queue.ts +463 -42
- package/src/data-structures/stack/stack.ts +384 -32
- package/src/data-structures/trie/trie.ts +470 -48
- package/src/interfaces/graph.ts +1 -1
- package/src/types/common.ts +2 -2
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/heap/heap.ts +1 -0
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
- package/src/types/utils/validate-type.ts +4 -4
- package/vercel.json +6 -0
- package/dist/leetcode/avl-tree-counter.mjs +0 -2957
- package/dist/leetcode/avl-tree-multi-map.mjs +0 -2889
- package/dist/leetcode/avl-tree.mjs +0 -2720
- package/dist/leetcode/binary-tree.mjs +0 -1594
- package/dist/leetcode/bst.mjs +0 -2398
- package/dist/leetcode/deque.mjs +0 -683
- package/dist/leetcode/directed-graph.mjs +0 -1733
- package/dist/leetcode/doubly-linked-list.mjs +0 -709
- package/dist/leetcode/hash-map.mjs +0 -493
- package/dist/leetcode/heap.mjs +0 -542
- package/dist/leetcode/max-heap.mjs +0 -375
- package/dist/leetcode/max-priority-queue.mjs +0 -383
- package/dist/leetcode/min-heap.mjs +0 -363
- package/dist/leetcode/min-priority-queue.mjs +0 -371
- package/dist/leetcode/priority-queue.mjs +0 -363
- package/dist/leetcode/queue.mjs +0 -943
- package/dist/leetcode/red-black-tree.mjs +0 -2765
- package/dist/leetcode/singly-linked-list.mjs +0 -754
- package/dist/leetcode/stack.mjs +0 -217
- package/dist/leetcode/tree-counter.mjs +0 -3039
- package/dist/leetcode/tree-multi-map.mjs +0 -2913
- package/dist/leetcode/trie.mjs +0 -413
- package/dist/leetcode/undirected-graph.mjs +0 -1650
|
@@ -0,0 +1,1573 @@
|
|
|
1
|
+
[**data-structure-typed**](../README.md)
|
|
2
|
+
|
|
3
|
+
***
|
|
4
|
+
|
|
5
|
+
[data-structure-typed](../README.md) / Stack
|
|
6
|
+
|
|
7
|
+
# Class: Stack\<E, R\>
|
|
8
|
+
|
|
9
|
+
Defined in: [data-structures/stack/stack.ts:135](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/stack/stack.ts#L135)
|
|
10
|
+
|
|
11
|
+
LIFO stack with array storage and optional record→element conversion.
|
|
12
|
+
|
|
13
|
+
## Remarks
|
|
14
|
+
|
|
15
|
+
Time O(1), Space O(1)
|
|
16
|
+
|
|
17
|
+
## Examples
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
// Function Call Stack
|
|
21
|
+
const functionStack = new Stack<string>();
|
|
22
|
+
functionStack.push('main');
|
|
23
|
+
functionStack.push('foo');
|
|
24
|
+
functionStack.push('bar');
|
|
25
|
+
console.log(functionStack.pop()); // 'bar';
|
|
26
|
+
console.log(functionStack.pop()); // 'foo';
|
|
27
|
+
console.log(functionStack.pop()); // 'main';
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
// Balanced Parentheses or Brackets
|
|
32
|
+
type ValidCharacters = ')' | '(' | ']' | '[' | '}' | '{';
|
|
33
|
+
|
|
34
|
+
const stack = new Stack<string>();
|
|
35
|
+
const input: ValidCharacters[] = '[({})]'.split('') as ValidCharacters[];
|
|
36
|
+
const matches: { [key in ValidCharacters]?: ValidCharacters } = { ')': '(', ']': '[', '}': '{' };
|
|
37
|
+
for (const char of input) {
|
|
38
|
+
if ('([{'.includes(char)) {
|
|
39
|
+
stack.push(char);
|
|
40
|
+
} else if (')]}'.includes(char)) {
|
|
41
|
+
if (stack.pop() !== matches[char]) {
|
|
42
|
+
fail('Parentheses are not balanced');
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
console.log(stack.isEmpty()); // true;
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
// Expression Evaluation and Conversion
|
|
51
|
+
const stack = new Stack<number>();
|
|
52
|
+
const expression = [5, 3, '+']; // Equivalent to 5 + 3
|
|
53
|
+
expression.forEach(token => {
|
|
54
|
+
if (typeof token === 'number') {
|
|
55
|
+
stack.push(token);
|
|
56
|
+
} else {
|
|
57
|
+
const b = stack.pop()!;
|
|
58
|
+
const a = stack.pop()!;
|
|
59
|
+
stack.push(token === '+' ? a + b : 0); // Only handling '+' here
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
console.log(stack.pop()); // 8;
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
// Backtracking Algorithms
|
|
67
|
+
const stack = new Stack<[number, number]>();
|
|
68
|
+
const maze = [
|
|
69
|
+
['S', ' ', 'X'],
|
|
70
|
+
['X', ' ', 'X'],
|
|
71
|
+
[' ', ' ', 'E']
|
|
72
|
+
];
|
|
73
|
+
const start: [number, number] = [0, 0];
|
|
74
|
+
const end = [2, 2];
|
|
75
|
+
const directions = [
|
|
76
|
+
[0, 1], // To the right
|
|
77
|
+
[1, 0], // down
|
|
78
|
+
[0, -1], // left
|
|
79
|
+
[-1, 0] // up
|
|
80
|
+
];
|
|
81
|
+
|
|
82
|
+
const visited = new Set<string>(); // Used to record visited nodes
|
|
83
|
+
stack.push(start);
|
|
84
|
+
const path: number[][] = [];
|
|
85
|
+
|
|
86
|
+
while (!stack.isEmpty()) {
|
|
87
|
+
const [x, y] = stack.pop()!;
|
|
88
|
+
if (visited.has(`${x},${y}`)) continue; // Skip already visited nodes
|
|
89
|
+
visited.add(`${x},${y}`);
|
|
90
|
+
|
|
91
|
+
path.push([x, y]);
|
|
92
|
+
|
|
93
|
+
if (x === end[0] && y === end[1]) {
|
|
94
|
+
break; // Find the end point and exit
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
for (const [dx, dy] of directions) {
|
|
98
|
+
const nx = x + dx;
|
|
99
|
+
const ny = y + dy;
|
|
100
|
+
if (
|
|
101
|
+
maze[nx]?.[ny] === ' ' || // feasible path
|
|
102
|
+
maze[nx]?.[ny] === 'E' // destination
|
|
103
|
+
) {
|
|
104
|
+
stack.push([nx, ny]);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
console.log(path); // contains end;
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
// Stock Span Problem
|
|
114
|
+
const stack = new Stack<number>();
|
|
115
|
+
const prices = [100, 80, 60, 70, 60, 75, 85];
|
|
116
|
+
const spans: number[] = [];
|
|
117
|
+
prices.forEach((price, i) => {
|
|
118
|
+
while (!stack.isEmpty() && prices[stack.peek()!] <= price) {
|
|
119
|
+
stack.pop();
|
|
120
|
+
}
|
|
121
|
+
spans.push(stack.isEmpty() ? i + 1 : i - stack.peek()!);
|
|
122
|
+
stack.push(i);
|
|
123
|
+
});
|
|
124
|
+
console.log(spans); // [1, 1, 1, 2, 1, 4, 6];
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
```ts
|
|
128
|
+
// Simplify File Paths
|
|
129
|
+
const stack = new Stack<string>();
|
|
130
|
+
const path = '/a/./b/../../c';
|
|
131
|
+
path.split('/').forEach(segment => {
|
|
132
|
+
if (segment === '..') stack.pop();
|
|
133
|
+
else if (segment && segment !== '.') stack.push(segment);
|
|
134
|
+
});
|
|
135
|
+
console.log(stack.elements.join('/')); // 'c';
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
// Convert stack to array
|
|
140
|
+
const stack = new Stack<number>([1, 2, 3]);
|
|
141
|
+
console.log(stack.toArray()); // [1, 2, 3];
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
## Extends
|
|
145
|
+
|
|
146
|
+
- [`IterableElementBase`](IterableElementBase.md)\<`E`, `R`\>
|
|
147
|
+
|
|
148
|
+
## Type Parameters
|
|
149
|
+
|
|
150
|
+
### E
|
|
151
|
+
|
|
152
|
+
`E` = `any`
|
|
153
|
+
|
|
154
|
+
### R
|
|
155
|
+
|
|
156
|
+
`R` = `any`
|
|
157
|
+
|
|
158
|
+
1. Last In, First Out (LIFO): The core characteristic of a stack is its last in, first out nature, meaning the last element added to the stack will be the first to be removed.
|
|
159
|
+
2. Uses: Stacks are commonly used for managing a series of tasks or elements that need to be processed in a last in, first out manner. They are widely used in various scenarios, such as in function calls in programming languages, evaluation of arithmetic expressions, and backtracking algorithms.
|
|
160
|
+
3. Performance: Stack operations are typically O(1) in time complexity, meaning that regardless of the stack's size, adding, removing, and viewing the top element are very fast operations.
|
|
161
|
+
4. Function Calls: In most modern programming languages, the records of function calls are managed through a stack. When a function is called, its record (including parameters, local variables, and return address) is 'pushed' into the stack. When the function returns, its record is 'popped' from the stack.
|
|
162
|
+
5. Expression Evaluation: Used for the evaluation of arithmetic or logical expressions, especially when dealing with parenthesis matching and operator precedence.
|
|
163
|
+
6. Backtracking Algorithms: In problems where multiple branches need to be explored but only one branch can be explored at a time, stacks can be used to save the state at each branching point.
|
|
164
|
+
|
|
165
|
+
## Constructors
|
|
166
|
+
|
|
167
|
+
### Constructor
|
|
168
|
+
|
|
169
|
+
```ts
|
|
170
|
+
new Stack<E, R>(elements?, options?): Stack<E, R>;
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
Defined in: [data-structures/stack/stack.ts:146](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/stack/stack.ts#L146)
|
|
174
|
+
|
|
175
|
+
Create a Stack and optionally bulk-push elements.
|
|
176
|
+
|
|
177
|
+
#### Parameters
|
|
178
|
+
|
|
179
|
+
##### elements?
|
|
180
|
+
|
|
181
|
+
`Iterable`\<`E`, `any`, `any`\> \| `Iterable`\<`R`, `any`, `any`\>
|
|
182
|
+
|
|
183
|
+
Iterable of elements (or raw records if toElementFn is set).
|
|
184
|
+
|
|
185
|
+
##### options?
|
|
186
|
+
|
|
187
|
+
`StackOptions`\<`E`, `R`\>
|
|
188
|
+
|
|
189
|
+
Options such as toElementFn and equality function.
|
|
190
|
+
|
|
191
|
+
#### Returns
|
|
192
|
+
|
|
193
|
+
`Stack`\<`E`, `R`\>
|
|
194
|
+
|
|
195
|
+
New Stack instance.
|
|
196
|
+
|
|
197
|
+
#### Remarks
|
|
198
|
+
|
|
199
|
+
Time O(N), Space O(N)
|
|
200
|
+
|
|
201
|
+
#### Overrides
|
|
202
|
+
|
|
203
|
+
[`IterableElementBase`](IterableElementBase.md).[`constructor`](IterableElementBase.md#constructor)
|
|
204
|
+
|
|
205
|
+
## Properties
|
|
206
|
+
|
|
207
|
+
### elements
|
|
208
|
+
|
|
209
|
+
#### Get Signature
|
|
210
|
+
|
|
211
|
+
```ts
|
|
212
|
+
get elements(): E[];
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
Defined in: [data-structures/stack/stack.ts:159](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/stack/stack.ts#L159)
|
|
216
|
+
|
|
217
|
+
Get the backing array of elements.
|
|
218
|
+
|
|
219
|
+
##### Remarks
|
|
220
|
+
|
|
221
|
+
Time O(1), Space O(1)
|
|
222
|
+
|
|
223
|
+
##### Returns
|
|
224
|
+
|
|
225
|
+
`E`[]
|
|
226
|
+
|
|
227
|
+
Internal elements array.
|
|
228
|
+
|
|
229
|
+
***
|
|
230
|
+
|
|
231
|
+
### size
|
|
232
|
+
|
|
233
|
+
#### Get Signature
|
|
234
|
+
|
|
235
|
+
```ts
|
|
236
|
+
get size(): number;
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
Defined in: [data-structures/stack/stack.ts:199](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/stack/stack.ts#L199)
|
|
240
|
+
|
|
241
|
+
Get the number of stored elements.
|
|
242
|
+
|
|
243
|
+
##### Remarks
|
|
244
|
+
|
|
245
|
+
Time O(1), Space O(1)
|
|
246
|
+
|
|
247
|
+
##### Example
|
|
248
|
+
|
|
249
|
+
```ts
|
|
250
|
+
// Get number of elements
|
|
251
|
+
const stack = new Stack<number>([1, 2, 3]);
|
|
252
|
+
console.log(stack.size); // 3;
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
##### Returns
|
|
256
|
+
|
|
257
|
+
`number`
|
|
258
|
+
|
|
259
|
+
Current size.
|
|
260
|
+
|
|
261
|
+
*
|
|
262
|
+
|
|
263
|
+
***
|
|
264
|
+
|
|
265
|
+
### toElementFn
|
|
266
|
+
|
|
267
|
+
#### Get Signature
|
|
268
|
+
|
|
269
|
+
```ts
|
|
270
|
+
get toElementFn(): ((rawElement) => E) | undefined;
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
Defined in: [data-structures/base/iterable-element-base.ts:47](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/base/iterable-element-base.ts#L47)
|
|
274
|
+
|
|
275
|
+
Exposes the current `toElementFn`, if configured.
|
|
276
|
+
|
|
277
|
+
##### Remarks
|
|
278
|
+
|
|
279
|
+
Time O(1), Space O(1).
|
|
280
|
+
|
|
281
|
+
##### Returns
|
|
282
|
+
|
|
283
|
+
((`rawElement`) => `E`) \| `undefined`
|
|
284
|
+
|
|
285
|
+
The converter function or `undefined` when not set.
|
|
286
|
+
|
|
287
|
+
#### Inherited from
|
|
288
|
+
|
|
289
|
+
[`IterableElementBase`](IterableElementBase.md).[`toElementFn`](IterableElementBase.md#toelementfn)
|
|
290
|
+
|
|
291
|
+
## Methods
|
|
292
|
+
|
|
293
|
+
### \[iterator\]()
|
|
294
|
+
|
|
295
|
+
```ts
|
|
296
|
+
iterator: IterableIterator<E>;
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
Defined in: [data-structures/base/iterable-element-base.ts:60](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/base/iterable-element-base.ts#L60)
|
|
300
|
+
|
|
301
|
+
Returns an iterator over the structure's elements.
|
|
302
|
+
|
|
303
|
+
#### Parameters
|
|
304
|
+
|
|
305
|
+
##### args
|
|
306
|
+
|
|
307
|
+
...`unknown`[]
|
|
308
|
+
|
|
309
|
+
Optional iterator arguments forwarded to the internal iterator.
|
|
310
|
+
|
|
311
|
+
#### Returns
|
|
312
|
+
|
|
313
|
+
`IterableIterator`\<`E`\>
|
|
314
|
+
|
|
315
|
+
An `IterableIterator<E>` that yields the elements in traversal order.
|
|
316
|
+
|
|
317
|
+
#### Remarks
|
|
318
|
+
|
|
319
|
+
Producing the iterator is O(1); consuming the entire iterator is Time O(n) with O(1) extra space.
|
|
320
|
+
|
|
321
|
+
#### Inherited from
|
|
322
|
+
|
|
323
|
+
[`IterableElementBase`](IterableElementBase.md).[`[iterator]`](IterableElementBase.md#iterator)
|
|
324
|
+
|
|
325
|
+
***
|
|
326
|
+
|
|
327
|
+
### clear()
|
|
328
|
+
|
|
329
|
+
```ts
|
|
330
|
+
clear(): void;
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
Defined in: [data-structures/stack/stack.ts:544](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/stack/stack.ts#L544)
|
|
334
|
+
|
|
335
|
+
Remove all elements and reset storage.
|
|
336
|
+
|
|
337
|
+
#### Returns
|
|
338
|
+
|
|
339
|
+
`void`
|
|
340
|
+
|
|
341
|
+
void
|
|
342
|
+
|
|
343
|
+
*
|
|
344
|
+
|
|
345
|
+
#### Remarks
|
|
346
|
+
|
|
347
|
+
Time O(1), Space O(1)
|
|
348
|
+
|
|
349
|
+
#### Example
|
|
350
|
+
|
|
351
|
+
```ts
|
|
352
|
+
// Remove all elements
|
|
353
|
+
const stack = new Stack<number>([1, 2, 3]);
|
|
354
|
+
stack.clear();
|
|
355
|
+
console.log(stack.isEmpty()); // true;
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
#### Overrides
|
|
359
|
+
|
|
360
|
+
[`IterableElementBase`](IterableElementBase.md).[`clear`](IterableElementBase.md#clear)
|
|
361
|
+
|
|
362
|
+
***
|
|
363
|
+
|
|
364
|
+
### clone()
|
|
365
|
+
|
|
366
|
+
```ts
|
|
367
|
+
clone(): this;
|
|
368
|
+
```
|
|
369
|
+
|
|
370
|
+
Defined in: [data-structures/stack/stack.ts:587](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/stack/stack.ts#L587)
|
|
371
|
+
|
|
372
|
+
Deep clone this stack.
|
|
373
|
+
|
|
374
|
+
#### Returns
|
|
375
|
+
|
|
376
|
+
`this`
|
|
377
|
+
|
|
378
|
+
A new stack with the same content.
|
|
379
|
+
|
|
380
|
+
*
|
|
381
|
+
|
|
382
|
+
#### Remarks
|
|
383
|
+
|
|
384
|
+
Time O(N), Space O(N)
|
|
385
|
+
|
|
386
|
+
#### Example
|
|
387
|
+
|
|
388
|
+
```ts
|
|
389
|
+
// Create independent copy
|
|
390
|
+
const stack = new Stack<number>([1, 2, 3]);
|
|
391
|
+
const copy = stack.clone();
|
|
392
|
+
copy.pop();
|
|
393
|
+
console.log(stack.size); // 3;
|
|
394
|
+
console.log(copy.size); // 2;
|
|
395
|
+
```
|
|
396
|
+
|
|
397
|
+
#### Overrides
|
|
398
|
+
|
|
399
|
+
[`IterableElementBase`](IterableElementBase.md).[`clone`](IterableElementBase.md#clone)
|
|
400
|
+
|
|
401
|
+
***
|
|
402
|
+
|
|
403
|
+
### delete()
|
|
404
|
+
|
|
405
|
+
```ts
|
|
406
|
+
delete(element): boolean;
|
|
407
|
+
```
|
|
408
|
+
|
|
409
|
+
Defined in: [data-structures/stack/stack.ts:472](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/stack/stack.ts#L472)
|
|
410
|
+
|
|
411
|
+
Delete the first occurrence of a specific element.
|
|
412
|
+
|
|
413
|
+
#### Parameters
|
|
414
|
+
|
|
415
|
+
##### element
|
|
416
|
+
|
|
417
|
+
`E`
|
|
418
|
+
|
|
419
|
+
Element to remove (using the configured equality).
|
|
420
|
+
|
|
421
|
+
#### Returns
|
|
422
|
+
|
|
423
|
+
`boolean`
|
|
424
|
+
|
|
425
|
+
True if an element was removed.
|
|
426
|
+
|
|
427
|
+
*
|
|
428
|
+
|
|
429
|
+
#### Remarks
|
|
430
|
+
|
|
431
|
+
Time O(N), Space O(1)
|
|
432
|
+
|
|
433
|
+
#### Example
|
|
434
|
+
|
|
435
|
+
```ts
|
|
436
|
+
// Remove element
|
|
437
|
+
const stack = new Stack<number>([1, 2, 3]);
|
|
438
|
+
stack.delete(2);
|
|
439
|
+
console.log(stack.toArray()); // [1, 3];
|
|
440
|
+
```
|
|
441
|
+
|
|
442
|
+
***
|
|
443
|
+
|
|
444
|
+
### deleteAt()
|
|
445
|
+
|
|
446
|
+
```ts
|
|
447
|
+
deleteAt(index): boolean;
|
|
448
|
+
```
|
|
449
|
+
|
|
450
|
+
Defined in: [data-structures/stack/stack.ts:484](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/stack/stack.ts#L484)
|
|
451
|
+
|
|
452
|
+
Delete the element at an index.
|
|
453
|
+
|
|
454
|
+
#### Parameters
|
|
455
|
+
|
|
456
|
+
##### index
|
|
457
|
+
|
|
458
|
+
`number`
|
|
459
|
+
|
|
460
|
+
Zero-based index from the bottom.
|
|
461
|
+
|
|
462
|
+
#### Returns
|
|
463
|
+
|
|
464
|
+
`boolean`
|
|
465
|
+
|
|
466
|
+
True if removed.
|
|
467
|
+
|
|
468
|
+
#### Remarks
|
|
469
|
+
|
|
470
|
+
Time O(N), Space O(1)
|
|
471
|
+
|
|
472
|
+
***
|
|
473
|
+
|
|
474
|
+
### deleteWhere()
|
|
475
|
+
|
|
476
|
+
```ts
|
|
477
|
+
deleteWhere(predicate): boolean;
|
|
478
|
+
```
|
|
479
|
+
|
|
480
|
+
Defined in: [data-structures/stack/stack.ts:497](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/stack/stack.ts#L497)
|
|
481
|
+
|
|
482
|
+
Delete the first element that satisfies a predicate.
|
|
483
|
+
|
|
484
|
+
#### Parameters
|
|
485
|
+
|
|
486
|
+
##### predicate
|
|
487
|
+
|
|
488
|
+
(`value`, `index`, `stack`) => `boolean`
|
|
489
|
+
|
|
490
|
+
Function (value, index, stack) → boolean to decide deletion.
|
|
491
|
+
|
|
492
|
+
#### Returns
|
|
493
|
+
|
|
494
|
+
`boolean`
|
|
495
|
+
|
|
496
|
+
True if a match was removed.
|
|
497
|
+
|
|
498
|
+
#### Remarks
|
|
499
|
+
|
|
500
|
+
Time O(N), Space O(1)
|
|
501
|
+
|
|
502
|
+
***
|
|
503
|
+
|
|
504
|
+
### every()
|
|
505
|
+
|
|
506
|
+
```ts
|
|
507
|
+
every(predicate, thisArg?): boolean;
|
|
508
|
+
```
|
|
509
|
+
|
|
510
|
+
Defined in: [data-structures/base/iterable-element-base.ts:86](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/base/iterable-element-base.ts#L86)
|
|
511
|
+
|
|
512
|
+
Tests whether all elements satisfy the predicate.
|
|
513
|
+
|
|
514
|
+
#### Parameters
|
|
515
|
+
|
|
516
|
+
##### predicate
|
|
517
|
+
|
|
518
|
+
`ElementCallback`\<`E`, `R`, `boolean`\>
|
|
519
|
+
|
|
520
|
+
Function invoked for each element with signature `(value, index, self)`.
|
|
521
|
+
|
|
522
|
+
##### thisArg?
|
|
523
|
+
|
|
524
|
+
`unknown`
|
|
525
|
+
|
|
526
|
+
Optional `this` binding for the predicate.
|
|
527
|
+
|
|
528
|
+
#### Returns
|
|
529
|
+
|
|
530
|
+
`boolean`
|
|
531
|
+
|
|
532
|
+
`true` if every element passes; otherwise `false`.
|
|
533
|
+
|
|
534
|
+
#### Remarks
|
|
535
|
+
|
|
536
|
+
Time O(n) in the worst case; may exit early when the first failure is found. Space O(1).
|
|
537
|
+
|
|
538
|
+
#### Inherited from
|
|
539
|
+
|
|
540
|
+
[`IterableElementBase`](IterableElementBase.md).[`every`](IterableElementBase.md#every)
|
|
541
|
+
|
|
542
|
+
***
|
|
543
|
+
|
|
544
|
+
### filter()
|
|
545
|
+
|
|
546
|
+
```ts
|
|
547
|
+
filter(predicate, thisArg?): this;
|
|
548
|
+
```
|
|
549
|
+
|
|
550
|
+
Defined in: [data-structures/stack/stack.ts:632](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/stack/stack.ts#L632)
|
|
551
|
+
|
|
552
|
+
Filter elements into a new stack of the same class.
|
|
553
|
+
|
|
554
|
+
#### Parameters
|
|
555
|
+
|
|
556
|
+
##### predicate
|
|
557
|
+
|
|
558
|
+
`ElementCallback`\<`E`, `R`, `boolean`\>
|
|
559
|
+
|
|
560
|
+
Predicate (value, index, stack) → boolean to keep value.
|
|
561
|
+
|
|
562
|
+
##### thisArg?
|
|
563
|
+
|
|
564
|
+
`unknown`
|
|
565
|
+
|
|
566
|
+
Value for `this` inside the predicate.
|
|
567
|
+
|
|
568
|
+
#### Returns
|
|
569
|
+
|
|
570
|
+
`this`
|
|
571
|
+
|
|
572
|
+
A new stack with kept values.
|
|
573
|
+
|
|
574
|
+
*
|
|
575
|
+
|
|
576
|
+
#### Remarks
|
|
577
|
+
|
|
578
|
+
Time O(N), Space O(N)
|
|
579
|
+
|
|
580
|
+
#### Example
|
|
581
|
+
|
|
582
|
+
```ts
|
|
583
|
+
// Filter elements
|
|
584
|
+
const stack = new Stack<number>([1, 2, 3, 4, 5]);
|
|
585
|
+
const evens = stack.filter(x => x % 2 === 0);
|
|
586
|
+
console.log(evens.toArray()); // [2, 4];
|
|
587
|
+
```
|
|
588
|
+
|
|
589
|
+
#### Overrides
|
|
590
|
+
|
|
591
|
+
[`IterableElementBase`](IterableElementBase.md).[`filter`](IterableElementBase.md#filter)
|
|
592
|
+
|
|
593
|
+
***
|
|
594
|
+
|
|
595
|
+
### find()
|
|
596
|
+
|
|
597
|
+
#### Call Signature
|
|
598
|
+
|
|
599
|
+
```ts
|
|
600
|
+
find<S>(predicate, thisArg?): S | undefined;
|
|
601
|
+
```
|
|
602
|
+
|
|
603
|
+
Defined in: [data-structures/base/iterable-element-base.ts:162](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/base/iterable-element-base.ts#L162)
|
|
604
|
+
|
|
605
|
+
Finds the first element that satisfies the predicate and returns it.
|
|
606
|
+
|
|
607
|
+
Finds the first element of type `S` (a subtype of `E`) that satisfies the predicate and returns it.
|
|
608
|
+
|
|
609
|
+
##### Type Parameters
|
|
610
|
+
|
|
611
|
+
###### S
|
|
612
|
+
|
|
613
|
+
`S`
|
|
614
|
+
|
|
615
|
+
##### Parameters
|
|
616
|
+
|
|
617
|
+
###### predicate
|
|
618
|
+
|
|
619
|
+
`ElementCallback`\<`E`, `R`, `S`\>
|
|
620
|
+
|
|
621
|
+
Type-guard predicate: `(value, index, self) => value is S`.
|
|
622
|
+
|
|
623
|
+
###### thisArg?
|
|
624
|
+
|
|
625
|
+
`unknown`
|
|
626
|
+
|
|
627
|
+
Optional `this` binding for the predicate.
|
|
628
|
+
|
|
629
|
+
##### Returns
|
|
630
|
+
|
|
631
|
+
`S` \| `undefined`
|
|
632
|
+
|
|
633
|
+
The matched element typed as `S`, or `undefined` if not found.
|
|
634
|
+
|
|
635
|
+
##### Remarks
|
|
636
|
+
|
|
637
|
+
Time O(n) in the worst case; may exit early on the first match. Space O(1).
|
|
638
|
+
|
|
639
|
+
##### Inherited from
|
|
640
|
+
|
|
641
|
+
[`IterableElementBase`](IterableElementBase.md).[`find`](IterableElementBase.md#find)
|
|
642
|
+
|
|
643
|
+
#### Call Signature
|
|
644
|
+
|
|
645
|
+
```ts
|
|
646
|
+
find(predicate, thisArg?): E | undefined;
|
|
647
|
+
```
|
|
648
|
+
|
|
649
|
+
Defined in: [data-structures/base/iterable-element-base.ts:163](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/base/iterable-element-base.ts#L163)
|
|
650
|
+
|
|
651
|
+
Finds the first element that satisfies the predicate and returns it.
|
|
652
|
+
|
|
653
|
+
Finds the first element of type `S` (a subtype of `E`) that satisfies the predicate and returns it.
|
|
654
|
+
|
|
655
|
+
##### Parameters
|
|
656
|
+
|
|
657
|
+
###### predicate
|
|
658
|
+
|
|
659
|
+
`ElementCallback`\<`E`, `R`, `unknown`\>
|
|
660
|
+
|
|
661
|
+
Type-guard predicate: `(value, index, self) => value is S`.
|
|
662
|
+
|
|
663
|
+
###### thisArg?
|
|
664
|
+
|
|
665
|
+
`unknown`
|
|
666
|
+
|
|
667
|
+
Optional `this` binding for the predicate.
|
|
668
|
+
|
|
669
|
+
##### Returns
|
|
670
|
+
|
|
671
|
+
`E` \| `undefined`
|
|
672
|
+
|
|
673
|
+
The matched element typed as `S`, or `undefined` if not found.
|
|
674
|
+
|
|
675
|
+
##### Remarks
|
|
676
|
+
|
|
677
|
+
Time O(n) in the worst case; may exit early on the first match. Space O(1).
|
|
678
|
+
|
|
679
|
+
##### Inherited from
|
|
680
|
+
|
|
681
|
+
[`IterableElementBase`](IterableElementBase.md).[`find`](IterableElementBase.md#find)
|
|
682
|
+
|
|
683
|
+
***
|
|
684
|
+
|
|
685
|
+
### forEach()
|
|
686
|
+
|
|
687
|
+
```ts
|
|
688
|
+
forEach(callbackfn, thisArg?): void;
|
|
689
|
+
```
|
|
690
|
+
|
|
691
|
+
Defined in: [data-structures/base/iterable-element-base.ts:132](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/base/iterable-element-base.ts#L132)
|
|
692
|
+
|
|
693
|
+
Invokes a callback for each element in iteration order.
|
|
694
|
+
|
|
695
|
+
#### Parameters
|
|
696
|
+
|
|
697
|
+
##### callbackfn
|
|
698
|
+
|
|
699
|
+
`ElementCallback`\<`E`, `R`, `void`\>
|
|
700
|
+
|
|
701
|
+
Function invoked per element with signature `(value, index, self)`.
|
|
702
|
+
|
|
703
|
+
##### thisArg?
|
|
704
|
+
|
|
705
|
+
`unknown`
|
|
706
|
+
|
|
707
|
+
Optional `this` binding for the callback.
|
|
708
|
+
|
|
709
|
+
#### Returns
|
|
710
|
+
|
|
711
|
+
`void`
|
|
712
|
+
|
|
713
|
+
`void`.
|
|
714
|
+
|
|
715
|
+
#### Remarks
|
|
716
|
+
|
|
717
|
+
Time O(n), Space O(1).
|
|
718
|
+
|
|
719
|
+
#### Inherited from
|
|
720
|
+
|
|
721
|
+
[`IterableElementBase`](IterableElementBase.md).[`forEach`](IterableElementBase.md#foreach)
|
|
722
|
+
|
|
723
|
+
***
|
|
724
|
+
|
|
725
|
+
### has()
|
|
726
|
+
|
|
727
|
+
```ts
|
|
728
|
+
has(element): boolean;
|
|
729
|
+
```
|
|
730
|
+
|
|
731
|
+
Defined in: [data-structures/base/iterable-element-base.ts:188](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/base/iterable-element-base.ts#L188)
|
|
732
|
+
|
|
733
|
+
Checks whether a strictly-equal element exists in the structure.
|
|
734
|
+
|
|
735
|
+
#### Parameters
|
|
736
|
+
|
|
737
|
+
##### element
|
|
738
|
+
|
|
739
|
+
`E`
|
|
740
|
+
|
|
741
|
+
The element to test with `===` equality.
|
|
742
|
+
|
|
743
|
+
#### Returns
|
|
744
|
+
|
|
745
|
+
`boolean`
|
|
746
|
+
|
|
747
|
+
`true` if an equal element is found; otherwise `false`.
|
|
748
|
+
|
|
749
|
+
#### Remarks
|
|
750
|
+
|
|
751
|
+
Time O(n) in the worst case. Space O(1).
|
|
752
|
+
|
|
753
|
+
#### Inherited from
|
|
754
|
+
|
|
755
|
+
[`IterableElementBase`](IterableElementBase.md).[`has`](IterableElementBase.md#has)
|
|
756
|
+
|
|
757
|
+
***
|
|
758
|
+
|
|
759
|
+
### isEmpty()
|
|
760
|
+
|
|
761
|
+
```ts
|
|
762
|
+
isEmpty(): boolean;
|
|
763
|
+
```
|
|
764
|
+
|
|
765
|
+
Defined in: [data-structures/stack/stack.ts:262](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/stack/stack.ts#L262)
|
|
766
|
+
|
|
767
|
+
Check whether the stack is empty.
|
|
768
|
+
|
|
769
|
+
#### Returns
|
|
770
|
+
|
|
771
|
+
`boolean`
|
|
772
|
+
|
|
773
|
+
True if size is 0.
|
|
774
|
+
|
|
775
|
+
*
|
|
776
|
+
|
|
777
|
+
#### Remarks
|
|
778
|
+
|
|
779
|
+
Time O(1), Space O(1)
|
|
780
|
+
|
|
781
|
+
#### Example
|
|
782
|
+
|
|
783
|
+
```ts
|
|
784
|
+
// Check if stack has elements
|
|
785
|
+
const stack = new Stack<number>();
|
|
786
|
+
console.log(stack.isEmpty()); // true;
|
|
787
|
+
stack.push(1);
|
|
788
|
+
console.log(stack.isEmpty()); // false;
|
|
789
|
+
```
|
|
790
|
+
|
|
791
|
+
#### Overrides
|
|
792
|
+
|
|
793
|
+
[`IterableElementBase`](IterableElementBase.md).[`isEmpty`](IterableElementBase.md#isempty)
|
|
794
|
+
|
|
795
|
+
***
|
|
796
|
+
|
|
797
|
+
### map()
|
|
798
|
+
|
|
799
|
+
```ts
|
|
800
|
+
map<EM, RM>(
|
|
801
|
+
callback,
|
|
802
|
+
options?,
|
|
803
|
+
thisArg?): Stack<EM, RM>;
|
|
804
|
+
```
|
|
805
|
+
|
|
806
|
+
Defined in: [data-structures/stack/stack.ts:701](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/stack/stack.ts#L701)
|
|
807
|
+
|
|
808
|
+
Map values into a new stack (possibly different element type).
|
|
809
|
+
|
|
810
|
+
#### Type Parameters
|
|
811
|
+
|
|
812
|
+
##### EM
|
|
813
|
+
|
|
814
|
+
`EM`
|
|
815
|
+
|
|
816
|
+
##### RM
|
|
817
|
+
|
|
818
|
+
`RM`
|
|
819
|
+
|
|
820
|
+
#### Parameters
|
|
821
|
+
|
|
822
|
+
##### callback
|
|
823
|
+
|
|
824
|
+
`ElementCallback`\<`E`, `R`, `EM`\>
|
|
825
|
+
|
|
826
|
+
Mapping function (value, index, stack) → newElement.
|
|
827
|
+
|
|
828
|
+
##### options?
|
|
829
|
+
|
|
830
|
+
`IterableElementBaseOptions`\<`EM`, `RM`\>
|
|
831
|
+
|
|
832
|
+
Options for the output stack (e.g., toElementFn).
|
|
833
|
+
|
|
834
|
+
##### thisArg?
|
|
835
|
+
|
|
836
|
+
`unknown`
|
|
837
|
+
|
|
838
|
+
Value for `this` inside the callback.
|
|
839
|
+
|
|
840
|
+
#### Returns
|
|
841
|
+
|
|
842
|
+
`Stack`\<`EM`, `RM`\>
|
|
843
|
+
|
|
844
|
+
A new Stack with mapped elements.
|
|
845
|
+
|
|
846
|
+
*
|
|
847
|
+
|
|
848
|
+
#### Remarks
|
|
849
|
+
|
|
850
|
+
Time O(N), Space O(N)
|
|
851
|
+
|
|
852
|
+
#### Example
|
|
853
|
+
|
|
854
|
+
```ts
|
|
855
|
+
// Transform elements
|
|
856
|
+
const stack = new Stack<number>([1, 2, 3]);
|
|
857
|
+
const doubled = stack.map(x => x * 2);
|
|
858
|
+
console.log(doubled.toArray()); // [2, 4, 6];
|
|
859
|
+
```
|
|
860
|
+
|
|
861
|
+
#### Overrides
|
|
862
|
+
|
|
863
|
+
[`IterableElementBase`](IterableElementBase.md).[`map`](IterableElementBase.md#map)
|
|
864
|
+
|
|
865
|
+
***
|
|
866
|
+
|
|
867
|
+
### mapSame()
|
|
868
|
+
|
|
869
|
+
```ts
|
|
870
|
+
mapSame(callback, thisArg?): this;
|
|
871
|
+
```
|
|
872
|
+
|
|
873
|
+
Defined in: [data-structures/stack/stack.ts:650](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/stack/stack.ts#L650)
|
|
874
|
+
|
|
875
|
+
Map values into a new stack of the same element type.
|
|
876
|
+
|
|
877
|
+
#### Parameters
|
|
878
|
+
|
|
879
|
+
##### callback
|
|
880
|
+
|
|
881
|
+
`ElementCallback`\<`E`, `R`, `E`\>
|
|
882
|
+
|
|
883
|
+
Mapping function (value, index, stack) → newValue.
|
|
884
|
+
|
|
885
|
+
##### thisArg?
|
|
886
|
+
|
|
887
|
+
`unknown`
|
|
888
|
+
|
|
889
|
+
Value for `this` inside the callback.
|
|
890
|
+
|
|
891
|
+
#### Returns
|
|
892
|
+
|
|
893
|
+
`this`
|
|
894
|
+
|
|
895
|
+
A new stack with mapped values.
|
|
896
|
+
|
|
897
|
+
#### Remarks
|
|
898
|
+
|
|
899
|
+
Time O(N), Space O(N)
|
|
900
|
+
|
|
901
|
+
#### Overrides
|
|
902
|
+
|
|
903
|
+
[`IterableElementBase`](IterableElementBase.md).[`mapSame`](IterableElementBase.md#mapsame)
|
|
904
|
+
|
|
905
|
+
***
|
|
906
|
+
|
|
907
|
+
### peek()
|
|
908
|
+
|
|
909
|
+
```ts
|
|
910
|
+
peek(): E | undefined;
|
|
911
|
+
```
|
|
912
|
+
|
|
913
|
+
Defined in: [data-structures/stack/stack.ts:305](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/stack/stack.ts#L305)
|
|
914
|
+
|
|
915
|
+
Get the top element without removing it.
|
|
916
|
+
|
|
917
|
+
#### Returns
|
|
918
|
+
|
|
919
|
+
`E` \| `undefined`
|
|
920
|
+
|
|
921
|
+
Top element or undefined.
|
|
922
|
+
|
|
923
|
+
*
|
|
924
|
+
|
|
925
|
+
#### Remarks
|
|
926
|
+
|
|
927
|
+
Time O(1), Space O(1)
|
|
928
|
+
|
|
929
|
+
#### Example
|
|
930
|
+
|
|
931
|
+
```ts
|
|
932
|
+
// View the top element without removing it
|
|
933
|
+
const stack = new Stack<string>(['a', 'b', 'c']);
|
|
934
|
+
console.log(stack.peek()); // 'c';
|
|
935
|
+
console.log(stack.size); // 3;
|
|
936
|
+
```
|
|
937
|
+
|
|
938
|
+
***
|
|
939
|
+
|
|
940
|
+
### pop()
|
|
941
|
+
|
|
942
|
+
```ts
|
|
943
|
+
pop(): E | undefined;
|
|
944
|
+
```
|
|
945
|
+
|
|
946
|
+
Defined in: [data-structures/stack/stack.ts:415](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/stack/stack.ts#L415)
|
|
947
|
+
|
|
948
|
+
Pop and return the top element.
|
|
949
|
+
|
|
950
|
+
#### Returns
|
|
951
|
+
|
|
952
|
+
`E` \| `undefined`
|
|
953
|
+
|
|
954
|
+
Removed element or undefined.
|
|
955
|
+
|
|
956
|
+
*
|
|
957
|
+
|
|
958
|
+
#### Remarks
|
|
959
|
+
|
|
960
|
+
Time O(1), Space O(1)
|
|
961
|
+
|
|
962
|
+
#### Example
|
|
963
|
+
|
|
964
|
+
```ts
|
|
965
|
+
// Stack pop operation (LIFO - Last In First Out)
|
|
966
|
+
const stack = new Stack<number>([10, 20, 30, 40, 50]);
|
|
967
|
+
|
|
968
|
+
// Peek at the top element without removing
|
|
969
|
+
const top = stack.peek();
|
|
970
|
+
console.log(top); // 50;
|
|
971
|
+
|
|
972
|
+
// Pop removes from the top (LIFO order)
|
|
973
|
+
const popped = stack.pop();
|
|
974
|
+
console.log(popped); // 50;
|
|
975
|
+
|
|
976
|
+
// Next pop gets the previous element
|
|
977
|
+
const next = stack.pop();
|
|
978
|
+
console.log(next); // 40;
|
|
979
|
+
|
|
980
|
+
// Verify length decreased
|
|
981
|
+
console.log(stack.size); // 3;
|
|
982
|
+
```
|
|
983
|
+
|
|
984
|
+
***
|
|
985
|
+
|
|
986
|
+
### print()
|
|
987
|
+
|
|
988
|
+
```ts
|
|
989
|
+
print(): void;
|
|
990
|
+
```
|
|
991
|
+
|
|
992
|
+
Defined in: [data-structures/base/iterable-element-base.ts:268](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/base/iterable-element-base.ts#L268)
|
|
993
|
+
|
|
994
|
+
Prints `toVisual()` to the console. Intended for quick debugging.
|
|
995
|
+
|
|
996
|
+
#### Returns
|
|
997
|
+
|
|
998
|
+
`void`
|
|
999
|
+
|
|
1000
|
+
`void`.
|
|
1001
|
+
|
|
1002
|
+
#### Remarks
|
|
1003
|
+
|
|
1004
|
+
Time O(n) due to materialization, Space O(n) for the intermediate representation.
|
|
1005
|
+
|
|
1006
|
+
#### Inherited from
|
|
1007
|
+
|
|
1008
|
+
[`IterableElementBase`](IterableElementBase.md).[`print`](IterableElementBase.md#print)
|
|
1009
|
+
|
|
1010
|
+
***
|
|
1011
|
+
|
|
1012
|
+
### push()
|
|
1013
|
+
|
|
1014
|
+
```ts
|
|
1015
|
+
push(element): boolean;
|
|
1016
|
+
```
|
|
1017
|
+
|
|
1018
|
+
Defined in: [data-structures/stack/stack.ts:358](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/stack/stack.ts#L358)
|
|
1019
|
+
|
|
1020
|
+
Push one element onto the top.
|
|
1021
|
+
|
|
1022
|
+
#### Parameters
|
|
1023
|
+
|
|
1024
|
+
##### element
|
|
1025
|
+
|
|
1026
|
+
`E`
|
|
1027
|
+
|
|
1028
|
+
Element to push.
|
|
1029
|
+
|
|
1030
|
+
#### Returns
|
|
1031
|
+
|
|
1032
|
+
`boolean`
|
|
1033
|
+
|
|
1034
|
+
True when pushed.
|
|
1035
|
+
|
|
1036
|
+
*
|
|
1037
|
+
|
|
1038
|
+
#### Remarks
|
|
1039
|
+
|
|
1040
|
+
Time O(1), Space O(1)
|
|
1041
|
+
|
|
1042
|
+
#### Example
|
|
1043
|
+
|
|
1044
|
+
```ts
|
|
1045
|
+
// basic Stack creation and push operation
|
|
1046
|
+
// Create a simple Stack with initial values
|
|
1047
|
+
const stack = new Stack([1, 2, 3, 4, 5]);
|
|
1048
|
+
|
|
1049
|
+
// Verify the stack maintains insertion order (LIFO will be shown in pop)
|
|
1050
|
+
console.log([...stack]); // [1, 2, 3, 4, 5];
|
|
1051
|
+
|
|
1052
|
+
// Check length
|
|
1053
|
+
console.log(stack.size); // 5;
|
|
1054
|
+
|
|
1055
|
+
// Push a new element to the top
|
|
1056
|
+
stack.push(6);
|
|
1057
|
+
console.log(stack.size); // 6;
|
|
1058
|
+
```
|
|
1059
|
+
|
|
1060
|
+
***
|
|
1061
|
+
|
|
1062
|
+
### pushMany()
|
|
1063
|
+
|
|
1064
|
+
```ts
|
|
1065
|
+
pushMany(elements): boolean[];
|
|
1066
|
+
```
|
|
1067
|
+
|
|
1068
|
+
Defined in: [data-structures/stack/stack.ts:426](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/stack/stack.ts#L426)
|
|
1069
|
+
|
|
1070
|
+
Push many elements from an iterable.
|
|
1071
|
+
|
|
1072
|
+
#### Parameters
|
|
1073
|
+
|
|
1074
|
+
##### elements
|
|
1075
|
+
|
|
1076
|
+
`Iterable`\<`E`, `any`, `any`\> \| `Iterable`\<`R`, `any`, `any`\>
|
|
1077
|
+
|
|
1078
|
+
Iterable of elements (or raw records if toElementFn is set).
|
|
1079
|
+
|
|
1080
|
+
#### Returns
|
|
1081
|
+
|
|
1082
|
+
`boolean`[]
|
|
1083
|
+
|
|
1084
|
+
Array of per-element success flags.
|
|
1085
|
+
|
|
1086
|
+
#### Remarks
|
|
1087
|
+
|
|
1088
|
+
Time O(N), Space O(1)
|
|
1089
|
+
|
|
1090
|
+
***
|
|
1091
|
+
|
|
1092
|
+
### reduce()
|
|
1093
|
+
|
|
1094
|
+
Reduces all elements to a single accumulated value.
|
|
1095
|
+
|
|
1096
|
+
#### Param
|
|
1097
|
+
|
|
1098
|
+
Reducer of signature `(acc, value, index, self) => nextAcc`. The first element is used as the initial accumulator.
|
|
1099
|
+
|
|
1100
|
+
#### Param
|
|
1101
|
+
|
|
1102
|
+
Reducer of signature `(acc, value, index, self) => nextAcc`.
|
|
1103
|
+
|
|
1104
|
+
#### Param
|
|
1105
|
+
|
|
1106
|
+
The initial accumulator value of type `E`.
|
|
1107
|
+
|
|
1108
|
+
#### Template
|
|
1109
|
+
|
|
1110
|
+
The accumulator type when it differs from `E`.
|
|
1111
|
+
|
|
1112
|
+
#### Param
|
|
1113
|
+
|
|
1114
|
+
Reducer of signature `(acc: U, value, index, self) => U`.
|
|
1115
|
+
|
|
1116
|
+
#### Param
|
|
1117
|
+
|
|
1118
|
+
The initial accumulator value of type `U`.
|
|
1119
|
+
|
|
1120
|
+
#### Remarks
|
|
1121
|
+
|
|
1122
|
+
Time O(n), Space O(1). Throws if called on an empty structure without `initialValue`.
|
|
1123
|
+
|
|
1124
|
+
#### Call Signature
|
|
1125
|
+
|
|
1126
|
+
```ts
|
|
1127
|
+
reduce(callbackfn): E;
|
|
1128
|
+
```
|
|
1129
|
+
|
|
1130
|
+
Defined in: [data-structures/base/iterable-element-base.ts:193](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/base/iterable-element-base.ts#L193)
|
|
1131
|
+
|
|
1132
|
+
##### Parameters
|
|
1133
|
+
|
|
1134
|
+
###### callbackfn
|
|
1135
|
+
|
|
1136
|
+
`ReduceElementCallback`\<`E`, `R`\>
|
|
1137
|
+
|
|
1138
|
+
##### Returns
|
|
1139
|
+
|
|
1140
|
+
`E`
|
|
1141
|
+
|
|
1142
|
+
##### Inherited from
|
|
1143
|
+
|
|
1144
|
+
[`IterableElementBase`](IterableElementBase.md).[`reduce`](IterableElementBase.md#reduce)
|
|
1145
|
+
|
|
1146
|
+
#### Call Signature
|
|
1147
|
+
|
|
1148
|
+
```ts
|
|
1149
|
+
reduce(callbackfn, initialValue): E;
|
|
1150
|
+
```
|
|
1151
|
+
|
|
1152
|
+
Defined in: [data-structures/base/iterable-element-base.ts:194](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/base/iterable-element-base.ts#L194)
|
|
1153
|
+
|
|
1154
|
+
##### Parameters
|
|
1155
|
+
|
|
1156
|
+
###### callbackfn
|
|
1157
|
+
|
|
1158
|
+
`ReduceElementCallback`\<`E`, `R`\>
|
|
1159
|
+
|
|
1160
|
+
###### initialValue
|
|
1161
|
+
|
|
1162
|
+
`E`
|
|
1163
|
+
|
|
1164
|
+
##### Returns
|
|
1165
|
+
|
|
1166
|
+
`E`
|
|
1167
|
+
|
|
1168
|
+
##### Inherited from
|
|
1169
|
+
|
|
1170
|
+
[`IterableElementBase`](IterableElementBase.md).[`reduce`](IterableElementBase.md#reduce)
|
|
1171
|
+
|
|
1172
|
+
#### Call Signature
|
|
1173
|
+
|
|
1174
|
+
```ts
|
|
1175
|
+
reduce<U>(callbackfn, initialValue): U;
|
|
1176
|
+
```
|
|
1177
|
+
|
|
1178
|
+
Defined in: [data-structures/base/iterable-element-base.ts:195](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/base/iterable-element-base.ts#L195)
|
|
1179
|
+
|
|
1180
|
+
##### Type Parameters
|
|
1181
|
+
|
|
1182
|
+
###### U
|
|
1183
|
+
|
|
1184
|
+
`U`
|
|
1185
|
+
|
|
1186
|
+
##### Parameters
|
|
1187
|
+
|
|
1188
|
+
###### callbackfn
|
|
1189
|
+
|
|
1190
|
+
`ReduceElementCallback`\<`E`, `R`, `U`\>
|
|
1191
|
+
|
|
1192
|
+
###### initialValue
|
|
1193
|
+
|
|
1194
|
+
`U`
|
|
1195
|
+
|
|
1196
|
+
##### Returns
|
|
1197
|
+
|
|
1198
|
+
`U`
|
|
1199
|
+
|
|
1200
|
+
##### Inherited from
|
|
1201
|
+
|
|
1202
|
+
[`IterableElementBase`](IterableElementBase.md).[`reduce`](IterableElementBase.md#reduce)
|
|
1203
|
+
|
|
1204
|
+
***
|
|
1205
|
+
|
|
1206
|
+
### setEquality()
|
|
1207
|
+
|
|
1208
|
+
```ts
|
|
1209
|
+
setEquality(equals): this;
|
|
1210
|
+
```
|
|
1211
|
+
|
|
1212
|
+
Defined in: [data-structures/stack/stack.ts:722](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/stack/stack.ts#L722)
|
|
1213
|
+
|
|
1214
|
+
Set the equality comparator used by delete/search operations.
|
|
1215
|
+
|
|
1216
|
+
#### Parameters
|
|
1217
|
+
|
|
1218
|
+
##### equals
|
|
1219
|
+
|
|
1220
|
+
(`a`, `b`) => `boolean`
|
|
1221
|
+
|
|
1222
|
+
Equality predicate (a, b) → boolean.
|
|
1223
|
+
|
|
1224
|
+
#### Returns
|
|
1225
|
+
|
|
1226
|
+
`this`
|
|
1227
|
+
|
|
1228
|
+
This stack.
|
|
1229
|
+
|
|
1230
|
+
#### Remarks
|
|
1231
|
+
|
|
1232
|
+
Time O(1), Space O(1)
|
|
1233
|
+
|
|
1234
|
+
***
|
|
1235
|
+
|
|
1236
|
+
### some()
|
|
1237
|
+
|
|
1238
|
+
```ts
|
|
1239
|
+
some(predicate, thisArg?): boolean;
|
|
1240
|
+
```
|
|
1241
|
+
|
|
1242
|
+
Defined in: [data-structures/base/iterable-element-base.ts:109](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/base/iterable-element-base.ts#L109)
|
|
1243
|
+
|
|
1244
|
+
Tests whether at least one element satisfies the predicate.
|
|
1245
|
+
|
|
1246
|
+
#### Parameters
|
|
1247
|
+
|
|
1248
|
+
##### predicate
|
|
1249
|
+
|
|
1250
|
+
`ElementCallback`\<`E`, `R`, `boolean`\>
|
|
1251
|
+
|
|
1252
|
+
Function invoked for each element with signature `(value, index, self)`.
|
|
1253
|
+
|
|
1254
|
+
##### thisArg?
|
|
1255
|
+
|
|
1256
|
+
`unknown`
|
|
1257
|
+
|
|
1258
|
+
Optional `this` binding for the predicate.
|
|
1259
|
+
|
|
1260
|
+
#### Returns
|
|
1261
|
+
|
|
1262
|
+
`boolean`
|
|
1263
|
+
|
|
1264
|
+
`true` if any element passes; otherwise `false`.
|
|
1265
|
+
|
|
1266
|
+
#### Remarks
|
|
1267
|
+
|
|
1268
|
+
Time O(n) in the worst case; may exit early on first success. Space O(1).
|
|
1269
|
+
|
|
1270
|
+
#### Inherited from
|
|
1271
|
+
|
|
1272
|
+
[`IterableElementBase`](IterableElementBase.md).[`some`](IterableElementBase.md#some)
|
|
1273
|
+
|
|
1274
|
+
***
|
|
1275
|
+
|
|
1276
|
+
### toArray()
|
|
1277
|
+
|
|
1278
|
+
```ts
|
|
1279
|
+
toArray(): E[];
|
|
1280
|
+
```
|
|
1281
|
+
|
|
1282
|
+
Defined in: [data-structures/base/iterable-element-base.ts:245](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/base/iterable-element-base.ts#L245)
|
|
1283
|
+
|
|
1284
|
+
Materializes the elements into a new array.
|
|
1285
|
+
|
|
1286
|
+
#### Returns
|
|
1287
|
+
|
|
1288
|
+
`E`[]
|
|
1289
|
+
|
|
1290
|
+
A shallow array copy of the iteration order.
|
|
1291
|
+
|
|
1292
|
+
#### Remarks
|
|
1293
|
+
|
|
1294
|
+
Time O(n), Space O(n).
|
|
1295
|
+
|
|
1296
|
+
#### Inherited from
|
|
1297
|
+
|
|
1298
|
+
[`IterableElementBase`](IterableElementBase.md).[`toArray`](IterableElementBase.md#toarray)
|
|
1299
|
+
|
|
1300
|
+
***
|
|
1301
|
+
|
|
1302
|
+
### toVisual()
|
|
1303
|
+
|
|
1304
|
+
```ts
|
|
1305
|
+
toVisual(): E[];
|
|
1306
|
+
```
|
|
1307
|
+
|
|
1308
|
+
Defined in: [data-structures/base/iterable-element-base.ts:257](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/base/iterable-element-base.ts#L257)
|
|
1309
|
+
|
|
1310
|
+
Returns a representation of the structure suitable for quick visualization.
|
|
1311
|
+
Defaults to an array of elements; subclasses may override to provide richer visuals.
|
|
1312
|
+
|
|
1313
|
+
#### Returns
|
|
1314
|
+
|
|
1315
|
+
`E`[]
|
|
1316
|
+
|
|
1317
|
+
A visual representation (array by default).
|
|
1318
|
+
|
|
1319
|
+
#### Remarks
|
|
1320
|
+
|
|
1321
|
+
Time O(n), Space O(n).
|
|
1322
|
+
|
|
1323
|
+
#### Inherited from
|
|
1324
|
+
|
|
1325
|
+
[`IterableElementBase`](IterableElementBase.md).[`toVisual`](IterableElementBase.md#tovisual)
|
|
1326
|
+
|
|
1327
|
+
***
|
|
1328
|
+
|
|
1329
|
+
### values()
|
|
1330
|
+
|
|
1331
|
+
```ts
|
|
1332
|
+
values(): IterableIterator<E>;
|
|
1333
|
+
```
|
|
1334
|
+
|
|
1335
|
+
Defined in: [data-structures/base/iterable-element-base.ts:71](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/base/iterable-element-base.ts#L71)
|
|
1336
|
+
|
|
1337
|
+
Returns an iterator over the values (alias of the default iterator).
|
|
1338
|
+
|
|
1339
|
+
#### Returns
|
|
1340
|
+
|
|
1341
|
+
`IterableIterator`\<`E`\>
|
|
1342
|
+
|
|
1343
|
+
An `IterableIterator<E>` over all elements.
|
|
1344
|
+
|
|
1345
|
+
#### Remarks
|
|
1346
|
+
|
|
1347
|
+
Creating the iterator is O(1); full iteration is Time O(n), Space O(1).
|
|
1348
|
+
|
|
1349
|
+
#### Inherited from
|
|
1350
|
+
|
|
1351
|
+
[`IterableElementBase`](IterableElementBase.md).[`values`](IterableElementBase.md#values)
|
|
1352
|
+
|
|
1353
|
+
***
|
|
1354
|
+
|
|
1355
|
+
### fromArray()
|
|
1356
|
+
|
|
1357
|
+
```ts
|
|
1358
|
+
static fromArray<E, R>(
|
|
1359
|
+
this,
|
|
1360
|
+
elements,
|
|
1361
|
+
options?): any;
|
|
1362
|
+
```
|
|
1363
|
+
|
|
1364
|
+
Defined in: [data-structures/stack/stack.ts:214](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/stack/stack.ts#L214)
|
|
1365
|
+
|
|
1366
|
+
Create a stack from an array of elements.
|
|
1367
|
+
|
|
1368
|
+
#### Type Parameters
|
|
1369
|
+
|
|
1370
|
+
##### E
|
|
1371
|
+
|
|
1372
|
+
`E`
|
|
1373
|
+
|
|
1374
|
+
##### R
|
|
1375
|
+
|
|
1376
|
+
`R` = `any`
|
|
1377
|
+
|
|
1378
|
+
#### Parameters
|
|
1379
|
+
|
|
1380
|
+
##### this
|
|
1381
|
+
|
|
1382
|
+
`Object`
|
|
1383
|
+
|
|
1384
|
+
The constructor (subclass) to instantiate.
|
|
1385
|
+
|
|
1386
|
+
##### elements
|
|
1387
|
+
|
|
1388
|
+
`E`[]
|
|
1389
|
+
|
|
1390
|
+
Array of elements to push in order.
|
|
1391
|
+
|
|
1392
|
+
##### options?
|
|
1393
|
+
|
|
1394
|
+
`StackOptions`\<`E`, `R`\>
|
|
1395
|
+
|
|
1396
|
+
Options forwarded to the constructor.
|
|
1397
|
+
|
|
1398
|
+
#### Returns
|
|
1399
|
+
|
|
1400
|
+
`any`
|
|
1401
|
+
|
|
1402
|
+
A new Stack populated from the array.
|
|
1403
|
+
|
|
1404
|
+
#### Remarks
|
|
1405
|
+
|
|
1406
|
+
Time O(N), Space O(N)
|
|
1407
|
+
|
|
1408
|
+
|
|
1409
|
+
---
|
|
1410
|
+
|
|
1411
|
+
## Protected Members
|
|
1412
|
+
|
|
1413
|
+
### \_toElementFn?
|
|
1414
|
+
|
|
1415
|
+
```ts
|
|
1416
|
+
protected optional _toElementFn?: (rawElement) => E;
|
|
1417
|
+
```
|
|
1418
|
+
|
|
1419
|
+
Defined in: [data-structures/base/iterable-element-base.ts:38](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/base/iterable-element-base.ts#L38)
|
|
1420
|
+
|
|
1421
|
+
The converter used to transform a raw element (`R`) into a public element (`E`).
|
|
1422
|
+
|
|
1423
|
+
#### Parameters
|
|
1424
|
+
|
|
1425
|
+
##### rawElement
|
|
1426
|
+
|
|
1427
|
+
`R`
|
|
1428
|
+
|
|
1429
|
+
#### Returns
|
|
1430
|
+
|
|
1431
|
+
`E`
|
|
1432
|
+
|
|
1433
|
+
#### Remarks
|
|
1434
|
+
|
|
1435
|
+
Time O(1), Space O(1).
|
|
1436
|
+
|
|
1437
|
+
#### Inherited from
|
|
1438
|
+
|
|
1439
|
+
[`IterableElementBase`](IterableElementBase.md).[`_toElementFn`](IterableElementBase.md#_toelementfn)
|
|
1440
|
+
|
|
1441
|
+
## Accessors
|
|
1442
|
+
|
|
1443
|
+
### \_createInstance()
|
|
1444
|
+
|
|
1445
|
+
```ts
|
|
1446
|
+
protected _createInstance(options?): this;
|
|
1447
|
+
```
|
|
1448
|
+
|
|
1449
|
+
Defined in: [data-structures/stack/stack.ts:746](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/stack/stack.ts#L746)
|
|
1450
|
+
|
|
1451
|
+
(Protected) Create an empty instance of the same concrete class.
|
|
1452
|
+
|
|
1453
|
+
#### Parameters
|
|
1454
|
+
|
|
1455
|
+
##### options?
|
|
1456
|
+
|
|
1457
|
+
`StackOptions`\<`E`, `R`\>
|
|
1458
|
+
|
|
1459
|
+
Options forwarded to the constructor.
|
|
1460
|
+
|
|
1461
|
+
#### Returns
|
|
1462
|
+
|
|
1463
|
+
`this`
|
|
1464
|
+
|
|
1465
|
+
An empty like-kind stack instance.
|
|
1466
|
+
|
|
1467
|
+
#### Remarks
|
|
1468
|
+
|
|
1469
|
+
Time O(1), Space O(1)
|
|
1470
|
+
|
|
1471
|
+
***
|
|
1472
|
+
|
|
1473
|
+
### \_createLike()
|
|
1474
|
+
|
|
1475
|
+
```ts
|
|
1476
|
+
protected _createLike<T, RR>(elements?, options?): Stack<T, RR>;
|
|
1477
|
+
```
|
|
1478
|
+
|
|
1479
|
+
Defined in: [data-structures/stack/stack.ts:761](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/stack/stack.ts#L761)
|
|
1480
|
+
|
|
1481
|
+
(Protected) Create a like-kind stack and seed it from an iterable.
|
|
1482
|
+
|
|
1483
|
+
#### Type Parameters
|
|
1484
|
+
|
|
1485
|
+
##### T
|
|
1486
|
+
|
|
1487
|
+
`T` = `E`
|
|
1488
|
+
|
|
1489
|
+
##### RR
|
|
1490
|
+
|
|
1491
|
+
`RR` = `R`
|
|
1492
|
+
|
|
1493
|
+
#### Parameters
|
|
1494
|
+
|
|
1495
|
+
##### elements?
|
|
1496
|
+
|
|
1497
|
+
`Iterable`\<`T`, `any`, `any`\> \| `Iterable`\<`RR`, `any`, `any`\>
|
|
1498
|
+
|
|
1499
|
+
Iterable used to seed the new stack.
|
|
1500
|
+
|
|
1501
|
+
##### options?
|
|
1502
|
+
|
|
1503
|
+
`StackOptions`\<`T`, `RR`\>
|
|
1504
|
+
|
|
1505
|
+
Options forwarded to the constructor.
|
|
1506
|
+
|
|
1507
|
+
#### Returns
|
|
1508
|
+
|
|
1509
|
+
`Stack`\<`T`, `RR`\>
|
|
1510
|
+
|
|
1511
|
+
A like-kind Stack instance.
|
|
1512
|
+
|
|
1513
|
+
#### Remarks
|
|
1514
|
+
|
|
1515
|
+
Time O(N), Space O(N)
|
|
1516
|
+
|
|
1517
|
+
***
|
|
1518
|
+
|
|
1519
|
+
### \_getIterator()
|
|
1520
|
+
|
|
1521
|
+
```ts
|
|
1522
|
+
protected _getIterator(): IterableIterator<E>;
|
|
1523
|
+
```
|
|
1524
|
+
|
|
1525
|
+
Defined in: [data-structures/stack/stack.ts:778](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/stack/stack.ts#L778)
|
|
1526
|
+
|
|
1527
|
+
(Protected) Iterate elements from bottom to top.
|
|
1528
|
+
|
|
1529
|
+
#### Returns
|
|
1530
|
+
|
|
1531
|
+
`IterableIterator`\<`E`\>
|
|
1532
|
+
|
|
1533
|
+
Iterator of elements.
|
|
1534
|
+
|
|
1535
|
+
#### Remarks
|
|
1536
|
+
|
|
1537
|
+
Time O(N), Space O(1)
|
|
1538
|
+
|
|
1539
|
+
#### Overrides
|
|
1540
|
+
|
|
1541
|
+
[`IterableElementBase`](IterableElementBase.md).[`_getIterator`](IterableElementBase.md#_getiterator)
|
|
1542
|
+
|
|
1543
|
+
***
|
|
1544
|
+
|
|
1545
|
+
### \_indexOfByEquals()
|
|
1546
|
+
|
|
1547
|
+
```ts
|
|
1548
|
+
protected _indexOfByEquals(target): number;
|
|
1549
|
+
```
|
|
1550
|
+
|
|
1551
|
+
Defined in: [data-structures/stack/stack.ts:734](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/stack/stack.ts#L734)
|
|
1552
|
+
|
|
1553
|
+
(Protected) Find the index of a target element using the equality function.
|
|
1554
|
+
|
|
1555
|
+
#### Parameters
|
|
1556
|
+
|
|
1557
|
+
##### target
|
|
1558
|
+
|
|
1559
|
+
`E`
|
|
1560
|
+
|
|
1561
|
+
Element to search for.
|
|
1562
|
+
|
|
1563
|
+
#### Returns
|
|
1564
|
+
|
|
1565
|
+
`number`
|
|
1566
|
+
|
|
1567
|
+
Index or -1 if not found.
|
|
1568
|
+
|
|
1569
|
+
#### Remarks
|
|
1570
|
+
|
|
1571
|
+
Time O(N), Space O(1)
|
|
1572
|
+
|
|
1573
|
+
***
|