data-structure-typed 2.5.0 → 2.5.2
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 +5 -1
- package/README.md +124 -29
- package/dist/cjs/binary-tree.cjs +26282 -0
- package/dist/cjs/graph.cjs +5422 -0
- package/dist/cjs/hash.cjs +1310 -0
- package/dist/cjs/heap.cjs +1602 -0
- package/dist/cjs/index.cjs +31257 -14673
- package/dist/cjs/linked-list.cjs +4576 -0
- package/dist/cjs/matrix.cjs +1080 -0
- package/dist/cjs/priority-queue.cjs +1376 -0
- package/dist/cjs/queue.cjs +4264 -0
- package/dist/cjs/stack.cjs +907 -0
- package/dist/cjs/trie.cjs +1223 -0
- package/dist/cjs-legacy/binary-tree.cjs +26319 -0
- package/dist/cjs-legacy/graph.cjs +5420 -0
- package/dist/cjs-legacy/hash.cjs +1310 -0
- package/dist/cjs-legacy/heap.cjs +1599 -0
- package/dist/cjs-legacy/index.cjs +31268 -14679
- package/dist/cjs-legacy/linked-list.cjs +4582 -0
- package/dist/cjs-legacy/matrix.cjs +1083 -0
- package/dist/cjs-legacy/priority-queue.cjs +1374 -0
- package/dist/cjs-legacy/queue.cjs +4262 -0
- package/dist/cjs-legacy/stack.cjs +907 -0
- package/dist/cjs-legacy/trie.cjs +1222 -0
- package/dist/esm/binary-tree.mjs +26267 -0
- package/dist/esm/graph.mjs +5409 -0
- package/dist/esm/hash.mjs +1307 -0
- package/dist/esm/heap.mjs +1596 -0
- package/dist/esm/index.mjs +31254 -14674
- package/dist/esm/linked-list.mjs +4569 -0
- package/dist/esm/matrix.mjs +1076 -0
- package/dist/esm/priority-queue.mjs +1372 -0
- package/dist/esm/queue.mjs +4260 -0
- package/dist/esm/stack.mjs +905 -0
- package/dist/esm/trie.mjs +1220 -0
- package/dist/esm-legacy/binary-tree.mjs +26304 -0
- package/dist/esm-legacy/graph.mjs +5407 -0
- package/dist/esm-legacy/hash.mjs +1307 -0
- package/dist/esm-legacy/heap.mjs +1593 -0
- package/dist/esm-legacy/index.mjs +31265 -14680
- package/dist/esm-legacy/linked-list.mjs +4575 -0
- package/dist/esm-legacy/matrix.mjs +1079 -0
- package/dist/esm-legacy/priority-queue.mjs +1370 -0
- package/dist/esm-legacy/queue.mjs +4258 -0
- package/dist/esm-legacy/stack.mjs +905 -0
- package/dist/esm-legacy/trie.mjs +1219 -0
- package/dist/types/common/error.d.ts +9 -0
- package/dist/types/common/index.d.ts +1 -1
- package/dist/types/data-structures/base/index.d.ts +1 -0
- 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 +288 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +336 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +618 -18
- package/dist/types/data-structures/binary-tree/bst.d.ts +676 -1
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +456 -0
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +144 -1
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +3307 -399
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3285 -360
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2674 -325
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +3072 -287
- package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
- package/dist/types/data-structures/graph/directed-graph.d.ts +240 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +216 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +274 -10
- package/dist/types/data-structures/heap/heap.d.ts +336 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +411 -3
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +363 -3
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +434 -2
- package/dist/types/data-structures/matrix/matrix.d.ts +192 -0
- package/dist/types/data-structures/queue/deque.d.ts +364 -4
- package/dist/types/data-structures/queue/queue.d.ts +288 -0
- package/dist/types/data-structures/stack/stack.d.ts +240 -0
- package/dist/types/data-structures/trie/trie.d.ts +292 -4
- 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/bst.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +5 -0
- package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +4 -0
- package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +4 -0
- package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
- 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 +31196 -14608
- package/dist/umd/data-structure-typed.min.js +11 -5
- 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 +6644 -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 +6293 -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 +6888 -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 +1389 -0
- package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +1591 -0
- package/docs-site-docusaurus/docs/api/classes/TreeSet.md +1246 -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 +615 -0
- package/docs-site-docusaurus/docs/guide/concepts.md +451 -0
- package/docs-site-docusaurus/docs/guide/faq.md +180 -0
- package/docs-site-docusaurus/docs/guide/guides.md +597 -0
- package/docs-site-docusaurus/docs/guide/installation.md +62 -0
- package/docs-site-docusaurus/docs/guide/integrations.md +825 -0
- package/docs-site-docusaurus/docs/guide/overview.md +645 -0
- package/docs-site-docusaurus/docs/guide/performance.md +835 -0
- package/docs-site-docusaurus/docs/guide/quick-start.md +104 -0
- package/docs-site-docusaurus/docs/guide/use-cases/_category_.json +6 -0
- package/docs-site-docusaurus/docs/guide/use-cases/array-sort-alternative.md +158 -0
- package/docs-site-docusaurus/docs/guide/use-cases/heap-vs-sorting.md +92 -0
- package/docs-site-docusaurus/docs/guide/use-cases/map-vs-treemap.md +151 -0
- package/docs-site-docusaurus/docs/guide/use-cases/priority-queue-typescript.md +113 -0
- package/docs-site-docusaurus/docs/guide/use-cases/treemap-javascript.md +151 -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 +120 -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/llms.txt +37 -0
- package/docs-site-docusaurus/static/robots.txt +4 -0
- package/docs-site-docusaurus/typedoc.json +23 -0
- package/llms.txt +37 -0
- package/package.json +159 -55
- package/src/common/error.ts +19 -1
- package/src/common/index.ts +1 -1
- package/src/data-structures/base/index.ts +1 -0
- package/src/data-structures/base/iterable-element-base.ts +3 -2
- 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 +287 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +327 -5
- package/src/data-structures/binary-tree/binary-tree.ts +581 -6
- package/src/data-structures/binary-tree/bst.ts +922 -7
- package/src/data-structures/binary-tree/red-black-tree.ts +453 -0
- package/src/data-structures/binary-tree/segment-tree.ts +139 -2
- package/src/data-structures/binary-tree/tree-map.ts +3300 -495
- package/src/data-structures/binary-tree/tree-multi-map.ts +3384 -563
- package/src/data-structures/binary-tree/tree-multi-set.ts +2757 -493
- package/src/data-structures/binary-tree/tree-set.ts +3122 -440
- package/src/data-structures/graph/abstract-graph.ts +6 -6
- package/src/data-structures/graph/directed-graph.ts +230 -0
- package/src/data-structures/graph/undirected-graph.ts +207 -0
- package/src/data-structures/hash/hash-map.ts +270 -19
- package/src/data-structures/heap/heap.ts +326 -4
- package/src/data-structures/heap/max-heap.ts +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +394 -3
- package/src/data-structures/linked-list/singly-linked-list.ts +348 -3
- package/src/data-structures/linked-list/skip-linked-list.ts +421 -7
- package/src/data-structures/matrix/matrix.ts +194 -10
- package/src/data-structures/priority-queue/max-priority-queue.ts +2 -2
- package/src/data-structures/queue/deque.ts +350 -5
- package/src/data-structures/queue/queue.ts +276 -0
- package/src/data-structures/stack/stack.ts +230 -0
- package/src/data-structures/trie/trie.ts +283 -7
- package/src/interfaces/graph.ts +1 -1
- package/src/types/common.ts +2 -2
- package/src/types/data-structures/binary-tree/bst.ts +1 -0
- package/src/types/data-structures/binary-tree/tree-map.ts +6 -0
- package/src/types/data-structures/binary-tree/tree-multi-set.ts +5 -0
- package/src/types/data-structures/binary-tree/tree-set.ts +5 -0
- package/src/types/data-structures/heap/heap.ts +1 -0
- 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,905 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
4
|
+
// src/common/error.ts
|
|
5
|
+
function raise(ErrorClass, message) {
|
|
6
|
+
throw new ErrorClass(message);
|
|
7
|
+
}
|
|
8
|
+
__name(raise, "raise");
|
|
9
|
+
|
|
10
|
+
// src/data-structures/base/iterable-element-base.ts
|
|
11
|
+
var IterableElementBase = class {
|
|
12
|
+
static {
|
|
13
|
+
__name(this, "IterableElementBase");
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Create a new iterable base.
|
|
17
|
+
*
|
|
18
|
+
* @param options Optional behavior overrides. When provided, a `toElementFn`
|
|
19
|
+
* is used to convert a raw element (`R`) into a public element (`E`).
|
|
20
|
+
*
|
|
21
|
+
* @remarks
|
|
22
|
+
* Time O(1), Space O(1).
|
|
23
|
+
*/
|
|
24
|
+
constructor(options) {
|
|
25
|
+
if (options) {
|
|
26
|
+
const { toElementFn } = options;
|
|
27
|
+
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
28
|
+
else if (toElementFn) raise(TypeError, "toElementFn must be a function type");
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The converter used to transform a raw element (`R`) into a public element (`E`).
|
|
33
|
+
*
|
|
34
|
+
* @remarks
|
|
35
|
+
* Time O(1), Space O(1).
|
|
36
|
+
*/
|
|
37
|
+
_toElementFn;
|
|
38
|
+
/**
|
|
39
|
+
* Exposes the current `toElementFn`, if configured.
|
|
40
|
+
*
|
|
41
|
+
* @returns The converter function or `undefined` when not set.
|
|
42
|
+
* @remarks
|
|
43
|
+
* Time O(1), Space O(1).
|
|
44
|
+
*/
|
|
45
|
+
get toElementFn() {
|
|
46
|
+
return this._toElementFn;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Returns an iterator over the structure's elements.
|
|
50
|
+
*
|
|
51
|
+
* @param args Optional iterator arguments forwarded to the internal iterator.
|
|
52
|
+
* @returns An `IterableIterator<E>` that yields the elements in traversal order.
|
|
53
|
+
*
|
|
54
|
+
* @remarks
|
|
55
|
+
* Producing the iterator is O(1); consuming the entire iterator is Time O(n) with O(1) extra space.
|
|
56
|
+
*/
|
|
57
|
+
*[Symbol.iterator](...args) {
|
|
58
|
+
yield* this._getIterator(...args);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Returns an iterator over the values (alias of the default iterator).
|
|
62
|
+
*
|
|
63
|
+
* @returns An `IterableIterator<E>` over all elements.
|
|
64
|
+
* @remarks
|
|
65
|
+
* Creating the iterator is O(1); full iteration is Time O(n), Space O(1).
|
|
66
|
+
*/
|
|
67
|
+
*values() {
|
|
68
|
+
for (const item of this) yield item;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Tests whether all elements satisfy the predicate.
|
|
72
|
+
*
|
|
73
|
+
* @template TReturn
|
|
74
|
+
* @param predicate Function invoked for each element with signature `(value, index, self)`.
|
|
75
|
+
* @param thisArg Optional `this` binding for the predicate.
|
|
76
|
+
* @returns `true` if every element passes; otherwise `false`.
|
|
77
|
+
*
|
|
78
|
+
* @remarks
|
|
79
|
+
* Time O(n) in the worst case; may exit early when the first failure is found. Space O(1).
|
|
80
|
+
*/
|
|
81
|
+
every(predicate, thisArg) {
|
|
82
|
+
let index = 0;
|
|
83
|
+
for (const item of this) {
|
|
84
|
+
if (thisArg === void 0) {
|
|
85
|
+
if (!predicate(item, index++, this)) return false;
|
|
86
|
+
} else {
|
|
87
|
+
const fn = predicate;
|
|
88
|
+
if (!fn.call(thisArg, item, index++, this)) return false;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Tests whether at least one element satisfies the predicate.
|
|
95
|
+
*
|
|
96
|
+
* @param predicate Function invoked for each element with signature `(value, index, self)`.
|
|
97
|
+
* @param thisArg Optional `this` binding for the predicate.
|
|
98
|
+
* @returns `true` if any element passes; otherwise `false`.
|
|
99
|
+
*
|
|
100
|
+
* @remarks
|
|
101
|
+
* Time O(n) in the worst case; may exit early on first success. Space O(1).
|
|
102
|
+
*/
|
|
103
|
+
some(predicate, thisArg) {
|
|
104
|
+
let index = 0;
|
|
105
|
+
for (const item of this) {
|
|
106
|
+
if (thisArg === void 0) {
|
|
107
|
+
if (predicate(item, index++, this)) return true;
|
|
108
|
+
} else {
|
|
109
|
+
const fn = predicate;
|
|
110
|
+
if (fn.call(thisArg, item, index++, this)) return true;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Invokes a callback for each element in iteration order.
|
|
117
|
+
*
|
|
118
|
+
* @param callbackfn Function invoked per element with signature `(value, index, self)`.
|
|
119
|
+
* @param thisArg Optional `this` binding for the callback.
|
|
120
|
+
* @returns `void`.
|
|
121
|
+
*
|
|
122
|
+
* @remarks
|
|
123
|
+
* Time O(n), Space O(1).
|
|
124
|
+
*/
|
|
125
|
+
forEach(callbackfn, thisArg) {
|
|
126
|
+
let index = 0;
|
|
127
|
+
for (const item of this) {
|
|
128
|
+
if (thisArg === void 0) {
|
|
129
|
+
callbackfn(item, index++, this);
|
|
130
|
+
} else {
|
|
131
|
+
const fn = callbackfn;
|
|
132
|
+
fn.call(thisArg, item, index++, this);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
// Implementation signature
|
|
137
|
+
find(predicate, thisArg) {
|
|
138
|
+
let index = 0;
|
|
139
|
+
for (const item of this) {
|
|
140
|
+
if (thisArg === void 0) {
|
|
141
|
+
if (predicate(item, index++, this)) return item;
|
|
142
|
+
} else {
|
|
143
|
+
const fn = predicate;
|
|
144
|
+
if (fn.call(thisArg, item, index++, this)) return item;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Checks whether a strictly-equal element exists in the structure.
|
|
151
|
+
*
|
|
152
|
+
* @param element The element to test with `===` equality.
|
|
153
|
+
* @returns `true` if an equal element is found; otherwise `false`.
|
|
154
|
+
*
|
|
155
|
+
* @remarks
|
|
156
|
+
* Time O(n) in the worst case. Space O(1).
|
|
157
|
+
*/
|
|
158
|
+
has(element) {
|
|
159
|
+
for (const ele of this) if (ele === element) return true;
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Reduces all elements to a single accumulated value.
|
|
164
|
+
*
|
|
165
|
+
* @overload
|
|
166
|
+
* @param callbackfn Reducer of signature `(acc, value, index, self) => nextAcc`. The first element is used as the initial accumulator.
|
|
167
|
+
* @returns The final accumulated value typed as `E`.
|
|
168
|
+
*
|
|
169
|
+
* @overload
|
|
170
|
+
* @param callbackfn Reducer of signature `(acc, value, index, self) => nextAcc`.
|
|
171
|
+
* @param initialValue The initial accumulator value of type `E`.
|
|
172
|
+
* @returns The final accumulated value typed as `E`.
|
|
173
|
+
*
|
|
174
|
+
* @overload
|
|
175
|
+
* @template U The accumulator type when it differs from `E`.
|
|
176
|
+
* @param callbackfn Reducer of signature `(acc: U, value, index, self) => U`.
|
|
177
|
+
* @param initialValue The initial accumulator value of type `U`.
|
|
178
|
+
* @returns The final accumulated value typed as `U`.
|
|
179
|
+
*
|
|
180
|
+
* @remarks
|
|
181
|
+
* Time O(n), Space O(1). Throws if called on an empty structure without `initialValue`.
|
|
182
|
+
*/
|
|
183
|
+
reduce(callbackfn, initialValue) {
|
|
184
|
+
let index = 0;
|
|
185
|
+
const iter = this[Symbol.iterator]();
|
|
186
|
+
let acc;
|
|
187
|
+
if (arguments.length >= 2) {
|
|
188
|
+
acc = initialValue;
|
|
189
|
+
} else {
|
|
190
|
+
const first = iter.next();
|
|
191
|
+
if (first.done) raise(TypeError, "Reduce of empty structure with no initial value");
|
|
192
|
+
acc = first.value;
|
|
193
|
+
index = 1;
|
|
194
|
+
}
|
|
195
|
+
for (const value of iter) {
|
|
196
|
+
acc = callbackfn(acc, value, index++, this);
|
|
197
|
+
}
|
|
198
|
+
return acc;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Materializes the elements into a new array.
|
|
202
|
+
*
|
|
203
|
+
* @returns A shallow array copy of the iteration order.
|
|
204
|
+
* @remarks
|
|
205
|
+
* Time O(n), Space O(n).
|
|
206
|
+
*/
|
|
207
|
+
toArray() {
|
|
208
|
+
return [...this];
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Returns a representation of the structure suitable for quick visualization.
|
|
212
|
+
* Defaults to an array of elements; subclasses may override to provide richer visuals.
|
|
213
|
+
*
|
|
214
|
+
* @returns A visual representation (array by default).
|
|
215
|
+
* @remarks
|
|
216
|
+
* Time O(n), Space O(n).
|
|
217
|
+
*/
|
|
218
|
+
toVisual() {
|
|
219
|
+
return [...this];
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Prints `toVisual()` to the console. Intended for quick debugging.
|
|
223
|
+
*
|
|
224
|
+
* @returns `void`.
|
|
225
|
+
* @remarks
|
|
226
|
+
* Time O(n) due to materialization, Space O(n) for the intermediate representation.
|
|
227
|
+
*/
|
|
228
|
+
print() {
|
|
229
|
+
console.log(this.toVisual());
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
// src/data-structures/stack/stack.ts
|
|
234
|
+
var Stack = class extends IterableElementBase {
|
|
235
|
+
static {
|
|
236
|
+
__name(this, "Stack");
|
|
237
|
+
}
|
|
238
|
+
_equals = /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals");
|
|
239
|
+
/**
|
|
240
|
+
* Create a Stack and optionally bulk-push elements.
|
|
241
|
+
* @remarks Time O(N), Space O(N)
|
|
242
|
+
* @param [elements] - Iterable of elements (or raw records if toElementFn is set).
|
|
243
|
+
* @param [options] - Options such as toElementFn and equality function.
|
|
244
|
+
* @returns New Stack instance.
|
|
245
|
+
*/
|
|
246
|
+
constructor(elements = [], options) {
|
|
247
|
+
super(options);
|
|
248
|
+
this.pushMany(elements);
|
|
249
|
+
}
|
|
250
|
+
_elements = [];
|
|
251
|
+
/**
|
|
252
|
+
* Get the backing array of elements.
|
|
253
|
+
* @remarks Time O(1), Space O(1)
|
|
254
|
+
* @returns Internal elements array.
|
|
255
|
+
*/
|
|
256
|
+
get elements() {
|
|
257
|
+
return this._elements;
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Get the number of stored elements.
|
|
261
|
+
* @remarks Time O(1), Space O(1)
|
|
262
|
+
* @returns Current size.
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
* @example
|
|
297
|
+
* // Get number of elements
|
|
298
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
299
|
+
* console.log(stack.size); // 3;
|
|
300
|
+
*/
|
|
301
|
+
get size() {
|
|
302
|
+
return this.elements.length;
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Create a stack from an array of elements.
|
|
306
|
+
* @remarks Time O(N), Space O(N)
|
|
307
|
+
* @template E
|
|
308
|
+
* @template R
|
|
309
|
+
* @param this - The constructor (subclass) to instantiate.
|
|
310
|
+
* @param elements - Array of elements to push in order.
|
|
311
|
+
* @param [options] - Options forwarded to the constructor.
|
|
312
|
+
* @returns A new Stack populated from the array.
|
|
313
|
+
*/
|
|
314
|
+
static fromArray(elements, options) {
|
|
315
|
+
return new this(elements, options);
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Check whether the stack is empty.
|
|
319
|
+
* @remarks Time O(1), Space O(1)
|
|
320
|
+
* @returns True if size is 0.
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
* @example
|
|
357
|
+
* // Check if stack has elements
|
|
358
|
+
* const stack = new Stack<number>();
|
|
359
|
+
* console.log(stack.isEmpty()); // true;
|
|
360
|
+
* stack.push(1);
|
|
361
|
+
* console.log(stack.isEmpty()); // false;
|
|
362
|
+
*/
|
|
363
|
+
isEmpty() {
|
|
364
|
+
return this.elements.length === 0;
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* Get the top element without removing it.
|
|
368
|
+
* @remarks Time O(1), Space O(1)
|
|
369
|
+
* @returns Top element or undefined.
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
* @example
|
|
406
|
+
* // View the top element without removing it
|
|
407
|
+
* const stack = new Stack<string>(['a', 'b', 'c']);
|
|
408
|
+
* console.log(stack.peek()); // 'c';
|
|
409
|
+
* console.log(stack.size); // 3;
|
|
410
|
+
*/
|
|
411
|
+
peek() {
|
|
412
|
+
return this.isEmpty() ? void 0 : this.elements[this.elements.length - 1];
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* Push one element onto the top.
|
|
416
|
+
* @remarks Time O(1), Space O(1)
|
|
417
|
+
* @param element - Element to push.
|
|
418
|
+
* @returns True when pushed.
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
|
|
436
|
+
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
|
|
453
|
+
|
|
454
|
+
* @example
|
|
455
|
+
* // basic Stack creation and push operation
|
|
456
|
+
* // Create a simple Stack with initial values
|
|
457
|
+
* const stack = new Stack([1, 2, 3, 4, 5]);
|
|
458
|
+
*
|
|
459
|
+
* // Verify the stack maintains insertion order (LIFO will be shown in pop)
|
|
460
|
+
* console.log([...stack]); // [1, 2, 3, 4, 5];
|
|
461
|
+
*
|
|
462
|
+
* // Check length
|
|
463
|
+
* console.log(stack.size); // 5;
|
|
464
|
+
*
|
|
465
|
+
* // Push a new element to the top
|
|
466
|
+
* stack.push(6);
|
|
467
|
+
* console.log(stack.size); // 6;
|
|
468
|
+
*/
|
|
469
|
+
push(element) {
|
|
470
|
+
this.elements.push(element);
|
|
471
|
+
return true;
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* Pop and return the top element.
|
|
475
|
+
* @remarks Time O(1), Space O(1)
|
|
476
|
+
* @returns Removed element or undefined.
|
|
477
|
+
|
|
478
|
+
|
|
479
|
+
|
|
480
|
+
|
|
481
|
+
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
|
|
485
|
+
|
|
486
|
+
|
|
487
|
+
|
|
488
|
+
|
|
489
|
+
|
|
490
|
+
|
|
491
|
+
|
|
492
|
+
|
|
493
|
+
|
|
494
|
+
|
|
495
|
+
|
|
496
|
+
|
|
497
|
+
|
|
498
|
+
|
|
499
|
+
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
|
|
504
|
+
|
|
505
|
+
|
|
506
|
+
|
|
507
|
+
|
|
508
|
+
|
|
509
|
+
|
|
510
|
+
|
|
511
|
+
|
|
512
|
+
* @example
|
|
513
|
+
* // Stack pop operation (LIFO - Last In First Out)
|
|
514
|
+
* const stack = new Stack<number>([10, 20, 30, 40, 50]);
|
|
515
|
+
*
|
|
516
|
+
* // Peek at the top element without removing
|
|
517
|
+
* const top = stack.peek();
|
|
518
|
+
* console.log(top); // 50;
|
|
519
|
+
*
|
|
520
|
+
* // Pop removes from the top (LIFO order)
|
|
521
|
+
* const popped = stack.pop();
|
|
522
|
+
* console.log(popped); // 50;
|
|
523
|
+
*
|
|
524
|
+
* // Next pop gets the previous element
|
|
525
|
+
* const next = stack.pop();
|
|
526
|
+
* console.log(next); // 40;
|
|
527
|
+
*
|
|
528
|
+
* // Verify length decreased
|
|
529
|
+
* console.log(stack.size); // 3;
|
|
530
|
+
*/
|
|
531
|
+
pop() {
|
|
532
|
+
return this.isEmpty() ? void 0 : this.elements.pop();
|
|
533
|
+
}
|
|
534
|
+
/**
|
|
535
|
+
* Push many elements from an iterable.
|
|
536
|
+
* @remarks Time O(N), Space O(1)
|
|
537
|
+
* @param elements - Iterable of elements (or raw records if toElementFn is set).
|
|
538
|
+
* @returns Array of per-element success flags.
|
|
539
|
+
*/
|
|
540
|
+
pushMany(elements) {
|
|
541
|
+
const ans = [];
|
|
542
|
+
for (const el of elements) {
|
|
543
|
+
if (this.toElementFn) ans.push(this.push(this.toElementFn(el)));
|
|
544
|
+
else ans.push(this.push(el));
|
|
545
|
+
}
|
|
546
|
+
return ans;
|
|
547
|
+
}
|
|
548
|
+
/**
|
|
549
|
+
* Delete the first occurrence of a specific element.
|
|
550
|
+
* @remarks Time O(N), Space O(1)
|
|
551
|
+
* @param element - Element to remove (using the configured equality).
|
|
552
|
+
* @returns True if an element was removed.
|
|
553
|
+
|
|
554
|
+
|
|
555
|
+
|
|
556
|
+
|
|
557
|
+
|
|
558
|
+
|
|
559
|
+
|
|
560
|
+
|
|
561
|
+
|
|
562
|
+
|
|
563
|
+
|
|
564
|
+
|
|
565
|
+
|
|
566
|
+
|
|
567
|
+
|
|
568
|
+
|
|
569
|
+
|
|
570
|
+
|
|
571
|
+
|
|
572
|
+
|
|
573
|
+
|
|
574
|
+
|
|
575
|
+
|
|
576
|
+
|
|
577
|
+
|
|
578
|
+
|
|
579
|
+
|
|
580
|
+
|
|
581
|
+
|
|
582
|
+
|
|
583
|
+
|
|
584
|
+
|
|
585
|
+
* @example
|
|
586
|
+
* // Remove element
|
|
587
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
588
|
+
* stack.delete(2);
|
|
589
|
+
* console.log(stack.toArray()); // [1, 3];
|
|
590
|
+
*/
|
|
591
|
+
delete(element) {
|
|
592
|
+
const idx = this._indexOfByEquals(element);
|
|
593
|
+
return this.deleteAt(idx);
|
|
594
|
+
}
|
|
595
|
+
/**
|
|
596
|
+
* Delete the element at an index.
|
|
597
|
+
* @remarks Time O(N), Space O(1)
|
|
598
|
+
* @param index - Zero-based index from the bottom.
|
|
599
|
+
* @returns True if removed.
|
|
600
|
+
*/
|
|
601
|
+
deleteAt(index) {
|
|
602
|
+
if (index < 0 || index >= this.elements.length) return false;
|
|
603
|
+
const spliced = this.elements.splice(index, 1);
|
|
604
|
+
return spliced.length === 1;
|
|
605
|
+
}
|
|
606
|
+
/**
|
|
607
|
+
* Delete the first element that satisfies a predicate.
|
|
608
|
+
* @remarks Time O(N), Space O(1)
|
|
609
|
+
* @param predicate - Function (value, index, stack) → boolean to decide deletion.
|
|
610
|
+
* @returns True if a match was removed.
|
|
611
|
+
*/
|
|
612
|
+
deleteWhere(predicate) {
|
|
613
|
+
for (let i = 0; i < this.elements.length; i++) {
|
|
614
|
+
if (predicate(this.elements[i], i, this)) {
|
|
615
|
+
this.elements.splice(i, 1);
|
|
616
|
+
return true;
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
return false;
|
|
620
|
+
}
|
|
621
|
+
/**
|
|
622
|
+
* Remove all elements and reset storage.
|
|
623
|
+
* @remarks Time O(1), Space O(1)
|
|
624
|
+
* @returns void
|
|
625
|
+
|
|
626
|
+
|
|
627
|
+
|
|
628
|
+
|
|
629
|
+
|
|
630
|
+
|
|
631
|
+
|
|
632
|
+
|
|
633
|
+
|
|
634
|
+
|
|
635
|
+
|
|
636
|
+
|
|
637
|
+
|
|
638
|
+
|
|
639
|
+
|
|
640
|
+
|
|
641
|
+
|
|
642
|
+
|
|
643
|
+
|
|
644
|
+
|
|
645
|
+
|
|
646
|
+
|
|
647
|
+
|
|
648
|
+
|
|
649
|
+
|
|
650
|
+
|
|
651
|
+
|
|
652
|
+
|
|
653
|
+
|
|
654
|
+
|
|
655
|
+
|
|
656
|
+
|
|
657
|
+
|
|
658
|
+
* @example
|
|
659
|
+
* // Remove all elements
|
|
660
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
661
|
+
* stack.clear();
|
|
662
|
+
* console.log(stack.isEmpty()); // true;
|
|
663
|
+
*/
|
|
664
|
+
clear() {
|
|
665
|
+
this._elements = [];
|
|
666
|
+
}
|
|
667
|
+
/**
|
|
668
|
+
* Deep clone this stack.
|
|
669
|
+
* @remarks Time O(N), Space O(N)
|
|
670
|
+
* @returns A new stack with the same content.
|
|
671
|
+
|
|
672
|
+
|
|
673
|
+
|
|
674
|
+
|
|
675
|
+
|
|
676
|
+
|
|
677
|
+
|
|
678
|
+
|
|
679
|
+
|
|
680
|
+
|
|
681
|
+
|
|
682
|
+
|
|
683
|
+
|
|
684
|
+
|
|
685
|
+
|
|
686
|
+
|
|
687
|
+
|
|
688
|
+
|
|
689
|
+
|
|
690
|
+
|
|
691
|
+
|
|
692
|
+
|
|
693
|
+
|
|
694
|
+
|
|
695
|
+
|
|
696
|
+
|
|
697
|
+
|
|
698
|
+
|
|
699
|
+
|
|
700
|
+
|
|
701
|
+
|
|
702
|
+
|
|
703
|
+
|
|
704
|
+
* @example
|
|
705
|
+
* // Create independent copy
|
|
706
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
707
|
+
* const copy = stack.clone();
|
|
708
|
+
* copy.pop();
|
|
709
|
+
* console.log(stack.size); // 3;
|
|
710
|
+
* console.log(copy.size); // 2;
|
|
711
|
+
*/
|
|
712
|
+
clone() {
|
|
713
|
+
const out = this._createInstance({ toElementFn: this.toElementFn });
|
|
714
|
+
for (const v of this) out.push(v);
|
|
715
|
+
return out;
|
|
716
|
+
}
|
|
717
|
+
/**
|
|
718
|
+
* Filter elements into a new stack of the same class.
|
|
719
|
+
* @remarks Time O(N), Space O(N)
|
|
720
|
+
* @param predicate - Predicate (value, index, stack) → boolean to keep value.
|
|
721
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
722
|
+
* @returns A new stack with kept values.
|
|
723
|
+
|
|
724
|
+
|
|
725
|
+
|
|
726
|
+
|
|
727
|
+
|
|
728
|
+
|
|
729
|
+
|
|
730
|
+
|
|
731
|
+
|
|
732
|
+
|
|
733
|
+
|
|
734
|
+
|
|
735
|
+
|
|
736
|
+
|
|
737
|
+
|
|
738
|
+
|
|
739
|
+
|
|
740
|
+
|
|
741
|
+
|
|
742
|
+
|
|
743
|
+
|
|
744
|
+
|
|
745
|
+
|
|
746
|
+
|
|
747
|
+
|
|
748
|
+
|
|
749
|
+
|
|
750
|
+
|
|
751
|
+
|
|
752
|
+
|
|
753
|
+
|
|
754
|
+
|
|
755
|
+
|
|
756
|
+
* @example
|
|
757
|
+
* // Filter elements
|
|
758
|
+
* const stack = new Stack<number>([1, 2, 3, 4, 5]);
|
|
759
|
+
* const evens = stack.filter(x => x % 2 === 0);
|
|
760
|
+
* console.log(evens.toArray()); // [2, 4];
|
|
761
|
+
*/
|
|
762
|
+
filter(predicate, thisArg) {
|
|
763
|
+
const out = this._createInstance({ toElementFn: this.toElementFn });
|
|
764
|
+
let index = 0;
|
|
765
|
+
for (const v of this) {
|
|
766
|
+
if (predicate.call(thisArg, v, index, this)) out.push(v);
|
|
767
|
+
index++;
|
|
768
|
+
}
|
|
769
|
+
return out;
|
|
770
|
+
}
|
|
771
|
+
/**
|
|
772
|
+
* Map values into a new stack of the same element type.
|
|
773
|
+
* @remarks Time O(N), Space O(N)
|
|
774
|
+
* @param callback - Mapping function (value, index, stack) → newValue.
|
|
775
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
776
|
+
* @returns A new stack with mapped values.
|
|
777
|
+
*/
|
|
778
|
+
mapSame(callback, thisArg) {
|
|
779
|
+
const out = this._createInstance({ toElementFn: this.toElementFn });
|
|
780
|
+
let index = 0;
|
|
781
|
+
for (const v of this) {
|
|
782
|
+
const mv = thisArg === void 0 ? callback(v, index++, this) : callback.call(thisArg, v, index++, this);
|
|
783
|
+
out.push(mv);
|
|
784
|
+
}
|
|
785
|
+
return out;
|
|
786
|
+
}
|
|
787
|
+
/**
|
|
788
|
+
* Map values into a new stack (possibly different element type).
|
|
789
|
+
* @remarks Time O(N), Space O(N)
|
|
790
|
+
* @template EM
|
|
791
|
+
* @template RM
|
|
792
|
+
* @param callback - Mapping function (value, index, stack) → newElement.
|
|
793
|
+
* @param [options] - Options for the output stack (e.g., toElementFn).
|
|
794
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
795
|
+
* @returns A new Stack with mapped elements.
|
|
796
|
+
|
|
797
|
+
|
|
798
|
+
|
|
799
|
+
|
|
800
|
+
|
|
801
|
+
|
|
802
|
+
|
|
803
|
+
|
|
804
|
+
|
|
805
|
+
|
|
806
|
+
|
|
807
|
+
|
|
808
|
+
|
|
809
|
+
|
|
810
|
+
|
|
811
|
+
|
|
812
|
+
|
|
813
|
+
|
|
814
|
+
|
|
815
|
+
|
|
816
|
+
|
|
817
|
+
|
|
818
|
+
|
|
819
|
+
|
|
820
|
+
|
|
821
|
+
|
|
822
|
+
|
|
823
|
+
|
|
824
|
+
|
|
825
|
+
|
|
826
|
+
|
|
827
|
+
|
|
828
|
+
* @example
|
|
829
|
+
* // Transform elements
|
|
830
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
831
|
+
* const doubled = stack.map(x => x * 2);
|
|
832
|
+
* console.log(doubled.toArray()); // [2, 4, 6];
|
|
833
|
+
*/
|
|
834
|
+
map(callback, options, thisArg) {
|
|
835
|
+
const out = this._createLike([], { ...options ?? {} });
|
|
836
|
+
let index = 0;
|
|
837
|
+
for (const v of this) {
|
|
838
|
+
out.push(thisArg === void 0 ? callback(v, index, this) : callback.call(thisArg, v, index, this));
|
|
839
|
+
index++;
|
|
840
|
+
}
|
|
841
|
+
return out;
|
|
842
|
+
}
|
|
843
|
+
/**
|
|
844
|
+
* Set the equality comparator used by delete/search operations.
|
|
845
|
+
* @remarks Time O(1), Space O(1)
|
|
846
|
+
* @param equals - Equality predicate (a, b) → boolean.
|
|
847
|
+
* @returns This stack.
|
|
848
|
+
*/
|
|
849
|
+
setEquality(equals) {
|
|
850
|
+
this._equals = equals;
|
|
851
|
+
return this;
|
|
852
|
+
}
|
|
853
|
+
/**
|
|
854
|
+
* (Protected) Find the index of a target element using the equality function.
|
|
855
|
+
* @remarks Time O(N), Space O(1)
|
|
856
|
+
* @param target - Element to search for.
|
|
857
|
+
* @returns Index or -1 if not found.
|
|
858
|
+
*/
|
|
859
|
+
_indexOfByEquals(target) {
|
|
860
|
+
for (let i = 0; i < this.elements.length; i++) if (this._equals(this.elements[i], target)) return i;
|
|
861
|
+
return -1;
|
|
862
|
+
}
|
|
863
|
+
/**
|
|
864
|
+
* (Protected) Create an empty instance of the same concrete class.
|
|
865
|
+
* @remarks Time O(1), Space O(1)
|
|
866
|
+
* @param [options] - Options forwarded to the constructor.
|
|
867
|
+
* @returns An empty like-kind stack instance.
|
|
868
|
+
*/
|
|
869
|
+
_createInstance(options) {
|
|
870
|
+
const Ctor = this.constructor;
|
|
871
|
+
return new Ctor([], options);
|
|
872
|
+
}
|
|
873
|
+
/**
|
|
874
|
+
* (Protected) Create a like-kind stack and seed it from an iterable.
|
|
875
|
+
* @remarks Time O(N), Space O(N)
|
|
876
|
+
* @template T
|
|
877
|
+
* @template RR
|
|
878
|
+
* @param [elements] - Iterable used to seed the new stack.
|
|
879
|
+
* @param [options] - Options forwarded to the constructor.
|
|
880
|
+
* @returns A like-kind Stack instance.
|
|
881
|
+
*/
|
|
882
|
+
_createLike(elements = [], options) {
|
|
883
|
+
const Ctor = this.constructor;
|
|
884
|
+
return new Ctor(elements, options);
|
|
885
|
+
}
|
|
886
|
+
/**
|
|
887
|
+
* (Protected) Iterate elements from bottom to top.
|
|
888
|
+
* @remarks Time O(N), Space O(1)
|
|
889
|
+
* @returns Iterator of elements.
|
|
890
|
+
*/
|
|
891
|
+
*_getIterator() {
|
|
892
|
+
for (let i = 0; i < this.elements.length; i++) yield this.elements[i];
|
|
893
|
+
}
|
|
894
|
+
};
|
|
895
|
+
/**
|
|
896
|
+
* data-structure-typed
|
|
897
|
+
*
|
|
898
|
+
* @author Pablo Zeng
|
|
899
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
900
|
+
* @license MIT License
|
|
901
|
+
*/
|
|
902
|
+
|
|
903
|
+
export { Stack };
|
|
904
|
+
//# sourceMappingURL=stack.mjs.map
|
|
905
|
+
//# sourceMappingURL=stack.mjs.map
|