stack-typed 2.0.5 → 2.1.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 (101) hide show
  1. package/dist/data-structures/base/iterable-element-base.d.ts +186 -83
  2. package/dist/data-structures/base/iterable-element-base.js +149 -107
  3. package/dist/data-structures/base/iterable-entry-base.d.ts +95 -119
  4. package/dist/data-structures/base/iterable-entry-base.js +59 -116
  5. package/dist/data-structures/base/linear-base.d.ts +250 -192
  6. package/dist/data-structures/base/linear-base.js +137 -274
  7. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
  8. package/dist/data-structures/binary-tree/avl-tree-counter.js +171 -205
  9. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
  10. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +135 -87
  11. package/dist/data-structures/binary-tree/avl-tree.d.ts +138 -149
  12. package/dist/data-structures/binary-tree/avl-tree.js +208 -195
  13. package/dist/data-structures/binary-tree/binary-tree.d.ts +476 -632
  14. package/dist/data-structures/binary-tree/binary-tree.js +602 -873
  15. package/dist/data-structures/binary-tree/bst.d.ts +258 -306
  16. package/dist/data-structures/binary-tree/bst.js +505 -481
  17. package/dist/data-structures/binary-tree/red-black-tree.d.ts +107 -179
  18. package/dist/data-structures/binary-tree/red-black-tree.js +114 -209
  19. package/dist/data-structures/binary-tree/tree-counter.d.ts +132 -154
  20. package/dist/data-structures/binary-tree/tree-counter.js +172 -203
  21. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
  22. package/dist/data-structures/binary-tree/tree-multi-map.js +105 -85
  23. package/dist/data-structures/graph/abstract-graph.d.ts +238 -233
  24. package/dist/data-structures/graph/abstract-graph.js +267 -237
  25. package/dist/data-structures/graph/directed-graph.d.ts +108 -224
  26. package/dist/data-structures/graph/directed-graph.js +146 -233
  27. package/dist/data-structures/graph/map-graph.d.ts +49 -55
  28. package/dist/data-structures/graph/map-graph.js +56 -59
  29. package/dist/data-structures/graph/undirected-graph.d.ts +103 -146
  30. package/dist/data-structures/graph/undirected-graph.js +129 -149
  31. package/dist/data-structures/hash/hash-map.d.ts +164 -338
  32. package/dist/data-structures/hash/hash-map.js +270 -457
  33. package/dist/data-structures/heap/heap.d.ts +214 -289
  34. package/dist/data-structures/heap/heap.js +340 -349
  35. package/dist/data-structures/heap/max-heap.d.ts +11 -47
  36. package/dist/data-structures/heap/max-heap.js +11 -66
  37. package/dist/data-structures/heap/min-heap.d.ts +12 -47
  38. package/dist/data-structures/heap/min-heap.js +11 -66
  39. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
  40. package/dist/data-structures/linked-list/doubly-linked-list.js +368 -494
  41. package/dist/data-structures/linked-list/singly-linked-list.d.ts +261 -310
  42. package/dist/data-structures/linked-list/singly-linked-list.js +447 -466
  43. package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -107
  44. package/dist/data-structures/linked-list/skip-linked-list.js +0 -100
  45. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
  46. package/dist/data-structures/priority-queue/max-priority-queue.js +11 -78
  47. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
  48. package/dist/data-structures/priority-queue/min-priority-queue.js +10 -79
  49. package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -61
  50. package/dist/data-structures/priority-queue/priority-queue.js +8 -83
  51. package/dist/data-structures/queue/deque.d.ts +227 -254
  52. package/dist/data-structures/queue/deque.js +309 -348
  53. package/dist/data-structures/queue/queue.d.ts +180 -201
  54. package/dist/data-structures/queue/queue.js +265 -248
  55. package/dist/data-structures/stack/stack.d.ts +124 -102
  56. package/dist/data-structures/stack/stack.js +181 -125
  57. package/dist/data-structures/trie/trie.d.ts +164 -165
  58. package/dist/data-structures/trie/trie.js +189 -172
  59. package/dist/interfaces/binary-tree.d.ts +56 -6
  60. package/dist/interfaces/graph.d.ts +16 -0
  61. package/dist/types/data-structures/base/base.d.ts +1 -1
  62. package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -0
  63. package/dist/types/utils/utils.d.ts +1 -0
  64. package/dist/utils/utils.d.ts +1 -1
  65. package/dist/utils/utils.js +2 -1
  66. package/package.json +2 -2
  67. package/src/data-structures/base/iterable-element-base.ts +238 -115
  68. package/src/data-structures/base/iterable-entry-base.ts +96 -120
  69. package/src/data-structures/base/linear-base.ts +271 -277
  70. package/src/data-structures/binary-tree/avl-tree-counter.ts +196 -217
  71. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +188 -102
  72. package/src/data-structures/binary-tree/avl-tree.ts +237 -206
  73. package/src/data-structures/binary-tree/binary-tree.ts +665 -896
  74. package/src/data-structures/binary-tree/bst.ts +565 -572
  75. package/src/data-structures/binary-tree/red-black-tree.ts +157 -223
  76. package/src/data-structures/binary-tree/tree-counter.ts +195 -219
  77. package/src/data-structures/binary-tree/tree-multi-map.ts +127 -98
  78. package/src/data-structures/graph/abstract-graph.ts +339 -264
  79. package/src/data-structures/graph/directed-graph.ts +146 -236
  80. package/src/data-structures/graph/map-graph.ts +63 -60
  81. package/src/data-structures/graph/undirected-graph.ts +129 -152
  82. package/src/data-structures/hash/hash-map.ts +274 -496
  83. package/src/data-structures/heap/heap.ts +389 -402
  84. package/src/data-structures/heap/max-heap.ts +12 -76
  85. package/src/data-structures/heap/min-heap.ts +13 -76
  86. package/src/data-structures/linked-list/doubly-linked-list.ts +426 -530
  87. package/src/data-structures/linked-list/singly-linked-list.ts +495 -517
  88. package/src/data-structures/linked-list/skip-linked-list.ts +1 -108
  89. package/src/data-structures/priority-queue/max-priority-queue.ts +12 -87
  90. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -88
  91. package/src/data-structures/priority-queue/priority-queue.ts +3 -92
  92. package/src/data-structures/queue/deque.ts +381 -357
  93. package/src/data-structures/queue/queue.ts +310 -264
  94. package/src/data-structures/stack/stack.ts +217 -131
  95. package/src/data-structures/trie/trie.ts +240 -175
  96. package/src/interfaces/binary-tree.ts +240 -6
  97. package/src/interfaces/graph.ts +37 -0
  98. package/src/types/data-structures/base/base.ts +5 -5
  99. package/src/types/data-structures/graph/abstract-graph.ts +5 -0
  100. package/src/types/utils/utils.ts +2 -0
  101. package/src/utils/utils.ts +9 -14
@@ -1,27 +1,32 @@
1
- import { EntryCallback, ReduceEntryCallback } from '../../types';
2
-
1
+ import type { EntryCallback, ReduceEntryCallback } from '../../types';
2
+
3
+ /**
4
+ * Iterable view over key-value entries.
5
+ * @template K - Key type.
6
+ * @template V - Value type.
7
+ * @remarks Time O(1), Space O(1)
8
+ */
3
9
  export abstract class IterableEntryBase<K = any, V = any> {
10
+ /**
11
+ * Total number of entries.
12
+ * @returns Entry count.
13
+ * @remarks Time O(1), Space O(1)
14
+ */
4
15
  abstract get size(): number;
5
16
 
6
17
  /**
7
- * Time Complexity: O(n)
8
- * Space Complexity: O(1)
9
- *
10
- * The function is an implementation of the Symbol.iterator method that returns an iterable iterator.
11
- * @param {any[]} args - The `args` parameter in the code snippet represents a rest parameter. It
12
- * allows the function to accept any number of arguments as an array. In this case, the `args`
13
- * parameter is used to pass any additional arguments to the `_getIterator` method.
18
+ * Default iterator yielding `[key, value]` entries.
19
+ * @returns Iterator of `[K, V]`.
20
+ * @remarks Time O(n) to iterate, Space O(1)
14
21
  */
15
22
  *[Symbol.iterator](...args: any[]): IterableIterator<[K, V]> {
16
23
  yield* this._getIterator(...args);
17
24
  }
18
25
 
19
26
  /**
20
- * Time Complexity: O(n)
21
- * Space Complexity: O(n)
22
- *
23
- * The function returns an iterator that yields key-value pairs from the object, where the value can
24
- * be undefined.
27
+ * Iterate over `[key, value]` pairs (may yield `undefined` values).
28
+ * @returns Iterator of `[K, V | undefined]`.
29
+ * @remarks Time O(n), Space O(1)
25
30
  */
26
31
  *entries(): IterableIterator<[K, V | undefined]> {
27
32
  for (const item of this) {
@@ -30,10 +35,9 @@ export abstract class IterableEntryBase<K = any, V = any> {
30
35
  }
31
36
 
32
37
  /**
33
- * Time Complexity: O(n)
34
- * Space Complexity: O(n)
35
- *
36
- * The function returns an iterator that yields the keys of a data structure.
38
+ * Iterate over keys only.
39
+ * @returns Iterator of keys.
40
+ * @remarks Time O(n), Space O(1)
37
41
  */
38
42
  *keys(): IterableIterator<K> {
39
43
  for (const item of this) {
@@ -42,10 +46,9 @@ export abstract class IterableEntryBase<K = any, V = any> {
42
46
  }
43
47
 
44
48
  /**
45
- * Time Complexity: O(n)
46
- * Space Complexity: O(n)
47
- *
48
- * The function returns an iterator that yields the values of a collection.
49
+ * Iterate over values only.
50
+ * @returns Iterator of values.
51
+ * @remarks Time O(n), Space O(1)
49
52
  */
50
53
  *values(): IterableIterator<V> {
51
54
  for (const item of this) {
@@ -54,18 +57,11 @@ export abstract class IterableEntryBase<K = any, V = any> {
54
57
  }
55
58
 
56
59
  /**
57
- * Time Complexity: O(n)
58
- * Space Complexity: O(1)
59
- *
60
- * The `every` function checks if every element in a collection satisfies a given condition.
61
- * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
62
- * `value`, `key`, and `index`. It should return a boolean value indicating whether the condition is
63
- * met for the current element in the iteration.
64
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
65
- * to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
66
- * passed as the first argument to the `predicate` function. If `thisArg` is not provided
67
- * @returns The `every` method is returning a boolean value. It returns `true` if every element in
68
- * the collection satisfies the provided predicate function, and `false` otherwise.
60
+ * Test whether all entries satisfy the predicate.
61
+ * @param predicate - `(key, value, index, self) => boolean`.
62
+ * @param thisArg - Optional `this` for callback.
63
+ * @returns `true` if all pass; otherwise `false`.
64
+ * @remarks Time O(n), Space O(1)
69
65
  */
70
66
  every(predicate: EntryCallback<K, V, boolean>, thisArg?: any): boolean {
71
67
  let index = 0;
@@ -78,19 +74,11 @@ export abstract class IterableEntryBase<K = any, V = any> {
78
74
  }
79
75
 
80
76
  /**
81
- * Time Complexity: O(n)
82
- * Space Complexity: O(1)
83
- *
84
- * The "some" function iterates over a collection and returns true if at least one element satisfies
85
- * a given predicate.
86
- * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
87
- * `value`, `key`, and `index`. It should return a boolean value indicating whether the condition is
88
- * met for the current element in the iteration.
89
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
90
- * to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
91
- * it will be passed as the first argument to the `predicate` function. If `thisArg` is
92
- * @returns a boolean value. It returns true if the predicate function returns true for any pair in
93
- * the collection, and false otherwise.
77
+ * Test whether any entry satisfies the predicate.
78
+ * @param predicate - `(key, value, index, self) => boolean`.
79
+ * @param thisArg - Optional `this` for callback.
80
+ * @returns `true` if any passes; otherwise `false`.
81
+ * @remarks Time O(n), Space O(1)
94
82
  */
95
83
  some(predicate: EntryCallback<K, V, boolean>, thisArg?: any): boolean {
96
84
  let index = 0;
@@ -103,17 +91,10 @@ export abstract class IterableEntryBase<K = any, V = any> {
103
91
  }
104
92
 
105
93
  /**
106
- * Time Complexity: O(n)
107
- * Space Complexity: O(1)
108
- *
109
- * The `forEach` function iterates over each key-value pair in a collection and executes a callback
110
- * function for each pair.
111
- * @param callbackfn - The callback function that will be called for each element in the collection.
112
- * It takes four parameters: the value of the current element, the key of the current element, the
113
- * index of the current element, and the collection itself.
114
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
115
- * specify the value of `this` within the callback function. If `thisArg` is provided, it will be
116
- * used as the `this` value when calling the callback function. If `thisArg` is not provided, `
94
+ * Visit each entry, left-to-right.
95
+ * @param callbackfn - `(key, value, index, self) => void`.
96
+ * @param thisArg - Optional `this` for callback.
97
+ * @remarks Time O(n), Space O(1)
117
98
  */
118
99
  forEach(callbackfn: EntryCallback<K, V, void>, thisArg?: any): void {
119
100
  let index = 0;
@@ -124,21 +105,11 @@ export abstract class IterableEntryBase<K = any, V = any> {
124
105
  }
125
106
 
126
107
  /**
127
- * Time Complexity: O(n)
128
- * Space Complexity: O(1)
129
- *
130
- * The `find` function iterates over the entries of a collection and returns the first value for
131
- * which the callback function returns true.
132
- * @param callbackfn - The callback function that will be called for each entry in the collection. It
133
- * takes three arguments: the value of the entry, the key of the entry, and the index of the entry in
134
- * the collection. It should return a boolean value indicating whether the current entry matches the
135
- * desired condition.
136
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
137
- * to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
138
- * be passed as the `this` value to the `callbackfn` function. If `thisArg
139
- * @returns The method `find` returns the value of the first element in the iterable that satisfies
140
- * the provided callback function. If no element satisfies the callback function, `undefined` is
141
- * returned.
108
+ * Find the first entry that matches a predicate.
109
+ * @param callbackfn - `(key, value, index, self) => boolean`.
110
+ * @param thisArg - Optional `this` for callback.
111
+ * @returns Matching `[key, value]` or `undefined`.
112
+ * @remarks Time O(n), Space O(1)
142
113
  */
143
114
  find(callbackfn: EntryCallback<K, V, boolean>, thisArg?: any): [K, V] | undefined {
144
115
  let index = 0;
@@ -150,14 +121,10 @@ export abstract class IterableEntryBase<K = any, V = any> {
150
121
  }
151
122
 
152
123
  /**
153
- * Time Complexity: O(n)
154
- * Space Complexity: O(1)
155
- *
156
- * The function checks if a given key exists in a collection.
157
- * @param {K} key - The parameter "key" is of type K, which means it can be any type. It represents
158
- * the key that we want to check for existence in the data structure.
159
- * @returns a boolean value. It returns true if the key is found in the collection, and false
160
- * otherwise.
124
+ * Whether the given key exists.
125
+ * @param key - Key to test.
126
+ * @returns `true` if found; otherwise `false`.
127
+ * @remarks Time O(n) generic, Space O(1)
161
128
  */
162
129
  has(key: K): boolean {
163
130
  for (const item of this) {
@@ -168,13 +135,10 @@ export abstract class IterableEntryBase<K = any, V = any> {
168
135
  }
169
136
 
170
137
  /**
171
- * Time Complexity: O(n)
172
- * Space Complexity: O(1)
173
- *
174
- * The function checks if a given value exists in a collection.
175
- * @param {V} value - The parameter "value" is the value that we want to check if it exists in the
176
- * collection.
177
- * @returns a boolean value, either true or false.
138
+ * Whether there exists an entry with the given value.
139
+ * @param value - Value to test.
140
+ * @returns `true` if found; otherwise `false`.
141
+ * @remarks Time O(n), Space O(1)
178
142
  */
179
143
  hasValue(value: V): boolean {
180
144
  for (const [, elementValue] of this) {
@@ -184,14 +148,10 @@ export abstract class IterableEntryBase<K = any, V = any> {
184
148
  }
185
149
 
186
150
  /**
187
- * Time Complexity: O(n)
188
- * Space Complexity: O(1)
189
- *
190
- * The `get` function retrieves the value associated with a given key from a collection.
191
- * @param {K} key - K (the type of the key) - This parameter represents the key that is being
192
- * searched for in the collection.
193
- * @returns The `get` method returns the value associated with the specified key if it exists in the
194
- * collection, otherwise it returns `undefined`.
151
+ * Get the value under a key.
152
+ * @param key - Key to look up.
153
+ * @returns Value or `undefined`.
154
+ * @remarks Time O(n) generic, Space O(1)
195
155
  */
196
156
  get(key: K): V | undefined {
197
157
  for (const item of this) {
@@ -202,20 +162,11 @@ export abstract class IterableEntryBase<K = any, V = any> {
202
162
  }
203
163
 
204
164
  /**
205
- * Time Complexity: O(n)
206
- * Space Complexity: O(1)
207
- *
208
- * The `reduce` function iterates over key-value pairs and applies a callback function to each pair,
209
- * accumulating a single value.
210
- * @param callbackfn - The callback function that will be called for each element in the collection.
211
- * It takes four arguments: the current accumulator value, the current value of the element, the key
212
- * of the element, and the index of the element in the collection. It should return the updated
213
- * accumulator value.
214
- * @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It
215
- * is the value that will be used as the first argument to the `callbackfn` function when reducing
216
- * the elements of the collection.
217
- * @returns The `reduce` method is returning the final value of the accumulator after iterating over
218
- * all the elements in the collection.
165
+ * Reduce entries into a single accumulator.
166
+ * @param callbackfn - `(acc, value, key, index, self) => acc`.
167
+ * @param initialValue - Initial accumulator.
168
+ * @returns Final accumulator.
169
+ * @remarks Time O(n), Space O(1)
219
170
  */
220
171
  reduce<U>(callbackfn: ReduceEntryCallback<K, V, U>, initialValue: U): U {
221
172
  let accumulator = initialValue;
@@ -228,34 +179,59 @@ export abstract class IterableEntryBase<K = any, V = any> {
228
179
  }
229
180
 
230
181
  /**
231
- * Time Complexity: O(n)
232
- * Space Complexity: O(n)
233
- *
234
- * The print function logs the elements of an array to the console.
182
+ * Visualize the iterable as an array of `[key, value]` pairs (or a custom string).
183
+ * @returns Array of entries (default) or a string.
184
+ * @remarks Time O(n), Space O(n)
235
185
  */
236
186
  toVisual(): [K, V][] | string {
237
187
  return [...this];
238
188
  }
239
189
 
240
190
  /**
241
- * Time Complexity: O(n)
242
- * Space Complexity: O(n)
243
- *
244
- * The print function logs the elements of an array to the console.
191
+ * Print a human-friendly representation to the console.
192
+ * @remarks Time O(n), Space O(n)
245
193
  */
246
194
  print(): void {
247
195
  console.log(this.toVisual());
248
196
  }
249
197
 
198
+ /**
199
+ * Whether there are no entries.
200
+ * @returns `true` if empty; `false` otherwise.
201
+ * @remarks Time O(1) typical, Space O(1)
202
+ */
250
203
  abstract isEmpty(): boolean;
251
204
 
205
+ /**
206
+ * Remove all entries.
207
+ * @remarks Time O(n) typical, Space O(1)
208
+ */
252
209
  abstract clear(): void;
253
210
 
254
- abstract clone(): any;
211
+ /**
212
+ * Deep clone preserving the concrete subtype.
213
+ * @returns A new instance of the same concrete class (`this` type).
214
+ * @remarks Time O(n) typical, Space O(n)
215
+ */
216
+ abstract clone(): this;
255
217
 
218
+ /**
219
+ * Map entries using an implementation-specific strategy.
220
+ * @remarks Time O(n), Space O(n)
221
+ */
256
222
  abstract map(...args: any[]): any;
257
223
 
258
- abstract filter(...args: any[]): any;
224
+ /**
225
+ * Filter entries and return the same-species structure.
226
+ * @returns A new instance of the same concrete class (`this` type).
227
+ * @remarks Time O(n), Space O(n)
228
+ */
229
+ abstract filter(...args: any[]): this;
259
230
 
231
+ /**
232
+ * Underlying iterator for the default iteration protocol.
233
+ * @returns Iterator of `[K, V]`.
234
+ * @remarks Time O(n), Space O(1)
235
+ */
260
236
  protected abstract _getIterator(...args: any[]): IterableIterator<[K, V]>;
261
237
  }