graph-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.
- package/dist/data-structures/hash/hash-map.d.ts +10 -91
- package/dist/data-structures/hash/hash-map.js +22 -178
- package/dist/data-structures/queue/deque.d.ts +358 -24
- package/dist/data-structures/queue/deque.js +380 -96
- package/dist/types/helpers.d.ts +2 -2
- package/package.json +2 -2
- package/src/data-structures/hash/hash-map.ts +25 -196
- package/src/data-structures/queue/deque.ts +401 -113
- package/src/types/helpers.ts +2 -2
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
* @license MIT License
|
|
8
8
|
*/
|
|
9
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
-
exports.ObjectDeque = exports.Deque =
|
|
10
|
+
exports.ObjectDeque = exports.Deque = void 0;
|
|
11
11
|
const utils_1 = require("../../utils");
|
|
12
12
|
/**
|
|
13
13
|
* Deque can provide random access with O(1) time complexity
|
|
@@ -15,65 +15,16 @@ const utils_1 = require("../../utils");
|
|
|
15
15
|
* Deque may experience performance jitter, but DoublyLinkedList will not
|
|
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
|
-
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
18
|
class Deque {
|
|
19
|
+
/**
|
|
20
|
+
* The constructor initializes a data structure with a specified bucket size and populates it with
|
|
21
|
+
* elements from an iterable.
|
|
22
|
+
* @param elements - The `elements` parameter is an iterable object (such as an array or a Set) that
|
|
23
|
+
* contains the initial elements to be stored in the data structure. It can also be an object with a
|
|
24
|
+
* `length` property or a `size` property, which represents the number of elements in the iterable.
|
|
25
|
+
* @param bucketSize - The `bucketSize` parameter is the maximum number of elements that can be
|
|
26
|
+
* stored in each bucket. It determines the size of each bucket in the data structure.
|
|
27
|
+
*/
|
|
77
28
|
constructor(elements = [], bucketSize = (1 << 12)) {
|
|
78
29
|
this._bucketFirst = 0;
|
|
79
30
|
this._firstInBucket = 0;
|
|
@@ -84,10 +35,16 @@ class Deque {
|
|
|
84
35
|
this._size = 0;
|
|
85
36
|
let _size;
|
|
86
37
|
if ('length' in elements) {
|
|
87
|
-
|
|
38
|
+
if (elements.length instanceof Function)
|
|
39
|
+
_size = elements.length();
|
|
40
|
+
else
|
|
41
|
+
_size = elements.length;
|
|
88
42
|
}
|
|
89
43
|
else {
|
|
90
|
-
|
|
44
|
+
if (elements.size instanceof Function)
|
|
45
|
+
_size = elements.size();
|
|
46
|
+
else
|
|
47
|
+
_size = elements.size;
|
|
91
48
|
}
|
|
92
49
|
this._bucketSize = bucketSize;
|
|
93
50
|
this._bucketCount = (0, utils_1.calcMinUnitsRequired)(_size, this._bucketSize) || 1;
|
|
@@ -107,6 +64,11 @@ class Deque {
|
|
|
107
64
|
get size() {
|
|
108
65
|
return this._size;
|
|
109
66
|
}
|
|
67
|
+
/**
|
|
68
|
+
* The function returns the first element in a collection if it exists, otherwise it returns
|
|
69
|
+
* undefined.
|
|
70
|
+
* @returns The first element of the collection, of type E, is being returned.
|
|
71
|
+
*/
|
|
110
72
|
get first() {
|
|
111
73
|
if (this.size === 0)
|
|
112
74
|
return;
|
|
@@ -117,13 +79,6 @@ class Deque {
|
|
|
117
79
|
return;
|
|
118
80
|
return this._buckets[this._bucketLast][this._lastInBucket];
|
|
119
81
|
}
|
|
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
82
|
/**
|
|
128
83
|
* Time Complexity: O(1) - Removes the last element.
|
|
129
84
|
* Space Complexity: O(1) - Operates in-place.
|
|
@@ -182,24 +137,50 @@ class Deque {
|
|
|
182
137
|
popFirst() {
|
|
183
138
|
return this.shift();
|
|
184
139
|
}
|
|
140
|
+
/**
|
|
141
|
+
* The clear() function resets the state of the object by initializing all variables to their default
|
|
142
|
+
* values.
|
|
143
|
+
*/
|
|
185
144
|
clear() {
|
|
186
145
|
this._buckets = [new Array(this._bucketSize)];
|
|
187
146
|
this._bucketCount = 1;
|
|
188
147
|
this._bucketFirst = this._bucketLast = this._size = 0;
|
|
189
148
|
this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
|
|
190
149
|
}
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
150
|
+
/**
|
|
151
|
+
* The below function is a generator that yields elements from a collection one by one.
|
|
152
|
+
*/
|
|
153
|
+
*begin() {
|
|
154
|
+
let index = 0;
|
|
155
|
+
while (index < this.size) {
|
|
156
|
+
yield this.getAt(index);
|
|
157
|
+
index++;
|
|
158
|
+
}
|
|
199
159
|
}
|
|
200
|
-
|
|
201
|
-
|
|
160
|
+
/**
|
|
161
|
+
* The function `reverseBegin()` is a generator that yields elements in reverse order starting from
|
|
162
|
+
* the last element.
|
|
163
|
+
*/
|
|
164
|
+
*reverseBegin() {
|
|
165
|
+
let index = this.size - 1;
|
|
166
|
+
while (index >= 0) {
|
|
167
|
+
yield this.getAt(index);
|
|
168
|
+
index--;
|
|
169
|
+
}
|
|
202
170
|
}
|
|
171
|
+
/**
|
|
172
|
+
* Time Complexity - Amortized O(1) (possible reallocation)
|
|
173
|
+
* Space Complexity - O(n) (due to potential resizing).
|
|
174
|
+
*/
|
|
175
|
+
/**
|
|
176
|
+
* Time Complexity - Amortized O(1) (possible reallocation),
|
|
177
|
+
* Space Complexity - O(n) (due to potential resizing).
|
|
178
|
+
*
|
|
179
|
+
* The push function adds an element to a data structure and reallocates memory if necessary.
|
|
180
|
+
* @param {E} element - The `element` parameter represents the value that you want to add to the data
|
|
181
|
+
* structure.
|
|
182
|
+
* @returns The size of the data structure after the element has been pushed.
|
|
183
|
+
*/
|
|
203
184
|
push(element) {
|
|
204
185
|
if (this.size) {
|
|
205
186
|
if (this._lastInBucket < this._bucketSize - 1) {
|
|
@@ -221,6 +202,18 @@ class Deque {
|
|
|
221
202
|
this._buckets[this._bucketLast][this._lastInBucket] = element;
|
|
222
203
|
return this.size;
|
|
223
204
|
}
|
|
205
|
+
/**
|
|
206
|
+
* Time Complexity: O(1)
|
|
207
|
+
* Space Complexity: O(1)
|
|
208
|
+
*/
|
|
209
|
+
/**
|
|
210
|
+
* Time Complexity: O(1)
|
|
211
|
+
* Space Complexity: O(1)
|
|
212
|
+
*
|
|
213
|
+
* The `pop()` function removes and returns the last element from a data structure, updating the
|
|
214
|
+
* internal state variables accordingly.
|
|
215
|
+
* @returns The element that was removed from the data structure is being returned.
|
|
216
|
+
*/
|
|
224
217
|
pop() {
|
|
225
218
|
if (this.size === 0)
|
|
226
219
|
return;
|
|
@@ -241,6 +234,20 @@ class Deque {
|
|
|
241
234
|
this._size -= 1;
|
|
242
235
|
return element;
|
|
243
236
|
}
|
|
237
|
+
/**
|
|
238
|
+
* Time Complexity: Amortized O(1)
|
|
239
|
+
* Space Complexity: O(n)
|
|
240
|
+
*/
|
|
241
|
+
/**
|
|
242
|
+
* Time Complexity: Amortized O(1)
|
|
243
|
+
* Space Complexity: O(n)
|
|
244
|
+
*
|
|
245
|
+
* The `unshift` function adds an element to the beginning of an array-like data structure and
|
|
246
|
+
* returns the new size of the structure.
|
|
247
|
+
* @param {E} element - The `element` parameter represents the element that you want to add to the
|
|
248
|
+
* beginning of the data structure.
|
|
249
|
+
* @returns The size of the data structure after the element has been added.
|
|
250
|
+
*/
|
|
244
251
|
unshift(element) {
|
|
245
252
|
if (this.size) {
|
|
246
253
|
if (this._firstInBucket > 0) {
|
|
@@ -262,6 +269,19 @@ class Deque {
|
|
|
262
269
|
this._buckets[this._bucketFirst][this._firstInBucket] = element;
|
|
263
270
|
return this.size;
|
|
264
271
|
}
|
|
272
|
+
/**
|
|
273
|
+
* Time Complexity: O(1)
|
|
274
|
+
* Space Complexity: O(1)
|
|
275
|
+
*/
|
|
276
|
+
/**
|
|
277
|
+
* Time Complexity: O(1)
|
|
278
|
+
* Space Complexity: O(1)
|
|
279
|
+
*
|
|
280
|
+
* The `shift()` function removes and returns the first element from a data structure, updating the
|
|
281
|
+
* internal state variables accordingly.
|
|
282
|
+
* @returns The element that is being removed from the beginning of the data structure is being
|
|
283
|
+
* returned.
|
|
284
|
+
*/
|
|
265
285
|
shift() {
|
|
266
286
|
if (this.size === 0)
|
|
267
287
|
return;
|
|
@@ -282,19 +302,66 @@ class Deque {
|
|
|
282
302
|
this._size -= 1;
|
|
283
303
|
return element;
|
|
284
304
|
}
|
|
305
|
+
/**
|
|
306
|
+
* Time Complexity: O(1)
|
|
307
|
+
* Space Complexity: O(1)
|
|
308
|
+
*/
|
|
309
|
+
/**
|
|
310
|
+
* Time Complexity: O(1)
|
|
311
|
+
* Space Complexity: O(1)
|
|
312
|
+
*
|
|
313
|
+
* The `getAt` function retrieves an element at a specified position in an array-like data structure.
|
|
314
|
+
* @param {number} pos - The `pos` parameter represents the position of the element that you want to
|
|
315
|
+
* retrieve from the data structure. It is of type `number` and should be a valid index within the
|
|
316
|
+
* range of the data structure.
|
|
317
|
+
* @returns The element at the specified position in the data structure is being returned.
|
|
318
|
+
*/
|
|
285
319
|
getAt(pos) {
|
|
286
|
-
utils_1.rangeCheck(pos, 0, this.size - 1);
|
|
320
|
+
(0, utils_1.rangeCheck)(pos, 0, this.size - 1);
|
|
287
321
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
288
322
|
return this._buckets[bucketIndex][indexInBucket];
|
|
289
323
|
}
|
|
324
|
+
/**
|
|
325
|
+
* Time Complexity: O(1)
|
|
326
|
+
* Space Complexity: O(1)
|
|
327
|
+
*/
|
|
328
|
+
/**
|
|
329
|
+
* Time Complexity: O(1)
|
|
330
|
+
* Space Complexity: O(1)
|
|
331
|
+
*
|
|
332
|
+
* The `setAt` function sets an element at a specific position in an array-like data structure.
|
|
333
|
+
* @param {number} pos - The `pos` parameter represents the position at which the element needs to be
|
|
334
|
+
* set. It is of type `number`.
|
|
335
|
+
* @param {E} element - The `element` parameter is the value that you want to set at the specified
|
|
336
|
+
* position in the data structure.
|
|
337
|
+
*/
|
|
290
338
|
setAt(pos, element) {
|
|
291
|
-
utils_1.rangeCheck(pos, 0, this.size - 1);
|
|
339
|
+
(0, utils_1.rangeCheck)(pos, 0, this.size - 1);
|
|
292
340
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
293
341
|
this._buckets[bucketIndex][indexInBucket] = element;
|
|
294
342
|
}
|
|
343
|
+
/**
|
|
344
|
+
* Time Complexity: O(n)
|
|
345
|
+
* Space Complexity: O(n)
|
|
346
|
+
*/
|
|
347
|
+
/**
|
|
348
|
+
* Time Complexity: O(n)
|
|
349
|
+
* Space Complexity: O(n)
|
|
350
|
+
*
|
|
351
|
+
* The `insertAt` function inserts one or more elements at a specified position in an array-like data
|
|
352
|
+
* structure.
|
|
353
|
+
* @param {number} pos - The `pos` parameter represents the position at which the element(s) should
|
|
354
|
+
* be inserted. It is of type `number`.
|
|
355
|
+
* @param {E} element - The `element` parameter represents the element that you want to insert into
|
|
356
|
+
* the array at the specified position.
|
|
357
|
+
* @param [num=1] - The `num` parameter represents the number of times the `element` should be
|
|
358
|
+
* inserted at the specified position (`pos`). By default, it is set to 1, meaning that the `element`
|
|
359
|
+
* will be inserted once. However, you can provide a different value for `num` if you want
|
|
360
|
+
* @returns The size of the array after the insertion is being returned.
|
|
361
|
+
*/
|
|
295
362
|
insertAt(pos, element, num = 1) {
|
|
296
363
|
const length = this.size;
|
|
297
|
-
utils_1.rangeCheck(pos, 0, length);
|
|
364
|
+
(0, utils_1.rangeCheck)(pos, 0, length);
|
|
298
365
|
if (pos === 0) {
|
|
299
366
|
while (num--)
|
|
300
367
|
this.unshift(element);
|
|
@@ -316,6 +383,20 @@ class Deque {
|
|
|
316
383
|
}
|
|
317
384
|
return this.size;
|
|
318
385
|
}
|
|
386
|
+
/**
|
|
387
|
+
* Time Complexity: O(1)
|
|
388
|
+
* Space Complexity: O(1)
|
|
389
|
+
*/
|
|
390
|
+
/**
|
|
391
|
+
* Time Complexity: O(1)
|
|
392
|
+
* Space Complexity: O(1)
|
|
393
|
+
*
|
|
394
|
+
* The `cut` function updates the state of the object based on the given position and returns the
|
|
395
|
+
* updated size.
|
|
396
|
+
* @param {number} pos - The `pos` parameter represents the position at which the string should be
|
|
397
|
+
* cut. It is a number that indicates the index of the character where the cut should be made.
|
|
398
|
+
* @returns The method is returning the updated size of the data structure.
|
|
399
|
+
*/
|
|
319
400
|
cut(pos) {
|
|
320
401
|
if (pos < 0) {
|
|
321
402
|
this.clear();
|
|
@@ -327,8 +408,23 @@ class Deque {
|
|
|
327
408
|
this._size = pos + 1;
|
|
328
409
|
return this.size;
|
|
329
410
|
}
|
|
411
|
+
/**
|
|
412
|
+
* Time Complexity: O(n)
|
|
413
|
+
* Space Complexity: O(1)
|
|
414
|
+
*/
|
|
415
|
+
/**
|
|
416
|
+
* Time Complexity: O(n)
|
|
417
|
+
* Space Complexity: O(1)
|
|
418
|
+
*
|
|
419
|
+
* The `deleteAt` function removes an element at a specified position in an array-like data
|
|
420
|
+
* structure.
|
|
421
|
+
* @param {number} pos - The `pos` parameter in the `deleteAt` function represents the position at
|
|
422
|
+
* which an element needs to be deleted from the data structure. It is of type `number` and indicates
|
|
423
|
+
* the index of the element to be deleted.
|
|
424
|
+
* @returns The size of the data structure after the deletion operation is performed.
|
|
425
|
+
*/
|
|
330
426
|
deleteAt(pos) {
|
|
331
|
-
utils_1.rangeCheck(pos, 0, this.size - 1);
|
|
427
|
+
(0, utils_1.rangeCheck)(pos, 0, this.size - 1);
|
|
332
428
|
if (pos === 0)
|
|
333
429
|
this.shift();
|
|
334
430
|
else if (pos === this.size - 1)
|
|
@@ -346,6 +442,20 @@ class Deque {
|
|
|
346
442
|
}
|
|
347
443
|
return this.size;
|
|
348
444
|
}
|
|
445
|
+
/**
|
|
446
|
+
* Time Complexity: O(n)
|
|
447
|
+
* Space Complexity: O(1)
|
|
448
|
+
*/
|
|
449
|
+
/**
|
|
450
|
+
* Time Complexity: O(n)
|
|
451
|
+
* Space Complexity: O(1)
|
|
452
|
+
*
|
|
453
|
+
* The `delete` function removes all occurrences of a specified element from an array-like data
|
|
454
|
+
* structure.
|
|
455
|
+
* @param {E} element - The `element` parameter represents the element that you want to delete from
|
|
456
|
+
* the data structure.
|
|
457
|
+
* @returns The size of the data structure after the element has been deleted.
|
|
458
|
+
*/
|
|
349
459
|
delete(element) {
|
|
350
460
|
const size = this.size;
|
|
351
461
|
if (size === 0)
|
|
@@ -363,20 +473,19 @@ class Deque {
|
|
|
363
473
|
this.cut(index - 1);
|
|
364
474
|
return this.size;
|
|
365
475
|
}
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
}
|
|
476
|
+
/**
|
|
477
|
+
* Time Complexity: O(n)
|
|
478
|
+
* Space Complexity: O(1)
|
|
479
|
+
*/
|
|
480
|
+
/**
|
|
481
|
+
* Time Complexity: O(n)
|
|
482
|
+
* Space Complexity: O(1)
|
|
483
|
+
*
|
|
484
|
+
* The reverse() function reverses the order of the buckets and the elements within each bucket in a
|
|
485
|
+
* data structure.
|
|
486
|
+
* @returns The reverse() method is returning the object itself (this) after performing the reverse
|
|
487
|
+
* operation on the buckets and updating the relevant properties.
|
|
488
|
+
*/
|
|
380
489
|
reverse() {
|
|
381
490
|
this._buckets.reverse().forEach(function (bucket) {
|
|
382
491
|
bucket.reverse();
|
|
@@ -388,6 +497,18 @@ class Deque {
|
|
|
388
497
|
this._lastInBucket = this._bucketSize - _firstInBucket - 1;
|
|
389
498
|
return this;
|
|
390
499
|
}
|
|
500
|
+
/**
|
|
501
|
+
* Time Complexity: O(n)
|
|
502
|
+
* Space Complexity: O(1)
|
|
503
|
+
*/
|
|
504
|
+
/**
|
|
505
|
+
* Time Complexity: O(n)
|
|
506
|
+
* Space Complexity: O(1)
|
|
507
|
+
*
|
|
508
|
+
* The `unique()` function removes duplicate elements from an array-like data structure and returns
|
|
509
|
+
* the number of unique elements.
|
|
510
|
+
* @returns The size of the modified array is being returned.
|
|
511
|
+
*/
|
|
391
512
|
unique() {
|
|
392
513
|
if (this.size <= 1) {
|
|
393
514
|
return this.size;
|
|
@@ -404,6 +525,20 @@ class Deque {
|
|
|
404
525
|
this.cut(index - 1);
|
|
405
526
|
return this.size;
|
|
406
527
|
}
|
|
528
|
+
/**
|
|
529
|
+
* Time Complexity: O(n log n)
|
|
530
|
+
* Space Complexity: O(n)
|
|
531
|
+
*/
|
|
532
|
+
/**
|
|
533
|
+
* Time Complexity: O(n log n)
|
|
534
|
+
* Space Complexity: O(n)
|
|
535
|
+
*
|
|
536
|
+
* The `sort` function sorts the elements in a data structure using a provided comparator function.
|
|
537
|
+
* @param [comparator] - The `comparator` parameter is a function that takes in two elements `x` and
|
|
538
|
+
* `y` of type `E` and returns a number. The comparator function is used to determine the order of
|
|
539
|
+
* the elements in the sorted array.
|
|
540
|
+
* @returns The method is returning the sorted instance of the object on which the method is called.
|
|
541
|
+
*/
|
|
407
542
|
sort(comparator) {
|
|
408
543
|
const arr = [];
|
|
409
544
|
for (let i = 0; i < this.size; ++i) {
|
|
@@ -415,6 +550,19 @@ class Deque {
|
|
|
415
550
|
}
|
|
416
551
|
return this;
|
|
417
552
|
}
|
|
553
|
+
/**
|
|
554
|
+
* Time Complexity: O(n)
|
|
555
|
+
* Space Complexity: O(n)
|
|
556
|
+
*/
|
|
557
|
+
/**
|
|
558
|
+
* Time Complexity: O(n)
|
|
559
|
+
* Space Complexity: O(n)
|
|
560
|
+
*
|
|
561
|
+
* The `shrinkToFit` function reorganizes the elements in an array-like data structure to minimize
|
|
562
|
+
* memory usage.
|
|
563
|
+
* @returns Nothing is being returned. The function is using the `return` statement to exit early if
|
|
564
|
+
* `this.size` is 0, but it does not return any value.
|
|
565
|
+
*/
|
|
418
566
|
shrinkToFit() {
|
|
419
567
|
if (this.size === 0)
|
|
420
568
|
return;
|
|
@@ -438,11 +586,39 @@ class Deque {
|
|
|
438
586
|
this._bucketLast = newBuckets.length - 1;
|
|
439
587
|
this._buckets = newBuckets;
|
|
440
588
|
}
|
|
589
|
+
/**
|
|
590
|
+
* Time Complexity: O(n)
|
|
591
|
+
* Space Complexity: O(1)
|
|
592
|
+
*/
|
|
593
|
+
/**
|
|
594
|
+
* Time Complexity: O(n)
|
|
595
|
+
* Space Complexity: O(1)
|
|
596
|
+
*
|
|
597
|
+
* The `forEach` function iterates over each element in a deque and applies a callback function to
|
|
598
|
+
* each element.
|
|
599
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
600
|
+
* deque. It takes three parameters:
|
|
601
|
+
*/
|
|
441
602
|
forEach(callback) {
|
|
442
603
|
for (let i = 0; i < this.size; ++i) {
|
|
443
604
|
callback(this.getAt(i), i, this);
|
|
444
605
|
}
|
|
445
606
|
}
|
|
607
|
+
/**
|
|
608
|
+
* Time Complexity: O(n)
|
|
609
|
+
* Space Complexity: O(1)
|
|
610
|
+
*/
|
|
611
|
+
/**
|
|
612
|
+
* Time Complexity: O(n)
|
|
613
|
+
* Space Complexity: O(1)
|
|
614
|
+
*
|
|
615
|
+
* The `find` function iterates over the elements in a deque and returns the first element for which
|
|
616
|
+
* the callback function returns true, or undefined if no such element is found.
|
|
617
|
+
* @param callback - A function that takes three parameters: element, index, and deque. It should
|
|
618
|
+
* return a boolean value indicating whether the element satisfies a certain condition.
|
|
619
|
+
* @returns The method `find` returns the first element in the deque that satisfies the condition
|
|
620
|
+
* specified by the callback function. If no element satisfies the condition, it returns `undefined`.
|
|
621
|
+
*/
|
|
446
622
|
find(callback) {
|
|
447
623
|
for (let i = 0; i < this.size; ++i) {
|
|
448
624
|
const element = this.getAt(i);
|
|
@@ -452,6 +628,17 @@ class Deque {
|
|
|
452
628
|
}
|
|
453
629
|
return undefined;
|
|
454
630
|
}
|
|
631
|
+
/**
|
|
632
|
+
* Time Complexity: O(n)
|
|
633
|
+
* Space Complexity: O(n)
|
|
634
|
+
*/
|
|
635
|
+
/**
|
|
636
|
+
* Time Complexity: O(n)
|
|
637
|
+
* Space Complexity: O(n)
|
|
638
|
+
*
|
|
639
|
+
* The `toArray` function converts the elements of a data structure into an array.
|
|
640
|
+
* @returns The `toArray()` method is returning an array of elements of type `E`.
|
|
641
|
+
*/
|
|
455
642
|
toArray() {
|
|
456
643
|
const arr = [];
|
|
457
644
|
for (let i = 0; i < this.size; ++i) {
|
|
@@ -459,6 +646,19 @@ class Deque {
|
|
|
459
646
|
}
|
|
460
647
|
return arr;
|
|
461
648
|
}
|
|
649
|
+
/**
|
|
650
|
+
* Time Complexity: O(n)
|
|
651
|
+
* Space Complexity: O(n)
|
|
652
|
+
*/
|
|
653
|
+
/**
|
|
654
|
+
* Time Complexity: O(n)
|
|
655
|
+
* Space Complexity: O(n)
|
|
656
|
+
*
|
|
657
|
+
* The `map` function takes a callback function and applies it to each element in the deque,
|
|
658
|
+
* returning a new deque with the results.
|
|
659
|
+
* @param callback - The `callback` parameter is a function that takes three arguments:
|
|
660
|
+
* @returns The `map` method is returning a new `Deque` object with the transformed elements.
|
|
661
|
+
*/
|
|
462
662
|
map(callback) {
|
|
463
663
|
const newDeque = new Deque([], this._bucketSize);
|
|
464
664
|
for (let i = 0; i < this.size; ++i) {
|
|
@@ -466,6 +666,21 @@ class Deque {
|
|
|
466
666
|
}
|
|
467
667
|
return newDeque;
|
|
468
668
|
}
|
|
669
|
+
/**
|
|
670
|
+
* Time Complexity: O(n)
|
|
671
|
+
* Space Complexity: O(n)
|
|
672
|
+
*/
|
|
673
|
+
/**
|
|
674
|
+
* Time Complexity: O(n)
|
|
675
|
+
* Space Complexity: O(n)
|
|
676
|
+
*
|
|
677
|
+
* The `filter` function creates a new deque containing only the elements that satisfy the given
|
|
678
|
+
* predicate function.
|
|
679
|
+
* @param predicate - The `predicate` parameter is a function that takes three arguments: `element`,
|
|
680
|
+
* `index`, and `deque`.
|
|
681
|
+
* @returns The `filter` method is returning a new `Deque` object that contains only the elements
|
|
682
|
+
* that satisfy the given `predicate` function.
|
|
683
|
+
*/
|
|
469
684
|
filter(predicate) {
|
|
470
685
|
const newDeque = new Deque([], this._bucketSize);
|
|
471
686
|
for (let i = 0; i < this.size; ++i) {
|
|
@@ -476,6 +691,23 @@ class Deque {
|
|
|
476
691
|
}
|
|
477
692
|
return newDeque;
|
|
478
693
|
}
|
|
694
|
+
/**
|
|
695
|
+
* Time Complexity: O(n)
|
|
696
|
+
* Space Complexity: O(1)
|
|
697
|
+
*/
|
|
698
|
+
/**
|
|
699
|
+
* Time Complexity: O(n)
|
|
700
|
+
* Space Complexity: O(1)
|
|
701
|
+
*
|
|
702
|
+
* The `reduce` function iterates over the elements of a deque and applies a callback function to
|
|
703
|
+
* each element, accumulating a single value.
|
|
704
|
+
* @param callback - The `callback` parameter is a function that takes four arguments:
|
|
705
|
+
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It
|
|
706
|
+
* is the value that will be passed as the first argument to the `callback` function when reducing
|
|
707
|
+
* the elements of the deque.
|
|
708
|
+
* @returns the final value of the accumulator after iterating over all elements in the deque and
|
|
709
|
+
* applying the callback function to each element.
|
|
710
|
+
*/
|
|
479
711
|
reduce(callback, initialValue) {
|
|
480
712
|
let accumulator = initialValue;
|
|
481
713
|
for (let i = 0; i < this.size; ++i) {
|
|
@@ -483,6 +715,21 @@ class Deque {
|
|
|
483
715
|
}
|
|
484
716
|
return accumulator;
|
|
485
717
|
}
|
|
718
|
+
/**
|
|
719
|
+
* Time Complexity: O(n)
|
|
720
|
+
* Space Complexity: O(1)
|
|
721
|
+
*/
|
|
722
|
+
/**
|
|
723
|
+
* Time Complexity: O(n)
|
|
724
|
+
* Space Complexity: O(1)
|
|
725
|
+
*
|
|
726
|
+
* The function "indexOf" returns the index of the first occurrence of a given element in an array,
|
|
727
|
+
* or -1 if the element is not found.
|
|
728
|
+
* @param {E} element - The "element" parameter represents the element that you want to find the
|
|
729
|
+
* index of in the data structure.
|
|
730
|
+
* @returns The indexOf function returns the index of the first occurrence of the specified element
|
|
731
|
+
* in the data structure. If the element is not found, it returns -1.
|
|
732
|
+
*/
|
|
486
733
|
indexOf(element) {
|
|
487
734
|
for (let i = 0; i < this.size; ++i) {
|
|
488
735
|
if (this.getAt(i) === element) {
|
|
@@ -491,11 +738,35 @@ class Deque {
|
|
|
491
738
|
}
|
|
492
739
|
return -1;
|
|
493
740
|
}
|
|
741
|
+
/**
|
|
742
|
+
* Time Complexity: O(n)
|
|
743
|
+
* Space Complexity: O(1)
|
|
744
|
+
*/
|
|
745
|
+
/**
|
|
746
|
+
* Time Complexity: O(n)
|
|
747
|
+
* Space Complexity: O(1)
|
|
748
|
+
*
|
|
749
|
+
* The above function is an implementation of the iterator protocol in TypeScript, allowing the
|
|
750
|
+
* object to be iterated over using a for...of loop.
|
|
751
|
+
*/
|
|
494
752
|
*[Symbol.iterator]() {
|
|
495
753
|
for (let i = 0; i < this.size; ++i) {
|
|
496
754
|
yield this.getAt(i);
|
|
497
755
|
}
|
|
498
756
|
}
|
|
757
|
+
/**
|
|
758
|
+
* Time Complexity: O(n)
|
|
759
|
+
* Space Complexity: O(n)
|
|
760
|
+
*/
|
|
761
|
+
/**
|
|
762
|
+
* Time Complexity: O(n)
|
|
763
|
+
* Space Complexity: O(n)
|
|
764
|
+
*
|
|
765
|
+
* The `_reallocate` function reallocates the buckets in an array, adding new buckets if needed.
|
|
766
|
+
* @param {number} [needBucketNum] - The `needBucketNum` parameter is an optional number that
|
|
767
|
+
* specifies the number of new buckets needed. If not provided, it will default to half of the
|
|
768
|
+
* current bucket count (`this._bucketCount >> 1`) or 1 if the current bucket count is less than 2.
|
|
769
|
+
*/
|
|
499
770
|
_reallocate(needBucketNum) {
|
|
500
771
|
const newBuckets = [];
|
|
501
772
|
const addBucketNum = needBucketNum || this._bucketCount >> 1 || 1;
|
|
@@ -517,6 +788,19 @@ class Deque {
|
|
|
517
788
|
this._buckets = newBuckets;
|
|
518
789
|
this._bucketCount = newBuckets.length;
|
|
519
790
|
}
|
|
791
|
+
/**
|
|
792
|
+
* Time Complexity: O(1)
|
|
793
|
+
* Space Complexity: O(1)
|
|
794
|
+
*/
|
|
795
|
+
/**
|
|
796
|
+
* Time Complexity: O(1)
|
|
797
|
+
* Space Complexity: O(1)
|
|
798
|
+
*
|
|
799
|
+
* The function calculates the bucket index and index within the bucket based on the given position.
|
|
800
|
+
* @param {number} pos - The `pos` parameter represents the position within the data structure. It is
|
|
801
|
+
* a number that indicates the index or position of an element within the structure.
|
|
802
|
+
* @returns an object with two properties: "bucketIndex" and "indexInBucket".
|
|
803
|
+
*/
|
|
520
804
|
_getBucketAndPosition(pos) {
|
|
521
805
|
let bucketIndex;
|
|
522
806
|
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.3",
|
|
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.3"
|
|
140
140
|
}
|
|
141
141
|
}
|