linked-list-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.
@@ -7,8 +7,8 @@
7
7
  */
8
8
 
9
9
 
10
- import { IterableWithSizeOrLength, IterateDirection } from "../../types";
11
- import { calcMinUnitsRequired, rangeCheck, throwRangeError } from "../../utils";
10
+ import { IterableWithSizeOrLength } from "../../types";
11
+ import { calcMinUnitsRequired, rangeCheck } from "../../utils";
12
12
 
13
13
  /**
14
14
  * Deque can provide random access with O(1) time complexity
@@ -17,75 +17,6 @@ import { calcMinUnitsRequired, rangeCheck, throwRangeError } from "../../utils";
17
17
  * Deque is implemented using a dynamic array. Inserting or deleting beyond both ends of the array may require moving elements or reallocating space.
18
18
  */
19
19
 
20
- export class DequeIterator<E> {
21
- iterateDirection: IterateDirection;
22
-
23
- index: number;
24
- readonly deque: Deque<E>;
25
-
26
- constructor(index: number, deque: Deque<E>, iterateDirection = IterateDirection.DEFAULT) {
27
- this.index = index;
28
- this.iterateDirection = iterateDirection;
29
- if (this.iterateDirection === IterateDirection.DEFAULT) {
30
- this.prev = function () {
31
- if (this.index === 0) {
32
- throwRangeError();
33
- }
34
- this.index -= 1;
35
- return this;
36
- };
37
- this.next = function () {
38
- if (this.index === this.deque.size) {
39
- throwRangeError();
40
- }
41
- this.index += 1;
42
- return this;
43
- };
44
- } else {
45
- this.prev = function () {
46
- if (this.index === this.deque.size - 1) {
47
- throwRangeError();
48
- }
49
- this.index += 1;
50
- return this;
51
- };
52
- this.next = function () {
53
- if (this.index === -1) {
54
- throwRangeError();
55
- }
56
- this.index -= 1;
57
- return this;
58
- };
59
- }
60
- this.deque = deque;
61
- }
62
-
63
- get current() {
64
- return this.deque.getAt(this.index);
65
- }
66
-
67
- set current(newElement: E) {
68
- this.deque.setAt(this.index, newElement);
69
- }
70
-
71
- isAccessible() {
72
- return this.index !== this.deque.size;
73
- }
74
-
75
- prev(): DequeIterator<E> {
76
- return this;
77
- }
78
-
79
- next(): DequeIterator<E> {
80
- return this;
81
- }
82
-
83
- clone() {
84
- return new DequeIterator<E>(this.index, this.deque, this.iterateDirection);
85
- }
86
-
87
- }
88
-
89
20
  export class Deque<E> {
90
21
  protected _bucketFirst = 0;
91
22
  protected _firstInBucket = 0;
@@ -94,13 +25,22 @@ export class Deque<E> {
94
25
  protected _bucketCount = 0;
95
26
  protected readonly _bucketSize: number;
96
27
 
28
+ /**
29
+ * The constructor initializes a data structure with a specified bucket size and populates it with
30
+ * elements from an iterable.
31
+ * @param elements - The `elements` parameter is an iterable object (such as an array or a Set) that
32
+ * contains the initial elements to be stored in the data structure. It can also be an object with a
33
+ * `length` property or a `size` property, which represents the number of elements in the iterable.
34
+ * @param bucketSize - The `bucketSize` parameter is the maximum number of elements that can be
35
+ * stored in each bucket. It determines the size of each bucket in the data structure.
36
+ */
97
37
  constructor(elements: IterableWithSizeOrLength<E> = [], bucketSize = (1 << 12)) {
98
38
 
99
- let _size;
39
+ let _size: number;
100
40
  if ('length' in elements) {
101
- _size = elements.length;
41
+ if (elements.length instanceof Function) _size = elements.length(); else _size = elements.length;
102
42
  } else {
103
- _size = elements.size;
43
+ if (elements.size instanceof Function) _size = elements.size(); else _size = elements.size;
104
44
  }
105
45
 
106
46
  this._bucketSize = bucketSize;
@@ -129,6 +69,11 @@ export class Deque<E> {
129
69
  return this._size;
130
70
  }
131
71
 
72
+ /**
73
+ * The function returns the first element in a collection if it exists, otherwise it returns
74
+ * undefined.
75
+ * @returns The first element of the collection, of type E, is being returned.
76
+ */
132
77
  get first(): E | undefined {
133
78
  if (this.size === 0) return;
134
79
  return this._buckets[this._bucketFirst][this._firstInBucket];
@@ -139,15 +84,6 @@ export class Deque<E> {
139
84
  return this._buckets[this._bucketLast][this._lastInBucket];
140
85
  }
141
86
 
142
- /**
143
- * Time Complexity: Amortized O(1) - Generally constant time, but resizing when the deque is full leads to O(n).
144
- * Space Complexity: O(n) - In worst case, resizing doubles the array size.
145
- */
146
-
147
- empty() {
148
- return this._size === 0;
149
- }
150
-
151
87
  /**
152
88
  * Time Complexity: O(1) - Removes the last element.
153
89
  * Space Complexity: O(1) - Operates in-place.
@@ -214,6 +150,10 @@ export class Deque<E> {
214
150
  return this.shift();
215
151
  }
216
152
 
153
+ /**
154
+ * The clear() function resets the state of the object by initializing all variables to their default
155
+ * values.
156
+ */
217
157
  clear() {
218
158
  this._buckets = [new Array(this._bucketSize)];
219
159
  this._bucketCount = 1;
@@ -221,22 +161,43 @@ export class Deque<E> {
221
161
  this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
222
162
  }
223
163
 
224
- begin() {
225
- return new DequeIterator<E>(0, this);
226
- }
227
-
228
- end() {
229
- return new DequeIterator<E>(this.size, this);
164
+ /**
165
+ * The below function is a generator that yields elements from a collection one by one.
166
+ */
167
+ * begin(): Generator<E> {
168
+ let index = 0;
169
+ while (index < this.size) {
170
+ yield this.getAt(index);
171
+ index++;
172
+ }
230
173
  }
231
174
 
232
- reverseBegin() {
233
- return new DequeIterator<E>(this.size - 1, this, IterateDirection.REVERSE);
175
+ /**
176
+ * The function `reverseBegin()` is a generator that yields elements in reverse order starting from
177
+ * the last element.
178
+ */
179
+ * reverseBegin(): Generator<E> {
180
+ let index = this.size - 1;
181
+ while (index >= 0) {
182
+ yield this.getAt(index);
183
+ index--;
184
+ }
234
185
  }
235
186
 
236
- reverseEnd() {
237
- return new DequeIterator<E>(-1, this, IterateDirection.REVERSE);
238
- }
187
+ /**
188
+ * Time Complexity - Amortized O(1) (possible reallocation)
189
+ * Space Complexity - O(n) (due to potential resizing).
190
+ */
239
191
 
192
+ /**
193
+ * Time Complexity - Amortized O(1) (possible reallocation),
194
+ * Space Complexity - O(n) (due to potential resizing).
195
+ *
196
+ * The push function adds an element to a data structure and reallocates memory if necessary.
197
+ * @param {E} element - The `element` parameter represents the value that you want to add to the data
198
+ * structure.
199
+ * @returns The size of the data structure after the element has been pushed.
200
+ */
240
201
  push(element: E) {
241
202
  if (this.size) {
242
203
  if (this._lastInBucket < this._bucketSize - 1) {
@@ -258,6 +219,19 @@ export class Deque<E> {
258
219
  return this.size;
259
220
  }
260
221
 
222
+ /**
223
+ * Time Complexity: O(1)
224
+ * Space Complexity: O(1)
225
+ */
226
+
227
+ /**
228
+ * Time Complexity: O(1)
229
+ * Space Complexity: O(1)
230
+ *
231
+ * The `pop()` function removes and returns the last element from a data structure, updating the
232
+ * internal state variables accordingly.
233
+ * @returns The element that was removed from the data structure is being returned.
234
+ */
261
235
  pop() {
262
236
  if (this.size === 0) return;
263
237
  const element = this._buckets[this._bucketLast][this._lastInBucket];
@@ -276,6 +250,21 @@ export class Deque<E> {
276
250
  return element;
277
251
  }
278
252
 
253
+ /**
254
+ * Time Complexity: Amortized O(1)
255
+ * Space Complexity: O(n)
256
+ */
257
+
258
+ /**
259
+ * Time Complexity: Amortized O(1)
260
+ * Space Complexity: O(n)
261
+ *
262
+ * The `unshift` function adds an element to the beginning of an array-like data structure and
263
+ * returns the new size of the structure.
264
+ * @param {E} element - The `element` parameter represents the element that you want to add to the
265
+ * beginning of the data structure.
266
+ * @returns The size of the data structure after the element has been added.
267
+ */
279
268
  unshift(element: E) {
280
269
  if (this.size) {
281
270
  if (this._firstInBucket > 0) {
@@ -297,6 +286,21 @@ export class Deque<E> {
297
286
  return this.size;
298
287
  }
299
288
 
289
+
290
+ /**
291
+ * Time Complexity: O(1)
292
+ * Space Complexity: O(1)
293
+ */
294
+
295
+ /**
296
+ * Time Complexity: O(1)
297
+ * Space Complexity: O(1)
298
+ *
299
+ * The `shift()` function removes and returns the first element from a data structure, updating the
300
+ * internal state variables accordingly.
301
+ * @returns The element that is being removed from the beginning of the data structure is being
302
+ * returned.
303
+ */
300
304
  shift() {
301
305
  if (this.size === 0) return;
302
306
  const element = this._buckets[this._bucketFirst][this._firstInBucket];
@@ -315,8 +319,24 @@ export class Deque<E> {
315
319
  return element;
316
320
  }
317
321
 
322
+
323
+ /**
324
+ * Time Complexity: O(1)
325
+ * Space Complexity: O(1)
326
+ */
327
+
328
+ /**
329
+ * Time Complexity: O(1)
330
+ * Space Complexity: O(1)
331
+ *
332
+ * The `getAt` function retrieves an element at a specified position in an array-like data structure.
333
+ * @param {number} pos - The `pos` parameter represents the position of the element that you want to
334
+ * retrieve from the data structure. It is of type `number` and should be a valid index within the
335
+ * range of the data structure.
336
+ * @returns The element at the specified position in the data structure is being returned.
337
+ */
318
338
  getAt(pos: number): E {
319
- rangeCheck!(pos, 0, this.size - 1);
339
+ rangeCheck(pos, 0, this.size - 1);
320
340
  const {
321
341
  bucketIndex,
322
342
  indexInBucket
@@ -324,8 +344,24 @@ export class Deque<E> {
324
344
  return this._buckets[bucketIndex][indexInBucket]!;
325
345
  }
326
346
 
347
+
348
+ /**
349
+ * Time Complexity: O(1)
350
+ * Space Complexity: O(1)
351
+ */
352
+
353
+ /**
354
+ * Time Complexity: O(1)
355
+ * Space Complexity: O(1)
356
+ *
357
+ * The `setAt` function sets an element at a specific position in an array-like data structure.
358
+ * @param {number} pos - The `pos` parameter represents the position at which the element needs to be
359
+ * set. It is of type `number`.
360
+ * @param {E} element - The `element` parameter is the value that you want to set at the specified
361
+ * position in the data structure.
362
+ */
327
363
  setAt(pos: number, element: E) {
328
- rangeCheck!(pos, 0, this.size - 1);
364
+ rangeCheck(pos, 0, this.size - 1);
329
365
  const {
330
366
  bucketIndex,
331
367
  indexInBucket
@@ -333,9 +369,29 @@ export class Deque<E> {
333
369
  this._buckets[bucketIndex][indexInBucket] = element;
334
370
  }
335
371
 
372
+ /**
373
+ * Time Complexity: O(n)
374
+ * Space Complexity: O(n)
375
+ */
376
+
377
+ /**
378
+ * Time Complexity: O(n)
379
+ * Space Complexity: O(n)
380
+ *
381
+ * The `insertAt` function inserts one or more elements at a specified position in an array-like data
382
+ * structure.
383
+ * @param {number} pos - The `pos` parameter represents the position at which the element(s) should
384
+ * be inserted. It is of type `number`.
385
+ * @param {E} element - The `element` parameter represents the element that you want to insert into
386
+ * the array at the specified position.
387
+ * @param [num=1] - The `num` parameter represents the number of times the `element` should be
388
+ * inserted at the specified position (`pos`). By default, it is set to 1, meaning that the `element`
389
+ * will be inserted once. However, you can provide a different value for `num` if you want
390
+ * @returns The size of the array after the insertion is being returned.
391
+ */
336
392
  insertAt(pos: number, element: E, num = 1) {
337
393
  const length = this.size;
338
- rangeCheck!(pos, 0, length);
394
+ rangeCheck(pos, 0, length);
339
395
  if (pos === 0) {
340
396
  while (num--) this.unshift(element);
341
397
  } else if (pos === this.size) {
@@ -352,6 +408,21 @@ export class Deque<E> {
352
408
  return this.size;
353
409
  }
354
410
 
411
+ /**
412
+ * Time Complexity: O(1)
413
+ * Space Complexity: O(1)
414
+ */
415
+
416
+ /**
417
+ * Time Complexity: O(1)
418
+ * Space Complexity: O(1)
419
+ *
420
+ * The `cut` function updates the state of the object based on the given position and returns the
421
+ * updated size.
422
+ * @param {number} pos - The `pos` parameter represents the position at which the string should be
423
+ * cut. It is a number that indicates the index of the character where the cut should be made.
424
+ * @returns The method is returning the updated size of the data structure.
425
+ */
355
426
  cut(pos: number) {
356
427
  if (pos < 0) {
357
428
  this.clear();
@@ -367,8 +438,24 @@ export class Deque<E> {
367
438
  return this.size;
368
439
  }
369
440
 
441
+ /**
442
+ * Time Complexity: O(n)
443
+ * Space Complexity: O(1)
444
+ */
445
+
446
+ /**
447
+ * Time Complexity: O(n)
448
+ * Space Complexity: O(1)
449
+ *
450
+ * The `deleteAt` function removes an element at a specified position in an array-like data
451
+ * structure.
452
+ * @param {number} pos - The `pos` parameter in the `deleteAt` function represents the position at
453
+ * which an element needs to be deleted from the data structure. It is of type `number` and indicates
454
+ * the index of the element to be deleted.
455
+ * @returns The size of the data structure after the deletion operation is performed.
456
+ */
370
457
  deleteAt(pos: number) {
371
- rangeCheck!(pos, 0, this.size - 1);
458
+ rangeCheck(pos, 0, this.size - 1);
372
459
  if (pos === 0) this.shift();
373
460
  else if (pos === this.size - 1) this.pop();
374
461
  else {
@@ -391,6 +478,21 @@ export class Deque<E> {
391
478
  return this.size;
392
479
  }
393
480
 
481
+ /**
482
+ * Time Complexity: O(n)
483
+ * Space Complexity: O(1)
484
+ */
485
+
486
+ /**
487
+ * Time Complexity: O(n)
488
+ * Space Complexity: O(1)
489
+ *
490
+ * The `delete` function removes all occurrences of a specified element from an array-like data
491
+ * structure.
492
+ * @param {E} element - The `element` parameter represents the element that you want to delete from
493
+ * the data structure.
494
+ * @returns The size of the data structure after the element has been deleted.
495
+ */
394
496
  delete(element: E) {
395
497
  const size = this.size;
396
498
  if (size === 0) return 0;
@@ -408,22 +510,20 @@ export class Deque<E> {
408
510
  return this.size;
409
511
  }
410
512
 
411
- deleteByIterator(iter: DequeIterator<E>) {
412
- const index = iter.index;
413
- this.deleteAt(index);
414
- iter = iter.next();
415
- return iter;
416
- }
417
-
418
- findIterator(element: E) {
419
- for (let i = 0; i < this.size; ++i) {
420
- if (this.getAt(i) === element) {
421
- return new DequeIterator<E>(i, this);
422
- }
423
- }
424
- return this.end();
425
- }
513
+ /**
514
+ * Time Complexity: O(n)
515
+ * Space Complexity: O(1)
516
+ */
426
517
 
518
+ /**
519
+ * Time Complexity: O(n)
520
+ * Space Complexity: O(1)
521
+ *
522
+ * The reverse() function reverses the order of the buckets and the elements within each bucket in a
523
+ * data structure.
524
+ * @returns The reverse() method is returning the object itself (this) after performing the reverse
525
+ * operation on the buckets and updating the relevant properties.
526
+ */
427
527
  reverse() {
428
528
  this._buckets.reverse().forEach(function (bucket) {
429
529
  bucket.reverse();
@@ -436,6 +536,19 @@ export class Deque<E> {
436
536
  return this;
437
537
  }
438
538
 
539
+ /**
540
+ * Time Complexity: O(n)
541
+ * Space Complexity: O(1)
542
+ */
543
+
544
+ /**
545
+ * Time Complexity: O(n)
546
+ * Space Complexity: O(1)
547
+ *
548
+ * The `unique()` function removes duplicate elements from an array-like data structure and returns
549
+ * the number of unique elements.
550
+ * @returns The size of the modified array is being returned.
551
+ */
439
552
  unique() {
440
553
  if (this.size <= 1) {
441
554
  return this.size;
@@ -453,6 +566,21 @@ export class Deque<E> {
453
566
  return this.size;
454
567
  }
455
568
 
569
+ /**
570
+ * Time Complexity: O(n log n)
571
+ * Space Complexity: O(n)
572
+ */
573
+
574
+ /**
575
+ * Time Complexity: O(n log n)
576
+ * Space Complexity: O(n)
577
+ *
578
+ * The `sort` function sorts the elements in a data structure using a provided comparator function.
579
+ * @param [comparator] - The `comparator` parameter is a function that takes in two elements `x` and
580
+ * `y` of type `E` and returns a number. The comparator function is used to determine the order of
581
+ * the elements in the sorted array.
582
+ * @returns The method is returning the sorted instance of the object on which the method is called.
583
+ */
456
584
  sort(comparator?: (x: E, y: E) => number) {
457
585
  const arr: E[] = [];
458
586
  for (let i = 0; i < this.size; ++i) {
@@ -465,6 +593,20 @@ export class Deque<E> {
465
593
  return this;
466
594
  }
467
595
 
596
+ /**
597
+ * Time Complexity: O(n)
598
+ * Space Complexity: O(n)
599
+ */
600
+
601
+ /**
602
+ * Time Complexity: O(n)
603
+ * Space Complexity: O(n)
604
+ *
605
+ * The `shrinkToFit` function reorganizes the elements in an array-like data structure to minimize
606
+ * memory usage.
607
+ * @returns Nothing is being returned. The function is using the `return` statement to exit early if
608
+ * `this.size` is 0, but it does not return any value.
609
+ */
468
610
  shrinkToFit() {
469
611
  if (this.size === 0) return;
470
612
  const newBuckets = [];
@@ -486,12 +628,42 @@ export class Deque<E> {
486
628
  this._buckets = newBuckets;
487
629
  }
488
630
 
631
+ /**
632
+ * Time Complexity: O(n)
633
+ * Space Complexity: O(1)
634
+ */
635
+
636
+ /**
637
+ * Time Complexity: O(n)
638
+ * Space Complexity: O(1)
639
+ *
640
+ * The `forEach` function iterates over each element in a deque and applies a callback function to
641
+ * each element.
642
+ * @param callback - The callback parameter is a function that will be called for each element in the
643
+ * deque. It takes three parameters:
644
+ */
489
645
  forEach(callback: (element: E, index: number, deque: Deque<E>) => void) {
490
646
  for (let i = 0; i < this.size; ++i) {
491
647
  callback(this.getAt(i), i, this);
492
648
  }
493
649
  }
494
650
 
651
+ /**
652
+ * Time Complexity: O(n)
653
+ * Space Complexity: O(1)
654
+ */
655
+
656
+ /**
657
+ * Time Complexity: O(n)
658
+ * Space Complexity: O(1)
659
+ *
660
+ * The `find` function iterates over the elements in a deque and returns the first element for which
661
+ * the callback function returns true, or undefined if no such element is found.
662
+ * @param callback - A function that takes three parameters: element, index, and deque. It should
663
+ * return a boolean value indicating whether the element satisfies a certain condition.
664
+ * @returns The method `find` returns the first element in the deque that satisfies the condition
665
+ * specified by the callback function. If no element satisfies the condition, it returns `undefined`.
666
+ */
495
667
  find(callback: (element: E, index: number, deque: Deque<E>) => boolean): E | undefined {
496
668
  for (let i = 0; i < this.size; ++i) {
497
669
  const element = this.getAt(i);
@@ -502,6 +674,18 @@ export class Deque<E> {
502
674
  return undefined;
503
675
  }
504
676
 
677
+ /**
678
+ * Time Complexity: O(n)
679
+ * Space Complexity: O(n)
680
+ */
681
+
682
+ /**
683
+ * Time Complexity: O(n)
684
+ * Space Complexity: O(n)
685
+ *
686
+ * The `toArray` function converts the elements of a data structure into an array.
687
+ * @returns The `toArray()` method is returning an array of elements of type `E`.
688
+ */
505
689
  toArray(): E[] {
506
690
  const arr: E[] = [];
507
691
  for (let i = 0; i < this.size; ++i) {
@@ -510,6 +694,20 @@ export class Deque<E> {
510
694
  return arr;
511
695
  }
512
696
 
697
+ /**
698
+ * Time Complexity: O(n)
699
+ * Space Complexity: O(n)
700
+ */
701
+
702
+ /**
703
+ * Time Complexity: O(n)
704
+ * Space Complexity: O(n)
705
+ *
706
+ * The `map` function takes a callback function and applies it to each element in the deque,
707
+ * returning a new deque with the results.
708
+ * @param callback - The `callback` parameter is a function that takes three arguments:
709
+ * @returns The `map` method is returning a new `Deque` object with the transformed elements.
710
+ */
513
711
  map<T>(callback: (element: E, index: number, deque: Deque<E>) => T): Deque<T> {
514
712
  const newDeque = new Deque<T>([], this._bucketSize);
515
713
  for (let i = 0; i < this.size; ++i) {
@@ -518,6 +716,22 @@ export class Deque<E> {
518
716
  return newDeque;
519
717
  }
520
718
 
719
+ /**
720
+ * Time Complexity: O(n)
721
+ * Space Complexity: O(n)
722
+ */
723
+
724
+ /**
725
+ * Time Complexity: O(n)
726
+ * Space Complexity: O(n)
727
+ *
728
+ * The `filter` function creates a new deque containing only the elements that satisfy the given
729
+ * predicate function.
730
+ * @param predicate - The `predicate` parameter is a function that takes three arguments: `element`,
731
+ * `index`, and `deque`.
732
+ * @returns The `filter` method is returning a new `Deque` object that contains only the elements
733
+ * that satisfy the given `predicate` function.
734
+ */
521
735
  filter(predicate: (element: E, index: number, deque: Deque<E>) => boolean): Deque<E> {
522
736
  const newDeque = new Deque<E>([], this._bucketSize);
523
737
  for (let i = 0; i < this.size; ++i) {
@@ -529,6 +743,24 @@ export class Deque<E> {
529
743
  return newDeque;
530
744
  }
531
745
 
746
+ /**
747
+ * Time Complexity: O(n)
748
+ * Space Complexity: O(1)
749
+ */
750
+
751
+ /**
752
+ * Time Complexity: O(n)
753
+ * Space Complexity: O(1)
754
+ *
755
+ * The `reduce` function iterates over the elements of a deque and applies a callback function to
756
+ * each element, accumulating a single value.
757
+ * @param callback - The `callback` parameter is a function that takes four arguments:
758
+ * @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It
759
+ * is the value that will be passed as the first argument to the `callback` function when reducing
760
+ * the elements of the deque.
761
+ * @returns the final value of the accumulator after iterating over all elements in the deque and
762
+ * applying the callback function to each element.
763
+ */
532
764
  reduce<T>(callback: (accumulator: T, element: E, index: number, deque: Deque<E>) => T, initialValue: T): T {
533
765
  let accumulator = initialValue;
534
766
  for (let i = 0; i < this.size; ++i) {
@@ -537,6 +769,22 @@ export class Deque<E> {
537
769
  return accumulator;
538
770
  }
539
771
 
772
+ /**
773
+ * Time Complexity: O(n)
774
+ * Space Complexity: O(1)
775
+ */
776
+
777
+ /**
778
+ * Time Complexity: O(n)
779
+ * Space Complexity: O(1)
780
+ *
781
+ * The function "indexOf" returns the index of the first occurrence of a given element in an array,
782
+ * or -1 if the element is not found.
783
+ * @param {E} element - The "element" parameter represents the element that you want to find the
784
+ * index of in the data structure.
785
+ * @returns The indexOf function returns the index of the first occurrence of the specified element
786
+ * in the data structure. If the element is not found, it returns -1.
787
+ */
540
788
  indexOf(element: E): number {
541
789
  for (let i = 0; i < this.size; ++i) {
542
790
  if (this.getAt(i) === element) {
@@ -546,12 +794,38 @@ export class Deque<E> {
546
794
  return -1;
547
795
  }
548
796
 
797
+ /**
798
+ * Time Complexity: O(n)
799
+ * Space Complexity: O(1)
800
+ */
801
+
802
+ /**
803
+ * Time Complexity: O(n)
804
+ * Space Complexity: O(1)
805
+ *
806
+ * The above function is an implementation of the iterator protocol in TypeScript, allowing the
807
+ * object to be iterated over using a for...of loop.
808
+ */
549
809
  * [Symbol.iterator]() {
550
810
  for (let i = 0; i < this.size; ++i) {
551
811
  yield this.getAt(i);
552
812
  }
553
813
  }
554
814
 
815
+ /**
816
+ * Time Complexity: O(n)
817
+ * Space Complexity: O(n)
818
+ */
819
+
820
+ /**
821
+ * Time Complexity: O(n)
822
+ * Space Complexity: O(n)
823
+ *
824
+ * The `_reallocate` function reallocates the buckets in an array, adding new buckets if needed.
825
+ * @param {number} [needBucketNum] - The `needBucketNum` parameter is an optional number that
826
+ * specifies the number of new buckets needed. If not provided, it will default to half of the
827
+ * current bucket count (`this._bucketCount >> 1`) or 1 if the current bucket count is less than 2.
828
+ */
555
829
  protected _reallocate(needBucketNum?: number) {
556
830
  const newBuckets = [];
557
831
  const addBucketNum = needBucketNum || this._bucketCount >> 1 || 1;
@@ -574,6 +848,20 @@ export class Deque<E> {
574
848
  this._bucketCount = newBuckets.length;
575
849
  }
576
850
 
851
+ /**
852
+ * Time Complexity: O(1)
853
+ * Space Complexity: O(1)
854
+ */
855
+
856
+ /**
857
+ * Time Complexity: O(1)
858
+ * Space Complexity: O(1)
859
+ *
860
+ * The function calculates the bucket index and index within the bucket based on the given position.
861
+ * @param {number} pos - The `pos` parameter represents the position within the data structure. It is
862
+ * a number that indicates the index or position of an element within the structure.
863
+ * @returns an object with two properties: "bucketIndex" and "indexInBucket".
864
+ */
577
865
  protected _getBucketAndPosition(pos: number) {
578
866
  let bucketIndex: number;
579
867
  let indexInBucket: number;