data-structure-typed 2.0.5 → 2.1.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/CHANGELOG.md +1 -1
- package/COMMANDS.md +17 -0
- package/benchmark/report.html +13 -77
- package/benchmark/report.json +145 -177
- package/dist/cjs/data-structures/base/iterable-element-base.d.ts +186 -83
- package/dist/cjs/data-structures/base/iterable-element-base.js +149 -107
- package/dist/cjs/data-structures/base/iterable-element-base.js.map +1 -1
- package/dist/cjs/data-structures/base/iterable-entry-base.d.ts +95 -119
- package/dist/cjs/data-structures/base/iterable-entry-base.js +59 -116
- package/dist/cjs/data-structures/base/iterable-entry-base.js.map +1 -1
- package/dist/cjs/data-structures/base/linear-base.d.ts +250 -192
- package/dist/cjs/data-structures/base/linear-base.js +137 -274
- package/dist/cjs/data-structures/base/linear-base.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
- package/dist/cjs/data-structures/binary-tree/avl-tree-counter.js +171 -205
- package/dist/cjs/data-structures/binary-tree/avl-tree-counter.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +135 -87
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +138 -149
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +208 -195
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +476 -632
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +594 -865
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +258 -306
- package/dist/cjs/data-structures/binary-tree/bst.js +505 -481
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/red-black-tree.d.ts +107 -179
- package/dist/cjs/data-structures/binary-tree/red-black-tree.js +114 -209
- package/dist/cjs/data-structures/binary-tree/red-black-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-counter.d.ts +132 -154
- package/dist/cjs/data-structures/binary-tree/tree-counter.js +172 -203
- package/dist/cjs/data-structures/binary-tree/tree-counter.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +105 -85
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +238 -233
- package/dist/cjs/data-structures/graph/abstract-graph.js +267 -237
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +108 -224
- package/dist/cjs/data-structures/graph/directed-graph.js +146 -233
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/map-graph.d.ts +49 -55
- package/dist/cjs/data-structures/graph/map-graph.js +56 -59
- package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +103 -146
- package/dist/cjs/data-structures/graph/undirected-graph.js +129 -149
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +164 -338
- package/dist/cjs/data-structures/hash/hash-map.js +270 -457
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +214 -289
- package/dist/cjs/data-structures/heap/heap.js +340 -349
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.d.ts +11 -47
- package/dist/cjs/data-structures/heap/max-heap.js +11 -66
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.d.ts +12 -47
- package/dist/cjs/data-structures/heap/min-heap.js +11 -66
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +368 -494
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +261 -310
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +447 -466
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +0 -107
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js +0 -100
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +11 -78
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +10 -79
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +2 -61
- package/dist/cjs/data-structures/priority-queue/priority-queue.js +8 -83
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +227 -254
- package/dist/cjs/data-structures/queue/deque.js +309 -348
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +180 -201
- package/dist/cjs/data-structures/queue/queue.js +265 -248
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +124 -102
- package/dist/cjs/data-structures/stack/stack.js +181 -125
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +164 -165
- package/dist/cjs/data-structures/trie/trie.js +189 -172
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +56 -6
- package/dist/cjs/interfaces/graph.d.ts +16 -0
- package/dist/cjs/types/data-structures/base/base.d.ts +1 -1
- package/dist/cjs/types/data-structures/graph/abstract-graph.d.ts +4 -0
- package/dist/cjs/types/utils/utils.d.ts +1 -0
- package/dist/cjs/utils/utils.d.ts +1 -1
- package/dist/cjs/utils/utils.js +2 -1
- package/dist/cjs/utils/utils.js.map +1 -1
- package/dist/esm/data-structures/base/iterable-element-base.d.ts +186 -83
- package/dist/esm/data-structures/base/iterable-element-base.js +155 -107
- package/dist/esm/data-structures/base/iterable-element-base.js.map +1 -1
- package/dist/esm/data-structures/base/iterable-entry-base.d.ts +95 -119
- package/dist/esm/data-structures/base/iterable-entry-base.js +59 -116
- package/dist/esm/data-structures/base/iterable-entry-base.js.map +1 -1
- package/dist/esm/data-structures/base/linear-base.d.ts +250 -192
- package/dist/esm/data-structures/base/linear-base.js +137 -274
- package/dist/esm/data-structures/base/linear-base.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
- package/dist/esm/data-structures/binary-tree/avl-tree-counter.js +171 -212
- package/dist/esm/data-structures/binary-tree/avl-tree-counter.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
- package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js +133 -94
- package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/avl-tree.d.ts +138 -149
- package/dist/esm/data-structures/binary-tree/avl-tree.js +206 -200
- package/dist/esm/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/binary-tree.d.ts +476 -632
- package/dist/esm/data-structures/binary-tree/binary-tree.js +598 -874
- package/dist/esm/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/bst.d.ts +258 -306
- package/dist/esm/data-structures/binary-tree/bst.js +507 -487
- package/dist/esm/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/red-black-tree.d.ts +107 -179
- package/dist/esm/data-structures/binary-tree/red-black-tree.js +114 -215
- package/dist/esm/data-structures/binary-tree/red-black-tree.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/tree-counter.d.ts +132 -154
- package/dist/esm/data-structures/binary-tree/tree-counter.js +175 -209
- package/dist/esm/data-structures/binary-tree/tree-counter.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
- package/dist/esm/data-structures/binary-tree/tree-multi-map.js +103 -92
- package/dist/esm/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/esm/data-structures/graph/abstract-graph.d.ts +238 -233
- package/dist/esm/data-structures/graph/abstract-graph.js +267 -237
- package/dist/esm/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/esm/data-structures/graph/directed-graph.d.ts +108 -224
- package/dist/esm/data-structures/graph/directed-graph.js +145 -233
- package/dist/esm/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/esm/data-structures/graph/map-graph.d.ts +49 -55
- package/dist/esm/data-structures/graph/map-graph.js +56 -59
- package/dist/esm/data-structures/graph/map-graph.js.map +1 -1
- package/dist/esm/data-structures/graph/undirected-graph.d.ts +103 -146
- package/dist/esm/data-structures/graph/undirected-graph.js +128 -149
- package/dist/esm/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/esm/data-structures/hash/hash-map.d.ts +164 -338
- package/dist/esm/data-structures/hash/hash-map.js +270 -457
- package/dist/esm/data-structures/hash/hash-map.js.map +1 -1
- package/dist/esm/data-structures/heap/heap.d.ts +214 -289
- package/dist/esm/data-structures/heap/heap.js +329 -349
- package/dist/esm/data-structures/heap/heap.js.map +1 -1
- package/dist/esm/data-structures/heap/max-heap.d.ts +11 -47
- package/dist/esm/data-structures/heap/max-heap.js +11 -66
- package/dist/esm/data-structures/heap/max-heap.js.map +1 -1
- package/dist/esm/data-structures/heap/min-heap.d.ts +12 -47
- package/dist/esm/data-structures/heap/min-heap.js +11 -66
- package/dist/esm/data-structures/heap/min-heap.js.map +1 -1
- package/dist/esm/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
- package/dist/esm/data-structures/linked-list/doubly-linked-list.js +368 -495
- package/dist/esm/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/esm/data-structures/linked-list/singly-linked-list.d.ts +261 -310
- package/dist/esm/data-structures/linked-list/singly-linked-list.js +448 -467
- package/dist/esm/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/esm/data-structures/linked-list/skip-linked-list.d.ts +0 -107
- package/dist/esm/data-structures/linked-list/skip-linked-list.js +0 -100
- package/dist/esm/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/esm/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
- package/dist/esm/data-structures/priority-queue/max-priority-queue.js +11 -78
- package/dist/esm/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/esm/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
- package/dist/esm/data-structures/priority-queue/min-priority-queue.js +10 -79
- package/dist/esm/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/esm/data-structures/priority-queue/priority-queue.d.ts +2 -61
- package/dist/esm/data-structures/priority-queue/priority-queue.js +8 -83
- package/dist/esm/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/esm/data-structures/queue/deque.d.ts +227 -254
- package/dist/esm/data-structures/queue/deque.js +313 -348
- package/dist/esm/data-structures/queue/deque.js.map +1 -1
- package/dist/esm/data-structures/queue/queue.d.ts +180 -201
- package/dist/esm/data-structures/queue/queue.js +263 -248
- package/dist/esm/data-structures/queue/queue.js.map +1 -1
- package/dist/esm/data-structures/stack/stack.d.ts +124 -102
- package/dist/esm/data-structures/stack/stack.js +181 -125
- package/dist/esm/data-structures/stack/stack.js.map +1 -1
- package/dist/esm/data-structures/trie/trie.d.ts +164 -165
- package/dist/esm/data-structures/trie/trie.js +193 -172
- package/dist/esm/data-structures/trie/trie.js.map +1 -1
- package/dist/esm/interfaces/binary-tree.d.ts +56 -6
- package/dist/esm/interfaces/graph.d.ts +16 -0
- package/dist/esm/types/data-structures/base/base.d.ts +1 -1
- package/dist/esm/types/data-structures/graph/abstract-graph.d.ts +4 -0
- package/dist/esm/types/utils/utils.d.ts +1 -0
- package/dist/esm/utils/utils.d.ts +1 -1
- package/dist/esm/utils/utils.js +2 -1
- package/dist/esm/utils/utils.js.map +1 -1
- package/dist/umd/data-structure-typed.js +4685 -6477
- package/dist/umd/data-structure-typed.min.js +8 -6
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +3 -4
- package/src/data-structures/base/iterable-element-base.ts +238 -115
- package/src/data-structures/base/iterable-entry-base.ts +96 -120
- package/src/data-structures/base/linear-base.ts +271 -277
- package/src/data-structures/binary-tree/avl-tree-counter.ts +198 -216
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +192 -101
- package/src/data-structures/binary-tree/avl-tree.ts +239 -206
- package/src/data-structures/binary-tree/binary-tree.ts +660 -889
- package/src/data-structures/binary-tree/bst.ts +568 -570
- package/src/data-structures/binary-tree/red-black-tree.ts +161 -222
- package/src/data-structures/binary-tree/tree-counter.ts +199 -218
- package/src/data-structures/binary-tree/tree-multi-map.ts +131 -97
- package/src/data-structures/graph/abstract-graph.ts +339 -264
- package/src/data-structures/graph/directed-graph.ts +146 -236
- package/src/data-structures/graph/map-graph.ts +63 -60
- package/src/data-structures/graph/undirected-graph.ts +129 -152
- package/src/data-structures/hash/hash-map.ts +274 -496
- package/src/data-structures/heap/heap.ts +389 -402
- package/src/data-structures/heap/max-heap.ts +12 -76
- package/src/data-structures/heap/min-heap.ts +13 -76
- package/src/data-structures/linked-list/doubly-linked-list.ts +426 -530
- package/src/data-structures/linked-list/singly-linked-list.ts +495 -517
- package/src/data-structures/linked-list/skip-linked-list.ts +1 -108
- package/src/data-structures/priority-queue/max-priority-queue.ts +12 -87
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -88
- package/src/data-structures/priority-queue/priority-queue.ts +3 -92
- package/src/data-structures/queue/deque.ts +381 -357
- package/src/data-structures/queue/queue.ts +310 -264
- package/src/data-structures/stack/stack.ts +217 -131
- package/src/data-structures/trie/trie.ts +240 -175
- package/src/interfaces/binary-tree.ts +240 -6
- package/src/interfaces/graph.ts +37 -0
- package/src/types/data-structures/base/base.ts +5 -5
- package/src/types/data-structures/graph/abstract-graph.ts +5 -0
- package/src/types/utils/utils.ts +2 -0
- package/src/utils/utils.ts +9 -14
- package/test/integration/index.html +1 -1
- package/test/performance/benchmark-runner.ts +528 -0
- package/test/performance/reportor.mjs +43 -43
- package/test/performance/runner-config.json +39 -0
- package/test/performance/single-suite-runner.ts +69 -0
- package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +5 -5
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +4 -4
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +350 -90
- package/test/unit/data-structures/binary-tree/bst.test.ts +12 -9
- package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/tree-counter.test.ts +25 -24
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +3 -3
- package/test/unit/data-structures/graph/abstract-graph.test.ts +0 -4
- package/test/unit/data-structures/graph/directed-graph.test.ts +1 -1
- package/test/unit/data-structures/heap/heap.test.ts +14 -21
- package/test/unit/data-structures/heap/max-heap.test.ts +5 -9
- package/test/unit/data-structures/heap/min-heap.test.ts +1 -4
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +14 -14
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +0 -7
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +8 -11
- package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +1 -4
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +1 -4
- package/test/unit/data-structures/queue/queue.test.ts +4 -5
- package/test/unit/utils/utils.test.ts +0 -1
- package/test/performance/data-structures/binary-tree/avl-tree.test.mjs +0 -71
- package/test/performance/data-structures/binary-tree/red-black-tree.test.mjs +0 -81
|
@@ -1,6 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
1
8
|
import { SinglyLinkedList } from '../linked-list';
|
|
2
9
|
import { LinearBase } from '../base/linear-base';
|
|
3
10
|
/**
|
|
11
|
+
* Array-backed queue with amortized O(1) enqueue/dequeue via an offset pointer and optional auto-compaction.
|
|
12
|
+
* @remarks Time O(1), Space O(1)
|
|
13
|
+
* @template E
|
|
14
|
+
* @template R
|
|
4
15
|
* 1. First In, First Out (FIFO): The core feature of a queue is its first in, first out nature. The element added to the queue first will be the one to be removed first.
|
|
5
16
|
* 2. Operations: The main operations include enqueue (adding an element to the end of the queue) and dequeue (removing and returning the element at the front of the queue). Typically, there is also a peek operation (looking at the front element without removing it).
|
|
6
17
|
* 3. Uses: Queues are commonly used to manage a series of tasks or elements that need to be processed in order. For example, managing task queues in a multi-threaded environment, or in algorithms for data structures like trees and graphs for breadth-first search.
|
|
@@ -17,7 +28,7 @@ import { LinearBase } from '../base/linear-base';
|
|
|
17
28
|
* let maxSum = 0;
|
|
18
29
|
* let currentSum = 0;
|
|
19
30
|
*
|
|
20
|
-
* nums.forEach(
|
|
31
|
+
* nums.forEach(num => {
|
|
21
32
|
* queue.push(num);
|
|
22
33
|
* currentSum += num;
|
|
23
34
|
*
|
|
@@ -57,6 +68,13 @@ import { LinearBase } from '../base/linear-base';
|
|
|
57
68
|
* console.log(visited); // [1, 2, 3, 4, 5]
|
|
58
69
|
*/
|
|
59
70
|
export class Queue extends LinearBase {
|
|
71
|
+
/**
|
|
72
|
+
* Create a Queue and optionally bulk-insert elements.
|
|
73
|
+
* @remarks Time O(N), Space O(N)
|
|
74
|
+
* @param [elements] - Iterable of elements (or raw records if toElementFn is set).
|
|
75
|
+
* @param [options] - Options such as toElementFn, maxLen, and autoCompactRatio.
|
|
76
|
+
* @returns New Queue instance.
|
|
77
|
+
*/
|
|
60
78
|
constructor(elements = [], options) {
|
|
61
79
|
super(options);
|
|
62
80
|
if (options) {
|
|
@@ -66,65 +84,88 @@ export class Queue extends LinearBase {
|
|
|
66
84
|
this.pushMany(elements);
|
|
67
85
|
}
|
|
68
86
|
_elements = [];
|
|
87
|
+
/**
|
|
88
|
+
* Get the underlying array buffer.
|
|
89
|
+
* @remarks Time O(1), Space O(1)
|
|
90
|
+
* @returns Backing array of elements.
|
|
91
|
+
*/
|
|
69
92
|
get elements() {
|
|
70
93
|
return this._elements;
|
|
71
94
|
}
|
|
72
95
|
_offset = 0;
|
|
96
|
+
/**
|
|
97
|
+
* Get the current start offset into the array.
|
|
98
|
+
* @remarks Time O(1), Space O(1)
|
|
99
|
+
* @returns Zero-based offset.
|
|
100
|
+
*/
|
|
73
101
|
get offset() {
|
|
74
102
|
return this._offset;
|
|
75
103
|
}
|
|
76
|
-
get length() {
|
|
77
|
-
return this.elements.length - this.offset;
|
|
78
|
-
}
|
|
79
104
|
_autoCompactRatio = 0.5;
|
|
105
|
+
/**
|
|
106
|
+
* Get the compaction threshold (offset/size).
|
|
107
|
+
* @remarks Time O(1), Space O(1)
|
|
108
|
+
* @returns Auto-compaction ratio in (0,1].
|
|
109
|
+
*/
|
|
80
110
|
get autoCompactRatio() {
|
|
81
111
|
return this._autoCompactRatio;
|
|
82
112
|
}
|
|
83
|
-
|
|
84
|
-
|
|
113
|
+
/**
|
|
114
|
+
* Set the compaction threshold.
|
|
115
|
+
* @remarks Time O(1), Space O(1)
|
|
116
|
+
* @param value - New ratio; compacts when offset/size exceeds this value.
|
|
117
|
+
* @returns void
|
|
118
|
+
*/
|
|
119
|
+
set autoCompactRatio(value) {
|
|
120
|
+
this._autoCompactRatio = value;
|
|
85
121
|
}
|
|
86
122
|
/**
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
123
|
+
* Get the number of elements currently in the queue.
|
|
124
|
+
* @remarks Time O(1), Space O(1)
|
|
125
|
+
* @returns Current length.
|
|
126
|
+
*/
|
|
127
|
+
get length() {
|
|
128
|
+
return this.elements.length - this._offset;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Get the first element (front) without removing it.
|
|
132
|
+
* @remarks Time O(1), Space O(1)
|
|
133
|
+
* @returns Front element or undefined.
|
|
93
134
|
*/
|
|
94
135
|
get first() {
|
|
95
|
-
return this.length > 0 ? this.elements[this.
|
|
136
|
+
return this.length > 0 ? this.elements[this._offset] : undefined;
|
|
96
137
|
}
|
|
97
138
|
/**
|
|
98
|
-
*
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
-
* The `last` function returns the last element in an array-like data structure, or undefined if the structure is empty.
|
|
102
|
-
* @returns The method `get last()` returns the last element of the `_elements` array if the array is not empty. If the
|
|
103
|
-
* array is empty, it returns `undefined`.
|
|
139
|
+
* Get the last element (back) without removing it.
|
|
140
|
+
* @remarks Time O(1), Space O(1)
|
|
141
|
+
* @returns Back element or undefined.
|
|
104
142
|
*/
|
|
105
143
|
get last() {
|
|
106
144
|
return this.length > 0 ? this.elements[this.elements.length - 1] : undefined;
|
|
107
145
|
}
|
|
108
146
|
/**
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
*
|
|
112
|
-
*
|
|
113
|
-
* @
|
|
114
|
-
* @param {E[]} elements - The "elements" parameter is an array of elements of type E.
|
|
115
|
-
* @returns The method is returning a new instance of the Queue class, initialized with the elements from the input
|
|
116
|
-
* array.
|
|
147
|
+
* Create a queue from an array of elements.
|
|
148
|
+
* @remarks Time O(N), Space O(N)
|
|
149
|
+
* @template E
|
|
150
|
+
* @param elements - Array of elements to enqueue in order.
|
|
151
|
+
* @returns A new queue populated from the array.
|
|
117
152
|
*/
|
|
118
153
|
static fromArray(elements) {
|
|
119
154
|
return new Queue(elements);
|
|
120
155
|
}
|
|
121
156
|
/**
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
*
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
157
|
+
* Check whether the queue is empty.
|
|
158
|
+
* @remarks Time O(1), Space O(1)
|
|
159
|
+
* @returns True if length is 0.
|
|
160
|
+
*/
|
|
161
|
+
isEmpty() {
|
|
162
|
+
return this.length === 0;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Enqueue one element at the back.
|
|
166
|
+
* @remarks Time O(1), Space O(1)
|
|
167
|
+
* @param element - Element to enqueue.
|
|
168
|
+
* @returns True on success.
|
|
128
169
|
*/
|
|
129
170
|
push(element) {
|
|
130
171
|
this.elements.push(element);
|
|
@@ -133,15 +174,10 @@ export class Queue extends LinearBase {
|
|
|
133
174
|
return true;
|
|
134
175
|
}
|
|
135
176
|
/**
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
*
|
|
140
|
-
* transformation function if provided.
|
|
141
|
-
* @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the `pushMany` function
|
|
142
|
-
* is an iterable containing elements of type `E` or `R`.
|
|
143
|
-
* @returns The `pushMany` function is returning an array of boolean values indicating whether each
|
|
144
|
-
* element was successfully pushed into the data structure.
|
|
177
|
+
* Enqueue many elements from an iterable.
|
|
178
|
+
* @remarks Time O(N), Space O(1)
|
|
179
|
+
* @param elements - Iterable of elements (or raw records if toElementFn is set).
|
|
180
|
+
* @returns Array of per-element success flags.
|
|
145
181
|
*/
|
|
146
182
|
pushMany(elements) {
|
|
147
183
|
const ans = [];
|
|
@@ -154,299 +190,278 @@ export class Queue extends LinearBase {
|
|
|
154
190
|
return ans;
|
|
155
191
|
}
|
|
156
192
|
/**
|
|
157
|
-
*
|
|
158
|
-
*
|
|
159
|
-
*
|
|
160
|
-
* The `shift` function removes and returns the first element in the queue, and adjusts the internal data structure if
|
|
161
|
-
* necessary to optimize performance.
|
|
162
|
-
* @returns The function `shift()` returns either the first element in the queue or `undefined` if the queue is empty.
|
|
193
|
+
* Dequeue one element from the front (amortized via offset).
|
|
194
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
195
|
+
* @returns Removed element or undefined.
|
|
163
196
|
*/
|
|
164
197
|
shift() {
|
|
165
198
|
if (this.length === 0)
|
|
166
199
|
return undefined;
|
|
167
200
|
const first = this.first;
|
|
168
201
|
this._offset += 1;
|
|
169
|
-
if (this.offset / this.elements.length > this.autoCompactRatio)
|
|
202
|
+
if (this.elements.length > 0 && this.offset / this.elements.length > this.autoCompactRatio)
|
|
170
203
|
this.compact();
|
|
171
204
|
return first;
|
|
172
205
|
}
|
|
173
206
|
/**
|
|
174
|
-
*
|
|
175
|
-
*
|
|
176
|
-
*
|
|
177
|
-
*
|
|
178
|
-
* @param {E} element - Specify the element to be deleted
|
|
179
|
-
* @return A boolean value indicating whether the element was successfully deleted or not
|
|
207
|
+
* Delete the first occurrence of a specific element.
|
|
208
|
+
* @remarks Time O(N), Space O(1)
|
|
209
|
+
* @param element - Element to remove (strict equality via Object.is).
|
|
210
|
+
* @returns True if an element was removed.
|
|
180
211
|
*/
|
|
181
212
|
delete(element) {
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
* The deleteAt function deletes the element at a given index.
|
|
190
|
-
* @param {number} index - Determine the index of the element to be deleted
|
|
191
|
-
* @return A boolean value
|
|
192
|
-
*/
|
|
193
|
-
deleteAt(index) {
|
|
194
|
-
const deleted = this.elements[index];
|
|
195
|
-
this.elements.splice(index, 1);
|
|
196
|
-
return deleted;
|
|
213
|
+
for (let i = this._offset; i < this.elements.length; i++) {
|
|
214
|
+
if (Object.is(this.elements[i], element)) {
|
|
215
|
+
this.elements.splice(i, 1);
|
|
216
|
+
return true;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return false;
|
|
197
220
|
}
|
|
198
221
|
/**
|
|
199
|
-
*
|
|
200
|
-
*
|
|
201
|
-
*
|
|
202
|
-
*
|
|
203
|
-
* if the index is out of bounds.
|
|
204
|
-
* @param {number} index - The `index` parameter represents the position of the element you want to
|
|
205
|
-
* retrieve from the data structure.
|
|
206
|
-
* @returns The `at` method is returning the element at the specified index adjusted by the offset
|
|
207
|
-
* `_offset`.
|
|
222
|
+
* Get the element at a given logical index.
|
|
223
|
+
* @remarks Time O(1), Space O(1)
|
|
224
|
+
* @param index - Zero-based index from the front.
|
|
225
|
+
* @returns Element or undefined.
|
|
208
226
|
*/
|
|
209
227
|
at(index) {
|
|
210
|
-
|
|
228
|
+
if (index < 0 || index >= this.length)
|
|
229
|
+
return undefined;
|
|
230
|
+
return this._elements[this._offset + index];
|
|
211
231
|
}
|
|
212
232
|
/**
|
|
213
|
-
*
|
|
214
|
-
*
|
|
215
|
-
*
|
|
216
|
-
*
|
|
217
|
-
* offset.
|
|
218
|
-
* @returns The `reverse()` method is returning the modified object itself (`this`) after reversing
|
|
219
|
-
* the elements in the array and resetting the offset to 0.
|
|
233
|
+
* Delete the element at a given index.
|
|
234
|
+
* @remarks Time O(N), Space O(1)
|
|
235
|
+
* @param index - Zero-based index from the front.
|
|
236
|
+
* @returns Removed element or undefined.
|
|
220
237
|
*/
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
238
|
+
deleteAt(index) {
|
|
239
|
+
if (index < 0 || index >= this.length)
|
|
240
|
+
return undefined;
|
|
241
|
+
const gi = this._offset + index;
|
|
242
|
+
const [deleted] = this.elements.splice(gi, 1);
|
|
243
|
+
return deleted;
|
|
225
244
|
}
|
|
226
245
|
/**
|
|
227
|
-
*
|
|
228
|
-
*
|
|
229
|
-
*
|
|
230
|
-
*
|
|
231
|
-
*
|
|
232
|
-
* @param {number} index - The `index` parameter represents the position at which the `newElement`
|
|
233
|
-
* should be added in the array.
|
|
234
|
-
* @param {E} newElement - The `newElement` parameter represents the element that you want to insert
|
|
235
|
-
* into the array at the specified index.
|
|
236
|
-
* @returns The `addAt` method returns a boolean value - `true` if the new element was successfully
|
|
237
|
-
* added at the specified index, and `false` if the index is out of bounds (less than 0 or greater
|
|
238
|
-
* than the length of the array).
|
|
246
|
+
* Insert a new element at a given index.
|
|
247
|
+
* @remarks Time O(N), Space O(1)
|
|
248
|
+
* @param index - Zero-based index from the front.
|
|
249
|
+
* @param newElement - Element to insert.
|
|
250
|
+
* @returns True if inserted.
|
|
239
251
|
*/
|
|
240
252
|
addAt(index, newElement) {
|
|
241
253
|
if (index < 0 || index > this.length)
|
|
242
254
|
return false;
|
|
243
|
-
this._elements.splice(this.
|
|
255
|
+
this._elements.splice(this._offset + index, 0, newElement);
|
|
244
256
|
return true;
|
|
245
257
|
}
|
|
246
258
|
/**
|
|
247
|
-
*
|
|
248
|
-
*
|
|
249
|
-
*
|
|
250
|
-
*
|
|
251
|
-
* @
|
|
252
|
-
* array where the new element will be set.
|
|
253
|
-
* @param {E} newElement - The `newElement` parameter represents the new value that you want to set
|
|
254
|
-
* at the specified index in the array.
|
|
255
|
-
* @returns The `setAt` method returns a boolean value - `true` if the element was successfully set
|
|
256
|
-
* at the specified index, and `false` if the index is out of bounds (less than 0 or greater than the
|
|
257
|
-
* length of the array).
|
|
259
|
+
* Replace the element at a given index.
|
|
260
|
+
* @remarks Time O(1), Space O(1)
|
|
261
|
+
* @param index - Zero-based index from the front.
|
|
262
|
+
* @param newElement - New element to set.
|
|
263
|
+
* @returns True if updated.
|
|
258
264
|
*/
|
|
259
265
|
setAt(index, newElement) {
|
|
260
|
-
if (index < 0 || index
|
|
266
|
+
if (index < 0 || index >= this.length)
|
|
261
267
|
return false;
|
|
262
|
-
this._elements[this.
|
|
268
|
+
this._elements[this._offset + index] = newElement;
|
|
263
269
|
return true;
|
|
264
270
|
}
|
|
265
271
|
/**
|
|
266
|
-
*
|
|
267
|
-
*
|
|
268
|
-
*
|
|
269
|
-
* The function checks if a data structure is empty by comparing its length to zero.
|
|
270
|
-
* @returns {boolean} A boolean value indicating whether the length of the object is 0 or not.
|
|
272
|
+
* Reverse the queue in-place by compacting then reversing.
|
|
273
|
+
* @remarks Time O(N), Space O(N)
|
|
274
|
+
* @returns This queue.
|
|
271
275
|
*/
|
|
272
|
-
|
|
273
|
-
|
|
276
|
+
reverse() {
|
|
277
|
+
this._elements = this.elements.slice(this._offset).reverse();
|
|
278
|
+
this._offset = 0;
|
|
279
|
+
return this;
|
|
274
280
|
}
|
|
275
281
|
/**
|
|
276
|
-
*
|
|
277
|
-
*
|
|
278
|
-
*
|
|
279
|
-
* The clear function resets the elements array and offset to their initial values.
|
|
282
|
+
* Remove all elements and reset offset.
|
|
283
|
+
* @remarks Time O(1), Space O(1)
|
|
284
|
+
* @returns void
|
|
280
285
|
*/
|
|
281
286
|
clear() {
|
|
282
287
|
this._elements = [];
|
|
283
288
|
this._offset = 0;
|
|
284
289
|
}
|
|
285
290
|
/**
|
|
286
|
-
*
|
|
287
|
-
*
|
|
288
|
-
*
|
|
289
|
-
* The `compact` function in TypeScript slices the elements array based on the offset and resets the
|
|
290
|
-
* offset to zero.
|
|
291
|
-
* @returns The `compact()` method is returning a boolean value of `true`.
|
|
291
|
+
* Compact storage by discarding consumed head elements.
|
|
292
|
+
* @remarks Time O(N), Space O(N)
|
|
293
|
+
* @returns True when compaction performed.
|
|
292
294
|
*/
|
|
293
295
|
compact() {
|
|
294
|
-
this._elements = this.elements.slice(this.
|
|
296
|
+
this._elements = this.elements.slice(this._offset);
|
|
295
297
|
this._offset = 0;
|
|
296
298
|
return true;
|
|
297
299
|
}
|
|
298
300
|
/**
|
|
299
|
-
*
|
|
300
|
-
*
|
|
301
|
-
*
|
|
302
|
-
*
|
|
303
|
-
*
|
|
304
|
-
* @
|
|
305
|
-
* to start changing the array. Items will be added or removed starting from this index.
|
|
306
|
-
* @param {number} [deleteCount=0] - The `deleteCount` parameter in the `splice` method specifies the
|
|
307
|
-
* number of elements to remove from the array starting at the specified `start` index. If
|
|
308
|
-
* `deleteCount` is not provided, it defaults to 0, meaning no elements will be removed but new
|
|
309
|
-
* elements can still be inserted at
|
|
310
|
-
* @param {E[]} items - The `items` parameter in the `splice` method represents the elements that
|
|
311
|
-
* will be added to the array at the specified `start` index. These elements will replace the
|
|
312
|
-
* existing elements starting from the `start` index for the `deleteCount` number of elements.
|
|
313
|
-
* @returns The `splice` method is returning the `removedQueue`, which is an instance of the same
|
|
314
|
-
* class as the original object.
|
|
301
|
+
* Remove and/or insert elements at a position (array-like).
|
|
302
|
+
* @remarks Time O(N + M), Space O(M)
|
|
303
|
+
* @param start - Start index (clamped to [0, length]).
|
|
304
|
+
* @param [deleteCount] - Number of elements to remove (default 0).
|
|
305
|
+
* @param [items] - Elements to insert after `start`.
|
|
306
|
+
* @returns A new queue containing the removed elements (typed as `this`).
|
|
315
307
|
*/
|
|
316
308
|
splice(start, deleteCount = 0, ...items) {
|
|
317
|
-
const removedQueue = this._createInstance();
|
|
318
309
|
start = Math.max(0, Math.min(start, this.length));
|
|
319
310
|
deleteCount = Math.max(0, Math.min(deleteCount, this.length - start));
|
|
320
|
-
const
|
|
321
|
-
const
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
311
|
+
const gi = this._offset + start;
|
|
312
|
+
const removedArray = this._elements.splice(gi, deleteCount, ...items);
|
|
313
|
+
if (this.elements.length > 0 && this.offset / this.elements.length > this.autoCompactRatio)
|
|
314
|
+
this.compact();
|
|
315
|
+
const removed = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
316
|
+
removed._setAutoCompactRatio(this._autoCompactRatio);
|
|
317
|
+
removed.pushMany(removedArray);
|
|
318
|
+
return removed;
|
|
325
319
|
}
|
|
326
320
|
/**
|
|
327
|
-
*
|
|
328
|
-
*
|
|
329
|
-
*
|
|
330
|
-
* The `clone()` function returns a new Queue object with the same elements as the original Queue.
|
|
331
|
-
* @returns The `clone()` method is returning a new instance of the `Queue` class.
|
|
321
|
+
* Deep clone this queue and its parameters.
|
|
322
|
+
* @remarks Time O(N), Space O(N)
|
|
323
|
+
* @returns A new queue with the same content and options.
|
|
332
324
|
*/
|
|
333
325
|
clone() {
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
*
|
|
342
|
-
* @
|
|
343
|
-
*
|
|
344
|
-
*
|
|
345
|
-
* queue
|
|
346
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
347
|
-
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
348
|
-
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
349
|
-
* @returns The `filter` method is returning a new `Queue` object that contains the elements that
|
|
350
|
-
* satisfy the given predicate function.
|
|
326
|
+
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
327
|
+
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
328
|
+
for (let i = this._offset; i < this.elements.length; i++)
|
|
329
|
+
out.push(this.elements[i]);
|
|
330
|
+
return out;
|
|
331
|
+
}
|
|
332
|
+
/**
|
|
333
|
+
* Filter elements into a new queue of the same class.
|
|
334
|
+
* @remarks Time O(N), Space O(N)
|
|
335
|
+
* @param predicate - Predicate (element, index, queue) → boolean to keep element.
|
|
336
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
337
|
+
* @returns A new queue with kept elements.
|
|
351
338
|
*/
|
|
352
339
|
filter(predicate, thisArg) {
|
|
353
|
-
const
|
|
354
|
-
|
|
355
|
-
autoCompactRatio: this._autoCompactRatio,
|
|
356
|
-
maxLen: this._maxLen
|
|
357
|
-
});
|
|
340
|
+
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
341
|
+
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
358
342
|
let index = 0;
|
|
359
|
-
for (const
|
|
360
|
-
if (predicate.call(thisArg,
|
|
361
|
-
|
|
362
|
-
}
|
|
343
|
+
for (const v of this) {
|
|
344
|
+
if (predicate.call(thisArg, v, index, this))
|
|
345
|
+
out.push(v);
|
|
363
346
|
index++;
|
|
364
347
|
}
|
|
365
|
-
return
|
|
366
|
-
}
|
|
367
|
-
/**
|
|
368
|
-
*
|
|
369
|
-
*
|
|
370
|
-
*
|
|
371
|
-
*
|
|
372
|
-
* element
|
|
373
|
-
* @param
|
|
374
|
-
*
|
|
375
|
-
*
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
* @returns A new Queue object containing elements of type EM, which are the result of applying the
|
|
383
|
-
* callback function to each element in the original Queue object.
|
|
384
|
-
*/
|
|
385
|
-
map(callback, toElementFn, thisArg) {
|
|
386
|
-
const newDeque = new Queue([], {
|
|
387
|
-
toElementFn,
|
|
388
|
-
autoCompactRatio: this._autoCompactRatio,
|
|
389
|
-
maxLen: this._maxLen
|
|
348
|
+
return out;
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Map each element to a new element in a possibly different-typed queue.
|
|
352
|
+
* @remarks Time O(N), Space O(N)
|
|
353
|
+
* @template EM
|
|
354
|
+
* @template RM
|
|
355
|
+
* @param callback - Mapping function (element, index, queue) → newElement.
|
|
356
|
+
* @param [options] - Options for the output queue (e.g., toElementFn, maxLen, autoCompactRatio).
|
|
357
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
358
|
+
* @returns A new Queue with mapped elements.
|
|
359
|
+
*/
|
|
360
|
+
map(callback, options, thisArg) {
|
|
361
|
+
const out = new this.constructor([], {
|
|
362
|
+
toElementFn: options?.toElementFn,
|
|
363
|
+
maxLen: options?.maxLen ?? this._maxLen,
|
|
364
|
+
autoCompactRatio: options?.autoCompactRatio ?? this._autoCompactRatio
|
|
390
365
|
});
|
|
391
366
|
let index = 0;
|
|
392
|
-
for (const
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
}
|
|
396
|
-
return newDeque;
|
|
367
|
+
for (const v of this)
|
|
368
|
+
out.push(thisArg === undefined ? callback(v, index++, this) : callback.call(thisArg, v, index++, this));
|
|
369
|
+
return out;
|
|
397
370
|
}
|
|
398
371
|
/**
|
|
399
|
-
*
|
|
400
|
-
*
|
|
401
|
-
*
|
|
402
|
-
*
|
|
372
|
+
* Map each element to a new value of the same type.
|
|
373
|
+
* @remarks Time O(N), Space O(N)
|
|
374
|
+
* @param callback - Mapping function (element, index, queue) → element.
|
|
375
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
376
|
+
* @returns A new queue with mapped elements (same element type).
|
|
403
377
|
*/
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
378
|
+
mapSame(callback, thisArg) {
|
|
379
|
+
const Ctor = this.constructor;
|
|
380
|
+
const out = new Ctor([], {
|
|
381
|
+
toElementFn: this.toElementFn,
|
|
382
|
+
maxLen: this._maxLen,
|
|
383
|
+
autoCompactRatio: this._autoCompactRatio
|
|
384
|
+
});
|
|
385
|
+
out._setAutoCompactRatio?.(this._autoCompactRatio);
|
|
386
|
+
let index = 0;
|
|
387
|
+
for (const v of this) {
|
|
388
|
+
const mv = thisArg === undefined ? callback(v, index++, this) : callback.call(thisArg, v, index++, this);
|
|
389
|
+
out.push(mv);
|
|
407
390
|
}
|
|
391
|
+
return out;
|
|
408
392
|
}
|
|
409
393
|
/**
|
|
410
|
-
*
|
|
411
|
-
*
|
|
412
|
-
* @param
|
|
413
|
-
*
|
|
414
|
-
* allows you to specify settings or properties that can influence how the queue operates.
|
|
415
|
-
* @returns An instance of the `Queue` class with an empty array and the provided options is being
|
|
416
|
-
* returned.
|
|
394
|
+
* (Protected) Set the internal auto-compaction ratio.
|
|
395
|
+
* @remarks Time O(1), Space O(1)
|
|
396
|
+
* @param value - New ratio to assign.
|
|
397
|
+
* @returns void
|
|
417
398
|
*/
|
|
418
|
-
|
|
419
|
-
|
|
399
|
+
_setAutoCompactRatio(value) {
|
|
400
|
+
this._autoCompactRatio = value;
|
|
420
401
|
}
|
|
421
402
|
/**
|
|
422
|
-
*
|
|
423
|
-
*
|
|
403
|
+
* (Protected) Iterate elements from front to back.
|
|
404
|
+
* @remarks Time O(N), Space O(1)
|
|
405
|
+
* @returns Iterator of E.
|
|
406
|
+
*/
|
|
407
|
+
*_getIterator() {
|
|
408
|
+
for (let i = this._offset; i < this.elements.length; i++)
|
|
409
|
+
yield this.elements[i];
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* (Protected) Iterate elements from back to front.
|
|
413
|
+
* @remarks Time O(N), Space O(1)
|
|
414
|
+
* @returns Iterator of E.
|
|
424
415
|
*/
|
|
425
416
|
*_getReverseIterator() {
|
|
426
417
|
for (let i = this.length - 1; i >= 0; i--) {
|
|
427
|
-
const cur = this.at(i);
|
|
418
|
+
const cur = this.at(i);
|
|
428
419
|
if (cur !== undefined)
|
|
429
420
|
yield cur;
|
|
430
421
|
}
|
|
431
422
|
}
|
|
423
|
+
/**
|
|
424
|
+
* (Protected) Create an empty instance of the same concrete class.
|
|
425
|
+
* @remarks Time O(1), Space O(1)
|
|
426
|
+
* @param [options] - Options forwarded to the constructor.
|
|
427
|
+
* @returns An empty like-kind queue instance.
|
|
428
|
+
*/
|
|
429
|
+
_createInstance(options) {
|
|
430
|
+
const Ctor = this.constructor;
|
|
431
|
+
return new Ctor([], options);
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* (Protected) Create a like-kind queue and seed it from an iterable.
|
|
435
|
+
* @remarks Time O(N), Space O(N)
|
|
436
|
+
* @template EM
|
|
437
|
+
* @template RM
|
|
438
|
+
* @param [elements] - Iterable used to seed the new queue.
|
|
439
|
+
* @param [options] - Options forwarded to the constructor.
|
|
440
|
+
* @returns A like-kind Queue instance.
|
|
441
|
+
*/
|
|
442
|
+
_createLike(elements = [], options) {
|
|
443
|
+
const Ctor = this.constructor;
|
|
444
|
+
return new Ctor(elements, options);
|
|
445
|
+
}
|
|
432
446
|
}
|
|
433
447
|
/**
|
|
434
|
-
*
|
|
435
|
-
*
|
|
436
|
-
*
|
|
437
|
-
*
|
|
448
|
+
* Queue implemented over a singly linked list; preserves head/tail operations with linear scans for queries.
|
|
449
|
+
* @remarks Time O(1), Space O(1)
|
|
450
|
+
* @template E
|
|
451
|
+
* @template R
|
|
452
|
+
* @example examples will be generated by unit test
|
|
438
453
|
*/
|
|
439
454
|
export class LinkedListQueue extends SinglyLinkedList {
|
|
440
455
|
/**
|
|
441
|
-
*
|
|
442
|
-
*
|
|
443
|
-
*
|
|
444
|
-
* the current instance.
|
|
445
|
-
* @returns The `clone()` method is returning a new instance of `LinkedListQueue` with the same
|
|
446
|
-
* values as the original `LinkedListQueue`.
|
|
456
|
+
* Deep clone this linked-list-based queue.
|
|
457
|
+
* @remarks Time O(N), Space O(N)
|
|
458
|
+
* @returns A new queue with the same sequence of elements.
|
|
447
459
|
*/
|
|
448
460
|
clone() {
|
|
449
|
-
|
|
461
|
+
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
462
|
+
for (const v of this)
|
|
463
|
+
out.push(v);
|
|
464
|
+
return out;
|
|
450
465
|
}
|
|
451
466
|
}
|
|
452
467
|
//# sourceMappingURL=queue.js.map
|