data-structure-typed 2.5.0 → 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.
Files changed (222) hide show
  1. package/.vitepress/cache/deps_temp_51f5f1b0/chunk-7OIKW5WK.js +12984 -0
  2. package/.vitepress/cache/deps_temp_51f5f1b0/package.json +3 -0
  3. package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vue_devtools-api.js +4505 -0
  4. package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vueuse_core.js +9731 -0
  5. package/.vitepress/cache/deps_temp_51f5f1b0/vue.js +347 -0
  6. package/CHANGELOG.md +3 -1
  7. package/README.md +63 -26
  8. package/dist/cjs/binary-tree.cjs +23698 -0
  9. package/dist/cjs/graph.cjs +5236 -0
  10. package/dist/cjs/hash.cjs +1262 -0
  11. package/dist/cjs/heap.cjs +1540 -0
  12. package/dist/cjs/index.cjs +28244 -14795
  13. package/dist/cjs/linked-list.cjs +4370 -0
  14. package/dist/cjs/matrix.cjs +1042 -0
  15. package/dist/cjs/priority-queue.cjs +1314 -0
  16. package/dist/cjs/queue.cjs +4090 -0
  17. package/dist/cjs/stack.cjs +861 -0
  18. package/dist/cjs/trie.cjs +1173 -0
  19. package/dist/cjs-legacy/binary-tree.cjs +23730 -0
  20. package/dist/cjs-legacy/graph.cjs +5234 -0
  21. package/dist/cjs-legacy/hash.cjs +1262 -0
  22. package/dist/cjs-legacy/heap.cjs +1537 -0
  23. package/dist/cjs-legacy/index.cjs +28239 -14790
  24. package/dist/cjs-legacy/linked-list.cjs +4376 -0
  25. package/dist/cjs-legacy/matrix.cjs +1045 -0
  26. package/dist/cjs-legacy/priority-queue.cjs +1312 -0
  27. package/dist/cjs-legacy/queue.cjs +4088 -0
  28. package/dist/cjs-legacy/stack.cjs +861 -0
  29. package/dist/cjs-legacy/trie.cjs +1172 -0
  30. package/dist/esm/binary-tree.mjs +23683 -0
  31. package/dist/esm/graph.mjs +5223 -0
  32. package/dist/esm/hash.mjs +1259 -0
  33. package/dist/esm/heap.mjs +1534 -0
  34. package/dist/esm/index.mjs +28242 -14796
  35. package/dist/esm/linked-list.mjs +4363 -0
  36. package/dist/esm/matrix.mjs +1038 -0
  37. package/dist/esm/priority-queue.mjs +1310 -0
  38. package/dist/esm/queue.mjs +4086 -0
  39. package/dist/esm/stack.mjs +859 -0
  40. package/dist/esm/trie.mjs +1170 -0
  41. package/dist/esm-legacy/binary-tree.mjs +23715 -0
  42. package/dist/esm-legacy/graph.mjs +5221 -0
  43. package/dist/esm-legacy/hash.mjs +1259 -0
  44. package/dist/esm-legacy/heap.mjs +1531 -0
  45. package/dist/esm-legacy/index.mjs +28237 -14791
  46. package/dist/esm-legacy/linked-list.mjs +4369 -0
  47. package/dist/esm-legacy/matrix.mjs +1041 -0
  48. package/dist/esm-legacy/priority-queue.mjs +1308 -0
  49. package/dist/esm-legacy/queue.mjs +4084 -0
  50. package/dist/esm-legacy/stack.mjs +859 -0
  51. package/dist/esm-legacy/trie.mjs +1169 -0
  52. package/dist/types/data-structures/base/index.d.ts +1 -0
  53. package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
  54. package/dist/types/data-structures/base/linear-base.d.ts +3 -3
  55. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +240 -0
  56. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +280 -0
  57. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +502 -2
  58. package/dist/types/data-structures/binary-tree/bst.d.ts +481 -1
  59. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +380 -0
  60. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +120 -1
  61. package/dist/types/data-structures/binary-tree/tree-map.d.ts +2764 -384
  62. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2753 -353
  63. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2241 -321
  64. package/dist/types/data-structures/binary-tree/tree-set.d.ts +2554 -274
  65. package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
  66. package/dist/types/data-structures/graph/directed-graph.d.ts +200 -0
  67. package/dist/types/data-structures/graph/undirected-graph.d.ts +180 -0
  68. package/dist/types/data-structures/hash/hash-map.d.ts +230 -10
  69. package/dist/types/data-structures/heap/heap.d.ts +280 -0
  70. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +343 -3
  71. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +303 -3
  72. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +362 -2
  73. package/dist/types/data-structures/matrix/matrix.d.ts +160 -0
  74. package/dist/types/data-structures/queue/deque.d.ts +304 -4
  75. package/dist/types/data-structures/queue/queue.d.ts +240 -0
  76. package/dist/types/data-structures/stack/stack.d.ts +200 -0
  77. package/dist/types/data-structures/trie/trie.d.ts +244 -4
  78. package/dist/types/interfaces/graph.d.ts +1 -1
  79. package/dist/types/types/common.d.ts +2 -2
  80. package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
  81. package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
  82. package/dist/types/types/utils/validate-type.d.ts +4 -4
  83. package/dist/umd/data-structure-typed.js +28022 -14573
  84. package/dist/umd/data-structure-typed.min.js +9 -3
  85. package/docs-site-docusaurus/README.md +41 -0
  86. package/docs-site-docusaurus/docs/api/README.md +52 -0
  87. package/docs-site-docusaurus/docs/api/classes/AVLTree.md +6130 -0
  88. package/docs-site-docusaurus/docs/api/classes/AVLTreeNode.md +282 -0
  89. package/docs-site-docusaurus/docs/api/classes/AbstractGraph.md +2266 -0
  90. package/docs-site-docusaurus/docs/api/classes/BST.md +5831 -0
  91. package/docs-site-docusaurus/docs/api/classes/BSTNode.md +333 -0
  92. package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +455 -0
  93. package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +4647 -0
  94. package/docs-site-docusaurus/docs/api/classes/BinaryTreeNode.md +331 -0
  95. package/docs-site-docusaurus/docs/api/classes/Deque.md +2767 -0
  96. package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +2999 -0
  97. package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +2685 -0
  98. package/docs-site-docusaurus/docs/api/classes/DoublyLinkedListNode.md +221 -0
  99. package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +253 -0
  100. package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +21 -0
  101. package/docs-site-docusaurus/docs/api/classes/HashMap.md +1333 -0
  102. package/docs-site-docusaurus/docs/api/classes/Heap.md +1881 -0
  103. package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +800 -0
  104. package/docs-site-docusaurus/docs/api/classes/IterableEntryBase.md +644 -0
  105. package/docs-site-docusaurus/docs/api/classes/LinearBase.md +1632 -0
  106. package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +1853 -0
  107. package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +1108 -0
  108. package/docs-site-docusaurus/docs/api/classes/LinkedListNode.md +156 -0
  109. package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +2824 -0
  110. package/docs-site-docusaurus/docs/api/classes/MapGraph.md +2929 -0
  111. package/docs-site-docusaurus/docs/api/classes/Matrix.md +1026 -0
  112. package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +1866 -0
  113. package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +1883 -0
  114. package/docs-site-docusaurus/docs/api/classes/MinHeap.md +1879 -0
  115. package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +1882 -0
  116. package/docs-site-docusaurus/docs/api/classes/Navigator.md +109 -0
  117. package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +1839 -0
  118. package/docs-site-docusaurus/docs/api/classes/Queue.md +2244 -0
  119. package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +6374 -0
  120. package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +372 -0
  121. package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +2897 -0
  122. package/docs-site-docusaurus/docs/api/classes/SinglyLinkedListNode.md +169 -0
  123. package/docs-site-docusaurus/docs/api/classes/SkipList.md +1229 -0
  124. package/docs-site-docusaurus/docs/api/classes/Stack.md +1573 -0
  125. package/docs-site-docusaurus/docs/api/classes/TreeMap.md +1257 -0
  126. package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +1475 -0
  127. package/docs-site-docusaurus/docs/api/classes/TreeSet.md +1117 -0
  128. package/docs-site-docusaurus/docs/api/classes/Trie.md +1708 -0
  129. package/docs-site-docusaurus/docs/api/classes/TrieNode.md +199 -0
  130. package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +2979 -0
  131. package/docs-site-docusaurus/docs/guide/_category_.json +6 -0
  132. package/docs-site-docusaurus/docs/guide/architecture.md +613 -0
  133. package/docs-site-docusaurus/docs/guide/concepts.md +420 -0
  134. package/docs-site-docusaurus/docs/guide/guides.md +611 -0
  135. package/docs-site-docusaurus/docs/guide/installation.md +60 -0
  136. package/docs-site-docusaurus/docs/guide/integrations.md +823 -0
  137. package/docs-site-docusaurus/docs/guide/overview.md +638 -0
  138. package/docs-site-docusaurus/docs/guide/performance.md +833 -0
  139. package/docs-site-docusaurus/docs/guide/quick-start.md +73 -0
  140. package/docs-site-docusaurus/docusaurus.config.ts +159 -0
  141. package/docs-site-docusaurus/fix-mdx-generics.mjs +75 -0
  142. package/docs-site-docusaurus/package-lock.json +18667 -0
  143. package/docs-site-docusaurus/package.json +50 -0
  144. package/docs-site-docusaurus/prefix-class-to-methods.mjs +48 -0
  145. package/docs-site-docusaurus/sidebars.ts +23 -0
  146. package/docs-site-docusaurus/sort-protected.mjs +87 -0
  147. package/docs-site-docusaurus/src/css/custom.css +96 -0
  148. package/docs-site-docusaurus/src/pages/index.module.css +13 -0
  149. package/docs-site-docusaurus/src/pages/index.tsx +71 -0
  150. package/docs-site-docusaurus/src/pages/markdown-page.md +7 -0
  151. package/docs-site-docusaurus/src/theme/TOCItems/index.tsx +34 -0
  152. package/docs-site-docusaurus/static/.nojekyll +0 -0
  153. package/docs-site-docusaurus/static/img/docusaurus-social-card.jpg +0 -0
  154. package/docs-site-docusaurus/static/img/docusaurus.png +0 -0
  155. package/docs-site-docusaurus/static/img/favicon.ico +0 -0
  156. package/docs-site-docusaurus/static/img/favicon.png +0 -0
  157. package/docs-site-docusaurus/static/img/logo-180.png +0 -0
  158. package/docs-site-docusaurus/static/img/logo.jpg +0 -0
  159. package/docs-site-docusaurus/static/img/logo.png +0 -0
  160. package/docs-site-docusaurus/static/img/logo.svg +1 -0
  161. package/docs-site-docusaurus/static/img/og-image.png +0 -0
  162. package/docs-site-docusaurus/static/img/undraw_docusaurus_mountain.svg +171 -0
  163. package/docs-site-docusaurus/static/img/undraw_docusaurus_react.svg +170 -0
  164. package/docs-site-docusaurus/static/img/undraw_docusaurus_tree.svg +40 -0
  165. package/docs-site-docusaurus/static/robots.txt +4 -0
  166. package/docs-site-docusaurus/typedoc.json +23 -0
  167. package/package.json +106 -10
  168. package/src/data-structures/base/index.ts +1 -0
  169. package/src/data-structures/base/iterable-entry-base.ts +8 -8
  170. package/src/data-structures/base/linear-base.ts +3 -3
  171. package/src/data-structures/binary-tree/avl-tree.ts +240 -0
  172. package/src/data-structures/binary-tree/binary-indexed-tree.ts +281 -1
  173. package/src/data-structures/binary-tree/binary-tree.ts +502 -2
  174. package/src/data-structures/binary-tree/bst.ts +481 -1
  175. package/src/data-structures/binary-tree/red-black-tree.ts +380 -0
  176. package/src/data-structures/binary-tree/segment-tree.ts +121 -2
  177. package/src/data-structures/binary-tree/tree-map.ts +2862 -482
  178. package/src/data-structures/binary-tree/tree-multi-map.ts +2963 -563
  179. package/src/data-structures/binary-tree/tree-multi-set.ts +2399 -479
  180. package/src/data-structures/binary-tree/tree-set.ts +2685 -405
  181. package/src/data-structures/graph/abstract-graph.ts +4 -4
  182. package/src/data-structures/graph/directed-graph.ts +200 -0
  183. package/src/data-structures/graph/undirected-graph.ts +180 -0
  184. package/src/data-structures/hash/hash-map.ts +235 -15
  185. package/src/data-structures/heap/heap.ts +280 -0
  186. package/src/data-structures/linked-list/doubly-linked-list.ts +343 -3
  187. package/src/data-structures/linked-list/singly-linked-list.ts +303 -3
  188. package/src/data-structures/linked-list/skip-linked-list.ts +362 -2
  189. package/src/data-structures/matrix/matrix.ts +161 -1
  190. package/src/data-structures/queue/deque.ts +305 -5
  191. package/src/data-structures/queue/queue.ts +240 -0
  192. package/src/data-structures/stack/stack.ts +200 -0
  193. package/src/data-structures/trie/trie.ts +245 -5
  194. package/src/interfaces/graph.ts +1 -1
  195. package/src/types/common.ts +2 -2
  196. package/src/types/data-structures/heap/heap.ts +1 -0
  197. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
  198. package/src/types/utils/validate-type.ts +4 -4
  199. package/vercel.json +6 -0
  200. package/dist/leetcode/avl-tree-counter.mjs +0 -2957
  201. package/dist/leetcode/avl-tree-multi-map.mjs +0 -2889
  202. package/dist/leetcode/avl-tree.mjs +0 -2720
  203. package/dist/leetcode/binary-tree.mjs +0 -1594
  204. package/dist/leetcode/bst.mjs +0 -2398
  205. package/dist/leetcode/deque.mjs +0 -683
  206. package/dist/leetcode/directed-graph.mjs +0 -1733
  207. package/dist/leetcode/doubly-linked-list.mjs +0 -709
  208. package/dist/leetcode/hash-map.mjs +0 -493
  209. package/dist/leetcode/heap.mjs +0 -542
  210. package/dist/leetcode/max-heap.mjs +0 -375
  211. package/dist/leetcode/max-priority-queue.mjs +0 -383
  212. package/dist/leetcode/min-heap.mjs +0 -363
  213. package/dist/leetcode/min-priority-queue.mjs +0 -371
  214. package/dist/leetcode/priority-queue.mjs +0 -363
  215. package/dist/leetcode/queue.mjs +0 -943
  216. package/dist/leetcode/red-black-tree.mjs +0 -2765
  217. package/dist/leetcode/singly-linked-list.mjs +0 -754
  218. package/dist/leetcode/stack.mjs +0 -217
  219. package/dist/leetcode/tree-counter.mjs +0 -3039
  220. package/dist/leetcode/tree-multi-map.mjs +0 -2913
  221. package/dist/leetcode/trie.mjs +0 -413
  222. package/dist/leetcode/undirected-graph.mjs +0 -1650
@@ -0,0 +1,1173 @@
1
+ 'use strict';
2
+
3
+ var __defProp = Object.defineProperty;
4
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
5
+
6
+ // src/data-structures/base/iterable-element-base.ts
7
+ var IterableElementBase = class {
8
+ static {
9
+ __name(this, "IterableElementBase");
10
+ }
11
+ /**
12
+ * Create a new iterable base.
13
+ *
14
+ * @param options Optional behavior overrides. When provided, a `toElementFn`
15
+ * is used to convert a raw element (`R`) into a public element (`E`).
16
+ *
17
+ * @remarks
18
+ * Time O(1), Space O(1).
19
+ */
20
+ constructor(options) {
21
+ if (options) {
22
+ const { toElementFn } = options;
23
+ if (typeof toElementFn === "function") this._toElementFn = toElementFn;
24
+ else if (toElementFn) throw new TypeError("toElementFn must be a function type");
25
+ }
26
+ }
27
+ /**
28
+ * The converter used to transform a raw element (`R`) into a public element (`E`).
29
+ *
30
+ * @remarks
31
+ * Time O(1), Space O(1).
32
+ */
33
+ _toElementFn;
34
+ /**
35
+ * Exposes the current `toElementFn`, if configured.
36
+ *
37
+ * @returns The converter function or `undefined` when not set.
38
+ * @remarks
39
+ * Time O(1), Space O(1).
40
+ */
41
+ get toElementFn() {
42
+ return this._toElementFn;
43
+ }
44
+ /**
45
+ * Returns an iterator over the structure's elements.
46
+ *
47
+ * @param args Optional iterator arguments forwarded to the internal iterator.
48
+ * @returns An `IterableIterator<E>` that yields the elements in traversal order.
49
+ *
50
+ * @remarks
51
+ * Producing the iterator is O(1); consuming the entire iterator is Time O(n) with O(1) extra space.
52
+ */
53
+ *[Symbol.iterator](...args) {
54
+ yield* this._getIterator(...args);
55
+ }
56
+ /**
57
+ * Returns an iterator over the values (alias of the default iterator).
58
+ *
59
+ * @returns An `IterableIterator<E>` over all elements.
60
+ * @remarks
61
+ * Creating the iterator is O(1); full iteration is Time O(n), Space O(1).
62
+ */
63
+ *values() {
64
+ for (const item of this) yield item;
65
+ }
66
+ /**
67
+ * Tests whether all elements satisfy the predicate.
68
+ *
69
+ * @template TReturn
70
+ * @param predicate Function invoked for each element with signature `(value, index, self)`.
71
+ * @param thisArg Optional `this` binding for the predicate.
72
+ * @returns `true` if every element passes; otherwise `false`.
73
+ *
74
+ * @remarks
75
+ * Time O(n) in the worst case; may exit early when the first failure is found. Space O(1).
76
+ */
77
+ every(predicate, thisArg) {
78
+ let index = 0;
79
+ for (const item of this) {
80
+ if (thisArg === void 0) {
81
+ if (!predicate(item, index++, this)) return false;
82
+ } else {
83
+ const fn = predicate;
84
+ if (!fn.call(thisArg, item, index++, this)) return false;
85
+ }
86
+ }
87
+ return true;
88
+ }
89
+ /**
90
+ * Tests whether at least one element satisfies the predicate.
91
+ *
92
+ * @param predicate Function invoked for each element with signature `(value, index, self)`.
93
+ * @param thisArg Optional `this` binding for the predicate.
94
+ * @returns `true` if any element passes; otherwise `false`.
95
+ *
96
+ * @remarks
97
+ * Time O(n) in the worst case; may exit early on first success. Space O(1).
98
+ */
99
+ some(predicate, thisArg) {
100
+ let index = 0;
101
+ for (const item of this) {
102
+ if (thisArg === void 0) {
103
+ if (predicate(item, index++, this)) return true;
104
+ } else {
105
+ const fn = predicate;
106
+ if (fn.call(thisArg, item, index++, this)) return true;
107
+ }
108
+ }
109
+ return false;
110
+ }
111
+ /**
112
+ * Invokes a callback for each element in iteration order.
113
+ *
114
+ * @param callbackfn Function invoked per element with signature `(value, index, self)`.
115
+ * @param thisArg Optional `this` binding for the callback.
116
+ * @returns `void`.
117
+ *
118
+ * @remarks
119
+ * Time O(n), Space O(1).
120
+ */
121
+ forEach(callbackfn, thisArg) {
122
+ let index = 0;
123
+ for (const item of this) {
124
+ if (thisArg === void 0) {
125
+ callbackfn(item, index++, this);
126
+ } else {
127
+ const fn = callbackfn;
128
+ fn.call(thisArg, item, index++, this);
129
+ }
130
+ }
131
+ }
132
+ // Implementation signature
133
+ find(predicate, thisArg) {
134
+ let index = 0;
135
+ for (const item of this) {
136
+ if (thisArg === void 0) {
137
+ if (predicate(item, index++, this)) return item;
138
+ } else {
139
+ const fn = predicate;
140
+ if (fn.call(thisArg, item, index++, this)) return item;
141
+ }
142
+ }
143
+ return;
144
+ }
145
+ /**
146
+ * Checks whether a strictly-equal element exists in the structure.
147
+ *
148
+ * @param element The element to test with `===` equality.
149
+ * @returns `true` if an equal element is found; otherwise `false`.
150
+ *
151
+ * @remarks
152
+ * Time O(n) in the worst case. Space O(1).
153
+ */
154
+ has(element) {
155
+ for (const ele of this) if (ele === element) return true;
156
+ return false;
157
+ }
158
+ /**
159
+ * Reduces all elements to a single accumulated value.
160
+ *
161
+ * @overload
162
+ * @param callbackfn Reducer of signature `(acc, value, index, self) => nextAcc`. The first element is used as the initial accumulator.
163
+ * @returns The final accumulated value typed as `E`.
164
+ *
165
+ * @overload
166
+ * @param callbackfn Reducer of signature `(acc, value, index, self) => nextAcc`.
167
+ * @param initialValue The initial accumulator value of type `E`.
168
+ * @returns The final accumulated value typed as `E`.
169
+ *
170
+ * @overload
171
+ * @template U The accumulator type when it differs from `E`.
172
+ * @param callbackfn Reducer of signature `(acc: U, value, index, self) => U`.
173
+ * @param initialValue The initial accumulator value of type `U`.
174
+ * @returns The final accumulated value typed as `U`.
175
+ *
176
+ * @remarks
177
+ * Time O(n), Space O(1). Throws if called on an empty structure without `initialValue`.
178
+ */
179
+ reduce(callbackfn, initialValue) {
180
+ let index = 0;
181
+ const iter = this[Symbol.iterator]();
182
+ let acc;
183
+ if (arguments.length >= 2) {
184
+ acc = initialValue;
185
+ } else {
186
+ const first = iter.next();
187
+ if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
188
+ acc = first.value;
189
+ index = 1;
190
+ }
191
+ for (const value of iter) {
192
+ acc = callbackfn(acc, value, index++, this);
193
+ }
194
+ return acc;
195
+ }
196
+ /**
197
+ * Materializes the elements into a new array.
198
+ *
199
+ * @returns A shallow array copy of the iteration order.
200
+ * @remarks
201
+ * Time O(n), Space O(n).
202
+ */
203
+ toArray() {
204
+ return [...this];
205
+ }
206
+ /**
207
+ * Returns a representation of the structure suitable for quick visualization.
208
+ * Defaults to an array of elements; subclasses may override to provide richer visuals.
209
+ *
210
+ * @returns A visual representation (array by default).
211
+ * @remarks
212
+ * Time O(n), Space O(n).
213
+ */
214
+ toVisual() {
215
+ return [...this];
216
+ }
217
+ /**
218
+ * Prints `toVisual()` to the console. Intended for quick debugging.
219
+ *
220
+ * @returns `void`.
221
+ * @remarks
222
+ * Time O(n) due to materialization, Space O(n) for the intermediate representation.
223
+ */
224
+ print() {
225
+ console.log(this.toVisual());
226
+ }
227
+ };
228
+
229
+ // src/common/error.ts
230
+ var ERR = {
231
+ // Range / index
232
+ indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
233
+ invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
234
+ // Type / argument
235
+ invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
236
+ comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
237
+ invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
238
+ notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
239
+ invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
240
+ invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
241
+ invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
242
+ reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
243
+ callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
244
+ // State / operation
245
+ invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
246
+ // Matrix
247
+ matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
248
+ matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
249
+ matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
250
+ matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
251
+ matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
252
+ };
253
+
254
+ // src/data-structures/trie/trie.ts
255
+ var TrieNode = class {
256
+ static {
257
+ __name(this, "TrieNode");
258
+ }
259
+ /**
260
+ * Create a Trie node with a character key.
261
+ * @remarks Time O(1), Space O(1)
262
+ * @returns New TrieNode instance.
263
+ */
264
+ constructor(key) {
265
+ this._key = key;
266
+ this._isEnd = false;
267
+ this._children = /* @__PURE__ */ new Map();
268
+ }
269
+ _key;
270
+ /**
271
+ * Get the character key of this node.
272
+ * @remarks Time O(1), Space O(1)
273
+ * @returns Character key string.
274
+ */
275
+ get key() {
276
+ return this._key;
277
+ }
278
+ /**
279
+ * Set the character key of this node.
280
+ * @remarks Time O(1), Space O(1)
281
+ * @param value - New character key.
282
+ * @returns void
283
+ */
284
+ set key(value) {
285
+ this._key = value;
286
+ }
287
+ _children;
288
+ /**
289
+ * Get the child map of this node.
290
+ * @remarks Time O(1), Space O(1)
291
+ * @returns Map from character to child node.
292
+ */
293
+ get children() {
294
+ return this._children;
295
+ }
296
+ /**
297
+ * Replace the child map of this node.
298
+ * @remarks Time O(1), Space O(1)
299
+ * @param value - New map of character → node.
300
+ * @returns void
301
+ */
302
+ set children(value) {
303
+ this._children = value;
304
+ }
305
+ _isEnd;
306
+ /**
307
+ * Check whether this node marks the end of a word.
308
+ * @remarks Time O(1), Space O(1)
309
+ * @returns True if this node ends a word.
310
+ */
311
+ get isEnd() {
312
+ return this._isEnd;
313
+ }
314
+ /**
315
+ * Mark this node as the end of a word or not.
316
+ * @remarks Time O(1), Space O(1)
317
+ * @param value - Whether this node ends a word.
318
+ * @returns void
319
+ */
320
+ set isEnd(value) {
321
+ this._isEnd = value;
322
+ }
323
+ };
324
+ var Trie = class extends IterableElementBase {
325
+ static {
326
+ __name(this, "Trie");
327
+ }
328
+ /**
329
+ * Create a Trie and optionally bulk-insert words.
330
+ * @remarks Time O(totalChars), Space O(totalChars)
331
+ * @param [words] - Iterable of strings (or raw records if toElementFn is provided).
332
+ * @param [options] - Options such as toElementFn and caseSensitive.
333
+ * @returns New Trie instance.
334
+ */
335
+ constructor(words = [], options) {
336
+ super(options);
337
+ if (options) {
338
+ const { caseSensitive } = options;
339
+ if (caseSensitive !== void 0) this._caseSensitive = caseSensitive;
340
+ }
341
+ if (words) {
342
+ this.addMany(words);
343
+ }
344
+ }
345
+ _size = 0;
346
+ /**
347
+ * Get the number of stored words.
348
+ * @remarks Time O(1), Space O(1)
349
+ * @returns Word count.
350
+ */
351
+ get size() {
352
+ return this._size;
353
+ }
354
+ _caseSensitive = true;
355
+ /**
356
+ * Get whether comparisons are case-sensitive.
357
+ * @remarks Time O(1), Space O(1)
358
+ * @returns True if case-sensitive.
359
+ */
360
+ get caseSensitive() {
361
+ return this._caseSensitive;
362
+ }
363
+ _root = new TrieNode("");
364
+ /**
365
+ * Get the root node.
366
+ * @remarks Time O(1), Space O(1)
367
+ * @returns Root TrieNode.
368
+ */
369
+ get root() {
370
+ return this._root;
371
+ }
372
+ /**
373
+ * (Protected) Get total count for base class iteration.
374
+ * @remarks Time O(1), Space O(1)
375
+ * @returns Total number of elements.
376
+ */
377
+ get _total() {
378
+ return this._size;
379
+ }
380
+ /**
381
+ * Insert one word into the trie.
382
+ * @remarks Time O(L), Space O(L)
383
+ * @param word - Word to insert.
384
+ * @returns True if the word was newly added.
385
+
386
+
387
+
388
+
389
+
390
+
391
+
392
+
393
+
394
+
395
+
396
+
397
+
398
+
399
+
400
+
401
+
402
+
403
+
404
+
405
+
406
+
407
+
408
+
409
+
410
+
411
+
412
+
413
+
414
+
415
+
416
+ * @example
417
+ * // basic Trie creation and add words
418
+ * // Create a simple Trie with initial words
419
+ * const trie = new Trie(['apple', 'app', 'apply']);
420
+ *
421
+ * // Verify size
422
+ * console.log(trie.size); // 3;
423
+ *
424
+ * // Check if words exist
425
+ * console.log(trie.has('apple')); // true;
426
+ * console.log(trie.has('app')); // true;
427
+ *
428
+ * // Add a new word
429
+ * trie.add('application');
430
+ * console.log(trie.size); // 4;
431
+ */
432
+ add(word) {
433
+ word = this._caseProcess(word);
434
+ let cur = this.root;
435
+ let isNewWord = false;
436
+ for (const c of word) {
437
+ let nodeC = cur.children.get(c);
438
+ if (!nodeC) {
439
+ nodeC = new TrieNode(c);
440
+ cur.children.set(c, nodeC);
441
+ }
442
+ cur = nodeC;
443
+ }
444
+ if (!cur.isEnd) {
445
+ isNewWord = true;
446
+ cur.isEnd = true;
447
+ this._size++;
448
+ }
449
+ return isNewWord;
450
+ }
451
+ /**
452
+ * Insert many words from an iterable.
453
+ * @remarks Time O(ΣL), Space O(ΣL)
454
+ * @param words - Iterable of strings (or raw records if toElementFn is provided).
455
+ * @returns Array of per-word 'added' flags.
456
+
457
+
458
+
459
+
460
+
461
+
462
+
463
+
464
+
465
+
466
+
467
+
468
+
469
+
470
+
471
+
472
+
473
+
474
+
475
+
476
+
477
+
478
+
479
+
480
+
481
+
482
+
483
+
484
+ * @example
485
+ * // Add multiple words
486
+ * const trie = new Trie();
487
+ * trie.addMany(['cat', 'car', 'card']);
488
+ * console.log(trie.has('cat')); // true;
489
+ * console.log(trie.has('car')); // true;
490
+ * console.log(trie.size); // 3;
491
+ */
492
+ addMany(words) {
493
+ const ans = [];
494
+ for (const word of words) {
495
+ if (this.toElementFn) {
496
+ ans.push(this.add(this.toElementFn(word)));
497
+ } else {
498
+ ans.push(this.add(word));
499
+ }
500
+ }
501
+ return ans;
502
+ }
503
+ /**
504
+ * Check whether a word exists.
505
+ * @remarks Time O(L), Space O(1)
506
+ * @param word - Word to search for.
507
+ * @returns True if present.
508
+
509
+
510
+
511
+
512
+
513
+
514
+
515
+
516
+
517
+
518
+
519
+
520
+
521
+
522
+
523
+
524
+
525
+
526
+
527
+
528
+
529
+
530
+
531
+
532
+
533
+
534
+
535
+
536
+
537
+
538
+
539
+ * @example
540
+ * // Check if a word exists
541
+ * const dict = new Trie(['apple', 'app', 'application']);
542
+ *
543
+ * console.log(dict.has('app')); // true;
544
+ * console.log(dict.has('apple')); // true;
545
+ * console.log(dict.has('ap')); // false;
546
+ */
547
+ has(word) {
548
+ word = this._caseProcess(word);
549
+ let cur = this.root;
550
+ for (const c of word) {
551
+ const nodeC = cur.children.get(c);
552
+ if (!nodeC) return false;
553
+ cur = nodeC;
554
+ }
555
+ return cur.isEnd;
556
+ }
557
+ /**
558
+ * Check whether the trie is empty.
559
+ * @remarks Time O(1), Space O(1)
560
+ * @returns True if size is 0.
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
+
586
+
587
+
588
+
589
+ * @example
590
+ * // Check if empty
591
+ * const trie = new Trie();
592
+ * console.log(trie.isEmpty()); // true;
593
+ * trie.add('word');
594
+ * console.log(trie.isEmpty()); // false;
595
+ */
596
+ isEmpty() {
597
+ return this._size === 0;
598
+ }
599
+ /**
600
+ * Remove all words and reset to a fresh root.
601
+ * @remarks Time O(1), Space O(1)
602
+ * @returns void
603
+
604
+
605
+
606
+
607
+
608
+
609
+
610
+
611
+
612
+
613
+
614
+
615
+
616
+
617
+
618
+
619
+
620
+
621
+
622
+
623
+
624
+
625
+
626
+
627
+
628
+
629
+
630
+
631
+ * @example
632
+ * // Remove all words
633
+ * const trie = new Trie(['a', 'b', 'c']);
634
+ * trie.clear();
635
+ * console.log(trie.isEmpty()); // true;
636
+ */
637
+ clear() {
638
+ this._size = 0;
639
+ this._root = new TrieNode("");
640
+ }
641
+ /**
642
+ * Delete one word if present.
643
+ * @remarks Time O(L), Space O(1)
644
+ * @param word - Word to delete.
645
+ * @returns True if a word was removed.
646
+
647
+
648
+
649
+
650
+
651
+
652
+
653
+
654
+
655
+
656
+
657
+
658
+
659
+
660
+
661
+
662
+
663
+
664
+
665
+
666
+
667
+
668
+
669
+
670
+
671
+
672
+
673
+
674
+
675
+
676
+
677
+ * @example
678
+ * // Trie delete and iteration
679
+ * const trie = new Trie(['car', 'card', 'care', 'careful', 'can', 'cat']);
680
+ *
681
+ * // Delete a word
682
+ * trie.delete('card');
683
+ * console.log(trie.has('card')); // false;
684
+ *
685
+ * // Word with same prefix still exists
686
+ * console.log(trie.has('care')); // true;
687
+ *
688
+ * // Size decreased
689
+ * console.log(trie.size); // 5;
690
+ *
691
+ * // Iterate through all words
692
+ * const allWords = [...trie];
693
+ * console.log(allWords.length); // 5;
694
+ */
695
+ delete(word) {
696
+ word = this._caseProcess(word);
697
+ let isDeleted = false;
698
+ const dfs = /* @__PURE__ */ __name((cur, i) => {
699
+ const char = word[i];
700
+ const child = cur.children.get(char);
701
+ if (child) {
702
+ if (i === word.length - 1) {
703
+ if (child.isEnd) {
704
+ if (child.children.size > 0) {
705
+ child.isEnd = false;
706
+ } else {
707
+ cur.children.delete(char);
708
+ }
709
+ isDeleted = true;
710
+ return true;
711
+ }
712
+ return false;
713
+ }
714
+ const res = dfs(child, i + 1);
715
+ if (res && !cur.isEnd && child.children.size === 0) {
716
+ cur.children.delete(char);
717
+ return true;
718
+ }
719
+ return false;
720
+ }
721
+ return false;
722
+ }, "dfs");
723
+ dfs(this.root, 0);
724
+ if (isDeleted) {
725
+ this._size--;
726
+ }
727
+ return isDeleted;
728
+ }
729
+ /**
730
+ * Compute the height (max depth) of the trie.
731
+ * @remarks Time O(N), Space O(H)
732
+ * @returns Maximum depth from root to a leaf.
733
+ */
734
+ getHeight() {
735
+ const startNode = this.root;
736
+ let maxDepth = 0;
737
+ if (startNode) {
738
+ const bfs = /* @__PURE__ */ __name((node, level) => {
739
+ if (level > maxDepth) {
740
+ maxDepth = level;
741
+ }
742
+ const { children } = node;
743
+ if (children) {
744
+ for (const child of children.entries()) {
745
+ bfs(child[1], level + 1);
746
+ }
747
+ }
748
+ }, "bfs");
749
+ bfs(startNode, 0);
750
+ }
751
+ return maxDepth;
752
+ }
753
+ /**
754
+ * Check whether input is a proper prefix of at least one word.
755
+ * @remarks Time O(L), Space O(1)
756
+ * @param input - String to test as prefix.
757
+ * @returns True if input is a prefix but not a full word.
758
+ */
759
+ hasPurePrefix(input) {
760
+ input = this._caseProcess(input);
761
+ let cur = this.root;
762
+ for (const c of input) {
763
+ const nodeC = cur.children.get(c);
764
+ if (!nodeC) return false;
765
+ cur = nodeC;
766
+ }
767
+ return !cur.isEnd;
768
+ }
769
+ /**
770
+ * Check whether any word starts with input.
771
+ * @remarks Time O(L), Space O(1)
772
+ * @param input - String to test as prefix.
773
+ * @returns True if input matches a path from root.
774
+
775
+
776
+
777
+
778
+
779
+
780
+
781
+
782
+
783
+
784
+
785
+
786
+
787
+
788
+
789
+
790
+
791
+
792
+
793
+
794
+
795
+
796
+
797
+
798
+
799
+
800
+
801
+
802
+
803
+
804
+
805
+ * @example
806
+ * // Check if a prefix exists
807
+ * const trie = new Trie(['hello', 'help', 'world']);
808
+ *
809
+ * console.log(trie.hasPrefix('hel')); // true;
810
+ * console.log(trie.hasPrefix('wor')); // true;
811
+ * console.log(trie.hasPrefix('xyz')); // false;
812
+ */
813
+ hasPrefix(input) {
814
+ input = this._caseProcess(input);
815
+ let cur = this.root;
816
+ for (const c of input) {
817
+ const nodeC = cur.children.get(c);
818
+ if (!nodeC) return false;
819
+ cur = nodeC;
820
+ }
821
+ return true;
822
+ }
823
+ /**
824
+ * Check whether the trie’s longest common prefix equals input.
825
+ * @remarks Time O(min(H,L)), Space O(1)
826
+ * @param input - Candidate longest common prefix.
827
+ * @returns True if input equals the common prefix.
828
+ */
829
+ hasCommonPrefix(input) {
830
+ input = this._caseProcess(input);
831
+ let commonPre = "";
832
+ const dfs = /* @__PURE__ */ __name((cur) => {
833
+ commonPre += cur.key;
834
+ if (commonPre === input) return;
835
+ if (cur.isEnd) return;
836
+ if (cur && cur.children && cur.children.size === 1) dfs(Array.from(cur.children.values())[0]);
837
+ else return;
838
+ }, "dfs");
839
+ dfs(this.root);
840
+ return commonPre === input;
841
+ }
842
+ /**
843
+ * Return the longest common prefix among all words.
844
+ * @remarks Time O(H), Space O(1)
845
+ * @returns The longest common prefix string.
846
+
847
+
848
+
849
+
850
+
851
+
852
+
853
+
854
+
855
+
856
+
857
+
858
+
859
+
860
+
861
+
862
+
863
+
864
+
865
+
866
+
867
+
868
+
869
+
870
+
871
+
872
+
873
+
874
+
875
+
876
+
877
+ * @example
878
+ * // Find shared prefix
879
+ * const trie = new Trie(['flower', 'flow', 'flight']);
880
+ *
881
+ * console.log(trie.getLongestCommonPrefix()); // 'fl';
882
+ */
883
+ getLongestCommonPrefix() {
884
+ let commonPre = "";
885
+ const dfs = /* @__PURE__ */ __name((cur) => {
886
+ commonPre += cur.key;
887
+ if (cur.isEnd) return;
888
+ if (cur && cur.children && cur.children.size === 1) dfs(Array.from(cur.children.values())[0]);
889
+ else return;
890
+ }, "dfs");
891
+ dfs(this.root);
892
+ return commonPre;
893
+ }
894
+ /**
895
+ * Collect words under a prefix up to a maximum count.
896
+ * @remarks Time O(K·L), Space O(K·L)
897
+ * @param [prefix] - Prefix to match; default empty string for root.
898
+ * @param [max] - Maximum number of words to return; default is Number.MAX_SAFE_INTEGER.
899
+ * @param [isAllWhenEmptyPrefix] - When true, collect from root even if prefix is empty.
900
+ * @returns Array of collected words (at most max).
901
+
902
+
903
+
904
+
905
+
906
+
907
+
908
+
909
+
910
+
911
+
912
+
913
+
914
+
915
+
916
+
917
+
918
+
919
+
920
+
921
+
922
+
923
+
924
+
925
+
926
+
927
+
928
+
929
+
930
+
931
+
932
+ * @example
933
+ * // Trie getWords and prefix search
934
+ * const trie = new Trie(['apple', 'app', 'apply', 'application', 'apricot']);
935
+ *
936
+ * // Get all words with prefix 'app'
937
+ * const appWords = trie.getWords('app');
938
+ * console.log(appWords); // contains 'app';
939
+ * console.log(appWords); // contains 'apple';
940
+ * console.log(appWords); // contains 'apply';
941
+ * console.log(appWords); // contains 'application';
942
+ * expect(appWords).not.toContain('apricot');
943
+ */
944
+ getWords(prefix = "", max = Number.MAX_SAFE_INTEGER, isAllWhenEmptyPrefix = false) {
945
+ prefix = this._caseProcess(prefix);
946
+ const words = [];
947
+ let found = 0;
948
+ const dfs = /* @__PURE__ */ __name((node, word) => {
949
+ for (const [char, childNode] of node.children) {
950
+ if (found >= max) return;
951
+ dfs(childNode, word + char);
952
+ }
953
+ if (node.isEnd) {
954
+ if (found >= max) return;
955
+ words.push(word);
956
+ found++;
957
+ }
958
+ }, "dfs");
959
+ let startNode = this.root;
960
+ if (prefix) {
961
+ for (const c of prefix) {
962
+ const nodeC = startNode.children.get(c);
963
+ if (nodeC) {
964
+ startNode = nodeC;
965
+ } else {
966
+ return [];
967
+ }
968
+ }
969
+ }
970
+ if (isAllWhenEmptyPrefix || startNode !== this.root) dfs(startNode, prefix);
971
+ return words;
972
+ }
973
+ /**
974
+ * Deep clone this trie by iterating and inserting all words.
975
+ * @remarks Time O(ΣL), Space O(ΣL)
976
+ * @returns A new trie with the same words and options.
977
+
978
+
979
+
980
+
981
+
982
+
983
+
984
+
985
+
986
+
987
+
988
+
989
+
990
+
991
+
992
+
993
+
994
+
995
+
996
+
997
+
998
+
999
+
1000
+
1001
+
1002
+
1003
+
1004
+
1005
+ * @example
1006
+ * // Create independent copy
1007
+ * const trie = new Trie(['hello', 'world']);
1008
+ * const copy = trie.clone();
1009
+ * copy.delete('hello');
1010
+ * console.log(trie.has('hello')); // true;
1011
+ */
1012
+ clone() {
1013
+ const next = this._createInstance();
1014
+ for (const x of this) next.add(x);
1015
+ return next;
1016
+ }
1017
+ /**
1018
+ * Filter words into a new trie of the same class.
1019
+ * @remarks Time O(ΣL), Space O(ΣL)
1020
+ * @param predicate - Predicate (word, index, trie) → boolean to keep word.
1021
+ * @param [thisArg] - Value for `this` inside the predicate.
1022
+ * @returns A new trie containing words that satisfy the predicate.
1023
+
1024
+
1025
+
1026
+
1027
+
1028
+
1029
+
1030
+
1031
+
1032
+
1033
+
1034
+
1035
+
1036
+
1037
+
1038
+
1039
+
1040
+
1041
+
1042
+
1043
+
1044
+
1045
+
1046
+
1047
+
1048
+
1049
+
1050
+
1051
+ * @example
1052
+ * // Filter words
1053
+ * const trie = new Trie(['cat', 'car', 'dog', 'card']);
1054
+ * const result = trie.filter(w => w.startsWith('ca'));
1055
+ * console.log(result.size); // 3;
1056
+ */
1057
+ filter(predicate, thisArg) {
1058
+ const results = this._createInstance();
1059
+ let index = 0;
1060
+ for (const word of this) {
1061
+ if (predicate.call(thisArg, word, index, this)) {
1062
+ results.add(word);
1063
+ }
1064
+ index++;
1065
+ }
1066
+ return results;
1067
+ }
1068
+ map(callback, options, thisArg) {
1069
+ const newTrie = this._createLike([], options);
1070
+ let i = 0;
1071
+ for (const x of this) {
1072
+ const v = thisArg === void 0 ? callback(x, i++, this) : callback.call(thisArg, x, i++, this);
1073
+ if (typeof v !== "string") {
1074
+ throw new TypeError(ERR.callbackReturnType("string", typeof v, "Trie.map"));
1075
+ }
1076
+ newTrie.add(v);
1077
+ }
1078
+ return newTrie;
1079
+ }
1080
+ /**
1081
+ * Map words into a new trie of the same element type.
1082
+ * @remarks Time O(ΣL), Space O(ΣL)
1083
+ * @param callback - Mapping function (word, index, trie) → string.
1084
+ * @param [thisArg] - Value for `this` inside the callback.
1085
+ * @returns A new trie with mapped words.
1086
+ */
1087
+ mapSame(callback, thisArg) {
1088
+ const next = this._createInstance();
1089
+ let i = 0;
1090
+ for (const key of this) {
1091
+ const mapped = thisArg === void 0 ? callback(key, i++, this) : callback.call(thisArg, key, i++, this);
1092
+ next.add(mapped);
1093
+ }
1094
+ return next;
1095
+ }
1096
+ /**
1097
+ * (Protected) Create an empty instance of the same concrete class.
1098
+ * @remarks Time O(1), Space O(1)
1099
+ * @param [options] - Options forwarded to the constructor.
1100
+ * @returns An empty like-kind trie instance.
1101
+ */
1102
+ _createInstance(options) {
1103
+ const Ctor = this.constructor;
1104
+ return new Ctor([], {
1105
+ toElementFn: this.toElementFn,
1106
+ caseSensitive: this.caseSensitive,
1107
+ ...options ?? {}
1108
+ });
1109
+ }
1110
+ /**
1111
+ * (Protected) Create a like-kind trie and seed it from an iterable.
1112
+ * @remarks Time O(ΣL), Space O(ΣL)
1113
+ * @template RM
1114
+ * @param [elements] - Iterable used to seed the new trie.
1115
+ * @param [options] - Options forwarded to the constructor.
1116
+ * @returns A like-kind Trie instance.
1117
+ */
1118
+ _createLike(elements = [], options) {
1119
+ const Ctor = this.constructor;
1120
+ return new Ctor(elements, options);
1121
+ }
1122
+ /**
1123
+ * (Protected) Spawn an empty like-kind trie instance.
1124
+ * @remarks Time O(1), Space O(1)
1125
+ * @template RM
1126
+ * @param [options] - Options forwarded to the constructor.
1127
+ * @returns An empty like-kind Trie instance.
1128
+ */
1129
+ _spawnLike(options) {
1130
+ return this._createLike([], options);
1131
+ }
1132
+ /**
1133
+ * (Protected) Iterate all words in lexicographic order of edges.
1134
+ * @remarks Time O(ΣL), Space O(H)
1135
+ * @returns Iterator of words.
1136
+ */
1137
+ *_getIterator() {
1138
+ function* _dfs(node, path) {
1139
+ if (node.isEnd) {
1140
+ yield path;
1141
+ }
1142
+ for (const [char, childNode] of node.children) {
1143
+ yield* _dfs(childNode, path + char);
1144
+ }
1145
+ }
1146
+ __name(_dfs, "_dfs");
1147
+ yield* _dfs(this.root, "");
1148
+ }
1149
+ /**
1150
+ * (Protected) Normalize a string according to case sensitivity.
1151
+ * @remarks Time O(L), Space O(L)
1152
+ * @param str - Input string to normalize.
1153
+ * @returns Normalized string based on caseSensitive.
1154
+ */
1155
+ _caseProcess(str) {
1156
+ if (!this._caseSensitive) {
1157
+ str = str.toLowerCase();
1158
+ }
1159
+ return str;
1160
+ }
1161
+ };
1162
+ /**
1163
+ * data-structure-typed
1164
+ *
1165
+ * @author Pablo Zeng
1166
+ * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
1167
+ * @license MIT License
1168
+ */
1169
+
1170
+ exports.Trie = Trie;
1171
+ exports.TrieNode = TrieNode;
1172
+ //# sourceMappingURL=trie.cjs.map
1173
+ //# sourceMappingURL=trie.cjs.map