deque-typed 2.1.0 → 2.1.2
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 +1223 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/esm/index.mjs +1219 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/types/data-structures/base/index.d.ts +2 -1
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +182 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +135 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +291 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +174 -1
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +754 -29
- package/dist/types/data-structures/binary-tree/bst.d.ts +413 -12
- package/dist/types/data-structures/binary-tree/index.d.ts +3 -2
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +208 -3
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +160 -1
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +190 -2
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +270 -2
- package/dist/types/data-structures/graph/abstract-graph.d.ts +340 -14
- package/dist/types/data-structures/graph/directed-graph.d.ts +207 -1
- package/dist/types/data-structures/graph/index.d.ts +2 -1
- package/dist/types/data-structures/graph/map-graph.d.ts +78 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +188 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +345 -19
- package/dist/types/data-structures/hash/index.d.ts +0 -1
- package/dist/types/data-structures/heap/heap.d.ts +503 -5
- package/dist/types/data-structures/heap/index.d.ts +2 -0
- package/dist/types/data-structures/heap/max-heap.d.ts +32 -1
- package/dist/types/data-structures/heap/min-heap.d.ts +33 -1
- package/dist/types/data-structures/index.d.ts +7 -7
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +769 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +451 -2
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +27 -4
- package/dist/types/data-structures/matrix/index.d.ts +1 -1
- package/dist/types/data-structures/matrix/matrix.d.ts +168 -7
- package/dist/types/data-structures/matrix/navigator.d.ts +54 -13
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +27 -1
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +26 -1
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +15 -2
- package/dist/types/data-structures/queue/deque.d.ts +431 -4
- package/dist/types/data-structures/queue/queue.d.ts +308 -4
- package/dist/types/data-structures/stack/stack.d.ts +306 -2
- package/dist/types/data-structures/tree/tree.d.ts +62 -1
- package/dist/types/data-structures/trie/trie.d.ts +350 -4
- package/dist/types/index.d.ts +11 -2
- package/dist/{interfaces → types/interfaces}/binary-tree.d.ts +3 -3
- package/dist/types/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/binary-tree.d.ts +29 -0
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +12 -0
- package/dist/{data-structures → types/types/data-structures}/binary-tree/index.d.ts +2 -3
- package/dist/types/types/data-structures/binary-tree/red-black-tree.d.ts +3 -0
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -0
- package/dist/types/types/data-structures/graph/abstract-graph.d.ts +14 -0
- package/dist/types/types/data-structures/graph/directed-graph.d.ts +1 -0
- package/dist/{data-structures → types/types/data-structures}/graph/index.d.ts +1 -2
- package/dist/types/types/data-structures/graph/map-graph.d.ts +1 -0
- package/dist/types/types/data-structures/graph/undirected-graph.d.ts +1 -0
- package/dist/types/types/data-structures/hash/hash-map.d.ts +19 -0
- package/dist/types/types/data-structures/hash/index.d.ts +2 -0
- package/dist/types/types/data-structures/heap/heap.d.ts +5 -0
- package/dist/types/types/data-structures/heap/index.d.ts +1 -0
- package/dist/types/types/data-structures/heap/max-heap.d.ts +1 -0
- package/dist/types/types/data-structures/heap/min-heap.d.ts +1 -0
- package/dist/types/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -0
- package/dist/types/types/data-structures/linked-list/singly-linked-list.d.ts +2 -0
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +4 -0
- package/dist/types/types/data-structures/matrix/matrix.d.ts +7 -0
- package/dist/types/types/data-structures/matrix/navigator.d.ts +14 -0
- package/dist/types/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -0
- package/dist/types/types/data-structures/priority-queue/min-priority-queue.d.ts +1 -0
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +2 -0
- package/dist/types/types/data-structures/queue/deque.d.ts +4 -0
- package/dist/types/types/data-structures/queue/queue.d.ts +4 -0
- package/dist/types/types/data-structures/stack/stack.d.ts +2 -0
- package/dist/types/types/data-structures/tree/tree.d.ts +1 -0
- package/dist/types/types/data-structures/trie/trie.d.ts +4 -0
- package/dist/types/types/index.d.ts +3 -0
- package/dist/types/types/utils/index.d.ts +2 -0
- package/dist/types/types/utils/utils.d.ts +22 -0
- package/dist/types/utils/index.d.ts +1 -1
- package/dist/types/utils/utils.d.ts +209 -22
- package/dist/umd/deque-typed.js +1232 -0
- package/dist/umd/deque-typed.js.map +1 -0
- package/dist/umd/deque-typed.min.js +9 -0
- package/dist/umd/deque-typed.min.js.map +1 -0
- package/package.json +25 -5
- package/src/data-structures/binary-tree/avl-tree-counter.ts +8 -11
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +6 -11
- package/src/data-structures/binary-tree/avl-tree.ts +6 -8
- package/src/data-structures/binary-tree/binary-tree.ts +13 -15
- package/src/data-structures/binary-tree/bst.ts +6 -11
- package/src/data-structures/binary-tree/red-black-tree.ts +6 -11
- package/src/data-structures/binary-tree/tree-counter.ts +8 -13
- package/src/data-structures/binary-tree/tree-multi-map.ts +6 -11
- package/src/data-structures/heap/heap.ts +5 -5
- package/src/data-structures/linked-list/singly-linked-list.ts +2 -2
- package/src/interfaces/binary-tree.ts +3 -3
- package/tsconfig.base.json +23 -0
- package/tsconfig.json +8 -34
- package/tsconfig.test.json +8 -0
- package/tsconfig.types.json +15 -0
- package/tsup.config.js +28 -0
- package/tsup.node.config.js +37 -0
- package/dist/common/index.js +0 -28
- package/dist/constants/index.js +0 -8
- package/dist/data-structures/base/index.d.ts +0 -2
- package/dist/data-structures/base/index.js +0 -18
- package/dist/data-structures/base/iterable-element-base.js +0 -243
- package/dist/data-structures/base/iterable-entry-base.js +0 -183
- package/dist/data-structures/base/linear-base.js +0 -415
- package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +0 -182
- package/dist/data-structures/binary-tree/avl-tree-counter.js +0 -374
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -135
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +0 -250
- package/dist/data-structures/binary-tree/avl-tree.d.ts +0 -291
- package/dist/data-structures/binary-tree/avl-tree.js +0 -611
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -174
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +0 -294
- package/dist/data-structures/binary-tree/binary-tree.d.ts +0 -754
- package/dist/data-structures/binary-tree/binary-tree.js +0 -1925
- package/dist/data-structures/binary-tree/bst.d.ts +0 -413
- package/dist/data-structures/binary-tree/bst.js +0 -903
- package/dist/data-structures/binary-tree/index.js +0 -26
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +0 -208
- package/dist/data-structures/binary-tree/red-black-tree.js +0 -546
- package/dist/data-structures/binary-tree/segment-tree.d.ts +0 -160
- package/dist/data-structures/binary-tree/segment-tree.js +0 -297
- package/dist/data-structures/binary-tree/tree-counter.d.ts +0 -190
- package/dist/data-structures/binary-tree/tree-counter.js +0 -413
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +0 -270
- package/dist/data-structures/binary-tree/tree-multi-map.js +0 -384
- package/dist/data-structures/graph/abstract-graph.d.ts +0 -340
- package/dist/data-structures/graph/abstract-graph.js +0 -896
- package/dist/data-structures/graph/directed-graph.d.ts +0 -207
- package/dist/data-structures/graph/directed-graph.js +0 -525
- package/dist/data-structures/graph/index.js +0 -20
- package/dist/data-structures/graph/map-graph.d.ts +0 -78
- package/dist/data-structures/graph/map-graph.js +0 -107
- package/dist/data-structures/graph/undirected-graph.d.ts +0 -188
- package/dist/data-structures/graph/undirected-graph.js +0 -424
- package/dist/data-structures/hash/hash-map.d.ts +0 -345
- package/dist/data-structures/hash/hash-map.js +0 -692
- package/dist/data-structures/hash/index.d.ts +0 -1
- package/dist/data-structures/hash/index.js +0 -17
- package/dist/data-structures/heap/heap.d.ts +0 -503
- package/dist/data-structures/heap/heap.js +0 -901
- package/dist/data-structures/heap/index.d.ts +0 -3
- package/dist/data-structures/heap/index.js +0 -19
- package/dist/data-structures/heap/max-heap.d.ts +0 -32
- package/dist/data-structures/heap/max-heap.js +0 -40
- package/dist/data-structures/heap/min-heap.d.ts +0 -33
- package/dist/data-structures/heap/min-heap.js +0 -31
- package/dist/data-structures/index.js +0 -28
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +0 -769
- package/dist/data-structures/linked-list/doubly-linked-list.js +0 -1111
- package/dist/data-structures/linked-list/index.js +0 -19
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +0 -451
- package/dist/data-structures/linked-list/singly-linked-list.js +0 -850
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -27
- package/dist/data-structures/linked-list/skip-linked-list.js +0 -144
- package/dist/data-structures/matrix/index.js +0 -18
- package/dist/data-structures/matrix/matrix.d.ts +0 -168
- package/dist/data-structures/matrix/matrix.js +0 -448
- package/dist/data-structures/matrix/navigator.d.ts +0 -55
- package/dist/data-structures/matrix/navigator.js +0 -111
- package/dist/data-structures/priority-queue/index.js +0 -19
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +0 -27
- package/dist/data-structures/priority-queue/max-priority-queue.js +0 -34
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +0 -26
- package/dist/data-structures/priority-queue/min-priority-queue.js +0 -24
- package/dist/data-structures/priority-queue/priority-queue.d.ts +0 -15
- package/dist/data-structures/priority-queue/priority-queue.js +0 -20
- package/dist/data-structures/queue/deque.d.ts +0 -431
- package/dist/data-structures/queue/deque.js +0 -879
- package/dist/data-structures/queue/index.js +0 -18
- package/dist/data-structures/queue/queue.d.ts +0 -308
- package/dist/data-structures/queue/queue.js +0 -473
- package/dist/data-structures/stack/index.js +0 -17
- package/dist/data-structures/stack/stack.d.ts +0 -306
- package/dist/data-structures/stack/stack.js +0 -401
- package/dist/data-structures/tree/index.js +0 -17
- package/dist/data-structures/tree/tree.d.ts +0 -62
- package/dist/data-structures/tree/tree.js +0 -107
- package/dist/data-structures/trie/index.js +0 -17
- package/dist/data-structures/trie/trie.d.ts +0 -350
- package/dist/data-structures/trie/trie.js +0 -610
- package/dist/index.d.ts +0 -12
- package/dist/index.js +0 -28
- package/dist/interfaces/binary-tree.js +0 -2
- package/dist/interfaces/doubly-linked-list.js +0 -2
- package/dist/interfaces/graph.js +0 -2
- package/dist/interfaces/heap.js +0 -2
- package/dist/interfaces/index.js +0 -24
- package/dist/interfaces/navigator.js +0 -2
- package/dist/interfaces/priority-queue.js +0 -2
- package/dist/interfaces/segment-tree.js +0 -2
- package/dist/interfaces/singly-linked-list.js +0 -2
- package/dist/types/common.js +0 -2
- package/dist/types/data-structures/base/base.js +0 -2
- package/dist/types/data-structures/base/index.js +0 -17
- package/dist/types/data-structures/binary-tree/avl-tree-counter.js +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.js +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/binary-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/bst.js +0 -2
- package/dist/types/data-structures/binary-tree/index.js +0 -25
- package/dist/types/data-structures/binary-tree/red-black-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/segment-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/tree-counter.js +0 -2
- package/dist/types/data-structures/binary-tree/tree-multi-map.js +0 -2
- package/dist/types/data-structures/graph/abstract-graph.js +0 -2
- package/dist/types/data-structures/graph/directed-graph.js +0 -2
- package/dist/types/data-structures/graph/index.js +0 -19
- package/dist/types/data-structures/graph/map-graph.js +0 -2
- package/dist/types/data-structures/graph/undirected-graph.js +0 -2
- package/dist/types/data-structures/hash/hash-map.js +0 -2
- package/dist/types/data-structures/hash/index.js +0 -17
- package/dist/types/data-structures/heap/heap.js +0 -2
- package/dist/types/data-structures/heap/index.js +0 -17
- package/dist/types/data-structures/heap/max-heap.js +0 -2
- package/dist/types/data-structures/heap/min-heap.js +0 -2
- package/dist/types/data-structures/index.js +0 -28
- package/dist/types/data-structures/linked-list/doubly-linked-list.js +0 -2
- package/dist/types/data-structures/linked-list/index.js +0 -19
- package/dist/types/data-structures/linked-list/singly-linked-list.js +0 -2
- package/dist/types/data-structures/linked-list/skip-linked-list.js +0 -2
- package/dist/types/data-structures/matrix/index.js +0 -18
- package/dist/types/data-structures/matrix/matrix.js +0 -2
- package/dist/types/data-structures/matrix/navigator.js +0 -2
- package/dist/types/data-structures/priority-queue/index.js +0 -19
- package/dist/types/data-structures/priority-queue/max-priority-queue.js +0 -2
- package/dist/types/data-structures/priority-queue/min-priority-queue.js +0 -2
- package/dist/types/data-structures/priority-queue/priority-queue.js +0 -2
- package/dist/types/data-structures/queue/deque.js +0 -2
- package/dist/types/data-structures/queue/index.js +0 -18
- package/dist/types/data-structures/queue/queue.js +0 -2
- package/dist/types/data-structures/stack/index.js +0 -17
- package/dist/types/data-structures/stack/stack.js +0 -2
- package/dist/types/data-structures/tree/index.js +0 -17
- package/dist/types/data-structures/tree/tree.js +0 -2
- package/dist/types/data-structures/trie/index.js +0 -17
- package/dist/types/data-structures/trie/trie.js +0 -2
- package/dist/types/index.js +0 -19
- package/dist/types/utils/index.js +0 -18
- package/dist/types/utils/utils.js +0 -2
- package/dist/types/utils/validate-type.js +0 -2
- package/dist/utils/index.d.ts +0 -2
- package/dist/utils/index.js +0 -18
- package/dist/utils/number.js +0 -24
- package/dist/utils/utils.d.ts +0 -209
- package/dist/utils/utils.js +0 -353
- package/dist/{common → types/common}/index.d.ts +0 -0
- package/dist/{constants → types/constants}/index.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/iterable-element-base.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/iterable-entry-base.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/linear-base.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/doubly-linked-list.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/graph.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/heap.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/index.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/navigator.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/priority-queue.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/segment-tree.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/singly-linked-list.d.ts +0 -0
- package/dist/types/{common.d.ts → types/common.d.ts} +0 -0
- package/dist/types/{data-structures → types/data-structures}/base/base.d.ts +0 -0
- package/dist/{data-structures → types/types/data-structures}/index.d.ts +7 -7
- package/dist/{data-structures → types/types/data-structures}/linked-list/index.d.ts +0 -0
- package/dist/{data-structures → types/types/data-structures}/matrix/index.d.ts +1 -1
- /package/dist/{data-structures → types/types/data-structures}/priority-queue/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/queue/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/stack/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/tree/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/trie/index.d.ts +0 -0
- /package/dist/types/{utils → types/utils}/validate-type.d.ts +0 -0
- /package/dist/{utils → types/utils}/number.d.ts +0 -0
|
@@ -0,0 +1,1223 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
5
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
6
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
7
|
+
|
|
8
|
+
// src/utils/utils.ts
|
|
9
|
+
var rangeCheck = /* @__PURE__ */ __name((index, min, max, message = "Index out of bounds.") => {
|
|
10
|
+
if (index < min || index > max) throw new RangeError(message);
|
|
11
|
+
}, "rangeCheck");
|
|
12
|
+
var calcMinUnitsRequired = /* @__PURE__ */ __name((totalQuantity, unitSize) => Math.floor((totalQuantity + unitSize - 1) / unitSize), "calcMinUnitsRequired");
|
|
13
|
+
function isPrimitiveComparable(value) {
|
|
14
|
+
const valueType = typeof value;
|
|
15
|
+
if (valueType === "number") return true;
|
|
16
|
+
return valueType === "bigint" || valueType === "string" || valueType === "boolean";
|
|
17
|
+
}
|
|
18
|
+
__name(isPrimitiveComparable, "isPrimitiveComparable");
|
|
19
|
+
function tryObjectToPrimitive(obj) {
|
|
20
|
+
if (typeof obj.valueOf === "function") {
|
|
21
|
+
const valueOfResult = obj.valueOf();
|
|
22
|
+
if (valueOfResult !== obj) {
|
|
23
|
+
if (isPrimitiveComparable(valueOfResult)) return valueOfResult;
|
|
24
|
+
if (typeof valueOfResult === "object" && valueOfResult !== null) return tryObjectToPrimitive(valueOfResult);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
if (typeof obj.toString === "function") {
|
|
28
|
+
const stringResult = obj.toString();
|
|
29
|
+
if (stringResult !== "[object Object]") return stringResult;
|
|
30
|
+
}
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
__name(tryObjectToPrimitive, "tryObjectToPrimitive");
|
|
34
|
+
function isComparable(value, isForceObjectComparable = false) {
|
|
35
|
+
if (value === null || value === void 0) return false;
|
|
36
|
+
if (isPrimitiveComparable(value)) return true;
|
|
37
|
+
if (typeof value !== "object") return false;
|
|
38
|
+
if (value instanceof Date) return true;
|
|
39
|
+
if (isForceObjectComparable) return true;
|
|
40
|
+
const comparableValue = tryObjectToPrimitive(value);
|
|
41
|
+
if (comparableValue === null || comparableValue === void 0) return false;
|
|
42
|
+
return isPrimitiveComparable(comparableValue);
|
|
43
|
+
}
|
|
44
|
+
__name(isComparable, "isComparable");
|
|
45
|
+
|
|
46
|
+
// src/data-structures/base/iterable-element-base.ts
|
|
47
|
+
var _IterableElementBase = class _IterableElementBase {
|
|
48
|
+
/**
|
|
49
|
+
* Create a new iterable base.
|
|
50
|
+
*
|
|
51
|
+
* @param options Optional behavior overrides. When provided, a `toElementFn`
|
|
52
|
+
* is used to convert a raw element (`R`) into a public element (`E`).
|
|
53
|
+
*
|
|
54
|
+
* @remarks
|
|
55
|
+
* Time O(1), Space O(1).
|
|
56
|
+
*/
|
|
57
|
+
constructor(options) {
|
|
58
|
+
/**
|
|
59
|
+
* The converter used to transform a raw element (`R`) into a public element (`E`).
|
|
60
|
+
*
|
|
61
|
+
* @remarks
|
|
62
|
+
* Time O(1), Space O(1).
|
|
63
|
+
*/
|
|
64
|
+
__publicField(this, "_toElementFn");
|
|
65
|
+
if (options) {
|
|
66
|
+
const { toElementFn } = options;
|
|
67
|
+
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
68
|
+
else if (toElementFn) throw new TypeError("toElementFn must be a function type");
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Exposes the current `toElementFn`, if configured.
|
|
73
|
+
*
|
|
74
|
+
* @returns The converter function or `undefined` when not set.
|
|
75
|
+
* @remarks
|
|
76
|
+
* Time O(1), Space O(1).
|
|
77
|
+
*/
|
|
78
|
+
get toElementFn() {
|
|
79
|
+
return this._toElementFn;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Returns an iterator over the structure's elements.
|
|
83
|
+
*
|
|
84
|
+
* @param args Optional iterator arguments forwarded to the internal iterator.
|
|
85
|
+
* @returns An `IterableIterator<E>` that yields the elements in traversal order.
|
|
86
|
+
*
|
|
87
|
+
* @remarks
|
|
88
|
+
* Producing the iterator is O(1); consuming the entire iterator is Time O(n) with O(1) extra space.
|
|
89
|
+
*/
|
|
90
|
+
*[Symbol.iterator](...args) {
|
|
91
|
+
yield* this._getIterator(...args);
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Returns an iterator over the values (alias of the default iterator).
|
|
95
|
+
*
|
|
96
|
+
* @returns An `IterableIterator<E>` over all elements.
|
|
97
|
+
* @remarks
|
|
98
|
+
* Creating the iterator is O(1); full iteration is Time O(n), Space O(1).
|
|
99
|
+
*/
|
|
100
|
+
*values() {
|
|
101
|
+
for (const item of this) yield item;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Tests whether all elements satisfy the predicate.
|
|
105
|
+
*
|
|
106
|
+
* @template TReturn
|
|
107
|
+
* @param predicate Function invoked for each element with signature `(value, index, self)`.
|
|
108
|
+
* @param thisArg Optional `this` binding for the predicate.
|
|
109
|
+
* @returns `true` if every element passes; otherwise `false`.
|
|
110
|
+
*
|
|
111
|
+
* @remarks
|
|
112
|
+
* Time O(n) in the worst case; may exit early when the first failure is found. Space O(1).
|
|
113
|
+
*/
|
|
114
|
+
every(predicate, thisArg) {
|
|
115
|
+
let index = 0;
|
|
116
|
+
for (const item of this) {
|
|
117
|
+
if (thisArg === void 0) {
|
|
118
|
+
if (!predicate(item, index++, this)) return false;
|
|
119
|
+
} else {
|
|
120
|
+
const fn = predicate;
|
|
121
|
+
if (!fn.call(thisArg, item, index++, this)) return false;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Tests whether at least one element satisfies the predicate.
|
|
128
|
+
*
|
|
129
|
+
* @param predicate Function invoked for each element with signature `(value, index, self)`.
|
|
130
|
+
* @param thisArg Optional `this` binding for the predicate.
|
|
131
|
+
* @returns `true` if any element passes; otherwise `false`.
|
|
132
|
+
*
|
|
133
|
+
* @remarks
|
|
134
|
+
* Time O(n) in the worst case; may exit early on first success. Space O(1).
|
|
135
|
+
*/
|
|
136
|
+
some(predicate, thisArg) {
|
|
137
|
+
let index = 0;
|
|
138
|
+
for (const item of this) {
|
|
139
|
+
if (thisArg === void 0) {
|
|
140
|
+
if (predicate(item, index++, this)) return true;
|
|
141
|
+
} else {
|
|
142
|
+
const fn = predicate;
|
|
143
|
+
if (fn.call(thisArg, item, index++, this)) return true;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Invokes a callback for each element in iteration order.
|
|
150
|
+
*
|
|
151
|
+
* @param callbackfn Function invoked per element with signature `(value, index, self)`.
|
|
152
|
+
* @param thisArg Optional `this` binding for the callback.
|
|
153
|
+
* @returns `void`.
|
|
154
|
+
*
|
|
155
|
+
* @remarks
|
|
156
|
+
* Time O(n), Space O(1).
|
|
157
|
+
*/
|
|
158
|
+
forEach(callbackfn, thisArg) {
|
|
159
|
+
let index = 0;
|
|
160
|
+
for (const item of this) {
|
|
161
|
+
if (thisArg === void 0) {
|
|
162
|
+
callbackfn(item, index++, this);
|
|
163
|
+
} else {
|
|
164
|
+
const fn = callbackfn;
|
|
165
|
+
fn.call(thisArg, item, index++, this);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
// Implementation signature
|
|
170
|
+
find(predicate, thisArg) {
|
|
171
|
+
let index = 0;
|
|
172
|
+
for (const item of this) {
|
|
173
|
+
if (thisArg === void 0) {
|
|
174
|
+
if (predicate(item, index++, this)) return item;
|
|
175
|
+
} else {
|
|
176
|
+
const fn = predicate;
|
|
177
|
+
if (fn.call(thisArg, item, index++, this)) return item;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Checks whether a strictly-equal element exists in the structure.
|
|
184
|
+
*
|
|
185
|
+
* @param element The element to test with `===` equality.
|
|
186
|
+
* @returns `true` if an equal element is found; otherwise `false`.
|
|
187
|
+
*
|
|
188
|
+
* @remarks
|
|
189
|
+
* Time O(n) in the worst case. Space O(1).
|
|
190
|
+
*/
|
|
191
|
+
has(element) {
|
|
192
|
+
for (const ele of this) if (ele === element) return true;
|
|
193
|
+
return false;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Reduces all elements to a single accumulated value.
|
|
197
|
+
*
|
|
198
|
+
* @overload
|
|
199
|
+
* @param callbackfn Reducer of signature `(acc, value, index, self) => nextAcc`. The first element is used as the initial accumulator.
|
|
200
|
+
* @returns The final accumulated value typed as `E`.
|
|
201
|
+
*
|
|
202
|
+
* @overload
|
|
203
|
+
* @param callbackfn Reducer of signature `(acc, value, index, self) => nextAcc`.
|
|
204
|
+
* @param initialValue The initial accumulator value of type `E`.
|
|
205
|
+
* @returns The final accumulated value typed as `E`.
|
|
206
|
+
*
|
|
207
|
+
* @overload
|
|
208
|
+
* @template U The accumulator type when it differs from `E`.
|
|
209
|
+
* @param callbackfn Reducer of signature `(acc: U, value, index, self) => U`.
|
|
210
|
+
* @param initialValue The initial accumulator value of type `U`.
|
|
211
|
+
* @returns The final accumulated value typed as `U`.
|
|
212
|
+
*
|
|
213
|
+
* @remarks
|
|
214
|
+
* Time O(n), Space O(1). Throws if called on an empty structure without `initialValue`.
|
|
215
|
+
*/
|
|
216
|
+
reduce(callbackfn, initialValue) {
|
|
217
|
+
let index = 0;
|
|
218
|
+
const iter = this[Symbol.iterator]();
|
|
219
|
+
let acc;
|
|
220
|
+
if (arguments.length >= 2) {
|
|
221
|
+
acc = initialValue;
|
|
222
|
+
} else {
|
|
223
|
+
const first = iter.next();
|
|
224
|
+
if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
|
|
225
|
+
acc = first.value;
|
|
226
|
+
index = 1;
|
|
227
|
+
}
|
|
228
|
+
for (const value of iter) {
|
|
229
|
+
acc = callbackfn(acc, value, index++, this);
|
|
230
|
+
}
|
|
231
|
+
return acc;
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Materializes the elements into a new array.
|
|
235
|
+
*
|
|
236
|
+
* @returns A shallow array copy of the iteration order.
|
|
237
|
+
* @remarks
|
|
238
|
+
* Time O(n), Space O(n).
|
|
239
|
+
*/
|
|
240
|
+
toArray() {
|
|
241
|
+
return [...this];
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Returns a representation of the structure suitable for quick visualization.
|
|
245
|
+
* Defaults to an array of elements; subclasses may override to provide richer visuals.
|
|
246
|
+
*
|
|
247
|
+
* @returns A visual representation (array by default).
|
|
248
|
+
* @remarks
|
|
249
|
+
* Time O(n), Space O(n).
|
|
250
|
+
*/
|
|
251
|
+
toVisual() {
|
|
252
|
+
return [...this];
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Prints `toVisual()` to the console. Intended for quick debugging.
|
|
256
|
+
*
|
|
257
|
+
* @returns `void`.
|
|
258
|
+
* @remarks
|
|
259
|
+
* Time O(n) due to materialization, Space O(n) for the intermediate representation.
|
|
260
|
+
*/
|
|
261
|
+
print() {
|
|
262
|
+
console.log(this.toVisual());
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
__name(_IterableElementBase, "IterableElementBase");
|
|
266
|
+
var IterableElementBase = _IterableElementBase;
|
|
267
|
+
|
|
268
|
+
// src/data-structures/base/linear-base.ts
|
|
269
|
+
var _LinearBase = class _LinearBase extends IterableElementBase {
|
|
270
|
+
/**
|
|
271
|
+
* Construct a linear container with runtime options.
|
|
272
|
+
* @param options - `{ maxLen?, ... }` bounds/behavior options.
|
|
273
|
+
* @remarks Time O(1), Space O(1)
|
|
274
|
+
*/
|
|
275
|
+
constructor(options) {
|
|
276
|
+
super(options);
|
|
277
|
+
__publicField(this, "_maxLen", -1);
|
|
278
|
+
if (options) {
|
|
279
|
+
const { maxLen } = options;
|
|
280
|
+
if (typeof maxLen === "number" && maxLen > 0 && maxLen % 1 === 0) this._maxLen = maxLen;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Upper bound for length (if positive), or `-1` when unbounded.
|
|
285
|
+
* @returns Maximum allowed length.
|
|
286
|
+
* @remarks Time O(1), Space O(1)
|
|
287
|
+
*/
|
|
288
|
+
get maxLen() {
|
|
289
|
+
return this._maxLen;
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* First index of a value from the left.
|
|
293
|
+
* @param searchElement - Value to match.
|
|
294
|
+
* @param fromIndex - Start position (supports negative index).
|
|
295
|
+
* @returns Index or `-1` if not found.
|
|
296
|
+
* @remarks Time O(n), Space O(1)
|
|
297
|
+
*/
|
|
298
|
+
indexOf(searchElement, fromIndex = 0) {
|
|
299
|
+
if (this.length === 0) return -1;
|
|
300
|
+
if (fromIndex < 0) fromIndex = this.length + fromIndex;
|
|
301
|
+
if (fromIndex < 0) fromIndex = 0;
|
|
302
|
+
for (let i = fromIndex; i < this.length; i++) {
|
|
303
|
+
const element = this.at(i);
|
|
304
|
+
if (element === searchElement) return i;
|
|
305
|
+
}
|
|
306
|
+
return -1;
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* Last index of a value from the right.
|
|
310
|
+
* @param searchElement - Value to match.
|
|
311
|
+
* @param fromIndex - Start position (supports negative index).
|
|
312
|
+
* @returns Index or `-1` if not found.
|
|
313
|
+
* @remarks Time O(n), Space O(1)
|
|
314
|
+
*/
|
|
315
|
+
lastIndexOf(searchElement, fromIndex = this.length - 1) {
|
|
316
|
+
if (this.length === 0) return -1;
|
|
317
|
+
if (fromIndex >= this.length) fromIndex = this.length - 1;
|
|
318
|
+
if (fromIndex < 0) fromIndex = this.length + fromIndex;
|
|
319
|
+
for (let i = fromIndex; i >= 0; i--) {
|
|
320
|
+
const element = this.at(i);
|
|
321
|
+
if (element === searchElement) return i;
|
|
322
|
+
}
|
|
323
|
+
return -1;
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Find the first index matching a predicate.
|
|
327
|
+
* @param predicate - `(element, index, self) => boolean`.
|
|
328
|
+
* @param thisArg - Optional `this` for callback.
|
|
329
|
+
* @returns Index or `-1`.
|
|
330
|
+
* @remarks Time O(n), Space O(1)
|
|
331
|
+
*/
|
|
332
|
+
findIndex(predicate, thisArg) {
|
|
333
|
+
for (let i = 0; i < this.length; i++) {
|
|
334
|
+
const item = this.at(i);
|
|
335
|
+
if (item !== void 0 && predicate.call(thisArg, item, i, this)) return i;
|
|
336
|
+
}
|
|
337
|
+
return -1;
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* Concatenate elements and/or containers.
|
|
341
|
+
* @param items - Elements or other containers.
|
|
342
|
+
* @returns New container with combined elements (`this` type).
|
|
343
|
+
* @remarks Time O(sum(length)), Space O(sum(length))
|
|
344
|
+
*/
|
|
345
|
+
concat(...items) {
|
|
346
|
+
const newList = this.clone();
|
|
347
|
+
for (const item of items) {
|
|
348
|
+
if (item instanceof _LinearBase) {
|
|
349
|
+
newList.pushMany(item);
|
|
350
|
+
} else {
|
|
351
|
+
newList.push(item);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
return newList;
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* In-place stable order via array sort semantics.
|
|
358
|
+
* @param compareFn - Comparator `(a, b) => number`.
|
|
359
|
+
* @returns This container.
|
|
360
|
+
* @remarks Time O(n log n), Space O(n) (materializes to array temporarily)
|
|
361
|
+
*/
|
|
362
|
+
sort(compareFn) {
|
|
363
|
+
const arr = this.toArray();
|
|
364
|
+
arr.sort(compareFn);
|
|
365
|
+
this.clear();
|
|
366
|
+
for (const item of arr) this.push(item);
|
|
367
|
+
return this;
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* Remove and/or insert elements at a position (array-compatible).
|
|
371
|
+
* @param start - Start index (supports negative index).
|
|
372
|
+
* @param deleteCount - How many to remove.
|
|
373
|
+
* @param items - Elements to insert.
|
|
374
|
+
* @returns Removed elements as a new list (`this` type).
|
|
375
|
+
* @remarks Time O(n + m), Space O(min(n, m)) where `m = items.length`
|
|
376
|
+
*/
|
|
377
|
+
splice(start, deleteCount = 0, ...items) {
|
|
378
|
+
const removedList = this._createInstance();
|
|
379
|
+
start = start < 0 ? this.length + start : start;
|
|
380
|
+
start = Math.max(0, Math.min(start, this.length));
|
|
381
|
+
deleteCount = Math.max(0, Math.min(deleteCount, this.length - start));
|
|
382
|
+
for (let i = 0; i < deleteCount; i++) {
|
|
383
|
+
const removed = this.deleteAt(start);
|
|
384
|
+
if (removed !== void 0) {
|
|
385
|
+
removedList.push(removed);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
for (let i = 0; i < items.length; i++) {
|
|
389
|
+
this.addAt(start + i, items[i]);
|
|
390
|
+
}
|
|
391
|
+
return removedList;
|
|
392
|
+
}
|
|
393
|
+
/**
|
|
394
|
+
* Join all elements into a string.
|
|
395
|
+
* @param separator - Separator string.
|
|
396
|
+
* @returns Concatenated string.
|
|
397
|
+
* @remarks Time O(n), Space O(n)
|
|
398
|
+
*/
|
|
399
|
+
join(separator = ",") {
|
|
400
|
+
return this.toArray().join(separator);
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* Snapshot elements into a reversed array.
|
|
404
|
+
* @returns New reversed array.
|
|
405
|
+
* @remarks Time O(n), Space O(n)
|
|
406
|
+
*/
|
|
407
|
+
toReversedArray() {
|
|
408
|
+
const array = [];
|
|
409
|
+
for (let i = this.length - 1; i >= 0; i--) {
|
|
410
|
+
array.push(this.at(i));
|
|
411
|
+
}
|
|
412
|
+
return array;
|
|
413
|
+
}
|
|
414
|
+
reduceRight(callbackfn, initialValue) {
|
|
415
|
+
let accumulator = initialValue != null ? initialValue : 0;
|
|
416
|
+
for (let i = this.length - 1; i >= 0; i--) {
|
|
417
|
+
accumulator = callbackfn(accumulator, this.at(i), i, this);
|
|
418
|
+
}
|
|
419
|
+
return accumulator;
|
|
420
|
+
}
|
|
421
|
+
/**
|
|
422
|
+
* Create a shallow copy of a subrange.
|
|
423
|
+
* @param start - Inclusive start (supports negative index).
|
|
424
|
+
* @param end - Exclusive end (supports negative index).
|
|
425
|
+
* @returns New list with the range (`this` type).
|
|
426
|
+
* @remarks Time O(n), Space O(n)
|
|
427
|
+
*/
|
|
428
|
+
slice(start = 0, end = this.length) {
|
|
429
|
+
start = start < 0 ? this.length + start : start;
|
|
430
|
+
end = end < 0 ? this.length + end : end;
|
|
431
|
+
const newList = this._createInstance();
|
|
432
|
+
for (let i = start; i < end; i++) {
|
|
433
|
+
newList.push(this.at(i));
|
|
434
|
+
}
|
|
435
|
+
return newList;
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* Fill a range with a value.
|
|
439
|
+
* @param value - Value to set.
|
|
440
|
+
* @param start - Inclusive start.
|
|
441
|
+
* @param end - Exclusive end.
|
|
442
|
+
* @returns This list.
|
|
443
|
+
* @remarks Time O(n), Space O(1)
|
|
444
|
+
*/
|
|
445
|
+
fill(value, start = 0, end = this.length) {
|
|
446
|
+
start = start < 0 ? this.length + start : start;
|
|
447
|
+
end = end < 0 ? this.length + end : end;
|
|
448
|
+
if (start < 0) start = 0;
|
|
449
|
+
if (end > this.length) end = this.length;
|
|
450
|
+
if (start >= end) return this;
|
|
451
|
+
for (let i = start; i < end; i++) {
|
|
452
|
+
this.setAt(i, value);
|
|
453
|
+
}
|
|
454
|
+
return this;
|
|
455
|
+
}
|
|
456
|
+
};
|
|
457
|
+
__name(_LinearBase, "LinearBase");
|
|
458
|
+
var LinearBase = _LinearBase;
|
|
459
|
+
|
|
460
|
+
// src/data-structures/queue/deque.ts
|
|
461
|
+
var _Deque = class _Deque extends LinearBase {
|
|
462
|
+
/**
|
|
463
|
+
* Create a Deque and optionally bulk-insert elements.
|
|
464
|
+
* @remarks Time O(N), Space O(N)
|
|
465
|
+
* @param [elements] - Iterable (or iterable-like) of elements/records to insert.
|
|
466
|
+
* @param [options] - Options such as bucketSize, toElementFn, and maxLen.
|
|
467
|
+
* @returns New Deque instance.
|
|
468
|
+
*/
|
|
469
|
+
constructor(elements = [], options) {
|
|
470
|
+
super(options);
|
|
471
|
+
__publicField(this, "_equals", Object.is);
|
|
472
|
+
__publicField(this, "_bucketSize", 1 << 12);
|
|
473
|
+
__publicField(this, "_bucketFirst", 0);
|
|
474
|
+
__publicField(this, "_firstInBucket", 0);
|
|
475
|
+
__publicField(this, "_bucketLast", 0);
|
|
476
|
+
__publicField(this, "_lastInBucket", 0);
|
|
477
|
+
__publicField(this, "_bucketCount", 0);
|
|
478
|
+
__publicField(this, "_buckets", []);
|
|
479
|
+
__publicField(this, "_length", 0);
|
|
480
|
+
if (options) {
|
|
481
|
+
const { bucketSize } = options;
|
|
482
|
+
if (typeof bucketSize === "number") this._bucketSize = bucketSize;
|
|
483
|
+
}
|
|
484
|
+
let _size;
|
|
485
|
+
if ("length" in elements) {
|
|
486
|
+
_size = typeof elements.length === "function" ? elements.length() : elements.length;
|
|
487
|
+
} else {
|
|
488
|
+
_size = typeof elements.size === "function" ? elements.size() : elements.size;
|
|
489
|
+
}
|
|
490
|
+
this._bucketCount = calcMinUnitsRequired(_size, this._bucketSize) || 1;
|
|
491
|
+
for (let i = 0; i < this._bucketCount; ++i) {
|
|
492
|
+
this._buckets.push(new Array(this._bucketSize));
|
|
493
|
+
}
|
|
494
|
+
const needBucketNum = calcMinUnitsRequired(_size, this._bucketSize);
|
|
495
|
+
this._bucketFirst = this._bucketLast = (this._bucketCount >> 1) - (needBucketNum >> 1);
|
|
496
|
+
this._firstInBucket = this._lastInBucket = this._bucketSize - _size % this._bucketSize >> 1;
|
|
497
|
+
this.pushMany(elements);
|
|
498
|
+
}
|
|
499
|
+
/**
|
|
500
|
+
* Get the current bucket size.
|
|
501
|
+
* @remarks Time O(1), Space O(1)
|
|
502
|
+
* @returns Bucket capacity per bucket.
|
|
503
|
+
*/
|
|
504
|
+
get bucketSize() {
|
|
505
|
+
return this._bucketSize;
|
|
506
|
+
}
|
|
507
|
+
/**
|
|
508
|
+
* Get the index of the first bucket in use.
|
|
509
|
+
* @remarks Time O(1), Space O(1)
|
|
510
|
+
* @returns Zero-based bucket index.
|
|
511
|
+
*/
|
|
512
|
+
get bucketFirst() {
|
|
513
|
+
return this._bucketFirst;
|
|
514
|
+
}
|
|
515
|
+
/**
|
|
516
|
+
* Get the index inside the first bucket.
|
|
517
|
+
* @remarks Time O(1), Space O(1)
|
|
518
|
+
* @returns Zero-based index within the first bucket.
|
|
519
|
+
*/
|
|
520
|
+
get firstInBucket() {
|
|
521
|
+
return this._firstInBucket;
|
|
522
|
+
}
|
|
523
|
+
/**
|
|
524
|
+
* Get the index of the last bucket in use.
|
|
525
|
+
* @remarks Time O(1), Space O(1)
|
|
526
|
+
* @returns Zero-based bucket index.
|
|
527
|
+
*/
|
|
528
|
+
get bucketLast() {
|
|
529
|
+
return this._bucketLast;
|
|
530
|
+
}
|
|
531
|
+
/**
|
|
532
|
+
* Get the index inside the last bucket.
|
|
533
|
+
* @remarks Time O(1), Space O(1)
|
|
534
|
+
* @returns Zero-based index within the last bucket.
|
|
535
|
+
*/
|
|
536
|
+
get lastInBucket() {
|
|
537
|
+
return this._lastInBucket;
|
|
538
|
+
}
|
|
539
|
+
/**
|
|
540
|
+
* Get the number of buckets allocated.
|
|
541
|
+
* @remarks Time O(1), Space O(1)
|
|
542
|
+
* @returns Bucket count.
|
|
543
|
+
*/
|
|
544
|
+
get bucketCount() {
|
|
545
|
+
return this._bucketCount;
|
|
546
|
+
}
|
|
547
|
+
/**
|
|
548
|
+
* Get the internal buckets array.
|
|
549
|
+
* @remarks Time O(1), Space O(1)
|
|
550
|
+
* @returns Array of buckets storing values.
|
|
551
|
+
*/
|
|
552
|
+
get buckets() {
|
|
553
|
+
return this._buckets;
|
|
554
|
+
}
|
|
555
|
+
/**
|
|
556
|
+
* Get the number of elements in the deque.
|
|
557
|
+
* @remarks Time O(1), Space O(1)
|
|
558
|
+
* @returns Current length.
|
|
559
|
+
*/
|
|
560
|
+
get length() {
|
|
561
|
+
return this._length;
|
|
562
|
+
}
|
|
563
|
+
/**
|
|
564
|
+
* Get the first element without removing it.
|
|
565
|
+
* @remarks Time O(1), Space O(1)
|
|
566
|
+
* @returns First element or undefined.
|
|
567
|
+
*/
|
|
568
|
+
get first() {
|
|
569
|
+
if (this._length === 0) return;
|
|
570
|
+
return this._buckets[this._bucketFirst][this._firstInBucket];
|
|
571
|
+
}
|
|
572
|
+
/**
|
|
573
|
+
* Get the last element without removing it.
|
|
574
|
+
* @remarks Time O(1), Space O(1)
|
|
575
|
+
* @returns Last element or undefined.
|
|
576
|
+
*/
|
|
577
|
+
get last() {
|
|
578
|
+
if (this._length === 0) return;
|
|
579
|
+
return this._buckets[this._bucketLast][this._lastInBucket];
|
|
580
|
+
}
|
|
581
|
+
/**
|
|
582
|
+
* Create a Deque from an array of elements.
|
|
583
|
+
* @remarks Time O(N), Space O(N)
|
|
584
|
+
* @template E
|
|
585
|
+
* @template R
|
|
586
|
+
* @param this - Constructor (subclass) to instantiate.
|
|
587
|
+
* @param data - Array of elements to insert in order.
|
|
588
|
+
* @param [options] - Options forwarded to the constructor.
|
|
589
|
+
* @returns A new Deque populated from the array.
|
|
590
|
+
*/
|
|
591
|
+
static fromArray(data, options) {
|
|
592
|
+
return new this(data, options);
|
|
593
|
+
}
|
|
594
|
+
/**
|
|
595
|
+
* Append one element at the back.
|
|
596
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
597
|
+
* @param element - Element to append.
|
|
598
|
+
* @returns True when appended.
|
|
599
|
+
*/
|
|
600
|
+
push(element) {
|
|
601
|
+
if (this._length) {
|
|
602
|
+
if (this._lastInBucket < this._bucketSize - 1) {
|
|
603
|
+
this._lastInBucket += 1;
|
|
604
|
+
} else if (this._bucketLast < this._bucketCount - 1) {
|
|
605
|
+
this._bucketLast += 1;
|
|
606
|
+
this._lastInBucket = 0;
|
|
607
|
+
} else {
|
|
608
|
+
this._bucketLast = 0;
|
|
609
|
+
this._lastInBucket = 0;
|
|
610
|
+
}
|
|
611
|
+
if (this._bucketLast === this._bucketFirst && this._lastInBucket === this._firstInBucket) this._reallocate();
|
|
612
|
+
}
|
|
613
|
+
this._length += 1;
|
|
614
|
+
this._buckets[this._bucketLast][this._lastInBucket] = element;
|
|
615
|
+
if (this._maxLen > 0 && this._length > this._maxLen) this.shift();
|
|
616
|
+
return true;
|
|
617
|
+
}
|
|
618
|
+
/**
|
|
619
|
+
* Remove and return the last element.
|
|
620
|
+
* @remarks Time O(1), Space O(1)
|
|
621
|
+
* @returns Removed element or undefined.
|
|
622
|
+
*/
|
|
623
|
+
pop() {
|
|
624
|
+
if (this._length === 0) return;
|
|
625
|
+
const element = this._buckets[this._bucketLast][this._lastInBucket];
|
|
626
|
+
if (this._length !== 1) {
|
|
627
|
+
if (this._lastInBucket > 0) {
|
|
628
|
+
this._lastInBucket -= 1;
|
|
629
|
+
} else if (this._bucketLast > 0) {
|
|
630
|
+
this._bucketLast -= 1;
|
|
631
|
+
this._lastInBucket = this._bucketSize - 1;
|
|
632
|
+
} else {
|
|
633
|
+
this._bucketLast = this._bucketCount - 1;
|
|
634
|
+
this._lastInBucket = this._bucketSize - 1;
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
this._length -= 1;
|
|
638
|
+
return element;
|
|
639
|
+
}
|
|
640
|
+
/**
|
|
641
|
+
* Remove and return the first element.
|
|
642
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
643
|
+
* @returns Removed element or undefined.
|
|
644
|
+
*/
|
|
645
|
+
shift() {
|
|
646
|
+
if (this._length === 0) return;
|
|
647
|
+
const element = this._buckets[this._bucketFirst][this._firstInBucket];
|
|
648
|
+
if (this._length !== 1) {
|
|
649
|
+
if (this._firstInBucket < this._bucketSize - 1) {
|
|
650
|
+
this._firstInBucket += 1;
|
|
651
|
+
} else if (this._bucketFirst < this._bucketCount - 1) {
|
|
652
|
+
this._bucketFirst += 1;
|
|
653
|
+
this._firstInBucket = 0;
|
|
654
|
+
} else {
|
|
655
|
+
this._bucketFirst = 0;
|
|
656
|
+
this._firstInBucket = 0;
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
this._length -= 1;
|
|
660
|
+
return element;
|
|
661
|
+
}
|
|
662
|
+
/**
|
|
663
|
+
* Prepend one element at the front.
|
|
664
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
665
|
+
* @param element - Element to prepend.
|
|
666
|
+
* @returns True when prepended.
|
|
667
|
+
*/
|
|
668
|
+
unshift(element) {
|
|
669
|
+
if (this._length) {
|
|
670
|
+
if (this._firstInBucket > 0) {
|
|
671
|
+
this._firstInBucket -= 1;
|
|
672
|
+
} else if (this._bucketFirst > 0) {
|
|
673
|
+
this._bucketFirst -= 1;
|
|
674
|
+
this._firstInBucket = this._bucketSize - 1;
|
|
675
|
+
} else {
|
|
676
|
+
this._bucketFirst = this._bucketCount - 1;
|
|
677
|
+
this._firstInBucket = this._bucketSize - 1;
|
|
678
|
+
}
|
|
679
|
+
if (this._bucketFirst === this._bucketLast && this._firstInBucket === this._lastInBucket) this._reallocate();
|
|
680
|
+
}
|
|
681
|
+
this._length += 1;
|
|
682
|
+
this._buckets[this._bucketFirst][this._firstInBucket] = element;
|
|
683
|
+
if (this._maxLen > 0 && this._length > this._maxLen) this.pop();
|
|
684
|
+
return true;
|
|
685
|
+
}
|
|
686
|
+
/**
|
|
687
|
+
* Append a sequence of elements.
|
|
688
|
+
* @remarks Time O(N), Space O(1)
|
|
689
|
+
* @param elements - Iterable (or iterable-like) of elements/records.
|
|
690
|
+
* @returns Array of per-element success flags.
|
|
691
|
+
*/
|
|
692
|
+
pushMany(elements) {
|
|
693
|
+
const ans = [];
|
|
694
|
+
for (const el of elements) {
|
|
695
|
+
if (this.toElementFn) {
|
|
696
|
+
ans.push(this.push(this.toElementFn(el)));
|
|
697
|
+
} else {
|
|
698
|
+
ans.push(this.push(el));
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
return ans;
|
|
702
|
+
}
|
|
703
|
+
/**
|
|
704
|
+
* Prepend a sequence of elements.
|
|
705
|
+
* @remarks Time O(N), Space O(1)
|
|
706
|
+
* @param [elements] - Iterable (or iterable-like) of elements/records.
|
|
707
|
+
* @returns Array of per-element success flags.
|
|
708
|
+
*/
|
|
709
|
+
unshiftMany(elements = []) {
|
|
710
|
+
const ans = [];
|
|
711
|
+
for (const el of elements) {
|
|
712
|
+
if (this.toElementFn) {
|
|
713
|
+
ans.push(this.unshift(this.toElementFn(el)));
|
|
714
|
+
} else {
|
|
715
|
+
ans.push(this.unshift(el));
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
return ans;
|
|
719
|
+
}
|
|
720
|
+
/**
|
|
721
|
+
* Check whether the deque is empty.
|
|
722
|
+
* @remarks Time O(1), Space O(1)
|
|
723
|
+
* @returns True if length is 0.
|
|
724
|
+
*/
|
|
725
|
+
isEmpty() {
|
|
726
|
+
return this._length === 0;
|
|
727
|
+
}
|
|
728
|
+
/**
|
|
729
|
+
* Remove all elements and reset structure.
|
|
730
|
+
* @remarks Time O(1), Space O(1)
|
|
731
|
+
* @returns void
|
|
732
|
+
*/
|
|
733
|
+
clear() {
|
|
734
|
+
this._buckets = [new Array(this._bucketSize)];
|
|
735
|
+
this._bucketCount = 1;
|
|
736
|
+
this._bucketFirst = this._bucketLast = this._length = 0;
|
|
737
|
+
this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
|
|
738
|
+
}
|
|
739
|
+
/**
|
|
740
|
+
* Get the element at a given position.
|
|
741
|
+
* @remarks Time O(1), Space O(1)
|
|
742
|
+
* @param pos - Zero-based position from the front.
|
|
743
|
+
* @returns Element or undefined.
|
|
744
|
+
*/
|
|
745
|
+
at(pos) {
|
|
746
|
+
if (pos < 0 || pos >= this._length) return void 0;
|
|
747
|
+
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
748
|
+
return this._buckets[bucketIndex][indexInBucket];
|
|
749
|
+
}
|
|
750
|
+
/**
|
|
751
|
+
* Replace the element at a given position.
|
|
752
|
+
* @remarks Time O(1), Space O(1)
|
|
753
|
+
* @param pos - Zero-based position from the front.
|
|
754
|
+
* @param element - New element value.
|
|
755
|
+
* @returns True if updated.
|
|
756
|
+
*/
|
|
757
|
+
setAt(pos, element) {
|
|
758
|
+
rangeCheck(pos, 0, this._length - 1);
|
|
759
|
+
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
760
|
+
this._buckets[bucketIndex][indexInBucket] = element;
|
|
761
|
+
return true;
|
|
762
|
+
}
|
|
763
|
+
/**
|
|
764
|
+
* Insert repeated copies of an element at a position.
|
|
765
|
+
* @remarks Time O(N), Space O(1)
|
|
766
|
+
* @param pos - Zero-based position from the front.
|
|
767
|
+
* @param element - Element to insert.
|
|
768
|
+
* @param [num] - Number of times to insert (default 1).
|
|
769
|
+
* @returns True if inserted.
|
|
770
|
+
*/
|
|
771
|
+
addAt(pos, element, num = 1) {
|
|
772
|
+
const length = this._length;
|
|
773
|
+
rangeCheck(pos, 0, length);
|
|
774
|
+
if (pos === 0) {
|
|
775
|
+
while (num--) this.unshift(element);
|
|
776
|
+
} else if (pos === this._length) {
|
|
777
|
+
while (num--) this.push(element);
|
|
778
|
+
} else {
|
|
779
|
+
const arr = [];
|
|
780
|
+
for (let i = pos; i < this._length; ++i) {
|
|
781
|
+
const v = this.at(i);
|
|
782
|
+
if (v !== void 0) arr.push(v);
|
|
783
|
+
}
|
|
784
|
+
this.cut(pos - 1, true);
|
|
785
|
+
for (let i = 0; i < num; ++i) this.push(element);
|
|
786
|
+
for (let i = 0; i < arr.length; ++i) this.push(arr[i]);
|
|
787
|
+
}
|
|
788
|
+
return true;
|
|
789
|
+
}
|
|
790
|
+
/**
|
|
791
|
+
* Cut the deque to keep items up to index; optionally mutate in-place.
|
|
792
|
+
* @remarks Time O(N), Space O(1)
|
|
793
|
+
* @param pos - Last index to keep.
|
|
794
|
+
* @param [isCutSelf] - When true, mutate this deque; otherwise return a new deque.
|
|
795
|
+
* @returns This deque if in-place; otherwise a new deque of the prefix.
|
|
796
|
+
*/
|
|
797
|
+
cut(pos, isCutSelf = false) {
|
|
798
|
+
if (isCutSelf) {
|
|
799
|
+
if (pos < 0) {
|
|
800
|
+
this.clear();
|
|
801
|
+
return this;
|
|
802
|
+
}
|
|
803
|
+
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
804
|
+
this._bucketLast = bucketIndex;
|
|
805
|
+
this._lastInBucket = indexInBucket;
|
|
806
|
+
this._length = pos + 1;
|
|
807
|
+
return this;
|
|
808
|
+
} else {
|
|
809
|
+
const newDeque = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
|
|
810
|
+
newDeque._setBucketSize(this._bucketSize);
|
|
811
|
+
for (let i = 0; i <= pos; i++) {
|
|
812
|
+
const v = this.at(i);
|
|
813
|
+
if (v !== void 0) newDeque.push(v);
|
|
814
|
+
}
|
|
815
|
+
return newDeque;
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
/**
|
|
819
|
+
* Remove and/or insert elements at a position (array-like behavior).
|
|
820
|
+
* @remarks Time O(N + M), Space O(M)
|
|
821
|
+
* @param start - Start index (clamped to [0, length]).
|
|
822
|
+
* @param [deleteCount] - Number of elements to remove (default: length - start).
|
|
823
|
+
* @param [items] - Elements to insert after `start`.
|
|
824
|
+
* @returns A new deque containing the removed elements (typed as `this`).
|
|
825
|
+
*/
|
|
826
|
+
splice(start, deleteCount = this._length - start, ...items) {
|
|
827
|
+
rangeCheck(start, 0, this._length);
|
|
828
|
+
if (deleteCount < 0) deleteCount = 0;
|
|
829
|
+
if (start + deleteCount > this._length) deleteCount = this._length - start;
|
|
830
|
+
const removed = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
|
|
831
|
+
removed._setBucketSize(this._bucketSize);
|
|
832
|
+
for (let i = 0; i < deleteCount; i++) {
|
|
833
|
+
const v = this.at(start + i);
|
|
834
|
+
if (v !== void 0) removed.push(v);
|
|
835
|
+
}
|
|
836
|
+
const tail = [];
|
|
837
|
+
for (let i = start + deleteCount; i < this._length; i++) {
|
|
838
|
+
const v = this.at(i);
|
|
839
|
+
if (v !== void 0) tail.push(v);
|
|
840
|
+
}
|
|
841
|
+
this.cut(start - 1, true);
|
|
842
|
+
for (const it of items) this.push(it);
|
|
843
|
+
for (const v of tail) this.push(v);
|
|
844
|
+
return removed;
|
|
845
|
+
}
|
|
846
|
+
/**
|
|
847
|
+
* Cut the deque to keep items from index onward; optionally mutate in-place.
|
|
848
|
+
* @remarks Time O(N), Space O(1)
|
|
849
|
+
* @param pos - First index to keep.
|
|
850
|
+
* @param [isCutSelf] - When true, mutate this deque; otherwise return a new deque.
|
|
851
|
+
* @returns This deque if in-place; otherwise a new deque of the suffix.
|
|
852
|
+
*/
|
|
853
|
+
cutRest(pos, isCutSelf = false) {
|
|
854
|
+
if (isCutSelf) {
|
|
855
|
+
if (pos < 0) {
|
|
856
|
+
return this;
|
|
857
|
+
}
|
|
858
|
+
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
859
|
+
this._bucketFirst = bucketIndex;
|
|
860
|
+
this._firstInBucket = indexInBucket;
|
|
861
|
+
this._length = this._length - pos;
|
|
862
|
+
return this;
|
|
863
|
+
} else {
|
|
864
|
+
const newDeque = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
|
|
865
|
+
newDeque._setBucketSize(this._bucketSize);
|
|
866
|
+
if (pos < 0) pos = 0;
|
|
867
|
+
for (let i = pos; i < this._length; i++) {
|
|
868
|
+
const v = this.at(i);
|
|
869
|
+
if (v !== void 0) newDeque.push(v);
|
|
870
|
+
}
|
|
871
|
+
return newDeque;
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
/**
|
|
875
|
+
* Delete the element at a given position.
|
|
876
|
+
* @remarks Time O(N), Space O(1)
|
|
877
|
+
* @param pos - Zero-based position from the front.
|
|
878
|
+
* @returns Removed element or undefined.
|
|
879
|
+
*/
|
|
880
|
+
deleteAt(pos) {
|
|
881
|
+
rangeCheck(pos, 0, this._length - 1);
|
|
882
|
+
let deleted;
|
|
883
|
+
if (pos === 0) {
|
|
884
|
+
return this.shift();
|
|
885
|
+
} else if (pos === this._length - 1) {
|
|
886
|
+
deleted = this.last;
|
|
887
|
+
this.pop();
|
|
888
|
+
return deleted;
|
|
889
|
+
} else {
|
|
890
|
+
const length = this._length - 1;
|
|
891
|
+
const { bucketIndex: targetBucket, indexInBucket: targetPointer } = this._getBucketAndPosition(pos);
|
|
892
|
+
deleted = this._buckets[targetBucket][targetPointer];
|
|
893
|
+
for (let i = pos; i < length; i++) {
|
|
894
|
+
const { bucketIndex: curBucket, indexInBucket: curPointer } = this._getBucketAndPosition(i);
|
|
895
|
+
const { bucketIndex: nextBucket, indexInBucket: nextPointer } = this._getBucketAndPosition(i + 1);
|
|
896
|
+
this._buckets[curBucket][curPointer] = this._buckets[nextBucket][nextPointer];
|
|
897
|
+
}
|
|
898
|
+
this.pop();
|
|
899
|
+
return deleted;
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
/**
|
|
903
|
+
* Delete the first occurrence of a value.
|
|
904
|
+
* @remarks Time O(N), Space O(1)
|
|
905
|
+
* @param element - Element to remove (using the configured equality).
|
|
906
|
+
* @returns True if an element was removed.
|
|
907
|
+
*/
|
|
908
|
+
delete(element) {
|
|
909
|
+
const size = this._length;
|
|
910
|
+
if (size === 0) return false;
|
|
911
|
+
let i = 0;
|
|
912
|
+
let index = 0;
|
|
913
|
+
while (i < size) {
|
|
914
|
+
const oldElement = this.at(i);
|
|
915
|
+
if (!this._equals(oldElement, element)) {
|
|
916
|
+
this.setAt(index, oldElement);
|
|
917
|
+
index += 1;
|
|
918
|
+
}
|
|
919
|
+
i += 1;
|
|
920
|
+
}
|
|
921
|
+
this.cut(index - 1, true);
|
|
922
|
+
return true;
|
|
923
|
+
}
|
|
924
|
+
/**
|
|
925
|
+
* Delete the first element matching a predicate.
|
|
926
|
+
* @remarks Time O(N), Space O(1)
|
|
927
|
+
* @param predicate - Function (value, index, deque) → boolean.
|
|
928
|
+
* @returns True if a match was removed.
|
|
929
|
+
*/
|
|
930
|
+
deleteWhere(predicate) {
|
|
931
|
+
for (let i = 0; i < this._length; i++) {
|
|
932
|
+
const v = this.at(i);
|
|
933
|
+
if (predicate(v, i, this)) {
|
|
934
|
+
this.deleteAt(i);
|
|
935
|
+
return true;
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
return false;
|
|
939
|
+
}
|
|
940
|
+
/**
|
|
941
|
+
* Set the equality comparator used by delete operations.
|
|
942
|
+
* @remarks Time O(1), Space O(1)
|
|
943
|
+
* @param equals - Equality predicate (a, b) → boolean.
|
|
944
|
+
* @returns This deque.
|
|
945
|
+
*/
|
|
946
|
+
setEquality(equals) {
|
|
947
|
+
this._equals = equals;
|
|
948
|
+
return this;
|
|
949
|
+
}
|
|
950
|
+
/**
|
|
951
|
+
* Reverse the deque by reversing buckets and pointers.
|
|
952
|
+
* @remarks Time O(N), Space O(N)
|
|
953
|
+
* @returns This deque.
|
|
954
|
+
*/
|
|
955
|
+
reverse() {
|
|
956
|
+
this._buckets.reverse().forEach(function(bucket) {
|
|
957
|
+
bucket.reverse();
|
|
958
|
+
});
|
|
959
|
+
const { _bucketFirst, _bucketLast, _firstInBucket, _lastInBucket } = this;
|
|
960
|
+
this._bucketFirst = this._bucketCount - _bucketLast - 1;
|
|
961
|
+
this._bucketLast = this._bucketCount - _bucketFirst - 1;
|
|
962
|
+
this._firstInBucket = this._bucketSize - _lastInBucket - 1;
|
|
963
|
+
this._lastInBucket = this._bucketSize - _firstInBucket - 1;
|
|
964
|
+
return this;
|
|
965
|
+
}
|
|
966
|
+
/**
|
|
967
|
+
* Deduplicate consecutive equal elements in-place.
|
|
968
|
+
* @remarks Time O(N), Space O(1)
|
|
969
|
+
* @returns This deque.
|
|
970
|
+
*/
|
|
971
|
+
unique() {
|
|
972
|
+
if (this._length <= 1) {
|
|
973
|
+
return this;
|
|
974
|
+
}
|
|
975
|
+
let index = 1;
|
|
976
|
+
let prev = this.at(0);
|
|
977
|
+
for (let i = 1; i < this._length; ++i) {
|
|
978
|
+
const cur = this.at(i);
|
|
979
|
+
if (!this._equals(cur, prev)) {
|
|
980
|
+
prev = cur;
|
|
981
|
+
this.setAt(index++, cur);
|
|
982
|
+
}
|
|
983
|
+
}
|
|
984
|
+
this.cut(index - 1, true);
|
|
985
|
+
return this;
|
|
986
|
+
}
|
|
987
|
+
/**
|
|
988
|
+
* Trim unused buckets to fit exactly the active range.
|
|
989
|
+
* @remarks Time O(N), Space O(1)
|
|
990
|
+
* @returns void
|
|
991
|
+
*/
|
|
992
|
+
shrinkToFit() {
|
|
993
|
+
if (this._length === 0) return;
|
|
994
|
+
const newBuckets = [];
|
|
995
|
+
if (this._bucketFirst === this._bucketLast) return;
|
|
996
|
+
else if (this._bucketFirst < this._bucketLast) {
|
|
997
|
+
for (let i = this._bucketFirst; i <= this._bucketLast; ++i) {
|
|
998
|
+
newBuckets.push(this._buckets[i]);
|
|
999
|
+
}
|
|
1000
|
+
} else {
|
|
1001
|
+
for (let i = this._bucketFirst; i < this._bucketCount; ++i) {
|
|
1002
|
+
newBuckets.push(this._buckets[i]);
|
|
1003
|
+
}
|
|
1004
|
+
for (let i = 0; i <= this._bucketLast; ++i) {
|
|
1005
|
+
newBuckets.push(this._buckets[i]);
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
this._bucketFirst = 0;
|
|
1009
|
+
this._bucketLast = newBuckets.length - 1;
|
|
1010
|
+
this._buckets = newBuckets;
|
|
1011
|
+
}
|
|
1012
|
+
/**
|
|
1013
|
+
* Deep clone this deque, preserving options.
|
|
1014
|
+
* @remarks Time O(N), Space O(N)
|
|
1015
|
+
* @returns A new deque with the same content and options.
|
|
1016
|
+
*/
|
|
1017
|
+
clone() {
|
|
1018
|
+
return this._createLike(this, {
|
|
1019
|
+
bucketSize: this.bucketSize,
|
|
1020
|
+
toElementFn: this.toElementFn,
|
|
1021
|
+
maxLen: this._maxLen
|
|
1022
|
+
});
|
|
1023
|
+
}
|
|
1024
|
+
/**
|
|
1025
|
+
* Filter elements into a new deque of the same class.
|
|
1026
|
+
* @remarks Time O(N), Space O(N)
|
|
1027
|
+
* @param predicate - Predicate (value, index, deque) → boolean to keep element.
|
|
1028
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
1029
|
+
* @returns A new deque with kept elements.
|
|
1030
|
+
*/
|
|
1031
|
+
filter(predicate, thisArg) {
|
|
1032
|
+
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
1033
|
+
out._setBucketSize(this._bucketSize);
|
|
1034
|
+
let index = 0;
|
|
1035
|
+
for (const el of this) {
|
|
1036
|
+
if (predicate.call(thisArg, el, index, this)) out.push(el);
|
|
1037
|
+
index++;
|
|
1038
|
+
}
|
|
1039
|
+
return out;
|
|
1040
|
+
}
|
|
1041
|
+
/**
|
|
1042
|
+
* Map elements into a new deque of the same element type.
|
|
1043
|
+
* @remarks Time O(N), Space O(N)
|
|
1044
|
+
* @param callback - Mapping function (value, index, deque) → newValue.
|
|
1045
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1046
|
+
* @returns A new deque with mapped values.
|
|
1047
|
+
*/
|
|
1048
|
+
mapSame(callback, thisArg) {
|
|
1049
|
+
const out = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
|
|
1050
|
+
out._setBucketSize(this._bucketSize);
|
|
1051
|
+
let index = 0;
|
|
1052
|
+
for (const v of this) {
|
|
1053
|
+
const mv = thisArg === void 0 ? callback(v, index++, this) : callback.call(thisArg, v, index++, this);
|
|
1054
|
+
out.push(mv);
|
|
1055
|
+
}
|
|
1056
|
+
return out;
|
|
1057
|
+
}
|
|
1058
|
+
/**
|
|
1059
|
+
* Map elements into a new deque (possibly different element type).
|
|
1060
|
+
* @remarks Time O(N), Space O(N)
|
|
1061
|
+
* @template EM
|
|
1062
|
+
* @template RM
|
|
1063
|
+
* @param callback - Mapping function (value, index, deque) → newElement.
|
|
1064
|
+
* @param [options] - Options for the output deque (e.g., bucketSize, toElementFn, maxLen).
|
|
1065
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1066
|
+
* @returns A new Deque with mapped elements.
|
|
1067
|
+
*/
|
|
1068
|
+
map(callback, options, thisArg) {
|
|
1069
|
+
const out = this._createLike([], {
|
|
1070
|
+
...options != null ? options : {},
|
|
1071
|
+
bucketSize: this._bucketSize,
|
|
1072
|
+
maxLen: this._maxLen
|
|
1073
|
+
});
|
|
1074
|
+
let index = 0;
|
|
1075
|
+
for (const el of this) {
|
|
1076
|
+
const mv = thisArg === void 0 ? callback(el, index, this) : callback.call(thisArg, el, index, this);
|
|
1077
|
+
out.push(mv);
|
|
1078
|
+
index++;
|
|
1079
|
+
}
|
|
1080
|
+
return out;
|
|
1081
|
+
}
|
|
1082
|
+
/**
|
|
1083
|
+
* (Protected) Set the internal bucket size.
|
|
1084
|
+
* @remarks Time O(1), Space O(1)
|
|
1085
|
+
* @param size - Bucket capacity to assign.
|
|
1086
|
+
* @returns void
|
|
1087
|
+
*/
|
|
1088
|
+
_setBucketSize(size) {
|
|
1089
|
+
this._bucketSize = size;
|
|
1090
|
+
}
|
|
1091
|
+
/**
|
|
1092
|
+
* (Protected) Iterate elements from front to back.
|
|
1093
|
+
* @remarks Time O(N), Space O(1)
|
|
1094
|
+
* @returns Iterator of elements.
|
|
1095
|
+
*/
|
|
1096
|
+
*_getIterator() {
|
|
1097
|
+
for (let i = 0; i < this._length; ++i) {
|
|
1098
|
+
const v = this.at(i);
|
|
1099
|
+
if (v !== void 0) yield v;
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
/**
|
|
1103
|
+
* (Protected) Reallocate buckets to make room near the ends.
|
|
1104
|
+
* @remarks Time O(N), Space O(N)
|
|
1105
|
+
* @param [needBucketNum] - How many extra buckets to add; defaults to half of current.
|
|
1106
|
+
* @returns void
|
|
1107
|
+
*/
|
|
1108
|
+
_reallocate(needBucketNum) {
|
|
1109
|
+
const newBuckets = [];
|
|
1110
|
+
const addBucketNum = needBucketNum || this._bucketCount >> 1 || 1;
|
|
1111
|
+
for (let i = 0; i < addBucketNum; ++i) {
|
|
1112
|
+
newBuckets[i] = new Array(this._bucketSize);
|
|
1113
|
+
}
|
|
1114
|
+
for (let i = this._bucketFirst; i < this._bucketCount; ++i) {
|
|
1115
|
+
newBuckets[newBuckets.length] = this._buckets[i];
|
|
1116
|
+
}
|
|
1117
|
+
for (let i = 0; i < this._bucketLast; ++i) {
|
|
1118
|
+
newBuckets[newBuckets.length] = this._buckets[i];
|
|
1119
|
+
}
|
|
1120
|
+
newBuckets[newBuckets.length] = [...this._buckets[this._bucketLast]];
|
|
1121
|
+
this._bucketFirst = addBucketNum;
|
|
1122
|
+
this._bucketLast = newBuckets.length - 1;
|
|
1123
|
+
for (let i = 0; i < addBucketNum; ++i) {
|
|
1124
|
+
newBuckets[newBuckets.length] = new Array(this._bucketSize);
|
|
1125
|
+
}
|
|
1126
|
+
this._buckets = newBuckets;
|
|
1127
|
+
this._bucketCount = newBuckets.length;
|
|
1128
|
+
}
|
|
1129
|
+
/**
|
|
1130
|
+
* (Protected) Translate a logical position to bucket/offset.
|
|
1131
|
+
* @remarks Time O(1), Space O(1)
|
|
1132
|
+
* @param pos - Zero-based position.
|
|
1133
|
+
* @returns An object containing bucketIndex and indexInBucket.
|
|
1134
|
+
*/
|
|
1135
|
+
_getBucketAndPosition(pos) {
|
|
1136
|
+
let bucketIndex;
|
|
1137
|
+
let indexInBucket;
|
|
1138
|
+
const overallIndex = this._firstInBucket + pos;
|
|
1139
|
+
bucketIndex = this._bucketFirst + Math.floor(overallIndex / this._bucketSize);
|
|
1140
|
+
if (bucketIndex >= this._bucketCount) {
|
|
1141
|
+
bucketIndex -= this._bucketCount;
|
|
1142
|
+
}
|
|
1143
|
+
indexInBucket = (overallIndex + 1) % this._bucketSize - 1;
|
|
1144
|
+
if (indexInBucket < 0) {
|
|
1145
|
+
indexInBucket = this._bucketSize - 1;
|
|
1146
|
+
}
|
|
1147
|
+
return { bucketIndex, indexInBucket };
|
|
1148
|
+
}
|
|
1149
|
+
/**
|
|
1150
|
+
* (Protected) Create an empty instance of the same concrete class.
|
|
1151
|
+
* @remarks Time O(1), Space O(1)
|
|
1152
|
+
* @param [options] - Options forwarded to the constructor.
|
|
1153
|
+
* @returns An empty like-kind deque instance.
|
|
1154
|
+
*/
|
|
1155
|
+
_createInstance(options) {
|
|
1156
|
+
const Ctor = this.constructor;
|
|
1157
|
+
return new Ctor([], options);
|
|
1158
|
+
}
|
|
1159
|
+
/**
|
|
1160
|
+
* (Protected) Create a like-kind deque seeded by elements.
|
|
1161
|
+
* @remarks Time O(N), Space O(N)
|
|
1162
|
+
* @template T
|
|
1163
|
+
* @template RR
|
|
1164
|
+
* @param [elements] - Iterable used to seed the new deque.
|
|
1165
|
+
* @param [options] - Options forwarded to the constructor.
|
|
1166
|
+
* @returns A like-kind Deque instance.
|
|
1167
|
+
*/
|
|
1168
|
+
_createLike(elements = [], options) {
|
|
1169
|
+
const Ctor = this.constructor;
|
|
1170
|
+
return new Ctor(elements, options);
|
|
1171
|
+
}
|
|
1172
|
+
/**
|
|
1173
|
+
* (Protected) Iterate elements from back to front.
|
|
1174
|
+
* @remarks Time O(N), Space O(1)
|
|
1175
|
+
* @returns Iterator of elements.
|
|
1176
|
+
*/
|
|
1177
|
+
*_getReverseIterator() {
|
|
1178
|
+
for (let i = this._length - 1; i > -1; i--) {
|
|
1179
|
+
const v = this.at(i);
|
|
1180
|
+
if (v !== void 0) yield v;
|
|
1181
|
+
}
|
|
1182
|
+
}
|
|
1183
|
+
};
|
|
1184
|
+
__name(_Deque, "Deque");
|
|
1185
|
+
var Deque = _Deque;
|
|
1186
|
+
|
|
1187
|
+
// src/common/index.ts
|
|
1188
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
1189
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
1190
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
1191
|
+
return DFSOperation2;
|
|
1192
|
+
})(DFSOperation || {});
|
|
1193
|
+
var _Range = class _Range {
|
|
1194
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
1195
|
+
this.low = low;
|
|
1196
|
+
this.high = high;
|
|
1197
|
+
this.includeLow = includeLow;
|
|
1198
|
+
this.includeHigh = includeHigh;
|
|
1199
|
+
if (!(isComparable(low) && isComparable(high))) throw new RangeError("low or high is not comparable");
|
|
1200
|
+
if (low > high) throw new RangeError("low must be less than or equal to high");
|
|
1201
|
+
}
|
|
1202
|
+
// Determine whether a key is within the range
|
|
1203
|
+
isInRange(key, comparator) {
|
|
1204
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
1205
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
1206
|
+
return lowCheck && highCheck;
|
|
1207
|
+
}
|
|
1208
|
+
};
|
|
1209
|
+
__name(_Range, "Range");
|
|
1210
|
+
var Range = _Range;
|
|
1211
|
+
/**
|
|
1212
|
+
* data-structure-typed
|
|
1213
|
+
*
|
|
1214
|
+
* @author Pablo Zeng
|
|
1215
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
1216
|
+
* @license MIT License
|
|
1217
|
+
*/
|
|
1218
|
+
|
|
1219
|
+
exports.DFSOperation = DFSOperation;
|
|
1220
|
+
exports.Deque = Deque;
|
|
1221
|
+
exports.Range = Range;
|
|
1222
|
+
//# sourceMappingURL=index.cjs.map
|
|
1223
|
+
//# sourceMappingURL=index.cjs.map
|