data-structure-typed 1.54.3 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (208) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/benchmark/report.html +26 -2
  3. package/benchmark/report.json +292 -42
  4. package/dist/cjs/data-structures/base/iterable-element-base.d.ts +14 -40
  5. package/dist/cjs/data-structures/base/iterable-element-base.js +14 -11
  6. package/dist/cjs/data-structures/base/iterable-element-base.js.map +1 -1
  7. package/dist/cjs/data-structures/base/linear-base.d.ts +277 -0
  8. package/dist/cjs/data-structures/base/linear-base.js +553 -0
  9. package/dist/cjs/data-structures/base/linear-base.js.map +1 -0
  10. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +12 -8
  11. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +50 -37
  12. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
  13. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +64 -0
  14. package/dist/cjs/data-structures/binary-tree/avl-tree.js +64 -0
  15. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  16. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +62 -0
  17. package/dist/cjs/data-structures/binary-tree/binary-tree.js +67 -5
  18. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  19. package/dist/cjs/data-structures/binary-tree/bst.d.ts +3 -3
  20. package/dist/cjs/data-structures/binary-tree/bst.js +14 -14
  21. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  22. package/dist/cjs/data-structures/binary-tree/red-black-tree.d.ts +1 -7
  23. package/dist/cjs/data-structures/binary-tree/red-black-tree.js +1 -7
  24. package/dist/cjs/data-structures/binary-tree/red-black-tree.js.map +1 -1
  25. package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +175 -14
  26. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +210 -40
  27. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
  28. package/dist/cjs/data-structures/graph/abstract-graph.js +16 -16
  29. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  30. package/dist/cjs/data-structures/hash/hash-map.d.ts +46 -0
  31. package/dist/cjs/data-structures/hash/hash-map.js +46 -0
  32. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  33. package/dist/cjs/data-structures/heap/heap.d.ts +3 -11
  34. package/dist/cjs/data-structures/heap/heap.js +0 -10
  35. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  36. package/dist/cjs/data-structures/heap/max-heap.d.ts +2 -2
  37. package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
  38. package/dist/cjs/data-structures/heap/min-heap.d.ts +2 -2
  39. package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
  40. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +65 -94
  41. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +131 -146
  42. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  43. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +145 -75
  44. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +283 -169
  45. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  46. package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -2
  47. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  48. package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -2
  49. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  50. package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +2 -2
  51. package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
  52. package/dist/cjs/data-structures/queue/deque.d.ts +130 -91
  53. package/dist/cjs/data-structures/queue/deque.js +269 -169
  54. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  55. package/dist/cjs/data-structures/queue/queue.d.ts +131 -40
  56. package/dist/cjs/data-structures/queue/queue.js +181 -50
  57. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  58. package/dist/cjs/data-structures/stack/stack.d.ts +124 -11
  59. package/dist/cjs/data-structures/stack/stack.js +121 -10
  60. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  61. package/dist/cjs/data-structures/trie/trie.d.ts +4 -3
  62. package/dist/cjs/data-structures/trie/trie.js +3 -0
  63. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  64. package/dist/cjs/types/data-structures/base/base.d.ts +9 -4
  65. package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
  66. package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
  67. package/dist/cjs/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
  68. package/dist/cjs/types/data-structures/linked-list/singly-linked-list.d.ts +2 -2
  69. package/dist/cjs/types/data-structures/queue/deque.d.ts +2 -3
  70. package/dist/cjs/types/data-structures/queue/queue.d.ts +2 -2
  71. package/dist/esm/data-structures/base/iterable-element-base.d.ts +14 -40
  72. package/dist/esm/data-structures/base/iterable-element-base.js +14 -11
  73. package/dist/esm/data-structures/base/iterable-element-base.js.map +1 -1
  74. package/dist/esm/data-structures/base/linear-base.d.ts +277 -0
  75. package/dist/esm/data-structures/base/linear-base.js +549 -0
  76. package/dist/esm/data-structures/base/linear-base.js.map +1 -0
  77. package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.d.ts +12 -8
  78. package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js +50 -36
  79. package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
  80. package/dist/esm/data-structures/binary-tree/avl-tree.d.ts +64 -0
  81. package/dist/esm/data-structures/binary-tree/avl-tree.js +64 -0
  82. package/dist/esm/data-structures/binary-tree/avl-tree.js.map +1 -1
  83. package/dist/esm/data-structures/binary-tree/binary-tree.d.ts +62 -0
  84. package/dist/esm/data-structures/binary-tree/binary-tree.js +67 -5
  85. package/dist/esm/data-structures/binary-tree/binary-tree.js.map +1 -1
  86. package/dist/esm/data-structures/binary-tree/bst.d.ts +3 -3
  87. package/dist/esm/data-structures/binary-tree/bst.js +14 -12
  88. package/dist/esm/data-structures/binary-tree/bst.js.map +1 -1
  89. package/dist/esm/data-structures/binary-tree/red-black-tree.d.ts +1 -7
  90. package/dist/esm/data-structures/binary-tree/red-black-tree.js +1 -7
  91. package/dist/esm/data-structures/binary-tree/red-black-tree.js.map +1 -1
  92. package/dist/esm/data-structures/binary-tree/tree-multi-map.d.ts +175 -14
  93. package/dist/esm/data-structures/binary-tree/tree-multi-map.js +210 -39
  94. package/dist/esm/data-structures/binary-tree/tree-multi-map.js.map +1 -1
  95. package/dist/esm/data-structures/graph/abstract-graph.js +16 -16
  96. package/dist/esm/data-structures/graph/abstract-graph.js.map +1 -1
  97. package/dist/esm/data-structures/hash/hash-map.d.ts +46 -0
  98. package/dist/esm/data-structures/hash/hash-map.js +46 -0
  99. package/dist/esm/data-structures/hash/hash-map.js.map +1 -1
  100. package/dist/esm/data-structures/heap/heap.d.ts +3 -11
  101. package/dist/esm/data-structures/heap/heap.js +0 -10
  102. package/dist/esm/data-structures/heap/heap.js.map +1 -1
  103. package/dist/esm/data-structures/heap/max-heap.d.ts +2 -2
  104. package/dist/esm/data-structures/heap/max-heap.js.map +1 -1
  105. package/dist/esm/data-structures/heap/min-heap.d.ts +2 -2
  106. package/dist/esm/data-structures/heap/min-heap.js.map +1 -1
  107. package/dist/esm/data-structures/linked-list/doubly-linked-list.d.ts +65 -94
  108. package/dist/esm/data-structures/linked-list/doubly-linked-list.js +132 -148
  109. package/dist/esm/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  110. package/dist/esm/data-structures/linked-list/singly-linked-list.d.ts +145 -75
  111. package/dist/esm/data-structures/linked-list/singly-linked-list.js +283 -170
  112. package/dist/esm/data-structures/linked-list/singly-linked-list.js.map +1 -1
  113. package/dist/esm/data-structures/priority-queue/max-priority-queue.d.ts +2 -2
  114. package/dist/esm/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  115. package/dist/esm/data-structures/priority-queue/min-priority-queue.d.ts +2 -2
  116. package/dist/esm/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  117. package/dist/esm/data-structures/priority-queue/priority-queue.d.ts +2 -2
  118. package/dist/esm/data-structures/priority-queue/priority-queue.js.map +1 -1
  119. package/dist/esm/data-structures/queue/deque.d.ts +130 -91
  120. package/dist/esm/data-structures/queue/deque.js +269 -169
  121. package/dist/esm/data-structures/queue/deque.js.map +1 -1
  122. package/dist/esm/data-structures/queue/queue.d.ts +131 -40
  123. package/dist/esm/data-structures/queue/queue.js +182 -51
  124. package/dist/esm/data-structures/queue/queue.js.map +1 -1
  125. package/dist/esm/data-structures/stack/stack.d.ts +124 -11
  126. package/dist/esm/data-structures/stack/stack.js +121 -10
  127. package/dist/esm/data-structures/stack/stack.js.map +1 -1
  128. package/dist/esm/data-structures/trie/trie.d.ts +4 -3
  129. package/dist/esm/data-structures/trie/trie.js +3 -0
  130. package/dist/esm/data-structures/trie/trie.js.map +1 -1
  131. package/dist/esm/types/data-structures/base/base.d.ts +9 -4
  132. package/dist/esm/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
  133. package/dist/esm/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
  134. package/dist/esm/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
  135. package/dist/esm/types/data-structures/linked-list/singly-linked-list.d.ts +2 -2
  136. package/dist/esm/types/data-structures/queue/deque.d.ts +2 -3
  137. package/dist/esm/types/data-structures/queue/queue.d.ts +2 -2
  138. package/dist/individuals/binary-tree/avl-tree-counter.mjs +4701 -0
  139. package/dist/individuals/binary-tree/avl-tree-multi-map.mjs +4514 -0
  140. package/dist/individuals/binary-tree/avl-tree.mjs +4321 -0
  141. package/dist/individuals/binary-tree/binary-tree.mjs +3097 -0
  142. package/dist/individuals/binary-tree/bst.mjs +3858 -0
  143. package/dist/individuals/binary-tree/red-black-tree.mjs +4391 -0
  144. package/dist/individuals/binary-tree/tree-counter.mjs +4806 -0
  145. package/dist/individuals/binary-tree/tree-multi-map.mjs +4582 -0
  146. package/dist/individuals/graph/directed-graph.mjs +2910 -0
  147. package/dist/individuals/graph/undirected-graph.mjs +2745 -0
  148. package/dist/individuals/hash/hash-map.mjs +1040 -0
  149. package/dist/individuals/heap/heap.mjs +909 -0
  150. package/dist/individuals/heap/max-heap.mjs +671 -0
  151. package/dist/individuals/heap/min-heap.mjs +659 -0
  152. package/dist/individuals/linked-list/doubly-linked-list.mjs +1495 -0
  153. package/dist/individuals/linked-list/singly-linked-list.mjs +1479 -0
  154. package/dist/individuals/priority-queue/max-priority-queue.mjs +768 -0
  155. package/dist/individuals/priority-queue/min-priority-queue.mjs +757 -0
  156. package/dist/individuals/priority-queue/priority-queue.mjs +670 -0
  157. package/dist/individuals/queue/deque.mjs +1262 -0
  158. package/dist/individuals/queue/queue.mjs +1865 -0
  159. package/dist/individuals/stack/stack.mjs +415 -0
  160. package/dist/individuals/trie/trie.mjs +687 -0
  161. package/dist/umd/data-structure-typed.js +1260 -649
  162. package/dist/umd/data-structure-typed.min.js +3 -3
  163. package/dist/umd/data-structure-typed.min.js.map +1 -1
  164. package/package.json +7 -7
  165. package/src/data-structures/base/iterable-element-base.ts +29 -20
  166. package/src/data-structures/base/linear-base.ts +649 -0
  167. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +51 -36
  168. package/src/data-structures/binary-tree/avl-tree.ts +64 -0
  169. package/src/data-structures/binary-tree/binary-tree.ts +5 -5
  170. package/src/data-structures/binary-tree/bst.ts +9 -9
  171. package/src/data-structures/binary-tree/tree-multi-map.ts +214 -40
  172. package/src/data-structures/graph/abstract-graph.ts +16 -16
  173. package/src/data-structures/hash/hash-map.ts +46 -0
  174. package/src/data-structures/heap/heap.ts +3 -14
  175. package/src/data-structures/heap/max-heap.ts +2 -2
  176. package/src/data-structures/heap/min-heap.ts +2 -2
  177. package/src/data-structures/linked-list/doubly-linked-list.ts +144 -160
  178. package/src/data-structures/linked-list/singly-linked-list.ts +307 -185
  179. package/src/data-structures/priority-queue/max-priority-queue.ts +2 -5
  180. package/src/data-structures/priority-queue/min-priority-queue.ts +2 -5
  181. package/src/data-structures/priority-queue/priority-queue.ts +2 -2
  182. package/src/data-structures/queue/deque.ts +286 -183
  183. package/src/data-structures/queue/queue.ts +196 -63
  184. package/src/data-structures/stack/stack.ts +124 -18
  185. package/src/data-structures/trie/trie.ts +7 -3
  186. package/src/types/data-structures/base/base.ts +17 -8
  187. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
  188. package/src/types/data-structures/binary-tree/tree-multi-map.ts +1 -1
  189. package/src/types/data-structures/linked-list/doubly-linked-list.ts +2 -2
  190. package/src/types/data-structures/linked-list/singly-linked-list.ts +2 -2
  191. package/src/types/data-structures/queue/deque.ts +2 -3
  192. package/src/types/data-structures/queue/queue.ts +2 -2
  193. package/test/integration/all-in-one.test.ts +1 -1
  194. package/test/integration/avl-tree.test.ts +1 -1
  195. package/test/integration/bst.test.ts +2 -2
  196. package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +168 -0
  197. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +15 -14
  198. package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +1 -1
  199. package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +165 -7
  200. package/test/unit/data-structures/graph/directed-graph.test.ts +37 -37
  201. package/test/unit/data-structures/graph/undirected-graph.test.ts +2 -2
  202. package/test/unit/data-structures/hash/hash-map.test.ts +135 -0
  203. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +135 -27
  204. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +183 -12
  205. package/test/unit/data-structures/queue/deque.test.ts +241 -60
  206. package/test/unit/data-structures/queue/queue.test.ts +332 -19
  207. package/test/unit/data-structures/stack/stack.test.ts +165 -0
  208. package/test/unit/unrestricted-interconversion.test.ts +1 -1
@@ -0,0 +1,687 @@
1
+ // src/data-structures/base/iterable-element-base.ts
2
+ var IterableElementBase = class {
3
+ /**
4
+ * The protected constructor initializes the options for the IterableElementBase class, including the
5
+ * toElementFn function.
6
+ * @param [options] - An optional object that contains the following properties:
7
+ */
8
+ constructor(options) {
9
+ if (options) {
10
+ const { toElementFn } = options;
11
+ if (typeof toElementFn === "function") this._toElementFn = toElementFn;
12
+ else if (toElementFn) throw new TypeError("toElementFn must be a function type");
13
+ }
14
+ }
15
+ _toElementFn;
16
+ get toElementFn() {
17
+ return this._toElementFn;
18
+ }
19
+ /**
20
+ * Time Complexity: O(n)
21
+ * Space Complexity: O(1)
22
+ *
23
+ * The function is an implementation of the Symbol.iterator method that returns an IterableIterator.
24
+ * @param {any[]} args - The `args` parameter in the code snippet represents a rest parameter. It
25
+ * allows the function to accept any number of arguments as an array. In this case, the `args`
26
+ * parameter is used to pass any number of arguments to the `_getIterator` method.
27
+ */
28
+ *[Symbol.iterator](...args) {
29
+ yield* this._getIterator(...args);
30
+ }
31
+ /**
32
+ * Time Complexity: O(n)
33
+ * Space Complexity: O(n)
34
+ *
35
+ * The function returns an iterator that yields all the values in the object.
36
+ */
37
+ *values() {
38
+ for (const item of this) {
39
+ yield item;
40
+ }
41
+ }
42
+ /**
43
+ * Time Complexity: O(n)
44
+ * Space Complexity: O(1)
45
+ *
46
+ * The `every` function checks if every element in the array satisfies a given predicate.
47
+ * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
48
+ * the current element being processed, its index, and the array it belongs to. It should return a
49
+ * boolean value indicating whether the element satisfies a certain condition or not.
50
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
51
+ * to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
52
+ * passed as the `this` value to the `predicate` function. If `thisArg` is
53
+ * @returns The `every` method is returning a boolean value. It returns `true` if every element in
54
+ * the array satisfies the provided predicate function, and `false` otherwise.
55
+ */
56
+ every(predicate, thisArg) {
57
+ let index = 0;
58
+ for (const item of this) {
59
+ if (!predicate.call(thisArg, item, index++, this)) {
60
+ return false;
61
+ }
62
+ }
63
+ return true;
64
+ }
65
+ /**
66
+ * Time Complexity: O(n)
67
+ * Space Complexity: O(1)
68
+ *
69
+ * The "some" function checks if at least one element in a collection satisfies a given predicate.
70
+ * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
71
+ * `value`, `index`, and `array`. It should return a boolean value indicating whether the current
72
+ * element satisfies the condition.
73
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
74
+ * to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
75
+ * it will be passed as the `this` value to the `predicate` function. If `thisArg
76
+ * @returns a boolean value. It returns true if the predicate function returns true for any element
77
+ * in the collection, and false otherwise.
78
+ */
79
+ some(predicate, thisArg) {
80
+ let index = 0;
81
+ for (const item of this) {
82
+ if (predicate.call(thisArg, item, index++, this)) {
83
+ return true;
84
+ }
85
+ }
86
+ return false;
87
+ }
88
+ /**
89
+ * Time Complexity: O(n)
90
+ * Space Complexity: O(1)
91
+ *
92
+ * The `forEach` function iterates over each element in an array-like object and calls a callback
93
+ * function for each element.
94
+ * @param callbackfn - The callbackfn parameter is a function that will be called for each element in
95
+ * the array. It takes three arguments: the current element being processed, the index of the current
96
+ * element, and the array that forEach was called upon.
97
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
98
+ * to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
99
+ * be passed as the `this` value to the `callbackfn` function. If `thisArg
100
+ */
101
+ forEach(callbackfn, thisArg) {
102
+ let index = 0;
103
+ for (const item of this) {
104
+ callbackfn.call(thisArg, item, index++, this);
105
+ }
106
+ }
107
+ /**
108
+ * Time Complexity: O(n)
109
+ * Space Complexity: O(1)
110
+ *
111
+ * The `find` function iterates over the elements of an array-like object and returns the first
112
+ * element that satisfies the provided callback function.
113
+ * @param predicate - The predicate parameter is a function that will be called for each element in
114
+ * the array. It takes three arguments: the current element being processed, the index of the current
115
+ * element, and the array itself. The function should return a boolean value indicating whether the
116
+ * current element matches the desired condition.
117
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
118
+ * to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
119
+ * be passed as the `this` value to the `callbackfn` function. If `thisArg
120
+ * @returns The `find` method returns the first element in the array that satisfies the provided
121
+ * callback function. If no element satisfies the callback function, `undefined` is returned.
122
+ */
123
+ find(predicate, thisArg) {
124
+ let index = 0;
125
+ for (const item of this) {
126
+ if (predicate.call(thisArg, item, index++, this)) return item;
127
+ }
128
+ return;
129
+ }
130
+ /**
131
+ * Time Complexity: O(n)
132
+ * Space Complexity: O(1)
133
+ *
134
+ * The function checks if a given element exists in a collection.
135
+ * @param {E} element - The parameter "element" is of type E, which means it can be any type. It
136
+ * represents the element that we want to check for existence in the collection.
137
+ * @returns a boolean value. It returns true if the element is found in the collection, and false
138
+ * otherwise.
139
+ */
140
+ has(element) {
141
+ for (const ele of this) {
142
+ if (ele === element) return true;
143
+ }
144
+ return false;
145
+ }
146
+ /**
147
+ * Time Complexity: O(n)
148
+ * Space Complexity: O(1)
149
+ *
150
+ * The `reduce` function iterates over the elements of an array-like object and applies a callback
151
+ * function to reduce them into a single value.
152
+ * @param callbackfn - The callbackfn parameter is a function that will be called for each element in
153
+ * the array. It takes four arguments:
154
+ * @param {U} initialValue - The initialValue parameter is the initial value of the accumulator. It
155
+ * is the value that the accumulator starts with before the reduction operation begins.
156
+ * @returns The `reduce` method is returning the final value of the accumulator after iterating over
157
+ * all the elements in the array and applying the callback function to each element.
158
+ */
159
+ reduce(callbackfn, initialValue) {
160
+ let accumulator = initialValue ?? 0;
161
+ let index = 0;
162
+ for (const item of this) {
163
+ accumulator = callbackfn(accumulator, item, index++, this);
164
+ }
165
+ return accumulator;
166
+ }
167
+ /**
168
+ * Time Complexity: O(n)
169
+ * Space Complexity: O(n)
170
+ *
171
+ * The `toArray` function converts a linked list into an array.
172
+ * @returns The `toArray()` method is returning an array of type `E[]`.
173
+ */
174
+ toArray() {
175
+ return [...this];
176
+ }
177
+ /**
178
+ * Time Complexity: O(n)
179
+ * Space Complexity: O(n)
180
+ *
181
+ * The print function logs the elements of an array to the console.
182
+ */
183
+ toVisual() {
184
+ return [...this];
185
+ }
186
+ /**
187
+ * Time Complexity: O(n)
188
+ * Space Complexity: O(n)
189
+ *
190
+ * The print function logs the elements of an array to the console.
191
+ */
192
+ print() {
193
+ console.log(this.toVisual());
194
+ }
195
+ };
196
+
197
+ // src/data-structures/trie/trie.ts
198
+ var TrieNode = class {
199
+ constructor(key) {
200
+ this._key = key;
201
+ this._isEnd = false;
202
+ this._children = /* @__PURE__ */ new Map();
203
+ }
204
+ _key;
205
+ /**
206
+ * The function returns the value of the protected variable _key.
207
+ * @returns The value of the `_key` property, which is a string.
208
+ */
209
+ get key() {
210
+ return this._key;
211
+ }
212
+ /**
213
+ * The above function sets the value of a protected variable called "key".
214
+ * @param {string} value - The value parameter is a string that represents the value to be assigned
215
+ * to the key.
216
+ */
217
+ set key(value) {
218
+ this._key = value;
219
+ }
220
+ _children;
221
+ /**
222
+ * The function returns the children of a TrieNode as a Map.
223
+ * @returns The `children` property of the TrieNode object, which is a Map containing string keys and
224
+ * TrieNode values.
225
+ */
226
+ get children() {
227
+ return this._children;
228
+ }
229
+ /**
230
+ * The function sets the value of the `_children` property of a TrieNode object.
231
+ * @param value - The value parameter is a Map object that represents the children of a TrieNode. The
232
+ * keys of the map are strings, which represent the characters that are associated with each child
233
+ * TrieNode. The values of the map are TrieNode objects, which represent the child nodes of the
234
+ * current TrieNode.
235
+ */
236
+ set children(value) {
237
+ this._children = value;
238
+ }
239
+ _isEnd;
240
+ /**
241
+ * The function returns a boolean value indicating whether a certain condition is met.
242
+ * @returns The method is returning a boolean value, specifically the value of the variable `_isEnd`.
243
+ */
244
+ get isEnd() {
245
+ return this._isEnd;
246
+ }
247
+ /**
248
+ * The function sets the value of the "_isEnd" property.
249
+ * @param {boolean} value - The value parameter is a boolean value that indicates whether the current
250
+ * state is the end state or not.
251
+ */
252
+ set isEnd(value) {
253
+ this._isEnd = value;
254
+ }
255
+ };
256
+ var Trie = class _Trie extends IterableElementBase {
257
+ /**
258
+ * The constructor initializes a Trie data structure with optional options and words provided as
259
+ * input.
260
+ * @param {Iterable<string> | Iterable<R>} words - The `words` parameter in the constructor is an
261
+ * iterable containing either strings or elements of type `R`. It is used to initialize the Trie with
262
+ * a list of words or elements. If no `words` are provided, an empty iterable is used as the default
263
+ * value.
264
+ * @param [options] - The `options` parameter in the constructor is an optional object that can
265
+ * contain configuration options for the Trie data structure. One of the options it can have is
266
+ * `caseSensitive`, which is a boolean value indicating whether the Trie should be case-sensitive or
267
+ * not. If `caseSensitive` is set to `
268
+ */
269
+ constructor(words = [], options) {
270
+ super(options);
271
+ if (options) {
272
+ const { caseSensitive } = options;
273
+ if (caseSensitive !== void 0) this._caseSensitive = caseSensitive;
274
+ }
275
+ if (words) {
276
+ this.addMany(words);
277
+ }
278
+ }
279
+ _size = 0;
280
+ /**
281
+ * The size function returns the size of the stack.
282
+ * @return The number of elements in the list
283
+ */
284
+ get size() {
285
+ return this._size;
286
+ }
287
+ _caseSensitive = true;
288
+ /**
289
+ * The caseSensitive function is a getter that returns the value of the protected _caseSensitive property.
290
+ * @return The value of the _caseSensitive protected variable
291
+ */
292
+ get caseSensitive() {
293
+ return this._caseSensitive;
294
+ }
295
+ _root = new TrieNode("");
296
+ /**
297
+ * The root function returns the root node of the tree.
298
+ * @return The root node
299
+ */
300
+ get root() {
301
+ return this._root;
302
+ }
303
+ /**
304
+ * Time Complexity: O(l), where l is the length of the word being added.
305
+ * Space Complexity: O(l) - Each character in the word adds a TrieNode.
306
+ *
307
+ * Add a word to the Trie structure.
308
+ * @param {string} word - The word to add.
309
+ * @returns {boolean} True if the word was successfully added.
310
+ */
311
+ add(word) {
312
+ word = this._caseProcess(word);
313
+ let cur = this.root;
314
+ let isNewWord = false;
315
+ for (const c of word) {
316
+ let nodeC = cur.children.get(c);
317
+ if (!nodeC) {
318
+ nodeC = new TrieNode(c);
319
+ cur.children.set(c, nodeC);
320
+ }
321
+ cur = nodeC;
322
+ }
323
+ if (!cur.isEnd) {
324
+ isNewWord = true;
325
+ cur.isEnd = true;
326
+ this._size++;
327
+ }
328
+ return isNewWord;
329
+ }
330
+ /**
331
+ * Time Complexity: O(n * l)
332
+ * Space Complexity: O(1)
333
+ *
334
+ * The `addMany` function in TypeScript takes an iterable of strings or elements of type R, converts
335
+ * them using a provided function if available, and adds them to a data structure while returning an
336
+ * array of boolean values indicating success.
337
+ * @param {Iterable<string> | Iterable<R>} words - The `words` parameter in the `addMany` function is
338
+ * an iterable that contains either strings or elements of type `R`.
339
+ * @returns The `addMany` method returns an array of boolean values indicating whether each word in
340
+ * the input iterable was successfully added to the data structure.
341
+ */
342
+ addMany(words) {
343
+ const ans = [];
344
+ for (const word of words) {
345
+ if (this.toElementFn) {
346
+ ans.push(this.add(this.toElementFn(word)));
347
+ } else {
348
+ ans.push(this.add(word));
349
+ }
350
+ }
351
+ return ans;
352
+ }
353
+ /**
354
+ * Time Complexity: O(l), where l is the length of the input word.
355
+ * Space Complexity: O(1) - Constant space.
356
+ *
357
+ * Check if the Trie contains a given word.
358
+ * @param {string} word - The word to check for.
359
+ * @returns {boolean} True if the word is present in the Trie.
360
+ */
361
+ has(word) {
362
+ word = this._caseProcess(word);
363
+ let cur = this.root;
364
+ for (const c of word) {
365
+ const nodeC = cur.children.get(c);
366
+ if (!nodeC) return false;
367
+ cur = nodeC;
368
+ }
369
+ return cur.isEnd;
370
+ }
371
+ /**
372
+ * Time Complexity: O(1)
373
+ * Space Complexity: O(1)
374
+ *
375
+ * The isEmpty function checks if the size of the queue is 0.
376
+ * @return True if the size of the queue is 0
377
+ */
378
+ isEmpty() {
379
+ return this._size === 0;
380
+ }
381
+ /**
382
+ * Time Complexity: O(1)
383
+ * Space Complexity: O(1)
384
+ *
385
+ * The clear function resets the size of the Trie to 0 and creates a new root TrieNode.
386
+ */
387
+ clear() {
388
+ this._size = 0;
389
+ this._root = new TrieNode("");
390
+ }
391
+ /**
392
+ * Time Complexity: O(l), where l is the length of the word being deleted.
393
+ * Space Complexity: O(n) - Due to the recursive DFS approach.
394
+ *
395
+ * Remove a word from the Trie structure.
396
+ * @param{string} word - The word to delete.
397
+ * @returns {boolean} True if the word was successfully removed.
398
+ */
399
+ delete(word) {
400
+ word = this._caseProcess(word);
401
+ let isDeleted = false;
402
+ const dfs = (cur, i) => {
403
+ const char = word[i];
404
+ const child = cur.children.get(char);
405
+ if (child) {
406
+ if (i === word.length - 1) {
407
+ if (child.isEnd) {
408
+ if (child.children.size > 0) {
409
+ child.isEnd = false;
410
+ } else {
411
+ cur.children.delete(char);
412
+ }
413
+ isDeleted = true;
414
+ return true;
415
+ }
416
+ return false;
417
+ }
418
+ const res = dfs(child, i + 1);
419
+ if (res && !cur.isEnd && child.children.size === 0) {
420
+ cur.children.delete(char);
421
+ return true;
422
+ }
423
+ return false;
424
+ }
425
+ return false;
426
+ };
427
+ dfs(this.root, 0);
428
+ if (isDeleted) {
429
+ this._size--;
430
+ }
431
+ return isDeleted;
432
+ }
433
+ /**
434
+ * Time Complexity: O(n)
435
+ * Space Complexity: O(1)
436
+ *
437
+ * The function `getHeight` calculates the height of a trie data structure starting from the root
438
+ * node.
439
+ * @returns The `getHeight` method returns the maximum depth or height of the trie tree starting from
440
+ * the root node. It calculates the depth using a breadth-first search (BFS) traversal of the trie
441
+ * tree and returns the maximum depth found.
442
+ */
443
+ getHeight() {
444
+ const startNode = this.root;
445
+ let maxDepth = 0;
446
+ if (startNode) {
447
+ const bfs = (node, level) => {
448
+ if (level > maxDepth) {
449
+ maxDepth = level;
450
+ }
451
+ const { children } = node;
452
+ if (children) {
453
+ for (const child of children.entries()) {
454
+ bfs(child[1], level + 1);
455
+ }
456
+ }
457
+ };
458
+ bfs(startNode, 0);
459
+ }
460
+ return maxDepth;
461
+ }
462
+ /**
463
+ * Time Complexity: O(l), where l is the length of the input prefix.
464
+ * Space Complexity: O(1) - Constant space.
465
+ *
466
+ * Check if a given input string has an absolute prefix in the Trie, meaning it's not a complete word.
467
+ * @param {string} input - The input string to check.
468
+ * @returns {boolean} True if it's an absolute prefix in the Trie.
469
+ */
470
+ hasPurePrefix(input) {
471
+ input = this._caseProcess(input);
472
+ let cur = this.root;
473
+ for (const c of input) {
474
+ const nodeC = cur.children.get(c);
475
+ if (!nodeC) return false;
476
+ cur = nodeC;
477
+ }
478
+ return !cur.isEnd;
479
+ }
480
+ /**
481
+ * Time Complexity: O(l), where l is the length of the input prefix.
482
+ * Space Complexity: O(1) - Constant space.
483
+ *
484
+ * Check if a given input string is a prefix of any existing word in the Trie, whether as an absolute prefix or a complete word.
485
+ * @param {string} input - The input string representing the prefix to check.
486
+ * @returns {boolean} True if it's a prefix in the Trie.
487
+ */
488
+ hasPrefix(input) {
489
+ input = this._caseProcess(input);
490
+ let cur = this.root;
491
+ for (const c of input) {
492
+ const nodeC = cur.children.get(c);
493
+ if (!nodeC) return false;
494
+ cur = nodeC;
495
+ }
496
+ return true;
497
+ }
498
+ /**
499
+ * Time Complexity: O(n), where n is the total number of nodes in the trie.
500
+ * Space Complexity: O(l), where l is the length of the input prefix.
501
+ *
502
+ * Check if the input string is a common prefix in the Trie, meaning it's a prefix shared by all words in the Trie.
503
+ * @param {string} input - The input string representing the common prefix to check for.
504
+ * @returns {boolean} True if it's a common prefix in the Trie.
505
+ */
506
+ hasCommonPrefix(input) {
507
+ input = this._caseProcess(input);
508
+ let commonPre = "";
509
+ const dfs = (cur) => {
510
+ commonPre += cur.key;
511
+ if (commonPre === input) return;
512
+ if (cur.isEnd) return;
513
+ if (cur && cur.children && cur.children.size === 1) dfs(Array.from(cur.children.values())[0]);
514
+ else return;
515
+ };
516
+ dfs(this.root);
517
+ return commonPre === input;
518
+ }
519
+ /**
520
+ * Time Complexity: O(n), where n is the total number of nodes in the trie.
521
+ * Space Complexity: O(l), where l is the length of the longest common prefix.
522
+ *
523
+ * Get the longest common prefix among all the words stored in the Trie.
524
+ * @returns {string} The longest common prefix found in the Trie.
525
+ */
526
+ getLongestCommonPrefix() {
527
+ let commonPre = "";
528
+ const dfs = (cur) => {
529
+ commonPre += cur.key;
530
+ if (cur.isEnd) return;
531
+ if (cur && cur.children && cur.children.size === 1) dfs(Array.from(cur.children.values())[0]);
532
+ else return;
533
+ };
534
+ dfs(this.root);
535
+ return commonPre;
536
+ }
537
+ /**
538
+ * Time Complexity: O(w * l), where w is the number of words retrieved, and l is the average length of the words.
539
+ * Space Complexity: O(w * l) - The space required for the output array.
540
+ *
541
+ * The `getAll` function returns an array of all words in a Trie data structure that start with a given prefix.
542
+ * @param {string} prefix - The `prefix` parameter is a string that represents the prefix that we want to search for in the
543
+ * trie. It is an optional parameter, so if no prefix is provided, it will default to an empty string.
544
+ * @param {number} max - The max count of words will be found
545
+ * @param isAllWhenEmptyPrefix - If true, when the prefix provided as '', returns all the words in the trie.
546
+ * @returns {string[]} an array of strings.
547
+ */
548
+ getWords(prefix = "", max = Number.MAX_SAFE_INTEGER, isAllWhenEmptyPrefix = false) {
549
+ prefix = this._caseProcess(prefix);
550
+ const words = [];
551
+ let found = 0;
552
+ function dfs(node, word) {
553
+ for (const char of node.children.keys()) {
554
+ const charNode = node.children.get(char);
555
+ if (charNode !== void 0) {
556
+ dfs(charNode, word.concat(char));
557
+ }
558
+ }
559
+ if (node.isEnd) {
560
+ if (found > max - 1) return;
561
+ words.push(word);
562
+ found++;
563
+ }
564
+ }
565
+ let startNode = this.root;
566
+ if (prefix) {
567
+ for (const c of prefix) {
568
+ const nodeC = startNode.children.get(c);
569
+ if (nodeC) {
570
+ startNode = nodeC;
571
+ } else {
572
+ return [];
573
+ }
574
+ }
575
+ }
576
+ if (isAllWhenEmptyPrefix || startNode !== this.root) dfs(startNode, prefix);
577
+ return words;
578
+ }
579
+ /**
580
+ * Time Complexity: O(n)
581
+ * Space Complexity: O(n)
582
+ *
583
+ * The `clone` function returns a new instance of the Trie class with the same values and case
584
+ * sensitivity as the original Trie.
585
+ * @returns A new instance of the Trie class is being returned.
586
+ */
587
+ clone() {
588
+ return new _Trie(this, { caseSensitive: this.caseSensitive, toElementFn: this.toElementFn });
589
+ }
590
+ /**
591
+ * Time Complexity: O(n)
592
+ * Space Complexity: O(n)
593
+ *
594
+ * The `filter` function takes a predicate function and returns a new array containing all the
595
+ * elements for which the predicate function returns true.
596
+ * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
597
+ * `word`, `index`, and `this`. It should return a boolean value indicating whether the current
598
+ * element should be included in the filtered results or not.
599
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
600
+ * specify the value of `this` within the `predicate` function. It is used when you want to bind a
601
+ * specific object as the context for the `predicate` function. If `thisArg` is provided, it will be
602
+ * @returns The `filter` method is returning an array of strings (`string[]`).
603
+ */
604
+ filter(predicate, thisArg) {
605
+ const results = new _Trie([], { toElementFn: this.toElementFn, caseSensitive: this.caseSensitive });
606
+ let index = 0;
607
+ for (const word of this) {
608
+ if (predicate.call(thisArg, word, index, this)) {
609
+ results.add(word);
610
+ }
611
+ index++;
612
+ }
613
+ return results;
614
+ }
615
+ /**
616
+ * Time Complexity: O(n)
617
+ * Space Complexity: O(n)
618
+ *
619
+ * The `map` function creates a new Trie by applying a callback function to each element in the
620
+ * current Trie.
621
+ * @param callback - The callback parameter is a function that will be called for each element in the
622
+ * Trie. It takes four arguments:
623
+ * @param [toElementFn] - The `toElementFn` parameter is an optional function that can be used to
624
+ * convert the raw element (`RM`) into a string representation. This can be useful if the raw element
625
+ * is not already a string or if you want to customize how the element is converted into a string. If
626
+ * this parameter is
627
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
628
+ * specify the value of `this` within the callback function. It is used to set the context or scope
629
+ * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
630
+ * value of
631
+ * @returns a new Trie object.
632
+ */
633
+ map(callback, toElementFn, thisArg) {
634
+ const newTrie = new _Trie([], { toElementFn, caseSensitive: this.caseSensitive });
635
+ let index = 0;
636
+ for (const word of this) {
637
+ newTrie.add(callback.call(thisArg, word, index, this));
638
+ index++;
639
+ }
640
+ return newTrie;
641
+ }
642
+ /**
643
+ * Time Complexity: O(n)
644
+ * Space Complexity: O(n)
645
+ *
646
+ * The function `_getIterator` returns an iterable iterator that performs a depth-first search on a
647
+ * trie data structure and yields all the paths to the end nodes.
648
+ */
649
+ *_getIterator() {
650
+ function* _dfs(node, path) {
651
+ if (node.isEnd) {
652
+ yield path;
653
+ }
654
+ for (const [char, childNode] of node.children) {
655
+ yield* _dfs(childNode, path + char);
656
+ }
657
+ }
658
+ yield* _dfs(this.root, "");
659
+ }
660
+ get _total() {
661
+ return this._size;
662
+ }
663
+ /**
664
+ * Time Complexity: O(l), where l is the length of the input string.
665
+ * Space Complexity: O(1) - Constant space.
666
+ *
667
+ * @param str
668
+ * @protected
669
+ */
670
+ _caseProcess(str) {
671
+ if (!this._caseSensitive) {
672
+ str = str.toLowerCase();
673
+ }
674
+ return str;
675
+ }
676
+ };
677
+ export {
678
+ Trie,
679
+ TrieNode
680
+ };
681
+ /**
682
+ * data-structure-typed
683
+ *
684
+ * @author Pablo Zeng
685
+ * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
686
+ * @license MIT License
687
+ */