data-structure-typed 1.45.3 → 1.46.1
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/README.md +91 -40
- package/benchmark/report.html +17 -17
- package/benchmark/report.json +160 -142
- package/dist/cjs/data-structures/hash/hash-map.d.ts +6 -5
- package/dist/cjs/data-structures/hash/hash-map.js +9 -6
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +59 -47
- package/dist/cjs/data-structures/heap/heap.js +133 -123
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +131 -164
- package/dist/cjs/data-structures/queue/deque.js +543 -211
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +0 -9
- package/dist/cjs/types/helpers.d.ts +11 -0
- package/dist/cjs/utils/utils.d.ts +1 -0
- package/dist/cjs/utils/utils.js +3 -1
- package/dist/cjs/utils/utils.js.map +1 -1
- package/dist/mjs/data-structures/hash/hash-map.d.ts +6 -5
- package/dist/mjs/data-structures/hash/hash-map.js +9 -6
- package/dist/mjs/data-structures/heap/heap.d.ts +59 -47
- package/dist/mjs/data-structures/heap/heap.js +133 -123
- package/dist/mjs/data-structures/queue/deque.d.ts +131 -164
- package/dist/mjs/data-structures/queue/deque.js +544 -201
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +0 -9
- package/dist/mjs/types/helpers.d.ts +11 -0
- package/dist/mjs/utils/utils.d.ts +1 -0
- package/dist/mjs/utils/utils.js +1 -0
- package/dist/umd/data-structure-typed.js +684 -328
- package/dist/umd/data-structure-typed.min.js +1 -1
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/hash/hash-map.ts +11 -7
- package/src/data-structures/heap/heap.ts +132 -128
- package/src/data-structures/queue/deque.ts +605 -226
- package/src/types/data-structures/hash/hash-map.ts +0 -11
- package/src/types/helpers.ts +15 -0
- package/src/utils/utils.ts +2 -0
- package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +21 -0
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +7 -7
- package/test/performance/data-structures/queue/deque.test.ts +24 -13
- package/test/unit/data-structures/hash/hash-map.test.ts +4 -4
- package/test/unit/data-structures/heap/heap.test.ts +2 -1
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +3 -3
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +3 -3
- package/test/unit/data-structures/queue/deque.test.ts +252 -240
- /package/test/performance/data-structures/{comparation.test.ts → comparison.test.ts} +0 -0
|
@@ -5,12 +5,534 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
import { calcMinUnitsRequired, rangeCheck, throwRangeError } from "../../utils";
|
|
9
|
+
/**
|
|
10
|
+
* Deque can provide random access with O(1) time complexity
|
|
11
|
+
* Deque is usually more compact and efficient in memory usage because it does not require additional space to store pointers.
|
|
12
|
+
* Deque may experience performance jitter, but DoublyLinkedList will not
|
|
13
|
+
* Deque is implemented using a dynamic array. Inserting or deleting beyond both ends of the array may require moving elements or reallocating space.
|
|
14
|
+
*/
|
|
15
|
+
export class DequeIterator {
|
|
16
|
+
iterateDirection;
|
|
17
|
+
index;
|
|
18
|
+
deque;
|
|
19
|
+
constructor(index, deque, iterateDirection = 0 /* IterateDirection.DEFAULT */) {
|
|
20
|
+
this.index = index;
|
|
21
|
+
this.iterateDirection = iterateDirection;
|
|
22
|
+
if (this.iterateDirection === 0 /* IterateDirection.DEFAULT */) {
|
|
23
|
+
this.prev = function () {
|
|
24
|
+
if (this.index === 0) {
|
|
25
|
+
throwRangeError();
|
|
26
|
+
}
|
|
27
|
+
this.index -= 1;
|
|
28
|
+
return this;
|
|
29
|
+
};
|
|
30
|
+
this.next = function () {
|
|
31
|
+
if (this.index === this.deque.size) {
|
|
32
|
+
throwRangeError();
|
|
33
|
+
}
|
|
34
|
+
this.index += 1;
|
|
35
|
+
return this;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
this.prev = function () {
|
|
40
|
+
if (this.index === this.deque.size - 1) {
|
|
41
|
+
throwRangeError();
|
|
42
|
+
}
|
|
43
|
+
this.index += 1;
|
|
44
|
+
return this;
|
|
45
|
+
};
|
|
46
|
+
this.next = function () {
|
|
47
|
+
if (this.index === -1) {
|
|
48
|
+
throwRangeError();
|
|
49
|
+
}
|
|
50
|
+
this.index -= 1;
|
|
51
|
+
return this;
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
this.deque = deque;
|
|
55
|
+
}
|
|
56
|
+
get current() {
|
|
57
|
+
return this.deque.getAt(this.index);
|
|
58
|
+
}
|
|
59
|
+
set current(newElement) {
|
|
60
|
+
this.deque.setAt(this.index, newElement);
|
|
61
|
+
}
|
|
62
|
+
isAccessible() {
|
|
63
|
+
return this.index !== this.deque.size;
|
|
64
|
+
}
|
|
65
|
+
prev() {
|
|
66
|
+
return this;
|
|
67
|
+
}
|
|
68
|
+
next() {
|
|
69
|
+
return this;
|
|
70
|
+
}
|
|
71
|
+
clone() {
|
|
72
|
+
return new DequeIterator(this.index, this.deque, this.iterateDirection);
|
|
73
|
+
}
|
|
12
74
|
}
|
|
13
|
-
|
|
75
|
+
export class Deque {
|
|
76
|
+
_bucketFirst = 0;
|
|
77
|
+
_firstInBucket = 0;
|
|
78
|
+
_bucketLast = 0;
|
|
79
|
+
_lastInBucket = 0;
|
|
80
|
+
_bucketCount = 0;
|
|
81
|
+
_bucketSize;
|
|
82
|
+
constructor(elements = [], bucketSize = (1 << 12)) {
|
|
83
|
+
let _size;
|
|
84
|
+
if ('length' in elements) {
|
|
85
|
+
_size = elements.length;
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
_size = elements.size;
|
|
89
|
+
}
|
|
90
|
+
this._bucketSize = bucketSize;
|
|
91
|
+
this._bucketCount = calcMinUnitsRequired(_size, this._bucketSize) || 1;
|
|
92
|
+
for (let i = 0; i < this._bucketCount; ++i) {
|
|
93
|
+
this._buckets.push(new Array(this._bucketSize));
|
|
94
|
+
}
|
|
95
|
+
const needBucketNum = calcMinUnitsRequired(_size, this._bucketSize);
|
|
96
|
+
this._bucketFirst = this._bucketLast = (this._bucketCount >> 1) - (needBucketNum >> 1);
|
|
97
|
+
this._firstInBucket = this._lastInBucket = (this._bucketSize - _size % this._bucketSize) >> 1;
|
|
98
|
+
for (const element of elements) {
|
|
99
|
+
this.push(element);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
_buckets = [];
|
|
103
|
+
get buckets() {
|
|
104
|
+
return this._buckets;
|
|
105
|
+
}
|
|
106
|
+
_size = 0;
|
|
107
|
+
get size() {
|
|
108
|
+
return this._size;
|
|
109
|
+
}
|
|
110
|
+
get first() {
|
|
111
|
+
if (this.size === 0)
|
|
112
|
+
return;
|
|
113
|
+
return this._buckets[this._bucketFirst][this._firstInBucket];
|
|
114
|
+
}
|
|
115
|
+
get last() {
|
|
116
|
+
if (this.size === 0)
|
|
117
|
+
return;
|
|
118
|
+
return this._buckets[this._bucketLast][this._lastInBucket];
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Time Complexity: Amortized O(1) - Generally constant time, but resizing when the deque is full leads to O(n).
|
|
122
|
+
* Space Complexity: O(n) - In worst case, resizing doubles the array size.
|
|
123
|
+
*/
|
|
124
|
+
empty() {
|
|
125
|
+
return this._size === 0;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Time Complexity: O(1) - Removes the last element.
|
|
129
|
+
* Space Complexity: O(1) - Operates in-place.
|
|
130
|
+
*/
|
|
131
|
+
isEmpty() {
|
|
132
|
+
return this.size === 0;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
|
|
136
|
+
* Space Complexity: O(n) - Due to potential resizing.
|
|
137
|
+
*/
|
|
138
|
+
/**
|
|
139
|
+
* Time Complexity: O(1)
|
|
140
|
+
* Space Complexity: O(n) - In worst case, resizing doubles the array size.
|
|
141
|
+
*
|
|
142
|
+
* The addLast function adds an element to the end of an array.
|
|
143
|
+
* @param {E} element - The element parameter represents the element that you want to add to the end of the
|
|
144
|
+
* data structure.
|
|
145
|
+
*/
|
|
146
|
+
addLast(element) {
|
|
147
|
+
this.push(element);
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Time Complexity: O(1) - Removes the first element.
|
|
151
|
+
* Space Complexity: O(1) - In-place operation.
|
|
152
|
+
*/
|
|
153
|
+
/**
|
|
154
|
+
* Time Complexity: O(1) - Removes the last element.
|
|
155
|
+
* Space Complexity: O(1) - Operates in-place.
|
|
156
|
+
*
|
|
157
|
+
* The function "popLast" removes and returns the last element of an array.
|
|
158
|
+
* @returns The last element of the array is being returned.
|
|
159
|
+
*/
|
|
160
|
+
popLast() {
|
|
161
|
+
return this.pop();
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Time Complexity: O(1).
|
|
165
|
+
* Space Complexity: O(n) - Due to potential resizing.
|
|
166
|
+
*
|
|
167
|
+
* The "addFirst" function adds an element to the beginning of an array.
|
|
168
|
+
* @param {E} element - The parameter "element" represents the element that you want to add to the
|
|
169
|
+
* beginning of the data structure.
|
|
170
|
+
*/
|
|
171
|
+
addFirst(element) {
|
|
172
|
+
this.unshift(element);
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Time Complexity: O(1) - Removes the first element.
|
|
176
|
+
* Space Complexity: O(1) - In-place operation.
|
|
177
|
+
*
|
|
178
|
+
* The function "popFirst" removes and returns the first element of an array.
|
|
179
|
+
* @returns The method `popFirst()` is returning the first element of the array after removing it
|
|
180
|
+
* from the beginning. If the array is empty, it will return `undefined`.
|
|
181
|
+
*/
|
|
182
|
+
popFirst() {
|
|
183
|
+
return this.shift();
|
|
184
|
+
}
|
|
185
|
+
clear() {
|
|
186
|
+
this._buckets = [new Array(this._bucketSize)];
|
|
187
|
+
this._bucketCount = 1;
|
|
188
|
+
this._bucketFirst = this._bucketLast = this._size = 0;
|
|
189
|
+
this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
|
|
190
|
+
}
|
|
191
|
+
begin() {
|
|
192
|
+
return new DequeIterator(0, this);
|
|
193
|
+
}
|
|
194
|
+
end() {
|
|
195
|
+
return new DequeIterator(this.size, this);
|
|
196
|
+
}
|
|
197
|
+
reverseBegin() {
|
|
198
|
+
return new DequeIterator(this.size - 1, this, 1 /* IterateDirection.REVERSE */);
|
|
199
|
+
}
|
|
200
|
+
reverseEnd() {
|
|
201
|
+
return new DequeIterator(-1, this, 1 /* IterateDirection.REVERSE */);
|
|
202
|
+
}
|
|
203
|
+
push(element) {
|
|
204
|
+
if (this.size) {
|
|
205
|
+
if (this._lastInBucket < this._bucketSize - 1) {
|
|
206
|
+
this._lastInBucket += 1;
|
|
207
|
+
}
|
|
208
|
+
else if (this._bucketLast < this._bucketCount - 1) {
|
|
209
|
+
this._bucketLast += 1;
|
|
210
|
+
this._lastInBucket = 0;
|
|
211
|
+
}
|
|
212
|
+
else {
|
|
213
|
+
this._bucketLast = 0;
|
|
214
|
+
this._lastInBucket = 0;
|
|
215
|
+
}
|
|
216
|
+
if (this._bucketLast === this._bucketFirst &&
|
|
217
|
+
this._lastInBucket === this._firstInBucket)
|
|
218
|
+
this._reallocate();
|
|
219
|
+
}
|
|
220
|
+
this._size += 1;
|
|
221
|
+
this._buckets[this._bucketLast][this._lastInBucket] = element;
|
|
222
|
+
return this.size;
|
|
223
|
+
}
|
|
224
|
+
pop() {
|
|
225
|
+
if (this.size === 0)
|
|
226
|
+
return;
|
|
227
|
+
const element = this._buckets[this._bucketLast][this._lastInBucket];
|
|
228
|
+
if (this.size !== 1) {
|
|
229
|
+
if (this._lastInBucket > 0) {
|
|
230
|
+
this._lastInBucket -= 1;
|
|
231
|
+
}
|
|
232
|
+
else if (this._bucketLast > 0) {
|
|
233
|
+
this._bucketLast -= 1;
|
|
234
|
+
this._lastInBucket = this._bucketSize - 1;
|
|
235
|
+
}
|
|
236
|
+
else {
|
|
237
|
+
this._bucketLast = this._bucketCount - 1;
|
|
238
|
+
this._lastInBucket = this._bucketSize - 1;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
this._size -= 1;
|
|
242
|
+
return element;
|
|
243
|
+
}
|
|
244
|
+
unshift(element) {
|
|
245
|
+
if (this.size) {
|
|
246
|
+
if (this._firstInBucket > 0) {
|
|
247
|
+
this._firstInBucket -= 1;
|
|
248
|
+
}
|
|
249
|
+
else if (this._bucketFirst > 0) {
|
|
250
|
+
this._bucketFirst -= 1;
|
|
251
|
+
this._firstInBucket = this._bucketSize - 1;
|
|
252
|
+
}
|
|
253
|
+
else {
|
|
254
|
+
this._bucketFirst = this._bucketCount - 1;
|
|
255
|
+
this._firstInBucket = this._bucketSize - 1;
|
|
256
|
+
}
|
|
257
|
+
if (this._bucketFirst === this._bucketLast &&
|
|
258
|
+
this._firstInBucket === this._lastInBucket)
|
|
259
|
+
this._reallocate();
|
|
260
|
+
}
|
|
261
|
+
this._size += 1;
|
|
262
|
+
this._buckets[this._bucketFirst][this._firstInBucket] = element;
|
|
263
|
+
return this.size;
|
|
264
|
+
}
|
|
265
|
+
shift() {
|
|
266
|
+
if (this.size === 0)
|
|
267
|
+
return;
|
|
268
|
+
const element = this._buckets[this._bucketFirst][this._firstInBucket];
|
|
269
|
+
if (this.size !== 1) {
|
|
270
|
+
if (this._firstInBucket < this._bucketSize - 1) {
|
|
271
|
+
this._firstInBucket += 1;
|
|
272
|
+
}
|
|
273
|
+
else if (this._bucketFirst < this._bucketCount - 1) {
|
|
274
|
+
this._bucketFirst += 1;
|
|
275
|
+
this._firstInBucket = 0;
|
|
276
|
+
}
|
|
277
|
+
else {
|
|
278
|
+
this._bucketFirst = 0;
|
|
279
|
+
this._firstInBucket = 0;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
this._size -= 1;
|
|
283
|
+
return element;
|
|
284
|
+
}
|
|
285
|
+
getAt(pos) {
|
|
286
|
+
rangeCheck(pos, 0, this.size - 1);
|
|
287
|
+
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
288
|
+
return this._buckets[bucketIndex][indexInBucket];
|
|
289
|
+
}
|
|
290
|
+
setAt(pos, element) {
|
|
291
|
+
rangeCheck(pos, 0, this.size - 1);
|
|
292
|
+
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
293
|
+
this._buckets[bucketIndex][indexInBucket] = element;
|
|
294
|
+
}
|
|
295
|
+
insertAt(pos, element, num = 1) {
|
|
296
|
+
const length = this.size;
|
|
297
|
+
rangeCheck(pos, 0, length);
|
|
298
|
+
if (pos === 0) {
|
|
299
|
+
while (num--)
|
|
300
|
+
this.unshift(element);
|
|
301
|
+
}
|
|
302
|
+
else if (pos === this.size) {
|
|
303
|
+
while (num--)
|
|
304
|
+
this.push(element);
|
|
305
|
+
}
|
|
306
|
+
else {
|
|
307
|
+
const arr = [];
|
|
308
|
+
for (let i = pos; i < this.size; ++i) {
|
|
309
|
+
arr.push(this.getAt(i));
|
|
310
|
+
}
|
|
311
|
+
this.cut(pos - 1);
|
|
312
|
+
for (let i = 0; i < num; ++i)
|
|
313
|
+
this.push(element);
|
|
314
|
+
for (let i = 0; i < arr.length; ++i)
|
|
315
|
+
this.push(arr[i]);
|
|
316
|
+
}
|
|
317
|
+
return this.size;
|
|
318
|
+
}
|
|
319
|
+
cut(pos) {
|
|
320
|
+
if (pos < 0) {
|
|
321
|
+
this.clear();
|
|
322
|
+
return 0;
|
|
323
|
+
}
|
|
324
|
+
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
325
|
+
this._bucketLast = bucketIndex;
|
|
326
|
+
this._lastInBucket = indexInBucket;
|
|
327
|
+
this._size = pos + 1;
|
|
328
|
+
return this.size;
|
|
329
|
+
}
|
|
330
|
+
deleteAt(pos) {
|
|
331
|
+
rangeCheck(pos, 0, this.size - 1);
|
|
332
|
+
if (pos === 0)
|
|
333
|
+
this.shift();
|
|
334
|
+
else if (pos === this.size - 1)
|
|
335
|
+
this.pop();
|
|
336
|
+
else {
|
|
337
|
+
const length = this.size - 1;
|
|
338
|
+
let { bucketIndex: curBucket, indexInBucket: curPointer } = this._getBucketAndPosition(pos);
|
|
339
|
+
for (let i = pos; i < length; ++i) {
|
|
340
|
+
const { bucketIndex: nextBucket, indexInBucket: nextPointer } = this._getBucketAndPosition(pos + 1);
|
|
341
|
+
this._buckets[curBucket][curPointer] = this._buckets[nextBucket][nextPointer];
|
|
342
|
+
curBucket = nextBucket;
|
|
343
|
+
curPointer = nextPointer;
|
|
344
|
+
}
|
|
345
|
+
this.pop();
|
|
346
|
+
}
|
|
347
|
+
return this.size;
|
|
348
|
+
}
|
|
349
|
+
delete(element) {
|
|
350
|
+
const size = this.size;
|
|
351
|
+
if (size === 0)
|
|
352
|
+
return 0;
|
|
353
|
+
let i = 0;
|
|
354
|
+
let index = 0;
|
|
355
|
+
while (i < size) {
|
|
356
|
+
const oldElement = this.getAt(i);
|
|
357
|
+
if (oldElement !== element) {
|
|
358
|
+
this.setAt(index, oldElement);
|
|
359
|
+
index += 1;
|
|
360
|
+
}
|
|
361
|
+
i += 1;
|
|
362
|
+
}
|
|
363
|
+
this.cut(index - 1);
|
|
364
|
+
return this.size;
|
|
365
|
+
}
|
|
366
|
+
deleteByIterator(iter) {
|
|
367
|
+
const index = iter.index;
|
|
368
|
+
this.deleteAt(index);
|
|
369
|
+
iter = iter.next();
|
|
370
|
+
return iter;
|
|
371
|
+
}
|
|
372
|
+
findIterator(element) {
|
|
373
|
+
for (let i = 0; i < this.size; ++i) {
|
|
374
|
+
if (this.getAt(i) === element) {
|
|
375
|
+
return new DequeIterator(i, this);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
return this.end();
|
|
379
|
+
}
|
|
380
|
+
reverse() {
|
|
381
|
+
this._buckets.reverse().forEach(function (bucket) {
|
|
382
|
+
bucket.reverse();
|
|
383
|
+
});
|
|
384
|
+
const { _bucketFirst, _bucketLast, _firstInBucket, _lastInBucket } = this;
|
|
385
|
+
this._bucketFirst = this._bucketCount - _bucketLast - 1;
|
|
386
|
+
this._bucketLast = this._bucketCount - _bucketFirst - 1;
|
|
387
|
+
this._firstInBucket = this._bucketSize - _lastInBucket - 1;
|
|
388
|
+
this._lastInBucket = this._bucketSize - _firstInBucket - 1;
|
|
389
|
+
return this;
|
|
390
|
+
}
|
|
391
|
+
unique() {
|
|
392
|
+
if (this.size <= 1) {
|
|
393
|
+
return this.size;
|
|
394
|
+
}
|
|
395
|
+
let index = 1;
|
|
396
|
+
let prev = this.getAt(0);
|
|
397
|
+
for (let i = 1; i < this.size; ++i) {
|
|
398
|
+
const cur = this.getAt(i);
|
|
399
|
+
if (cur !== prev) {
|
|
400
|
+
prev = cur;
|
|
401
|
+
this.setAt(index++, cur);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
this.cut(index - 1);
|
|
405
|
+
return this.size;
|
|
406
|
+
}
|
|
407
|
+
sort(comparator) {
|
|
408
|
+
const arr = [];
|
|
409
|
+
for (let i = 0; i < this.size; ++i) {
|
|
410
|
+
arr.push(this.getAt(i));
|
|
411
|
+
}
|
|
412
|
+
arr.sort(comparator);
|
|
413
|
+
for (let i = 0; i < this.size; ++i) {
|
|
414
|
+
this.setAt(i, arr[i]);
|
|
415
|
+
}
|
|
416
|
+
return this;
|
|
417
|
+
}
|
|
418
|
+
shrinkToFit() {
|
|
419
|
+
if (this.size === 0)
|
|
420
|
+
return;
|
|
421
|
+
const newBuckets = [];
|
|
422
|
+
if (this._bucketFirst === this._bucketLast)
|
|
423
|
+
return;
|
|
424
|
+
else if (this._bucketFirst < this._bucketLast) {
|
|
425
|
+
for (let i = this._bucketFirst; i <= this._bucketLast; ++i) {
|
|
426
|
+
newBuckets.push(this._buckets[i]);
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
else {
|
|
430
|
+
for (let i = this._bucketFirst; i < this._bucketCount; ++i) {
|
|
431
|
+
newBuckets.push(this._buckets[i]);
|
|
432
|
+
}
|
|
433
|
+
for (let i = 0; i <= this._bucketLast; ++i) {
|
|
434
|
+
newBuckets.push(this._buckets[i]);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
this._bucketFirst = 0;
|
|
438
|
+
this._bucketLast = newBuckets.length - 1;
|
|
439
|
+
this._buckets = newBuckets;
|
|
440
|
+
}
|
|
441
|
+
forEach(callback) {
|
|
442
|
+
for (let i = 0; i < this.size; ++i) {
|
|
443
|
+
callback(this.getAt(i), i, this);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
find(callback) {
|
|
447
|
+
for (let i = 0; i < this.size; ++i) {
|
|
448
|
+
const element = this.getAt(i);
|
|
449
|
+
if (callback(element, i, this)) {
|
|
450
|
+
return element;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
return undefined;
|
|
454
|
+
}
|
|
455
|
+
toArray() {
|
|
456
|
+
const arr = [];
|
|
457
|
+
for (let i = 0; i < this.size; ++i) {
|
|
458
|
+
arr.push(this.getAt(i));
|
|
459
|
+
}
|
|
460
|
+
return arr;
|
|
461
|
+
}
|
|
462
|
+
map(callback) {
|
|
463
|
+
const newDeque = new Deque([], this._bucketSize);
|
|
464
|
+
for (let i = 0; i < this.size; ++i) {
|
|
465
|
+
newDeque.push(callback(this.getAt(i), i, this));
|
|
466
|
+
}
|
|
467
|
+
return newDeque;
|
|
468
|
+
}
|
|
469
|
+
filter(predicate) {
|
|
470
|
+
const newDeque = new Deque([], this._bucketSize);
|
|
471
|
+
for (let i = 0; i < this.size; ++i) {
|
|
472
|
+
const element = this.getAt(i);
|
|
473
|
+
if (predicate(element, i, this)) {
|
|
474
|
+
newDeque.push(element);
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
return newDeque;
|
|
478
|
+
}
|
|
479
|
+
reduce(callback, initialValue) {
|
|
480
|
+
let accumulator = initialValue;
|
|
481
|
+
for (let i = 0; i < this.size; ++i) {
|
|
482
|
+
accumulator = callback(accumulator, this.getAt(i), i, this);
|
|
483
|
+
}
|
|
484
|
+
return accumulator;
|
|
485
|
+
}
|
|
486
|
+
indexOf(element) {
|
|
487
|
+
for (let i = 0; i < this.size; ++i) {
|
|
488
|
+
if (this.getAt(i) === element) {
|
|
489
|
+
return i;
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
return -1;
|
|
493
|
+
}
|
|
494
|
+
*[Symbol.iterator]() {
|
|
495
|
+
for (let i = 0; i < this.size; ++i) {
|
|
496
|
+
yield this.getAt(i);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
_reallocate(needBucketNum) {
|
|
500
|
+
const newBuckets = [];
|
|
501
|
+
const addBucketNum = needBucketNum || this._bucketCount >> 1 || 1;
|
|
502
|
+
for (let i = 0; i < addBucketNum; ++i) {
|
|
503
|
+
newBuckets[i] = new Array(this._bucketSize);
|
|
504
|
+
}
|
|
505
|
+
for (let i = this._bucketFirst; i < this._bucketCount; ++i) {
|
|
506
|
+
newBuckets[newBuckets.length] = this._buckets[i];
|
|
507
|
+
}
|
|
508
|
+
for (let i = 0; i < this._bucketLast; ++i) {
|
|
509
|
+
newBuckets[newBuckets.length] = this._buckets[i];
|
|
510
|
+
}
|
|
511
|
+
newBuckets[newBuckets.length] = [...this._buckets[this._bucketLast]];
|
|
512
|
+
this._bucketFirst = addBucketNum;
|
|
513
|
+
this._bucketLast = newBuckets.length - 1;
|
|
514
|
+
for (let i = 0; i < addBucketNum; ++i) {
|
|
515
|
+
newBuckets[newBuckets.length] = new Array(this._bucketSize);
|
|
516
|
+
}
|
|
517
|
+
this._buckets = newBuckets;
|
|
518
|
+
this._bucketCount = newBuckets.length;
|
|
519
|
+
}
|
|
520
|
+
_getBucketAndPosition(pos) {
|
|
521
|
+
let bucketIndex;
|
|
522
|
+
let indexInBucket;
|
|
523
|
+
const overallIndex = this._firstInBucket + pos;
|
|
524
|
+
bucketIndex = this._bucketFirst + Math.floor(overallIndex / this._bucketSize);
|
|
525
|
+
if (bucketIndex >= this._bucketCount) {
|
|
526
|
+
bucketIndex -= this._bucketCount;
|
|
527
|
+
}
|
|
528
|
+
indexInBucket = (overallIndex + 1) % this._bucketSize - 1;
|
|
529
|
+
if (indexInBucket < 0) {
|
|
530
|
+
indexInBucket = this._bucketSize - 1;
|
|
531
|
+
}
|
|
532
|
+
return { bucketIndex, indexInBucket };
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
// O(1) time complexity of obtaining the element
|
|
14
536
|
// O(n) time complexity of adding at the beginning and the end
|
|
15
537
|
// todo tested slowest one
|
|
16
538
|
export class ObjectDeque {
|
|
@@ -46,11 +568,11 @@ export class ObjectDeque {
|
|
|
46
568
|
* Time Complexity: O(1)
|
|
47
569
|
* Space Complexity: O(1)
|
|
48
570
|
*
|
|
49
|
-
* The "addFirst" function adds
|
|
50
|
-
* @param {E}
|
|
571
|
+
* The "addFirst" function adds an element to the beginning of an array-like data structure.
|
|
572
|
+
* @param {E} element - The `element` parameter represents the element that you want to add to the beginning of the data
|
|
51
573
|
* structure.
|
|
52
574
|
*/
|
|
53
|
-
addFirst(
|
|
575
|
+
addFirst(element) {
|
|
54
576
|
if (this.size === 0) {
|
|
55
577
|
const mid = Math.floor(this.capacity / 2);
|
|
56
578
|
this._first = mid;
|
|
@@ -59,7 +581,7 @@ export class ObjectDeque {
|
|
|
59
581
|
else {
|
|
60
582
|
this._first--;
|
|
61
583
|
}
|
|
62
|
-
this.nodes[this.first] =
|
|
584
|
+
this.nodes[this.first] = element;
|
|
63
585
|
this._size++;
|
|
64
586
|
}
|
|
65
587
|
/**
|
|
@@ -70,10 +592,10 @@ export class ObjectDeque {
|
|
|
70
592
|
* Time Complexity: O(1)
|
|
71
593
|
* Space Complexity: O(1)
|
|
72
594
|
*
|
|
73
|
-
* The addLast function adds
|
|
74
|
-
* @param {E}
|
|
595
|
+
* The addLast function adds an element to the end of an array-like data structure.
|
|
596
|
+
* @param {E} element - The `element` parameter represents the element that you want to add to the end of the data structure.
|
|
75
597
|
*/
|
|
76
|
-
addLast(
|
|
598
|
+
addLast(element) {
|
|
77
599
|
if (this.size === 0) {
|
|
78
600
|
const mid = Math.floor(this.capacity / 2);
|
|
79
601
|
this._first = mid;
|
|
@@ -82,7 +604,7 @@ export class ObjectDeque {
|
|
|
82
604
|
else {
|
|
83
605
|
this._last++;
|
|
84
606
|
}
|
|
85
|
-
this.nodes[this.last] =
|
|
607
|
+
this.nodes[this.last] = element;
|
|
86
608
|
this._size++;
|
|
87
609
|
}
|
|
88
610
|
/**
|
|
@@ -94,16 +616,16 @@ export class ObjectDeque {
|
|
|
94
616
|
* Space Complexity: O(1)
|
|
95
617
|
*
|
|
96
618
|
* The function `popFirst()` removes and returns the first element in a data structure.
|
|
97
|
-
* @returns The
|
|
619
|
+
* @returns The element of the first element in the data structure.
|
|
98
620
|
*/
|
|
99
621
|
popFirst() {
|
|
100
622
|
if (!this.size)
|
|
101
623
|
return;
|
|
102
|
-
const
|
|
624
|
+
const element = this.getFirst();
|
|
103
625
|
delete this.nodes[this.first];
|
|
104
626
|
this._first++;
|
|
105
627
|
this._size--;
|
|
106
|
-
return
|
|
628
|
+
return element;
|
|
107
629
|
}
|
|
108
630
|
/**
|
|
109
631
|
* Time Complexity: O(1)
|
|
@@ -129,16 +651,16 @@ export class ObjectDeque {
|
|
|
129
651
|
* Space Complexity: O(1)
|
|
130
652
|
*
|
|
131
653
|
* The `popLast()` function removes and returns the last element in a data structure.
|
|
132
|
-
* @returns The
|
|
654
|
+
* @returns The element that was removed from the data structure.
|
|
133
655
|
*/
|
|
134
656
|
popLast() {
|
|
135
657
|
if (!this.size)
|
|
136
658
|
return;
|
|
137
|
-
const
|
|
659
|
+
const element = this.getLast();
|
|
138
660
|
delete this.nodes[this.last];
|
|
139
661
|
this._last--;
|
|
140
662
|
this._size--;
|
|
141
|
-
return
|
|
663
|
+
return element;
|
|
142
664
|
}
|
|
143
665
|
/**
|
|
144
666
|
* Time Complexity: O(1)
|
|
@@ -167,195 +689,16 @@ export class ObjectDeque {
|
|
|
167
689
|
* @param {number} index - The index parameter is a number that represents the position of the element you want to
|
|
168
690
|
* retrieve from the array.
|
|
169
691
|
* @returns The element at the specified index in the `_nodes` array is being returned. If there is no element at that
|
|
170
|
-
* index, `
|
|
692
|
+
* index, `undefined` is returned.
|
|
171
693
|
*/
|
|
172
694
|
get(index) {
|
|
173
|
-
return this.nodes[this.first + index] ||
|
|
695
|
+
return this.nodes[this.first + index] || undefined;
|
|
174
696
|
}
|
|
175
697
|
/**
|
|
176
698
|
* The function checks if the size of a data structure is less than or equal to zero.
|
|
177
|
-
* @returns The method is returning a boolean
|
|
699
|
+
* @returns The method is returning a boolean element indicating whether the size of the object is less than or equal to 0.
|
|
178
700
|
*/
|
|
179
701
|
isEmpty() {
|
|
180
702
|
return this.size <= 0;
|
|
181
703
|
}
|
|
182
704
|
}
|
|
183
|
-
// O(1) time complexity of obtaining the value
|
|
184
|
-
// O(n) time complexity of adding at the beginning and the end
|
|
185
|
-
export class ArrayDeque {
|
|
186
|
-
_nodes = [];
|
|
187
|
-
get nodes() {
|
|
188
|
-
return this._nodes;
|
|
189
|
-
}
|
|
190
|
-
get size() {
|
|
191
|
-
return this.nodes.length;
|
|
192
|
-
}
|
|
193
|
-
/**
|
|
194
|
-
* Time Complexity: O(1)
|
|
195
|
-
* Space Complexity: O(1)
|
|
196
|
-
*/
|
|
197
|
-
/**
|
|
198
|
-
* Time Complexity: O(1)
|
|
199
|
-
* Space Complexity: O(1)
|
|
200
|
-
*
|
|
201
|
-
* The function "addLast" adds a value to the end of an array.
|
|
202
|
-
* @param {E} value - The value parameter represents the value that you want to add to the end of the array.
|
|
203
|
-
* @returns The return value is the new length of the array after the value has been added.
|
|
204
|
-
*/
|
|
205
|
-
addLast(value) {
|
|
206
|
-
return this.nodes.push(value);
|
|
207
|
-
}
|
|
208
|
-
/**
|
|
209
|
-
* Time Complexity: O(1)
|
|
210
|
-
* Space Complexity: O(1)
|
|
211
|
-
*/
|
|
212
|
-
/**
|
|
213
|
-
* Time Complexity: O(1)
|
|
214
|
-
* Space Complexity: O(1)
|
|
215
|
-
*
|
|
216
|
-
* The function "popLast" returns and removes the last element from an array, or returns null if the array is empty.
|
|
217
|
-
* @returns The method `popLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
218
|
-
*/
|
|
219
|
-
popLast() {
|
|
220
|
-
return this.nodes.pop() ?? null;
|
|
221
|
-
}
|
|
222
|
-
/**
|
|
223
|
-
* Time Complexity: O(n)
|
|
224
|
-
* Space Complexity: O(1)
|
|
225
|
-
*/
|
|
226
|
-
/**
|
|
227
|
-
* Time Complexity: O(n)
|
|
228
|
-
* Space Complexity: O(1)
|
|
229
|
-
*
|
|
230
|
-
* The `popFirst` function removes and returns the first element from an array, or returns null if the array is empty.
|
|
231
|
-
* @returns The `popFirst()` function returns the first element of the `_nodes` array, or `null` if the array is
|
|
232
|
-
* empty.
|
|
233
|
-
*/
|
|
234
|
-
popFirst() {
|
|
235
|
-
return this.nodes.shift() ?? null;
|
|
236
|
-
}
|
|
237
|
-
/**
|
|
238
|
-
* Time Complexity: O(n)
|
|
239
|
-
* Space Complexity: O(1)
|
|
240
|
-
*/
|
|
241
|
-
/**
|
|
242
|
-
* Time Complexity: O(n)
|
|
243
|
-
* Space Complexity: O(1)
|
|
244
|
-
*
|
|
245
|
-
* The function "addFirst" adds a value to the beginning of an array.
|
|
246
|
-
* @param {E} value - The value parameter represents the value that you want to add to the beginning of the array.
|
|
247
|
-
* @returns The return value of the `addFirst` function is the new length of the array `_nodes` after adding the
|
|
248
|
-
* `value` at the beginning.
|
|
249
|
-
*/
|
|
250
|
-
addFirst(value) {
|
|
251
|
-
return this.nodes.unshift(value);
|
|
252
|
-
}
|
|
253
|
-
/**
|
|
254
|
-
* Time Complexity: O(1)
|
|
255
|
-
* Space Complexity: O(1)
|
|
256
|
-
*/
|
|
257
|
-
/**
|
|
258
|
-
* Time Complexity: O(1)
|
|
259
|
-
* Space Complexity: O(1)
|
|
260
|
-
*
|
|
261
|
-
* The `getFirst` function returns the first element of an array or null if the array is empty.
|
|
262
|
-
* @returns The function `getFirst()` is returning the first element (`E`) of the `_nodes` array. If the array is
|
|
263
|
-
* empty, it will return `null`.
|
|
264
|
-
*/
|
|
265
|
-
getFirst() {
|
|
266
|
-
return this.nodes[0] ?? null;
|
|
267
|
-
}
|
|
268
|
-
/**
|
|
269
|
-
* Time Complexity: O(1)
|
|
270
|
-
* Space Complexity: O(1)
|
|
271
|
-
*/
|
|
272
|
-
/**
|
|
273
|
-
* Time Complexity: O(1)
|
|
274
|
-
* Space Complexity: O(1)
|
|
275
|
-
*
|
|
276
|
-
* The `getLast` function returns the last element of an array or null if the array is empty.
|
|
277
|
-
* @returns The method `getLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
278
|
-
*/
|
|
279
|
-
getLast() {
|
|
280
|
-
return this.nodes[this.nodes.length - 1] ?? null;
|
|
281
|
-
}
|
|
282
|
-
/**
|
|
283
|
-
* Time Complexity: O(1)
|
|
284
|
-
* Space Complexity: O(1)
|
|
285
|
-
*/
|
|
286
|
-
/**
|
|
287
|
-
* Time Complexity: O(1)
|
|
288
|
-
* Space Complexity: O(1)
|
|
289
|
-
*
|
|
290
|
-
* The get function returns the element at the specified index in an array, or null if the index is out of bounds.
|
|
291
|
-
* @param {number} index - The index parameter is a number that represents the position of the element you want to
|
|
292
|
-
* retrieve from the array.
|
|
293
|
-
* @returns The method is returning the element at the specified index in the `_nodes` array. If the element exists, it
|
|
294
|
-
* will be returned. If the element does not exist (i.e., the index is out of bounds), `null` will be returned.
|
|
295
|
-
*/
|
|
296
|
-
get(index) {
|
|
297
|
-
return this.nodes[index] ?? null;
|
|
298
|
-
}
|
|
299
|
-
/**
|
|
300
|
-
* Time Complexity: O(1)
|
|
301
|
-
* Space Complexity: O(1)
|
|
302
|
-
*/
|
|
303
|
-
/**
|
|
304
|
-
* Time Complexity: O(1)
|
|
305
|
-
* Space Complexity: O(1)
|
|
306
|
-
*
|
|
307
|
-
* The set function assigns a value to a specific index in an array.
|
|
308
|
-
* @param {number} index - The index parameter is a number that represents the position of the element in the array
|
|
309
|
-
* that you want to set a new value for.
|
|
310
|
-
* @param {E} value - The value parameter represents the new value that you want to set at the specified index in the
|
|
311
|
-
* _nodes array.
|
|
312
|
-
* @returns The value that is being set at the specified index in the `_nodes` array.
|
|
313
|
-
*/
|
|
314
|
-
set(index, value) {
|
|
315
|
-
return (this.nodes[index] = value);
|
|
316
|
-
}
|
|
317
|
-
/**
|
|
318
|
-
* Time Complexity: O(n)
|
|
319
|
-
* Space Complexity: O(1)
|
|
320
|
-
*/
|
|
321
|
-
/**
|
|
322
|
-
* Time Complexity: O(n)
|
|
323
|
-
* Space Complexity: O(1)
|
|
324
|
-
*
|
|
325
|
-
* The insert function adds a value at a specified index in an array.
|
|
326
|
-
* @param {number} index - The index parameter specifies the position at which the value should be inserted in the
|
|
327
|
-
* array. It is a number that represents the index of the array where the value should be inserted. The index starts
|
|
328
|
-
* from 0, so the first element of the array has an index of 0, the second element has
|
|
329
|
-
* @param {E} value - The value parameter represents the value that you want to insert into the array at the specified
|
|
330
|
-
* index.
|
|
331
|
-
* @returns The splice method returns an array containing the removed elements, if any. In this case, since no elements
|
|
332
|
-
* are being removed, an empty array will be returned.
|
|
333
|
-
*/
|
|
334
|
-
insert(index, value) {
|
|
335
|
-
return this.nodes.splice(index, 0, value);
|
|
336
|
-
}
|
|
337
|
-
/**
|
|
338
|
-
* Time Complexity: O(n)
|
|
339
|
-
* Space Complexity: O(1)
|
|
340
|
-
*/
|
|
341
|
-
/**
|
|
342
|
-
* Time Complexity: O(n)
|
|
343
|
-
* Space Complexity: O(1)
|
|
344
|
-
*
|
|
345
|
-
* The delete function removes an element from an array at a specified index.
|
|
346
|
-
* @param {number} index - The index parameter specifies the position of the element to be removed from the array. It
|
|
347
|
-
* is a number that represents the index of the element to be removed.
|
|
348
|
-
* @returns The method is returning an array containing the removed element.
|
|
349
|
-
*/
|
|
350
|
-
delete(index) {
|
|
351
|
-
return this.nodes.splice(index, 1);
|
|
352
|
-
}
|
|
353
|
-
/**
|
|
354
|
-
* The function checks if an array called "_nodes" is empty.
|
|
355
|
-
* @returns The method `isEmpty()` is returning a boolean value. It returns `true` if the length of the `_nodes` array
|
|
356
|
-
* is 0, indicating that the array is empty. Otherwise, it returns `false`.
|
|
357
|
-
*/
|
|
358
|
-
isEmpty() {
|
|
359
|
-
return this.nodes.length === 0;
|
|
360
|
-
}
|
|
361
|
-
}
|