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,1223 @@
1
+ 'use strict';
2
+
3
+ var __defProp = Object.defineProperty;
4
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
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 {
39
+ static {
40
+ __name(this, "IterableElementBase");
41
+ }
42
+ /**
43
+ * Create a new iterable base.
44
+ *
45
+ * @param options Optional behavior overrides. When provided, a `toElementFn`
46
+ * is used to convert a raw element (`R`) into a public element (`E`).
47
+ *
48
+ * @remarks
49
+ * Time O(1), Space O(1).
50
+ */
51
+ constructor(options) {
52
+ if (options) {
53
+ const { toElementFn } = options;
54
+ if (typeof toElementFn === "function") this._toElementFn = toElementFn;
55
+ else if (toElementFn) raise(TypeError, "toElementFn must be a function type");
56
+ }
57
+ }
58
+ /**
59
+ * The converter used to transform a raw element (`R`) into a public element (`E`).
60
+ *
61
+ * @remarks
62
+ * Time O(1), Space O(1).
63
+ */
64
+ _toElementFn;
65
+ /**
66
+ * Exposes the current `toElementFn`, if configured.
67
+ *
68
+ * @returns The converter function or `undefined` when not set.
69
+ * @remarks
70
+ * Time O(1), Space O(1).
71
+ */
72
+ get toElementFn() {
73
+ return this._toElementFn;
74
+ }
75
+ /**
76
+ * Returns an iterator over the structure's elements.
77
+ *
78
+ * @param args Optional iterator arguments forwarded to the internal iterator.
79
+ * @returns An `IterableIterator<E>` that yields the elements in traversal order.
80
+ *
81
+ * @remarks
82
+ * Producing the iterator is O(1); consuming the entire iterator is Time O(n) with O(1) extra space.
83
+ */
84
+ *[Symbol.iterator](...args) {
85
+ yield* this._getIterator(...args);
86
+ }
87
+ /**
88
+ * Returns an iterator over the values (alias of the default iterator).
89
+ *
90
+ * @returns An `IterableIterator<E>` over all elements.
91
+ * @remarks
92
+ * Creating the iterator is O(1); full iteration is Time O(n), Space O(1).
93
+ */
94
+ *values() {
95
+ for (const item of this) yield item;
96
+ }
97
+ /**
98
+ * Tests whether all elements satisfy the predicate.
99
+ *
100
+ * @template TReturn
101
+ * @param predicate Function invoked for each element with signature `(value, index, self)`.
102
+ * @param thisArg Optional `this` binding for the predicate.
103
+ * @returns `true` if every element passes; otherwise `false`.
104
+ *
105
+ * @remarks
106
+ * Time O(n) in the worst case; may exit early when the first failure is found. Space O(1).
107
+ */
108
+ every(predicate, thisArg) {
109
+ let index = 0;
110
+ for (const item of this) {
111
+ if (thisArg === void 0) {
112
+ if (!predicate(item, index++, this)) return false;
113
+ } else {
114
+ const fn = predicate;
115
+ if (!fn.call(thisArg, item, index++, this)) return false;
116
+ }
117
+ }
118
+ return true;
119
+ }
120
+ /**
121
+ * Tests whether at least one element satisfies the predicate.
122
+ *
123
+ * @param predicate Function invoked for each element with signature `(value, index, self)`.
124
+ * @param thisArg Optional `this` binding for the predicate.
125
+ * @returns `true` if any element passes; otherwise `false`.
126
+ *
127
+ * @remarks
128
+ * Time O(n) in the worst case; may exit early on first success. Space O(1).
129
+ */
130
+ some(predicate, thisArg) {
131
+ let index = 0;
132
+ for (const item of this) {
133
+ if (thisArg === void 0) {
134
+ if (predicate(item, index++, this)) return true;
135
+ } else {
136
+ const fn = predicate;
137
+ if (fn.call(thisArg, item, index++, this)) return true;
138
+ }
139
+ }
140
+ return false;
141
+ }
142
+ /**
143
+ * Invokes a callback for each element in iteration order.
144
+ *
145
+ * @param callbackfn Function invoked per element with signature `(value, index, self)`.
146
+ * @param thisArg Optional `this` binding for the callback.
147
+ * @returns `void`.
148
+ *
149
+ * @remarks
150
+ * Time O(n), Space O(1).
151
+ */
152
+ forEach(callbackfn, thisArg) {
153
+ let index = 0;
154
+ for (const item of this) {
155
+ if (thisArg === void 0) {
156
+ callbackfn(item, index++, this);
157
+ } else {
158
+ const fn = callbackfn;
159
+ fn.call(thisArg, item, index++, this);
160
+ }
161
+ }
162
+ }
163
+ // Implementation signature
164
+ find(predicate, thisArg) {
165
+ let index = 0;
166
+ for (const item of this) {
167
+ if (thisArg === void 0) {
168
+ if (predicate(item, index++, this)) return item;
169
+ } else {
170
+ const fn = predicate;
171
+ if (fn.call(thisArg, item, index++, this)) return item;
172
+ }
173
+ }
174
+ return;
175
+ }
176
+ /**
177
+ * Checks whether a strictly-equal element exists in the structure.
178
+ *
179
+ * @param element The element to test with `===` equality.
180
+ * @returns `true` if an equal element is found; otherwise `false`.
181
+ *
182
+ * @remarks
183
+ * Time O(n) in the worst case. Space O(1).
184
+ */
185
+ has(element) {
186
+ for (const ele of this) if (ele === element) return true;
187
+ return false;
188
+ }
189
+ /**
190
+ * Reduces all elements to a single accumulated value.
191
+ *
192
+ * @overload
193
+ * @param callbackfn Reducer of signature `(acc, value, index, self) => nextAcc`. The first element is used as the initial accumulator.
194
+ * @returns The final accumulated value typed as `E`.
195
+ *
196
+ * @overload
197
+ * @param callbackfn Reducer of signature `(acc, value, index, self) => nextAcc`.
198
+ * @param initialValue The initial accumulator value of type `E`.
199
+ * @returns The final accumulated value typed as `E`.
200
+ *
201
+ * @overload
202
+ * @template U The accumulator type when it differs from `E`.
203
+ * @param callbackfn Reducer of signature `(acc: U, value, index, self) => U`.
204
+ * @param initialValue The initial accumulator value of type `U`.
205
+ * @returns The final accumulated value typed as `U`.
206
+ *
207
+ * @remarks
208
+ * Time O(n), Space O(1). Throws if called on an empty structure without `initialValue`.
209
+ */
210
+ reduce(callbackfn, initialValue) {
211
+ let index = 0;
212
+ const iter = this[Symbol.iterator]();
213
+ let acc;
214
+ if (arguments.length >= 2) {
215
+ acc = initialValue;
216
+ } else {
217
+ const first = iter.next();
218
+ if (first.done) raise(TypeError, "Reduce of empty structure with no initial value");
219
+ acc = first.value;
220
+ index = 1;
221
+ }
222
+ for (const value of iter) {
223
+ acc = callbackfn(acc, value, index++, this);
224
+ }
225
+ return acc;
226
+ }
227
+ /**
228
+ * Materializes the elements into a new array.
229
+ *
230
+ * @returns A shallow array copy of the iteration order.
231
+ * @remarks
232
+ * Time O(n), Space O(n).
233
+ */
234
+ toArray() {
235
+ return [...this];
236
+ }
237
+ /**
238
+ * Returns a representation of the structure suitable for quick visualization.
239
+ * Defaults to an array of elements; subclasses may override to provide richer visuals.
240
+ *
241
+ * @returns A visual representation (array by default).
242
+ * @remarks
243
+ * Time O(n), Space O(n).
244
+ */
245
+ toVisual() {
246
+ return [...this];
247
+ }
248
+ /**
249
+ * Prints `toVisual()` to the console. Intended for quick debugging.
250
+ *
251
+ * @returns `void`.
252
+ * @remarks
253
+ * Time O(n) due to materialization, Space O(n) for the intermediate representation.
254
+ */
255
+ print() {
256
+ console.log(this.toVisual());
257
+ }
258
+ };
259
+
260
+ // src/data-structures/trie/trie.ts
261
+ var TrieNode = class {
262
+ static {
263
+ __name(this, "TrieNode");
264
+ }
265
+ /**
266
+ * Create a Trie node with a character key.
267
+ * @remarks Time O(1), Space O(1)
268
+ * @returns New TrieNode instance.
269
+ */
270
+ constructor(key) {
271
+ this._key = key;
272
+ this._isEnd = false;
273
+ this._children = /* @__PURE__ */ new Map();
274
+ }
275
+ _key;
276
+ /**
277
+ * Get the character key of this node.
278
+ * @remarks Time O(1), Space O(1)
279
+ * @returns Character key string.
280
+ */
281
+ get key() {
282
+ return this._key;
283
+ }
284
+ /**
285
+ * Set the character key of this node.
286
+ * @remarks Time O(1), Space O(1)
287
+ * @param value - New character key.
288
+ * @returns void
289
+ */
290
+ set key(value) {
291
+ this._key = value;
292
+ }
293
+ _children;
294
+ /**
295
+ * Get the child map of this node.
296
+ * @remarks Time O(1), Space O(1)
297
+ * @returns Map from character to child node.
298
+ */
299
+ get children() {
300
+ return this._children;
301
+ }
302
+ /**
303
+ * Replace the child map of this node.
304
+ * @remarks Time O(1), Space O(1)
305
+ * @param value - New map of character → node.
306
+ * @returns void
307
+ */
308
+ set children(value) {
309
+ this._children = value;
310
+ }
311
+ _isEnd;
312
+ /**
313
+ * Check whether this node marks the end of a word.
314
+ * @remarks Time O(1), Space O(1)
315
+ * @returns True if this node ends a word.
316
+ */
317
+ get isEnd() {
318
+ return this._isEnd;
319
+ }
320
+ /**
321
+ * Mark this node as the end of a word or not.
322
+ * @remarks Time O(1), Space O(1)
323
+ * @param value - Whether this node ends a word.
324
+ * @returns void
325
+ */
326
+ set isEnd(value) {
327
+ this._isEnd = value;
328
+ }
329
+ };
330
+ var Trie = class extends IterableElementBase {
331
+ static {
332
+ __name(this, "Trie");
333
+ }
334
+ /**
335
+ * Create a Trie and optionally bulk-insert words.
336
+ * @remarks Time O(totalChars), Space O(totalChars)
337
+ * @param [words] - Iterable of strings (or raw records if toElementFn is provided).
338
+ * @param [options] - Options such as toElementFn and caseSensitive.
339
+ * @returns New Trie instance.
340
+ */
341
+ constructor(words = [], options) {
342
+ super(options);
343
+ if (options) {
344
+ const { caseSensitive } = options;
345
+ if (caseSensitive !== void 0) this._caseSensitive = caseSensitive;
346
+ }
347
+ if (words) {
348
+ this.addMany(words);
349
+ }
350
+ }
351
+ _size = 0;
352
+ /**
353
+ * Get the number of stored words.
354
+ * @remarks Time O(1), Space O(1)
355
+ * @returns Word count.
356
+ */
357
+ get size() {
358
+ return this._size;
359
+ }
360
+ _caseSensitive = true;
361
+ /**
362
+ * Get whether comparisons are case-sensitive.
363
+ * @remarks Time O(1), Space O(1)
364
+ * @returns True if case-sensitive.
365
+ */
366
+ get caseSensitive() {
367
+ return this._caseSensitive;
368
+ }
369
+ _root = new TrieNode("");
370
+ /**
371
+ * Get the root node.
372
+ * @remarks Time O(1), Space O(1)
373
+ * @returns Root TrieNode.
374
+ */
375
+ get root() {
376
+ return this._root;
377
+ }
378
+ /**
379
+ * (Protected) Get total count for base class iteration.
380
+ * @remarks Time O(1), Space O(1)
381
+ * @returns Total number of elements.
382
+ */
383
+ get _total() {
384
+ return this._size;
385
+ }
386
+ /**
387
+ * Insert one word into the trie.
388
+ * @remarks Time O(L), Space O(L)
389
+ * @param word - Word to insert.
390
+ * @returns True if the word was newly added.
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
+
422
+
423
+
424
+
425
+
426
+ * @example
427
+ * // basic Trie creation and add words
428
+ * // Create a simple Trie with initial words
429
+ * const trie = new Trie(['apple', 'app', 'apply']);
430
+ *
431
+ * // Verify size
432
+ * console.log(trie.size); // 3;
433
+ *
434
+ * // Check if words exist
435
+ * console.log(trie.has('apple')); // true;
436
+ * console.log(trie.has('app')); // true;
437
+ *
438
+ * // Add a new word
439
+ * trie.add('application');
440
+ * console.log(trie.size); // 4;
441
+ */
442
+ add(word) {
443
+ word = this._caseProcess(word);
444
+ let cur = this.root;
445
+ let isNewWord = false;
446
+ for (const c of word) {
447
+ let nodeC = cur.children.get(c);
448
+ if (!nodeC) {
449
+ nodeC = new TrieNode(c);
450
+ cur.children.set(c, nodeC);
451
+ }
452
+ cur = nodeC;
453
+ }
454
+ if (!cur.isEnd) {
455
+ isNewWord = true;
456
+ cur.isEnd = true;
457
+ this._size++;
458
+ }
459
+ return isNewWord;
460
+ }
461
+ /**
462
+ * Insert many words from an iterable.
463
+ * @remarks Time O(ΣL), Space O(ΣL)
464
+ * @param words - Iterable of strings (or raw records if toElementFn is provided).
465
+ * @returns Array of per-word 'added' flags.
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
+
494
+
495
+
496
+
497
+
498
+ * @example
499
+ * // Add multiple words
500
+ * const trie = new Trie();
501
+ * trie.addMany(['cat', 'car', 'card']);
502
+ * console.log(trie.has('cat')); // true;
503
+ * console.log(trie.has('car')); // true;
504
+ * console.log(trie.size); // 3;
505
+ */
506
+ addMany(words) {
507
+ const ans = [];
508
+ for (const word of words) {
509
+ if (this.toElementFn) {
510
+ ans.push(this.add(this.toElementFn(word)));
511
+ } else {
512
+ ans.push(this.add(word));
513
+ }
514
+ }
515
+ return ans;
516
+ }
517
+ /**
518
+ * Check whether a word exists.
519
+ * @remarks Time O(L), Space O(1)
520
+ * @param word - Word to search for.
521
+ * @returns True if present.
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
+
553
+
554
+
555
+
556
+
557
+ * @example
558
+ * // Check if a word exists
559
+ * const dict = new Trie(['apple', 'app', 'application']);
560
+ *
561
+ * console.log(dict.has('app')); // true;
562
+ * console.log(dict.has('apple')); // true;
563
+ * console.log(dict.has('ap')); // false;
564
+ */
565
+ has(word) {
566
+ word = this._caseProcess(word);
567
+ let cur = this.root;
568
+ for (const c of word) {
569
+ const nodeC = cur.children.get(c);
570
+ if (!nodeC) return false;
571
+ cur = nodeC;
572
+ }
573
+ return cur.isEnd;
574
+ }
575
+ /**
576
+ * Check whether the trie is empty.
577
+ * @remarks Time O(1), Space O(1)
578
+ * @returns True if size is 0.
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
+
607
+
608
+
609
+
610
+
611
+ * @example
612
+ * // Check if empty
613
+ * const trie = new Trie();
614
+ * console.log(trie.isEmpty()); // true;
615
+ * trie.add('word');
616
+ * console.log(trie.isEmpty()); // false;
617
+ */
618
+ isEmpty() {
619
+ return this._size === 0;
620
+ }
621
+ /**
622
+ * Remove all words and reset to a fresh root.
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
+ * @example
658
+ * // Remove all words
659
+ * const trie = new Trie(['a', 'b', 'c']);
660
+ * trie.clear();
661
+ * console.log(trie.isEmpty()); // true;
662
+ */
663
+ clear() {
664
+ this._size = 0;
665
+ this._root = new TrieNode("");
666
+ }
667
+ /**
668
+ * Delete one word if present.
669
+ * @remarks Time O(L), Space O(1)
670
+ * @param word - Word to delete.
671
+ * @returns True if a word was removed.
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
+
705
+
706
+
707
+ * @example
708
+ * // Trie delete and iteration
709
+ * const trie = new Trie(['car', 'card', 'care', 'careful', 'can', 'cat']);
710
+ *
711
+ * // Delete a word
712
+ * trie.delete('card');
713
+ * console.log(trie.has('card')); // false;
714
+ *
715
+ * // Word with same prefix still exists
716
+ * console.log(trie.has('care')); // true;
717
+ *
718
+ * // Size decreased
719
+ * console.log(trie.size); // 5;
720
+ *
721
+ * // Iterate through all words
722
+ * const allWords = [...trie];
723
+ * console.log(allWords.length); // 5;
724
+ */
725
+ delete(word) {
726
+ word = this._caseProcess(word);
727
+ let isDeleted = false;
728
+ const dfs = /* @__PURE__ */ __name((cur, i) => {
729
+ const char = word[i];
730
+ const child = cur.children.get(char);
731
+ if (child) {
732
+ if (i === word.length - 1) {
733
+ if (child.isEnd) {
734
+ if (child.children.size > 0) {
735
+ child.isEnd = false;
736
+ } else {
737
+ cur.children.delete(char);
738
+ }
739
+ isDeleted = true;
740
+ return true;
741
+ }
742
+ return false;
743
+ }
744
+ const res = dfs(child, i + 1);
745
+ if (res && !cur.isEnd && child.children.size === 0) {
746
+ cur.children.delete(char);
747
+ return true;
748
+ }
749
+ return false;
750
+ }
751
+ return false;
752
+ }, "dfs");
753
+ dfs(this.root, 0);
754
+ if (isDeleted) {
755
+ this._size--;
756
+ }
757
+ return isDeleted;
758
+ }
759
+ /**
760
+ * Compute the height (max depth) of the trie.
761
+ * @remarks Time O(N), Space O(H)
762
+ * @returns Maximum depth from root to a leaf.
763
+ */
764
+ getHeight() {
765
+ const startNode = this.root;
766
+ let maxDepth = 0;
767
+ if (startNode) {
768
+ const bfs = /* @__PURE__ */ __name((node, level) => {
769
+ if (level > maxDepth) {
770
+ maxDepth = level;
771
+ }
772
+ const { children } = node;
773
+ if (children) {
774
+ for (const child of children.entries()) {
775
+ bfs(child[1], level + 1);
776
+ }
777
+ }
778
+ }, "bfs");
779
+ bfs(startNode, 0);
780
+ }
781
+ return maxDepth;
782
+ }
783
+ /**
784
+ * Check whether input is a proper prefix of at least one word.
785
+ * @remarks Time O(L), Space O(1)
786
+ * @param input - String to test as prefix.
787
+ * @returns True if input is a prefix but not a full word.
788
+ */
789
+ hasPurePrefix(input) {
790
+ input = this._caseProcess(input);
791
+ let cur = this.root;
792
+ for (const c of input) {
793
+ const nodeC = cur.children.get(c);
794
+ if (!nodeC) return false;
795
+ cur = nodeC;
796
+ }
797
+ return !cur.isEnd;
798
+ }
799
+ /**
800
+ * Check whether any word starts with input.
801
+ * @remarks Time O(L), Space O(1)
802
+ * @param input - String to test as prefix.
803
+ * @returns True if input matches a path from root.
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
+
835
+
836
+
837
+
838
+
839
+ * @example
840
+ * // Check if a prefix exists
841
+ * const trie = new Trie(['hello', 'help', 'world']);
842
+ *
843
+ * console.log(trie.hasPrefix('hel')); // true;
844
+ * console.log(trie.hasPrefix('wor')); // true;
845
+ * console.log(trie.hasPrefix('xyz')); // false;
846
+ */
847
+ hasPrefix(input) {
848
+ input = this._caseProcess(input);
849
+ let cur = this.root;
850
+ for (const c of input) {
851
+ const nodeC = cur.children.get(c);
852
+ if (!nodeC) return false;
853
+ cur = nodeC;
854
+ }
855
+ return true;
856
+ }
857
+ /**
858
+ * Check whether the trie’s longest common prefix equals input.
859
+ * @remarks Time O(min(H,L)), Space O(1)
860
+ * @param input - Candidate longest common prefix.
861
+ * @returns True if input equals the common prefix.
862
+ */
863
+ hasCommonPrefix(input) {
864
+ input = this._caseProcess(input);
865
+ let commonPre = "";
866
+ const dfs = /* @__PURE__ */ __name((cur) => {
867
+ commonPre += cur.key;
868
+ if (commonPre === input) return;
869
+ if (cur.isEnd) return;
870
+ if (cur && cur.children && cur.children.size === 1) dfs(Array.from(cur.children.values())[0]);
871
+ else return;
872
+ }, "dfs");
873
+ dfs(this.root);
874
+ return commonPre === input;
875
+ }
876
+ /**
877
+ * Return the longest common prefix among all words.
878
+ * @remarks Time O(H), Space O(1)
879
+ * @returns The longest common prefix string.
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
+
911
+
912
+
913
+
914
+
915
+ * @example
916
+ * // Find shared prefix
917
+ * const trie = new Trie(['flower', 'flow', 'flight']);
918
+ *
919
+ * console.log(trie.getLongestCommonPrefix()); // 'fl';
920
+ */
921
+ getLongestCommonPrefix() {
922
+ let commonPre = "";
923
+ const dfs = /* @__PURE__ */ __name((cur) => {
924
+ commonPre += cur.key;
925
+ if (cur.isEnd) return;
926
+ if (cur && cur.children && cur.children.size === 1) dfs(Array.from(cur.children.values())[0]);
927
+ else return;
928
+ }, "dfs");
929
+ dfs(this.root);
930
+ return commonPre;
931
+ }
932
+ /**
933
+ * Collect words under a prefix up to a maximum count.
934
+ * @remarks Time O(K·L), Space O(K·L)
935
+ * @param [prefix] - Prefix to match; default empty string for root.
936
+ * @param [max] - Maximum number of words to return; default is Number.MAX_SAFE_INTEGER.
937
+ * @param [isAllWhenEmptyPrefix] - When true, collect from root even if prefix is empty.
938
+ * @returns Array of collected words (at most max).
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
+
970
+
971
+
972
+
973
+
974
+ * @example
975
+ * // Trie getWords and prefix search
976
+ * const trie = new Trie(['apple', 'app', 'apply', 'application', 'apricot']);
977
+ *
978
+ * // Get all words with prefix 'app'
979
+ * const appWords = trie.getWords('app');
980
+ * console.log(appWords); // contains 'app';
981
+ * console.log(appWords); // contains 'apple';
982
+ * console.log(appWords); // contains 'apply';
983
+ * console.log(appWords); // contains 'application';
984
+ * expect(appWords).not.toContain('apricot');
985
+ */
986
+ getWords(prefix = "", max = Number.MAX_SAFE_INTEGER, isAllWhenEmptyPrefix = false) {
987
+ prefix = this._caseProcess(prefix);
988
+ const words = [];
989
+ let found = 0;
990
+ const dfs = /* @__PURE__ */ __name((node, word) => {
991
+ for (const [char, childNode] of node.children) {
992
+ if (found >= max) return;
993
+ dfs(childNode, word + char);
994
+ }
995
+ if (node.isEnd) {
996
+ if (found >= max) return;
997
+ words.push(word);
998
+ found++;
999
+ }
1000
+ }, "dfs");
1001
+ let startNode = this.root;
1002
+ if (prefix) {
1003
+ for (const c of prefix) {
1004
+ const nodeC = startNode.children.get(c);
1005
+ if (nodeC) {
1006
+ startNode = nodeC;
1007
+ } else {
1008
+ return [];
1009
+ }
1010
+ }
1011
+ }
1012
+ if (isAllWhenEmptyPrefix || startNode !== this.root) dfs(startNode, prefix);
1013
+ return words;
1014
+ }
1015
+ /**
1016
+ * Deep clone this trie by iterating and inserting all words.
1017
+ * @remarks Time O(ΣL), Space O(ΣL)
1018
+ * @returns A new trie with the same words and options.
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
+
1047
+
1048
+
1049
+
1050
+
1051
+ * @example
1052
+ * // Create independent copy
1053
+ * const trie = new Trie(['hello', 'world']);
1054
+ * const copy = trie.clone();
1055
+ * copy.delete('hello');
1056
+ * console.log(trie.has('hello')); // true;
1057
+ */
1058
+ clone() {
1059
+ const next = this._createInstance();
1060
+ for (const x of this) next.add(x);
1061
+ return next;
1062
+ }
1063
+ /**
1064
+ * Filter words into a new trie of the same class.
1065
+ * @remarks Time O(ΣL), Space O(ΣL)
1066
+ * @param predicate - Predicate (word, index, trie) → boolean to keep word.
1067
+ * @param [thisArg] - Value for `this` inside the predicate.
1068
+ * @returns A new trie containing words that satisfy the predicate.
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
+
1097
+
1098
+
1099
+
1100
+
1101
+ * @example
1102
+ * // Filter words
1103
+ * const trie = new Trie(['cat', 'car', 'dog', 'card']);
1104
+ * const result = trie.filter(w => w.startsWith('ca'));
1105
+ * console.log(result.size); // 3;
1106
+ */
1107
+ filter(predicate, thisArg) {
1108
+ const results = this._createInstance();
1109
+ let index = 0;
1110
+ for (const word of this) {
1111
+ if (predicate.call(thisArg, word, index, this)) {
1112
+ results.add(word);
1113
+ }
1114
+ index++;
1115
+ }
1116
+ return results;
1117
+ }
1118
+ map(callback, options, thisArg) {
1119
+ const newTrie = this._createLike([], options);
1120
+ let i = 0;
1121
+ for (const x of this) {
1122
+ const v = thisArg === void 0 ? callback(x, i++, this) : callback.call(thisArg, x, i++, this);
1123
+ if (typeof v !== "string") {
1124
+ raise(TypeError, ERR.callbackReturnType("string", typeof v, "Trie.map"));
1125
+ }
1126
+ newTrie.add(v);
1127
+ }
1128
+ return newTrie;
1129
+ }
1130
+ /**
1131
+ * Map words into a new trie of the same element type.
1132
+ * @remarks Time O(ΣL), Space O(ΣL)
1133
+ * @param callback - Mapping function (word, index, trie) → string.
1134
+ * @param [thisArg] - Value for `this` inside the callback.
1135
+ * @returns A new trie with mapped words.
1136
+ */
1137
+ mapSame(callback, thisArg) {
1138
+ const next = this._createInstance();
1139
+ let i = 0;
1140
+ for (const key of this) {
1141
+ const mapped = thisArg === void 0 ? callback(key, i++, this) : callback.call(thisArg, key, i++, this);
1142
+ next.add(mapped);
1143
+ }
1144
+ return next;
1145
+ }
1146
+ /**
1147
+ * (Protected) Create an empty instance of the same concrete class.
1148
+ * @remarks Time O(1), Space O(1)
1149
+ * @param [options] - Options forwarded to the constructor.
1150
+ * @returns An empty like-kind trie instance.
1151
+ */
1152
+ _createInstance(options) {
1153
+ const Ctor = this.constructor;
1154
+ return new Ctor([], {
1155
+ toElementFn: this.toElementFn,
1156
+ caseSensitive: this.caseSensitive,
1157
+ ...options ?? {}
1158
+ });
1159
+ }
1160
+ /**
1161
+ * (Protected) Create a like-kind trie and seed it from an iterable.
1162
+ * @remarks Time O(ΣL), Space O(ΣL)
1163
+ * @template RM
1164
+ * @param [elements] - Iterable used to seed the new trie.
1165
+ * @param [options] - Options forwarded to the constructor.
1166
+ * @returns A like-kind Trie instance.
1167
+ */
1168
+ _createLike(elements = [], options) {
1169
+ const Ctor = this.constructor;
1170
+ return new Ctor(elements, options);
1171
+ }
1172
+ /**
1173
+ * (Protected) Spawn an empty like-kind trie instance.
1174
+ * @remarks Time O(1), Space O(1)
1175
+ * @template RM
1176
+ * @param [options] - Options forwarded to the constructor.
1177
+ * @returns An empty like-kind Trie instance.
1178
+ */
1179
+ _spawnLike(options) {
1180
+ return this._createLike([], options);
1181
+ }
1182
+ /**
1183
+ * (Protected) Iterate all words in lexicographic order of edges.
1184
+ * @remarks Time O(ΣL), Space O(H)
1185
+ * @returns Iterator of words.
1186
+ */
1187
+ *_getIterator() {
1188
+ function* _dfs(node, path) {
1189
+ if (node.isEnd) {
1190
+ yield path;
1191
+ }
1192
+ for (const [char, childNode] of node.children) {
1193
+ yield* _dfs(childNode, path + char);
1194
+ }
1195
+ }
1196
+ __name(_dfs, "_dfs");
1197
+ yield* _dfs(this.root, "");
1198
+ }
1199
+ /**
1200
+ * (Protected) Normalize a string according to case sensitivity.
1201
+ * @remarks Time O(L), Space O(L)
1202
+ * @param str - Input string to normalize.
1203
+ * @returns Normalized string based on caseSensitive.
1204
+ */
1205
+ _caseProcess(str) {
1206
+ if (!this._caseSensitive) {
1207
+ str = str.toLowerCase();
1208
+ }
1209
+ return str;
1210
+ }
1211
+ };
1212
+ /**
1213
+ * data-structure-typed
1214
+ *
1215
+ * @author Pablo Zeng
1216
+ * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
1217
+ * @license MIT License
1218
+ */
1219
+
1220
+ exports.Trie = Trie;
1221
+ exports.TrieNode = TrieNode;
1222
+ //# sourceMappingURL=trie.cjs.map
1223
+ //# sourceMappingURL=trie.cjs.map