stack-typed 2.4.5 → 2.5.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.
- package/README.md +6 -36
- package/dist/cjs/index.cjs +480 -100
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +479 -99
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +480 -100
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +479 -99
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
- package/dist/types/data-structures/base/linear-base.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +380 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +487 -147
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +956 -80
- package/dist/types/data-structures/binary-tree/bst.d.ts +816 -29
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +610 -31
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +326 -135
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +3781 -6
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3607 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2874 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +3528 -6
- package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
- package/dist/types/data-structures/graph/directed-graph.d.ts +429 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +393 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +473 -89
- package/dist/types/data-structures/heap/heap.d.ts +581 -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 +646 -47
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +596 -68
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +793 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +499 -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 +593 -71
- package/dist/types/data-structures/queue/queue.d.ts +463 -42
- package/dist/types/data-structures/stack/stack.d.ts +384 -32
- package/dist/types/data-structures/trie/trie.d.ts +470 -48
- package/dist/types/interfaces/graph.d.ts +1 -1
- package/dist/types/types/common.d.ts +2 -2
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
- package/dist/types/types/utils/validate-type.d.ts +4 -4
- package/dist/umd/stack-typed.js +477 -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/index.ts +1 -0
- package/src/data-structures/base/iterable-element-base.ts +4 -5
- package/src/data-structures/base/iterable-entry-base.ts +8 -8
- package/src/data-structures/base/linear-base.ts +3 -3
- package/src/data-structures/binary-tree/avl-tree.ts +386 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +596 -247
- package/src/data-structures/binary-tree/binary-tree.ts +956 -81
- package/src/data-structures/binary-tree/bst.ts +840 -35
- package/src/data-structures/binary-tree/red-black-tree.ts +689 -97
- package/src/data-structures/binary-tree/segment-tree.ts +498 -249
- package/src/data-structures/binary-tree/tree-map.ts +3784 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +3614 -211
- package/src/data-structures/binary-tree/tree-multi-set.ts +2874 -65
- package/src/data-structures/binary-tree/tree-set.ts +3531 -10
- package/src/data-structures/graph/abstract-graph.ts +4 -4
- package/src/data-structures/graph/directed-graph.ts +429 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +393 -59
- package/src/data-structures/hash/hash-map.ts +476 -92
- package/src/data-structures/heap/heap.ts +581 -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 +646 -47
- package/src/data-structures/linked-list/singly-linked-list.ts +596 -68
- package/src/data-structures/linked-list/skip-linked-list.ts +1067 -90
- package/src/data-structures/matrix/matrix.ts +584 -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 +592 -70
- package/src/data-structures/queue/queue.ts +463 -42
- package/src/data-structures/stack/stack.ts +384 -32
- package/src/data-structures/trie/trie.ts +470 -48
- package/src/interfaces/graph.ts +1 -1
- package/src/types/common.ts +2 -2
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/heap/heap.ts +1 -0
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
- package/src/types/utils/validate-type.ts +4 -4
|
@@ -3,54 +3,6 @@ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { en
|
|
|
3
3
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
4
4
|
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
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 _Range {
|
|
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
|
-
// Determine whether a key is within the range
|
|
45
|
-
isInRange(key, comparator) {
|
|
46
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
47
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
48
|
-
return lowCheck && highCheck;
|
|
49
|
-
}
|
|
50
|
-
};
|
|
51
|
-
__name(_Range, "Range");
|
|
52
|
-
var Range = _Range;
|
|
53
|
-
|
|
54
6
|
// src/data-structures/base/iterable-element-base.ts
|
|
55
7
|
var _IterableElementBase = class _IterableElementBase {
|
|
56
8
|
/**
|
|
@@ -73,7 +25,7 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
73
25
|
if (options) {
|
|
74
26
|
const { toElementFn } = options;
|
|
75
27
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
76
|
-
else if (toElementFn) throw new TypeError(
|
|
28
|
+
else if (toElementFn) throw new TypeError("toElementFn must be a function type");
|
|
77
29
|
}
|
|
78
30
|
}
|
|
79
31
|
/**
|
|
@@ -229,7 +181,7 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
229
181
|
acc = initialValue;
|
|
230
182
|
} else {
|
|
231
183
|
const first = iter.next();
|
|
232
|
-
if (first.done) throw new TypeError(
|
|
184
|
+
if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
|
|
233
185
|
acc = first.value;
|
|
234
186
|
index = 1;
|
|
235
187
|
}
|
|
@@ -297,10 +249,44 @@ var _Stack = class _Stack extends IterableElementBase {
|
|
|
297
249
|
return this._elements;
|
|
298
250
|
}
|
|
299
251
|
/**
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
252
|
+
* Get the number of stored elements.
|
|
253
|
+
* @remarks Time O(1), Space O(1)
|
|
254
|
+
* @returns Current size.
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
* @example
|
|
286
|
+
* // Get number of elements
|
|
287
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
288
|
+
* console.log(stack.size); // 3;
|
|
289
|
+
*/
|
|
304
290
|
get size() {
|
|
305
291
|
return this.elements.length;
|
|
306
292
|
}
|
|
@@ -318,36 +304,207 @@ var _Stack = class _Stack extends IterableElementBase {
|
|
|
318
304
|
return new this(elements, options);
|
|
319
305
|
}
|
|
320
306
|
/**
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
307
|
+
* Check whether the stack is empty.
|
|
308
|
+
* @remarks Time O(1), Space O(1)
|
|
309
|
+
* @returns True if size is 0.
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
* @example
|
|
343
|
+
* // Check if stack has elements
|
|
344
|
+
* const stack = new Stack<number>();
|
|
345
|
+
* console.log(stack.isEmpty()); // true;
|
|
346
|
+
* stack.push(1);
|
|
347
|
+
* console.log(stack.isEmpty()); // false;
|
|
348
|
+
*/
|
|
325
349
|
isEmpty() {
|
|
326
350
|
return this.elements.length === 0;
|
|
327
351
|
}
|
|
328
352
|
/**
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
353
|
+
* Get the top element without removing it.
|
|
354
|
+
* @remarks Time O(1), Space O(1)
|
|
355
|
+
* @returns Top element or undefined.
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
* @example
|
|
389
|
+
* // View the top element without removing it
|
|
390
|
+
* const stack = new Stack<string>(['a', 'b', 'c']);
|
|
391
|
+
* console.log(stack.peek()); // 'c';
|
|
392
|
+
* console.log(stack.size); // 3;
|
|
393
|
+
*/
|
|
333
394
|
peek() {
|
|
334
395
|
return this.isEmpty() ? void 0 : this.elements[this.elements.length - 1];
|
|
335
396
|
}
|
|
336
397
|
/**
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
398
|
+
* Push one element onto the top.
|
|
399
|
+
* @remarks Time O(1), Space O(1)
|
|
400
|
+
* @param element - Element to push.
|
|
401
|
+
* @returns True when pushed.
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
* @example
|
|
435
|
+
* // basic Stack creation and push operation
|
|
436
|
+
* // Create a simple Stack with initial values
|
|
437
|
+
* const stack = new Stack([1, 2, 3, 4, 5]);
|
|
438
|
+
*
|
|
439
|
+
* // Verify the stack maintains insertion order (LIFO will be shown in pop)
|
|
440
|
+
* console.log([...stack]); // [1, 2, 3, 4, 5];
|
|
441
|
+
*
|
|
442
|
+
* // Check length
|
|
443
|
+
* console.log(stack.size); // 5;
|
|
444
|
+
*
|
|
445
|
+
* // Push a new element to the top
|
|
446
|
+
* stack.push(6);
|
|
447
|
+
* console.log(stack.size); // 6;
|
|
448
|
+
*/
|
|
342
449
|
push(element) {
|
|
343
450
|
this.elements.push(element);
|
|
344
451
|
return true;
|
|
345
452
|
}
|
|
346
453
|
/**
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
454
|
+
* Pop and return the top element.
|
|
455
|
+
* @remarks Time O(1), Space O(1)
|
|
456
|
+
* @returns Removed element or undefined.
|
|
457
|
+
|
|
458
|
+
|
|
459
|
+
|
|
460
|
+
|
|
461
|
+
|
|
462
|
+
|
|
463
|
+
|
|
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
|
+
* @example
|
|
490
|
+
* // Stack pop operation (LIFO - Last In First Out)
|
|
491
|
+
* const stack = new Stack<number>([10, 20, 30, 40, 50]);
|
|
492
|
+
*
|
|
493
|
+
* // Peek at the top element without removing
|
|
494
|
+
* const top = stack.peek();
|
|
495
|
+
* console.log(top); // 50;
|
|
496
|
+
*
|
|
497
|
+
* // Pop removes from the top (LIFO order)
|
|
498
|
+
* const popped = stack.pop();
|
|
499
|
+
* console.log(popped); // 50;
|
|
500
|
+
*
|
|
501
|
+
* // Next pop gets the previous element
|
|
502
|
+
* const next = stack.pop();
|
|
503
|
+
* console.log(next); // 40;
|
|
504
|
+
*
|
|
505
|
+
* // Verify length decreased
|
|
506
|
+
* console.log(stack.size); // 3;
|
|
507
|
+
*/
|
|
351
508
|
pop() {
|
|
352
509
|
return this.isEmpty() ? void 0 : this.elements.pop();
|
|
353
510
|
}
|
|
@@ -366,11 +523,45 @@ var _Stack = class _Stack extends IterableElementBase {
|
|
|
366
523
|
return ans;
|
|
367
524
|
}
|
|
368
525
|
/**
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
526
|
+
* Delete the first occurrence of a specific element.
|
|
527
|
+
* @remarks Time O(N), Space O(1)
|
|
528
|
+
* @param element - Element to remove (using the configured equality).
|
|
529
|
+
* @returns True if an element was removed.
|
|
530
|
+
|
|
531
|
+
|
|
532
|
+
|
|
533
|
+
|
|
534
|
+
|
|
535
|
+
|
|
536
|
+
|
|
537
|
+
|
|
538
|
+
|
|
539
|
+
|
|
540
|
+
|
|
541
|
+
|
|
542
|
+
|
|
543
|
+
|
|
544
|
+
|
|
545
|
+
|
|
546
|
+
|
|
547
|
+
|
|
548
|
+
|
|
549
|
+
|
|
550
|
+
|
|
551
|
+
|
|
552
|
+
|
|
553
|
+
|
|
554
|
+
|
|
555
|
+
|
|
556
|
+
|
|
557
|
+
|
|
558
|
+
|
|
559
|
+
* @example
|
|
560
|
+
* // Remove element
|
|
561
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
562
|
+
* stack.delete(2);
|
|
563
|
+
* console.log(stack.toArray()); // [1, 3];
|
|
564
|
+
*/
|
|
374
565
|
delete(element) {
|
|
375
566
|
const idx = this._indexOfByEquals(element);
|
|
376
567
|
return this.deleteAt(idx);
|
|
@@ -402,30 +593,137 @@ var _Stack = class _Stack extends IterableElementBase {
|
|
|
402
593
|
return false;
|
|
403
594
|
}
|
|
404
595
|
/**
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
596
|
+
* Remove all elements and reset storage.
|
|
597
|
+
* @remarks Time O(1), Space O(1)
|
|
598
|
+
* @returns void
|
|
599
|
+
|
|
600
|
+
|
|
601
|
+
|
|
602
|
+
|
|
603
|
+
|
|
604
|
+
|
|
605
|
+
|
|
606
|
+
|
|
607
|
+
|
|
608
|
+
|
|
609
|
+
|
|
610
|
+
|
|
611
|
+
|
|
612
|
+
|
|
613
|
+
|
|
614
|
+
|
|
615
|
+
|
|
616
|
+
|
|
617
|
+
|
|
618
|
+
|
|
619
|
+
|
|
620
|
+
|
|
621
|
+
|
|
622
|
+
|
|
623
|
+
|
|
624
|
+
|
|
625
|
+
|
|
626
|
+
|
|
627
|
+
|
|
628
|
+
|
|
629
|
+
* @example
|
|
630
|
+
* // Remove all elements
|
|
631
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
632
|
+
* stack.clear();
|
|
633
|
+
* console.log(stack.isEmpty()); // true;
|
|
634
|
+
*/
|
|
409
635
|
clear() {
|
|
410
636
|
this._elements = [];
|
|
411
637
|
}
|
|
412
638
|
/**
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
639
|
+
* Deep clone this stack.
|
|
640
|
+
* @remarks Time O(N), Space O(N)
|
|
641
|
+
* @returns A new stack with the same content.
|
|
642
|
+
|
|
643
|
+
|
|
644
|
+
|
|
645
|
+
|
|
646
|
+
|
|
647
|
+
|
|
648
|
+
|
|
649
|
+
|
|
650
|
+
|
|
651
|
+
|
|
652
|
+
|
|
653
|
+
|
|
654
|
+
|
|
655
|
+
|
|
656
|
+
|
|
657
|
+
|
|
658
|
+
|
|
659
|
+
|
|
660
|
+
|
|
661
|
+
|
|
662
|
+
|
|
663
|
+
|
|
664
|
+
|
|
665
|
+
|
|
666
|
+
|
|
667
|
+
|
|
668
|
+
|
|
669
|
+
|
|
670
|
+
|
|
671
|
+
|
|
672
|
+
* @example
|
|
673
|
+
* // Create independent copy
|
|
674
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
675
|
+
* const copy = stack.clone();
|
|
676
|
+
* copy.pop();
|
|
677
|
+
* console.log(stack.size); // 3;
|
|
678
|
+
* console.log(copy.size); // 2;
|
|
679
|
+
*/
|
|
417
680
|
clone() {
|
|
418
681
|
const out = this._createInstance({ toElementFn: this.toElementFn });
|
|
419
682
|
for (const v of this) out.push(v);
|
|
420
683
|
return out;
|
|
421
684
|
}
|
|
422
685
|
/**
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
686
|
+
* Filter elements into a new stack of the same class.
|
|
687
|
+
* @remarks Time O(N), Space O(N)
|
|
688
|
+
* @param predicate - Predicate (value, index, stack) → boolean to keep value.
|
|
689
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
690
|
+
* @returns A new stack with kept values.
|
|
691
|
+
|
|
692
|
+
|
|
693
|
+
|
|
694
|
+
|
|
695
|
+
|
|
696
|
+
|
|
697
|
+
|
|
698
|
+
|
|
699
|
+
|
|
700
|
+
|
|
701
|
+
|
|
702
|
+
|
|
703
|
+
|
|
704
|
+
|
|
705
|
+
|
|
706
|
+
|
|
707
|
+
|
|
708
|
+
|
|
709
|
+
|
|
710
|
+
|
|
711
|
+
|
|
712
|
+
|
|
713
|
+
|
|
714
|
+
|
|
715
|
+
|
|
716
|
+
|
|
717
|
+
|
|
718
|
+
|
|
719
|
+
|
|
720
|
+
|
|
721
|
+
* @example
|
|
722
|
+
* // Filter elements
|
|
723
|
+
* const stack = new Stack<number>([1, 2, 3, 4, 5]);
|
|
724
|
+
* const evens = stack.filter(x => x % 2 === 0);
|
|
725
|
+
* console.log(evens.toArray()); // [2, 4];
|
|
726
|
+
*/
|
|
429
727
|
filter(predicate, thisArg) {
|
|
430
728
|
const out = this._createInstance({ toElementFn: this.toElementFn });
|
|
431
729
|
let index = 0;
|
|
@@ -452,15 +750,49 @@ var _Stack = class _Stack extends IterableElementBase {
|
|
|
452
750
|
return out;
|
|
453
751
|
}
|
|
454
752
|
/**
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
753
|
+
* Map values into a new stack (possibly different element type).
|
|
754
|
+
* @remarks Time O(N), Space O(N)
|
|
755
|
+
* @template EM
|
|
756
|
+
* @template RM
|
|
757
|
+
* @param callback - Mapping function (value, index, stack) → newElement.
|
|
758
|
+
* @param [options] - Options for the output stack (e.g., toElementFn).
|
|
759
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
760
|
+
* @returns A new Stack with mapped elements.
|
|
761
|
+
|
|
762
|
+
|
|
763
|
+
|
|
764
|
+
|
|
765
|
+
|
|
766
|
+
|
|
767
|
+
|
|
768
|
+
|
|
769
|
+
|
|
770
|
+
|
|
771
|
+
|
|
772
|
+
|
|
773
|
+
|
|
774
|
+
|
|
775
|
+
|
|
776
|
+
|
|
777
|
+
|
|
778
|
+
|
|
779
|
+
|
|
780
|
+
|
|
781
|
+
|
|
782
|
+
|
|
783
|
+
|
|
784
|
+
|
|
785
|
+
|
|
786
|
+
|
|
787
|
+
|
|
788
|
+
|
|
789
|
+
|
|
790
|
+
* @example
|
|
791
|
+
* // Transform elements
|
|
792
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
793
|
+
* const doubled = stack.map(x => x * 2);
|
|
794
|
+
* console.log(doubled.toArray()); // [2, 4, 6];
|
|
795
|
+
*/
|
|
464
796
|
map(callback, options, thisArg) {
|
|
465
797
|
const out = this._createLike([], { ...options != null ? options : {} });
|
|
466
798
|
let index = 0;
|
|
@@ -524,6 +856,54 @@ var _Stack = class _Stack extends IterableElementBase {
|
|
|
524
856
|
};
|
|
525
857
|
__name(_Stack, "Stack");
|
|
526
858
|
var Stack = _Stack;
|
|
859
|
+
|
|
860
|
+
// src/common/error.ts
|
|
861
|
+
var ERR = {
|
|
862
|
+
// Range / index
|
|
863
|
+
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
864
|
+
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
865
|
+
// Type / argument
|
|
866
|
+
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
867
|
+
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
868
|
+
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
869
|
+
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
870
|
+
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
871
|
+
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
872
|
+
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
873
|
+
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
874
|
+
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
875
|
+
// State / operation
|
|
876
|
+
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
877
|
+
// Matrix
|
|
878
|
+
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
879
|
+
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
880
|
+
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
881
|
+
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
882
|
+
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
883
|
+
};
|
|
884
|
+
|
|
885
|
+
// src/common/index.ts
|
|
886
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
887
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
888
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
889
|
+
return DFSOperation2;
|
|
890
|
+
})(DFSOperation || {});
|
|
891
|
+
var _Range = class _Range {
|
|
892
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
893
|
+
this.low = low;
|
|
894
|
+
this.high = high;
|
|
895
|
+
this.includeLow = includeLow;
|
|
896
|
+
this.includeHigh = includeHigh;
|
|
897
|
+
}
|
|
898
|
+
// Determine whether a key is within the range
|
|
899
|
+
isInRange(key, comparator) {
|
|
900
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
901
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
902
|
+
return lowCheck && highCheck;
|
|
903
|
+
}
|
|
904
|
+
};
|
|
905
|
+
__name(_Range, "Range");
|
|
906
|
+
var Range = _Range;
|
|
527
907
|
/**
|
|
528
908
|
* data-structure-typed
|
|
529
909
|
*
|