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.
- package/dist/data-structures/hash/hash-map.d.ts +6 -5
- package/dist/data-structures/hash/hash-map.js +9 -6
- package/dist/data-structures/heap/heap.d.ts +59 -47
- package/dist/data-structures/heap/heap.js +133 -123
- package/dist/data-structures/queue/deque.d.ts +131 -164
- package/dist/data-structures/queue/deque.js +543 -211
- package/dist/types/data-structures/hash/hash-map.d.ts +0 -9
- package/dist/types/helpers.d.ts +11 -0
- package/dist/utils/utils.d.ts +1 -0
- package/dist/utils/utils.js +3 -1
- package/package.json +2 -2
- package/src/data-structures/hash/hash-map.ts +11 -7
- package/src/data-structures/heap/heap.ts +132 -128
- package/src/data-structures/queue/deque.ts +605 -226
- package/src/types/data-structures/hash/hash-map.ts +0 -11
- package/src/types/helpers.ts +15 -0
- package/src/utils/utils.ts +2 -0
|
@@ -5,14 +5,596 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { DoublyLinkedList } from '../linked-list';
|
|
9
8
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
|
|
10
|
+
import { IterableWithSizeOrLength, IterateDirection } from "../../types";
|
|
11
|
+
import { calcMinUnitsRequired, rangeCheck, throwRangeError } from "../../utils";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Deque can provide random access with O(1) time complexity
|
|
15
|
+
* Deque is usually more compact and efficient in memory usage because it does not require additional space to store pointers.
|
|
16
|
+
* Deque may experience performance jitter, but DoublyLinkedList will not
|
|
17
|
+
* Deque is implemented using a dynamic array. Inserting or deleting beyond both ends of the array may require moving elements or reallocating space.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
export class DequeIterator<E> {
|
|
21
|
+
iterateDirection: IterateDirection;
|
|
22
|
+
|
|
23
|
+
index: number;
|
|
24
|
+
readonly deque: Deque<E>;
|
|
25
|
+
|
|
26
|
+
constructor(index: number, deque: Deque<E>, iterateDirection = IterateDirection.DEFAULT) {
|
|
27
|
+
this.index = index;
|
|
28
|
+
this.iterateDirection = iterateDirection;
|
|
29
|
+
if (this.iterateDirection === IterateDirection.DEFAULT) {
|
|
30
|
+
this.prev = function () {
|
|
31
|
+
if (this.index === 0) {
|
|
32
|
+
throwRangeError();
|
|
33
|
+
}
|
|
34
|
+
this.index -= 1;
|
|
35
|
+
return this;
|
|
36
|
+
};
|
|
37
|
+
this.next = function () {
|
|
38
|
+
if (this.index === this.deque.size) {
|
|
39
|
+
throwRangeError();
|
|
40
|
+
}
|
|
41
|
+
this.index += 1;
|
|
42
|
+
return this;
|
|
43
|
+
};
|
|
44
|
+
} else {
|
|
45
|
+
this.prev = function () {
|
|
46
|
+
if (this.index === this.deque.size - 1) {
|
|
47
|
+
throwRangeError();
|
|
48
|
+
}
|
|
49
|
+
this.index += 1;
|
|
50
|
+
return this;
|
|
51
|
+
};
|
|
52
|
+
this.next = function () {
|
|
53
|
+
if (this.index === -1) {
|
|
54
|
+
throwRangeError();
|
|
55
|
+
}
|
|
56
|
+
this.index -= 1;
|
|
57
|
+
return this;
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
this.deque = deque;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
get current() {
|
|
64
|
+
return this.deque.getAt(this.index);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
set current(newElement: E) {
|
|
68
|
+
this.deque.setAt(this.index, newElement);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
isAccessible() {
|
|
72
|
+
return this.index !== this.deque.size;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
prev(): DequeIterator<E> {
|
|
76
|
+
return this;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
next(): DequeIterator<E> {
|
|
80
|
+
return this;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
clone() {
|
|
84
|
+
return new DequeIterator<E>(this.index, this.deque, this.iterateDirection);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export class Deque<E> {
|
|
90
|
+
protected _bucketFirst = 0;
|
|
91
|
+
protected _firstInBucket = 0;
|
|
92
|
+
protected _bucketLast = 0;
|
|
93
|
+
protected _lastInBucket = 0;
|
|
94
|
+
protected _bucketCount = 0;
|
|
95
|
+
protected readonly _bucketSize: number;
|
|
96
|
+
|
|
97
|
+
constructor(elements: IterableWithSizeOrLength<E> = [], bucketSize = (1 << 12)) {
|
|
98
|
+
|
|
99
|
+
let _size;
|
|
100
|
+
if ('length' in elements) {
|
|
101
|
+
_size = elements.length;
|
|
102
|
+
} else {
|
|
103
|
+
_size = elements.size;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
this._bucketSize = bucketSize;
|
|
107
|
+
this._bucketCount = calcMinUnitsRequired(_size, this._bucketSize) || 1;
|
|
108
|
+
for (let i = 0; i < this._bucketCount; ++i) {
|
|
109
|
+
this._buckets.push(new Array(this._bucketSize));
|
|
110
|
+
}
|
|
111
|
+
const needBucketNum = calcMinUnitsRequired(_size, this._bucketSize);
|
|
112
|
+
this._bucketFirst = this._bucketLast = (this._bucketCount >> 1) - (needBucketNum >> 1);
|
|
113
|
+
this._firstInBucket = this._lastInBucket = (this._bucketSize - _size % this._bucketSize) >> 1;
|
|
114
|
+
|
|
115
|
+
for (const element of elements) {
|
|
116
|
+
this.push(element);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
protected _buckets: E[][] = [];
|
|
121
|
+
|
|
122
|
+
get buckets() {
|
|
123
|
+
return this._buckets;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
protected _size = 0;
|
|
127
|
+
|
|
128
|
+
get size() {
|
|
129
|
+
return this._size;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
get first(): E | undefined {
|
|
133
|
+
if (this.size === 0) return;
|
|
134
|
+
return this._buckets[this._bucketFirst][this._firstInBucket];
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
get last(): E | undefined {
|
|
138
|
+
if (this.size === 0) return;
|
|
139
|
+
return this._buckets[this._bucketLast][this._lastInBucket];
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Time Complexity: Amortized O(1) - Generally constant time, but resizing when the deque is full leads to O(n).
|
|
144
|
+
* Space Complexity: O(n) - In worst case, resizing doubles the array size.
|
|
145
|
+
*/
|
|
146
|
+
|
|
147
|
+
empty() {
|
|
148
|
+
return this._size === 0;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Time Complexity: O(1) - Removes the last element.
|
|
153
|
+
* Space Complexity: O(1) - Operates in-place.
|
|
154
|
+
*/
|
|
155
|
+
|
|
156
|
+
isEmpty() {
|
|
157
|
+
return this.size === 0;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
|
|
162
|
+
* Space Complexity: O(n) - Due to potential resizing.
|
|
163
|
+
*/
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Time Complexity: O(1)
|
|
167
|
+
* Space Complexity: O(n) - In worst case, resizing doubles the array size.
|
|
168
|
+
*
|
|
169
|
+
* The addLast function adds an element to the end of an array.
|
|
170
|
+
* @param {E} element - The element parameter represents the element that you want to add to the end of the
|
|
171
|
+
* data structure.
|
|
172
|
+
*/
|
|
173
|
+
addLast(element: E): void {
|
|
174
|
+
this.push(element);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Time Complexity: O(1) - Removes the first element.
|
|
179
|
+
* Space Complexity: O(1) - In-place operation.
|
|
180
|
+
*/
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Time Complexity: O(1) - Removes the last element.
|
|
184
|
+
* Space Complexity: O(1) - Operates in-place.
|
|
185
|
+
*
|
|
186
|
+
* The function "popLast" removes and returns the last element of an array.
|
|
187
|
+
* @returns The last element of the array is being returned.
|
|
188
|
+
*/
|
|
189
|
+
popLast(): E | undefined {
|
|
190
|
+
return this.pop();
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Time Complexity: O(1).
|
|
195
|
+
* Space Complexity: O(n) - Due to potential resizing.
|
|
196
|
+
*
|
|
197
|
+
* The "addFirst" function adds an element to the beginning of an array.
|
|
198
|
+
* @param {E} element - The parameter "element" represents the element that you want to add to the
|
|
199
|
+
* beginning of the data structure.
|
|
200
|
+
*/
|
|
201
|
+
addFirst(element: E): void {
|
|
202
|
+
this.unshift(element);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Time Complexity: O(1) - Removes the first element.
|
|
207
|
+
* Space Complexity: O(1) - In-place operation.
|
|
208
|
+
*
|
|
209
|
+
* The function "popFirst" removes and returns the first element of an array.
|
|
210
|
+
* @returns The method `popFirst()` is returning the first element of the array after removing it
|
|
211
|
+
* from the beginning. If the array is empty, it will return `undefined`.
|
|
212
|
+
*/
|
|
213
|
+
popFirst(): E | undefined {
|
|
214
|
+
return this.shift();
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
clear() {
|
|
218
|
+
this._buckets = [new Array(this._bucketSize)];
|
|
219
|
+
this._bucketCount = 1;
|
|
220
|
+
this._bucketFirst = this._bucketLast = this._size = 0;
|
|
221
|
+
this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
begin() {
|
|
225
|
+
return new DequeIterator<E>(0, this);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
end() {
|
|
229
|
+
return new DequeIterator<E>(this.size, this);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
reverseBegin() {
|
|
233
|
+
return new DequeIterator<E>(this.size - 1, this, IterateDirection.REVERSE);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
reverseEnd() {
|
|
237
|
+
return new DequeIterator<E>(-1, this, IterateDirection.REVERSE);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
push(element: E) {
|
|
241
|
+
if (this.size) {
|
|
242
|
+
if (this._lastInBucket < this._bucketSize - 1) {
|
|
243
|
+
this._lastInBucket += 1;
|
|
244
|
+
} else if (this._bucketLast < this._bucketCount - 1) {
|
|
245
|
+
this._bucketLast += 1;
|
|
246
|
+
this._lastInBucket = 0;
|
|
247
|
+
} else {
|
|
248
|
+
this._bucketLast = 0;
|
|
249
|
+
this._lastInBucket = 0;
|
|
250
|
+
}
|
|
251
|
+
if (
|
|
252
|
+
this._bucketLast === this._bucketFirst &&
|
|
253
|
+
this._lastInBucket === this._firstInBucket
|
|
254
|
+
) this._reallocate();
|
|
255
|
+
}
|
|
256
|
+
this._size += 1;
|
|
257
|
+
this._buckets[this._bucketLast][this._lastInBucket] = element;
|
|
258
|
+
return this.size;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
pop() {
|
|
262
|
+
if (this.size === 0) return;
|
|
263
|
+
const element = this._buckets[this._bucketLast][this._lastInBucket];
|
|
264
|
+
if (this.size !== 1) {
|
|
265
|
+
if (this._lastInBucket > 0) {
|
|
266
|
+
this._lastInBucket -= 1;
|
|
267
|
+
} else if (this._bucketLast > 0) {
|
|
268
|
+
this._bucketLast -= 1;
|
|
269
|
+
this._lastInBucket = this._bucketSize - 1;
|
|
270
|
+
} else {
|
|
271
|
+
this._bucketLast = this._bucketCount - 1;
|
|
272
|
+
this._lastInBucket = this._bucketSize - 1;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
this._size -= 1;
|
|
276
|
+
return element;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
unshift(element: E) {
|
|
280
|
+
if (this.size) {
|
|
281
|
+
if (this._firstInBucket > 0) {
|
|
282
|
+
this._firstInBucket -= 1;
|
|
283
|
+
} else if (this._bucketFirst > 0) {
|
|
284
|
+
this._bucketFirst -= 1;
|
|
285
|
+
this._firstInBucket = this._bucketSize - 1;
|
|
286
|
+
} else {
|
|
287
|
+
this._bucketFirst = this._bucketCount - 1;
|
|
288
|
+
this._firstInBucket = this._bucketSize - 1;
|
|
289
|
+
}
|
|
290
|
+
if (
|
|
291
|
+
this._bucketFirst === this._bucketLast &&
|
|
292
|
+
this._firstInBucket === this._lastInBucket
|
|
293
|
+
) this._reallocate();
|
|
294
|
+
}
|
|
295
|
+
this._size += 1;
|
|
296
|
+
this._buckets[this._bucketFirst][this._firstInBucket] = element;
|
|
297
|
+
return this.size;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
shift() {
|
|
301
|
+
if (this.size === 0) return;
|
|
302
|
+
const element = this._buckets[this._bucketFirst][this._firstInBucket];
|
|
303
|
+
if (this.size !== 1) {
|
|
304
|
+
if (this._firstInBucket < this._bucketSize - 1) {
|
|
305
|
+
this._firstInBucket += 1;
|
|
306
|
+
} else if (this._bucketFirst < this._bucketCount - 1) {
|
|
307
|
+
this._bucketFirst += 1;
|
|
308
|
+
this._firstInBucket = 0;
|
|
309
|
+
} else {
|
|
310
|
+
this._bucketFirst = 0;
|
|
311
|
+
this._firstInBucket = 0;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
this._size -= 1;
|
|
315
|
+
return element;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
getAt(pos: number): E {
|
|
319
|
+
rangeCheck!(pos, 0, this.size - 1);
|
|
320
|
+
const {
|
|
321
|
+
bucketIndex,
|
|
322
|
+
indexInBucket
|
|
323
|
+
} = this._getBucketAndPosition(pos);
|
|
324
|
+
return this._buckets[bucketIndex][indexInBucket]!;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
setAt(pos: number, element: E) {
|
|
328
|
+
rangeCheck!(pos, 0, this.size - 1);
|
|
329
|
+
const {
|
|
330
|
+
bucketIndex,
|
|
331
|
+
indexInBucket
|
|
332
|
+
} = this._getBucketAndPosition(pos);
|
|
333
|
+
this._buckets[bucketIndex][indexInBucket] = element;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
insertAt(pos: number, element: E, num = 1) {
|
|
337
|
+
const length = this.size;
|
|
338
|
+
rangeCheck!(pos, 0, length);
|
|
339
|
+
if (pos === 0) {
|
|
340
|
+
while (num--) this.unshift(element);
|
|
341
|
+
} else if (pos === this.size) {
|
|
342
|
+
while (num--) this.push(element);
|
|
343
|
+
} else {
|
|
344
|
+
const arr: E[] = [];
|
|
345
|
+
for (let i = pos; i < this.size; ++i) {
|
|
346
|
+
arr.push(this.getAt(i));
|
|
347
|
+
}
|
|
348
|
+
this.cut(pos - 1);
|
|
349
|
+
for (let i = 0; i < num; ++i) this.push(element);
|
|
350
|
+
for (let i = 0; i < arr.length; ++i) this.push(arr[i]);
|
|
351
|
+
}
|
|
352
|
+
return this.size;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
cut(pos: number) {
|
|
356
|
+
if (pos < 0) {
|
|
357
|
+
this.clear();
|
|
358
|
+
return 0;
|
|
359
|
+
}
|
|
360
|
+
const {
|
|
361
|
+
bucketIndex,
|
|
362
|
+
indexInBucket
|
|
363
|
+
} = this._getBucketAndPosition(pos);
|
|
364
|
+
this._bucketLast = bucketIndex;
|
|
365
|
+
this._lastInBucket = indexInBucket;
|
|
366
|
+
this._size = pos + 1;
|
|
367
|
+
return this.size;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
deleteAt(pos: number) {
|
|
371
|
+
rangeCheck!(pos, 0, this.size - 1);
|
|
372
|
+
if (pos === 0) this.shift();
|
|
373
|
+
else if (pos === this.size - 1) this.pop();
|
|
374
|
+
else {
|
|
375
|
+
const length = this.size - 1;
|
|
376
|
+
let {
|
|
377
|
+
bucketIndex: curBucket,
|
|
378
|
+
indexInBucket: curPointer
|
|
379
|
+
} = this._getBucketAndPosition(pos);
|
|
380
|
+
for (let i = pos; i < length; ++i) {
|
|
381
|
+
const {
|
|
382
|
+
bucketIndex: nextBucket,
|
|
383
|
+
indexInBucket: nextPointer
|
|
384
|
+
} = this._getBucketAndPosition(pos + 1);
|
|
385
|
+
this._buckets[curBucket][curPointer] = this._buckets[nextBucket][nextPointer];
|
|
386
|
+
curBucket = nextBucket;
|
|
387
|
+
curPointer = nextPointer;
|
|
388
|
+
}
|
|
389
|
+
this.pop();
|
|
390
|
+
}
|
|
391
|
+
return this.size;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
delete(element: E) {
|
|
395
|
+
const size = this.size;
|
|
396
|
+
if (size === 0) return 0;
|
|
397
|
+
let i = 0;
|
|
398
|
+
let index = 0;
|
|
399
|
+
while (i < size) {
|
|
400
|
+
const oldElement = this.getAt(i);
|
|
401
|
+
if (oldElement !== element) {
|
|
402
|
+
this.setAt(index, oldElement!);
|
|
403
|
+
index += 1;
|
|
404
|
+
}
|
|
405
|
+
i += 1;
|
|
406
|
+
}
|
|
407
|
+
this.cut(index - 1);
|
|
408
|
+
return this.size;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
deleteByIterator(iter: DequeIterator<E>) {
|
|
412
|
+
const index = iter.index;
|
|
413
|
+
this.deleteAt(index);
|
|
414
|
+
iter = iter.next();
|
|
415
|
+
return iter;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
findIterator(element: E) {
|
|
419
|
+
for (let i = 0; i < this.size; ++i) {
|
|
420
|
+
if (this.getAt(i) === element) {
|
|
421
|
+
return new DequeIterator<E>(i, this);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
return this.end();
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
reverse() {
|
|
428
|
+
this._buckets.reverse().forEach(function (bucket) {
|
|
429
|
+
bucket.reverse();
|
|
430
|
+
});
|
|
431
|
+
const { _bucketFirst, _bucketLast, _firstInBucket, _lastInBucket } = this;
|
|
432
|
+
this._bucketFirst = this._bucketCount - _bucketLast - 1;
|
|
433
|
+
this._bucketLast = this._bucketCount - _bucketFirst - 1;
|
|
434
|
+
this._firstInBucket = this._bucketSize - _lastInBucket - 1;
|
|
435
|
+
this._lastInBucket = this._bucketSize - _firstInBucket - 1;
|
|
436
|
+
return this;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
unique() {
|
|
440
|
+
if (this.size <= 1) {
|
|
441
|
+
return this.size;
|
|
442
|
+
}
|
|
443
|
+
let index = 1;
|
|
444
|
+
let prev = this.getAt(0);
|
|
445
|
+
for (let i = 1; i < this.size; ++i) {
|
|
446
|
+
const cur = this.getAt(i);
|
|
447
|
+
if (cur !== prev) {
|
|
448
|
+
prev = cur;
|
|
449
|
+
this.setAt(index++, cur);
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
this.cut(index - 1);
|
|
453
|
+
return this.size;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
sort(comparator?: (x: E, y: E) => number) {
|
|
457
|
+
const arr: E[] = [];
|
|
458
|
+
for (let i = 0; i < this.size; ++i) {
|
|
459
|
+
arr.push(this.getAt(i));
|
|
460
|
+
}
|
|
461
|
+
arr.sort(comparator);
|
|
462
|
+
for (let i = 0; i < this.size; ++i) {
|
|
463
|
+
this.setAt(i, arr[i]);
|
|
464
|
+
}
|
|
465
|
+
return this;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
shrinkToFit() {
|
|
469
|
+
if (this.size === 0) return;
|
|
470
|
+
const newBuckets = [];
|
|
471
|
+
if (this._bucketFirst === this._bucketLast) return;
|
|
472
|
+
else if (this._bucketFirst < this._bucketLast) {
|
|
473
|
+
for (let i = this._bucketFirst; i <= this._bucketLast; ++i) {
|
|
474
|
+
newBuckets.push(this._buckets[i]);
|
|
475
|
+
}
|
|
476
|
+
} else {
|
|
477
|
+
for (let i = this._bucketFirst; i < this._bucketCount; ++i) {
|
|
478
|
+
newBuckets.push(this._buckets[i]);
|
|
479
|
+
}
|
|
480
|
+
for (let i = 0; i <= this._bucketLast; ++i) {
|
|
481
|
+
newBuckets.push(this._buckets[i]);
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
this._bucketFirst = 0;
|
|
485
|
+
this._bucketLast = newBuckets.length - 1;
|
|
486
|
+
this._buckets = newBuckets;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
forEach(callback: (element: E, index: number, deque: Deque<E>) => void) {
|
|
490
|
+
for (let i = 0; i < this.size; ++i) {
|
|
491
|
+
callback(this.getAt(i), i, this);
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
find(callback: (element: E, index: number, deque: Deque<E>) => boolean): E | undefined {
|
|
496
|
+
for (let i = 0; i < this.size; ++i) {
|
|
497
|
+
const element = this.getAt(i);
|
|
498
|
+
if (callback(element, i, this)) {
|
|
499
|
+
return element;
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
return undefined;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
toArray(): E[] {
|
|
506
|
+
const arr: E[] = [];
|
|
507
|
+
for (let i = 0; i < this.size; ++i) {
|
|
508
|
+
arr.push(this.getAt(i));
|
|
509
|
+
}
|
|
510
|
+
return arr;
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
map<T>(callback: (element: E, index: number, deque: Deque<E>) => T): Deque<T> {
|
|
514
|
+
const newDeque = new Deque<T>([], this._bucketSize);
|
|
515
|
+
for (let i = 0; i < this.size; ++i) {
|
|
516
|
+
newDeque.push(callback(this.getAt(i), i, this));
|
|
517
|
+
}
|
|
518
|
+
return newDeque;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
filter(predicate: (element: E, index: number, deque: Deque<E>) => boolean): Deque<E> {
|
|
522
|
+
const newDeque = new Deque<E>([], this._bucketSize);
|
|
523
|
+
for (let i = 0; i < this.size; ++i) {
|
|
524
|
+
const element = this.getAt(i);
|
|
525
|
+
if (predicate(element, i, this)) {
|
|
526
|
+
newDeque.push(element);
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
return newDeque;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
reduce<T>(callback: (accumulator: T, element: E, index: number, deque: Deque<E>) => T, initialValue: T): T {
|
|
533
|
+
let accumulator = initialValue;
|
|
534
|
+
for (let i = 0; i < this.size; ++i) {
|
|
535
|
+
accumulator = callback(accumulator, this.getAt(i), i, this);
|
|
536
|
+
}
|
|
537
|
+
return accumulator;
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
indexOf(element: E): number {
|
|
541
|
+
for (let i = 0; i < this.size; ++i) {
|
|
542
|
+
if (this.getAt(i) === element) {
|
|
543
|
+
return i;
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
return -1;
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
* [Symbol.iterator]() {
|
|
550
|
+
for (let i = 0; i < this.size; ++i) {
|
|
551
|
+
yield this.getAt(i);
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
protected _reallocate(needBucketNum?: number) {
|
|
556
|
+
const newBuckets = [];
|
|
557
|
+
const addBucketNum = needBucketNum || this._bucketCount >> 1 || 1;
|
|
558
|
+
for (let i = 0; i < addBucketNum; ++i) {
|
|
559
|
+
newBuckets[i] = new Array(this._bucketSize);
|
|
560
|
+
}
|
|
561
|
+
for (let i = this._bucketFirst; i < this._bucketCount; ++i) {
|
|
562
|
+
newBuckets[newBuckets.length] = this._buckets[i];
|
|
563
|
+
}
|
|
564
|
+
for (let i = 0; i < this._bucketLast; ++i) {
|
|
565
|
+
newBuckets[newBuckets.length] = this._buckets[i];
|
|
566
|
+
}
|
|
567
|
+
newBuckets[newBuckets.length] = [...this._buckets[this._bucketLast]];
|
|
568
|
+
this._bucketFirst = addBucketNum;
|
|
569
|
+
this._bucketLast = newBuckets.length - 1;
|
|
570
|
+
for (let i = 0; i < addBucketNum; ++i) {
|
|
571
|
+
newBuckets[newBuckets.length] = new Array(this._bucketSize);
|
|
572
|
+
}
|
|
573
|
+
this._buckets = newBuckets;
|
|
574
|
+
this._bucketCount = newBuckets.length;
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
protected _getBucketAndPosition(pos: number) {
|
|
578
|
+
let bucketIndex: number;
|
|
579
|
+
let indexInBucket: number;
|
|
580
|
+
|
|
581
|
+
const overallIndex = this._firstInBucket + pos;
|
|
582
|
+
bucketIndex = this._bucketFirst + Math.floor(overallIndex / this._bucketSize);
|
|
583
|
+
|
|
584
|
+
if (bucketIndex >= this._bucketCount) {
|
|
585
|
+
bucketIndex -= this._bucketCount;
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
indexInBucket = (overallIndex + 1) % this._bucketSize - 1;
|
|
589
|
+
if (indexInBucket < 0) {
|
|
590
|
+
indexInBucket = this._bucketSize - 1;
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
return { bucketIndex, indexInBucket };
|
|
594
|
+
}
|
|
13
595
|
}
|
|
14
596
|
|
|
15
|
-
// O(1) time complexity of obtaining the
|
|
597
|
+
// O(1) time complexity of obtaining the element
|
|
16
598
|
// O(n) time complexity of adding at the beginning and the end
|
|
17
599
|
// todo tested slowest one
|
|
18
600
|
export class ObjectDeque<E = number> {
|
|
@@ -59,11 +641,11 @@ export class ObjectDeque<E = number> {
|
|
|
59
641
|
* Time Complexity: O(1)
|
|
60
642
|
* Space Complexity: O(1)
|
|
61
643
|
*
|
|
62
|
-
* The "addFirst" function adds
|
|
63
|
-
* @param {E}
|
|
644
|
+
* The "addFirst" function adds an element to the beginning of an array-like data structure.
|
|
645
|
+
* @param {E} element - The `element` parameter represents the element that you want to add to the beginning of the data
|
|
64
646
|
* structure.
|
|
65
647
|
*/
|
|
66
|
-
addFirst(
|
|
648
|
+
addFirst(element: E) {
|
|
67
649
|
if (this.size === 0) {
|
|
68
650
|
const mid = Math.floor(this.capacity / 2);
|
|
69
651
|
this._first = mid;
|
|
@@ -71,7 +653,7 @@ export class ObjectDeque<E = number> {
|
|
|
71
653
|
} else {
|
|
72
654
|
this._first--;
|
|
73
655
|
}
|
|
74
|
-
this.nodes[this.first] =
|
|
656
|
+
this.nodes[this.first] = element;
|
|
75
657
|
this._size++;
|
|
76
658
|
}
|
|
77
659
|
|
|
@@ -84,10 +666,10 @@ export class ObjectDeque<E = number> {
|
|
|
84
666
|
* Time Complexity: O(1)
|
|
85
667
|
* Space Complexity: O(1)
|
|
86
668
|
*
|
|
87
|
-
* The addLast function adds
|
|
88
|
-
* @param {E}
|
|
669
|
+
* The addLast function adds an element to the end of an array-like data structure.
|
|
670
|
+
* @param {E} element - The `element` parameter represents the element that you want to add to the end of the data structure.
|
|
89
671
|
*/
|
|
90
|
-
addLast(
|
|
672
|
+
addLast(element: E) {
|
|
91
673
|
if (this.size === 0) {
|
|
92
674
|
const mid = Math.floor(this.capacity / 2);
|
|
93
675
|
this._first = mid;
|
|
@@ -95,7 +677,7 @@ export class ObjectDeque<E = number> {
|
|
|
95
677
|
} else {
|
|
96
678
|
this._last++;
|
|
97
679
|
}
|
|
98
|
-
this.nodes[this.last] =
|
|
680
|
+
this.nodes[this.last] = element;
|
|
99
681
|
this._size++;
|
|
100
682
|
}
|
|
101
683
|
|
|
@@ -109,15 +691,15 @@ export class ObjectDeque<E = number> {
|
|
|
109
691
|
* Space Complexity: O(1)
|
|
110
692
|
*
|
|
111
693
|
* The function `popFirst()` removes and returns the first element in a data structure.
|
|
112
|
-
* @returns The
|
|
694
|
+
* @returns The element of the first element in the data structure.
|
|
113
695
|
*/
|
|
114
696
|
popFirst() {
|
|
115
697
|
if (!this.size) return;
|
|
116
|
-
const
|
|
698
|
+
const element = this.getFirst();
|
|
117
699
|
delete this.nodes[this.first];
|
|
118
700
|
this._first++;
|
|
119
701
|
this._size--;
|
|
120
|
-
return
|
|
702
|
+
return element;
|
|
121
703
|
}
|
|
122
704
|
|
|
123
705
|
/**
|
|
@@ -146,16 +728,16 @@ export class ObjectDeque<E = number> {
|
|
|
146
728
|
* Space Complexity: O(1)
|
|
147
729
|
*
|
|
148
730
|
* The `popLast()` function removes and returns the last element in a data structure.
|
|
149
|
-
* @returns The
|
|
731
|
+
* @returns The element that was removed from the data structure.
|
|
150
732
|
*/
|
|
151
733
|
popLast() {
|
|
152
734
|
if (!this.size) return;
|
|
153
|
-
const
|
|
735
|
+
const element = this.getLast();
|
|
154
736
|
delete this.nodes[this.last];
|
|
155
737
|
this._last--;
|
|
156
738
|
this._size--;
|
|
157
739
|
|
|
158
|
-
return
|
|
740
|
+
return element;
|
|
159
741
|
}
|
|
160
742
|
|
|
161
743
|
/**
|
|
@@ -187,220 +769,17 @@ export class ObjectDeque<E = number> {
|
|
|
187
769
|
* @param {number} index - The index parameter is a number that represents the position of the element you want to
|
|
188
770
|
* retrieve from the array.
|
|
189
771
|
* @returns The element at the specified index in the `_nodes` array is being returned. If there is no element at that
|
|
190
|
-
* index, `
|
|
772
|
+
* index, `undefined` is returned.
|
|
191
773
|
*/
|
|
192
774
|
get(index: number) {
|
|
193
|
-
return this.nodes[this.first + index] ||
|
|
775
|
+
return this.nodes[this.first + index] || undefined;
|
|
194
776
|
}
|
|
195
777
|
|
|
196
778
|
/**
|
|
197
779
|
* The function checks if the size of a data structure is less than or equal to zero.
|
|
198
|
-
* @returns The method is returning a boolean
|
|
780
|
+
* @returns The method is returning a boolean element indicating whether the size of the object is less than or equal to 0.
|
|
199
781
|
*/
|
|
200
782
|
isEmpty() {
|
|
201
783
|
return this.size <= 0;
|
|
202
784
|
}
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
// O(1) time complexity of obtaining the value
|
|
206
|
-
// O(n) time complexity of adding at the beginning and the end
|
|
207
|
-
export class ArrayDeque<E> {
|
|
208
|
-
protected _nodes: E[] = [];
|
|
209
|
-
|
|
210
|
-
get nodes(): E[] {
|
|
211
|
-
return this._nodes;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
get size() {
|
|
215
|
-
return this.nodes.length;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
/**
|
|
219
|
-
* Time Complexity: O(1)
|
|
220
|
-
* Space Complexity: O(1)
|
|
221
|
-
*/
|
|
222
|
-
|
|
223
|
-
/**
|
|
224
|
-
* Time Complexity: O(1)
|
|
225
|
-
* Space Complexity: O(1)
|
|
226
|
-
*
|
|
227
|
-
* The function "addLast" adds a value to the end of an array.
|
|
228
|
-
* @param {E} value - The value parameter represents the value that you want to add to the end of the array.
|
|
229
|
-
* @returns The return value is the new length of the array after the value has been added.
|
|
230
|
-
*/
|
|
231
|
-
addLast(value: E) {
|
|
232
|
-
return this.nodes.push(value);
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
/**
|
|
236
|
-
* Time Complexity: O(1)
|
|
237
|
-
* Space Complexity: O(1)
|
|
238
|
-
*/
|
|
239
|
-
|
|
240
|
-
/**
|
|
241
|
-
* Time Complexity: O(1)
|
|
242
|
-
* Space Complexity: O(1)
|
|
243
|
-
*
|
|
244
|
-
* The function "popLast" returns and removes the last element from an array, or returns null if the array is empty.
|
|
245
|
-
* @returns The method `popLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
246
|
-
*/
|
|
247
|
-
popLast(): E | null {
|
|
248
|
-
return this.nodes.pop() ?? null;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
/**
|
|
252
|
-
* Time Complexity: O(n)
|
|
253
|
-
* Space Complexity: O(1)
|
|
254
|
-
*/
|
|
255
|
-
|
|
256
|
-
/**
|
|
257
|
-
* Time Complexity: O(n)
|
|
258
|
-
* Space Complexity: O(1)
|
|
259
|
-
*
|
|
260
|
-
* The `popFirst` function removes and returns the first element from an array, or returns null if the array is empty.
|
|
261
|
-
* @returns The `popFirst()` function returns the first element of the `_nodes` array, or `null` if the array is
|
|
262
|
-
* empty.
|
|
263
|
-
*/
|
|
264
|
-
popFirst(): E | null {
|
|
265
|
-
return this.nodes.shift() ?? null;
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
/**
|
|
269
|
-
* Time Complexity: O(n)
|
|
270
|
-
* Space Complexity: O(1)
|
|
271
|
-
*/
|
|
272
|
-
|
|
273
|
-
/**
|
|
274
|
-
* Time Complexity: O(n)
|
|
275
|
-
* Space Complexity: O(1)
|
|
276
|
-
*
|
|
277
|
-
* The function "addFirst" adds a value to the beginning of an array.
|
|
278
|
-
* @param {E} value - The value parameter represents the value that you want to add to the beginning of the array.
|
|
279
|
-
* @returns The return value of the `addFirst` function is the new length of the array `_nodes` after adding the
|
|
280
|
-
* `value` at the beginning.
|
|
281
|
-
*/
|
|
282
|
-
addFirst(value: E) {
|
|
283
|
-
return this.nodes.unshift(value);
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
/**
|
|
287
|
-
* Time Complexity: O(1)
|
|
288
|
-
* Space Complexity: O(1)
|
|
289
|
-
*/
|
|
290
|
-
|
|
291
|
-
/**
|
|
292
|
-
* Time Complexity: O(1)
|
|
293
|
-
* Space Complexity: O(1)
|
|
294
|
-
*
|
|
295
|
-
* The `getFirst` function returns the first element of an array or null if the array is empty.
|
|
296
|
-
* @returns The function `getFirst()` is returning the first element (`E`) of the `_nodes` array. If the array is
|
|
297
|
-
* empty, it will return `null`.
|
|
298
|
-
*/
|
|
299
|
-
getFirst(): E | null {
|
|
300
|
-
return this.nodes[0] ?? null;
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
/**
|
|
304
|
-
* Time Complexity: O(1)
|
|
305
|
-
* Space Complexity: O(1)
|
|
306
|
-
*/
|
|
307
|
-
|
|
308
|
-
/**
|
|
309
|
-
* Time Complexity: O(1)
|
|
310
|
-
* Space Complexity: O(1)
|
|
311
|
-
*
|
|
312
|
-
* The `getLast` function returns the last element of an array or null if the array is empty.
|
|
313
|
-
* @returns The method `getLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
314
|
-
*/
|
|
315
|
-
getLast(): E | null {
|
|
316
|
-
return this.nodes[this.nodes.length - 1] ?? null;
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
/**
|
|
320
|
-
* Time Complexity: O(1)
|
|
321
|
-
* Space Complexity: O(1)
|
|
322
|
-
*/
|
|
323
|
-
|
|
324
|
-
/**
|
|
325
|
-
* Time Complexity: O(1)
|
|
326
|
-
* Space Complexity: O(1)
|
|
327
|
-
*
|
|
328
|
-
* The get function returns the element at the specified index in an array, or null if the index is out of bounds.
|
|
329
|
-
* @param {number} index - The index parameter is a number that represents the position of the element you want to
|
|
330
|
-
* retrieve from the array.
|
|
331
|
-
* @returns The method is returning the element at the specified index in the `_nodes` array. If the element exists, it
|
|
332
|
-
* will be returned. If the element does not exist (i.e., the index is out of bounds), `null` will be returned.
|
|
333
|
-
*/
|
|
334
|
-
get(index: number): E | null {
|
|
335
|
-
return this.nodes[index] ?? null;
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
/**
|
|
339
|
-
* Time Complexity: O(1)
|
|
340
|
-
* Space Complexity: O(1)
|
|
341
|
-
*/
|
|
342
|
-
|
|
343
|
-
/**
|
|
344
|
-
* Time Complexity: O(1)
|
|
345
|
-
* Space Complexity: O(1)
|
|
346
|
-
*
|
|
347
|
-
* The set function assigns a value to a specific index in an array.
|
|
348
|
-
* @param {number} index - The index parameter is a number that represents the position of the element in the array
|
|
349
|
-
* that you want to set a new value for.
|
|
350
|
-
* @param {E} value - The value parameter represents the new value that you want to set at the specified index in the
|
|
351
|
-
* _nodes array.
|
|
352
|
-
* @returns The value that is being set at the specified index in the `_nodes` array.
|
|
353
|
-
*/
|
|
354
|
-
set(index: number, value: E) {
|
|
355
|
-
return (this.nodes[index] = value);
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
/**
|
|
359
|
-
* Time Complexity: O(n)
|
|
360
|
-
* Space Complexity: O(1)
|
|
361
|
-
*/
|
|
362
|
-
|
|
363
|
-
/**
|
|
364
|
-
* Time Complexity: O(n)
|
|
365
|
-
* Space Complexity: O(1)
|
|
366
|
-
*
|
|
367
|
-
* The insert function adds a value at a specified index in an array.
|
|
368
|
-
* @param {number} index - The index parameter specifies the position at which the value should be inserted in the
|
|
369
|
-
* array. It is a number that represents the index of the array where the value should be inserted. The index starts
|
|
370
|
-
* from 0, so the first element of the array has an index of 0, the second element has
|
|
371
|
-
* @param {E} value - The value parameter represents the value that you want to insert into the array at the specified
|
|
372
|
-
* index.
|
|
373
|
-
* @returns The splice method returns an array containing the removed elements, if any. In this case, since no elements
|
|
374
|
-
* are being removed, an empty array will be returned.
|
|
375
|
-
*/
|
|
376
|
-
insert(index: number, value: E) {
|
|
377
|
-
return this.nodes.splice(index, 0, value);
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
/**
|
|
381
|
-
* Time Complexity: O(n)
|
|
382
|
-
* Space Complexity: O(1)
|
|
383
|
-
*/
|
|
384
|
-
|
|
385
|
-
/**
|
|
386
|
-
* Time Complexity: O(n)
|
|
387
|
-
* Space Complexity: O(1)
|
|
388
|
-
*
|
|
389
|
-
* The delete function removes an element from an array at a specified index.
|
|
390
|
-
* @param {number} index - The index parameter specifies the position of the element to be removed from the array. It
|
|
391
|
-
* is a number that represents the index of the element to be removed.
|
|
392
|
-
* @returns The method is returning an array containing the removed element.
|
|
393
|
-
*/
|
|
394
|
-
delete(index: number) {
|
|
395
|
-
return this.nodes.splice(index, 1);
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
/**
|
|
399
|
-
* The function checks if an array called "_nodes" is empty.
|
|
400
|
-
* @returns The method `isEmpty()` is returning a boolean value. It returns `true` if the length of the `_nodes` array
|
|
401
|
-
* is 0, indicating that the array is empty. Otherwise, it returns `false`.
|
|
402
|
-
*/
|
|
403
|
-
isEmpty() {
|
|
404
|
-
return this.nodes.length === 0;
|
|
405
|
-
}
|
|
406
|
-
}
|
|
785
|
+
}
|