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