data-structure-typed 1.54.2 → 2.0.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/LICENSE +2 -2
- package/README.md +14 -1
- package/README_zh-CN.md +1 -1
- package/benchmark/report.html +28 -1
- package/benchmark/report.json +327 -18
- package/dist/cjs/data-structures/base/iterable-element-base.d.ts +14 -40
- package/dist/cjs/data-structures/base/iterable-element-base.js +14 -11
- package/dist/cjs/data-structures/base/iterable-element-base.js.map +1 -1
- package/dist/cjs/data-structures/base/linear-base.d.ts +277 -0
- package/dist/cjs/data-structures/base/linear-base.js +553 -0
- package/dist/cjs/data-structures/base/linear-base.js.map +1 -0
- package/dist/cjs/data-structures/binary-tree/avl-tree-counter.d.ts +21 -20
- package/dist/cjs/data-structures/binary-tree/avl-tree-counter.js +8 -7
- 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 +23 -19
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +51 -38
- 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 +89 -21
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +76 -8
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +173 -225
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +244 -149
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +62 -56
- package/dist/cjs/data-structures/binary-tree/bst.js +89 -133
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/red-black-tree.d.ts +19 -25
- package/dist/cjs/data-structures/binary-tree/red-black-tree.js +7 -13
- 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 +19 -19
- package/dist/cjs/data-structures/binary-tree/tree-counter.js +12 -12
- 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 +186 -25
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +211 -41
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js +2 -2
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +3 -11
- package/dist/cjs/data-structures/heap/heap.js +0 -10
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.d.ts +2 -2
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.d.ts +2 -2
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +65 -94
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +131 -146
- 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 +79 -75
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +217 -169
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -2
- 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 +2 -2
- 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 -2
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +130 -91
- package/dist/cjs/data-structures/queue/deque.js +269 -169
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +84 -40
- package/dist/cjs/data-structures/queue/queue.js +134 -50
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +3 -11
- package/dist/cjs/data-structures/stack/stack.js +0 -10
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +4 -3
- package/dist/cjs/data-structures/trie/trie.js +3 -0
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/types/data-structures/base/base.d.ts +9 -4
- package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -0
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
- package/dist/cjs/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
- package/dist/cjs/types/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/cjs/types/data-structures/queue/deque.d.ts +2 -3
- package/dist/cjs/types/data-structures/queue/queue.d.ts +2 -2
- package/dist/cjs/utils/utils.d.ts +2 -2
- package/dist/esm/data-structures/base/iterable-element-base.d.ts +14 -40
- package/dist/esm/data-structures/base/iterable-element-base.js +14 -11
- package/dist/esm/data-structures/base/iterable-element-base.js.map +1 -1
- package/dist/esm/data-structures/base/linear-base.d.ts +277 -0
- package/dist/esm/data-structures/base/linear-base.js +549 -0
- package/dist/esm/data-structures/base/linear-base.js.map +1 -0
- package/dist/esm/data-structures/binary-tree/avl-tree-counter.d.ts +21 -20
- package/dist/esm/data-structures/binary-tree/avl-tree-counter.js +9 -8
- 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 +23 -19
- package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js +52 -38
- 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 +89 -21
- package/dist/esm/data-structures/binary-tree/avl-tree.js +77 -9
- package/dist/esm/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/binary-tree.d.ts +173 -225
- package/dist/esm/data-structures/binary-tree/binary-tree.js +248 -153
- package/dist/esm/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/bst.d.ts +62 -56
- package/dist/esm/data-structures/binary-tree/bst.js +90 -132
- package/dist/esm/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/red-black-tree.d.ts +19 -25
- package/dist/esm/data-structures/binary-tree/red-black-tree.js +8 -14
- 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 +19 -19
- package/dist/esm/data-structures/binary-tree/tree-counter.js +13 -13
- 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 +186 -25
- package/dist/esm/data-structures/binary-tree/tree-multi-map.js +212 -41
- package/dist/esm/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/esm/data-structures/graph/abstract-graph.js +2 -2
- package/dist/esm/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/esm/data-structures/heap/heap.d.ts +3 -11
- package/dist/esm/data-structures/heap/heap.js +0 -10
- package/dist/esm/data-structures/heap/heap.js.map +1 -1
- package/dist/esm/data-structures/heap/max-heap.d.ts +2 -2
- package/dist/esm/data-structures/heap/max-heap.js.map +1 -1
- package/dist/esm/data-structures/heap/min-heap.d.ts +2 -2
- package/dist/esm/data-structures/heap/min-heap.js.map +1 -1
- package/dist/esm/data-structures/linked-list/doubly-linked-list.d.ts +65 -94
- package/dist/esm/data-structures/linked-list/doubly-linked-list.js +132 -148
- 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 +79 -75
- package/dist/esm/data-structures/linked-list/singly-linked-list.js +217 -170
- package/dist/esm/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/esm/data-structures/priority-queue/max-priority-queue.d.ts +2 -2
- 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 +2 -2
- 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 -2
- package/dist/esm/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/esm/data-structures/queue/deque.d.ts +130 -91
- package/dist/esm/data-structures/queue/deque.js +269 -169
- package/dist/esm/data-structures/queue/deque.js.map +1 -1
- package/dist/esm/data-structures/queue/queue.d.ts +84 -40
- package/dist/esm/data-structures/queue/queue.js +135 -51
- package/dist/esm/data-structures/queue/queue.js.map +1 -1
- package/dist/esm/data-structures/stack/stack.d.ts +3 -11
- package/dist/esm/data-structures/stack/stack.js +0 -10
- package/dist/esm/data-structures/stack/stack.js.map +1 -1
- package/dist/esm/data-structures/trie/trie.d.ts +4 -3
- package/dist/esm/data-structures/trie/trie.js +3 -0
- package/dist/esm/data-structures/trie/trie.js.map +1 -1
- package/dist/esm/types/data-structures/base/base.d.ts +9 -4
- package/dist/esm/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
- package/dist/esm/types/data-structures/binary-tree/binary-tree.d.ts +1 -0
- package/dist/esm/types/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/esm/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
- package/dist/esm/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
- package/dist/esm/types/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/esm/types/data-structures/queue/deque.d.ts +2 -3
- package/dist/esm/types/data-structures/queue/queue.d.ts +2 -2
- package/dist/esm/utils/utils.d.ts +2 -2
- package/dist/umd/data-structure-typed.js +1548 -920
- package/dist/umd/data-structure-typed.min.js +5 -12
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +7 -7
- package/src/data-structures/base/iterable-element-base.ts +29 -20
- package/src/data-structures/base/linear-base.ts +649 -0
- package/src/data-structures/binary-tree/avl-tree-counter.ts +30 -23
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +74 -49
- package/src/data-structures/binary-tree/avl-tree.ts +99 -29
- package/src/data-structures/binary-tree/binary-tree.ts +474 -257
- package/src/data-structures/binary-tree/bst.ts +150 -152
- package/src/data-structures/binary-tree/red-black-tree.ts +27 -35
- package/src/data-structures/binary-tree/tree-counter.ts +33 -27
- package/src/data-structures/binary-tree/tree-multi-map.ts +235 -53
- package/src/data-structures/graph/abstract-graph.ts +2 -2
- package/src/data-structures/heap/heap.ts +3 -14
- package/src/data-structures/heap/max-heap.ts +2 -2
- package/src/data-structures/heap/min-heap.ts +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +144 -160
- package/src/data-structures/linked-list/singly-linked-list.ts +241 -185
- package/src/data-structures/priority-queue/max-priority-queue.ts +2 -5
- package/src/data-structures/priority-queue/min-priority-queue.ts +2 -5
- package/src/data-structures/priority-queue/priority-queue.ts +2 -2
- package/src/data-structures/queue/deque.ts +286 -183
- package/src/data-structures/queue/queue.ts +149 -63
- package/src/data-structures/stack/stack.ts +3 -18
- package/src/data-structures/trie/trie.ts +7 -3
- package/src/types/data-structures/base/base.ts +17 -8
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -0
- package/src/types/data-structures/binary-tree/bst.ts +1 -1
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +1 -1
- package/src/types/data-structures/linked-list/doubly-linked-list.ts +2 -2
- package/src/types/data-structures/linked-list/singly-linked-list.ts +2 -2
- package/src/types/data-structures/queue/deque.ts +2 -3
- package/src/types/data-structures/queue/queue.ts +2 -2
- package/src/utils/utils.ts +2 -2
- package/test/integration/all-in-one.test.ts +1 -1
- package/test/integration/avl-tree.test.ts +1 -1
- package/test/integration/bst.test.ts +2 -2
- package/test/integration/compile.mjs +21 -21
- package/test/performance/data-structures/binary-tree/avl-tree.test.mjs +71 -0
- package/test/performance/data-structures/binary-tree/red-black-tree.test.mjs +81 -0
- package/test/performance/{reportor.js → reportor.mjs} +264 -8
- package/test/performance/reportor.ts +1 -1
- package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +7 -7
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +172 -5
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +72 -4
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +132 -82
- package/test/unit/data-structures/binary-tree/bst.test.ts +12 -12
- package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +5 -13
- package/test/unit/data-structures/binary-tree/tree-counter.test.ts +4 -4
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +168 -10
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +135 -27
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +111 -11
- package/test/unit/data-structures/queue/deque.test.ts +241 -60
- package/test/unit/data-structures/queue/queue.test.ts +118 -19
- package/test/unit/unrestricted-interconversion.test.ts +1 -1
- package/test/utils/json2html.ts +0 -154
- package/test/performance/data-structures/binary-tree/avl-tree.test.js +0 -45
- /package/test/performance/data-structures/binary-tree/{rb-tree.test.ts → red-black-tree.test.ts} +0 -0
|
@@ -1,13 +1,101 @@
|
|
|
1
|
-
import { IterableElementBase } from '../base';
|
|
2
1
|
import { calcMinUnitsRequired, rangeCheck } from '../../utils';
|
|
2
|
+
import { LinearBase } from '../base/linear-base';
|
|
3
3
|
/**
|
|
4
4
|
* 1. Operations at Both Ends: Supports adding and removing elements at both the front and back of the queue. This allows it to be used as a stack (last in, first out) and a queue (first in, first out).
|
|
5
5
|
* 2. Efficient Random Access: Being based on an array, it offers fast random access capability, allowing constant time access to any element.
|
|
6
6
|
* 3. Continuous Memory Allocation: Since it is based on an array, all elements are stored contiguously in memory, which can bring cache friendliness and efficient memory access.
|
|
7
7
|
* 4. Efficiency: Adding and removing elements at both ends of a deque is usually very fast. However, when the dynamic array needs to expand, it may involve copying the entire array to a larger one, and this operation has a time complexity of O(n).
|
|
8
8
|
* 5. Performance jitter: Deque may experience performance jitter, but DoublyLinkedList will not
|
|
9
|
+
* @example
|
|
10
|
+
* // prize roulette
|
|
11
|
+
* class PrizeRoulette {
|
|
12
|
+
* private deque: Deque<string>;
|
|
13
|
+
*
|
|
14
|
+
* constructor(prizes: string[]) {
|
|
15
|
+
* // Initialize the deque with prizes
|
|
16
|
+
* this.deque = new Deque<string>(prizes);
|
|
17
|
+
* }
|
|
18
|
+
*
|
|
19
|
+
* // Rotate clockwise to the right (forward)
|
|
20
|
+
* rotateClockwise(steps: number): void {
|
|
21
|
+
* const n = this.deque.length;
|
|
22
|
+
* if (n === 0) return;
|
|
23
|
+
*
|
|
24
|
+
* for (let i = 0; i < steps; i++) {
|
|
25
|
+
* const last = this.deque.pop(); // Remove the last element
|
|
26
|
+
* this.deque.unshift(last!); // Add it to the front
|
|
27
|
+
* }
|
|
28
|
+
* }
|
|
29
|
+
*
|
|
30
|
+
* // Rotate counterclockwise to the left (backward)
|
|
31
|
+
* rotateCounterClockwise(steps: number): void {
|
|
32
|
+
* const n = this.deque.length;
|
|
33
|
+
* if (n === 0) return;
|
|
34
|
+
*
|
|
35
|
+
* for (let i = 0; i < steps; i++) {
|
|
36
|
+
* const first = this.deque.shift(); // Remove the first element
|
|
37
|
+
* this.deque.push(first!); // Add it to the back
|
|
38
|
+
* }
|
|
39
|
+
* }
|
|
40
|
+
*
|
|
41
|
+
* // Display the current prize at the head
|
|
42
|
+
* display() {
|
|
43
|
+
* return this.deque.first;
|
|
44
|
+
* }
|
|
45
|
+
* }
|
|
46
|
+
*
|
|
47
|
+
* // Example usage
|
|
48
|
+
* const prizes = ['Car', 'Bike', 'Laptop', 'Phone', 'Watch', 'Headphones']; // Initialize the prize list
|
|
49
|
+
* const roulette = new PrizeRoulette(prizes);
|
|
50
|
+
*
|
|
51
|
+
* // Display the initial state
|
|
52
|
+
* console.log(roulette.display()); // 'Car' // Car
|
|
53
|
+
*
|
|
54
|
+
* // Rotate clockwise by 3 steps
|
|
55
|
+
* roulette.rotateClockwise(3);
|
|
56
|
+
* console.log(roulette.display()); // 'Phone' // Phone
|
|
57
|
+
*
|
|
58
|
+
* // Rotate counterclockwise by 2 steps
|
|
59
|
+
* roulette.rotateCounterClockwise(2);
|
|
60
|
+
* console.log(roulette.display()); // 'Headphones'
|
|
61
|
+
* @example
|
|
62
|
+
* // sliding window
|
|
63
|
+
* // Maximum function of sliding window
|
|
64
|
+
* function maxSlidingWindow(nums: number[], k: number): number[] {
|
|
65
|
+
* const n = nums.length;
|
|
66
|
+
* if (n * k === 0) return [];
|
|
67
|
+
*
|
|
68
|
+
* const deq = new Deque<number>();
|
|
69
|
+
* const result: number[] = [];
|
|
70
|
+
*
|
|
71
|
+
* for (let i = 0; i < n; i++) {
|
|
72
|
+
* // Delete indexes in the queue that are not within the window range
|
|
73
|
+
* if (deq.length > 0 && deq.first! === i - k) {
|
|
74
|
+
* deq.shift();
|
|
75
|
+
* }
|
|
76
|
+
*
|
|
77
|
+
* // Remove all indices less than the current value from the tail of the queue
|
|
78
|
+
* while (deq.length > 0 && nums[deq.last!] < nums[i]) {
|
|
79
|
+
* deq.pop();
|
|
80
|
+
* }
|
|
81
|
+
*
|
|
82
|
+
* // Add the current index to the end of the queue
|
|
83
|
+
* deq.push(i);
|
|
84
|
+
*
|
|
85
|
+
* // Add the maximum value of the window to the results
|
|
86
|
+
* if (i >= k - 1) {
|
|
87
|
+
* result.push(nums[deq.first!]);
|
|
88
|
+
* }
|
|
89
|
+
* }
|
|
90
|
+
*
|
|
91
|
+
* return result;
|
|
92
|
+
* }
|
|
93
|
+
*
|
|
94
|
+
* const nums = [1, 3, -1, -3, 5, 3, 6, 7];
|
|
95
|
+
* const k = 3;
|
|
96
|
+
* console.log(maxSlidingWindow(nums, k)); // [3, 3, 5, 5, 6, 7]
|
|
9
97
|
*/
|
|
10
|
-
export class Deque extends
|
|
98
|
+
export class Deque extends LinearBase {
|
|
11
99
|
/**
|
|
12
100
|
* The constructor initializes a Deque object with optional iterable of elements and options.
|
|
13
101
|
* @param elements - An iterable object (such as an array or a Set) that contains the initial
|
|
@@ -22,11 +110,9 @@ export class Deque extends IterableElementBase {
|
|
|
22
110
|
constructor(elements = [], options) {
|
|
23
111
|
super(options);
|
|
24
112
|
if (options) {
|
|
25
|
-
const { bucketSize
|
|
113
|
+
const { bucketSize } = options;
|
|
26
114
|
if (typeof bucketSize === 'number')
|
|
27
115
|
this._bucketSize = bucketSize;
|
|
28
|
-
if (typeof maxLen === 'number' && maxLen > 0 && maxLen % 1 === 0)
|
|
29
|
-
this._maxLen = maxLen;
|
|
30
116
|
}
|
|
31
117
|
let _size;
|
|
32
118
|
if ('length' in elements) {
|
|
@@ -51,80 +137,36 @@ export class Deque extends IterableElementBase {
|
|
|
51
137
|
this.pushMany(elements);
|
|
52
138
|
}
|
|
53
139
|
_bucketSize = 1 << 12;
|
|
54
|
-
/**
|
|
55
|
-
* The bucketSize function returns the size of the bucket.
|
|
56
|
-
*
|
|
57
|
-
* @return The size of the bucket
|
|
58
|
-
*/
|
|
59
140
|
get bucketSize() {
|
|
60
141
|
return this._bucketSize;
|
|
61
142
|
}
|
|
62
|
-
_maxLen = -1;
|
|
63
|
-
/**
|
|
64
|
-
* The maxLen function returns the max length of the deque.
|
|
65
|
-
*
|
|
66
|
-
* @return The max length of the deque
|
|
67
|
-
*/
|
|
68
|
-
get maxLen() {
|
|
69
|
-
return this._maxLen;
|
|
70
|
-
}
|
|
71
143
|
_bucketFirst = 0;
|
|
72
|
-
/**
|
|
73
|
-
* The function returns the value of the protected variable `_bucketFirst`.
|
|
74
|
-
* @returns The value of the `_bucketFirst` property.
|
|
75
|
-
*/
|
|
76
144
|
get bucketFirst() {
|
|
77
145
|
return this._bucketFirst;
|
|
78
146
|
}
|
|
79
147
|
_firstInBucket = 0;
|
|
80
|
-
/**
|
|
81
|
-
* The function returns the value of the protected variable _firstInBucket.
|
|
82
|
-
* @returns The method is returning the value of the variable `_firstInBucket`, which is of type
|
|
83
|
-
* `number`.
|
|
84
|
-
*/
|
|
85
148
|
get firstInBucket() {
|
|
86
149
|
return this._firstInBucket;
|
|
87
150
|
}
|
|
88
151
|
_bucketLast = 0;
|
|
89
|
-
/**
|
|
90
|
-
* The function returns the value of the protected variable `_bucketLast`.
|
|
91
|
-
* @returns The value of the `_bucketLast` property, which is a number.
|
|
92
|
-
*/
|
|
93
152
|
get bucketLast() {
|
|
94
153
|
return this._bucketLast;
|
|
95
154
|
}
|
|
96
155
|
_lastInBucket = 0;
|
|
97
|
-
/**
|
|
98
|
-
* The function returns the value of the protected variable _lastInBucket.
|
|
99
|
-
* @returns The method is returning the value of the variable `_lastInBucket`, which is of type
|
|
100
|
-
* `number`.
|
|
101
|
-
*/
|
|
102
156
|
get lastInBucket() {
|
|
103
157
|
return this._lastInBucket;
|
|
104
158
|
}
|
|
105
159
|
_bucketCount = 0;
|
|
106
|
-
/**
|
|
107
|
-
* The function returns the number of buckets.
|
|
108
|
-
* @returns The number of buckets.
|
|
109
|
-
*/
|
|
110
160
|
get bucketCount() {
|
|
111
161
|
return this._bucketCount;
|
|
112
162
|
}
|
|
113
163
|
_buckets = [];
|
|
114
|
-
/**
|
|
115
|
-
* The buckets function returns the buckets property of the object.
|
|
116
|
-
* @return The buckets property
|
|
117
|
-
*/
|
|
118
164
|
get buckets() {
|
|
119
165
|
return this._buckets;
|
|
120
166
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
* @return The number of values in the set
|
|
125
|
-
*/
|
|
126
|
-
get size() {
|
|
127
|
-
return this._size;
|
|
167
|
+
_length = 0;
|
|
168
|
+
get length() {
|
|
169
|
+
return this._length;
|
|
128
170
|
}
|
|
129
171
|
/**
|
|
130
172
|
* The function returns the first element in a collection if it exists, otherwise it returns
|
|
@@ -132,7 +174,7 @@ export class Deque extends IterableElementBase {
|
|
|
132
174
|
* @returns The first element of the collection, of type E, is being returned.
|
|
133
175
|
*/
|
|
134
176
|
get first() {
|
|
135
|
-
if (this.
|
|
177
|
+
if (this._length === 0)
|
|
136
178
|
return;
|
|
137
179
|
return this._buckets[this._bucketFirst][this._firstInBucket];
|
|
138
180
|
}
|
|
@@ -141,7 +183,7 @@ export class Deque extends IterableElementBase {
|
|
|
141
183
|
* @return The last element in the array
|
|
142
184
|
*/
|
|
143
185
|
get last() {
|
|
144
|
-
if (this.
|
|
186
|
+
if (this._length === 0)
|
|
145
187
|
return;
|
|
146
188
|
return this._buckets[this._bucketLast][this._lastInBucket];
|
|
147
189
|
}
|
|
@@ -155,7 +197,7 @@ export class Deque extends IterableElementBase {
|
|
|
155
197
|
* @returns The size of the data structure after the element has been pushed.
|
|
156
198
|
*/
|
|
157
199
|
push(element) {
|
|
158
|
-
if (this.
|
|
200
|
+
if (this._length) {
|
|
159
201
|
if (this._lastInBucket < this._bucketSize - 1) {
|
|
160
202
|
this._lastInBucket += 1;
|
|
161
203
|
}
|
|
@@ -170,9 +212,9 @@ export class Deque extends IterableElementBase {
|
|
|
170
212
|
if (this._bucketLast === this._bucketFirst && this._lastInBucket === this._firstInBucket)
|
|
171
213
|
this._reallocate();
|
|
172
214
|
}
|
|
173
|
-
this.
|
|
215
|
+
this._length += 1;
|
|
174
216
|
this._buckets[this._bucketLast][this._lastInBucket] = element;
|
|
175
|
-
if (this._maxLen > 0 && this.
|
|
217
|
+
if (this._maxLen > 0 && this._length > this._maxLen)
|
|
176
218
|
this.shift();
|
|
177
219
|
return true;
|
|
178
220
|
}
|
|
@@ -185,10 +227,10 @@ export class Deque extends IterableElementBase {
|
|
|
185
227
|
* @returns The element that was removed from the data structure is being returned.
|
|
186
228
|
*/
|
|
187
229
|
pop() {
|
|
188
|
-
if (this.
|
|
230
|
+
if (this._length === 0)
|
|
189
231
|
return;
|
|
190
232
|
const element = this._buckets[this._bucketLast][this._lastInBucket];
|
|
191
|
-
if (this.
|
|
233
|
+
if (this._length !== 1) {
|
|
192
234
|
if (this._lastInBucket > 0) {
|
|
193
235
|
this._lastInBucket -= 1;
|
|
194
236
|
}
|
|
@@ -201,7 +243,7 @@ export class Deque extends IterableElementBase {
|
|
|
201
243
|
this._lastInBucket = this._bucketSize - 1;
|
|
202
244
|
}
|
|
203
245
|
}
|
|
204
|
-
this.
|
|
246
|
+
this._length -= 1;
|
|
205
247
|
return element;
|
|
206
248
|
}
|
|
207
249
|
/**
|
|
@@ -214,10 +256,10 @@ export class Deque extends IterableElementBase {
|
|
|
214
256
|
* returned.
|
|
215
257
|
*/
|
|
216
258
|
shift() {
|
|
217
|
-
if (this.
|
|
259
|
+
if (this._length === 0)
|
|
218
260
|
return;
|
|
219
261
|
const element = this._buckets[this._bucketFirst][this._firstInBucket];
|
|
220
|
-
if (this.
|
|
262
|
+
if (this._length !== 1) {
|
|
221
263
|
if (this._firstInBucket < this._bucketSize - 1) {
|
|
222
264
|
this._firstInBucket += 1;
|
|
223
265
|
}
|
|
@@ -230,7 +272,7 @@ export class Deque extends IterableElementBase {
|
|
|
230
272
|
this._firstInBucket = 0;
|
|
231
273
|
}
|
|
232
274
|
}
|
|
233
|
-
this.
|
|
275
|
+
this._length -= 1;
|
|
234
276
|
return element;
|
|
235
277
|
}
|
|
236
278
|
/**
|
|
@@ -244,7 +286,7 @@ export class Deque extends IterableElementBase {
|
|
|
244
286
|
* @returns The size of the data structure after the element has been added.
|
|
245
287
|
*/
|
|
246
288
|
unshift(element) {
|
|
247
|
-
if (this.
|
|
289
|
+
if (this._length) {
|
|
248
290
|
if (this._firstInBucket > 0) {
|
|
249
291
|
this._firstInBucket -= 1;
|
|
250
292
|
}
|
|
@@ -259,9 +301,9 @@ export class Deque extends IterableElementBase {
|
|
|
259
301
|
if (this._bucketFirst === this._bucketLast && this._firstInBucket === this._lastInBucket)
|
|
260
302
|
this._reallocate();
|
|
261
303
|
}
|
|
262
|
-
this.
|
|
304
|
+
this._length += 1;
|
|
263
305
|
this._buckets[this._bucketFirst][this._firstInBucket] = element;
|
|
264
|
-
if (this._maxLen > 0 && this.
|
|
306
|
+
if (this._maxLen > 0 && this._length > this._maxLen)
|
|
265
307
|
this.pop();
|
|
266
308
|
return true;
|
|
267
309
|
}
|
|
@@ -324,7 +366,7 @@ export class Deque extends IterableElementBase {
|
|
|
324
366
|
* @returns A boolean value indicating whether the size of the object is 0 or not.
|
|
325
367
|
*/
|
|
326
368
|
isEmpty() {
|
|
327
|
-
return this.
|
|
369
|
+
return this._length === 0;
|
|
328
370
|
}
|
|
329
371
|
/**
|
|
330
372
|
* Time Complexity: O(1)
|
|
@@ -336,30 +378,9 @@ export class Deque extends IterableElementBase {
|
|
|
336
378
|
clear() {
|
|
337
379
|
this._buckets = [new Array(this._bucketSize)];
|
|
338
380
|
this._bucketCount = 1;
|
|
339
|
-
this._bucketFirst = this._bucketLast = this.
|
|
381
|
+
this._bucketFirst = this._bucketLast = this._length = 0;
|
|
340
382
|
this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
|
|
341
383
|
}
|
|
342
|
-
/**
|
|
343
|
-
* The below function is a generator that yields elements from a collection one by one.
|
|
344
|
-
*/
|
|
345
|
-
*begin() {
|
|
346
|
-
let index = 0;
|
|
347
|
-
while (index < this._size) {
|
|
348
|
-
yield this.at(index);
|
|
349
|
-
index++;
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
/**
|
|
353
|
-
* The function `reverseBegin()` is a generator that yields elements in reverse order starting from
|
|
354
|
-
* the last element.
|
|
355
|
-
*/
|
|
356
|
-
*reverseBegin() {
|
|
357
|
-
let index = this._size - 1;
|
|
358
|
-
while (index >= 0) {
|
|
359
|
-
yield this.at(index);
|
|
360
|
-
index--;
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
384
|
/**
|
|
364
385
|
* Time Complexity: O(1)
|
|
365
386
|
* Space Complexity: O(1)
|
|
@@ -371,7 +392,7 @@ export class Deque extends IterableElementBase {
|
|
|
371
392
|
* @returns The element at the specified position in the data structure is being returned.
|
|
372
393
|
*/
|
|
373
394
|
at(pos) {
|
|
374
|
-
rangeCheck(pos, 0, this.
|
|
395
|
+
rangeCheck(pos, 0, this._length - 1);
|
|
375
396
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
376
397
|
return this._buckets[bucketIndex][indexInBucket];
|
|
377
398
|
}
|
|
@@ -386,7 +407,7 @@ export class Deque extends IterableElementBase {
|
|
|
386
407
|
* position in the data structure.
|
|
387
408
|
*/
|
|
388
409
|
setAt(pos, element) {
|
|
389
|
-
rangeCheck(pos, 0, this.
|
|
410
|
+
rangeCheck(pos, 0, this._length - 1);
|
|
390
411
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
391
412
|
this._buckets[bucketIndex][indexInBucket] = element;
|
|
392
413
|
return true;
|
|
@@ -407,19 +428,19 @@ export class Deque extends IterableElementBase {
|
|
|
407
428
|
* @returns The size of the array after the insertion is being returned.
|
|
408
429
|
*/
|
|
409
430
|
addAt(pos, element, num = 1) {
|
|
410
|
-
const length = this.
|
|
431
|
+
const length = this._length;
|
|
411
432
|
rangeCheck(pos, 0, length);
|
|
412
433
|
if (pos === 0) {
|
|
413
434
|
while (num--)
|
|
414
435
|
this.unshift(element);
|
|
415
436
|
}
|
|
416
|
-
else if (pos === this.
|
|
437
|
+
else if (pos === this._length) {
|
|
417
438
|
while (num--)
|
|
418
439
|
this.push(element);
|
|
419
440
|
}
|
|
420
441
|
else {
|
|
421
442
|
const arr = [];
|
|
422
|
-
for (let i = pos; i < this.
|
|
443
|
+
for (let i = pos; i < this._length; ++i) {
|
|
423
444
|
arr.push(this.at(i));
|
|
424
445
|
}
|
|
425
446
|
this.cut(pos - 1, true);
|
|
@@ -450,17 +471,69 @@ export class Deque extends IterableElementBase {
|
|
|
450
471
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
451
472
|
this._bucketLast = bucketIndex;
|
|
452
473
|
this._lastInBucket = indexInBucket;
|
|
453
|
-
this.
|
|
474
|
+
this._length = pos + 1;
|
|
454
475
|
return this;
|
|
455
476
|
}
|
|
456
477
|
else {
|
|
457
|
-
const newDeque =
|
|
478
|
+
const newDeque = this._createInstance({
|
|
479
|
+
bucketSize: this._bucketSize,
|
|
480
|
+
toElementFn: this._toElementFn,
|
|
481
|
+
maxLen: this._maxLen
|
|
482
|
+
});
|
|
458
483
|
for (let i = 0; i <= pos; i++) {
|
|
459
484
|
newDeque.push(this.at(i));
|
|
460
485
|
}
|
|
461
486
|
return newDeque;
|
|
462
487
|
}
|
|
463
488
|
}
|
|
489
|
+
/**
|
|
490
|
+
* Time Complexity: O(n)
|
|
491
|
+
* Space Complexity: O(1)
|
|
492
|
+
*
|
|
493
|
+
* The `splice` function in TypeScript overrides the default behavior to remove and insert elements
|
|
494
|
+
* in a Deque data structure while ensuring the starting position and delete count are within bounds.
|
|
495
|
+
* @param {number} start - The `start` parameter in the `splice` method represents the index at which
|
|
496
|
+
* to start changing the array. Items will be removed or added starting from this index.
|
|
497
|
+
* @param {number} deleteCount - The `deleteCount` parameter in the `splice` method represents the
|
|
498
|
+
* number of elements to remove from the array starting at the specified `start` index. If
|
|
499
|
+
* `deleteCount` is not provided, it defaults to the number of elements from the `start` index to the
|
|
500
|
+
* end of the array (`
|
|
501
|
+
* @param {E[]} items - The `items` parameter in the `splice` method represents the elements that
|
|
502
|
+
* will be inserted into the deque at the specified `start` index. These elements will be inserted in
|
|
503
|
+
* place of the elements that are removed based on the `start` and `deleteCount` parameters.
|
|
504
|
+
* @returns The `splice` method is returning the array `deletedElements` which contains the elements
|
|
505
|
+
* that were removed from the Deque during the splice operation.
|
|
506
|
+
*/
|
|
507
|
+
splice(start, deleteCount = this._length - start, ...items) {
|
|
508
|
+
// Check whether the starting position is legal
|
|
509
|
+
rangeCheck(start, 0, this._length);
|
|
510
|
+
// Adjust the value of deleteCount
|
|
511
|
+
if (deleteCount < 0)
|
|
512
|
+
deleteCount = 0;
|
|
513
|
+
if (start + deleteCount > this._length)
|
|
514
|
+
deleteCount = this._length - start;
|
|
515
|
+
// Save deleted elements
|
|
516
|
+
const deletedElements = this._createInstance();
|
|
517
|
+
// Add removed elements to the result
|
|
518
|
+
for (let i = 0; i < deleteCount; i++) {
|
|
519
|
+
deletedElements.push(this.at(start + i));
|
|
520
|
+
}
|
|
521
|
+
// Calculate the range that needs to be deleted
|
|
522
|
+
const elementsAfter = [];
|
|
523
|
+
for (let i = start + deleteCount; i < this._length; i++) {
|
|
524
|
+
elementsAfter.push(this.at(i));
|
|
525
|
+
}
|
|
526
|
+
// Adjust the length of the current Deque
|
|
527
|
+
this.cut(start - 1, true);
|
|
528
|
+
for (const item of items) {
|
|
529
|
+
this.push(item);
|
|
530
|
+
}
|
|
531
|
+
// Insert subsequent elements back
|
|
532
|
+
for (const element of elementsAfter) {
|
|
533
|
+
this.push(element);
|
|
534
|
+
}
|
|
535
|
+
return deletedElements;
|
|
536
|
+
}
|
|
464
537
|
/**
|
|
465
538
|
* Time Complexity: O(1)
|
|
466
539
|
* Space Complexity: O(1) or O(n)
|
|
@@ -484,14 +557,18 @@ export class Deque extends IterableElementBase {
|
|
|
484
557
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
485
558
|
this._bucketFirst = bucketIndex;
|
|
486
559
|
this._firstInBucket = indexInBucket;
|
|
487
|
-
this.
|
|
560
|
+
this._length = this._length - pos;
|
|
488
561
|
return this;
|
|
489
562
|
}
|
|
490
563
|
else {
|
|
491
|
-
const newDeque =
|
|
564
|
+
const newDeque = this._createInstance({
|
|
565
|
+
bucketSize: this._bucketSize,
|
|
566
|
+
toElementFn: this._toElementFn,
|
|
567
|
+
maxLen: this._maxLen
|
|
568
|
+
});
|
|
492
569
|
if (pos < 0)
|
|
493
570
|
pos = 0;
|
|
494
|
-
for (let i = pos; i < this.
|
|
571
|
+
for (let i = pos; i < this._length; i++) {
|
|
495
572
|
newDeque.push(this.at(i));
|
|
496
573
|
}
|
|
497
574
|
return newDeque;
|
|
@@ -509,23 +586,32 @@ export class Deque extends IterableElementBase {
|
|
|
509
586
|
* @returns The size of the data structure after the deletion operation is performed.
|
|
510
587
|
*/
|
|
511
588
|
deleteAt(pos) {
|
|
512
|
-
rangeCheck(pos, 0, this.
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
589
|
+
rangeCheck(pos, 0, this._length - 1);
|
|
590
|
+
let deleted;
|
|
591
|
+
if (pos === 0) {
|
|
592
|
+
//If it is the first element, use shift() directly
|
|
593
|
+
return this.shift();
|
|
594
|
+
}
|
|
595
|
+
else if (pos === this._length - 1) {
|
|
596
|
+
// If it is the last element, just use pop()
|
|
597
|
+
deleted = this.last;
|
|
516
598
|
this.pop();
|
|
599
|
+
return deleted;
|
|
600
|
+
}
|
|
517
601
|
else {
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
602
|
+
// Delete the middle element
|
|
603
|
+
const length = this._length - 1;
|
|
604
|
+
const { bucketIndex: targetBucket, indexInBucket: targetPointer } = this._getBucketAndPosition(pos);
|
|
605
|
+
deleted = this._buckets[targetBucket][targetPointer];
|
|
606
|
+
for (let i = pos; i < length; i++) {
|
|
607
|
+
const { bucketIndex: curBucket, indexInBucket: curPointer } = this._getBucketAndPosition(i);
|
|
608
|
+
const { bucketIndex: nextBucket, indexInBucket: nextPointer } = this._getBucketAndPosition(i + 1);
|
|
522
609
|
this._buckets[curBucket][curPointer] = this._buckets[nextBucket][nextPointer];
|
|
523
|
-
curBucket = nextBucket;
|
|
524
|
-
curPointer = nextPointer;
|
|
525
610
|
}
|
|
611
|
+
// Remove last duplicate element
|
|
526
612
|
this.pop();
|
|
613
|
+
return deleted;
|
|
527
614
|
}
|
|
528
|
-
return true;
|
|
529
615
|
}
|
|
530
616
|
/**
|
|
531
617
|
* Time Complexity: O(n)
|
|
@@ -538,7 +624,7 @@ export class Deque extends IterableElementBase {
|
|
|
538
624
|
* @returns The size of the data structure after the element has been deleted.
|
|
539
625
|
*/
|
|
540
626
|
delete(element) {
|
|
541
|
-
const size = this.
|
|
627
|
+
const size = this._length;
|
|
542
628
|
if (size === 0)
|
|
543
629
|
return false;
|
|
544
630
|
let i = 0;
|
|
@@ -554,6 +640,42 @@ export class Deque extends IterableElementBase {
|
|
|
554
640
|
this.cut(index - 1, true);
|
|
555
641
|
return true;
|
|
556
642
|
}
|
|
643
|
+
// /**
|
|
644
|
+
// * Time Complexity: O(n)
|
|
645
|
+
// * Space Complexity: O(1)
|
|
646
|
+
// *
|
|
647
|
+
// * This function overrides the indexOf method to search for an element within a custom data
|
|
648
|
+
// * structure.
|
|
649
|
+
// * @param {E} searchElement - The `searchElement` parameter is the element that you are searching for
|
|
650
|
+
// * within the data structure. The `indexOf` method will return the index of the first occurrence of
|
|
651
|
+
// * this element within the data structure.
|
|
652
|
+
// * @param {number} [fromIndex=0] - The `fromIndex` parameter in the `indexOf` method specifies the
|
|
653
|
+
// * index at which to start searching for the `searchElement` within the data structure. If provided,
|
|
654
|
+
// * the search will begin at this index instead of the beginning of the data structure.
|
|
655
|
+
// * @returns The indexOf method is returning the index of the searchElement if it is found in the data
|
|
656
|
+
// * structure, or -1 if the searchElement is not found.
|
|
657
|
+
// */
|
|
658
|
+
// override indexOf(searchElement: E, fromIndex: number = 0): number {
|
|
659
|
+
// let index = fromIndex;
|
|
660
|
+
// let bucketIndex = this._bucketFirst;
|
|
661
|
+
// let indexInBucket = this._firstInBucket + fromIndex;
|
|
662
|
+
//
|
|
663
|
+
// for (let i = 0; i < this._length; i++) {
|
|
664
|
+
// if (this._buckets[bucketIndex][indexInBucket] === searchElement) {
|
|
665
|
+
// return index;
|
|
666
|
+
// }
|
|
667
|
+
// index++;
|
|
668
|
+
// indexInBucket++;
|
|
669
|
+
// if (indexInBucket >= this._bucketSize) {
|
|
670
|
+
// bucketIndex++;
|
|
671
|
+
// indexInBucket = 0;
|
|
672
|
+
// }
|
|
673
|
+
// if (bucketIndex >= this._bucketCount) {
|
|
674
|
+
// bucketIndex = 0;
|
|
675
|
+
// }
|
|
676
|
+
// }
|
|
677
|
+
// return -1;
|
|
678
|
+
// }
|
|
557
679
|
/**
|
|
558
680
|
* Time Complexity: O(n)
|
|
559
681
|
* Space Complexity: O(1)
|
|
@@ -583,12 +705,12 @@ export class Deque extends IterableElementBase {
|
|
|
583
705
|
* @returns The size of the modified array is being returned.
|
|
584
706
|
*/
|
|
585
707
|
unique() {
|
|
586
|
-
if (this.
|
|
708
|
+
if (this._length <= 1) {
|
|
587
709
|
return this;
|
|
588
710
|
}
|
|
589
711
|
let index = 1;
|
|
590
712
|
let prev = this.at(0);
|
|
591
|
-
for (let i = 1; i < this.
|
|
713
|
+
for (let i = 1; i < this._length; ++i) {
|
|
592
714
|
const cur = this.at(i);
|
|
593
715
|
if (cur !== prev) {
|
|
594
716
|
prev = cur;
|
|
@@ -598,27 +720,6 @@ export class Deque extends IterableElementBase {
|
|
|
598
720
|
this.cut(index - 1, true);
|
|
599
721
|
return this;
|
|
600
722
|
}
|
|
601
|
-
/**
|
|
602
|
-
* Time Complexity: O(n log n)
|
|
603
|
-
* Space Complexity: O(n)
|
|
604
|
-
*
|
|
605
|
-
* The `sort` function sorts the elements in a data structure using a provided comparator function.
|
|
606
|
-
* @param [comparator] - The `comparator` parameter is a function that takes in two elements `x` and
|
|
607
|
-
* `y` of type `E` and returns a number. The comparator function is used to determine the order of
|
|
608
|
-
* the elements in the sorted array.
|
|
609
|
-
* @returns Deque<E>
|
|
610
|
-
*/
|
|
611
|
-
sort(comparator) {
|
|
612
|
-
const arr = [];
|
|
613
|
-
for (let i = 0; i < this._size; ++i) {
|
|
614
|
-
arr.push(this.at(i));
|
|
615
|
-
}
|
|
616
|
-
arr.sort(comparator);
|
|
617
|
-
for (let i = 0; i < this._size; ++i) {
|
|
618
|
-
this.setAt(i, arr[i]);
|
|
619
|
-
}
|
|
620
|
-
return this;
|
|
621
|
-
}
|
|
622
723
|
/**
|
|
623
724
|
* Time Complexity: O(n)
|
|
624
725
|
* Space Complexity: O(n)
|
|
@@ -626,10 +727,10 @@ export class Deque extends IterableElementBase {
|
|
|
626
727
|
* The `shrinkToFit` function reorganizes the elements in an array-like data structure to minimize
|
|
627
728
|
* memory usage.
|
|
628
729
|
* @returns Nothing is being returned. The function is using the `return` statement to exit early if
|
|
629
|
-
* `this.
|
|
730
|
+
* `this._length` is 0, but it does not return any value.
|
|
630
731
|
*/
|
|
631
732
|
shrinkToFit() {
|
|
632
|
-
if (this.
|
|
733
|
+
if (this._length === 0)
|
|
633
734
|
return;
|
|
634
735
|
const newBuckets = [];
|
|
635
736
|
if (this._bucketFirst === this._bucketLast)
|
|
@@ -651,35 +752,6 @@ export class Deque extends IterableElementBase {
|
|
|
651
752
|
this._bucketLast = newBuckets.length - 1;
|
|
652
753
|
this._buckets = newBuckets;
|
|
653
754
|
}
|
|
654
|
-
/**
|
|
655
|
-
* Time Complexity: O(n)
|
|
656
|
-
* Space Complexity: O(1)
|
|
657
|
-
*
|
|
658
|
-
* The function "indexOf" returns the index of the first occurrence of a given element in an array,
|
|
659
|
-
* or -1 if the element is not found.
|
|
660
|
-
* @param {E} element - The "element" parameter represents the element that you want to find the
|
|
661
|
-
* index of in the data structure.
|
|
662
|
-
* @returns The indexOf function returns the index of the first occurrence of the specified element
|
|
663
|
-
* in the data structure. If the element is not found, it returns -1.
|
|
664
|
-
*/
|
|
665
|
-
indexOf(element) {
|
|
666
|
-
for (let i = 0; i < this._size; ++i) {
|
|
667
|
-
if (this.at(i) === element) {
|
|
668
|
-
return i;
|
|
669
|
-
}
|
|
670
|
-
}
|
|
671
|
-
return -1;
|
|
672
|
-
}
|
|
673
|
-
/**
|
|
674
|
-
* Time Complexity: O(n)
|
|
675
|
-
* Space Complexity: O(n)
|
|
676
|
-
*
|
|
677
|
-
* The `toArray` function converts the elements of a data structure into an array.
|
|
678
|
-
* @returns The `toArray()` method is returning an array of elements of type `E`.
|
|
679
|
-
*/
|
|
680
|
-
toArray() {
|
|
681
|
-
return [...this];
|
|
682
|
-
}
|
|
683
755
|
/**
|
|
684
756
|
* Time Complexity: O(n)
|
|
685
757
|
* Space Complexity: O(n)
|
|
@@ -690,7 +762,11 @@ export class Deque extends IterableElementBase {
|
|
|
690
762
|
* elements as the original deque (`this`) and the same bucket size.
|
|
691
763
|
*/
|
|
692
764
|
clone() {
|
|
693
|
-
return new Deque(this, {
|
|
765
|
+
return new Deque(this, {
|
|
766
|
+
bucketSize: this.bucketSize,
|
|
767
|
+
toElementFn: this.toElementFn,
|
|
768
|
+
maxLen: this._maxLen
|
|
769
|
+
});
|
|
694
770
|
}
|
|
695
771
|
/**
|
|
696
772
|
* Time Complexity: O(n)
|
|
@@ -709,7 +785,11 @@ export class Deque extends IterableElementBase {
|
|
|
709
785
|
* satisfy the given predicate function.
|
|
710
786
|
*/
|
|
711
787
|
filter(predicate, thisArg) {
|
|
712
|
-
const newDeque =
|
|
788
|
+
const newDeque = this._createInstance({
|
|
789
|
+
bucketSize: this._bucketSize,
|
|
790
|
+
toElementFn: this.toElementFn,
|
|
791
|
+
maxLen: this._maxLen
|
|
792
|
+
});
|
|
713
793
|
let index = 0;
|
|
714
794
|
for (const el of this) {
|
|
715
795
|
if (predicate.call(thisArg, el, index, this)) {
|
|
@@ -738,7 +818,7 @@ export class Deque extends IterableElementBase {
|
|
|
738
818
|
* @returns a new Deque object with elements of type EM and raw elements of type RM.
|
|
739
819
|
*/
|
|
740
820
|
map(callback, toElementFn, thisArg) {
|
|
741
|
-
const newDeque = new Deque([], { bucketSize: this._bucketSize, toElementFn });
|
|
821
|
+
const newDeque = new Deque([], { bucketSize: this._bucketSize, toElementFn, maxLen: this._maxLen });
|
|
742
822
|
let index = 0;
|
|
743
823
|
for (const el of this) {
|
|
744
824
|
newDeque.push(callback.call(thisArg, el, index, this));
|
|
@@ -754,7 +834,7 @@ export class Deque extends IterableElementBase {
|
|
|
754
834
|
* object to be iterated over using a for...of loop.
|
|
755
835
|
*/
|
|
756
836
|
*_getIterator() {
|
|
757
|
-
for (let i = 0; i < this.
|
|
837
|
+
for (let i = 0; i < this._length; ++i) {
|
|
758
838
|
yield this.at(i);
|
|
759
839
|
}
|
|
760
840
|
}
|
|
@@ -811,5 +891,25 @@ export class Deque extends IterableElementBase {
|
|
|
811
891
|
}
|
|
812
892
|
return { bucketIndex, indexInBucket };
|
|
813
893
|
}
|
|
894
|
+
/**
|
|
895
|
+
* The function `_createInstance` returns a new instance of the `Deque` class with the specified
|
|
896
|
+
* options.
|
|
897
|
+
* @param [options] - The `options` parameter in the `_createInstance` method is of type
|
|
898
|
+
* `DequeOptions<E, R>`, which is an optional parameter that allows you to pass additional
|
|
899
|
+
* configuration options when creating a new instance of the `Deque` class.
|
|
900
|
+
* @returns An instance of the `Deque` class with an empty array and the provided options, casted as
|
|
901
|
+
* `this`.
|
|
902
|
+
*/
|
|
903
|
+
_createInstance(options) {
|
|
904
|
+
return new Deque([], options);
|
|
905
|
+
}
|
|
906
|
+
/**
|
|
907
|
+
* This function returns an iterator that iterates over elements in reverse order.
|
|
908
|
+
*/
|
|
909
|
+
*_getReverseIterator() {
|
|
910
|
+
for (let i = this._length - 1; i > -1; i--) {
|
|
911
|
+
yield this.at(i);
|
|
912
|
+
}
|
|
913
|
+
}
|
|
814
914
|
}
|
|
815
915
|
//# sourceMappingURL=deque.js.map
|