singly-linked-list-typed 2.4.3 → 2.4.5
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/dist/cjs/index.cjs +58 -32
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +58 -31
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +58 -33
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +58 -32
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/common/error.d.ts +23 -0
- package/dist/types/common/index.d.ts +1 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +15 -5
- package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +7 -1
- package/dist/types/data-structures/graph/abstract-graph.d.ts +44 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +3 -2
- package/dist/types/data-structures/graph/undirected-graph.d.ts +16 -2
- package/dist/types/data-structures/hash/hash-map.d.ts +2 -2
- package/dist/types/data-structures/heap/heap.d.ts +3 -7
- package/dist/types/data-structures/queue/deque.d.ts +41 -1
- package/dist/types/types/data-structures/binary-tree/avl-tree.d.ts +1 -1
- package/dist/types/types/data-structures/binary-tree/red-black-tree.d.ts +1 -1
- package/dist/types/types/data-structures/linked-list/doubly-linked-list.d.ts +1 -1
- package/dist/types/types/data-structures/linked-list/singly-linked-list.d.ts +1 -1
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/types/types/data-structures/queue/deque.d.ts +6 -0
- package/dist/types/types/data-structures/stack/stack.d.ts +1 -1
- package/dist/umd/singly-linked-list-typed.js +56 -29
- package/dist/umd/singly-linked-list-typed.js.map +1 -1
- package/dist/umd/singly-linked-list-typed.min.js +1 -1
- package/dist/umd/singly-linked-list-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/common/error.ts +60 -0
- package/src/common/index.ts +2 -0
- package/src/data-structures/base/iterable-element-base.ts +5 -4
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +6 -5
- package/src/data-structures/binary-tree/binary-tree.ts +121 -49
- package/src/data-structures/binary-tree/bst.ts +12 -4
- package/src/data-structures/binary-tree/red-black-tree.ts +20 -0
- package/src/data-structures/binary-tree/tree-map.ts +8 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +4 -4
- package/src/data-structures/binary-tree/tree-multi-set.ts +10 -9
- package/src/data-structures/binary-tree/tree-set.ts +7 -6
- package/src/data-structures/graph/abstract-graph.ts +124 -19
- package/src/data-structures/graph/directed-graph.ts +8 -4
- package/src/data-structures/graph/map-graph.ts +1 -1
- package/src/data-structures/graph/undirected-graph.ts +99 -4
- package/src/data-structures/hash/hash-map.ts +19 -6
- package/src/data-structures/heap/heap.ts +21 -17
- package/src/data-structures/heap/max-heap.ts +2 -3
- package/src/data-structures/linked-list/doubly-linked-list.ts +4 -4
- package/src/data-structures/linked-list/singly-linked-list.ts +15 -9
- package/src/data-structures/matrix/matrix.ts +9 -10
- package/src/data-structures/priority-queue/max-priority-queue.ts +2 -3
- package/src/data-structures/queue/deque.ts +72 -4
- package/src/data-structures/stack/stack.ts +1 -1
- package/src/data-structures/trie/trie.ts +12 -6
- package/src/types/data-structures/binary-tree/avl-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/red-black-tree.ts +1 -1
- package/src/types/data-structures/linked-list/doubly-linked-list.ts +1 -1
- package/src/types/data-structures/linked-list/singly-linked-list.ts +1 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/types/data-structures/queue/deque.ts +7 -0
- package/src/types/data-structures/stack/stack.ts +1 -1
- package/src/utils/utils.ts +4 -2
package/dist/cjs/index.cjs
CHANGED
|
@@ -3,6 +3,55 @@
|
|
|
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
|
+
|
|
6
55
|
// src/data-structures/base/iterable-element-base.ts
|
|
7
56
|
var IterableElementBase = class {
|
|
8
57
|
static {
|
|
@@ -21,7 +70,7 @@ var IterableElementBase = class {
|
|
|
21
70
|
if (options) {
|
|
22
71
|
const { toElementFn } = options;
|
|
23
72
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
24
|
-
else if (toElementFn) throw new TypeError("toElementFn
|
|
73
|
+
else if (toElementFn) throw new TypeError(ERR.notAFunction("toElementFn"));
|
|
25
74
|
}
|
|
26
75
|
}
|
|
27
76
|
/**
|
|
@@ -184,7 +233,7 @@ var IterableElementBase = class {
|
|
|
184
233
|
acc = initialValue;
|
|
185
234
|
} else {
|
|
186
235
|
const first = iter.next();
|
|
187
|
-
if (first.done) throw new TypeError(
|
|
236
|
+
if (first.done) throw new TypeError(ERR.reduceEmpty());
|
|
188
237
|
acc = first.value;
|
|
189
238
|
index = 1;
|
|
190
239
|
}
|
|
@@ -653,7 +702,7 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
653
702
|
static {
|
|
654
703
|
__name(this, "SinglyLinkedList");
|
|
655
704
|
}
|
|
656
|
-
_equals = Object.is;
|
|
705
|
+
_equals = /* @__PURE__ */ __name((a, b) => Object.is(a, b), "_equals");
|
|
657
706
|
/**
|
|
658
707
|
* Create a SinglyLinkedList and optionally bulk-insert elements.
|
|
659
708
|
* @remarks Time O(N), Space O(N)
|
|
@@ -757,8 +806,8 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
757
806
|
return value2;
|
|
758
807
|
}
|
|
759
808
|
let current = this.head;
|
|
760
|
-
while (current.next !== this.tail) current = current.next;
|
|
761
|
-
const value = this.tail
|
|
809
|
+
while (current.next && current.next !== this.tail) current = current.next;
|
|
810
|
+
const value = this.tail?.value;
|
|
762
811
|
current.next = void 0;
|
|
763
812
|
this._tail = current;
|
|
764
813
|
this._length--;
|
|
@@ -846,8 +895,8 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
846
895
|
at(index) {
|
|
847
896
|
if (index < 0 || index >= this._length) return void 0;
|
|
848
897
|
let current = this.head;
|
|
849
|
-
for (let i = 0; i < index; i++) current = current.next;
|
|
850
|
-
return current
|
|
898
|
+
for (let i = 0; i < index && current; i++) current = current.next;
|
|
899
|
+
return current?.value;
|
|
851
900
|
}
|
|
852
901
|
/**
|
|
853
902
|
* Type guard: check whether the input is a SinglyLinkedListNode.
|
|
@@ -867,7 +916,7 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
867
916
|
getNodeAt(index) {
|
|
868
917
|
if (index < 0 || index >= this._length) return void 0;
|
|
869
918
|
let current = this.head;
|
|
870
|
-
for (let i = 0; i < index; i++) current = current.next;
|
|
919
|
+
for (let i = 0; i < index && current; i++) current = current.next;
|
|
871
920
|
return current;
|
|
872
921
|
}
|
|
873
922
|
/**
|
|
@@ -1317,30 +1366,6 @@ function elementOrPredicate(input, equals) {
|
|
|
1317
1366
|
return (node) => equals(node.value, value);
|
|
1318
1367
|
}
|
|
1319
1368
|
__name(elementOrPredicate, "elementOrPredicate");
|
|
1320
|
-
|
|
1321
|
-
// src/common/index.ts
|
|
1322
|
-
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
1323
|
-
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
1324
|
-
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
1325
|
-
return DFSOperation2;
|
|
1326
|
-
})(DFSOperation || {});
|
|
1327
|
-
var Range = class {
|
|
1328
|
-
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
1329
|
-
this.low = low;
|
|
1330
|
-
this.high = high;
|
|
1331
|
-
this.includeLow = includeLow;
|
|
1332
|
-
this.includeHigh = includeHigh;
|
|
1333
|
-
}
|
|
1334
|
-
static {
|
|
1335
|
-
__name(this, "Range");
|
|
1336
|
-
}
|
|
1337
|
-
// Determine whether a key is within the range
|
|
1338
|
-
isInRange(key, comparator) {
|
|
1339
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
1340
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
1341
|
-
return lowCheck && highCheck;
|
|
1342
|
-
}
|
|
1343
|
-
};
|
|
1344
1369
|
/**
|
|
1345
1370
|
* data-structure-typed
|
|
1346
1371
|
*
|
|
@@ -1350,6 +1375,7 @@ var Range = class {
|
|
|
1350
1375
|
*/
|
|
1351
1376
|
|
|
1352
1377
|
exports.DFSOperation = DFSOperation;
|
|
1378
|
+
exports.ERR = ERR;
|
|
1353
1379
|
exports.Range = Range;
|
|
1354
1380
|
exports.SinglyLinkedList = SinglyLinkedList;
|
|
1355
1381
|
exports.SinglyLinkedListNode = SinglyLinkedListNode;
|