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
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,44 @@ 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
|
+
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
* @example
|
|
290
|
+
* // Get number of elements
|
|
291
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
292
|
+
* console.log(stack.size); // 3;
|
|
293
|
+
*/
|
|
309
294
|
get size() {
|
|
310
295
|
return this.elements.length;
|
|
311
296
|
}
|
|
@@ -323,36 +308,207 @@ var Stack = class extends IterableElementBase {
|
|
|
323
308
|
return new this(elements, options);
|
|
324
309
|
}
|
|
325
310
|
/**
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
311
|
+
* Check whether the stack is empty.
|
|
312
|
+
* @remarks Time O(1), Space O(1)
|
|
313
|
+
* @returns True if size is 0.
|
|
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
|
+
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
* @example
|
|
347
|
+
* // Check if stack has elements
|
|
348
|
+
* const stack = new Stack<number>();
|
|
349
|
+
* console.log(stack.isEmpty()); // true;
|
|
350
|
+
* stack.push(1);
|
|
351
|
+
* console.log(stack.isEmpty()); // false;
|
|
352
|
+
*/
|
|
330
353
|
isEmpty() {
|
|
331
354
|
return this.elements.length === 0;
|
|
332
355
|
}
|
|
333
356
|
/**
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
357
|
+
* Get the top element without removing it.
|
|
358
|
+
* @remarks Time O(1), Space O(1)
|
|
359
|
+
* @returns Top element or undefined.
|
|
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
|
+
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
* @example
|
|
393
|
+
* // View the top element without removing it
|
|
394
|
+
* const stack = new Stack<string>(['a', 'b', 'c']);
|
|
395
|
+
* console.log(stack.peek()); // 'c';
|
|
396
|
+
* console.log(stack.size); // 3;
|
|
397
|
+
*/
|
|
338
398
|
peek() {
|
|
339
399
|
return this.isEmpty() ? void 0 : this.elements[this.elements.length - 1];
|
|
340
400
|
}
|
|
341
401
|
/**
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
402
|
+
* Push one element onto the top.
|
|
403
|
+
* @remarks Time O(1), Space O(1)
|
|
404
|
+
* @param element - Element to push.
|
|
405
|
+
* @returns True when pushed.
|
|
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
|
+
|
|
435
|
+
|
|
436
|
+
|
|
437
|
+
|
|
438
|
+
* @example
|
|
439
|
+
* // basic Stack creation and push operation
|
|
440
|
+
* // Create a simple Stack with initial values
|
|
441
|
+
* const stack = new Stack([1, 2, 3, 4, 5]);
|
|
442
|
+
*
|
|
443
|
+
* // Verify the stack maintains insertion order (LIFO will be shown in pop)
|
|
444
|
+
* console.log([...stack]); // [1, 2, 3, 4, 5];
|
|
445
|
+
*
|
|
446
|
+
* // Check length
|
|
447
|
+
* console.log(stack.size); // 5;
|
|
448
|
+
*
|
|
449
|
+
* // Push a new element to the top
|
|
450
|
+
* stack.push(6);
|
|
451
|
+
* console.log(stack.size); // 6;
|
|
452
|
+
*/
|
|
347
453
|
push(element) {
|
|
348
454
|
this.elements.push(element);
|
|
349
455
|
return true;
|
|
350
456
|
}
|
|
351
457
|
/**
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
458
|
+
* Pop and return the top element.
|
|
459
|
+
* @remarks Time O(1), Space O(1)
|
|
460
|
+
* @returns Removed element or undefined.
|
|
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
|
+
|
|
490
|
+
|
|
491
|
+
|
|
492
|
+
|
|
493
|
+
* @example
|
|
494
|
+
* // Stack pop operation (LIFO - Last In First Out)
|
|
495
|
+
* const stack = new Stack<number>([10, 20, 30, 40, 50]);
|
|
496
|
+
*
|
|
497
|
+
* // Peek at the top element without removing
|
|
498
|
+
* const top = stack.peek();
|
|
499
|
+
* console.log(top); // 50;
|
|
500
|
+
*
|
|
501
|
+
* // Pop removes from the top (LIFO order)
|
|
502
|
+
* const popped = stack.pop();
|
|
503
|
+
* console.log(popped); // 50;
|
|
504
|
+
*
|
|
505
|
+
* // Next pop gets the previous element
|
|
506
|
+
* const next = stack.pop();
|
|
507
|
+
* console.log(next); // 40;
|
|
508
|
+
*
|
|
509
|
+
* // Verify length decreased
|
|
510
|
+
* console.log(stack.size); // 3;
|
|
511
|
+
*/
|
|
356
512
|
pop() {
|
|
357
513
|
return this.isEmpty() ? void 0 : this.elements.pop();
|
|
358
514
|
}
|
|
@@ -371,11 +527,45 @@ var Stack = class extends IterableElementBase {
|
|
|
371
527
|
return ans;
|
|
372
528
|
}
|
|
373
529
|
/**
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
530
|
+
* Delete the first occurrence of a specific element.
|
|
531
|
+
* @remarks Time O(N), Space O(1)
|
|
532
|
+
* @param element - Element to remove (using the configured equality).
|
|
533
|
+
* @returns True if an element was removed.
|
|
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
|
+
|
|
560
|
+
|
|
561
|
+
|
|
562
|
+
|
|
563
|
+
* @example
|
|
564
|
+
* // Remove element
|
|
565
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
566
|
+
* stack.delete(2);
|
|
567
|
+
* console.log(stack.toArray()); // [1, 3];
|
|
568
|
+
*/
|
|
379
569
|
delete(element) {
|
|
380
570
|
const idx = this._indexOfByEquals(element);
|
|
381
571
|
return this.deleteAt(idx);
|
|
@@ -407,30 +597,137 @@ var Stack = class extends IterableElementBase {
|
|
|
407
597
|
return false;
|
|
408
598
|
}
|
|
409
599
|
/**
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
600
|
+
* Remove all elements and reset storage.
|
|
601
|
+
* @remarks Time O(1), Space O(1)
|
|
602
|
+
* @returns void
|
|
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
|
+
|
|
630
|
+
|
|
631
|
+
|
|
632
|
+
|
|
633
|
+
* @example
|
|
634
|
+
* // Remove all elements
|
|
635
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
636
|
+
* stack.clear();
|
|
637
|
+
* console.log(stack.isEmpty()); // true;
|
|
638
|
+
*/
|
|
414
639
|
clear() {
|
|
415
640
|
this._elements = [];
|
|
416
641
|
}
|
|
417
642
|
/**
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
643
|
+
* Deep clone this stack.
|
|
644
|
+
* @remarks Time O(N), Space O(N)
|
|
645
|
+
* @returns A new stack with the same content.
|
|
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
|
+
|
|
673
|
+
|
|
674
|
+
|
|
675
|
+
|
|
676
|
+
* @example
|
|
677
|
+
* // Create independent copy
|
|
678
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
679
|
+
* const copy = stack.clone();
|
|
680
|
+
* copy.pop();
|
|
681
|
+
* console.log(stack.size); // 3;
|
|
682
|
+
* console.log(copy.size); // 2;
|
|
683
|
+
*/
|
|
422
684
|
clone() {
|
|
423
685
|
const out = this._createInstance({ toElementFn: this.toElementFn });
|
|
424
686
|
for (const v of this) out.push(v);
|
|
425
687
|
return out;
|
|
426
688
|
}
|
|
427
689
|
/**
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
690
|
+
* Filter elements into a new stack of the same class.
|
|
691
|
+
* @remarks Time O(N), Space O(N)
|
|
692
|
+
* @param predicate - Predicate (value, index, stack) → boolean to keep value.
|
|
693
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
694
|
+
* @returns A new stack with kept values.
|
|
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
|
+
|
|
722
|
+
|
|
723
|
+
|
|
724
|
+
|
|
725
|
+
* @example
|
|
726
|
+
* // Filter elements
|
|
727
|
+
* const stack = new Stack<number>([1, 2, 3, 4, 5]);
|
|
728
|
+
* const evens = stack.filter(x => x % 2 === 0);
|
|
729
|
+
* console.log(evens.toArray()); // [2, 4];
|
|
730
|
+
*/
|
|
434
731
|
filter(predicate, thisArg) {
|
|
435
732
|
const out = this._createInstance({ toElementFn: this.toElementFn });
|
|
436
733
|
let index = 0;
|
|
@@ -457,15 +754,49 @@ var Stack = class extends IterableElementBase {
|
|
|
457
754
|
return out;
|
|
458
755
|
}
|
|
459
756
|
/**
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
757
|
+
* Map values into a new stack (possibly different element type).
|
|
758
|
+
* @remarks Time O(N), Space O(N)
|
|
759
|
+
* @template EM
|
|
760
|
+
* @template RM
|
|
761
|
+
* @param callback - Mapping function (value, index, stack) → newElement.
|
|
762
|
+
* @param [options] - Options for the output stack (e.g., toElementFn).
|
|
763
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
764
|
+
* @returns A new Stack with mapped elements.
|
|
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
|
+
|
|
791
|
+
|
|
792
|
+
|
|
793
|
+
|
|
794
|
+
* @example
|
|
795
|
+
* // Transform elements
|
|
796
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
797
|
+
* const doubled = stack.map(x => x * 2);
|
|
798
|
+
* console.log(doubled.toArray()); // [2, 4, 6];
|
|
799
|
+
*/
|
|
469
800
|
map(callback, options, thisArg) {
|
|
470
801
|
const out = this._createLike([], { ...options ?? {} });
|
|
471
802
|
let index = 0;
|
|
@@ -527,6 +858,55 @@ var Stack = class extends IterableElementBase {
|
|
|
527
858
|
for (let i = 0; i < this.elements.length; i++) yield this.elements[i];
|
|
528
859
|
}
|
|
529
860
|
};
|
|
861
|
+
|
|
862
|
+
// src/common/error.ts
|
|
863
|
+
var ERR = {
|
|
864
|
+
// Range / index
|
|
865
|
+
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
866
|
+
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
867
|
+
// Type / argument
|
|
868
|
+
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
869
|
+
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
870
|
+
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
871
|
+
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
872
|
+
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
873
|
+
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
874
|
+
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
875
|
+
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
876
|
+
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
877
|
+
// State / operation
|
|
878
|
+
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
879
|
+
// Matrix
|
|
880
|
+
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
881
|
+
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
882
|
+
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
883
|
+
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
884
|
+
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
885
|
+
};
|
|
886
|
+
|
|
887
|
+
// src/common/index.ts
|
|
888
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
889
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
890
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
891
|
+
return DFSOperation2;
|
|
892
|
+
})(DFSOperation || {});
|
|
893
|
+
var Range = class {
|
|
894
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
895
|
+
this.low = low;
|
|
896
|
+
this.high = high;
|
|
897
|
+
this.includeLow = includeLow;
|
|
898
|
+
this.includeHigh = includeHigh;
|
|
899
|
+
}
|
|
900
|
+
static {
|
|
901
|
+
__name(this, "Range");
|
|
902
|
+
}
|
|
903
|
+
// Determine whether a key is within the range
|
|
904
|
+
isInRange(key, comparator) {
|
|
905
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
906
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
907
|
+
return lowCheck && highCheck;
|
|
908
|
+
}
|
|
909
|
+
};
|
|
530
910
|
/**
|
|
531
911
|
* data-structure-typed
|
|
532
912
|
*
|