heap-typed 1.49.0 → 1.49.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.
Files changed (88) hide show
  1. package/dist/data-structures/base/iterable-base.d.ts +11 -0
  2. package/dist/data-structures/base/iterable-base.js +21 -0
  3. package/dist/data-structures/binary-tree/avl-tree.d.ts +10 -12
  4. package/dist/data-structures/binary-tree/avl-tree.js +3 -4
  5. package/dist/data-structures/binary-tree/binary-tree.d.ts +58 -58
  6. package/dist/data-structures/binary-tree/binary-tree.js +6 -6
  7. package/dist/data-structures/binary-tree/bst.d.ts +15 -15
  8. package/dist/data-structures/binary-tree/bst.js +3 -3
  9. package/dist/data-structures/binary-tree/rb-tree.d.ts +11 -11
  10. package/dist/data-structures/binary-tree/rb-tree.js +5 -6
  11. package/dist/data-structures/binary-tree/tree-multimap.d.ts +14 -14
  12. package/dist/data-structures/binary-tree/tree-multimap.js +3 -3
  13. package/dist/data-structures/graph/abstract-graph.d.ts +9 -3
  14. package/dist/data-structures/graph/abstract-graph.js +27 -31
  15. package/dist/data-structures/graph/directed-graph.d.ts +8 -1
  16. package/dist/data-structures/graph/directed-graph.js +1 -8
  17. package/dist/data-structures/graph/map-graph.d.ts +1 -1
  18. package/dist/data-structures/graph/undirected-graph.d.ts +8 -1
  19. package/dist/data-structures/graph/undirected-graph.js +1 -8
  20. package/dist/data-structures/hash/hash-map.d.ts +22 -10
  21. package/dist/data-structures/hash/hash-map.js +28 -16
  22. package/dist/data-structures/hash/hash-table.d.ts +2 -2
  23. package/dist/data-structures/heap/heap.d.ts +20 -38
  24. package/dist/data-structures/heap/heap.js +22 -42
  25. package/dist/data-structures/heap/max-heap.d.ts +11 -1
  26. package/dist/data-structures/heap/max-heap.js +10 -7
  27. package/dist/data-structures/heap/min-heap.d.ts +11 -1
  28. package/dist/data-structures/heap/min-heap.js +10 -7
  29. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +95 -95
  30. package/dist/data-structures/linked-list/doubly-linked-list.js +132 -136
  31. package/dist/data-structures/linked-list/singly-linked-list.d.ts +18 -23
  32. package/dist/data-structures/linked-list/singly-linked-list.js +42 -49
  33. package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -2
  34. package/dist/data-structures/linked-list/skip-linked-list.js +2 -2
  35. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +1 -1
  36. package/dist/data-structures/priority-queue/max-priority-queue.js +0 -7
  37. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +1 -1
  38. package/dist/data-structures/priority-queue/min-priority-queue.js +0 -7
  39. package/dist/data-structures/priority-queue/priority-queue.d.ts +9 -1
  40. package/dist/data-structures/priority-queue/priority-queue.js +8 -7
  41. package/dist/data-structures/queue/deque.d.ts +76 -80
  42. package/dist/data-structures/queue/deque.js +106 -122
  43. package/dist/data-structures/queue/queue.d.ts +30 -16
  44. package/dist/data-structures/queue/queue.js +31 -24
  45. package/dist/data-structures/stack/stack.d.ts +17 -8
  46. package/dist/data-structures/stack/stack.js +9 -9
  47. package/dist/data-structures/trie/trie.d.ts +14 -5
  48. package/dist/data-structures/trie/trie.js +13 -13
  49. package/dist/interfaces/binary-tree.d.ts +4 -4
  50. package/dist/types/common.d.ts +32 -8
  51. package/dist/types/common.js +22 -1
  52. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -24
  53. package/dist/types/data-structures/binary-tree/binary-tree.js +0 -22
  54. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
  55. package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +1 -1
  56. package/package.json +2 -2
  57. package/src/data-structures/base/iterable-base.ts +24 -0
  58. package/src/data-structures/binary-tree/avl-tree.ts +14 -14
  59. package/src/data-structures/binary-tree/binary-tree.ts +74 -77
  60. package/src/data-structures/binary-tree/bst.ts +18 -17
  61. package/src/data-structures/binary-tree/rb-tree.ts +17 -18
  62. package/src/data-structures/binary-tree/tree-multimap.ts +22 -20
  63. package/src/data-structures/graph/abstract-graph.ts +35 -25
  64. package/src/data-structures/graph/directed-graph.ts +2 -2
  65. package/src/data-structures/graph/map-graph.ts +1 -1
  66. package/src/data-structures/graph/undirected-graph.ts +2 -2
  67. package/src/data-structures/hash/hash-map.ts +40 -24
  68. package/src/data-structures/hash/hash-table.ts +3 -3
  69. package/src/data-structures/heap/heap.ts +33 -60
  70. package/src/data-structures/heap/max-heap.ts +11 -2
  71. package/src/data-structures/heap/min-heap.ts +11 -2
  72. package/src/data-structures/linked-list/doubly-linked-list.ts +147 -145
  73. package/src/data-structures/linked-list/singly-linked-list.ts +52 -52
  74. package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
  75. package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
  76. package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
  77. package/src/data-structures/priority-queue/priority-queue.ts +9 -2
  78. package/src/data-structures/queue/deque.ts +129 -144
  79. package/src/data-structures/queue/queue.ts +37 -26
  80. package/src/data-structures/stack/stack.ts +20 -14
  81. package/src/data-structures/trie/trie.ts +18 -13
  82. package/src/interfaces/binary-tree.ts +5 -5
  83. package/src/types/common.ts +37 -12
  84. package/src/types/data-structures/binary-tree/avl-tree.ts +0 -1
  85. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -26
  86. package/src/types/data-structures/binary-tree/bst.ts +0 -1
  87. package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
  88. package/src/types/data-structures/binary-tree/tree-multimap.ts +1 -1
@@ -5,19 +5,17 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
-
9
-
10
- import { ElementCallback, IterableWithSizeOrLength } from "../../types";
11
- import { calcMinUnitsRequired, rangeCheck } from "../../utils";
8
+ import type { ElementCallback, IterableWithSizeOrLength } from "../../types";
12
9
  import { IterableElementBase } from "../base";
10
+ import { calcMinUnitsRequired, rangeCheck } from "../../utils";
13
11
 
14
12
  /**
15
- * Deque can provide random access with O(1) time complexity
16
- * Deque is usually more compact and efficient in memory usage because it does not require additional space to store pointers.
17
- * Deque may experience performance jitter, but DoublyLinkedList will not
18
- * Deque is implemented using a dynamic array. Inserting or deleting beyond both ends of the array may require moving elements or reallocating space.
13
+ * 1. Operations at Both Ends: Supports adding and removing elements at both the front and back of the queue. This allows it to be used as a stack (last in, first out) and a queue (first in, first out).
14
+ * 2. Efficient Random Access: Being based on an array, it offers fast random access capability, allowing constant time access to any element.
15
+ * 3. Continuous Memory Allocation: Since it is based on an array, all elements are stored contiguously in memory, which can bring cache friendliness and efficient memory access.
16
+ * 4. Efficiency: Adding and removing elements at both ends of a deque is usually very fast. However, when the dynamic array needs to expand, it may involve copying the entire array to a larger one, and this operation has a time complexity of O(n).
17
+ * 5. Performance jitter: Deque may experience performance jitter, but DoublyLinkedList will not
19
18
  */
20
-
21
19
  export class Deque<E> extends IterableElementBase<E> {
22
20
  protected _bucketFirst = 0;
23
21
  protected _firstInBucket = 0;
@@ -85,106 +83,6 @@ export class Deque<E> extends IterableElementBase<E> {
85
83
  return this._buckets[this._bucketLast][this._lastInBucket];
86
84
  }
87
85
 
88
- /**
89
- * Time Complexity: O(1) - Removes the last element.
90
- * Space Complexity: O(1) - Operates in-place.
91
- */
92
-
93
- isEmpty() {
94
- return this.size === 0;
95
- }
96
-
97
- /**
98
- * Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
99
- * Space Complexity: O(n) - Due to potential resizing.
100
- */
101
-
102
- /**
103
- * Time Complexity: O(1)
104
- * Space Complexity: O(n) - In worst case, resizing doubles the array size.
105
- *
106
- * The addLast function adds an element to the end of an array.
107
- * @param {E} element - The element parameter represents the element that you want to add to the end of the
108
- * data structure.
109
- */
110
- addLast(element: E): void {
111
- this.push(element);
112
- }
113
-
114
- /**
115
- * Time Complexity: O(1) - Removes the first element.
116
- * Space Complexity: O(1) - In-place operation.
117
- */
118
-
119
- /**
120
- * Time Complexity: O(1) - Removes the last element.
121
- * Space Complexity: O(1) - Operates in-place.
122
- *
123
- * The function "pollLast" removes and returns the last element of an array.
124
- * @returns The last element of the array is being returned.
125
- */
126
- pollLast(): E | undefined {
127
- return this.pop();
128
- }
129
-
130
- /**
131
- * Time Complexity: O(1).
132
- * Space Complexity: O(n) - Due to potential resizing.
133
- *
134
- * The "addFirst" function adds an element to the beginning of an array.
135
- * @param {E} element - The parameter "element" represents the element that you want to add to the
136
- * beginning of the data structure.
137
- */
138
- addFirst(element: E): void {
139
- this.unshift(element);
140
- }
141
-
142
- /**
143
- * Time Complexity: O(1) - Removes the first element.
144
- * Space Complexity: O(1) - In-place operation.
145
- *
146
- * The function "pollFirst" removes and returns the first element of an array.
147
- * @returns The method `pollFirst()` is returning the first element of the array after removing it
148
- * from the beginning. If the array is empty, it will return `undefined`.
149
- */
150
- pollFirst(): E | undefined {
151
- return this.shift();
152
- }
153
-
154
- /**
155
- * The clear() function resets the state of the object by initializing all variables to their default
156
- * values.
157
- */
158
- clear() {
159
- this._buckets = [new Array(this._bucketSize)];
160
- this._bucketCount = 1;
161
- this._bucketFirst = this._bucketLast = this._size = 0;
162
- this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
163
- }
164
-
165
- /**
166
- * The below function is a generator that yields elements from a collection one by one.
167
- */
168
- * begin(): Generator<E> {
169
- let index = 0;
170
- while (index < this.size) {
171
- yield this.getAt(index);
172
- index++;
173
- }
174
- }
175
-
176
- /**
177
- * The function `reverseBegin()` is a generator that yields elements in reverse order starting from
178
- * the last element.
179
- */
180
- * reverseBegin(): Generator<E> {
181
- let index = this.size - 1;
182
- while (index >= 0) {
183
- yield this.getAt(index);
184
- index--;
185
- }
186
- }
187
-
188
86
  /**
189
87
  * Time Complexity - Amortized O(1) (possible reallocation)
190
88
  * Space Complexity - O(n) (due to potential resizing).
@@ -199,7 +97,7 @@ export class Deque<E> extends IterableElementBase<E> {
199
97
  * structure.
200
98
  * @returns The size of the data structure after the element has been pushed.
201
99
  */
202
- push(element: E) {
100
+ push(element: E): boolean {
203
101
  if (this.size) {
204
102
  if (this._lastInBucket < this._bucketSize - 1) {
205
103
  this._lastInBucket += 1;
@@ -217,7 +115,7 @@ export class Deque<E> extends IterableElementBase<E> {
217
115
  }
218
116
  this._size += 1;
219
117
  this._buckets[this._bucketLast][this._lastInBucket] = element;
220
- return this.size;
118
+ return true;
221
119
  }
222
120
 
223
121
  /**
@@ -233,7 +131,7 @@ export class Deque<E> extends IterableElementBase<E> {
233
131
  * internal state variables accordingly.
234
132
  * @returns The element that was removed from the data structure is being returned.
235
133
  */
236
- pop() {
134
+ pop(): E | undefined {
237
135
  if (this.size === 0) return;
238
136
  const element = this._buckets[this._bucketLast][this._lastInBucket];
239
137
  if (this.size !== 1) {
@@ -266,7 +164,7 @@ export class Deque<E> extends IterableElementBase<E> {
266
164
  * beginning of the data structure.
267
165
  * @returns The size of the data structure after the element has been added.
268
166
  */
269
- unshift(element: E) {
167
+ unshift(element: E): boolean {
270
168
  if (this.size) {
271
169
  if (this._firstInBucket > 0) {
272
170
  this._firstInBucket -= 1;
@@ -284,10 +182,9 @@ export class Deque<E> extends IterableElementBase<E> {
284
182
  }
285
183
  this._size += 1;
286
184
  this._buckets[this._bucketFirst][this._firstInBucket] = element;
287
- return this.size;
185
+ return true;
288
186
  }
289
187
 
290
-
291
188
  /**
292
189
  * Time Complexity: O(1)
293
190
  * Space Complexity: O(1)
@@ -302,7 +199,7 @@ export class Deque<E> extends IterableElementBase<E> {
302
199
  * @returns The element that is being removed from the beginning of the data structure is being
303
200
  * returned.
304
201
  */
305
- shift() {
202
+ shift(): E | undefined {
306
203
  if (this.size === 0) return;
307
204
  const element = this._buckets[this._bucketFirst][this._firstInBucket];
308
205
  if (this.size !== 1) {
@@ -320,6 +217,49 @@ export class Deque<E> extends IterableElementBase<E> {
320
217
  return element;
321
218
  }
322
219
 
220
+ /**
221
+ * Time Complexity: O(1) - Removes the last element.
222
+ * Space Complexity: O(1) - Operates in-place.
223
+ */
224
+
225
+ isEmpty(): boolean {
226
+ return this.size === 0;
227
+ }
228
+
229
+ /**
230
+ * The clear() function resets the state of the object by initializing all variables to their default
231
+ * values.
232
+ */
233
+ clear(): void {
234
+ this._buckets = [new Array(this._bucketSize)];
235
+ this._bucketCount = 1;
236
+ this._bucketFirst = this._bucketLast = this._size = 0;
237
+ this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
238
+ }
239
+
240
+ /**
241
+ * The below function is a generator that yields elements from a collection one by one.
242
+ */
243
+ * begin(): Generator<E> {
244
+ let index = 0;
245
+ while (index < this.size) {
246
+ yield this.getAt(index);
247
+ index++;
248
+ }
249
+ }
250
+
251
+ /**
252
+ * The function `reverseBegin()` is a generator that yields elements in reverse order starting from
253
+ * the last element.
254
+ */
255
+ * reverseBegin(): Generator<E> {
256
+ let index = this.size - 1;
257
+ while (index >= 0) {
258
+ yield this.getAt(index);
259
+ index--;
260
+ }
261
+ }
262
+
323
263
 
324
264
  /**
325
265
  * Time Complexity: O(1)
@@ -361,13 +301,14 @@ export class Deque<E> extends IterableElementBase<E> {
361
301
  * @param {E} element - The `element` parameter is the value that you want to set at the specified
362
302
  * position in the data structure.
363
303
  */
364
- setAt(pos: number, element: E) {
304
+ setAt(pos: number, element: E): boolean {
365
305
  rangeCheck(pos, 0, this.size - 1);
366
306
  const {
367
307
  bucketIndex,
368
308
  indexInBucket
369
309
  } = this._getBucketAndPosition(pos);
370
310
  this._buckets[bucketIndex][indexInBucket] = element;
311
+ return true;
371
312
  }
372
313
 
373
314
  /**
@@ -379,7 +320,7 @@ export class Deque<E> extends IterableElementBase<E> {
379
320
  * Time Complexity: O(n)
380
321
  * Space Complexity: O(n)
381
322
  *
382
- * The `insertAt` function inserts one or more elements at a specified position in an array-like data
323
+ * The `addAt` function inserts one or more elements at a specified position in an array-like data
383
324
  * structure.
384
325
  * @param {number} pos - The `pos` parameter represents the position at which the element(s) should
385
326
  * be inserted. It is of type `number`.
@@ -390,7 +331,7 @@ export class Deque<E> extends IterableElementBase<E> {
390
331
  * will be inserted once. However, you can provide a different value for `num` if you want
391
332
  * @returns The size of the array after the insertion is being returned.
392
333
  */
393
- insertAt(pos: number, element: E, num = 1) {
334
+ addAt(pos: number, element: E, num = 1): boolean {
394
335
  const length = this.size;
395
336
  rangeCheck(pos, 0, length);
396
337
  if (pos === 0) {
@@ -406,7 +347,7 @@ export class Deque<E> extends IterableElementBase<E> {
406
347
  for (let i = 0; i < num; ++i) this.push(element);
407
348
  for (let i = 0; i < arr.length; ++i) this.push(arr[i]);
408
349
  }
409
- return this.size;
350
+ return true;
410
351
  }
411
352
 
412
353
  /**
@@ -424,7 +365,7 @@ export class Deque<E> extends IterableElementBase<E> {
424
365
  * cut. It is a number that indicates the index of the character where the cut should be made.
425
366
  * @returns The method is returning the updated size of the data structure.
426
367
  */
427
- cut(pos: number) {
368
+ cut(pos: number): number {
428
369
  if (pos < 0) {
429
370
  this.clear();
430
371
  return 0;
@@ -455,7 +396,7 @@ export class Deque<E> extends IterableElementBase<E> {
455
396
  * the index of the element to be deleted.
456
397
  * @returns The size of the data structure after the deletion operation is performed.
457
398
  */
458
- deleteAt(pos: number) {
399
+ deleteAt(pos: number): boolean {
459
400
  rangeCheck(pos, 0, this.size - 1);
460
401
  if (pos === 0) this.shift();
461
402
  else if (pos === this.size - 1) this.pop();
@@ -476,7 +417,7 @@ export class Deque<E> extends IterableElementBase<E> {
476
417
  }
477
418
  this.pop();
478
419
  }
479
- return this.size;
420
+ return true;
480
421
  }
481
422
 
482
423
  /**
@@ -494,9 +435,9 @@ export class Deque<E> extends IterableElementBase<E> {
494
435
  * the data structure.
495
436
  * @returns The size of the data structure after the element has been deleted.
496
437
  */
497
- delete(element: E) {
438
+ delete(element: E): boolean {
498
439
  const size = this.size;
499
- if (size === 0) return 0;
440
+ if (size === 0) return false;
500
441
  let i = 0;
501
442
  let index = 0;
502
443
  while (i < size) {
@@ -508,7 +449,7 @@ export class Deque<E> extends IterableElementBase<E> {
508
449
  i += 1;
509
450
  }
510
451
  this.cut(index - 1);
511
- return this.size;
452
+ return true;
512
453
  }
513
454
 
514
455
  /**
@@ -525,7 +466,7 @@ export class Deque<E> extends IterableElementBase<E> {
525
466
  * @returns The reverse() method is returning the object itself (this) after performing the reverse
526
467
  * operation on the buckets and updating the relevant properties.
527
468
  */
528
- reverse() {
469
+ reverse(): this {
529
470
  this._buckets.reverse().forEach(function (bucket) {
530
471
  bucket.reverse();
531
472
  });
@@ -550,9 +491,9 @@ export class Deque<E> extends IterableElementBase<E> {
550
491
  * the number of unique elements.
551
492
  * @returns The size of the modified array is being returned.
552
493
  */
553
- unique() {
494
+ unique(): this {
554
495
  if (this.size <= 1) {
555
- return this.size;
496
+ return this;
556
497
  }
557
498
  let index = 1;
558
499
  let prev = this.getAt(0);
@@ -564,7 +505,7 @@ export class Deque<E> extends IterableElementBase<E> {
564
505
  }
565
506
  }
566
507
  this.cut(index - 1);
567
- return this.size;
508
+ return this;
568
509
  }
569
510
 
570
511
  /**
@@ -580,9 +521,9 @@ export class Deque<E> extends IterableElementBase<E> {
580
521
  * @param [comparator] - The `comparator` parameter is a function that takes in two elements `x` and
581
522
  * `y` of type `E` and returns a number. The comparator function is used to determine the order of
582
523
  * the elements in the sorted array.
583
- * @returns The method is returning the sorted instance of the object on which the method is called.
524
+ * @returns Deque<E>
584
525
  */
585
- sort(comparator?: (x: E, y: E) => number) {
526
+ sort(comparator?: (x: E, y: E) => number): this {
586
527
  const arr: E[] = [];
587
528
  for (let i = 0; i < this.size; ++i) {
588
529
  arr.push(this.getAt(i));
@@ -608,7 +549,7 @@ export class Deque<E> extends IterableElementBase<E> {
608
549
  * @returns Nothing is being returned. The function is using the `return` statement to exit early if
609
550
  * `this.size` is 0, but it does not return any value.
610
551
  */
611
- shrinkToFit() {
552
+ shrinkToFit(): void {
612
553
  if (this.size === 0) return;
613
554
  const newBuckets = [];
614
555
  if (this._bucketFirst === this._bucketLast) return;
@@ -652,7 +593,7 @@ export class Deque<E> extends IterableElementBase<E> {
652
593
  return element;
653
594
  }
654
595
  }
655
- return undefined;
596
+ return;
656
597
  }
657
598
 
658
599
  /**
@@ -693,11 +634,7 @@ export class Deque<E> extends IterableElementBase<E> {
693
634
  * @returns The `toArray()` method is returning an array of elements of type `E`.
694
635
  */
695
636
  toArray(): E[] {
696
- const arr: E[] = [];
697
- for (let i = 0; i < this.size; ++i) {
698
- arr.push(this.getAt(i));
699
- }
700
- return arr;
637
+ return [...this];
701
638
  }
702
639
 
703
640
  /**
@@ -760,12 +697,60 @@ export class Deque<E> extends IterableElementBase<E> {
760
697
  }
761
698
 
762
699
  /**
763
- * Time Complexity: O(n)
764
- * Space Complexity: O(n)
700
+ * Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
701
+ * Space Complexity: O(n) - Due to potential resizing.
702
+ */
703
+
704
+ /**
705
+ * Time Complexity: O(1)
706
+ * Space Complexity: O(n) - In worst case, resizing doubles the array size.
707
+ *
708
+ * The addLast function adds an element to the end of an array.
709
+ * @param {E} element - The element parameter represents the element that you want to add to the end of the
710
+ * data structure.
711
+ */
712
+ addLast(element: E): boolean {
713
+ return this.push(element);
714
+ }
715
+
716
+ /**
717
+ * Time Complexity: O(1) - Removes the first element.
718
+ * Space Complexity: O(1) - In-place operation.
719
+ */
720
+
721
+ /**
722
+ * Time Complexity: O(1) - Removes the last element.
723
+ * Space Complexity: O(1) - Operates in-place.
724
+ *
725
+ * The function "pollLast" removes and returns the last element of an array.
726
+ * @returns The last element of the array is being returned.
765
727
  */
728
+ pollLast(): E | undefined {
729
+ return this.pop();
730
+ }
766
731
 
767
- print(): void {
768
- console.log([...this])
732
+ /**
733
+ * Time Complexity: O(1).
734
+ * Space Complexity: O(n) - Due to potential resizing.
735
+ *
736
+ * The "addFirst" function adds an element to the beginning of an array.
737
+ * @param {E} element - The parameter "element" represents the element that you want to add to the
738
+ * beginning of the data structure.
739
+ */
740
+ addFirst(element: E): boolean {
741
+ return this.unshift(element);
742
+ }
743
+
744
+ /**
745
+ * Time Complexity: O(1) - Removes the first element.
746
+ * Space Complexity: O(1) - In-place operation.
747
+ *
748
+ * The function "pollFirst" removes and returns the first element of an array.
749
+ * @returns The method `pollFirst()` is returning the first element of the array after removing it
750
+ * from the beginning. If the array is empty, it will return `undefined`.
751
+ */
752
+ pollFirst(): E | undefined {
753
+ return this.shift();
769
754
  }
770
755
 
771
756
  /**
@@ -775,7 +760,7 @@ export class Deque<E> extends IterableElementBase<E> {
775
760
  * The above function is an implementation of the iterator protocol in TypeScript, allowing the
776
761
  * object to be iterated over using a for...of loop.
777
762
  */
778
- protected* _getIterator() {
763
+ protected* _getIterator(): IterableIterator<E> {
779
764
  for (let i = 0; i < this.size; ++i) {
780
765
  yield this.getAt(i);
781
766
  }
@@ -3,10 +3,19 @@
3
3
  * @copyright Tyler Zeng <zrwusa@gmail.com>
4
4
  * @class
5
5
  */
6
- import { SinglyLinkedList } from '../linked-list';
6
+ import type { ElementCallback } from '../../types';
7
7
  import { IterableElementBase } from "../base";
8
- import { ElementCallback } from "../../types";
8
+ import { SinglyLinkedList } from '../linked-list';
9
9
 
10
+ /**
11
+ * 1. First In, First Out (FIFO): The core feature of a queue is its first in, first out nature. The element added to the queue first will be the one to be removed first.
12
+ * 2. Operations: The main operations include enqueue (adding an element to the end of the queue) and dequeue (removing and returning the element at the front of the queue). Typically, there is also a peek operation (looking at the front element without removing it).
13
+ * 3. Uses: Queues are commonly used to manage a series of tasks or elements that need to be processed in order. For example, managing task queues in a multi-threaded environment, or in algorithms for data structures like trees and graphs for breadth-first search.
14
+ * 4. Task Scheduling: Managing the order of task execution in operating systems or applications.
15
+ * 5. Data Buffering: Acting as a buffer for data packets in network communication.
16
+ * 6. Breadth-First Search (BFS): In traversal algorithms for graphs and trees, queues store nodes that are to be visited.
17
+ * 7. Real-time Queuing: Like queuing systems in banks or supermarkets.
18
+ */
10
19
  export class Queue<E = any> extends IterableElementBase<E> {
11
20
  /**
12
21
  * The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0.
@@ -65,9 +74,9 @@ export class Queue<E = any> extends IterableElementBase<E> {
65
74
  * @param {E} element - The `element` parameter represents the element that you want to add to the queue.
66
75
  * @returns The `add` method is returning a `Queue<E>` object.
67
76
  */
68
- push(element: E): Queue<E> {
77
+ push(element: E): boolean {
69
78
  this.nodes.push(element);
70
- return this;
79
+ return true;
71
80
  }
72
81
 
73
82
  /**
@@ -86,7 +95,7 @@ export class Queue<E = any> extends IterableElementBase<E> {
86
95
  shift(): E | undefined {
87
96
  if (this.size === 0) return undefined;
88
97
 
89
- const first = this.getFirst();
98
+ const first = this.first;
90
99
  this._offset += 1;
91
100
 
92
101
  if (this.offset * 2 < this.nodes.length) return first;
@@ -107,11 +116,11 @@ export class Queue<E = any> extends IterableElementBase<E> {
107
116
  * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
108
117
  * Space Complexity: O(1) - no additional space is used.
109
118
  *
110
- * The `getFirst` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`.
111
- * @returns The `getFirst()` method returns the first element of the data structure, represented by the `_nodes` array at
119
+ * The `first` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`.
120
+ * @returns The `get first()` method returns the first element of the data structure, represented by the `_nodes` array at
112
121
  * the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
113
122
  */
114
- getFirst(): E | undefined {
123
+ get first(): E | undefined {
115
124
  return this.size > 0 ? this.nodes[this.offset] : undefined;
116
125
  }
117
126
 
@@ -129,7 +138,7 @@ export class Queue<E = any> extends IterableElementBase<E> {
129
138
  * the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
130
139
  */
131
140
  peek(): E | undefined {
132
- return this.getFirst();
141
+ return this.first;
133
142
  }
134
143
 
135
144
  /**
@@ -141,11 +150,11 @@ export class Queue<E = any> extends IterableElementBase<E> {
141
150
  * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
142
151
  * Space Complexity: O(1) - no additional space is used.
143
152
  *
144
- * The `getLast` function returns the last element in an array-like data structure, or undefined if the structure is empty.
145
- * @returns The method `getLast()` returns the last element of the `_nodes` array if the array is not empty. If the
153
+ * The `last` function returns the last element in an array-like data structure, or undefined if the structure is empty.
154
+ * @returns The method `get last()` returns the last element of the `_nodes` array if the array is not empty. If the
146
155
  * array is empty, it returns `undefined`.
147
156
  */
148
- getLast(): E | undefined {
157
+ get last(): E | undefined {
149
158
  return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined;
150
159
  }
151
160
 
@@ -163,7 +172,7 @@ export class Queue<E = any> extends IterableElementBase<E> {
163
172
  * array is empty, it returns `undefined`.
164
173
  */
165
174
  peekLast(): E | undefined {
166
- return this.getLast();
175
+ return this.last;
167
176
  }
168
177
 
169
178
  /**
@@ -178,8 +187,8 @@ export class Queue<E = any> extends IterableElementBase<E> {
178
187
  * The enqueue function adds a value to the end of a queue.
179
188
  * @param {E} value - The value parameter represents the value that you want to add to the queue.
180
189
  */
181
- enqueue(value: E) {
182
- this.push(value);
190
+ enqueue(value: E): boolean {
191
+ return this.push(value);
183
192
  }
184
193
 
185
194
  /**
@@ -269,10 +278,6 @@ export class Queue<E = any> extends IterableElementBase<E> {
269
278
  return new Queue(this.nodes.slice(this.offset));
270
279
  }
271
280
 
272
- print(): void {
273
- console.log([...this]);
274
- }
275
-
276
281
  /**
277
282
  * Time Complexity: O(n)
278
283
  * Space Complexity: O(n)
@@ -339,20 +344,26 @@ export class Queue<E = any> extends IterableElementBase<E> {
339
344
  * Space Complexity: O(n)
340
345
  */
341
346
 
342
- protected* _getIterator() {
347
+ protected* _getIterator(): IterableIterator<E> {
343
348
  for (const item of this.nodes) {
344
349
  yield item;
345
350
  }
346
351
  }
347
352
  }
348
353
 
354
+ /**
355
+ * 1. First In, First Out (FIFO) Strategy: Like other queue implementations, LinkedListQueue follows the first in, first out principle, meaning the element that is added to the queue first will be the first to be removed.
356
+ * 2. Based on Linked List: LinkedListQueue uses a linked list to store elements. Each node in the linked list contains data and a pointer to the next node.
357
+ * 3. Memory Usage: Since each element requires additional space to store a pointer to the next element, linked lists may use more memory compared to arrays.
358
+ * 4. Frequent Enqueuing and Dequeuing Operations: If your application involves frequent enqueuing and dequeuing operations and is less concerned with random access, then LinkedListQueue is a good choice.
359
+ */
349
360
  export class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
350
361
  /**
351
362
  * The enqueue function adds a value to the end of an array.
352
363
  * @param {E} value - The value parameter represents the value that you want to add to the queue.
353
364
  */
354
- enqueue(value: E) {
355
- this.push(value);
365
+ enqueue(value: E): boolean {
366
+ return this.push(value);
356
367
  }
357
368
 
358
369
  /**
@@ -364,10 +375,10 @@ export class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
364
375
  }
365
376
 
366
377
  /**
367
- * The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
368
- * @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
378
+ * The `get first` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
379
+ * @returns The `get first()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
369
380
  */
370
- getFirst(): E | undefined {
381
+ get first(): E | undefined {
371
382
  return this.head?.value;
372
383
  }
373
384
 
@@ -376,6 +387,6 @@ export class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
376
387
  * @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
377
388
  */
378
389
  peek(): E | undefined {
379
- return this.getFirst();
390
+ return this.first;
380
391
  }
381
392
  }