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