graph-typed 1.46.1 → 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.
|
@@ -16,6 +16,20 @@ const utils_1 = require("../../utils");
|
|
|
16
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
17
|
*/
|
|
18
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
|
+
*/
|
|
19
33
|
constructor(index, deque, iterateDirection = 0 /* IterateDirection.DEFAULT */) {
|
|
20
34
|
this.index = index;
|
|
21
35
|
this.iterateDirection = iterateDirection;
|
|
@@ -74,6 +88,15 @@ class DequeIterator {
|
|
|
74
88
|
}
|
|
75
89
|
exports.DequeIterator = DequeIterator;
|
|
76
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
|
+
*/
|
|
77
100
|
constructor(elements = [], bucketSize = (1 << 12)) {
|
|
78
101
|
this._bucketFirst = 0;
|
|
79
102
|
this._firstInBucket = 0;
|
|
@@ -84,10 +107,16 @@ class Deque {
|
|
|
84
107
|
this._size = 0;
|
|
85
108
|
let _size;
|
|
86
109
|
if ('length' in elements) {
|
|
87
|
-
|
|
110
|
+
if (elements.length instanceof Function)
|
|
111
|
+
_size = elements.length();
|
|
112
|
+
else
|
|
113
|
+
_size = elements.length;
|
|
88
114
|
}
|
|
89
115
|
else {
|
|
90
|
-
|
|
116
|
+
if (elements.size instanceof Function)
|
|
117
|
+
_size = elements.size();
|
|
118
|
+
else
|
|
119
|
+
_size = elements.size;
|
|
91
120
|
}
|
|
92
121
|
this._bucketSize = bucketSize;
|
|
93
122
|
this._bucketCount = (0, utils_1.calcMinUnitsRequired)(_size, this._bucketSize) || 1;
|
|
@@ -107,6 +136,11 @@ class Deque {
|
|
|
107
136
|
get size() {
|
|
108
137
|
return this._size;
|
|
109
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
|
+
*/
|
|
110
144
|
get first() {
|
|
111
145
|
if (this.size === 0)
|
|
112
146
|
return;
|
|
@@ -117,13 +151,6 @@ class Deque {
|
|
|
117
151
|
return;
|
|
118
152
|
return this._buckets[this._bucketLast][this._lastInBucket];
|
|
119
153
|
}
|
|
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
154
|
/**
|
|
128
155
|
* Time Complexity: O(1) - Removes the last element.
|
|
129
156
|
* Space Complexity: O(1) - Operates in-place.
|
|
@@ -182,24 +209,60 @@ class Deque {
|
|
|
182
209
|
popFirst() {
|
|
183
210
|
return this.shift();
|
|
184
211
|
}
|
|
212
|
+
/**
|
|
213
|
+
* The clear() function resets the state of the object by initializing all variables to their default
|
|
214
|
+
* values.
|
|
215
|
+
*/
|
|
185
216
|
clear() {
|
|
186
217
|
this._buckets = [new Array(this._bucketSize)];
|
|
187
218
|
this._bucketCount = 1;
|
|
188
219
|
this._bucketFirst = this._bucketLast = this._size = 0;
|
|
189
220
|
this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
|
|
190
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
|
+
*/
|
|
191
226
|
begin() {
|
|
192
227
|
return new DequeIterator(0, this);
|
|
193
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
|
+
*/
|
|
194
234
|
end() {
|
|
195
235
|
return new DequeIterator(this.size, this);
|
|
196
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
|
+
*/
|
|
197
242
|
reverseBegin() {
|
|
198
243
|
return new DequeIterator(this.size - 1, this, 1 /* IterateDirection.REVERSE */);
|
|
199
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
|
+
*/
|
|
200
250
|
reverseEnd() {
|
|
201
251
|
return new DequeIterator(-1, this, 1 /* IterateDirection.REVERSE */);
|
|
202
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
|
|
263
|
+
* structure.
|
|
264
|
+
* @returns The size of the data structure after the element has been pushed.
|
|
265
|
+
*/
|
|
203
266
|
push(element) {
|
|
204
267
|
if (this.size) {
|
|
205
268
|
if (this._lastInBucket < this._bucketSize - 1) {
|
|
@@ -221,6 +284,18 @@ class Deque {
|
|
|
221
284
|
this._buckets[this._bucketLast][this._lastInBucket] = element;
|
|
222
285
|
return this.size;
|
|
223
286
|
}
|
|
287
|
+
/**
|
|
288
|
+
* Time Complexity: O(1)
|
|
289
|
+
* Space Complexity: O(1)
|
|
290
|
+
*/
|
|
291
|
+
/**
|
|
292
|
+
* Time Complexity: O(1)
|
|
293
|
+
* Space Complexity: O(1)
|
|
294
|
+
*
|
|
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.
|
|
298
|
+
*/
|
|
224
299
|
pop() {
|
|
225
300
|
if (this.size === 0)
|
|
226
301
|
return;
|
|
@@ -241,6 +316,20 @@ class Deque {
|
|
|
241
316
|
this._size -= 1;
|
|
242
317
|
return element;
|
|
243
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
|
+
*/
|
|
244
333
|
unshift(element) {
|
|
245
334
|
if (this.size) {
|
|
246
335
|
if (this._firstInBucket > 0) {
|
|
@@ -262,6 +351,19 @@ class Deque {
|
|
|
262
351
|
this._buckets[this._bucketFirst][this._firstInBucket] = element;
|
|
263
352
|
return this.size;
|
|
264
353
|
}
|
|
354
|
+
/**
|
|
355
|
+
* Time Complexity: O(1)
|
|
356
|
+
* Space Complexity: O(1)
|
|
357
|
+
*/
|
|
358
|
+
/**
|
|
359
|
+
* Time Complexity: O(1)
|
|
360
|
+
* Space Complexity: O(1)
|
|
361
|
+
*
|
|
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.
|
|
366
|
+
*/
|
|
265
367
|
shift() {
|
|
266
368
|
if (this.size === 0)
|
|
267
369
|
return;
|
|
@@ -282,16 +384,63 @@ class Deque {
|
|
|
282
384
|
this._size -= 1;
|
|
283
385
|
return element;
|
|
284
386
|
}
|
|
387
|
+
/**
|
|
388
|
+
* Time Complexity: O(1)
|
|
389
|
+
* Space Complexity: O(1)
|
|
390
|
+
*/
|
|
391
|
+
/**
|
|
392
|
+
* Time Complexity: O(1)
|
|
393
|
+
* Space Complexity: O(1)
|
|
394
|
+
*
|
|
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.
|
|
400
|
+
*/
|
|
285
401
|
getAt(pos) {
|
|
286
402
|
utils_1.rangeCheck(pos, 0, this.size - 1);
|
|
287
403
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
288
404
|
return this._buckets[bucketIndex][indexInBucket];
|
|
289
405
|
}
|
|
406
|
+
/**
|
|
407
|
+
* Time Complexity: O(1)
|
|
408
|
+
* Space Complexity: O(1)
|
|
409
|
+
*/
|
|
410
|
+
/**
|
|
411
|
+
* Time Complexity: O(1)
|
|
412
|
+
* Space Complexity: O(1)
|
|
413
|
+
*
|
|
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.
|
|
419
|
+
*/
|
|
290
420
|
setAt(pos, element) {
|
|
291
421
|
utils_1.rangeCheck(pos, 0, this.size - 1);
|
|
292
422
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
293
423
|
this._buckets[bucketIndex][indexInBucket] = element;
|
|
294
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
|
+
*/
|
|
295
444
|
insertAt(pos, element, num = 1) {
|
|
296
445
|
const length = this.size;
|
|
297
446
|
utils_1.rangeCheck(pos, 0, length);
|
|
@@ -316,6 +465,20 @@ class Deque {
|
|
|
316
465
|
}
|
|
317
466
|
return this.size;
|
|
318
467
|
}
|
|
468
|
+
/**
|
|
469
|
+
* Time Complexity: O(1)
|
|
470
|
+
* Space Complexity: O(1)
|
|
471
|
+
*/
|
|
472
|
+
/**
|
|
473
|
+
* Time Complexity: O(1)
|
|
474
|
+
* Space Complexity: O(1)
|
|
475
|
+
*
|
|
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.
|
|
481
|
+
*/
|
|
319
482
|
cut(pos) {
|
|
320
483
|
if (pos < 0) {
|
|
321
484
|
this.clear();
|
|
@@ -327,6 +490,21 @@ class Deque {
|
|
|
327
490
|
this._size = pos + 1;
|
|
328
491
|
return this.size;
|
|
329
492
|
}
|
|
493
|
+
/**
|
|
494
|
+
* Time Complexity: O(n)
|
|
495
|
+
* Space Complexity: O(1)
|
|
496
|
+
*/
|
|
497
|
+
/**
|
|
498
|
+
* Time Complexity: O(n)
|
|
499
|
+
* Space Complexity: O(1)
|
|
500
|
+
*
|
|
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.
|
|
507
|
+
*/
|
|
330
508
|
deleteAt(pos) {
|
|
331
509
|
utils_1.rangeCheck(pos, 0, this.size - 1);
|
|
332
510
|
if (pos === 0)
|
|
@@ -346,6 +524,20 @@ class Deque {
|
|
|
346
524
|
}
|
|
347
525
|
return this.size;
|
|
348
526
|
}
|
|
527
|
+
/**
|
|
528
|
+
* Time Complexity: O(n)
|
|
529
|
+
* Space Complexity: O(1)
|
|
530
|
+
*/
|
|
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
|
+
*/
|
|
349
541
|
delete(element) {
|
|
350
542
|
const size = this.size;
|
|
351
543
|
if (size === 0)
|
|
@@ -363,12 +555,38 @@ class Deque {
|
|
|
363
555
|
this.cut(index - 1);
|
|
364
556
|
return this.size;
|
|
365
557
|
}
|
|
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
|
+
*/
|
|
366
571
|
deleteByIterator(iter) {
|
|
367
572
|
const index = iter.index;
|
|
368
573
|
this.deleteAt(index);
|
|
369
574
|
iter = iter.next();
|
|
370
575
|
return iter;
|
|
371
576
|
}
|
|
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
|
+
*/
|
|
372
590
|
findIterator(element) {
|
|
373
591
|
for (let i = 0; i < this.size; ++i) {
|
|
374
592
|
if (this.getAt(i) === element) {
|
|
@@ -377,6 +595,19 @@ class Deque {
|
|
|
377
595
|
}
|
|
378
596
|
return this.end();
|
|
379
597
|
}
|
|
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
|
+
*/
|
|
380
611
|
reverse() {
|
|
381
612
|
this._buckets.reverse().forEach(function (bucket) {
|
|
382
613
|
bucket.reverse();
|
|
@@ -388,6 +619,18 @@ class Deque {
|
|
|
388
619
|
this._lastInBucket = this._bucketSize - _firstInBucket - 1;
|
|
389
620
|
return this;
|
|
390
621
|
}
|
|
622
|
+
/**
|
|
623
|
+
* Time Complexity: O(n)
|
|
624
|
+
* Space Complexity: O(1)
|
|
625
|
+
*/
|
|
626
|
+
/**
|
|
627
|
+
* Time Complexity: O(n)
|
|
628
|
+
* Space Complexity: O(1)
|
|
629
|
+
*
|
|
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.
|
|
633
|
+
*/
|
|
391
634
|
unique() {
|
|
392
635
|
if (this.size <= 1) {
|
|
393
636
|
return this.size;
|
|
@@ -404,6 +647,20 @@ class Deque {
|
|
|
404
647
|
this.cut(index - 1);
|
|
405
648
|
return this.size;
|
|
406
649
|
}
|
|
650
|
+
/**
|
|
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
|
+
*/
|
|
407
664
|
sort(comparator) {
|
|
408
665
|
const arr = [];
|
|
409
666
|
for (let i = 0; i < this.size; ++i) {
|
|
@@ -415,6 +672,19 @@ class Deque {
|
|
|
415
672
|
}
|
|
416
673
|
return this;
|
|
417
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
|
+
*/
|
|
418
688
|
shrinkToFit() {
|
|
419
689
|
if (this.size === 0)
|
|
420
690
|
return;
|
|
@@ -438,11 +708,39 @@ class Deque {
|
|
|
438
708
|
this._bucketLast = newBuckets.length - 1;
|
|
439
709
|
this._buckets = newBuckets;
|
|
440
710
|
}
|
|
711
|
+
/**
|
|
712
|
+
* Time Complexity: O(n)
|
|
713
|
+
* Space Complexity: O(1)
|
|
714
|
+
*/
|
|
715
|
+
/**
|
|
716
|
+
* Time Complexity: O(n)
|
|
717
|
+
* Space Complexity: O(1)
|
|
718
|
+
*
|
|
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:
|
|
723
|
+
*/
|
|
441
724
|
forEach(callback) {
|
|
442
725
|
for (let i = 0; i < this.size; ++i) {
|
|
443
726
|
callback(this.getAt(i), i, this);
|
|
444
727
|
}
|
|
445
728
|
}
|
|
729
|
+
/**
|
|
730
|
+
* Time Complexity: O(n)
|
|
731
|
+
* Space Complexity: O(1)
|
|
732
|
+
*/
|
|
733
|
+
/**
|
|
734
|
+
* Time Complexity: O(n)
|
|
735
|
+
* Space Complexity: O(1)
|
|
736
|
+
*
|
|
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`.
|
|
743
|
+
*/
|
|
446
744
|
find(callback) {
|
|
447
745
|
for (let i = 0; i < this.size; ++i) {
|
|
448
746
|
const element = this.getAt(i);
|
|
@@ -452,6 +750,17 @@ class Deque {
|
|
|
452
750
|
}
|
|
453
751
|
return undefined;
|
|
454
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
|
+
*/
|
|
455
764
|
toArray() {
|
|
456
765
|
const arr = [];
|
|
457
766
|
for (let i = 0; i < this.size; ++i) {
|
|
@@ -459,6 +768,19 @@ class Deque {
|
|
|
459
768
|
}
|
|
460
769
|
return arr;
|
|
461
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
|
+
*/
|
|
462
784
|
map(callback) {
|
|
463
785
|
const newDeque = new Deque([], this._bucketSize);
|
|
464
786
|
for (let i = 0; i < this.size; ++i) {
|
|
@@ -466,6 +788,21 @@ class Deque {
|
|
|
466
788
|
}
|
|
467
789
|
return newDeque;
|
|
468
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
|
+
*/
|
|
469
806
|
filter(predicate) {
|
|
470
807
|
const newDeque = new Deque([], this._bucketSize);
|
|
471
808
|
for (let i = 0; i < this.size; ++i) {
|
|
@@ -476,6 +813,23 @@ class Deque {
|
|
|
476
813
|
}
|
|
477
814
|
return newDeque;
|
|
478
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
|
+
*/
|
|
479
833
|
reduce(callback, initialValue) {
|
|
480
834
|
let accumulator = initialValue;
|
|
481
835
|
for (let i = 0; i < this.size; ++i) {
|
|
@@ -483,6 +837,21 @@ class Deque {
|
|
|
483
837
|
}
|
|
484
838
|
return accumulator;
|
|
485
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
|
+
*/
|
|
486
855
|
indexOf(element) {
|
|
487
856
|
for (let i = 0; i < this.size; ++i) {
|
|
488
857
|
if (this.getAt(i) === element) {
|
|
@@ -491,11 +860,35 @@ class Deque {
|
|
|
491
860
|
}
|
|
492
861
|
return -1;
|
|
493
862
|
}
|
|
863
|
+
/**
|
|
864
|
+
* Time Complexity: O(n)
|
|
865
|
+
* Space Complexity: O(1)
|
|
866
|
+
*/
|
|
867
|
+
/**
|
|
868
|
+
* Time Complexity: O(n)
|
|
869
|
+
* Space Complexity: O(1)
|
|
870
|
+
*
|
|
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.
|
|
873
|
+
*/
|
|
494
874
|
*[Symbol.iterator]() {
|
|
495
875
|
for (let i = 0; i < this.size; ++i) {
|
|
496
876
|
yield this.getAt(i);
|
|
497
877
|
}
|
|
498
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
|
+
*/
|
|
499
892
|
_reallocate(needBucketNum) {
|
|
500
893
|
const newBuckets = [];
|
|
501
894
|
const addBucketNum = needBucketNum || this._bucketCount >> 1 || 1;
|
|
@@ -517,6 +910,19 @@ class Deque {
|
|
|
517
910
|
this._buckets = newBuckets;
|
|
518
911
|
this._bucketCount = newBuckets.length;
|
|
519
912
|
}
|
|
913
|
+
/**
|
|
914
|
+
* Time Complexity: O(1)
|
|
915
|
+
* Space Complexity: O(1)
|
|
916
|
+
*/
|
|
917
|
+
/**
|
|
918
|
+
* Time Complexity: O(1)
|
|
919
|
+
* Space Complexity: O(1)
|
|
920
|
+
*
|
|
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".
|
|
925
|
+
*/
|
|
520
926
|
_getBucketAndPosition(pos) {
|
|
521
927
|
let bucketIndex;
|
|
522
928
|
let indexInBucket;
|
package/dist/types/helpers.d.ts
CHANGED
|
@@ -11,9 +11,9 @@ export declare const enum IterateDirection {
|
|
|
11
11
|
REVERSE = 1
|
|
12
12
|
}
|
|
13
13
|
export interface IterableWithSize<T> extends Iterable<T> {
|
|
14
|
-
size: number;
|
|
14
|
+
size: number | ((...args: any[]) => number);
|
|
15
15
|
}
|
|
16
16
|
export interface IterableWithLength<T> extends Iterable<T> {
|
|
17
|
-
length: number;
|
|
17
|
+
length: number | ((...args: any[]) => number);
|
|
18
18
|
}
|
|
19
19
|
export type IterableWithSizeOrLength<T> = IterableWithSize<T> | IterableWithLength<T>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "graph-typed",
|
|
3
|
-
"version": "1.46.
|
|
3
|
+
"version": "1.46.2",
|
|
4
4
|
"description": "Graph. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -136,6 +136,6 @@
|
|
|
136
136
|
"typescript": "^4.9.5"
|
|
137
137
|
},
|
|
138
138
|
"dependencies": {
|
|
139
|
-
"data-structure-typed": "^1.46.
|
|
139
|
+
"data-structure-typed": "^1.46.2"
|
|
140
140
|
}
|
|
141
141
|
}
|