graph-typed 1.45.3 → 1.46.1

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