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
|
@@ -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,23 @@ 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
|
+
* @example
|
|
265
|
+
* // Get number of elements
|
|
266
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
267
|
+
* console.log(stack.size); // 3;
|
|
268
|
+
*/
|
|
304
269
|
get size() {
|
|
305
270
|
return this.elements.length;
|
|
306
271
|
}
|
|
@@ -318,36 +283,123 @@ var _Stack = class _Stack extends IterableElementBase {
|
|
|
318
283
|
return new this(elements, options);
|
|
319
284
|
}
|
|
320
285
|
/**
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
286
|
+
* Check whether the stack is empty.
|
|
287
|
+
* @remarks Time O(1), Space O(1)
|
|
288
|
+
* @returns True if size is 0.
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
* @example
|
|
301
|
+
* // Check if stack has elements
|
|
302
|
+
* const stack = new Stack<number>();
|
|
303
|
+
* console.log(stack.isEmpty()); // true;
|
|
304
|
+
* stack.push(1);
|
|
305
|
+
* console.log(stack.isEmpty()); // false;
|
|
306
|
+
*/
|
|
325
307
|
isEmpty() {
|
|
326
308
|
return this.elements.length === 0;
|
|
327
309
|
}
|
|
328
310
|
/**
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
311
|
+
* Get the top element without removing it.
|
|
312
|
+
* @remarks Time O(1), Space O(1)
|
|
313
|
+
* @returns Top element or undefined.
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
* @example
|
|
326
|
+
* // View the top element without removing it
|
|
327
|
+
* const stack = new Stack<string>(['a', 'b', 'c']);
|
|
328
|
+
* console.log(stack.peek()); // 'c';
|
|
329
|
+
* console.log(stack.size); // 3;
|
|
330
|
+
*/
|
|
333
331
|
peek() {
|
|
334
332
|
return this.isEmpty() ? void 0 : this.elements[this.elements.length - 1];
|
|
335
333
|
}
|
|
336
334
|
/**
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
335
|
+
* Push one element onto the top.
|
|
336
|
+
* @remarks Time O(1), Space O(1)
|
|
337
|
+
* @param element - Element to push.
|
|
338
|
+
* @returns True when pushed.
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
* @example
|
|
351
|
+
* // basic Stack creation and push operation
|
|
352
|
+
* // Create a simple Stack with initial values
|
|
353
|
+
* const stack = new Stack([1, 2, 3, 4, 5]);
|
|
354
|
+
*
|
|
355
|
+
* // Verify the stack maintains insertion order (LIFO will be shown in pop)
|
|
356
|
+
* console.log([...stack]); // [1, 2, 3, 4, 5];
|
|
357
|
+
*
|
|
358
|
+
* // Check length
|
|
359
|
+
* console.log(stack.size); // 5;
|
|
360
|
+
*
|
|
361
|
+
* // Push a new element to the top
|
|
362
|
+
* stack.push(6);
|
|
363
|
+
* console.log(stack.size); // 6;
|
|
364
|
+
*/
|
|
342
365
|
push(element) {
|
|
343
366
|
this.elements.push(element);
|
|
344
367
|
return true;
|
|
345
368
|
}
|
|
346
369
|
/**
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
370
|
+
* Pop and return the top element.
|
|
371
|
+
* @remarks Time O(1), Space O(1)
|
|
372
|
+
* @returns Removed element or undefined.
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
* @example
|
|
385
|
+
* // Stack pop operation (LIFO - Last In First Out)
|
|
386
|
+
* const stack = new Stack<number>([10, 20, 30, 40, 50]);
|
|
387
|
+
*
|
|
388
|
+
* // Peek at the top element without removing
|
|
389
|
+
* const top = stack.peek();
|
|
390
|
+
* console.log(top); // 50;
|
|
391
|
+
*
|
|
392
|
+
* // Pop removes from the top (LIFO order)
|
|
393
|
+
* const popped = stack.pop();
|
|
394
|
+
* console.log(popped); // 50;
|
|
395
|
+
*
|
|
396
|
+
* // Next pop gets the previous element
|
|
397
|
+
* const next = stack.pop();
|
|
398
|
+
* console.log(next); // 40;
|
|
399
|
+
*
|
|
400
|
+
* // Verify length decreased
|
|
401
|
+
* console.log(stack.size); // 3;
|
|
402
|
+
*/
|
|
351
403
|
pop() {
|
|
352
404
|
return this.isEmpty() ? void 0 : this.elements.pop();
|
|
353
405
|
}
|
|
@@ -366,11 +418,24 @@ var _Stack = class _Stack extends IterableElementBase {
|
|
|
366
418
|
return ans;
|
|
367
419
|
}
|
|
368
420
|
/**
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
421
|
+
* Delete the first occurrence of a specific element.
|
|
422
|
+
* @remarks Time O(N), Space O(1)
|
|
423
|
+
* @param element - Element to remove (using the configured equality).
|
|
424
|
+
* @returns True if an element was removed.
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
* @example
|
|
434
|
+
* // Remove element
|
|
435
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
436
|
+
* stack.delete(2);
|
|
437
|
+
* console.log(stack.toArray()); // [1, 3];
|
|
438
|
+
*/
|
|
374
439
|
delete(element) {
|
|
375
440
|
const idx = this._indexOfByEquals(element);
|
|
376
441
|
return this.deleteAt(idx);
|
|
@@ -402,30 +467,74 @@ var _Stack = class _Stack extends IterableElementBase {
|
|
|
402
467
|
return false;
|
|
403
468
|
}
|
|
404
469
|
/**
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
470
|
+
* Remove all elements and reset storage.
|
|
471
|
+
* @remarks Time O(1), Space O(1)
|
|
472
|
+
* @returns void
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
|
|
479
|
+
|
|
480
|
+
|
|
481
|
+
|
|
482
|
+
* @example
|
|
483
|
+
* // Remove all elements
|
|
484
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
485
|
+
* stack.clear();
|
|
486
|
+
* console.log(stack.isEmpty()); // true;
|
|
487
|
+
*/
|
|
409
488
|
clear() {
|
|
410
489
|
this._elements = [];
|
|
411
490
|
}
|
|
412
491
|
/**
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
492
|
+
* Deep clone this stack.
|
|
493
|
+
* @remarks Time O(N), Space O(N)
|
|
494
|
+
* @returns A new stack with the same content.
|
|
495
|
+
|
|
496
|
+
|
|
497
|
+
|
|
498
|
+
|
|
499
|
+
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
|
|
504
|
+
* @example
|
|
505
|
+
* // Create independent copy
|
|
506
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
507
|
+
* const copy = stack.clone();
|
|
508
|
+
* copy.pop();
|
|
509
|
+
* console.log(stack.size); // 3;
|
|
510
|
+
* console.log(copy.size); // 2;
|
|
511
|
+
*/
|
|
417
512
|
clone() {
|
|
418
513
|
const out = this._createInstance({ toElementFn: this.toElementFn });
|
|
419
514
|
for (const v of this) out.push(v);
|
|
420
515
|
return out;
|
|
421
516
|
}
|
|
422
517
|
/**
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
518
|
+
* Filter elements into a new stack of the same class.
|
|
519
|
+
* @remarks Time O(N), Space O(N)
|
|
520
|
+
* @param predicate - Predicate (value, index, stack) → boolean to keep value.
|
|
521
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
522
|
+
* @returns A new stack with kept values.
|
|
523
|
+
|
|
524
|
+
|
|
525
|
+
|
|
526
|
+
|
|
527
|
+
|
|
528
|
+
|
|
529
|
+
|
|
530
|
+
|
|
531
|
+
|
|
532
|
+
* @example
|
|
533
|
+
* // Filter elements
|
|
534
|
+
* const stack = new Stack<number>([1, 2, 3, 4, 5]);
|
|
535
|
+
* const evens = stack.filter(x => x % 2 === 0);
|
|
536
|
+
* console.log(evens.toArray()); // [2, 4];
|
|
537
|
+
*/
|
|
429
538
|
filter(predicate, thisArg) {
|
|
430
539
|
const out = this._createInstance({ toElementFn: this.toElementFn });
|
|
431
540
|
let index = 0;
|
|
@@ -452,15 +561,28 @@ var _Stack = class _Stack extends IterableElementBase {
|
|
|
452
561
|
return out;
|
|
453
562
|
}
|
|
454
563
|
/**
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
564
|
+
* Map values into a new stack (possibly different element type).
|
|
565
|
+
* @remarks Time O(N), Space O(N)
|
|
566
|
+
* @template EM
|
|
567
|
+
* @template RM
|
|
568
|
+
* @param callback - Mapping function (value, index, stack) → newElement.
|
|
569
|
+
* @param [options] - Options for the output stack (e.g., toElementFn).
|
|
570
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
571
|
+
* @returns A new Stack with mapped elements.
|
|
572
|
+
|
|
573
|
+
|
|
574
|
+
|
|
575
|
+
|
|
576
|
+
|
|
577
|
+
|
|
578
|
+
|
|
579
|
+
|
|
580
|
+
* @example
|
|
581
|
+
* // Transform elements
|
|
582
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
583
|
+
* const doubled = stack.map(x => x * 2);
|
|
584
|
+
* console.log(doubled.toArray()); // [2, 4, 6];
|
|
585
|
+
*/
|
|
464
586
|
map(callback, options, thisArg) {
|
|
465
587
|
const out = this._createLike([], { ...options != null ? options : {} });
|
|
466
588
|
let index = 0;
|
|
@@ -524,6 +646,54 @@ var _Stack = class _Stack extends IterableElementBase {
|
|
|
524
646
|
};
|
|
525
647
|
__name(_Stack, "Stack");
|
|
526
648
|
var Stack = _Stack;
|
|
649
|
+
|
|
650
|
+
// src/common/error.ts
|
|
651
|
+
var ERR = {
|
|
652
|
+
// Range / index
|
|
653
|
+
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
654
|
+
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
655
|
+
// Type / argument
|
|
656
|
+
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
657
|
+
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
658
|
+
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
659
|
+
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
660
|
+
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
661
|
+
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
662
|
+
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
663
|
+
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
664
|
+
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
665
|
+
// State / operation
|
|
666
|
+
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
667
|
+
// Matrix
|
|
668
|
+
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
669
|
+
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
670
|
+
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
671
|
+
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
672
|
+
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
673
|
+
};
|
|
674
|
+
|
|
675
|
+
// src/common/index.ts
|
|
676
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
677
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
678
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
679
|
+
return DFSOperation2;
|
|
680
|
+
})(DFSOperation || {});
|
|
681
|
+
var _Range = class _Range {
|
|
682
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
683
|
+
this.low = low;
|
|
684
|
+
this.high = high;
|
|
685
|
+
this.includeLow = includeLow;
|
|
686
|
+
this.includeHigh = includeHigh;
|
|
687
|
+
}
|
|
688
|
+
// Determine whether a key is within the range
|
|
689
|
+
isInRange(key, comparator) {
|
|
690
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
691
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
692
|
+
return lowCheck && highCheck;
|
|
693
|
+
}
|
|
694
|
+
};
|
|
695
|
+
__name(_Range, "Range");
|
|
696
|
+
var Range = _Range;
|
|
527
697
|
/**
|
|
528
698
|
* data-structure-typed
|
|
529
699
|
*
|