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
|
@@ -5,54 +5,6 @@ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { en
|
|
|
5
5
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
6
6
|
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
7
7
|
|
|
8
|
-
// src/common/error.ts
|
|
9
|
-
var ERR = {
|
|
10
|
-
// Range / index
|
|
11
|
-
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
12
|
-
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
13
|
-
// Type / argument
|
|
14
|
-
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
15
|
-
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
16
|
-
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
17
|
-
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
18
|
-
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
19
|
-
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
20
|
-
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
21
|
-
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
22
|
-
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
23
|
-
// State / operation
|
|
24
|
-
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
25
|
-
// Matrix
|
|
26
|
-
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
27
|
-
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
28
|
-
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
29
|
-
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
30
|
-
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
// src/common/index.ts
|
|
34
|
-
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
35
|
-
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
36
|
-
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
37
|
-
return DFSOperation2;
|
|
38
|
-
})(DFSOperation || {});
|
|
39
|
-
var _Range = class _Range {
|
|
40
|
-
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
41
|
-
this.low = low;
|
|
42
|
-
this.high = high;
|
|
43
|
-
this.includeLow = includeLow;
|
|
44
|
-
this.includeHigh = includeHigh;
|
|
45
|
-
}
|
|
46
|
-
// Determine whether a key is within the range
|
|
47
|
-
isInRange(key, comparator) {
|
|
48
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
49
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
50
|
-
return lowCheck && highCheck;
|
|
51
|
-
}
|
|
52
|
-
};
|
|
53
|
-
__name(_Range, "Range");
|
|
54
|
-
var Range = _Range;
|
|
55
|
-
|
|
56
8
|
// src/data-structures/base/iterable-element-base.ts
|
|
57
9
|
var _IterableElementBase = class _IterableElementBase {
|
|
58
10
|
/**
|
|
@@ -75,7 +27,7 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
75
27
|
if (options) {
|
|
76
28
|
const { toElementFn } = options;
|
|
77
29
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
78
|
-
else if (toElementFn) throw new TypeError(
|
|
30
|
+
else if (toElementFn) throw new TypeError("toElementFn must be a function type");
|
|
79
31
|
}
|
|
80
32
|
}
|
|
81
33
|
/**
|
|
@@ -231,7 +183,7 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
231
183
|
acc = initialValue;
|
|
232
184
|
} else {
|
|
233
185
|
const first = iter.next();
|
|
234
|
-
if (first.done) throw new TypeError(
|
|
186
|
+
if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
|
|
235
187
|
acc = first.value;
|
|
236
188
|
index = 1;
|
|
237
189
|
}
|
|
@@ -299,10 +251,23 @@ var _Stack = class _Stack extends IterableElementBase {
|
|
|
299
251
|
return this._elements;
|
|
300
252
|
}
|
|
301
253
|
/**
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
254
|
+
* Get the number of stored elements.
|
|
255
|
+
* @remarks Time O(1), Space O(1)
|
|
256
|
+
* @returns Current size.
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
* @example
|
|
267
|
+
* // Get number of elements
|
|
268
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
269
|
+
* console.log(stack.size); // 3;
|
|
270
|
+
*/
|
|
306
271
|
get size() {
|
|
307
272
|
return this.elements.length;
|
|
308
273
|
}
|
|
@@ -320,36 +285,123 @@ var _Stack = class _Stack extends IterableElementBase {
|
|
|
320
285
|
return new this(elements, options);
|
|
321
286
|
}
|
|
322
287
|
/**
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
288
|
+
* Check whether the stack is empty.
|
|
289
|
+
* @remarks Time O(1), Space O(1)
|
|
290
|
+
* @returns True if size is 0.
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
* @example
|
|
303
|
+
* // Check if stack has elements
|
|
304
|
+
* const stack = new Stack<number>();
|
|
305
|
+
* console.log(stack.isEmpty()); // true;
|
|
306
|
+
* stack.push(1);
|
|
307
|
+
* console.log(stack.isEmpty()); // false;
|
|
308
|
+
*/
|
|
327
309
|
isEmpty() {
|
|
328
310
|
return this.elements.length === 0;
|
|
329
311
|
}
|
|
330
312
|
/**
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
313
|
+
* Get the top element without removing it.
|
|
314
|
+
* @remarks Time O(1), Space O(1)
|
|
315
|
+
* @returns Top element or undefined.
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
* @example
|
|
328
|
+
* // View the top element without removing it
|
|
329
|
+
* const stack = new Stack<string>(['a', 'b', 'c']);
|
|
330
|
+
* console.log(stack.peek()); // 'c';
|
|
331
|
+
* console.log(stack.size); // 3;
|
|
332
|
+
*/
|
|
335
333
|
peek() {
|
|
336
334
|
return this.isEmpty() ? void 0 : this.elements[this.elements.length - 1];
|
|
337
335
|
}
|
|
338
336
|
/**
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
337
|
+
* Push one element onto the top.
|
|
338
|
+
* @remarks Time O(1), Space O(1)
|
|
339
|
+
* @param element - Element to push.
|
|
340
|
+
* @returns True when pushed.
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
* @example
|
|
353
|
+
* // basic Stack creation and push operation
|
|
354
|
+
* // Create a simple Stack with initial values
|
|
355
|
+
* const stack = new Stack([1, 2, 3, 4, 5]);
|
|
356
|
+
*
|
|
357
|
+
* // Verify the stack maintains insertion order (LIFO will be shown in pop)
|
|
358
|
+
* console.log([...stack]); // [1, 2, 3, 4, 5];
|
|
359
|
+
*
|
|
360
|
+
* // Check length
|
|
361
|
+
* console.log(stack.size); // 5;
|
|
362
|
+
*
|
|
363
|
+
* // Push a new element to the top
|
|
364
|
+
* stack.push(6);
|
|
365
|
+
* console.log(stack.size); // 6;
|
|
366
|
+
*/
|
|
344
367
|
push(element) {
|
|
345
368
|
this.elements.push(element);
|
|
346
369
|
return true;
|
|
347
370
|
}
|
|
348
371
|
/**
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
372
|
+
* Pop and return the top element.
|
|
373
|
+
* @remarks Time O(1), Space O(1)
|
|
374
|
+
* @returns Removed element or undefined.
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
* @example
|
|
387
|
+
* // Stack pop operation (LIFO - Last In First Out)
|
|
388
|
+
* const stack = new Stack<number>([10, 20, 30, 40, 50]);
|
|
389
|
+
*
|
|
390
|
+
* // Peek at the top element without removing
|
|
391
|
+
* const top = stack.peek();
|
|
392
|
+
* console.log(top); // 50;
|
|
393
|
+
*
|
|
394
|
+
* // Pop removes from the top (LIFO order)
|
|
395
|
+
* const popped = stack.pop();
|
|
396
|
+
* console.log(popped); // 50;
|
|
397
|
+
*
|
|
398
|
+
* // Next pop gets the previous element
|
|
399
|
+
* const next = stack.pop();
|
|
400
|
+
* console.log(next); // 40;
|
|
401
|
+
*
|
|
402
|
+
* // Verify length decreased
|
|
403
|
+
* console.log(stack.size); // 3;
|
|
404
|
+
*/
|
|
353
405
|
pop() {
|
|
354
406
|
return this.isEmpty() ? void 0 : this.elements.pop();
|
|
355
407
|
}
|
|
@@ -368,11 +420,24 @@ var _Stack = class _Stack extends IterableElementBase {
|
|
|
368
420
|
return ans;
|
|
369
421
|
}
|
|
370
422
|
/**
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
423
|
+
* Delete the first occurrence of a specific element.
|
|
424
|
+
* @remarks Time O(N), Space O(1)
|
|
425
|
+
* @param element - Element to remove (using the configured equality).
|
|
426
|
+
* @returns True if an element was removed.
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
* @example
|
|
436
|
+
* // Remove element
|
|
437
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
438
|
+
* stack.delete(2);
|
|
439
|
+
* console.log(stack.toArray()); // [1, 3];
|
|
440
|
+
*/
|
|
376
441
|
delete(element) {
|
|
377
442
|
const idx = this._indexOfByEquals(element);
|
|
378
443
|
return this.deleteAt(idx);
|
|
@@ -404,30 +469,74 @@ var _Stack = class _Stack extends IterableElementBase {
|
|
|
404
469
|
return false;
|
|
405
470
|
}
|
|
406
471
|
/**
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
472
|
+
* Remove all elements and reset storage.
|
|
473
|
+
* @remarks Time O(1), Space O(1)
|
|
474
|
+
* @returns void
|
|
475
|
+
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
|
|
479
|
+
|
|
480
|
+
|
|
481
|
+
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
* @example
|
|
485
|
+
* // Remove all elements
|
|
486
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
487
|
+
* stack.clear();
|
|
488
|
+
* console.log(stack.isEmpty()); // true;
|
|
489
|
+
*/
|
|
411
490
|
clear() {
|
|
412
491
|
this._elements = [];
|
|
413
492
|
}
|
|
414
493
|
/**
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
494
|
+
* Deep clone this stack.
|
|
495
|
+
* @remarks Time O(N), Space O(N)
|
|
496
|
+
* @returns A new stack with the same content.
|
|
497
|
+
|
|
498
|
+
|
|
499
|
+
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
|
|
504
|
+
|
|
505
|
+
|
|
506
|
+
* @example
|
|
507
|
+
* // Create independent copy
|
|
508
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
509
|
+
* const copy = stack.clone();
|
|
510
|
+
* copy.pop();
|
|
511
|
+
* console.log(stack.size); // 3;
|
|
512
|
+
* console.log(copy.size); // 2;
|
|
513
|
+
*/
|
|
419
514
|
clone() {
|
|
420
515
|
const out = this._createInstance({ toElementFn: this.toElementFn });
|
|
421
516
|
for (const v of this) out.push(v);
|
|
422
517
|
return out;
|
|
423
518
|
}
|
|
424
519
|
/**
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
520
|
+
* Filter elements into a new stack of the same class.
|
|
521
|
+
* @remarks Time O(N), Space O(N)
|
|
522
|
+
* @param predicate - Predicate (value, index, stack) → boolean to keep value.
|
|
523
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
524
|
+
* @returns A new stack with kept values.
|
|
525
|
+
|
|
526
|
+
|
|
527
|
+
|
|
528
|
+
|
|
529
|
+
|
|
530
|
+
|
|
531
|
+
|
|
532
|
+
|
|
533
|
+
|
|
534
|
+
* @example
|
|
535
|
+
* // Filter elements
|
|
536
|
+
* const stack = new Stack<number>([1, 2, 3, 4, 5]);
|
|
537
|
+
* const evens = stack.filter(x => x % 2 === 0);
|
|
538
|
+
* console.log(evens.toArray()); // [2, 4];
|
|
539
|
+
*/
|
|
431
540
|
filter(predicate, thisArg) {
|
|
432
541
|
const out = this._createInstance({ toElementFn: this.toElementFn });
|
|
433
542
|
let index = 0;
|
|
@@ -454,15 +563,28 @@ var _Stack = class _Stack extends IterableElementBase {
|
|
|
454
563
|
return out;
|
|
455
564
|
}
|
|
456
565
|
/**
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
566
|
+
* Map values into a new stack (possibly different element type).
|
|
567
|
+
* @remarks Time O(N), Space O(N)
|
|
568
|
+
* @template EM
|
|
569
|
+
* @template RM
|
|
570
|
+
* @param callback - Mapping function (value, index, stack) → newElement.
|
|
571
|
+
* @param [options] - Options for the output stack (e.g., toElementFn).
|
|
572
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
573
|
+
* @returns A new Stack with mapped elements.
|
|
574
|
+
|
|
575
|
+
|
|
576
|
+
|
|
577
|
+
|
|
578
|
+
|
|
579
|
+
|
|
580
|
+
|
|
581
|
+
|
|
582
|
+
* @example
|
|
583
|
+
* // Transform elements
|
|
584
|
+
* const stack = new Stack<number>([1, 2, 3]);
|
|
585
|
+
* const doubled = stack.map(x => x * 2);
|
|
586
|
+
* console.log(doubled.toArray()); // [2, 4, 6];
|
|
587
|
+
*/
|
|
466
588
|
map(callback, options, thisArg) {
|
|
467
589
|
const out = this._createLike([], { ...options != null ? options : {} });
|
|
468
590
|
let index = 0;
|
|
@@ -526,6 +648,54 @@ var _Stack = class _Stack extends IterableElementBase {
|
|
|
526
648
|
};
|
|
527
649
|
__name(_Stack, "Stack");
|
|
528
650
|
var Stack = _Stack;
|
|
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 _Range {
|
|
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
|
+
// Determine whether a key is within the range
|
|
691
|
+
isInRange(key, comparator) {
|
|
692
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
693
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
694
|
+
return lowCheck && highCheck;
|
|
695
|
+
}
|
|
696
|
+
};
|
|
697
|
+
__name(_Range, "Range");
|
|
698
|
+
var Range = _Range;
|
|
529
699
|
/**
|
|
530
700
|
* data-structure-typed
|
|
531
701
|
*
|