data-structure-typed 1.46.1 → 1.46.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 +1 -1
- package/README.md +16 -16
- package/benchmark/report.html +1 -46
- package/benchmark/report.json +16 -385
- package/dist/cjs/data-structures/hash/hash-map.d.ts +10 -91
- package/dist/cjs/data-structures/hash/hash-map.js +22 -178
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +358 -24
- package/dist/cjs/data-structures/queue/deque.js +380 -96
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/types/helpers.d.ts +2 -2
- package/dist/mjs/data-structures/hash/hash-map.d.ts +10 -91
- package/dist/mjs/data-structures/hash/hash-map.js +22 -181
- package/dist/mjs/data-structures/queue/deque.d.ts +358 -24
- package/dist/mjs/data-structures/queue/deque.js +376 -94
- package/dist/mjs/types/helpers.d.ts +2 -2
- package/dist/umd/data-structure-typed.js +433 -303
- package/dist/umd/data-structure-typed.min.js +1 -1
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +4 -3
- package/src/data-structures/hash/hash-map.ts +25 -196
- package/src/data-structures/queue/deque.ts +401 -113
- package/src/types/helpers.ts +2 -2
- package/test/performance/data-structures/hash/hash-map.test.ts +11 -11
- package/test/performance/reportor.ts +6 -3
- package/test/unit/data-structures/hash/hash-map.test.ts +4 -5
- package/test/unit/data-structures/queue/deque.test.ts +28 -0
|
@@ -5,73 +5,13 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { calcMinUnitsRequired, rangeCheck
|
|
8
|
+
import { calcMinUnitsRequired, rangeCheck } from "../../utils";
|
|
9
9
|
/**
|
|
10
10
|
* Deque can provide random access with O(1) time complexity
|
|
11
11
|
* Deque is usually more compact and efficient in memory usage because it does not require additional space to store pointers.
|
|
12
12
|
* Deque may experience performance jitter, but DoublyLinkedList will not
|
|
13
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
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
|
-
}
|
|
74
|
-
}
|
|
75
15
|
export class Deque {
|
|
76
16
|
_bucketFirst = 0;
|
|
77
17
|
_firstInBucket = 0;
|
|
@@ -79,13 +19,28 @@ export class Deque {
|
|
|
79
19
|
_lastInBucket = 0;
|
|
80
20
|
_bucketCount = 0;
|
|
81
21
|
_bucketSize;
|
|
22
|
+
/**
|
|
23
|
+
* The constructor initializes a data structure with a specified bucket size and populates it with
|
|
24
|
+
* elements from an iterable.
|
|
25
|
+
* @param elements - The `elements` parameter is an iterable object (such as an array or a Set) that
|
|
26
|
+
* contains the initial elements to be stored in the data structure. It can also be an object with a
|
|
27
|
+
* `length` property or a `size` property, which represents the number of elements in the iterable.
|
|
28
|
+
* @param bucketSize - The `bucketSize` parameter is the maximum number of elements that can be
|
|
29
|
+
* stored in each bucket. It determines the size of each bucket in the data structure.
|
|
30
|
+
*/
|
|
82
31
|
constructor(elements = [], bucketSize = (1 << 12)) {
|
|
83
32
|
let _size;
|
|
84
33
|
if ('length' in elements) {
|
|
85
|
-
|
|
34
|
+
if (elements.length instanceof Function)
|
|
35
|
+
_size = elements.length();
|
|
36
|
+
else
|
|
37
|
+
_size = elements.length;
|
|
86
38
|
}
|
|
87
39
|
else {
|
|
88
|
-
|
|
40
|
+
if (elements.size instanceof Function)
|
|
41
|
+
_size = elements.size();
|
|
42
|
+
else
|
|
43
|
+
_size = elements.size;
|
|
89
44
|
}
|
|
90
45
|
this._bucketSize = bucketSize;
|
|
91
46
|
this._bucketCount = calcMinUnitsRequired(_size, this._bucketSize) || 1;
|
|
@@ -107,6 +62,11 @@ export class Deque {
|
|
|
107
62
|
get size() {
|
|
108
63
|
return this._size;
|
|
109
64
|
}
|
|
65
|
+
/**
|
|
66
|
+
* The function returns the first element in a collection if it exists, otherwise it returns
|
|
67
|
+
* undefined.
|
|
68
|
+
* @returns The first element of the collection, of type E, is being returned.
|
|
69
|
+
*/
|
|
110
70
|
get first() {
|
|
111
71
|
if (this.size === 0)
|
|
112
72
|
return;
|
|
@@ -117,13 +77,6 @@ export class Deque {
|
|
|
117
77
|
return;
|
|
118
78
|
return this._buckets[this._bucketLast][this._lastInBucket];
|
|
119
79
|
}
|
|
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
80
|
/**
|
|
128
81
|
* Time Complexity: O(1) - Removes the last element.
|
|
129
82
|
* Space Complexity: O(1) - Operates in-place.
|
|
@@ -182,24 +135,50 @@ export class Deque {
|
|
|
182
135
|
popFirst() {
|
|
183
136
|
return this.shift();
|
|
184
137
|
}
|
|
138
|
+
/**
|
|
139
|
+
* The clear() function resets the state of the object by initializing all variables to their default
|
|
140
|
+
* values.
|
|
141
|
+
*/
|
|
185
142
|
clear() {
|
|
186
143
|
this._buckets = [new Array(this._bucketSize)];
|
|
187
144
|
this._bucketCount = 1;
|
|
188
145
|
this._bucketFirst = this._bucketLast = this._size = 0;
|
|
189
146
|
this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
|
|
190
147
|
}
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
148
|
+
/**
|
|
149
|
+
* The below function is a generator that yields elements from a collection one by one.
|
|
150
|
+
*/
|
|
151
|
+
*begin() {
|
|
152
|
+
let index = 0;
|
|
153
|
+
while (index < this.size) {
|
|
154
|
+
yield this.getAt(index);
|
|
155
|
+
index++;
|
|
156
|
+
}
|
|
199
157
|
}
|
|
200
|
-
|
|
201
|
-
|
|
158
|
+
/**
|
|
159
|
+
* The function `reverseBegin()` is a generator that yields elements in reverse order starting from
|
|
160
|
+
* the last element.
|
|
161
|
+
*/
|
|
162
|
+
*reverseBegin() {
|
|
163
|
+
let index = this.size - 1;
|
|
164
|
+
while (index >= 0) {
|
|
165
|
+
yield this.getAt(index);
|
|
166
|
+
index--;
|
|
167
|
+
}
|
|
202
168
|
}
|
|
169
|
+
/**
|
|
170
|
+
* Time Complexity - Amortized O(1) (possible reallocation)
|
|
171
|
+
* Space Complexity - O(n) (due to potential resizing).
|
|
172
|
+
*/
|
|
173
|
+
/**
|
|
174
|
+
* Time Complexity - Amortized O(1) (possible reallocation),
|
|
175
|
+
* Space Complexity - O(n) (due to potential resizing).
|
|
176
|
+
*
|
|
177
|
+
* The push function adds an element to a data structure and reallocates memory if necessary.
|
|
178
|
+
* @param {E} element - The `element` parameter represents the value that you want to add to the data
|
|
179
|
+
* structure.
|
|
180
|
+
* @returns The size of the data structure after the element has been pushed.
|
|
181
|
+
*/
|
|
203
182
|
push(element) {
|
|
204
183
|
if (this.size) {
|
|
205
184
|
if (this._lastInBucket < this._bucketSize - 1) {
|
|
@@ -221,6 +200,18 @@ export class Deque {
|
|
|
221
200
|
this._buckets[this._bucketLast][this._lastInBucket] = element;
|
|
222
201
|
return this.size;
|
|
223
202
|
}
|
|
203
|
+
/**
|
|
204
|
+
* Time Complexity: O(1)
|
|
205
|
+
* Space Complexity: O(1)
|
|
206
|
+
*/
|
|
207
|
+
/**
|
|
208
|
+
* Time Complexity: O(1)
|
|
209
|
+
* Space Complexity: O(1)
|
|
210
|
+
*
|
|
211
|
+
* The `pop()` function removes and returns the last element from a data structure, updating the
|
|
212
|
+
* internal state variables accordingly.
|
|
213
|
+
* @returns The element that was removed from the data structure is being returned.
|
|
214
|
+
*/
|
|
224
215
|
pop() {
|
|
225
216
|
if (this.size === 0)
|
|
226
217
|
return;
|
|
@@ -241,6 +232,20 @@ export class Deque {
|
|
|
241
232
|
this._size -= 1;
|
|
242
233
|
return element;
|
|
243
234
|
}
|
|
235
|
+
/**
|
|
236
|
+
* Time Complexity: Amortized O(1)
|
|
237
|
+
* Space Complexity: O(n)
|
|
238
|
+
*/
|
|
239
|
+
/**
|
|
240
|
+
* Time Complexity: Amortized O(1)
|
|
241
|
+
* Space Complexity: O(n)
|
|
242
|
+
*
|
|
243
|
+
* The `unshift` function adds an element to the beginning of an array-like data structure and
|
|
244
|
+
* returns the new size of the structure.
|
|
245
|
+
* @param {E} element - The `element` parameter represents the element that you want to add to the
|
|
246
|
+
* beginning of the data structure.
|
|
247
|
+
* @returns The size of the data structure after the element has been added.
|
|
248
|
+
*/
|
|
244
249
|
unshift(element) {
|
|
245
250
|
if (this.size) {
|
|
246
251
|
if (this._firstInBucket > 0) {
|
|
@@ -262,6 +267,19 @@ export class Deque {
|
|
|
262
267
|
this._buckets[this._bucketFirst][this._firstInBucket] = element;
|
|
263
268
|
return this.size;
|
|
264
269
|
}
|
|
270
|
+
/**
|
|
271
|
+
* Time Complexity: O(1)
|
|
272
|
+
* Space Complexity: O(1)
|
|
273
|
+
*/
|
|
274
|
+
/**
|
|
275
|
+
* Time Complexity: O(1)
|
|
276
|
+
* Space Complexity: O(1)
|
|
277
|
+
*
|
|
278
|
+
* The `shift()` function removes and returns the first element from a data structure, updating the
|
|
279
|
+
* internal state variables accordingly.
|
|
280
|
+
* @returns The element that is being removed from the beginning of the data structure is being
|
|
281
|
+
* returned.
|
|
282
|
+
*/
|
|
265
283
|
shift() {
|
|
266
284
|
if (this.size === 0)
|
|
267
285
|
return;
|
|
@@ -282,16 +300,63 @@ export class Deque {
|
|
|
282
300
|
this._size -= 1;
|
|
283
301
|
return element;
|
|
284
302
|
}
|
|
303
|
+
/**
|
|
304
|
+
* Time Complexity: O(1)
|
|
305
|
+
* Space Complexity: O(1)
|
|
306
|
+
*/
|
|
307
|
+
/**
|
|
308
|
+
* Time Complexity: O(1)
|
|
309
|
+
* Space Complexity: O(1)
|
|
310
|
+
*
|
|
311
|
+
* The `getAt` function retrieves an element at a specified position in an array-like data structure.
|
|
312
|
+
* @param {number} pos - The `pos` parameter represents the position of the element that you want to
|
|
313
|
+
* retrieve from the data structure. It is of type `number` and should be a valid index within the
|
|
314
|
+
* range of the data structure.
|
|
315
|
+
* @returns The element at the specified position in the data structure is being returned.
|
|
316
|
+
*/
|
|
285
317
|
getAt(pos) {
|
|
286
318
|
rangeCheck(pos, 0, this.size - 1);
|
|
287
319
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
288
320
|
return this._buckets[bucketIndex][indexInBucket];
|
|
289
321
|
}
|
|
322
|
+
/**
|
|
323
|
+
* Time Complexity: O(1)
|
|
324
|
+
* Space Complexity: O(1)
|
|
325
|
+
*/
|
|
326
|
+
/**
|
|
327
|
+
* Time Complexity: O(1)
|
|
328
|
+
* Space Complexity: O(1)
|
|
329
|
+
*
|
|
330
|
+
* The `setAt` function sets an element at a specific position in an array-like data structure.
|
|
331
|
+
* @param {number} pos - The `pos` parameter represents the position at which the element needs to be
|
|
332
|
+
* set. It is of type `number`.
|
|
333
|
+
* @param {E} element - The `element` parameter is the value that you want to set at the specified
|
|
334
|
+
* position in the data structure.
|
|
335
|
+
*/
|
|
290
336
|
setAt(pos, element) {
|
|
291
337
|
rangeCheck(pos, 0, this.size - 1);
|
|
292
338
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
293
339
|
this._buckets[bucketIndex][indexInBucket] = element;
|
|
294
340
|
}
|
|
341
|
+
/**
|
|
342
|
+
* Time Complexity: O(n)
|
|
343
|
+
* Space Complexity: O(n)
|
|
344
|
+
*/
|
|
345
|
+
/**
|
|
346
|
+
* Time Complexity: O(n)
|
|
347
|
+
* Space Complexity: O(n)
|
|
348
|
+
*
|
|
349
|
+
* The `insertAt` function inserts one or more elements at a specified position in an array-like data
|
|
350
|
+
* structure.
|
|
351
|
+
* @param {number} pos - The `pos` parameter represents the position at which the element(s) should
|
|
352
|
+
* be inserted. It is of type `number`.
|
|
353
|
+
* @param {E} element - The `element` parameter represents the element that you want to insert into
|
|
354
|
+
* the array at the specified position.
|
|
355
|
+
* @param [num=1] - The `num` parameter represents the number of times the `element` should be
|
|
356
|
+
* inserted at the specified position (`pos`). By default, it is set to 1, meaning that the `element`
|
|
357
|
+
* will be inserted once. However, you can provide a different value for `num` if you want
|
|
358
|
+
* @returns The size of the array after the insertion is being returned.
|
|
359
|
+
*/
|
|
295
360
|
insertAt(pos, element, num = 1) {
|
|
296
361
|
const length = this.size;
|
|
297
362
|
rangeCheck(pos, 0, length);
|
|
@@ -316,6 +381,20 @@ export class Deque {
|
|
|
316
381
|
}
|
|
317
382
|
return this.size;
|
|
318
383
|
}
|
|
384
|
+
/**
|
|
385
|
+
* Time Complexity: O(1)
|
|
386
|
+
* Space Complexity: O(1)
|
|
387
|
+
*/
|
|
388
|
+
/**
|
|
389
|
+
* Time Complexity: O(1)
|
|
390
|
+
* Space Complexity: O(1)
|
|
391
|
+
*
|
|
392
|
+
* The `cut` function updates the state of the object based on the given position and returns the
|
|
393
|
+
* updated size.
|
|
394
|
+
* @param {number} pos - The `pos` parameter represents the position at which the string should be
|
|
395
|
+
* cut. It is a number that indicates the index of the character where the cut should be made.
|
|
396
|
+
* @returns The method is returning the updated size of the data structure.
|
|
397
|
+
*/
|
|
319
398
|
cut(pos) {
|
|
320
399
|
if (pos < 0) {
|
|
321
400
|
this.clear();
|
|
@@ -327,6 +406,21 @@ export class Deque {
|
|
|
327
406
|
this._size = pos + 1;
|
|
328
407
|
return this.size;
|
|
329
408
|
}
|
|
409
|
+
/**
|
|
410
|
+
* Time Complexity: O(n)
|
|
411
|
+
* Space Complexity: O(1)
|
|
412
|
+
*/
|
|
413
|
+
/**
|
|
414
|
+
* Time Complexity: O(n)
|
|
415
|
+
* Space Complexity: O(1)
|
|
416
|
+
*
|
|
417
|
+
* The `deleteAt` function removes an element at a specified position in an array-like data
|
|
418
|
+
* structure.
|
|
419
|
+
* @param {number} pos - The `pos` parameter in the `deleteAt` function represents the position at
|
|
420
|
+
* which an element needs to be deleted from the data structure. It is of type `number` and indicates
|
|
421
|
+
* the index of the element to be deleted.
|
|
422
|
+
* @returns The size of the data structure after the deletion operation is performed.
|
|
423
|
+
*/
|
|
330
424
|
deleteAt(pos) {
|
|
331
425
|
rangeCheck(pos, 0, this.size - 1);
|
|
332
426
|
if (pos === 0)
|
|
@@ -346,6 +440,20 @@ export class Deque {
|
|
|
346
440
|
}
|
|
347
441
|
return this.size;
|
|
348
442
|
}
|
|
443
|
+
/**
|
|
444
|
+
* Time Complexity: O(n)
|
|
445
|
+
* Space Complexity: O(1)
|
|
446
|
+
*/
|
|
447
|
+
/**
|
|
448
|
+
* Time Complexity: O(n)
|
|
449
|
+
* Space Complexity: O(1)
|
|
450
|
+
*
|
|
451
|
+
* The `delete` function removes all occurrences of a specified element from an array-like data
|
|
452
|
+
* structure.
|
|
453
|
+
* @param {E} element - The `element` parameter represents the element that you want to delete from
|
|
454
|
+
* the data structure.
|
|
455
|
+
* @returns The size of the data structure after the element has been deleted.
|
|
456
|
+
*/
|
|
349
457
|
delete(element) {
|
|
350
458
|
const size = this.size;
|
|
351
459
|
if (size === 0)
|
|
@@ -363,20 +471,19 @@ export class Deque {
|
|
|
363
471
|
this.cut(index - 1);
|
|
364
472
|
return this.size;
|
|
365
473
|
}
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
}
|
|
474
|
+
/**
|
|
475
|
+
* Time Complexity: O(n)
|
|
476
|
+
* Space Complexity: O(1)
|
|
477
|
+
*/
|
|
478
|
+
/**
|
|
479
|
+
* Time Complexity: O(n)
|
|
480
|
+
* Space Complexity: O(1)
|
|
481
|
+
*
|
|
482
|
+
* The reverse() function reverses the order of the buckets and the elements within each bucket in a
|
|
483
|
+
* data structure.
|
|
484
|
+
* @returns The reverse() method is returning the object itself (this) after performing the reverse
|
|
485
|
+
* operation on the buckets and updating the relevant properties.
|
|
486
|
+
*/
|
|
380
487
|
reverse() {
|
|
381
488
|
this._buckets.reverse().forEach(function (bucket) {
|
|
382
489
|
bucket.reverse();
|
|
@@ -388,6 +495,18 @@ export class Deque {
|
|
|
388
495
|
this._lastInBucket = this._bucketSize - _firstInBucket - 1;
|
|
389
496
|
return this;
|
|
390
497
|
}
|
|
498
|
+
/**
|
|
499
|
+
* Time Complexity: O(n)
|
|
500
|
+
* Space Complexity: O(1)
|
|
501
|
+
*/
|
|
502
|
+
/**
|
|
503
|
+
* Time Complexity: O(n)
|
|
504
|
+
* Space Complexity: O(1)
|
|
505
|
+
*
|
|
506
|
+
* The `unique()` function removes duplicate elements from an array-like data structure and returns
|
|
507
|
+
* the number of unique elements.
|
|
508
|
+
* @returns The size of the modified array is being returned.
|
|
509
|
+
*/
|
|
391
510
|
unique() {
|
|
392
511
|
if (this.size <= 1) {
|
|
393
512
|
return this.size;
|
|
@@ -404,6 +523,20 @@ export class Deque {
|
|
|
404
523
|
this.cut(index - 1);
|
|
405
524
|
return this.size;
|
|
406
525
|
}
|
|
526
|
+
/**
|
|
527
|
+
* Time Complexity: O(n log n)
|
|
528
|
+
* Space Complexity: O(n)
|
|
529
|
+
*/
|
|
530
|
+
/**
|
|
531
|
+
* Time Complexity: O(n log n)
|
|
532
|
+
* Space Complexity: O(n)
|
|
533
|
+
*
|
|
534
|
+
* The `sort` function sorts the elements in a data structure using a provided comparator function.
|
|
535
|
+
* @param [comparator] - The `comparator` parameter is a function that takes in two elements `x` and
|
|
536
|
+
* `y` of type `E` and returns a number. The comparator function is used to determine the order of
|
|
537
|
+
* the elements in the sorted array.
|
|
538
|
+
* @returns The method is returning the sorted instance of the object on which the method is called.
|
|
539
|
+
*/
|
|
407
540
|
sort(comparator) {
|
|
408
541
|
const arr = [];
|
|
409
542
|
for (let i = 0; i < this.size; ++i) {
|
|
@@ -415,6 +548,19 @@ export class Deque {
|
|
|
415
548
|
}
|
|
416
549
|
return this;
|
|
417
550
|
}
|
|
551
|
+
/**
|
|
552
|
+
* Time Complexity: O(n)
|
|
553
|
+
* Space Complexity: O(n)
|
|
554
|
+
*/
|
|
555
|
+
/**
|
|
556
|
+
* Time Complexity: O(n)
|
|
557
|
+
* Space Complexity: O(n)
|
|
558
|
+
*
|
|
559
|
+
* The `shrinkToFit` function reorganizes the elements in an array-like data structure to minimize
|
|
560
|
+
* memory usage.
|
|
561
|
+
* @returns Nothing is being returned. The function is using the `return` statement to exit early if
|
|
562
|
+
* `this.size` is 0, but it does not return any value.
|
|
563
|
+
*/
|
|
418
564
|
shrinkToFit() {
|
|
419
565
|
if (this.size === 0)
|
|
420
566
|
return;
|
|
@@ -438,11 +584,39 @@ export class Deque {
|
|
|
438
584
|
this._bucketLast = newBuckets.length - 1;
|
|
439
585
|
this._buckets = newBuckets;
|
|
440
586
|
}
|
|
587
|
+
/**
|
|
588
|
+
* Time Complexity: O(n)
|
|
589
|
+
* Space Complexity: O(1)
|
|
590
|
+
*/
|
|
591
|
+
/**
|
|
592
|
+
* Time Complexity: O(n)
|
|
593
|
+
* Space Complexity: O(1)
|
|
594
|
+
*
|
|
595
|
+
* The `forEach` function iterates over each element in a deque and applies a callback function to
|
|
596
|
+
* each element.
|
|
597
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
598
|
+
* deque. It takes three parameters:
|
|
599
|
+
*/
|
|
441
600
|
forEach(callback) {
|
|
442
601
|
for (let i = 0; i < this.size; ++i) {
|
|
443
602
|
callback(this.getAt(i), i, this);
|
|
444
603
|
}
|
|
445
604
|
}
|
|
605
|
+
/**
|
|
606
|
+
* Time Complexity: O(n)
|
|
607
|
+
* Space Complexity: O(1)
|
|
608
|
+
*/
|
|
609
|
+
/**
|
|
610
|
+
* Time Complexity: O(n)
|
|
611
|
+
* Space Complexity: O(1)
|
|
612
|
+
*
|
|
613
|
+
* The `find` function iterates over the elements in a deque and returns the first element for which
|
|
614
|
+
* the callback function returns true, or undefined if no such element is found.
|
|
615
|
+
* @param callback - A function that takes three parameters: element, index, and deque. It should
|
|
616
|
+
* return a boolean value indicating whether the element satisfies a certain condition.
|
|
617
|
+
* @returns The method `find` returns the first element in the deque that satisfies the condition
|
|
618
|
+
* specified by the callback function. If no element satisfies the condition, it returns `undefined`.
|
|
619
|
+
*/
|
|
446
620
|
find(callback) {
|
|
447
621
|
for (let i = 0; i < this.size; ++i) {
|
|
448
622
|
const element = this.getAt(i);
|
|
@@ -452,6 +626,17 @@ export class Deque {
|
|
|
452
626
|
}
|
|
453
627
|
return undefined;
|
|
454
628
|
}
|
|
629
|
+
/**
|
|
630
|
+
* Time Complexity: O(n)
|
|
631
|
+
* Space Complexity: O(n)
|
|
632
|
+
*/
|
|
633
|
+
/**
|
|
634
|
+
* Time Complexity: O(n)
|
|
635
|
+
* Space Complexity: O(n)
|
|
636
|
+
*
|
|
637
|
+
* The `toArray` function converts the elements of a data structure into an array.
|
|
638
|
+
* @returns The `toArray()` method is returning an array of elements of type `E`.
|
|
639
|
+
*/
|
|
455
640
|
toArray() {
|
|
456
641
|
const arr = [];
|
|
457
642
|
for (let i = 0; i < this.size; ++i) {
|
|
@@ -459,6 +644,19 @@ export class Deque {
|
|
|
459
644
|
}
|
|
460
645
|
return arr;
|
|
461
646
|
}
|
|
647
|
+
/**
|
|
648
|
+
* Time Complexity: O(n)
|
|
649
|
+
* Space Complexity: O(n)
|
|
650
|
+
*/
|
|
651
|
+
/**
|
|
652
|
+
* Time Complexity: O(n)
|
|
653
|
+
* Space Complexity: O(n)
|
|
654
|
+
*
|
|
655
|
+
* The `map` function takes a callback function and applies it to each element in the deque,
|
|
656
|
+
* returning a new deque with the results.
|
|
657
|
+
* @param callback - The `callback` parameter is a function that takes three arguments:
|
|
658
|
+
* @returns The `map` method is returning a new `Deque` object with the transformed elements.
|
|
659
|
+
*/
|
|
462
660
|
map(callback) {
|
|
463
661
|
const newDeque = new Deque([], this._bucketSize);
|
|
464
662
|
for (let i = 0; i < this.size; ++i) {
|
|
@@ -466,6 +664,21 @@ export class Deque {
|
|
|
466
664
|
}
|
|
467
665
|
return newDeque;
|
|
468
666
|
}
|
|
667
|
+
/**
|
|
668
|
+
* Time Complexity: O(n)
|
|
669
|
+
* Space Complexity: O(n)
|
|
670
|
+
*/
|
|
671
|
+
/**
|
|
672
|
+
* Time Complexity: O(n)
|
|
673
|
+
* Space Complexity: O(n)
|
|
674
|
+
*
|
|
675
|
+
* The `filter` function creates a new deque containing only the elements that satisfy the given
|
|
676
|
+
* predicate function.
|
|
677
|
+
* @param predicate - The `predicate` parameter is a function that takes three arguments: `element`,
|
|
678
|
+
* `index`, and `deque`.
|
|
679
|
+
* @returns The `filter` method is returning a new `Deque` object that contains only the elements
|
|
680
|
+
* that satisfy the given `predicate` function.
|
|
681
|
+
*/
|
|
469
682
|
filter(predicate) {
|
|
470
683
|
const newDeque = new Deque([], this._bucketSize);
|
|
471
684
|
for (let i = 0; i < this.size; ++i) {
|
|
@@ -476,6 +689,23 @@ export class Deque {
|
|
|
476
689
|
}
|
|
477
690
|
return newDeque;
|
|
478
691
|
}
|
|
692
|
+
/**
|
|
693
|
+
* Time Complexity: O(n)
|
|
694
|
+
* Space Complexity: O(1)
|
|
695
|
+
*/
|
|
696
|
+
/**
|
|
697
|
+
* Time Complexity: O(n)
|
|
698
|
+
* Space Complexity: O(1)
|
|
699
|
+
*
|
|
700
|
+
* The `reduce` function iterates over the elements of a deque and applies a callback function to
|
|
701
|
+
* each element, accumulating a single value.
|
|
702
|
+
* @param callback - The `callback` parameter is a function that takes four arguments:
|
|
703
|
+
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It
|
|
704
|
+
* is the value that will be passed as the first argument to the `callback` function when reducing
|
|
705
|
+
* the elements of the deque.
|
|
706
|
+
* @returns the final value of the accumulator after iterating over all elements in the deque and
|
|
707
|
+
* applying the callback function to each element.
|
|
708
|
+
*/
|
|
479
709
|
reduce(callback, initialValue) {
|
|
480
710
|
let accumulator = initialValue;
|
|
481
711
|
for (let i = 0; i < this.size; ++i) {
|
|
@@ -483,6 +713,21 @@ export class Deque {
|
|
|
483
713
|
}
|
|
484
714
|
return accumulator;
|
|
485
715
|
}
|
|
716
|
+
/**
|
|
717
|
+
* Time Complexity: O(n)
|
|
718
|
+
* Space Complexity: O(1)
|
|
719
|
+
*/
|
|
720
|
+
/**
|
|
721
|
+
* Time Complexity: O(n)
|
|
722
|
+
* Space Complexity: O(1)
|
|
723
|
+
*
|
|
724
|
+
* The function "indexOf" returns the index of the first occurrence of a given element in an array,
|
|
725
|
+
* or -1 if the element is not found.
|
|
726
|
+
* @param {E} element - The "element" parameter represents the element that you want to find the
|
|
727
|
+
* index of in the data structure.
|
|
728
|
+
* @returns The indexOf function returns the index of the first occurrence of the specified element
|
|
729
|
+
* in the data structure. If the element is not found, it returns -1.
|
|
730
|
+
*/
|
|
486
731
|
indexOf(element) {
|
|
487
732
|
for (let i = 0; i < this.size; ++i) {
|
|
488
733
|
if (this.getAt(i) === element) {
|
|
@@ -491,11 +736,35 @@ export class Deque {
|
|
|
491
736
|
}
|
|
492
737
|
return -1;
|
|
493
738
|
}
|
|
739
|
+
/**
|
|
740
|
+
* Time Complexity: O(n)
|
|
741
|
+
* Space Complexity: O(1)
|
|
742
|
+
*/
|
|
743
|
+
/**
|
|
744
|
+
* Time Complexity: O(n)
|
|
745
|
+
* Space Complexity: O(1)
|
|
746
|
+
*
|
|
747
|
+
* The above function is an implementation of the iterator protocol in TypeScript, allowing the
|
|
748
|
+
* object to be iterated over using a for...of loop.
|
|
749
|
+
*/
|
|
494
750
|
*[Symbol.iterator]() {
|
|
495
751
|
for (let i = 0; i < this.size; ++i) {
|
|
496
752
|
yield this.getAt(i);
|
|
497
753
|
}
|
|
498
754
|
}
|
|
755
|
+
/**
|
|
756
|
+
* Time Complexity: O(n)
|
|
757
|
+
* Space Complexity: O(n)
|
|
758
|
+
*/
|
|
759
|
+
/**
|
|
760
|
+
* Time Complexity: O(n)
|
|
761
|
+
* Space Complexity: O(n)
|
|
762
|
+
*
|
|
763
|
+
* The `_reallocate` function reallocates the buckets in an array, adding new buckets if needed.
|
|
764
|
+
* @param {number} [needBucketNum] - The `needBucketNum` parameter is an optional number that
|
|
765
|
+
* specifies the number of new buckets needed. If not provided, it will default to half of the
|
|
766
|
+
* current bucket count (`this._bucketCount >> 1`) or 1 if the current bucket count is less than 2.
|
|
767
|
+
*/
|
|
499
768
|
_reallocate(needBucketNum) {
|
|
500
769
|
const newBuckets = [];
|
|
501
770
|
const addBucketNum = needBucketNum || this._bucketCount >> 1 || 1;
|
|
@@ -517,6 +786,19 @@ export class Deque {
|
|
|
517
786
|
this._buckets = newBuckets;
|
|
518
787
|
this._bucketCount = newBuckets.length;
|
|
519
788
|
}
|
|
789
|
+
/**
|
|
790
|
+
* Time Complexity: O(1)
|
|
791
|
+
* Space Complexity: O(1)
|
|
792
|
+
*/
|
|
793
|
+
/**
|
|
794
|
+
* Time Complexity: O(1)
|
|
795
|
+
* Space Complexity: O(1)
|
|
796
|
+
*
|
|
797
|
+
* The function calculates the bucket index and index within the bucket based on the given position.
|
|
798
|
+
* @param {number} pos - The `pos` parameter represents the position within the data structure. It is
|
|
799
|
+
* a number that indicates the index or position of an element within the structure.
|
|
800
|
+
* @returns an object with two properties: "bucketIndex" and "indexInBucket".
|
|
801
|
+
*/
|
|
520
802
|
_getBucketAndPosition(pos) {
|
|
521
803
|
let bucketIndex;
|
|
522
804
|
let indexInBucket;
|
|
@@ -11,9 +11,9 @@ export declare const enum IterateDirection {
|
|
|
11
11
|
REVERSE = 1
|
|
12
12
|
}
|
|
13
13
|
export interface IterableWithSize<T> extends Iterable<T> {
|
|
14
|
-
size: number;
|
|
14
|
+
size: number | ((...args: any[]) => number);
|
|
15
15
|
}
|
|
16
16
|
export interface IterableWithLength<T> extends Iterable<T> {
|
|
17
|
-
length: number;
|
|
17
|
+
length: number | ((...args: any[]) => number);
|
|
18
18
|
}
|
|
19
19
|
export type IterableWithSizeOrLength<T> = IterableWithSize<T> | IterableWithLength<T>;
|