data-structure-typed 1.49.1 → 1.49.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/CHANGELOG.md +1 -1
- package/README.md +16 -16
- package/README_zh-CN.md +2 -2
- package/benchmark/report.html +46 -1
- package/benchmark/report.json +457 -22
- package/dist/cjs/data-structures/base/iterable-base.d.ts +11 -0
- package/dist/cjs/data-structures/base/iterable-base.js +21 -0
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +7 -7
- package/dist/cjs/data-structures/graph/abstract-graph.js +43 -12
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +2 -2
- package/dist/cjs/data-structures/graph/directed-graph.js +2 -2
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.js +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +9 -9
- package/dist/cjs/data-structures/hash/hash-map.js +16 -15
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +6 -35
- package/dist/cjs/data-structures/heap/heap.js +10 -42
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +99 -105
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +143 -146
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +16 -21
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +42 -42
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +25 -25
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js +36 -36
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +70 -75
- package/dist/cjs/data-structures/queue/deque.js +100 -110
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +37 -38
- package/dist/cjs/data-structures/queue/queue.js +46 -49
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +2 -3
- package/dist/cjs/data-structures/stack/stack.js +2 -5
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +1 -2
- package/dist/cjs/data-structures/trie/trie.js +2 -5
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/mjs/data-structures/base/iterable-base.d.ts +11 -0
- package/dist/mjs/data-structures/base/iterable-base.js +21 -0
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +7 -7
- package/dist/mjs/data-structures/graph/abstract-graph.js +43 -12
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +2 -2
- package/dist/mjs/data-structures/graph/directed-graph.js +2 -2
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +1 -1
- package/dist/mjs/data-structures/graph/undirected-graph.js +1 -1
- package/dist/mjs/data-structures/hash/hash-map.d.ts +9 -9
- package/dist/mjs/data-structures/hash/hash-map.js +16 -15
- package/dist/mjs/data-structures/heap/heap.d.ts +6 -35
- package/dist/mjs/data-structures/heap/heap.js +10 -42
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +99 -105
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +142 -145
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +16 -21
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +43 -43
- package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +25 -25
- package/dist/mjs/data-structures/linked-list/skip-linked-list.js +36 -36
- package/dist/mjs/data-structures/queue/deque.d.ts +70 -75
- package/dist/mjs/data-structures/queue/deque.js +100 -110
- package/dist/mjs/data-structures/queue/queue.d.ts +37 -38
- package/dist/mjs/data-structures/queue/queue.js +45 -48
- package/dist/mjs/data-structures/stack/stack.d.ts +2 -3
- package/dist/mjs/data-structures/stack/stack.js +2 -5
- package/dist/mjs/data-structures/trie/trie.d.ts +1 -2
- package/dist/mjs/data-structures/trie/trie.js +2 -5
- package/dist/umd/data-structure-typed.js +531 -532
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/base/iterable-base.ts +24 -0
- package/src/data-structures/graph/abstract-graph.ts +55 -14
- package/src/data-structures/graph/directed-graph.ts +3 -2
- package/src/data-structures/graph/undirected-graph.ts +1 -1
- package/src/data-structures/hash/hash-map.ts +27 -28
- package/src/data-structures/heap/heap.ts +19 -57
- package/src/data-structures/linked-list/doubly-linked-list.ts +157 -161
- package/src/data-structures/linked-list/singly-linked-list.ts +49 -49
- package/src/data-structures/linked-list/skip-linked-list.ts +40 -40
- package/src/data-structures/queue/deque.ts +122 -135
- package/src/data-structures/queue/queue.ts +54 -58
- package/src/data-structures/stack/stack.ts +4 -8
- package/src/data-structures/trie/trie.ts +5 -9
- package/test/performance/data-structures/comparison/comparison.test.ts +6 -6
- package/test/performance/data-structures/heap/heap.test.ts +2 -2
- package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +2 -2
- package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +2 -2
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +2 -2
- package/test/unit/data-structures/graph/abstract-graph.test.ts +1 -1
- package/test/unit/data-structures/graph/directed-graph.test.ts +48 -3
- package/test/unit/data-structures/graph/undirected-graph.test.ts +48 -4
- package/test/unit/data-structures/hash/hash-map.test.ts +2 -2
- package/test/unit/data-structures/heap/heap.test.ts +6 -1
- package/test/unit/data-structures/heap/min-heap.test.ts +1 -1
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +30 -30
- package/test/unit/data-structures/linked-list/linked-list.test.ts +1 -1
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +21 -21
- package/test/unit/data-structures/linked-list/skip-list.test.ts +2 -2
- package/test/unit/data-structures/queue/deque.test.ts +5 -5
- package/test/unit/data-structures/queue/queue.test.ts +4 -4
- package/test/unit/data-structures/trie/trie.test.ts +1 -1
|
@@ -42,68 +42,6 @@ export declare class Deque<E> extends IterableElementBase<E> {
|
|
|
42
42
|
*/
|
|
43
43
|
get first(): E | undefined;
|
|
44
44
|
get last(): E | undefined;
|
|
45
|
-
/**
|
|
46
|
-
* Time Complexity: O(1) - Removes the last element.
|
|
47
|
-
* Space Complexity: O(1) - Operates in-place.
|
|
48
|
-
*/
|
|
49
|
-
isEmpty(): boolean;
|
|
50
|
-
/**
|
|
51
|
-
* Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
|
|
52
|
-
* Space Complexity: O(n) - Due to potential resizing.
|
|
53
|
-
*/
|
|
54
|
-
/**
|
|
55
|
-
* Time Complexity: O(1)
|
|
56
|
-
* Space Complexity: O(n) - In worst case, resizing doubles the array size.
|
|
57
|
-
*
|
|
58
|
-
* The addLast function adds an element to the end of an array.
|
|
59
|
-
* @param {E} element - The element parameter represents the element that you want to add to the end of the
|
|
60
|
-
* data structure.
|
|
61
|
-
*/
|
|
62
|
-
addLast(element: E): void;
|
|
63
|
-
/**
|
|
64
|
-
* Time Complexity: O(1) - Removes the first element.
|
|
65
|
-
* Space Complexity: O(1) - In-place operation.
|
|
66
|
-
*/
|
|
67
|
-
/**
|
|
68
|
-
* Time Complexity: O(1) - Removes the last element.
|
|
69
|
-
* Space Complexity: O(1) - Operates in-place.
|
|
70
|
-
*
|
|
71
|
-
* The function "pollLast" removes and returns the last element of an array.
|
|
72
|
-
* @returns The last element of the array is being returned.
|
|
73
|
-
*/
|
|
74
|
-
pollLast(): E | undefined;
|
|
75
|
-
/**
|
|
76
|
-
* Time Complexity: O(1).
|
|
77
|
-
* Space Complexity: O(n) - Due to potential resizing.
|
|
78
|
-
*
|
|
79
|
-
* The "addFirst" function adds an element to the beginning of an array.
|
|
80
|
-
* @param {E} element - The parameter "element" represents the element that you want to add to the
|
|
81
|
-
* beginning of the data structure.
|
|
82
|
-
*/
|
|
83
|
-
addFirst(element: E): void;
|
|
84
|
-
/**
|
|
85
|
-
* Time Complexity: O(1) - Removes the first element.
|
|
86
|
-
* Space Complexity: O(1) - In-place operation.
|
|
87
|
-
*
|
|
88
|
-
* The function "pollFirst" removes and returns the first element of an array.
|
|
89
|
-
* @returns The method `pollFirst()` is returning the first element of the array after removing it
|
|
90
|
-
* from the beginning. If the array is empty, it will return `undefined`.
|
|
91
|
-
*/
|
|
92
|
-
pollFirst(): E | undefined;
|
|
93
|
-
/**
|
|
94
|
-
* The clear() function resets the state of the object by initializing all variables to their default
|
|
95
|
-
* values.
|
|
96
|
-
*/
|
|
97
|
-
clear(): void;
|
|
98
|
-
/**
|
|
99
|
-
* The below function is a generator that yields elements from a collection one by one.
|
|
100
|
-
*/
|
|
101
|
-
begin(): Generator<E>;
|
|
102
|
-
/**
|
|
103
|
-
* The function `reverseBegin()` is a generator that yields elements in reverse order starting from
|
|
104
|
-
* the last element.
|
|
105
|
-
*/
|
|
106
|
-
reverseBegin(): Generator<E>;
|
|
107
45
|
/**
|
|
108
46
|
* Time Complexity - Amortized O(1) (possible reallocation)
|
|
109
47
|
* Space Complexity - O(n) (due to potential resizing).
|
|
@@ -117,7 +55,7 @@ export declare class Deque<E> extends IterableElementBase<E> {
|
|
|
117
55
|
* structure.
|
|
118
56
|
* @returns The size of the data structure after the element has been pushed.
|
|
119
57
|
*/
|
|
120
|
-
push(element: E):
|
|
58
|
+
push(element: E): boolean;
|
|
121
59
|
/**
|
|
122
60
|
* Time Complexity: O(1)
|
|
123
61
|
* Space Complexity: O(1)
|
|
@@ -145,7 +83,7 @@ export declare class Deque<E> extends IterableElementBase<E> {
|
|
|
145
83
|
* beginning of the data structure.
|
|
146
84
|
* @returns The size of the data structure after the element has been added.
|
|
147
85
|
*/
|
|
148
|
-
unshift(element: E):
|
|
86
|
+
unshift(element: E): boolean;
|
|
149
87
|
/**
|
|
150
88
|
* Time Complexity: O(1)
|
|
151
89
|
* Space Complexity: O(1)
|
|
@@ -160,6 +98,25 @@ export declare class Deque<E> extends IterableElementBase<E> {
|
|
|
160
98
|
* returned.
|
|
161
99
|
*/
|
|
162
100
|
shift(): E | undefined;
|
|
101
|
+
/**
|
|
102
|
+
* Time Complexity: O(1) - Removes the last element.
|
|
103
|
+
* Space Complexity: O(1) - Operates in-place.
|
|
104
|
+
*/
|
|
105
|
+
isEmpty(): boolean;
|
|
106
|
+
/**
|
|
107
|
+
* The clear() function resets the state of the object by initializing all variables to their default
|
|
108
|
+
* values.
|
|
109
|
+
*/
|
|
110
|
+
clear(): void;
|
|
111
|
+
/**
|
|
112
|
+
* The below function is a generator that yields elements from a collection one by one.
|
|
113
|
+
*/
|
|
114
|
+
begin(): Generator<E>;
|
|
115
|
+
/**
|
|
116
|
+
* The function `reverseBegin()` is a generator that yields elements in reverse order starting from
|
|
117
|
+
* the last element.
|
|
118
|
+
*/
|
|
119
|
+
reverseBegin(): Generator<E>;
|
|
163
120
|
/**
|
|
164
121
|
* Time Complexity: O(1)
|
|
165
122
|
* Space Complexity: O(1)
|
|
@@ -189,7 +146,7 @@ export declare class Deque<E> extends IterableElementBase<E> {
|
|
|
189
146
|
* @param {E} element - The `element` parameter is the value that you want to set at the specified
|
|
190
147
|
* position in the data structure.
|
|
191
148
|
*/
|
|
192
|
-
setAt(pos: number, element: E):
|
|
149
|
+
setAt(pos: number, element: E): boolean;
|
|
193
150
|
/**
|
|
194
151
|
* Time Complexity: O(n)
|
|
195
152
|
* Space Complexity: O(n)
|
|
@@ -198,7 +155,7 @@ export declare class Deque<E> extends IterableElementBase<E> {
|
|
|
198
155
|
* Time Complexity: O(n)
|
|
199
156
|
* Space Complexity: O(n)
|
|
200
157
|
*
|
|
201
|
-
* The `
|
|
158
|
+
* The `addAt` function inserts one or more elements at a specified position in an array-like data
|
|
202
159
|
* structure.
|
|
203
160
|
* @param {number} pos - The `pos` parameter represents the position at which the element(s) should
|
|
204
161
|
* be inserted. It is of type `number`.
|
|
@@ -209,7 +166,7 @@ export declare class Deque<E> extends IterableElementBase<E> {
|
|
|
209
166
|
* will be inserted once. However, you can provide a different value for `num` if you want
|
|
210
167
|
* @returns The size of the array after the insertion is being returned.
|
|
211
168
|
*/
|
|
212
|
-
|
|
169
|
+
addAt(pos: number, element: E, num?: number): boolean;
|
|
213
170
|
/**
|
|
214
171
|
* Time Complexity: O(1)
|
|
215
172
|
* Space Complexity: O(1)
|
|
@@ -240,7 +197,7 @@ export declare class Deque<E> extends IterableElementBase<E> {
|
|
|
240
197
|
* the index of the element to be deleted.
|
|
241
198
|
* @returns The size of the data structure after the deletion operation is performed.
|
|
242
199
|
*/
|
|
243
|
-
deleteAt(pos: number):
|
|
200
|
+
deleteAt(pos: number): boolean;
|
|
244
201
|
/**
|
|
245
202
|
* Time Complexity: O(n)
|
|
246
203
|
* Space Complexity: O(1)
|
|
@@ -255,7 +212,7 @@ export declare class Deque<E> extends IterableElementBase<E> {
|
|
|
255
212
|
* the data structure.
|
|
256
213
|
* @returns The size of the data structure after the element has been deleted.
|
|
257
214
|
*/
|
|
258
|
-
delete(element: E):
|
|
215
|
+
delete(element: E): boolean;
|
|
259
216
|
/**
|
|
260
217
|
* Time Complexity: O(n)
|
|
261
218
|
* Space Complexity: O(1)
|
|
@@ -282,7 +239,7 @@ export declare class Deque<E> extends IterableElementBase<E> {
|
|
|
282
239
|
* the number of unique elements.
|
|
283
240
|
* @returns The size of the modified array is being returned.
|
|
284
241
|
*/
|
|
285
|
-
unique():
|
|
242
|
+
unique(): this;
|
|
286
243
|
/**
|
|
287
244
|
* Time Complexity: O(n log n)
|
|
288
245
|
* Space Complexity: O(n)
|
|
@@ -295,7 +252,7 @@ export declare class Deque<E> extends IterableElementBase<E> {
|
|
|
295
252
|
* @param [comparator] - The `comparator` parameter is a function that takes in two elements `x` and
|
|
296
253
|
* `y` of type `E` and returns a number. The comparator function is used to determine the order of
|
|
297
254
|
* the elements in the sorted array.
|
|
298
|
-
* @returns
|
|
255
|
+
* @returns Deque<E>
|
|
299
256
|
*/
|
|
300
257
|
sort(comparator?: (x: E, y: E) => number): this;
|
|
301
258
|
/**
|
|
@@ -396,10 +353,48 @@ export declare class Deque<E> extends IterableElementBase<E> {
|
|
|
396
353
|
*/
|
|
397
354
|
map<T>(callback: ElementCallback<E, T>, thisArg?: any): Deque<T>;
|
|
398
355
|
/**
|
|
399
|
-
* Time Complexity: O(n)
|
|
400
|
-
* Space Complexity: O(n)
|
|
356
|
+
* Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
|
|
357
|
+
* Space Complexity: O(n) - Due to potential resizing.
|
|
358
|
+
*/
|
|
359
|
+
/**
|
|
360
|
+
* Time Complexity: O(1)
|
|
361
|
+
* Space Complexity: O(n) - In worst case, resizing doubles the array size.
|
|
362
|
+
*
|
|
363
|
+
* The addLast function adds an element to the end of an array.
|
|
364
|
+
* @param {E} element - The element parameter represents the element that you want to add to the end of the
|
|
365
|
+
* data structure.
|
|
366
|
+
*/
|
|
367
|
+
addLast(element: E): boolean;
|
|
368
|
+
/**
|
|
369
|
+
* Time Complexity: O(1) - Removes the first element.
|
|
370
|
+
* Space Complexity: O(1) - In-place operation.
|
|
371
|
+
*/
|
|
372
|
+
/**
|
|
373
|
+
* Time Complexity: O(1) - Removes the last element.
|
|
374
|
+
* Space Complexity: O(1) - Operates in-place.
|
|
375
|
+
*
|
|
376
|
+
* The function "pollLast" removes and returns the last element of an array.
|
|
377
|
+
* @returns The last element of the array is being returned.
|
|
378
|
+
*/
|
|
379
|
+
pollLast(): E | undefined;
|
|
380
|
+
/**
|
|
381
|
+
* Time Complexity: O(1).
|
|
382
|
+
* Space Complexity: O(n) - Due to potential resizing.
|
|
383
|
+
*
|
|
384
|
+
* The "addFirst" function adds an element to the beginning of an array.
|
|
385
|
+
* @param {E} element - The parameter "element" represents the element that you want to add to the
|
|
386
|
+
* beginning of the data structure.
|
|
401
387
|
*/
|
|
402
|
-
|
|
388
|
+
addFirst(element: E): boolean;
|
|
389
|
+
/**
|
|
390
|
+
* Time Complexity: O(1) - Removes the first element.
|
|
391
|
+
* Space Complexity: O(1) - In-place operation.
|
|
392
|
+
*
|
|
393
|
+
* The function "pollFirst" removes and returns the first element of an array.
|
|
394
|
+
* @returns The method `pollFirst()` is returning the first element of the array after removing it
|
|
395
|
+
* from the beginning. If the array is empty, it will return `undefined`.
|
|
396
|
+
*/
|
|
397
|
+
pollFirst(): E | undefined;
|
|
403
398
|
/**
|
|
404
399
|
* Time Complexity: O(n)
|
|
405
400
|
* Space Complexity: O(1)
|
|
@@ -407,7 +402,7 @@ export declare class Deque<E> extends IterableElementBase<E> {
|
|
|
407
402
|
* The above function is an implementation of the iterator protocol in TypeScript, allowing the
|
|
408
403
|
* object to be iterated over using a for...of loop.
|
|
409
404
|
*/
|
|
410
|
-
protected _getIterator():
|
|
405
|
+
protected _getIterator(): IterableIterator<E>;
|
|
411
406
|
/**
|
|
412
407
|
* Time Complexity: O(n)
|
|
413
408
|
* Space Complexity: O(n)
|
|
@@ -73,95 +73,6 @@ export class Deque extends IterableElementBase {
|
|
|
73
73
|
return;
|
|
74
74
|
return this._buckets[this._bucketLast][this._lastInBucket];
|
|
75
75
|
}
|
|
76
|
-
/**
|
|
77
|
-
* Time Complexity: O(1) - Removes the last element.
|
|
78
|
-
* Space Complexity: O(1) - Operates in-place.
|
|
79
|
-
*/
|
|
80
|
-
isEmpty() {
|
|
81
|
-
return this.size === 0;
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
|
|
85
|
-
* Space Complexity: O(n) - Due to potential resizing.
|
|
86
|
-
*/
|
|
87
|
-
/**
|
|
88
|
-
* Time Complexity: O(1)
|
|
89
|
-
* Space Complexity: O(n) - In worst case, resizing doubles the array size.
|
|
90
|
-
*
|
|
91
|
-
* The addLast function adds an element to the end of an array.
|
|
92
|
-
* @param {E} element - The element parameter represents the element that you want to add to the end of the
|
|
93
|
-
* data structure.
|
|
94
|
-
*/
|
|
95
|
-
addLast(element) {
|
|
96
|
-
this.push(element);
|
|
97
|
-
}
|
|
98
|
-
/**
|
|
99
|
-
* Time Complexity: O(1) - Removes the first element.
|
|
100
|
-
* Space Complexity: O(1) - In-place operation.
|
|
101
|
-
*/
|
|
102
|
-
/**
|
|
103
|
-
* Time Complexity: O(1) - Removes the last element.
|
|
104
|
-
* Space Complexity: O(1) - Operates in-place.
|
|
105
|
-
*
|
|
106
|
-
* The function "pollLast" removes and returns the last element of an array.
|
|
107
|
-
* @returns The last element of the array is being returned.
|
|
108
|
-
*/
|
|
109
|
-
pollLast() {
|
|
110
|
-
return this.pop();
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Time Complexity: O(1).
|
|
114
|
-
* Space Complexity: O(n) - Due to potential resizing.
|
|
115
|
-
*
|
|
116
|
-
* The "addFirst" function adds an element to the beginning of an array.
|
|
117
|
-
* @param {E} element - The parameter "element" represents the element that you want to add to the
|
|
118
|
-
* beginning of the data structure.
|
|
119
|
-
*/
|
|
120
|
-
addFirst(element) {
|
|
121
|
-
this.unshift(element);
|
|
122
|
-
}
|
|
123
|
-
/**
|
|
124
|
-
* Time Complexity: O(1) - Removes the first element.
|
|
125
|
-
* Space Complexity: O(1) - In-place operation.
|
|
126
|
-
*
|
|
127
|
-
* The function "pollFirst" removes and returns the first element of an array.
|
|
128
|
-
* @returns The method `pollFirst()` is returning the first element of the array after removing it
|
|
129
|
-
* from the beginning. If the array is empty, it will return `undefined`.
|
|
130
|
-
*/
|
|
131
|
-
pollFirst() {
|
|
132
|
-
return this.shift();
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* The clear() function resets the state of the object by initializing all variables to their default
|
|
136
|
-
* values.
|
|
137
|
-
*/
|
|
138
|
-
clear() {
|
|
139
|
-
this._buckets = [new Array(this._bucketSize)];
|
|
140
|
-
this._bucketCount = 1;
|
|
141
|
-
this._bucketFirst = this._bucketLast = this._size = 0;
|
|
142
|
-
this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
|
|
143
|
-
}
|
|
144
|
-
/**
|
|
145
|
-
* The below function is a generator that yields elements from a collection one by one.
|
|
146
|
-
*/
|
|
147
|
-
*begin() {
|
|
148
|
-
let index = 0;
|
|
149
|
-
while (index < this.size) {
|
|
150
|
-
yield this.getAt(index);
|
|
151
|
-
index++;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
/**
|
|
155
|
-
* The function `reverseBegin()` is a generator that yields elements in reverse order starting from
|
|
156
|
-
* the last element.
|
|
157
|
-
*/
|
|
158
|
-
*reverseBegin() {
|
|
159
|
-
let index = this.size - 1;
|
|
160
|
-
while (index >= 0) {
|
|
161
|
-
yield this.getAt(index);
|
|
162
|
-
index--;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
76
|
/**
|
|
166
77
|
* Time Complexity - Amortized O(1) (possible reallocation)
|
|
167
78
|
* Space Complexity - O(n) (due to potential resizing).
|
|
@@ -194,7 +105,7 @@ export class Deque extends IterableElementBase {
|
|
|
194
105
|
}
|
|
195
106
|
this._size += 1;
|
|
196
107
|
this._buckets[this._bucketLast][this._lastInBucket] = element;
|
|
197
|
-
return
|
|
108
|
+
return true;
|
|
198
109
|
}
|
|
199
110
|
/**
|
|
200
111
|
* Time Complexity: O(1)
|
|
@@ -261,7 +172,7 @@ export class Deque extends IterableElementBase {
|
|
|
261
172
|
}
|
|
262
173
|
this._size += 1;
|
|
263
174
|
this._buckets[this._bucketFirst][this._firstInBucket] = element;
|
|
264
|
-
return
|
|
175
|
+
return true;
|
|
265
176
|
}
|
|
266
177
|
/**
|
|
267
178
|
* Time Complexity: O(1)
|
|
@@ -296,6 +207,44 @@ export class Deque extends IterableElementBase {
|
|
|
296
207
|
this._size -= 1;
|
|
297
208
|
return element;
|
|
298
209
|
}
|
|
210
|
+
/**
|
|
211
|
+
* Time Complexity: O(1) - Removes the last element.
|
|
212
|
+
* Space Complexity: O(1) - Operates in-place.
|
|
213
|
+
*/
|
|
214
|
+
isEmpty() {
|
|
215
|
+
return this.size === 0;
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* The clear() function resets the state of the object by initializing all variables to their default
|
|
219
|
+
* values.
|
|
220
|
+
*/
|
|
221
|
+
clear() {
|
|
222
|
+
this._buckets = [new Array(this._bucketSize)];
|
|
223
|
+
this._bucketCount = 1;
|
|
224
|
+
this._bucketFirst = this._bucketLast = this._size = 0;
|
|
225
|
+
this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* The below function is a generator that yields elements from a collection one by one.
|
|
229
|
+
*/
|
|
230
|
+
*begin() {
|
|
231
|
+
let index = 0;
|
|
232
|
+
while (index < this.size) {
|
|
233
|
+
yield this.getAt(index);
|
|
234
|
+
index++;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* The function `reverseBegin()` is a generator that yields elements in reverse order starting from
|
|
239
|
+
* the last element.
|
|
240
|
+
*/
|
|
241
|
+
*reverseBegin() {
|
|
242
|
+
let index = this.size - 1;
|
|
243
|
+
while (index >= 0) {
|
|
244
|
+
yield this.getAt(index);
|
|
245
|
+
index--;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
299
248
|
/**
|
|
300
249
|
* Time Complexity: O(1)
|
|
301
250
|
* Space Complexity: O(1)
|
|
@@ -333,6 +282,7 @@ export class Deque extends IterableElementBase {
|
|
|
333
282
|
rangeCheck(pos, 0, this.size - 1);
|
|
334
283
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
335
284
|
this._buckets[bucketIndex][indexInBucket] = element;
|
|
285
|
+
return true;
|
|
336
286
|
}
|
|
337
287
|
/**
|
|
338
288
|
* Time Complexity: O(n)
|
|
@@ -342,7 +292,7 @@ export class Deque extends IterableElementBase {
|
|
|
342
292
|
* Time Complexity: O(n)
|
|
343
293
|
* Space Complexity: O(n)
|
|
344
294
|
*
|
|
345
|
-
* The `
|
|
295
|
+
* The `addAt` function inserts one or more elements at a specified position in an array-like data
|
|
346
296
|
* structure.
|
|
347
297
|
* @param {number} pos - The `pos` parameter represents the position at which the element(s) should
|
|
348
298
|
* be inserted. It is of type `number`.
|
|
@@ -353,7 +303,7 @@ export class Deque extends IterableElementBase {
|
|
|
353
303
|
* will be inserted once. However, you can provide a different value for `num` if you want
|
|
354
304
|
* @returns The size of the array after the insertion is being returned.
|
|
355
305
|
*/
|
|
356
|
-
|
|
306
|
+
addAt(pos, element, num = 1) {
|
|
357
307
|
const length = this.size;
|
|
358
308
|
rangeCheck(pos, 0, length);
|
|
359
309
|
if (pos === 0) {
|
|
@@ -375,7 +325,7 @@ export class Deque extends IterableElementBase {
|
|
|
375
325
|
for (let i = 0; i < arr.length; ++i)
|
|
376
326
|
this.push(arr[i]);
|
|
377
327
|
}
|
|
378
|
-
return
|
|
328
|
+
return true;
|
|
379
329
|
}
|
|
380
330
|
/**
|
|
381
331
|
* Time Complexity: O(1)
|
|
@@ -434,7 +384,7 @@ export class Deque extends IterableElementBase {
|
|
|
434
384
|
}
|
|
435
385
|
this.pop();
|
|
436
386
|
}
|
|
437
|
-
return
|
|
387
|
+
return true;
|
|
438
388
|
}
|
|
439
389
|
/**
|
|
440
390
|
* Time Complexity: O(n)
|
|
@@ -453,7 +403,7 @@ export class Deque extends IterableElementBase {
|
|
|
453
403
|
delete(element) {
|
|
454
404
|
const size = this.size;
|
|
455
405
|
if (size === 0)
|
|
456
|
-
return
|
|
406
|
+
return false;
|
|
457
407
|
let i = 0;
|
|
458
408
|
let index = 0;
|
|
459
409
|
while (i < size) {
|
|
@@ -465,7 +415,7 @@ export class Deque extends IterableElementBase {
|
|
|
465
415
|
i += 1;
|
|
466
416
|
}
|
|
467
417
|
this.cut(index - 1);
|
|
468
|
-
return
|
|
418
|
+
return true;
|
|
469
419
|
}
|
|
470
420
|
/**
|
|
471
421
|
* Time Complexity: O(n)
|
|
@@ -505,7 +455,7 @@ export class Deque extends IterableElementBase {
|
|
|
505
455
|
*/
|
|
506
456
|
unique() {
|
|
507
457
|
if (this.size <= 1) {
|
|
508
|
-
return this
|
|
458
|
+
return this;
|
|
509
459
|
}
|
|
510
460
|
let index = 1;
|
|
511
461
|
let prev = this.getAt(0);
|
|
@@ -517,7 +467,7 @@ export class Deque extends IterableElementBase {
|
|
|
517
467
|
}
|
|
518
468
|
}
|
|
519
469
|
this.cut(index - 1);
|
|
520
|
-
return this
|
|
470
|
+
return this;
|
|
521
471
|
}
|
|
522
472
|
/**
|
|
523
473
|
* Time Complexity: O(n log n)
|
|
@@ -531,7 +481,7 @@ export class Deque extends IterableElementBase {
|
|
|
531
481
|
* @param [comparator] - The `comparator` parameter is a function that takes in two elements `x` and
|
|
532
482
|
* `y` of type `E` and returns a number. The comparator function is used to determine the order of
|
|
533
483
|
* the elements in the sorted array.
|
|
534
|
-
* @returns
|
|
484
|
+
* @returns Deque<E>
|
|
535
485
|
*/
|
|
536
486
|
sort(comparator) {
|
|
537
487
|
const arr = [];
|
|
@@ -602,7 +552,7 @@ export class Deque extends IterableElementBase {
|
|
|
602
552
|
return element;
|
|
603
553
|
}
|
|
604
554
|
}
|
|
605
|
-
return
|
|
555
|
+
return;
|
|
606
556
|
}
|
|
607
557
|
/**
|
|
608
558
|
* Time Complexity: O(n)
|
|
@@ -639,11 +589,7 @@ export class Deque extends IterableElementBase {
|
|
|
639
589
|
* @returns The `toArray()` method is returning an array of elements of type `E`.
|
|
640
590
|
*/
|
|
641
591
|
toArray() {
|
|
642
|
-
|
|
643
|
-
for (let i = 0; i < this.size; ++i) {
|
|
644
|
-
arr.push(this.getAt(i));
|
|
645
|
-
}
|
|
646
|
-
return arr;
|
|
592
|
+
return [...this];
|
|
647
593
|
}
|
|
648
594
|
/**
|
|
649
595
|
* Time Complexity: O(n)
|
|
@@ -703,11 +649,55 @@ export class Deque extends IterableElementBase {
|
|
|
703
649
|
return newDeque;
|
|
704
650
|
}
|
|
705
651
|
/**
|
|
706
|
-
* Time Complexity: O(n)
|
|
707
|
-
* Space Complexity: O(n)
|
|
652
|
+
* Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
|
|
653
|
+
* Space Complexity: O(n) - Due to potential resizing.
|
|
654
|
+
*/
|
|
655
|
+
/**
|
|
656
|
+
* Time Complexity: O(1)
|
|
657
|
+
* Space Complexity: O(n) - In worst case, resizing doubles the array size.
|
|
658
|
+
*
|
|
659
|
+
* The addLast function adds an element to the end of an array.
|
|
660
|
+
* @param {E} element - The element parameter represents the element that you want to add to the end of the
|
|
661
|
+
* data structure.
|
|
662
|
+
*/
|
|
663
|
+
addLast(element) {
|
|
664
|
+
return this.push(element);
|
|
665
|
+
}
|
|
666
|
+
/**
|
|
667
|
+
* Time Complexity: O(1) - Removes the first element.
|
|
668
|
+
* Space Complexity: O(1) - In-place operation.
|
|
669
|
+
*/
|
|
670
|
+
/**
|
|
671
|
+
* Time Complexity: O(1) - Removes the last element.
|
|
672
|
+
* Space Complexity: O(1) - Operates in-place.
|
|
673
|
+
*
|
|
674
|
+
* The function "pollLast" removes and returns the last element of an array.
|
|
675
|
+
* @returns The last element of the array is being returned.
|
|
708
676
|
*/
|
|
709
|
-
|
|
710
|
-
|
|
677
|
+
pollLast() {
|
|
678
|
+
return this.pop();
|
|
679
|
+
}
|
|
680
|
+
/**
|
|
681
|
+
* Time Complexity: O(1).
|
|
682
|
+
* Space Complexity: O(n) - Due to potential resizing.
|
|
683
|
+
*
|
|
684
|
+
* The "addFirst" function adds an element to the beginning of an array.
|
|
685
|
+
* @param {E} element - The parameter "element" represents the element that you want to add to the
|
|
686
|
+
* beginning of the data structure.
|
|
687
|
+
*/
|
|
688
|
+
addFirst(element) {
|
|
689
|
+
return this.unshift(element);
|
|
690
|
+
}
|
|
691
|
+
/**
|
|
692
|
+
* Time Complexity: O(1) - Removes the first element.
|
|
693
|
+
* Space Complexity: O(1) - In-place operation.
|
|
694
|
+
*
|
|
695
|
+
* The function "pollFirst" removes and returns the first element of an array.
|
|
696
|
+
* @returns The method `pollFirst()` is returning the first element of the array after removing it
|
|
697
|
+
* from the beginning. If the array is empty, it will return `undefined`.
|
|
698
|
+
*/
|
|
699
|
+
pollFirst() {
|
|
700
|
+
return this.shift();
|
|
711
701
|
}
|
|
712
702
|
/**
|
|
713
703
|
* Time Complexity: O(n)
|