min-heap-typed 1.45.3 → 1.46.2

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.
@@ -1,6 +1,4 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ArrayDeque = exports.ObjectDeque = exports.Deque = void 0;
4
2
  /**
5
3
  * data-structure-typed
6
4
  *
@@ -8,63 +6,283 @@ exports.ArrayDeque = exports.ObjectDeque = exports.Deque = void 0;
8
6
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
9
7
  * @license MIT License
10
8
  */
11
- const linked_list_1 = require("../linked-list");
12
- // O(n) time complexity of obtaining the value
13
- // O(1) time complexity of adding at the beginning and the end
14
- class Deque extends linked_list_1.DoublyLinkedList {
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.ObjectDeque = exports.Deque = exports.DequeIterator = void 0;
11
+ const utils_1 = require("../../utils");
12
+ /**
13
+ * Deque can provide random access with O(1) time complexity
14
+ * Deque is usually more compact and efficient in memory usage because it does not require additional space to store pointers.
15
+ * Deque may experience performance jitter, but DoublyLinkedList will not
16
+ * Deque is implemented using a dynamic array. Inserting or deleting beyond both ends of the array may require moving elements or reallocating space.
17
+ */
18
+ class DequeIterator {
19
+ /**
20
+ * The constructor initializes the index, iterate direction, and prev/next functions for a
21
+ * DequeIterator object.
22
+ * @param {number} index - The index parameter represents the current index position of the iterator
23
+ * within the deque. It is a number that indicates the position of the element that the iterator is
24
+ * currently pointing to.
25
+ * @param deque - The `deque` parameter is an instance of the `Deque` class. It represents a
26
+ * double-ended queue data structure, which allows elements to be added or removed from both ends.
27
+ * @param iterateDirection - The `iterateDirection` parameter is an optional parameter that specifies
28
+ * the direction in which the iterator should iterate over the elements of the `deque`. It has a
29
+ * default value of `IterateDirection.DEFAULT`.
30
+ * @returns The constructor is not returning anything. It is used to initialize the properties of the
31
+ * object being created.
32
+ */
33
+ constructor(index, deque, iterateDirection = 0 /* IterateDirection.DEFAULT */) {
34
+ this.index = index;
35
+ this.iterateDirection = iterateDirection;
36
+ if (this.iterateDirection === 0 /* IterateDirection.DEFAULT */) {
37
+ this.prev = function () {
38
+ if (this.index === 0) {
39
+ (0, utils_1.throwRangeError)();
40
+ }
41
+ this.index -= 1;
42
+ return this;
43
+ };
44
+ this.next = function () {
45
+ if (this.index === this.deque.size) {
46
+ (0, utils_1.throwRangeError)();
47
+ }
48
+ this.index += 1;
49
+ return this;
50
+ };
51
+ }
52
+ else {
53
+ this.prev = function () {
54
+ if (this.index === this.deque.size - 1) {
55
+ (0, utils_1.throwRangeError)();
56
+ }
57
+ this.index += 1;
58
+ return this;
59
+ };
60
+ this.next = function () {
61
+ if (this.index === -1) {
62
+ (0, utils_1.throwRangeError)();
63
+ }
64
+ this.index -= 1;
65
+ return this;
66
+ };
67
+ }
68
+ this.deque = deque;
69
+ }
70
+ get current() {
71
+ return this.deque.getAt(this.index);
72
+ }
73
+ set current(newElement) {
74
+ this.deque.setAt(this.index, newElement);
75
+ }
76
+ isAccessible() {
77
+ return this.index !== this.deque.size;
78
+ }
79
+ prev() {
80
+ return this;
81
+ }
82
+ next() {
83
+ return this;
84
+ }
85
+ clone() {
86
+ return new DequeIterator(this.index, this.deque, this.iterateDirection);
87
+ }
15
88
  }
16
- exports.Deque = Deque;
17
- // O(1) time complexity of obtaining the value
18
- // O(n) time complexity of adding at the beginning and the end
19
- // todo tested slowest one
20
- class ObjectDeque {
21
- constructor(capacity) {
22
- this._nodes = {};
23
- this._capacity = Number.MAX_SAFE_INTEGER;
24
- this._first = -1;
25
- this._last = -1;
89
+ exports.DequeIterator = DequeIterator;
90
+ class Deque {
91
+ /**
92
+ * The constructor initializes a data structure with a specified bucket size and populates it with
93
+ * elements from an iterable.
94
+ * @param elements - The `elements` parameter is an iterable object (such as an array or a Set) that
95
+ * contains the initial elements to be stored in the data structure. It can also be an object with a
96
+ * `length` property or a `size` property, which represents the number of elements in the iterable.
97
+ * @param bucketSize - The `bucketSize` parameter is the maximum number of elements that can be
98
+ * stored in each bucket. It determines the size of each bucket in the data structure.
99
+ */
100
+ constructor(elements = [], bucketSize = (1 << 12)) {
101
+ this._bucketFirst = 0;
102
+ this._firstInBucket = 0;
103
+ this._bucketLast = 0;
104
+ this._lastInBucket = 0;
105
+ this._bucketCount = 0;
106
+ this._buckets = [];
26
107
  this._size = 0;
27
- if (capacity !== undefined)
28
- this._capacity = capacity;
108
+ let _size;
109
+ if ('length' in elements) {
110
+ if (elements.length instanceof Function)
111
+ _size = elements.length();
112
+ else
113
+ _size = elements.length;
114
+ }
115
+ else {
116
+ if (elements.size instanceof Function)
117
+ _size = elements.size();
118
+ else
119
+ _size = elements.size;
120
+ }
121
+ this._bucketSize = bucketSize;
122
+ this._bucketCount = (0, utils_1.calcMinUnitsRequired)(_size, this._bucketSize) || 1;
123
+ for (let i = 0; i < this._bucketCount; ++i) {
124
+ this._buckets.push(new Array(this._bucketSize));
125
+ }
126
+ const needBucketNum = (0, utils_1.calcMinUnitsRequired)(_size, this._bucketSize);
127
+ this._bucketFirst = this._bucketLast = (this._bucketCount >> 1) - (needBucketNum >> 1);
128
+ this._firstInBucket = this._lastInBucket = (this._bucketSize - _size % this._bucketSize) >> 1;
129
+ for (const element of elements) {
130
+ this.push(element);
131
+ }
29
132
  }
30
- get nodes() {
31
- return this._nodes;
133
+ get buckets() {
134
+ return this._buckets;
32
135
  }
33
- get capacity() {
34
- return this._capacity;
136
+ get size() {
137
+ return this._size;
35
138
  }
139
+ /**
140
+ * The function returns the first element in a collection if it exists, otherwise it returns
141
+ * undefined.
142
+ * @returns The first element of the collection, of type E, is being returned.
143
+ */
36
144
  get first() {
37
- return this._first;
145
+ if (this.size === 0)
146
+ return;
147
+ return this._buckets[this._bucketFirst][this._firstInBucket];
38
148
  }
39
149
  get last() {
40
- return this._last;
150
+ if (this.size === 0)
151
+ return;
152
+ return this._buckets[this._bucketLast][this._lastInBucket];
41
153
  }
42
- get size() {
43
- return this._size;
154
+ /**
155
+ * Time Complexity: O(1) - Removes the last element.
156
+ * Space Complexity: O(1) - Operates in-place.
157
+ */
158
+ isEmpty() {
159
+ return this.size === 0;
44
160
  }
45
161
  /**
46
- * Time Complexity: O(1)
47
- * Space Complexity: O(1)
162
+ * Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
163
+ * Space Complexity: O(n) - Due to potential resizing.
48
164
  */
49
165
  /**
50
166
  * Time Complexity: O(1)
51
- * Space Complexity: O(1)
167
+ * Space Complexity: O(n) - In worst case, resizing doubles the array size.
168
+ *
169
+ * The addLast function adds an element to the end of an array.
170
+ * @param {E} element - The element parameter represents the element that you want to add to the end of the
171
+ * data structure.
172
+ */
173
+ addLast(element) {
174
+ this.push(element);
175
+ }
176
+ /**
177
+ * Time Complexity: O(1) - Removes the first element.
178
+ * Space Complexity: O(1) - In-place operation.
179
+ */
180
+ /**
181
+ * Time Complexity: O(1) - Removes the last element.
182
+ * Space Complexity: O(1) - Operates in-place.
52
183
  *
53
- * The "addFirst" function adds a value to the beginning of an array-like data structure.
54
- * @param {E} value - The `value` parameter represents the value that you want to add to the beginning of the data
184
+ * The function "popLast" removes and returns the last element of an array.
185
+ * @returns The last element of the array is being returned.
186
+ */
187
+ popLast() {
188
+ return this.pop();
189
+ }
190
+ /**
191
+ * Time Complexity: O(1).
192
+ * Space Complexity: O(n) - Due to potential resizing.
193
+ *
194
+ * The "addFirst" function adds an element to the beginning of an array.
195
+ * @param {E} element - The parameter "element" represents the element that you want to add to the
196
+ * beginning of the data structure.
197
+ */
198
+ addFirst(element) {
199
+ this.unshift(element);
200
+ }
201
+ /**
202
+ * Time Complexity: O(1) - Removes the first element.
203
+ * Space Complexity: O(1) - In-place operation.
204
+ *
205
+ * The function "popFirst" removes and returns the first element of an array.
206
+ * @returns The method `popFirst()` is returning the first element of the array after removing it
207
+ * from the beginning. If the array is empty, it will return `undefined`.
208
+ */
209
+ popFirst() {
210
+ return this.shift();
211
+ }
212
+ /**
213
+ * The clear() function resets the state of the object by initializing all variables to their default
214
+ * values.
215
+ */
216
+ clear() {
217
+ this._buckets = [new Array(this._bucketSize)];
218
+ this._bucketCount = 1;
219
+ this._bucketFirst = this._bucketLast = this._size = 0;
220
+ this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
221
+ }
222
+ /**
223
+ * The `begin()` function returns a new iterator for a deque starting from the first element.
224
+ * @returns A new instance of the DequeIterator class is being returned.
225
+ */
226
+ begin() {
227
+ return new DequeIterator(0, this);
228
+ }
229
+ /**
230
+ * The `end()` function returns a new `DequeIterator` object with the size and reference to the
231
+ * current deque.
232
+ * @returns A new instance of the DequeIterator class is being returned.
233
+ */
234
+ end() {
235
+ return new DequeIterator(this.size, this);
236
+ }
237
+ /**
238
+ * The reverseBegin function returns a new DequeIterator object that starts at the last element of
239
+ * the deque and iterates in reverse direction.
240
+ * @returns A new instance of the DequeIterator class is being returned.
241
+ */
242
+ reverseBegin() {
243
+ return new DequeIterator(this.size - 1, this, 1 /* IterateDirection.REVERSE */);
244
+ }
245
+ /**
246
+ * The reverseEnd() function returns a new DequeIterator object that iterates over the elements of a
247
+ * Deque in reverse order.
248
+ * @returns A new instance of the DequeIterator class is being returned.
249
+ */
250
+ reverseEnd() {
251
+ return new DequeIterator(-1, this, 1 /* IterateDirection.REVERSE */);
252
+ }
253
+ /**
254
+ * Time Complexity - Amortized O(1) (possible reallocation)
255
+ * Space Complexity - O(n) (due to potential resizing).
256
+ */
257
+ /**
258
+ * Time Complexity - Amortized O(1) (possible reallocation),
259
+ * Space Complexity - O(n) (due to potential resizing).
260
+ *
261
+ * The push function adds an element to a data structure and reallocates memory if necessary.
262
+ * @param {E} element - The `element` parameter represents the value that you want to add to the data
55
263
  * structure.
264
+ * @returns The size of the data structure after the element has been pushed.
56
265
  */
57
- addFirst(value) {
58
- if (this.size === 0) {
59
- const mid = Math.floor(this.capacity / 2);
60
- this._first = mid;
61
- this._last = mid;
266
+ push(element) {
267
+ if (this.size) {
268
+ if (this._lastInBucket < this._bucketSize - 1) {
269
+ this._lastInBucket += 1;
270
+ }
271
+ else if (this._bucketLast < this._bucketCount - 1) {
272
+ this._bucketLast += 1;
273
+ this._lastInBucket = 0;
274
+ }
275
+ else {
276
+ this._bucketLast = 0;
277
+ this._lastInBucket = 0;
278
+ }
279
+ if (this._bucketLast === this._bucketFirst &&
280
+ this._lastInBucket === this._firstInBucket)
281
+ this._reallocate();
62
282
  }
63
- else {
64
- this._first--;
65
- }
66
- this.nodes[this.first] = value;
67
- this._size++;
283
+ this._size += 1;
284
+ this._buckets[this._bucketLast][this._lastInBucket] = element;
285
+ return this.size;
68
286
  }
69
287
  /**
70
288
  * Time Complexity: O(1)
@@ -74,20 +292,64 @@ class ObjectDeque {
74
292
  * Time Complexity: O(1)
75
293
  * Space Complexity: O(1)
76
294
  *
77
- * The addLast function adds a value to the end of an array-like data structure.
78
- * @param {E} value - The `value` parameter represents the value that you want to add to the end of the data structure.
295
+ * The `pop()` function removes and returns the last element from a data structure, updating the
296
+ * internal state variables accordingly.
297
+ * @returns The element that was removed from the data structure is being returned.
79
298
  */
80
- addLast(value) {
81
- if (this.size === 0) {
82
- const mid = Math.floor(this.capacity / 2);
83
- this._first = mid;
84
- this._last = mid;
299
+ pop() {
300
+ if (this.size === 0)
301
+ return;
302
+ const element = this._buckets[this._bucketLast][this._lastInBucket];
303
+ if (this.size !== 1) {
304
+ if (this._lastInBucket > 0) {
305
+ this._lastInBucket -= 1;
306
+ }
307
+ else if (this._bucketLast > 0) {
308
+ this._bucketLast -= 1;
309
+ this._lastInBucket = this._bucketSize - 1;
310
+ }
311
+ else {
312
+ this._bucketLast = this._bucketCount - 1;
313
+ this._lastInBucket = this._bucketSize - 1;
314
+ }
85
315
  }
86
- else {
87
- this._last++;
316
+ this._size -= 1;
317
+ return element;
318
+ }
319
+ /**
320
+ * Time Complexity: Amortized O(1)
321
+ * Space Complexity: O(n)
322
+ */
323
+ /**
324
+ * Time Complexity: Amortized O(1)
325
+ * Space Complexity: O(n)
326
+ *
327
+ * The `unshift` function adds an element to the beginning of an array-like data structure and
328
+ * returns the new size of the structure.
329
+ * @param {E} element - The `element` parameter represents the element that you want to add to the
330
+ * beginning of the data structure.
331
+ * @returns The size of the data structure after the element has been added.
332
+ */
333
+ unshift(element) {
334
+ if (this.size) {
335
+ if (this._firstInBucket > 0) {
336
+ this._firstInBucket -= 1;
337
+ }
338
+ else if (this._bucketFirst > 0) {
339
+ this._bucketFirst -= 1;
340
+ this._firstInBucket = this._bucketSize - 1;
341
+ }
342
+ else {
343
+ this._bucketFirst = this._bucketCount - 1;
344
+ this._firstInBucket = this._bucketSize - 1;
345
+ }
346
+ if (this._bucketFirst === this._bucketLast &&
347
+ this._firstInBucket === this._lastInBucket)
348
+ this._reallocate();
88
349
  }
89
- this.nodes[this.last] = value;
90
- this._size++;
350
+ this._size += 1;
351
+ this._buckets[this._bucketFirst][this._firstInBucket] = element;
352
+ return this.size;
91
353
  }
92
354
  /**
93
355
  * Time Complexity: O(1)
@@ -97,17 +359,30 @@ class ObjectDeque {
97
359
  * Time Complexity: O(1)
98
360
  * Space Complexity: O(1)
99
361
  *
100
- * The function `popFirst()` removes and returns the first element in a data structure.
101
- * @returns The value of the first element in the data structure.
362
+ * The `shift()` function removes and returns the first element from a data structure, updating the
363
+ * internal state variables accordingly.
364
+ * @returns The element that is being removed from the beginning of the data structure is being
365
+ * returned.
102
366
  */
103
- popFirst() {
104
- if (!this.size)
367
+ shift() {
368
+ if (this.size === 0)
105
369
  return;
106
- const value = this.getFirst();
107
- delete this.nodes[this.first];
108
- this._first++;
109
- this._size--;
110
- return value;
370
+ const element = this._buckets[this._bucketFirst][this._firstInBucket];
371
+ if (this.size !== 1) {
372
+ if (this._firstInBucket < this._bucketSize - 1) {
373
+ this._firstInBucket += 1;
374
+ }
375
+ else if (this._bucketFirst < this._bucketCount - 1) {
376
+ this._bucketFirst += 1;
377
+ this._firstInBucket = 0;
378
+ }
379
+ else {
380
+ this._bucketFirst = 0;
381
+ this._firstInBucket = 0;
382
+ }
383
+ }
384
+ this._size -= 1;
385
+ return element;
111
386
  }
112
387
  /**
113
388
  * Time Complexity: O(1)
@@ -117,12 +392,16 @@ class ObjectDeque {
117
392
  * Time Complexity: O(1)
118
393
  * Space Complexity: O(1)
119
394
  *
120
- * The `getFirst` function returns the first element in an array-like data structure if it exists.
121
- * @returns The element at the first position of the `_nodes` array.
395
+ * The `getAt` function retrieves an element at a specified position in an array-like data structure.
396
+ * @param {number} pos - The `pos` parameter represents the position of the element that you want to
397
+ * retrieve from the data structure. It is of type `number` and should be a valid index within the
398
+ * range of the data structure.
399
+ * @returns The element at the specified position in the data structure is being returned.
122
400
  */
123
- getFirst() {
124
- if (this.size)
125
- return this.nodes[this.first];
401
+ getAt(pos) {
402
+ utils_1.rangeCheck(pos, 0, this.size - 1);
403
+ const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
404
+ return this._buckets[bucketIndex][indexInBucket];
126
405
  }
127
406
  /**
128
407
  * Time Complexity: O(1)
@@ -132,17 +411,59 @@ class ObjectDeque {
132
411
  * Time Complexity: O(1)
133
412
  * Space Complexity: O(1)
134
413
  *
135
- * The `popLast()` function removes and returns the last element in a data structure.
136
- * @returns The value that was removed from the data structure.
414
+ * The `setAt` function sets an element at a specific position in an array-like data structure.
415
+ * @param {number} pos - The `pos` parameter represents the position at which the element needs to be
416
+ * set. It is of type `number`.
417
+ * @param {E} element - The `element` parameter is the value that you want to set at the specified
418
+ * position in the data structure.
137
419
  */
138
- popLast() {
139
- if (!this.size)
140
- return;
141
- const value = this.getLast();
142
- delete this.nodes[this.last];
143
- this._last--;
144
- this._size--;
145
- return value;
420
+ setAt(pos, element) {
421
+ utils_1.rangeCheck(pos, 0, this.size - 1);
422
+ const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
423
+ this._buckets[bucketIndex][indexInBucket] = element;
424
+ }
425
+ /**
426
+ * Time Complexity: O(n)
427
+ * Space Complexity: O(n)
428
+ */
429
+ /**
430
+ * Time Complexity: O(n)
431
+ * Space Complexity: O(n)
432
+ *
433
+ * The `insertAt` function inserts one or more elements at a specified position in an array-like data
434
+ * structure.
435
+ * @param {number} pos - The `pos` parameter represents the position at which the element(s) should
436
+ * be inserted. It is of type `number`.
437
+ * @param {E} element - The `element` parameter represents the element that you want to insert into
438
+ * the array at the specified position.
439
+ * @param [num=1] - The `num` parameter represents the number of times the `element` should be
440
+ * inserted at the specified position (`pos`). By default, it is set to 1, meaning that the `element`
441
+ * will be inserted once. However, you can provide a different value for `num` if you want
442
+ * @returns The size of the array after the insertion is being returned.
443
+ */
444
+ insertAt(pos, element, num = 1) {
445
+ const length = this.size;
446
+ utils_1.rangeCheck(pos, 0, length);
447
+ if (pos === 0) {
448
+ while (num--)
449
+ this.unshift(element);
450
+ }
451
+ else if (pos === this.size) {
452
+ while (num--)
453
+ this.push(element);
454
+ }
455
+ else {
456
+ const arr = [];
457
+ for (let i = pos; i < this.size; ++i) {
458
+ arr.push(this.getAt(i));
459
+ }
460
+ this.cut(pos - 1);
461
+ for (let i = 0; i < num; ++i)
462
+ this.push(element);
463
+ for (let i = 0; i < arr.length; ++i)
464
+ this.push(arr[i]);
465
+ }
466
+ return this.size;
146
467
  }
147
468
  /**
148
469
  * Time Complexity: O(1)
@@ -152,80 +473,258 @@ class ObjectDeque {
152
473
  * Time Complexity: O(1)
153
474
  * Space Complexity: O(1)
154
475
  *
155
- * The `getLast()` function returns the last element in an array-like data structure.
156
- * @returns The last element in the array "_nodes" is being returned.
476
+ * The `cut` function updates the state of the object based on the given position and returns the
477
+ * updated size.
478
+ * @param {number} pos - The `pos` parameter represents the position at which the string should be
479
+ * cut. It is a number that indicates the index of the character where the cut should be made.
480
+ * @returns The method is returning the updated size of the data structure.
157
481
  */
158
- getLast() {
159
- if (this.size)
160
- return this.nodes[this.last];
482
+ cut(pos) {
483
+ if (pos < 0) {
484
+ this.clear();
485
+ return 0;
486
+ }
487
+ const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
488
+ this._bucketLast = bucketIndex;
489
+ this._lastInBucket = indexInBucket;
490
+ this._size = pos + 1;
491
+ return this.size;
161
492
  }
162
493
  /**
163
- * Time Complexity: O(1)
494
+ * Time Complexity: O(n)
164
495
  * Space Complexity: O(1)
165
496
  */
166
497
  /**
167
- * Time Complexity: O(1)
498
+ * Time Complexity: O(n)
168
499
  * Space Complexity: O(1)
169
500
  *
170
- * The get function returns the element at the specified index in an array-like data structure.
171
- * @param {number} index - The index parameter is a number that represents the position of the element you want to
172
- * retrieve from the array.
173
- * @returns The element at the specified index in the `_nodes` array is being returned. If there is no element at that
174
- * index, `null` is returned.
501
+ * The `deleteAt` function removes an element at a specified position in an array-like data
502
+ * structure.
503
+ * @param {number} pos - The `pos` parameter in the `deleteAt` function represents the position at
504
+ * which an element needs to be deleted from the data structure. It is of type `number` and indicates
505
+ * the index of the element to be deleted.
506
+ * @returns The size of the data structure after the deletion operation is performed.
175
507
  */
176
- get(index) {
177
- return this.nodes[this.first + index] || null;
508
+ deleteAt(pos) {
509
+ utils_1.rangeCheck(pos, 0, this.size - 1);
510
+ if (pos === 0)
511
+ this.shift();
512
+ else if (pos === this.size - 1)
513
+ this.pop();
514
+ else {
515
+ const length = this.size - 1;
516
+ let { bucketIndex: curBucket, indexInBucket: curPointer } = this._getBucketAndPosition(pos);
517
+ for (let i = pos; i < length; ++i) {
518
+ const { bucketIndex: nextBucket, indexInBucket: nextPointer } = this._getBucketAndPosition(pos + 1);
519
+ this._buckets[curBucket][curPointer] = this._buckets[nextBucket][nextPointer];
520
+ curBucket = nextBucket;
521
+ curPointer = nextPointer;
522
+ }
523
+ this.pop();
524
+ }
525
+ return this.size;
178
526
  }
179
527
  /**
180
- * The function checks if the size of a data structure is less than or equal to zero.
181
- * @returns The method is returning a boolean value indicating whether the size of the object is less than or equal to 0.
528
+ * Time Complexity: O(n)
529
+ * Space Complexity: O(1)
182
530
  */
183
- isEmpty() {
184
- return this.size <= 0;
531
+ /**
532
+ * Time Complexity: O(n)
533
+ * Space Complexity: O(1)
534
+ *
535
+ * The `delete` function removes all occurrences of a specified element from an array-like data
536
+ * structure.
537
+ * @param {E} element - The `element` parameter represents the element that you want to delete from
538
+ * the data structure.
539
+ * @returns The size of the data structure after the element has been deleted.
540
+ */
541
+ delete(element) {
542
+ const size = this.size;
543
+ if (size === 0)
544
+ return 0;
545
+ let i = 0;
546
+ let index = 0;
547
+ while (i < size) {
548
+ const oldElement = this.getAt(i);
549
+ if (oldElement !== element) {
550
+ this.setAt(index, oldElement);
551
+ index += 1;
552
+ }
553
+ i += 1;
554
+ }
555
+ this.cut(index - 1);
556
+ return this.size;
185
557
  }
186
- }
187
- exports.ObjectDeque = ObjectDeque;
188
- // O(1) time complexity of obtaining the value
189
- // O(n) time complexity of adding at the beginning and the end
190
- class ArrayDeque {
191
- constructor() {
192
- this._nodes = [];
558
+ /**
559
+ * Time Complexity: O(n)
560
+ * Space Complexity: O(1)
561
+ */
562
+ /**
563
+ * Time Complexity: O(n)
564
+ * Space Complexity: O(1)
565
+ *
566
+ * The function deletes an element from a deque using an iterator and returns the next iterator.
567
+ * @param iter - The parameter `iter` is of type `DequeIterator<E>`. It represents an iterator object
568
+ * that is used to iterate over elements in a deque (double-ended queue).
569
+ * @returns the updated iterator after deleting an element from the deque.
570
+ */
571
+ deleteByIterator(iter) {
572
+ const index = iter.index;
573
+ this.deleteAt(index);
574
+ iter = iter.next();
575
+ return iter;
193
576
  }
194
- get nodes() {
195
- return this._nodes;
577
+ /**
578
+ * Time Complexity: O(n)
579
+ * Space Complexity: O(1)
580
+ */
581
+ /**
582
+ * Time Complexity: O(n)
583
+ * Space Complexity: O(1)
584
+ *
585
+ * The function `findIterator` searches for an element in a deque and returns an iterator pointing to
586
+ * the element if found, otherwise it returns an iterator pointing to the end of the deque.
587
+ * @param {E} element - The `element` parameter is the element that you want to find in the deque.
588
+ * @returns The method `findIterator(element: E)` returns a `DequeIterator<E>` object.
589
+ */
590
+ findIterator(element) {
591
+ for (let i = 0; i < this.size; ++i) {
592
+ if (this.getAt(i) === element) {
593
+ return new DequeIterator(i, this);
594
+ }
595
+ }
596
+ return this.end();
196
597
  }
197
- get size() {
198
- return this.nodes.length;
598
+ /**
599
+ * Time Complexity: O(n)
600
+ * Space Complexity: O(1)
601
+ */
602
+ /**
603
+ * Time Complexity: O(n)
604
+ * Space Complexity: O(1)
605
+ *
606
+ * The reverse() function reverses the order of the buckets and the elements within each bucket in a
607
+ * data structure.
608
+ * @returns The reverse() method is returning the object itself (this) after performing the reverse
609
+ * operation on the buckets and updating the relevant properties.
610
+ */
611
+ reverse() {
612
+ this._buckets.reverse().forEach(function (bucket) {
613
+ bucket.reverse();
614
+ });
615
+ const { _bucketFirst, _bucketLast, _firstInBucket, _lastInBucket } = this;
616
+ this._bucketFirst = this._bucketCount - _bucketLast - 1;
617
+ this._bucketLast = this._bucketCount - _bucketFirst - 1;
618
+ this._firstInBucket = this._bucketSize - _lastInBucket - 1;
619
+ this._lastInBucket = this._bucketSize - _firstInBucket - 1;
620
+ return this;
199
621
  }
200
622
  /**
201
- * Time Complexity: O(1)
623
+ * Time Complexity: O(n)
202
624
  * Space Complexity: O(1)
203
625
  */
204
626
  /**
205
- * Time Complexity: O(1)
627
+ * Time Complexity: O(n)
206
628
  * Space Complexity: O(1)
207
629
  *
208
- * The function "addLast" adds a value to the end of an array.
209
- * @param {E} value - The value parameter represents the value that you want to add to the end of the array.
210
- * @returns The return value is the new length of the array after the value has been added.
630
+ * The `unique()` function removes duplicate elements from an array-like data structure and returns
631
+ * the number of unique elements.
632
+ * @returns The size of the modified array is being returned.
211
633
  */
212
- addLast(value) {
213
- return this.nodes.push(value);
634
+ unique() {
635
+ if (this.size <= 1) {
636
+ return this.size;
637
+ }
638
+ let index = 1;
639
+ let prev = this.getAt(0);
640
+ for (let i = 1; i < this.size; ++i) {
641
+ const cur = this.getAt(i);
642
+ if (cur !== prev) {
643
+ prev = cur;
644
+ this.setAt(index++, cur);
645
+ }
646
+ }
647
+ this.cut(index - 1);
648
+ return this.size;
214
649
  }
215
650
  /**
216
- * Time Complexity: O(1)
651
+ * Time Complexity: O(n log n)
652
+ * Space Complexity: O(n)
653
+ */
654
+ /**
655
+ * Time Complexity: O(n log n)
656
+ * Space Complexity: O(n)
657
+ *
658
+ * The `sort` function sorts the elements in a data structure using a provided comparator function.
659
+ * @param [comparator] - The `comparator` parameter is a function that takes in two elements `x` and
660
+ * `y` of type `E` and returns a number. The comparator function is used to determine the order of
661
+ * the elements in the sorted array.
662
+ * @returns The method is returning the sorted instance of the object on which the method is called.
663
+ */
664
+ sort(comparator) {
665
+ const arr = [];
666
+ for (let i = 0; i < this.size; ++i) {
667
+ arr.push(this.getAt(i));
668
+ }
669
+ arr.sort(comparator);
670
+ for (let i = 0; i < this.size; ++i) {
671
+ this.setAt(i, arr[i]);
672
+ }
673
+ return this;
674
+ }
675
+ /**
676
+ * Time Complexity: O(n)
677
+ * Space Complexity: O(n)
678
+ */
679
+ /**
680
+ * Time Complexity: O(n)
681
+ * Space Complexity: O(n)
682
+ *
683
+ * The `shrinkToFit` function reorganizes the elements in an array-like data structure to minimize
684
+ * memory usage.
685
+ * @returns Nothing is being returned. The function is using the `return` statement to exit early if
686
+ * `this.size` is 0, but it does not return any value.
687
+ */
688
+ shrinkToFit() {
689
+ if (this.size === 0)
690
+ return;
691
+ const newBuckets = [];
692
+ if (this._bucketFirst === this._bucketLast)
693
+ return;
694
+ else if (this._bucketFirst < this._bucketLast) {
695
+ for (let i = this._bucketFirst; i <= this._bucketLast; ++i) {
696
+ newBuckets.push(this._buckets[i]);
697
+ }
698
+ }
699
+ else {
700
+ for (let i = this._bucketFirst; i < this._bucketCount; ++i) {
701
+ newBuckets.push(this._buckets[i]);
702
+ }
703
+ for (let i = 0; i <= this._bucketLast; ++i) {
704
+ newBuckets.push(this._buckets[i]);
705
+ }
706
+ }
707
+ this._bucketFirst = 0;
708
+ this._bucketLast = newBuckets.length - 1;
709
+ this._buckets = newBuckets;
710
+ }
711
+ /**
712
+ * Time Complexity: O(n)
217
713
  * Space Complexity: O(1)
218
714
  */
219
715
  /**
220
- * Time Complexity: O(1)
716
+ * Time Complexity: O(n)
221
717
  * Space Complexity: O(1)
222
718
  *
223
- * The function "popLast" returns and removes the last element from an array, or returns null if the array is empty.
224
- * @returns The method `popLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
719
+ * The `forEach` function iterates over each element in a deque and applies a callback function to
720
+ * each element.
721
+ * @param callback - The callback parameter is a function that will be called for each element in the
722
+ * deque. It takes three parameters:
225
723
  */
226
- popLast() {
227
- var _a;
228
- return (_a = this.nodes.pop()) !== null && _a !== void 0 ? _a : null;
724
+ forEach(callback) {
725
+ for (let i = 0; i < this.size; ++i) {
726
+ callback(this.getAt(i), i, this);
727
+ }
229
728
  }
230
729
  /**
231
730
  * Time Complexity: O(n)
@@ -235,13 +734,131 @@ class ArrayDeque {
235
734
  * Time Complexity: O(n)
236
735
  * Space Complexity: O(1)
237
736
  *
238
- * The `popFirst` function removes and returns the first element from an array, or returns null if the array is empty.
239
- * @returns The `popFirst()` function returns the first element of the `_nodes` array, or `null` if the array is
240
- * empty.
737
+ * The `find` function iterates over the elements in a deque and returns the first element for which
738
+ * the callback function returns true, or undefined if no such element is found.
739
+ * @param callback - A function that takes three parameters: element, index, and deque. It should
740
+ * return a boolean value indicating whether the element satisfies a certain condition.
741
+ * @returns The method `find` returns the first element in the deque that satisfies the condition
742
+ * specified by the callback function. If no element satisfies the condition, it returns `undefined`.
241
743
  */
242
- popFirst() {
243
- var _a;
244
- return (_a = this.nodes.shift()) !== null && _a !== void 0 ? _a : null;
744
+ find(callback) {
745
+ for (let i = 0; i < this.size; ++i) {
746
+ const element = this.getAt(i);
747
+ if (callback(element, i, this)) {
748
+ return element;
749
+ }
750
+ }
751
+ return undefined;
752
+ }
753
+ /**
754
+ * Time Complexity: O(n)
755
+ * Space Complexity: O(n)
756
+ */
757
+ /**
758
+ * Time Complexity: O(n)
759
+ * Space Complexity: O(n)
760
+ *
761
+ * The `toArray` function converts the elements of a data structure into an array.
762
+ * @returns The `toArray()` method is returning an array of elements of type `E`.
763
+ */
764
+ toArray() {
765
+ const arr = [];
766
+ for (let i = 0; i < this.size; ++i) {
767
+ arr.push(this.getAt(i));
768
+ }
769
+ return arr;
770
+ }
771
+ /**
772
+ * Time Complexity: O(n)
773
+ * Space Complexity: O(n)
774
+ */
775
+ /**
776
+ * Time Complexity: O(n)
777
+ * Space Complexity: O(n)
778
+ *
779
+ * The `map` function takes a callback function and applies it to each element in the deque,
780
+ * returning a new deque with the results.
781
+ * @param callback - The `callback` parameter is a function that takes three arguments:
782
+ * @returns The `map` method is returning a new `Deque` object with the transformed elements.
783
+ */
784
+ map(callback) {
785
+ const newDeque = new Deque([], this._bucketSize);
786
+ for (let i = 0; i < this.size; ++i) {
787
+ newDeque.push(callback(this.getAt(i), i, this));
788
+ }
789
+ return newDeque;
790
+ }
791
+ /**
792
+ * Time Complexity: O(n)
793
+ * Space Complexity: O(n)
794
+ */
795
+ /**
796
+ * Time Complexity: O(n)
797
+ * Space Complexity: O(n)
798
+ *
799
+ * The `filter` function creates a new deque containing only the elements that satisfy the given
800
+ * predicate function.
801
+ * @param predicate - The `predicate` parameter is a function that takes three arguments: `element`,
802
+ * `index`, and `deque`.
803
+ * @returns The `filter` method is returning a new `Deque` object that contains only the elements
804
+ * that satisfy the given `predicate` function.
805
+ */
806
+ filter(predicate) {
807
+ const newDeque = new Deque([], this._bucketSize);
808
+ for (let i = 0; i < this.size; ++i) {
809
+ const element = this.getAt(i);
810
+ if (predicate(element, i, this)) {
811
+ newDeque.push(element);
812
+ }
813
+ }
814
+ return newDeque;
815
+ }
816
+ /**
817
+ * Time Complexity: O(n)
818
+ * Space Complexity: O(1)
819
+ */
820
+ /**
821
+ * Time Complexity: O(n)
822
+ * Space Complexity: O(1)
823
+ *
824
+ * The `reduce` function iterates over the elements of a deque and applies a callback function to
825
+ * each element, accumulating a single value.
826
+ * @param callback - The `callback` parameter is a function that takes four arguments:
827
+ * @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It
828
+ * is the value that will be passed as the first argument to the `callback` function when reducing
829
+ * the elements of the deque.
830
+ * @returns the final value of the accumulator after iterating over all elements in the deque and
831
+ * applying the callback function to each element.
832
+ */
833
+ reduce(callback, initialValue) {
834
+ let accumulator = initialValue;
835
+ for (let i = 0; i < this.size; ++i) {
836
+ accumulator = callback(accumulator, this.getAt(i), i, this);
837
+ }
838
+ return accumulator;
839
+ }
840
+ /**
841
+ * Time Complexity: O(n)
842
+ * Space Complexity: O(1)
843
+ */
844
+ /**
845
+ * Time Complexity: O(n)
846
+ * Space Complexity: O(1)
847
+ *
848
+ * The function "indexOf" returns the index of the first occurrence of a given element in an array,
849
+ * or -1 if the element is not found.
850
+ * @param {E} element - The "element" parameter represents the element that you want to find the
851
+ * index of in the data structure.
852
+ * @returns The indexOf function returns the index of the first occurrence of the specified element
853
+ * in the data structure. If the element is not found, it returns -1.
854
+ */
855
+ indexOf(element) {
856
+ for (let i = 0; i < this.size; ++i) {
857
+ if (this.getAt(i) === element) {
858
+ return i;
859
+ }
860
+ }
861
+ return -1;
245
862
  }
246
863
  /**
247
864
  * Time Complexity: O(n)
@@ -251,13 +868,47 @@ class ArrayDeque {
251
868
  * Time Complexity: O(n)
252
869
  * Space Complexity: O(1)
253
870
  *
254
- * The function "addFirst" adds a value to the beginning of an array.
255
- * @param {E} value - The value parameter represents the value that you want to add to the beginning of the array.
256
- * @returns The return value of the `addFirst` function is the new length of the array `_nodes` after adding the
257
- * `value` at the beginning.
871
+ * The above function is an implementation of the iterator protocol in TypeScript, allowing the
872
+ * object to be iterated over using a for...of loop.
258
873
  */
259
- addFirst(value) {
260
- return this.nodes.unshift(value);
874
+ *[Symbol.iterator]() {
875
+ for (let i = 0; i < this.size; ++i) {
876
+ yield this.getAt(i);
877
+ }
878
+ }
879
+ /**
880
+ * Time Complexity: O(n)
881
+ * Space Complexity: O(n)
882
+ */
883
+ /**
884
+ * Time Complexity: O(n)
885
+ * Space Complexity: O(n)
886
+ *
887
+ * The `_reallocate` function reallocates the buckets in an array, adding new buckets if needed.
888
+ * @param {number} [needBucketNum] - The `needBucketNum` parameter is an optional number that
889
+ * specifies the number of new buckets needed. If not provided, it will default to half of the
890
+ * current bucket count (`this._bucketCount >> 1`) or 1 if the current bucket count is less than 2.
891
+ */
892
+ _reallocate(needBucketNum) {
893
+ const newBuckets = [];
894
+ const addBucketNum = needBucketNum || this._bucketCount >> 1 || 1;
895
+ for (let i = 0; i < addBucketNum; ++i) {
896
+ newBuckets[i] = new Array(this._bucketSize);
897
+ }
898
+ for (let i = this._bucketFirst; i < this._bucketCount; ++i) {
899
+ newBuckets[newBuckets.length] = this._buckets[i];
900
+ }
901
+ for (let i = 0; i < this._bucketLast; ++i) {
902
+ newBuckets[newBuckets.length] = this._buckets[i];
903
+ }
904
+ newBuckets[newBuckets.length] = [...this._buckets[this._bucketLast]];
905
+ this._bucketFirst = addBucketNum;
906
+ this._bucketLast = newBuckets.length - 1;
907
+ for (let i = 0; i < addBucketNum; ++i) {
908
+ newBuckets[newBuckets.length] = new Array(this._bucketSize);
909
+ }
910
+ this._buckets = newBuckets;
911
+ this._bucketCount = newBuckets.length;
261
912
  }
262
913
  /**
263
914
  * Time Complexity: O(1)
@@ -267,13 +918,54 @@ class ArrayDeque {
267
918
  * Time Complexity: O(1)
268
919
  * Space Complexity: O(1)
269
920
  *
270
- * The `getFirst` function returns the first element of an array or null if the array is empty.
271
- * @returns The function `getFirst()` is returning the first element (`E`) of the `_nodes` array. If the array is
272
- * empty, it will return `null`.
921
+ * The function calculates the bucket index and index within the bucket based on the given position.
922
+ * @param {number} pos - The `pos` parameter represents the position within the data structure. It is
923
+ * a number that indicates the index or position of an element within the structure.
924
+ * @returns an object with two properties: "bucketIndex" and "indexInBucket".
273
925
  */
274
- getFirst() {
275
- var _a;
276
- return (_a = this.nodes[0]) !== null && _a !== void 0 ? _a : null;
926
+ _getBucketAndPosition(pos) {
927
+ let bucketIndex;
928
+ let indexInBucket;
929
+ const overallIndex = this._firstInBucket + pos;
930
+ bucketIndex = this._bucketFirst + Math.floor(overallIndex / this._bucketSize);
931
+ if (bucketIndex >= this._bucketCount) {
932
+ bucketIndex -= this._bucketCount;
933
+ }
934
+ indexInBucket = (overallIndex + 1) % this._bucketSize - 1;
935
+ if (indexInBucket < 0) {
936
+ indexInBucket = this._bucketSize - 1;
937
+ }
938
+ return { bucketIndex, indexInBucket };
939
+ }
940
+ }
941
+ exports.Deque = Deque;
942
+ // O(1) time complexity of obtaining the element
943
+ // O(n) time complexity of adding at the beginning and the end
944
+ // todo tested slowest one
945
+ class ObjectDeque {
946
+ constructor(capacity) {
947
+ this._nodes = {};
948
+ this._capacity = Number.MAX_SAFE_INTEGER;
949
+ this._first = -1;
950
+ this._last = -1;
951
+ this._size = 0;
952
+ if (capacity !== undefined)
953
+ this._capacity = capacity;
954
+ }
955
+ get nodes() {
956
+ return this._nodes;
957
+ }
958
+ get capacity() {
959
+ return this._capacity;
960
+ }
961
+ get first() {
962
+ return this._first;
963
+ }
964
+ get last() {
965
+ return this._last;
966
+ }
967
+ get size() {
968
+ return this._size;
277
969
  }
278
970
  /**
279
971
  * Time Complexity: O(1)
@@ -283,12 +975,21 @@ class ArrayDeque {
283
975
  * Time Complexity: O(1)
284
976
  * Space Complexity: O(1)
285
977
  *
286
- * The `getLast` function returns the last element of an array or null if the array is empty.
287
- * @returns The method `getLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
978
+ * The "addFirst" function adds an element to the beginning of an array-like data structure.
979
+ * @param {E} element - The `element` parameter represents the element that you want to add to the beginning of the data
980
+ * structure.
288
981
  */
289
- getLast() {
290
- var _a;
291
- return (_a = this.nodes[this.nodes.length - 1]) !== null && _a !== void 0 ? _a : null;
982
+ addFirst(element) {
983
+ if (this.size === 0) {
984
+ const mid = Math.floor(this.capacity / 2);
985
+ this._first = mid;
986
+ this._last = mid;
987
+ }
988
+ else {
989
+ this._first--;
990
+ }
991
+ this.nodes[this.first] = element;
992
+ this._size++;
292
993
  }
293
994
  /**
294
995
  * Time Complexity: O(1)
@@ -298,15 +999,20 @@ class ArrayDeque {
298
999
  * Time Complexity: O(1)
299
1000
  * Space Complexity: O(1)
300
1001
  *
301
- * The get function returns the element at the specified index in an array, or null if the index is out of bounds.
302
- * @param {number} index - The index parameter is a number that represents the position of the element you want to
303
- * retrieve from the array.
304
- * @returns The method is returning the element at the specified index in the `_nodes` array. If the element exists, it
305
- * will be returned. If the element does not exist (i.e., the index is out of bounds), `null` will be returned.
1002
+ * The addLast function adds an element to the end of an array-like data structure.
1003
+ * @param {E} element - The `element` parameter represents the element that you want to add to the end of the data structure.
306
1004
  */
307
- get(index) {
308
- var _a;
309
- return (_a = this.nodes[index]) !== null && _a !== void 0 ? _a : null;
1005
+ addLast(element) {
1006
+ if (this.size === 0) {
1007
+ const mid = Math.floor(this.capacity / 2);
1008
+ this._first = mid;
1009
+ this._last = mid;
1010
+ }
1011
+ else {
1012
+ this._last++;
1013
+ }
1014
+ this.nodes[this.last] = element;
1015
+ this._size++;
310
1016
  }
311
1017
  /**
312
1018
  * Time Complexity: O(1)
@@ -316,59 +1022,91 @@ class ArrayDeque {
316
1022
  * Time Complexity: O(1)
317
1023
  * Space Complexity: O(1)
318
1024
  *
319
- * The set function assigns a value to a specific index in an array.
320
- * @param {number} index - The index parameter is a number that represents the position of the element in the array
321
- * that you want to set a new value for.
322
- * @param {E} value - The value parameter represents the new value that you want to set at the specified index in the
323
- * _nodes array.
324
- * @returns The value that is being set at the specified index in the `_nodes` array.
1025
+ * The function `popFirst()` removes and returns the first element in a data structure.
1026
+ * @returns The element of the first element in the data structure.
325
1027
  */
326
- set(index, value) {
327
- return (this.nodes[index] = value);
1028
+ popFirst() {
1029
+ if (!this.size)
1030
+ return;
1031
+ const element = this.getFirst();
1032
+ delete this.nodes[this.first];
1033
+ this._first++;
1034
+ this._size--;
1035
+ return element;
328
1036
  }
329
1037
  /**
330
- * Time Complexity: O(n)
1038
+ * Time Complexity: O(1)
331
1039
  * Space Complexity: O(1)
332
1040
  */
333
1041
  /**
334
- * Time Complexity: O(n)
1042
+ * Time Complexity: O(1)
335
1043
  * Space Complexity: O(1)
336
1044
  *
337
- * The insert function adds a value at a specified index in an array.
338
- * @param {number} index - The index parameter specifies the position at which the value should be inserted in the
339
- * array. It is a number that represents the index of the array where the value should be inserted. The index starts
340
- * from 0, so the first element of the array has an index of 0, the second element has
341
- * @param {E} value - The value parameter represents the value that you want to insert into the array at the specified
342
- * index.
343
- * @returns The splice method returns an array containing the removed elements, if any. In this case, since no elements
344
- * are being removed, an empty array will be returned.
1045
+ * The `getFirst` function returns the first element in an array-like data structure if it exists.
1046
+ * @returns The element at the first position of the `_nodes` array.
345
1047
  */
346
- insert(index, value) {
347
- return this.nodes.splice(index, 0, value);
1048
+ getFirst() {
1049
+ if (this.size)
1050
+ return this.nodes[this.first];
348
1051
  }
349
1052
  /**
350
- * Time Complexity: O(n)
1053
+ * Time Complexity: O(1)
351
1054
  * Space Complexity: O(1)
352
1055
  */
353
1056
  /**
354
- * Time Complexity: O(n)
1057
+ * Time Complexity: O(1)
1058
+ * Space Complexity: O(1)
1059
+ *
1060
+ * The `popLast()` function removes and returns the last element in a data structure.
1061
+ * @returns The element that was removed from the data structure.
1062
+ */
1063
+ popLast() {
1064
+ if (!this.size)
1065
+ return;
1066
+ const element = this.getLast();
1067
+ delete this.nodes[this.last];
1068
+ this._last--;
1069
+ this._size--;
1070
+ return element;
1071
+ }
1072
+ /**
1073
+ * Time Complexity: O(1)
1074
+ * Space Complexity: O(1)
1075
+ */
1076
+ /**
1077
+ * Time Complexity: O(1)
1078
+ * Space Complexity: O(1)
1079
+ *
1080
+ * The `getLast()` function returns the last element in an array-like data structure.
1081
+ * @returns The last element in the array "_nodes" is being returned.
1082
+ */
1083
+ getLast() {
1084
+ if (this.size)
1085
+ return this.nodes[this.last];
1086
+ }
1087
+ /**
1088
+ * Time Complexity: O(1)
1089
+ * Space Complexity: O(1)
1090
+ */
1091
+ /**
1092
+ * Time Complexity: O(1)
355
1093
  * Space Complexity: O(1)
356
1094
  *
357
- * The delete function removes an element from an array at a specified index.
358
- * @param {number} index - The index parameter specifies the position of the element to be removed from the array. It
359
- * is a number that represents the index of the element to be removed.
360
- * @returns The method is returning an array containing the removed element.
1095
+ * The get function returns the element at the specified index in an array-like data structure.
1096
+ * @param {number} index - The index parameter is a number that represents the position of the element you want to
1097
+ * retrieve from the array.
1098
+ * @returns The element at the specified index in the `_nodes` array is being returned. If there is no element at that
1099
+ * index, `undefined` is returned.
361
1100
  */
362
- delete(index) {
363
- return this.nodes.splice(index, 1);
1101
+ get(index) {
1102
+ return this.nodes[this.first + index] || undefined;
364
1103
  }
365
1104
  /**
366
- * The function checks if an array called "_nodes" is empty.
367
- * @returns The method `isEmpty()` is returning a boolean value. It returns `true` if the length of the `_nodes` array
368
- * is 0, indicating that the array is empty. Otherwise, it returns `false`.
1105
+ * The function checks if the size of a data structure is less than or equal to zero.
1106
+ * @returns The method is returning a boolean element indicating whether the size of the object is less than or equal to 0.
369
1107
  */
370
1108
  isEmpty() {
371
- return this.nodes.length === 0;
1109
+ return this.size <= 0;
372
1110
  }
373
1111
  }
374
- exports.ArrayDeque = ArrayDeque;
1112
+ exports.ObjectDeque = ObjectDeque;