data-structure-typed 1.50.1 → 1.50.3
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 +2 -1
- package/README.md +30 -28
- package/README_zh-CN.md +1 -1
- package/SPECIFICATION.md +56 -0
- package/SPONSOR-zh-CN.md +90 -0
- package/SPONSOR.md +54 -0
- package/benchmark/report.html +13 -13
- package/benchmark/report.json +149 -161
- package/dist/cjs/data-structures/base/iterable-base.d.ts +120 -9
- package/dist/cjs/data-structures/base/iterable-base.js +143 -7
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +72 -47
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +101 -72
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js +22 -0
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +244 -199
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +484 -376
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +92 -79
- package/dist/cjs/data-structures/binary-tree/bst.js +68 -76
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +127 -57
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +152 -99
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/segment-tree.d.ts +99 -6
- package/dist/cjs/data-structures/binary-tree/segment-tree.js +127 -10
- package/dist/cjs/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +72 -58
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +102 -85
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +1 -78
- package/dist/cjs/data-structures/graph/abstract-graph.js +3 -189
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +73 -0
- package/dist/cjs/data-structures/graph/directed-graph.js +131 -0
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/map-graph.d.ts +8 -0
- package/dist/cjs/data-structures/graph/map-graph.js +14 -0
- package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +76 -7
- package/dist/cjs/data-structures/graph/undirected-graph.js +151 -18
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +254 -28
- package/dist/cjs/data-structures/hash/hash-map.js +347 -78
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +95 -25
- package/dist/cjs/data-structures/heap/heap.js +95 -26
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +126 -63
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +141 -77
- 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 +154 -106
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +164 -115
- 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 +63 -36
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js +63 -36
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/matrix/matrix.d.ts +35 -4
- package/dist/cjs/data-structures/matrix/matrix.js +50 -11
- package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +10 -0
- 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 -0
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +11 -0
- 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 +8 -0
- package/dist/cjs/data-structures/priority-queue/priority-queue.js +8 -0
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +139 -35
- package/dist/cjs/data-structures/queue/deque.js +200 -62
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +103 -49
- package/dist/cjs/data-structures/queue/queue.js +111 -49
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +51 -21
- package/dist/cjs/data-structures/stack/stack.js +58 -22
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/tree/tree.d.ts +57 -3
- package/dist/cjs/data-structures/tree/tree.js +77 -11
- package/dist/cjs/data-structures/tree/tree.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +135 -34
- package/dist/cjs/data-structures/trie/trie.js +153 -33
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +4 -3
- package/dist/cjs/types/utils/utils.d.ts +1 -0
- package/dist/mjs/data-structures/base/iterable-base.d.ts +120 -9
- package/dist/mjs/data-structures/base/iterable-base.js +143 -7
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +72 -47
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +102 -73
- package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
- package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.js +22 -0
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +244 -199
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +483 -375
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +92 -79
- package/dist/mjs/data-structures/binary-tree/bst.js +68 -76
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +127 -57
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +153 -100
- package/dist/mjs/data-structures/binary-tree/segment-tree.d.ts +99 -6
- package/dist/mjs/data-structures/binary-tree/segment-tree.js +127 -10
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +72 -58
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +102 -86
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +1 -78
- package/dist/mjs/data-structures/graph/abstract-graph.js +3 -188
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +73 -0
- package/dist/mjs/data-structures/graph/directed-graph.js +131 -0
- package/dist/mjs/data-structures/graph/map-graph.d.ts +8 -0
- package/dist/mjs/data-structures/graph/map-graph.js +14 -0
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +76 -7
- package/dist/mjs/data-structures/graph/undirected-graph.js +152 -19
- package/dist/mjs/data-structures/hash/hash-map.d.ts +254 -28
- package/dist/mjs/data-structures/hash/hash-map.js +353 -84
- package/dist/mjs/data-structures/heap/heap.d.ts +95 -25
- package/dist/mjs/data-structures/heap/heap.js +95 -26
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +126 -63
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +144 -80
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +154 -106
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +166 -117
- package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +63 -36
- package/dist/mjs/data-structures/linked-list/skip-linked-list.js +63 -36
- package/dist/mjs/data-structures/matrix/matrix.d.ts +35 -4
- package/dist/mjs/data-structures/matrix/matrix.js +50 -11
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +10 -0
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +11 -0
- package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +8 -0
- package/dist/mjs/data-structures/priority-queue/priority-queue.js +8 -0
- package/dist/mjs/data-structures/queue/deque.d.ts +139 -35
- package/dist/mjs/data-structures/queue/deque.js +205 -67
- package/dist/mjs/data-structures/queue/queue.d.ts +103 -49
- package/dist/mjs/data-structures/queue/queue.js +111 -49
- package/dist/mjs/data-structures/stack/stack.d.ts +51 -21
- package/dist/mjs/data-structures/stack/stack.js +58 -22
- package/dist/mjs/data-structures/tree/tree.d.ts +57 -3
- package/dist/mjs/data-structures/tree/tree.js +80 -14
- package/dist/mjs/data-structures/trie/trie.d.ts +135 -34
- package/dist/mjs/data-structures/trie/trie.js +156 -36
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +4 -3
- package/dist/mjs/types/utils/utils.d.ts +1 -0
- package/dist/umd/data-structure-typed.js +3121 -1583
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +61 -55
- package/src/data-structures/base/iterable-base.ts +184 -19
- package/src/data-structures/binary-tree/avl-tree.ts +134 -100
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +22 -0
- package/src/data-structures/binary-tree/binary-tree.ts +674 -671
- package/src/data-structures/binary-tree/bst.ts +127 -136
- package/src/data-structures/binary-tree/rb-tree.ts +199 -166
- package/src/data-structures/binary-tree/segment-tree.ts +145 -11
- package/src/data-structures/binary-tree/tree-multimap.ts +138 -115
- package/src/data-structures/graph/abstract-graph.ts +4 -211
- package/src/data-structures/graph/directed-graph.ts +152 -0
- package/src/data-structures/graph/map-graph.ts +15 -0
- package/src/data-structures/graph/undirected-graph.ts +171 -19
- package/src/data-structures/hash/hash-map.ts +389 -96
- package/src/data-structures/heap/heap.ts +97 -26
- package/src/data-structures/linked-list/doubly-linked-list.ts +156 -83
- package/src/data-structures/linked-list/singly-linked-list.ts +174 -120
- package/src/data-structures/linked-list/skip-linked-list.ts +63 -37
- package/src/data-structures/matrix/matrix.ts +52 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +10 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -0
- package/src/data-structures/priority-queue/priority-queue.ts +8 -0
- package/src/data-structures/queue/deque.ts +225 -70
- package/src/data-structures/queue/queue.ts +118 -49
- package/src/data-structures/stack/stack.ts +63 -23
- package/src/data-structures/tree/tree.ts +89 -15
- package/src/data-structures/trie/trie.ts +173 -38
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/hash/hash-map.ts +4 -3
- package/src/types/utils/utils.ts +2 -0
- package/test/performance/data-structures/graph/directed-graph.test.ts +4 -4
- package/test/performance/data-structures/queue/deque.test.ts +26 -25
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +37 -0
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +46 -17
- package/test/unit/data-structures/binary-tree/bst.test.ts +65 -1
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +38 -1
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +37 -32
- package/test/unit/data-structures/graph/abstract-graph.test.ts +11 -0
- package/test/unit/data-structures/graph/directed-graph.test.ts +190 -5
- package/test/unit/data-structures/graph/undirected-graph.test.ts +263 -18
- package/test/unit/data-structures/hash/hash-map.test.ts +376 -353
- package/test/unit/data-structures/heap/heap.test.ts +18 -1
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +24 -5
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +20 -2
- package/test/unit/data-structures/linked-list/skip-list.test.ts +1 -1
- package/test/unit/data-structures/queue/deque.test.ts +65 -5
- package/test/unit/data-structures/queue/queue.test.ts +24 -5
- package/test/unit/data-structures/stack/stack.test.ts +17 -0
- package/test/unit/data-structures/trie/trie.test.ts +17 -0
|
@@ -8,12 +8,17 @@ import { calcMinUnitsRequired, rangeCheck } from '../../utils';
|
|
|
8
8
|
* 5. Performance jitter: Deque may experience performance jitter, but DoublyLinkedList will not
|
|
9
9
|
*/
|
|
10
10
|
export class Deque extends IterableElementBase {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
11
|
+
/**
|
|
12
|
+
* The constructor initializes a Deque object with an optional iterable of elements and options.
|
|
13
|
+
* @param elements - An iterable object (such as an array or a Set) that contains the initial
|
|
14
|
+
* elements to be added to the deque. It can also be an object with a `length` or `size` property
|
|
15
|
+
* that represents the number of elements in the iterable object. If no elements are provided, an
|
|
16
|
+
* empty deque
|
|
17
|
+
* @param {DequeOptions} [options] - The `options` parameter is an optional object that can contain
|
|
18
|
+
* configuration options for the deque. In this code, it is used to set the `bucketSize` option,
|
|
19
|
+
* which determines the size of each bucket in the deque. If the `bucketSize` option is not provided
|
|
20
|
+
* or is not a number
|
|
21
|
+
*/
|
|
17
22
|
constructor(elements = [], options) {
|
|
18
23
|
super();
|
|
19
24
|
if (options) {
|
|
@@ -45,11 +50,70 @@ export class Deque extends IterableElementBase {
|
|
|
45
50
|
this.push(element);
|
|
46
51
|
}
|
|
47
52
|
}
|
|
53
|
+
_bucketSize = 1 << 12;
|
|
54
|
+
/**
|
|
55
|
+
* The bucketSize function returns the size of the bucket.
|
|
56
|
+
*
|
|
57
|
+
* @return The size of the bucket
|
|
58
|
+
*/
|
|
59
|
+
get bucketSize() {
|
|
60
|
+
return this._bucketSize;
|
|
61
|
+
}
|
|
62
|
+
_bucketFirst = 0;
|
|
63
|
+
/**
|
|
64
|
+
* The function returns the value of the protected variable `_bucketFirst`.
|
|
65
|
+
* @returns The value of the `_bucketFirst` property.
|
|
66
|
+
*/
|
|
67
|
+
get bucketFirst() {
|
|
68
|
+
return this._bucketFirst;
|
|
69
|
+
}
|
|
70
|
+
_firstInBucket = 0;
|
|
71
|
+
/**
|
|
72
|
+
* The function returns the value of the protected variable _firstInBucket.
|
|
73
|
+
* @returns The method is returning the value of the variable `_firstInBucket`, which is of type
|
|
74
|
+
* `number`.
|
|
75
|
+
*/
|
|
76
|
+
get firstInBucket() {
|
|
77
|
+
return this._firstInBucket;
|
|
78
|
+
}
|
|
79
|
+
_bucketLast = 0;
|
|
80
|
+
/**
|
|
81
|
+
* The function returns the value of the protected variable `_bucketLast`.
|
|
82
|
+
* @returns The value of the `_bucketLast` property, which is a number.
|
|
83
|
+
*/
|
|
84
|
+
get bucketLast() {
|
|
85
|
+
return this._bucketLast;
|
|
86
|
+
}
|
|
87
|
+
_lastInBucket = 0;
|
|
88
|
+
/**
|
|
89
|
+
* The function returns the value of the protected variable _lastInBucket.
|
|
90
|
+
* @returns The method is returning the value of the variable `_lastInBucket`, which is of type
|
|
91
|
+
* `number`.
|
|
92
|
+
*/
|
|
93
|
+
get lastInBucket() {
|
|
94
|
+
return this._lastInBucket;
|
|
95
|
+
}
|
|
96
|
+
_bucketCount = 0;
|
|
97
|
+
/**
|
|
98
|
+
* The function returns the number of buckets.
|
|
99
|
+
* @returns The number of buckets.
|
|
100
|
+
*/
|
|
101
|
+
get bucketCount() {
|
|
102
|
+
return this._bucketCount;
|
|
103
|
+
}
|
|
48
104
|
_buckets = [];
|
|
105
|
+
/**
|
|
106
|
+
* The buckets function returns the buckets property of the object.
|
|
107
|
+
* @return The buckets property
|
|
108
|
+
*/
|
|
49
109
|
get buckets() {
|
|
50
110
|
return this._buckets;
|
|
51
111
|
}
|
|
52
112
|
_size = 0;
|
|
113
|
+
/**
|
|
114
|
+
* The size function returns the number of items in the stack.
|
|
115
|
+
* @return The number of values in the set
|
|
116
|
+
*/
|
|
53
117
|
get size() {
|
|
54
118
|
return this._size;
|
|
55
119
|
}
|
|
@@ -63,6 +127,10 @@ export class Deque extends IterableElementBase {
|
|
|
63
127
|
return;
|
|
64
128
|
return this._buckets[this._bucketFirst][this._firstInBucket];
|
|
65
129
|
}
|
|
130
|
+
/**
|
|
131
|
+
* The last function returns the last element in the queue.
|
|
132
|
+
* @return The last element in the array
|
|
133
|
+
*/
|
|
66
134
|
get last() {
|
|
67
135
|
if (this.size === 0)
|
|
68
136
|
return;
|
|
@@ -201,13 +269,27 @@ export class Deque extends IterableElementBase {
|
|
|
201
269
|
return element;
|
|
202
270
|
}
|
|
203
271
|
/**
|
|
204
|
-
* Time Complexity: O(1)
|
|
205
|
-
* Space Complexity: O(1)
|
|
272
|
+
* Time Complexity: O(1)
|
|
273
|
+
* Space Complexity: O(1)
|
|
274
|
+
*/
|
|
275
|
+
/**
|
|
276
|
+
* Time Complexity: O(1)
|
|
277
|
+
* Space Complexity: O(1)
|
|
278
|
+
*
|
|
279
|
+
* The function checks if the size of an object is equal to zero and returns a boolean value.
|
|
280
|
+
* @returns A boolean value indicating whether the size of the object is 0 or not.
|
|
206
281
|
*/
|
|
207
282
|
isEmpty() {
|
|
208
283
|
return this.size === 0;
|
|
209
284
|
}
|
|
210
285
|
/**
|
|
286
|
+
* Time Complexity: O(1)
|
|
287
|
+
* Space Complexity: O(1)
|
|
288
|
+
*/
|
|
289
|
+
/**
|
|
290
|
+
* Time Complexity: O(1)
|
|
291
|
+
* Space Complexity: O(1)
|
|
292
|
+
*
|
|
211
293
|
* The clear() function resets the state of the object by initializing all variables to their default
|
|
212
294
|
* values.
|
|
213
295
|
*/
|
|
@@ -223,7 +305,7 @@ export class Deque extends IterableElementBase {
|
|
|
223
305
|
*begin() {
|
|
224
306
|
let index = 0;
|
|
225
307
|
while (index < this.size) {
|
|
226
|
-
yield this.
|
|
308
|
+
yield this.at(index);
|
|
227
309
|
index++;
|
|
228
310
|
}
|
|
229
311
|
}
|
|
@@ -234,7 +316,7 @@ export class Deque extends IterableElementBase {
|
|
|
234
316
|
*reverseBegin() {
|
|
235
317
|
let index = this.size - 1;
|
|
236
318
|
while (index >= 0) {
|
|
237
|
-
yield this.
|
|
319
|
+
yield this.at(index);
|
|
238
320
|
index--;
|
|
239
321
|
}
|
|
240
322
|
}
|
|
@@ -246,13 +328,13 @@ export class Deque extends IterableElementBase {
|
|
|
246
328
|
* Time Complexity: O(1)
|
|
247
329
|
* Space Complexity: O(1)
|
|
248
330
|
*
|
|
249
|
-
* The `
|
|
331
|
+
* The `at` function retrieves an element at a specified position in an array-like data structure.
|
|
250
332
|
* @param {number} pos - The `pos` parameter represents the position of the element that you want to
|
|
251
333
|
* retrieve from the data structure. It is of type `number` and should be a valid index within the
|
|
252
334
|
* range of the data structure.
|
|
253
335
|
* @returns The element at the specified position in the data structure is being returned.
|
|
254
336
|
*/
|
|
255
|
-
|
|
337
|
+
at(pos) {
|
|
256
338
|
rangeCheck(pos, 0, this.size - 1);
|
|
257
339
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
258
340
|
return this._buckets[bucketIndex][indexInBucket];
|
|
@@ -310,9 +392,9 @@ export class Deque extends IterableElementBase {
|
|
|
310
392
|
else {
|
|
311
393
|
const arr = [];
|
|
312
394
|
for (let i = pos; i < this.size; ++i) {
|
|
313
|
-
arr.push(this.
|
|
395
|
+
arr.push(this.at(i));
|
|
314
396
|
}
|
|
315
|
-
this.cut(pos - 1);
|
|
397
|
+
this.cut(pos - 1, true);
|
|
316
398
|
for (let i = 0; i < num; ++i)
|
|
317
399
|
this.push(element);
|
|
318
400
|
for (let i = 0; i < arr.length; ++i)
|
|
@@ -332,26 +414,75 @@ export class Deque extends IterableElementBase {
|
|
|
332
414
|
* updated size.
|
|
333
415
|
* @param {number} pos - The `pos` parameter represents the position at which the string should be
|
|
334
416
|
* cut. It is a number that indicates the index of the character where the cut should be made.
|
|
417
|
+
* @param {boolean} isCutSelf - If true, the original deque will not be cut, and return a new deque
|
|
335
418
|
* @returns The method is returning the updated size of the data structure.
|
|
336
419
|
*/
|
|
337
|
-
cut(pos) {
|
|
338
|
-
if (
|
|
339
|
-
|
|
340
|
-
|
|
420
|
+
cut(pos, isCutSelf = false) {
|
|
421
|
+
if (isCutSelf) {
|
|
422
|
+
if (pos < 0) {
|
|
423
|
+
this.clear();
|
|
424
|
+
return this;
|
|
425
|
+
}
|
|
426
|
+
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
427
|
+
this._bucketLast = bucketIndex;
|
|
428
|
+
this._lastInBucket = indexInBucket;
|
|
429
|
+
this._size = pos + 1;
|
|
430
|
+
return this;
|
|
431
|
+
}
|
|
432
|
+
else {
|
|
433
|
+
const newDeque = new Deque([], { bucketSize: this._bucketSize });
|
|
434
|
+
for (let i = 0; i <= pos; i++) {
|
|
435
|
+
newDeque.push(this.at(i));
|
|
436
|
+
}
|
|
437
|
+
return newDeque;
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
/**
|
|
441
|
+
* Time Complexity: O(1)
|
|
442
|
+
* Space Complexity: O(1) or O(n)
|
|
443
|
+
*/
|
|
444
|
+
/**
|
|
445
|
+
* Time Complexity: O(1)
|
|
446
|
+
* Space Complexity: O(1) or O(n)
|
|
447
|
+
*
|
|
448
|
+
* The `cutRest` function cuts the elements from a specified position in a deque and returns a new
|
|
449
|
+
* deque with the cut elements.
|
|
450
|
+
* @param {number} pos - The `pos` parameter represents the position from which to cut the Deque. It
|
|
451
|
+
* is a number that indicates the index of the element in the Deque where the cut should start.
|
|
452
|
+
* @param [isCutSelf=false] - isCutSelf is a boolean parameter that determines whether the original
|
|
453
|
+
* Deque should be modified or a new Deque should be created. If isCutSelf is true, the original
|
|
454
|
+
* Deque will be modified by cutting off elements starting from the specified position. If isCutSelf
|
|
455
|
+
* is false, a new De
|
|
456
|
+
* @returns The function `cutRest` returns either the modified original deque (`this`) or a new deque
|
|
457
|
+
* (`newDeque`) depending on the value of the `isCutSelf` parameter.
|
|
458
|
+
*/
|
|
459
|
+
cutRest(pos, isCutSelf = false) {
|
|
460
|
+
if (isCutSelf) {
|
|
461
|
+
if (pos < 0) {
|
|
462
|
+
this.clear();
|
|
463
|
+
return this;
|
|
464
|
+
}
|
|
465
|
+
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
466
|
+
this._bucketFirst = bucketIndex;
|
|
467
|
+
this._firstInBucket = indexInBucket;
|
|
468
|
+
this._size = this._size - pos;
|
|
469
|
+
return this;
|
|
470
|
+
}
|
|
471
|
+
else {
|
|
472
|
+
const newDeque = new Deque([], { bucketSize: this._bucketSize });
|
|
473
|
+
for (let i = pos; i < this.size; i++) {
|
|
474
|
+
newDeque.push(this.at(i));
|
|
475
|
+
}
|
|
476
|
+
return newDeque;
|
|
341
477
|
}
|
|
342
|
-
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
343
|
-
this._bucketLast = bucketIndex;
|
|
344
|
-
this._lastInBucket = indexInBucket;
|
|
345
|
-
this._size = pos + 1;
|
|
346
|
-
return this.size;
|
|
347
478
|
}
|
|
348
479
|
/**
|
|
349
480
|
* Time Complexity: O(n)
|
|
350
|
-
* Space Complexity: O(1)
|
|
481
|
+
* Space Complexity: O(1) or O(n)
|
|
351
482
|
*/
|
|
352
483
|
/**
|
|
353
484
|
* Time Complexity: O(n)
|
|
354
|
-
* Space Complexity: O(1)
|
|
485
|
+
* Space Complexity: O(1) or O(n)
|
|
355
486
|
*
|
|
356
487
|
* The `deleteAt` function removes an element at a specified position in an array-like data
|
|
357
488
|
* structure.
|
|
@@ -400,14 +531,14 @@ export class Deque extends IterableElementBase {
|
|
|
400
531
|
let i = 0;
|
|
401
532
|
let index = 0;
|
|
402
533
|
while (i < size) {
|
|
403
|
-
const oldElement = this.
|
|
534
|
+
const oldElement = this.at(i);
|
|
404
535
|
if (oldElement !== element) {
|
|
405
536
|
this.setAt(index, oldElement);
|
|
406
537
|
index += 1;
|
|
407
538
|
}
|
|
408
539
|
i += 1;
|
|
409
540
|
}
|
|
410
|
-
this.cut(index - 1);
|
|
541
|
+
this.cut(index - 1, true);
|
|
411
542
|
return true;
|
|
412
543
|
}
|
|
413
544
|
/**
|
|
@@ -451,15 +582,15 @@ export class Deque extends IterableElementBase {
|
|
|
451
582
|
return this;
|
|
452
583
|
}
|
|
453
584
|
let index = 1;
|
|
454
|
-
let prev = this.
|
|
585
|
+
let prev = this.at(0);
|
|
455
586
|
for (let i = 1; i < this.size; ++i) {
|
|
456
|
-
const cur = this.
|
|
587
|
+
const cur = this.at(i);
|
|
457
588
|
if (cur !== prev) {
|
|
458
589
|
prev = cur;
|
|
459
590
|
this.setAt(index++, cur);
|
|
460
591
|
}
|
|
461
592
|
}
|
|
462
|
-
this.cut(index - 1);
|
|
593
|
+
this.cut(index - 1, true);
|
|
463
594
|
return this;
|
|
464
595
|
}
|
|
465
596
|
/**
|
|
@@ -479,7 +610,7 @@ export class Deque extends IterableElementBase {
|
|
|
479
610
|
sort(comparator) {
|
|
480
611
|
const arr = [];
|
|
481
612
|
for (let i = 0; i < this.size; ++i) {
|
|
482
|
-
arr.push(this.
|
|
613
|
+
arr.push(this.at(i));
|
|
483
614
|
}
|
|
484
615
|
arr.sort(comparator);
|
|
485
616
|
for (let i = 0; i < this.size; ++i) {
|
|
@@ -523,30 +654,6 @@ export class Deque extends IterableElementBase {
|
|
|
523
654
|
this._bucketLast = newBuckets.length - 1;
|
|
524
655
|
this._buckets = newBuckets;
|
|
525
656
|
}
|
|
526
|
-
/**
|
|
527
|
-
* Time Complexity: O(n)
|
|
528
|
-
* Space Complexity: O(1)
|
|
529
|
-
*/
|
|
530
|
-
/**
|
|
531
|
-
* Time Complexity: O(n)
|
|
532
|
-
* Space Complexity: O(1)
|
|
533
|
-
*
|
|
534
|
-
* The `find` function iterates over the elements in a deque and returns the first element for which
|
|
535
|
-
* the callback function returns true, or undefined if no such element is found.
|
|
536
|
-
* @param callback - A function that takes three parameters: element, index, and deque. It should
|
|
537
|
-
* return a boolean value indicating whether the element satisfies a certain condition.
|
|
538
|
-
* @returns The method `find` returns the first element in the deque that satisfies the condition
|
|
539
|
-
* specified by the callback function. If no element satisfies the condition, it returns `undefined`.
|
|
540
|
-
*/
|
|
541
|
-
find(callback) {
|
|
542
|
-
for (let i = 0; i < this.size; ++i) {
|
|
543
|
-
const element = this.getAt(i);
|
|
544
|
-
if (callback(element, i, this)) {
|
|
545
|
-
return element;
|
|
546
|
-
}
|
|
547
|
-
}
|
|
548
|
-
return;
|
|
549
|
-
}
|
|
550
657
|
/**
|
|
551
658
|
* Time Complexity: O(n)
|
|
552
659
|
* Space Complexity: O(1)
|
|
@@ -564,7 +671,7 @@ export class Deque extends IterableElementBase {
|
|
|
564
671
|
*/
|
|
565
672
|
indexOf(element) {
|
|
566
673
|
for (let i = 0; i < this.size; ++i) {
|
|
567
|
-
if (this.
|
|
674
|
+
if (this.at(i) === element) {
|
|
568
675
|
return i;
|
|
569
676
|
}
|
|
570
677
|
}
|
|
@@ -584,6 +691,22 @@ export class Deque extends IterableElementBase {
|
|
|
584
691
|
toArray() {
|
|
585
692
|
return [...this];
|
|
586
693
|
}
|
|
694
|
+
/**
|
|
695
|
+
* Time Complexity: O(n)
|
|
696
|
+
* Space Complexity: O(n)
|
|
697
|
+
*/
|
|
698
|
+
/**
|
|
699
|
+
* Time Complexity: O(n)
|
|
700
|
+
* Space Complexity: O(n)
|
|
701
|
+
*
|
|
702
|
+
* The `clone()` function returns a new instance of the `Deque` class with the same elements and
|
|
703
|
+
* bucket size as the original instance.
|
|
704
|
+
* @returns The `clone()` method is returning a new instance of the `Deque` class with the same
|
|
705
|
+
* elements as the original deque (`this`) and the same bucket size.
|
|
706
|
+
*/
|
|
707
|
+
clone() {
|
|
708
|
+
return new Deque([...this], { bucketSize: this.bucketSize });
|
|
709
|
+
}
|
|
587
710
|
/**
|
|
588
711
|
* Time Complexity: O(n)
|
|
589
712
|
* Space Complexity: O(n)
|
|
@@ -646,8 +769,8 @@ export class Deque extends IterableElementBase {
|
|
|
646
769
|
* Space Complexity: O(n) - Due to potential resizing.
|
|
647
770
|
*/
|
|
648
771
|
/**
|
|
649
|
-
* Time Complexity: O(1)
|
|
650
|
-
* Space Complexity: O(n) -
|
|
772
|
+
* Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
|
|
773
|
+
* Space Complexity: O(n) - Due to potential resizing.
|
|
651
774
|
*
|
|
652
775
|
* The addLast function adds an element to the end of an array.
|
|
653
776
|
* @param {E} element - The element parameter represents the element that you want to add to the end of the
|
|
@@ -657,12 +780,12 @@ export class Deque extends IterableElementBase {
|
|
|
657
780
|
return this.push(element);
|
|
658
781
|
}
|
|
659
782
|
/**
|
|
660
|
-
* Time Complexity: O(1)
|
|
661
|
-
* Space Complexity: O(1)
|
|
783
|
+
* Time Complexity: O(1)
|
|
784
|
+
* Space Complexity: O(1)
|
|
662
785
|
*/
|
|
663
786
|
/**
|
|
664
|
-
* Time Complexity: O(1)
|
|
665
|
-
* Space Complexity: O(1)
|
|
787
|
+
* Time Complexity: O(1)
|
|
788
|
+
* Space Complexity: O(1)
|
|
666
789
|
*
|
|
667
790
|
* The function "pollLast" removes and returns the last element of an array.
|
|
668
791
|
* @returns The last element of the array is being returned.
|
|
@@ -671,8 +794,13 @@ export class Deque extends IterableElementBase {
|
|
|
671
794
|
return this.pop();
|
|
672
795
|
}
|
|
673
796
|
/**
|
|
674
|
-
* Time Complexity: O(1)
|
|
675
|
-
* Space Complexity: O(
|
|
797
|
+
* Time Complexity: O(1)
|
|
798
|
+
* Space Complexity: O(1)
|
|
799
|
+
* /
|
|
800
|
+
|
|
801
|
+
/**
|
|
802
|
+
* Time Complexity: O(1)
|
|
803
|
+
* Space Complexity: O(1)
|
|
676
804
|
*
|
|
677
805
|
* The "addFirst" function adds an element to the beginning of an array.
|
|
678
806
|
* @param {E} element - The parameter "element" represents the element that you want to add to the
|
|
@@ -682,8 +810,13 @@ export class Deque extends IterableElementBase {
|
|
|
682
810
|
return this.unshift(element);
|
|
683
811
|
}
|
|
684
812
|
/**
|
|
685
|
-
* Time Complexity: O(1)
|
|
686
|
-
* Space Complexity: O(1)
|
|
813
|
+
* Time Complexity: O(1)
|
|
814
|
+
* Space Complexity: O(1)
|
|
815
|
+
* /
|
|
816
|
+
|
|
817
|
+
/**
|
|
818
|
+
* Time Complexity: O(1)
|
|
819
|
+
* Space Complexity: O(1)
|
|
687
820
|
*
|
|
688
821
|
* The function "pollFirst" removes and returns the first element of an array.
|
|
689
822
|
* @returns The method `pollFirst()` is returning the first element of the array after removing it
|
|
@@ -693,6 +826,11 @@ export class Deque extends IterableElementBase {
|
|
|
693
826
|
return this.shift();
|
|
694
827
|
}
|
|
695
828
|
/**
|
|
829
|
+
* Time Complexity: O(n)
|
|
830
|
+
* Space Complexity: O(1)
|
|
831
|
+
* /
|
|
832
|
+
|
|
833
|
+
/**
|
|
696
834
|
* Time Complexity: O(n)
|
|
697
835
|
* Space Complexity: O(1)
|
|
698
836
|
*
|
|
@@ -701,7 +839,7 @@ export class Deque extends IterableElementBase {
|
|
|
701
839
|
*/
|
|
702
840
|
*_getIterator() {
|
|
703
841
|
for (let i = 0; i < this.size; ++i) {
|
|
704
|
-
yield this.
|
|
842
|
+
yield this.at(i);
|
|
705
843
|
}
|
|
706
844
|
}
|
|
707
845
|
/**
|