stack-typed 2.4.5 → 2.5.0
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.
- package/README.md +6 -36
- package/dist/cjs/index.cjs +270 -100
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +269 -99
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +270 -100
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +269 -99
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +429 -78
- package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +212 -32
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
- package/dist/types/data-structures/graph/directed-graph.d.ts +219 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +204 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
- package/dist/types/data-structures/heap/heap.d.ts +287 -99
- package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +286 -44
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +331 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
- package/dist/types/data-structures/queue/deque.d.ts +272 -65
- package/dist/types/data-structures/queue/queue.d.ts +211 -42
- package/dist/types/data-structures/stack/stack.d.ts +174 -32
- package/dist/types/data-structures/trie/trie.d.ts +213 -43
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- package/dist/umd/stack-typed.js +267 -97
- package/dist/umd/stack-typed.js.map +1 -1
- package/dist/umd/stack-typed.min.js +1 -1
- package/dist/umd/stack-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/base/iterable-element-base.ts +4 -5
- package/src/data-structures/binary-tree/avl-tree.ts +134 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +302 -247
- package/src/data-structures/binary-tree/binary-tree.ts +429 -79
- package/src/data-structures/binary-tree/bst.ts +335 -34
- package/src/data-structures/binary-tree/red-black-tree.ts +290 -97
- package/src/data-structures/binary-tree/segment-tree.ts +372 -248
- package/src/data-structures/binary-tree/tree-map.ts +1284 -6
- package/src/data-structures/binary-tree/tree-multi-map.ts +1094 -211
- package/src/data-structures/binary-tree/tree-multi-set.ts +858 -65
- package/src/data-structures/binary-tree/tree-set.ts +1136 -9
- package/src/data-structures/graph/directed-graph.ts +219 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +204 -59
- package/src/data-structures/hash/hash-map.ts +230 -77
- package/src/data-structures/heap/heap.ts +287 -99
- package/src/data-structures/heap/max-heap.ts +46 -0
- package/src/data-structures/heap/min-heap.ts +59 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +286 -44
- package/src/data-structures/linked-list/singly-linked-list.ts +278 -65
- package/src/data-structures/linked-list/skip-linked-list.ts +689 -90
- package/src/data-structures/matrix/matrix.ts +416 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
- package/src/data-structures/priority-queue/priority-queue.ts +60 -0
- package/src/data-structures/queue/deque.ts +272 -65
- package/src/data-structures/queue/queue.ts +211 -42
- package/src/data-structures/stack/stack.ts +174 -32
- package/src/data-structures/trie/trie.ts +213 -43
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
package/README.md
CHANGED
|
@@ -35,42 +35,6 @@ yarn add stack-typed
|
|
|
35
35
|
|
|
36
36
|
[//]: # (No deletion!!! Start of Example Replace Section)
|
|
37
37
|
|
|
38
|
-
### basic Stack creation and push operation
|
|
39
|
-
```typescript
|
|
40
|
-
// Create a simple Stack with initial values
|
|
41
|
-
const stack = new Stack([1, 2, 3, 4, 5]);
|
|
42
|
-
|
|
43
|
-
// Verify the stack maintains insertion order (LIFO will be shown in pop)
|
|
44
|
-
console.log([...stack]); // [1, 2, 3, 4, 5];
|
|
45
|
-
|
|
46
|
-
// Check length
|
|
47
|
-
console.log(stack.size); // 5;
|
|
48
|
-
|
|
49
|
-
// Push a new element to the top
|
|
50
|
-
stack.push(6);
|
|
51
|
-
console.log(stack.size); // 6;
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
### Stack pop operation (LIFO - Last In First Out)
|
|
55
|
-
```typescript
|
|
56
|
-
const stack = new Stack<number>([10, 20, 30, 40, 50]);
|
|
57
|
-
|
|
58
|
-
// Peek at the top element without removing
|
|
59
|
-
const top = stack.peek();
|
|
60
|
-
console.log(top); // 50;
|
|
61
|
-
|
|
62
|
-
// Pop removes from the top (LIFO order)
|
|
63
|
-
const popped = stack.pop();
|
|
64
|
-
console.log(popped); // 50;
|
|
65
|
-
|
|
66
|
-
// Next pop gets the previous element
|
|
67
|
-
const next = stack.pop();
|
|
68
|
-
console.log(next); // 40;
|
|
69
|
-
|
|
70
|
-
// Verify length decreased
|
|
71
|
-
console.log(stack.size); // 3;
|
|
72
|
-
```
|
|
73
|
-
|
|
74
38
|
### Function Call Stack
|
|
75
39
|
```typescript
|
|
76
40
|
const functionStack = new Stack<string>();
|
|
@@ -190,6 +154,12 @@ yarn add stack-typed
|
|
|
190
154
|
console.log(stack.elements.join('/')); // 'c';
|
|
191
155
|
```
|
|
192
156
|
|
|
157
|
+
### Convert stack to array
|
|
158
|
+
```typescript
|
|
159
|
+
const stack = new Stack<number>([1, 2, 3]);
|
|
160
|
+
console.log(stack.toArray()); // [1, 2, 3];
|
|
161
|
+
```
|
|
162
|
+
|
|
193
163
|
[//]: # (No deletion!!! End of Example Replace Section)
|
|
194
164
|
|
|
195
165
|
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -3,55 +3,6 @@
|
|
|
3
3
|
var __defProp = Object.defineProperty;
|
|
4
4
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
5
5
|
|
|
6
|
-
// src/common/error.ts
|
|
7
|
-
var ERR = {
|
|
8
|
-
// Range / index
|
|
9
|
-
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
10
|
-
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
11
|
-
// Type / argument
|
|
12
|
-
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
13
|
-
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
14
|
-
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
15
|
-
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
16
|
-
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
17
|
-
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
18
|
-
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
19
|
-
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
20
|
-
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
21
|
-
// State / operation
|
|
22
|
-
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
23
|
-
// Matrix
|
|
24
|
-
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
25
|
-
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
26
|
-
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
27
|
-
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
28
|
-
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
// src/common/index.ts
|
|
32
|
-
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
33
|
-
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
34
|
-
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
35
|
-
return DFSOperation2;
|
|
36
|
-
})(DFSOperation || {});
|
|
37
|
-
var Range = class {
|
|
38
|
-
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
39
|
-
this.low = low;
|
|
40
|
-
this.high = high;
|
|
41
|
-
this.includeLow = includeLow;
|
|
42
|
-
this.includeHigh = includeHigh;
|
|
43
|
-
}
|
|
44
|
-
static {
|
|
45
|
-
__name(this, "Range");
|
|
46
|
-
}
|
|
47
|
-
// Determine whether a key is within the range
|
|
48
|
-
isInRange(key, comparator) {
|
|
49
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
50
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
51
|
-
return lowCheck && highCheck;
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
|
|
55
6
|
// src/data-structures/base/iterable-element-base.ts
|
|
56
7
|
var IterableElementBase = class {
|
|
57
8
|
static {
|
|
@@ -70,7 +21,7 @@ var IterableElementBase = class {
|
|
|
70
21
|
if (options) {
|
|
71
22
|
const { toElementFn } = options;
|
|
72
23
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
73
|
-
else if (toElementFn) throw new TypeError(
|
|
24
|
+
else if (toElementFn) throw new TypeError("toElementFn must be a function type");
|
|
74
25
|
}
|
|
75
26
|
}
|
|
76
27
|
/**
|
|
@@ -233,7 +184,7 @@ var IterableElementBase = class {
|
|
|
233
184
|
acc = initialValue;
|
|
234
185
|
} else {
|
|
235
186
|
const first = iter.next();
|
|
236
|
-
if (first.done) throw new TypeError(
|
|
187
|
+
if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
|
|
237
188
|
acc = first.value;
|
|
238
189
|
index = 1;
|
|
239
190
|
}
|
|
@@ -302,10 +253,23 @@ var Stack = class extends IterableElementBase {
|
|
|
302
253
|
return this._elements;
|
|
303
254
|
}
|
|
304
255
|
/**
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
256
|
+
* Get the number of stored elements.
|
|
257
|
+
* @remarks Time O(1), Space O(1)
|
|
258
|
+
* @returns Current size.
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
* @example
|
|
269
|
+
* // Get number of elements
|
|
270
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
271
|
+
* console.log(stack.size); // 3;
|
|
272
|
+
*/
|
|
309
273
|
get size() {
|
|
310
274
|
return this.elements.length;
|
|
311
275
|
}
|
|
@@ -323,36 +287,123 @@ var Stack = class extends IterableElementBase {
|
|
|
323
287
|
return new this(elements, options);
|
|
324
288
|
}
|
|
325
289
|
/**
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
290
|
+
* Check whether the stack is empty.
|
|
291
|
+
* @remarks Time O(1), Space O(1)
|
|
292
|
+
* @returns True if size is 0.
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
* @example
|
|
305
|
+
* // Check if stack has elements
|
|
306
|
+
* const stack = new Stack<number>();
|
|
307
|
+
* console.log(stack.isEmpty()); // true;
|
|
308
|
+
* stack.push(1);
|
|
309
|
+
* console.log(stack.isEmpty()); // false;
|
|
310
|
+
*/
|
|
330
311
|
isEmpty() {
|
|
331
312
|
return this.elements.length === 0;
|
|
332
313
|
}
|
|
333
314
|
/**
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
315
|
+
* Get the top element without removing it.
|
|
316
|
+
* @remarks Time O(1), Space O(1)
|
|
317
|
+
* @returns Top element or undefined.
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
* @example
|
|
330
|
+
* // View the top element without removing it
|
|
331
|
+
* const stack = new Stack<string>(['a', 'b', 'c']);
|
|
332
|
+
* console.log(stack.peek()); // 'c';
|
|
333
|
+
* console.log(stack.size); // 3;
|
|
334
|
+
*/
|
|
338
335
|
peek() {
|
|
339
336
|
return this.isEmpty() ? void 0 : this.elements[this.elements.length - 1];
|
|
340
337
|
}
|
|
341
338
|
/**
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
339
|
+
* Push one element onto the top.
|
|
340
|
+
* @remarks Time O(1), Space O(1)
|
|
341
|
+
* @param element - Element to push.
|
|
342
|
+
* @returns True when pushed.
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
* @example
|
|
355
|
+
* // basic Stack creation and push operation
|
|
356
|
+
* // Create a simple Stack with initial values
|
|
357
|
+
* const stack = new Stack([1, 2, 3, 4, 5]);
|
|
358
|
+
*
|
|
359
|
+
* // Verify the stack maintains insertion order (LIFO will be shown in pop)
|
|
360
|
+
* console.log([...stack]); // [1, 2, 3, 4, 5];
|
|
361
|
+
*
|
|
362
|
+
* // Check length
|
|
363
|
+
* console.log(stack.size); // 5;
|
|
364
|
+
*
|
|
365
|
+
* // Push a new element to the top
|
|
366
|
+
* stack.push(6);
|
|
367
|
+
* console.log(stack.size); // 6;
|
|
368
|
+
*/
|
|
347
369
|
push(element) {
|
|
348
370
|
this.elements.push(element);
|
|
349
371
|
return true;
|
|
350
372
|
}
|
|
351
373
|
/**
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
374
|
+
* Pop and return the top element.
|
|
375
|
+
* @remarks Time O(1), Space O(1)
|
|
376
|
+
* @returns Removed element or undefined.
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
* @example
|
|
389
|
+
* // Stack pop operation (LIFO - Last In First Out)
|
|
390
|
+
* const stack = new Stack<number>([10, 20, 30, 40, 50]);
|
|
391
|
+
*
|
|
392
|
+
* // Peek at the top element without removing
|
|
393
|
+
* const top = stack.peek();
|
|
394
|
+
* console.log(top); // 50;
|
|
395
|
+
*
|
|
396
|
+
* // Pop removes from the top (LIFO order)
|
|
397
|
+
* const popped = stack.pop();
|
|
398
|
+
* console.log(popped); // 50;
|
|
399
|
+
*
|
|
400
|
+
* // Next pop gets the previous element
|
|
401
|
+
* const next = stack.pop();
|
|
402
|
+
* console.log(next); // 40;
|
|
403
|
+
*
|
|
404
|
+
* // Verify length decreased
|
|
405
|
+
* console.log(stack.size); // 3;
|
|
406
|
+
*/
|
|
356
407
|
pop() {
|
|
357
408
|
return this.isEmpty() ? void 0 : this.elements.pop();
|
|
358
409
|
}
|
|
@@ -371,11 +422,24 @@ var Stack = class extends IterableElementBase {
|
|
|
371
422
|
return ans;
|
|
372
423
|
}
|
|
373
424
|
/**
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
425
|
+
* Delete the first occurrence of a specific element.
|
|
426
|
+
* @remarks Time O(N), Space O(1)
|
|
427
|
+
* @param element - Element to remove (using the configured equality).
|
|
428
|
+
* @returns True if an element was removed.
|
|
429
|
+
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
|
|
436
|
+
|
|
437
|
+
* @example
|
|
438
|
+
* // Remove element
|
|
439
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
440
|
+
* stack.delete(2);
|
|
441
|
+
* console.log(stack.toArray()); // [1, 3];
|
|
442
|
+
*/
|
|
379
443
|
delete(element) {
|
|
380
444
|
const idx = this._indexOfByEquals(element);
|
|
381
445
|
return this.deleteAt(idx);
|
|
@@ -407,30 +471,74 @@ var Stack = class extends IterableElementBase {
|
|
|
407
471
|
return false;
|
|
408
472
|
}
|
|
409
473
|
/**
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
474
|
+
* Remove all elements and reset storage.
|
|
475
|
+
* @remarks Time O(1), Space O(1)
|
|
476
|
+
* @returns void
|
|
477
|
+
|
|
478
|
+
|
|
479
|
+
|
|
480
|
+
|
|
481
|
+
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
|
|
485
|
+
|
|
486
|
+
* @example
|
|
487
|
+
* // Remove all elements
|
|
488
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
489
|
+
* stack.clear();
|
|
490
|
+
* console.log(stack.isEmpty()); // true;
|
|
491
|
+
*/
|
|
414
492
|
clear() {
|
|
415
493
|
this._elements = [];
|
|
416
494
|
}
|
|
417
495
|
/**
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
496
|
+
* Deep clone this stack.
|
|
497
|
+
* @remarks Time O(N), Space O(N)
|
|
498
|
+
* @returns A new stack with the same content.
|
|
499
|
+
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
|
|
504
|
+
|
|
505
|
+
|
|
506
|
+
|
|
507
|
+
|
|
508
|
+
* @example
|
|
509
|
+
* // Create independent copy
|
|
510
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
511
|
+
* const copy = stack.clone();
|
|
512
|
+
* copy.pop();
|
|
513
|
+
* console.log(stack.size); // 3;
|
|
514
|
+
* console.log(copy.size); // 2;
|
|
515
|
+
*/
|
|
422
516
|
clone() {
|
|
423
517
|
const out = this._createInstance({ toElementFn: this.toElementFn });
|
|
424
518
|
for (const v of this) out.push(v);
|
|
425
519
|
return out;
|
|
426
520
|
}
|
|
427
521
|
/**
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
522
|
+
* Filter elements into a new stack of the same class.
|
|
523
|
+
* @remarks Time O(N), Space O(N)
|
|
524
|
+
* @param predicate - Predicate (value, index, stack) → boolean to keep value.
|
|
525
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
526
|
+
* @returns A new stack with kept values.
|
|
527
|
+
|
|
528
|
+
|
|
529
|
+
|
|
530
|
+
|
|
531
|
+
|
|
532
|
+
|
|
533
|
+
|
|
534
|
+
|
|
535
|
+
|
|
536
|
+
* @example
|
|
537
|
+
* // Filter elements
|
|
538
|
+
* const stack = new Stack<number>([1, 2, 3, 4, 5]);
|
|
539
|
+
* const evens = stack.filter(x => x % 2 === 0);
|
|
540
|
+
* console.log(evens.toArray()); // [2, 4];
|
|
541
|
+
*/
|
|
434
542
|
filter(predicate, thisArg) {
|
|
435
543
|
const out = this._createInstance({ toElementFn: this.toElementFn });
|
|
436
544
|
let index = 0;
|
|
@@ -457,15 +565,28 @@ var Stack = class extends IterableElementBase {
|
|
|
457
565
|
return out;
|
|
458
566
|
}
|
|
459
567
|
/**
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
568
|
+
* Map values into a new stack (possibly different element type).
|
|
569
|
+
* @remarks Time O(N), Space O(N)
|
|
570
|
+
* @template EM
|
|
571
|
+
* @template RM
|
|
572
|
+
* @param callback - Mapping function (value, index, stack) → newElement.
|
|
573
|
+
* @param [options] - Options for the output stack (e.g., toElementFn).
|
|
574
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
575
|
+
* @returns A new Stack with mapped elements.
|
|
576
|
+
|
|
577
|
+
|
|
578
|
+
|
|
579
|
+
|
|
580
|
+
|
|
581
|
+
|
|
582
|
+
|
|
583
|
+
|
|
584
|
+
* @example
|
|
585
|
+
* // Transform elements
|
|
586
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
587
|
+
* const doubled = stack.map(x => x * 2);
|
|
588
|
+
* console.log(doubled.toArray()); // [2, 4, 6];
|
|
589
|
+
*/
|
|
469
590
|
map(callback, options, thisArg) {
|
|
470
591
|
const out = this._createLike([], { ...options ?? {} });
|
|
471
592
|
let index = 0;
|
|
@@ -527,6 +648,55 @@ var Stack = class extends IterableElementBase {
|
|
|
527
648
|
for (let i = 0; i < this.elements.length; i++) yield this.elements[i];
|
|
528
649
|
}
|
|
529
650
|
};
|
|
651
|
+
|
|
652
|
+
// src/common/error.ts
|
|
653
|
+
var ERR = {
|
|
654
|
+
// Range / index
|
|
655
|
+
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
656
|
+
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
657
|
+
// Type / argument
|
|
658
|
+
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
659
|
+
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
660
|
+
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
661
|
+
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
662
|
+
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
663
|
+
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
664
|
+
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
665
|
+
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
666
|
+
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
667
|
+
// State / operation
|
|
668
|
+
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
669
|
+
// Matrix
|
|
670
|
+
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
671
|
+
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
672
|
+
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
673
|
+
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
674
|
+
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
675
|
+
};
|
|
676
|
+
|
|
677
|
+
// src/common/index.ts
|
|
678
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
679
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
680
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
681
|
+
return DFSOperation2;
|
|
682
|
+
})(DFSOperation || {});
|
|
683
|
+
var Range = class {
|
|
684
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
685
|
+
this.low = low;
|
|
686
|
+
this.high = high;
|
|
687
|
+
this.includeLow = includeLow;
|
|
688
|
+
this.includeHigh = includeHigh;
|
|
689
|
+
}
|
|
690
|
+
static {
|
|
691
|
+
__name(this, "Range");
|
|
692
|
+
}
|
|
693
|
+
// Determine whether a key is within the range
|
|
694
|
+
isInRange(key, comparator) {
|
|
695
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
696
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
697
|
+
return lowCheck && highCheck;
|
|
698
|
+
}
|
|
699
|
+
};
|
|
530
700
|
/**
|
|
531
701
|
* data-structure-typed
|
|
532
702
|
*
|