graph-typed 1.54.3 → 2.0.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/base/iterable-element-base.d.ts +14 -40
- package/dist/data-structures/base/iterable-element-base.js +14 -11
- package/dist/data-structures/base/linear-base.d.ts +277 -0
- package/dist/data-structures/base/linear-base.js +552 -0
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +12 -8
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +50 -37
- package/dist/data-structures/binary-tree/avl-tree.d.ts +64 -0
- package/dist/data-structures/binary-tree/avl-tree.js +64 -0
- package/dist/data-structures/binary-tree/binary-tree.js +5 -5
- package/dist/data-structures/binary-tree/bst.js +11 -11
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +175 -14
- package/dist/data-structures/binary-tree/tree-multi-map.js +210 -40
- package/dist/data-structures/graph/abstract-graph.js +16 -16
- package/dist/data-structures/hash/hash-map.d.ts +46 -0
- package/dist/data-structures/hash/hash-map.js +46 -0
- package/dist/data-structures/heap/heap.d.ts +3 -11
- package/dist/data-structures/heap/heap.js +0 -10
- package/dist/data-structures/heap/max-heap.d.ts +2 -2
- package/dist/data-structures/heap/min-heap.d.ts +2 -2
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +65 -94
- package/dist/data-structures/linked-list/doubly-linked-list.js +131 -146
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +145 -75
- package/dist/data-structures/linked-list/singly-linked-list.js +283 -169
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +2 -2
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +2 -2
- package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -2
- package/dist/data-structures/queue/deque.d.ts +130 -91
- package/dist/data-structures/queue/deque.js +269 -169
- package/dist/data-structures/queue/queue.d.ts +131 -40
- package/dist/data-structures/queue/queue.js +181 -50
- package/dist/data-structures/stack/stack.d.ts +124 -11
- package/dist/data-structures/stack/stack.js +121 -10
- package/dist/data-structures/trie/trie.d.ts +4 -3
- package/dist/data-structures/trie/trie.js +3 -0
- package/dist/types/data-structures/base/base.d.ts +9 -4
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/types/data-structures/queue/deque.d.ts +2 -3
- package/dist/types/data-structures/queue/queue.d.ts +2 -2
- package/package.json +2 -2
- package/src/data-structures/base/iterable-element-base.ts +29 -20
- package/src/data-structures/base/linear-base.ts +649 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +51 -36
- package/src/data-structures/binary-tree/avl-tree.ts +64 -0
- package/src/data-structures/binary-tree/binary-tree.ts +5 -5
- package/src/data-structures/binary-tree/bst.ts +9 -9
- package/src/data-structures/binary-tree/tree-multi-map.ts +214 -40
- package/src/data-structures/graph/abstract-graph.ts +16 -16
- package/src/data-structures/hash/hash-map.ts +46 -0
- package/src/data-structures/heap/heap.ts +3 -14
- package/src/data-structures/heap/max-heap.ts +2 -2
- package/src/data-structures/heap/min-heap.ts +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +144 -160
- package/src/data-structures/linked-list/singly-linked-list.ts +307 -185
- package/src/data-structures/priority-queue/max-priority-queue.ts +2 -5
- package/src/data-structures/priority-queue/min-priority-queue.ts +2 -5
- package/src/data-structures/priority-queue/priority-queue.ts +2 -2
- package/src/data-structures/queue/deque.ts +286 -183
- package/src/data-structures/queue/queue.ts +196 -63
- package/src/data-structures/stack/stack.ts +124 -18
- package/src/data-structures/trie/trie.ts +7 -3
- package/src/types/data-structures/base/base.ts +17 -8
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +1 -1
- package/src/types/data-structures/linked-list/doubly-linked-list.ts +2 -2
- package/src/types/data-structures/linked-list/singly-linked-list.ts +2 -2
- package/src/types/data-structures/queue/deque.ts +2 -3
- package/src/types/data-structures/queue/queue.ts +2 -2
|
@@ -1,16 +1,104 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Deque = void 0;
|
|
4
|
-
const base_1 = require("../base");
|
|
5
4
|
const utils_1 = require("../../utils");
|
|
5
|
+
const linear_base_1 = require("../base/linear-base");
|
|
6
6
|
/**
|
|
7
7
|
* 1. Operations at Both Ends: Supports adding and removing elements at both the front and back of the queue. This allows it to be used as a stack (last in, first out) and a queue (first in, first out).
|
|
8
8
|
* 2. Efficient Random Access: Being based on an array, it offers fast random access capability, allowing constant time access to any element.
|
|
9
9
|
* 3. Continuous Memory Allocation: Since it is based on an array, all elements are stored contiguously in memory, which can bring cache friendliness and efficient memory access.
|
|
10
10
|
* 4. Efficiency: Adding and removing elements at both ends of a deque is usually very fast. However, when the dynamic array needs to expand, it may involve copying the entire array to a larger one, and this operation has a time complexity of O(n).
|
|
11
11
|
* 5. Performance jitter: Deque may experience performance jitter, but DoublyLinkedList will not
|
|
12
|
+
* @example
|
|
13
|
+
* // prize roulette
|
|
14
|
+
* class PrizeRoulette {
|
|
15
|
+
* private deque: Deque<string>;
|
|
16
|
+
*
|
|
17
|
+
* constructor(prizes: string[]) {
|
|
18
|
+
* // Initialize the deque with prizes
|
|
19
|
+
* this.deque = new Deque<string>(prizes);
|
|
20
|
+
* }
|
|
21
|
+
*
|
|
22
|
+
* // Rotate clockwise to the right (forward)
|
|
23
|
+
* rotateClockwise(steps: number): void {
|
|
24
|
+
* const n = this.deque.length;
|
|
25
|
+
* if (n === 0) return;
|
|
26
|
+
*
|
|
27
|
+
* for (let i = 0; i < steps; i++) {
|
|
28
|
+
* const last = this.deque.pop(); // Remove the last element
|
|
29
|
+
* this.deque.unshift(last!); // Add it to the front
|
|
30
|
+
* }
|
|
31
|
+
* }
|
|
32
|
+
*
|
|
33
|
+
* // Rotate counterclockwise to the left (backward)
|
|
34
|
+
* rotateCounterClockwise(steps: number): void {
|
|
35
|
+
* const n = this.deque.length;
|
|
36
|
+
* if (n === 0) return;
|
|
37
|
+
*
|
|
38
|
+
* for (let i = 0; i < steps; i++) {
|
|
39
|
+
* const first = this.deque.shift(); // Remove the first element
|
|
40
|
+
* this.deque.push(first!); // Add it to the back
|
|
41
|
+
* }
|
|
42
|
+
* }
|
|
43
|
+
*
|
|
44
|
+
* // Display the current prize at the head
|
|
45
|
+
* display() {
|
|
46
|
+
* return this.deque.first;
|
|
47
|
+
* }
|
|
48
|
+
* }
|
|
49
|
+
*
|
|
50
|
+
* // Example usage
|
|
51
|
+
* const prizes = ['Car', 'Bike', 'Laptop', 'Phone', 'Watch', 'Headphones']; // Initialize the prize list
|
|
52
|
+
* const roulette = new PrizeRoulette(prizes);
|
|
53
|
+
*
|
|
54
|
+
* // Display the initial state
|
|
55
|
+
* console.log(roulette.display()); // 'Car' // Car
|
|
56
|
+
*
|
|
57
|
+
* // Rotate clockwise by 3 steps
|
|
58
|
+
* roulette.rotateClockwise(3);
|
|
59
|
+
* console.log(roulette.display()); // 'Phone' // Phone
|
|
60
|
+
*
|
|
61
|
+
* // Rotate counterclockwise by 2 steps
|
|
62
|
+
* roulette.rotateCounterClockwise(2);
|
|
63
|
+
* console.log(roulette.display()); // 'Headphones'
|
|
64
|
+
* @example
|
|
65
|
+
* // sliding window
|
|
66
|
+
* // Maximum function of sliding window
|
|
67
|
+
* function maxSlidingWindow(nums: number[], k: number): number[] {
|
|
68
|
+
* const n = nums.length;
|
|
69
|
+
* if (n * k === 0) return [];
|
|
70
|
+
*
|
|
71
|
+
* const deq = new Deque<number>();
|
|
72
|
+
* const result: number[] = [];
|
|
73
|
+
*
|
|
74
|
+
* for (let i = 0; i < n; i++) {
|
|
75
|
+
* // Delete indexes in the queue that are not within the window range
|
|
76
|
+
* if (deq.length > 0 && deq.first! === i - k) {
|
|
77
|
+
* deq.shift();
|
|
78
|
+
* }
|
|
79
|
+
*
|
|
80
|
+
* // Remove all indices less than the current value from the tail of the queue
|
|
81
|
+
* while (deq.length > 0 && nums[deq.last!] < nums[i]) {
|
|
82
|
+
* deq.pop();
|
|
83
|
+
* }
|
|
84
|
+
*
|
|
85
|
+
* // Add the current index to the end of the queue
|
|
86
|
+
* deq.push(i);
|
|
87
|
+
*
|
|
88
|
+
* // Add the maximum value of the window to the results
|
|
89
|
+
* if (i >= k - 1) {
|
|
90
|
+
* result.push(nums[deq.first!]);
|
|
91
|
+
* }
|
|
92
|
+
* }
|
|
93
|
+
*
|
|
94
|
+
* return result;
|
|
95
|
+
* }
|
|
96
|
+
*
|
|
97
|
+
* const nums = [1, 3, -1, -3, 5, 3, 6, 7];
|
|
98
|
+
* const k = 3;
|
|
99
|
+
* console.log(maxSlidingWindow(nums, k)); // [3, 3, 5, 5, 6, 7]
|
|
12
100
|
*/
|
|
13
|
-
class Deque extends
|
|
101
|
+
class Deque extends linear_base_1.LinearBase {
|
|
14
102
|
/**
|
|
15
103
|
* The constructor initializes a Deque object with optional iterable of elements and options.
|
|
16
104
|
* @param elements - An iterable object (such as an array or a Set) that contains the initial
|
|
@@ -25,20 +113,17 @@ class Deque extends base_1.IterableElementBase {
|
|
|
25
113
|
constructor(elements = [], options) {
|
|
26
114
|
super(options);
|
|
27
115
|
this._bucketSize = 1 << 12;
|
|
28
|
-
this._maxLen = -1;
|
|
29
116
|
this._bucketFirst = 0;
|
|
30
117
|
this._firstInBucket = 0;
|
|
31
118
|
this._bucketLast = 0;
|
|
32
119
|
this._lastInBucket = 0;
|
|
33
120
|
this._bucketCount = 0;
|
|
34
121
|
this._buckets = [];
|
|
35
|
-
this.
|
|
122
|
+
this._length = 0;
|
|
36
123
|
if (options) {
|
|
37
|
-
const { bucketSize
|
|
124
|
+
const { bucketSize } = options;
|
|
38
125
|
if (typeof bucketSize === 'number')
|
|
39
126
|
this._bucketSize = bucketSize;
|
|
40
|
-
if (typeof maxLen === 'number' && maxLen > 0 && maxLen % 1 === 0)
|
|
41
|
-
this._maxLen = maxLen;
|
|
42
127
|
}
|
|
43
128
|
let _size;
|
|
44
129
|
if ('length' in elements) {
|
|
@@ -62,72 +147,29 @@ class Deque extends base_1.IterableElementBase {
|
|
|
62
147
|
this._firstInBucket = this._lastInBucket = (this._bucketSize - (_size % this._bucketSize)) >> 1;
|
|
63
148
|
this.pushMany(elements);
|
|
64
149
|
}
|
|
65
|
-
/**
|
|
66
|
-
* The bucketSize function returns the size of the bucket.
|
|
67
|
-
*
|
|
68
|
-
* @return The size of the bucket
|
|
69
|
-
*/
|
|
70
150
|
get bucketSize() {
|
|
71
151
|
return this._bucketSize;
|
|
72
152
|
}
|
|
73
|
-
/**
|
|
74
|
-
* The maxLen function returns the max length of the deque.
|
|
75
|
-
*
|
|
76
|
-
* @return The max length of the deque
|
|
77
|
-
*/
|
|
78
|
-
get maxLen() {
|
|
79
|
-
return this._maxLen;
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* The function returns the value of the protected variable `_bucketFirst`.
|
|
83
|
-
* @returns The value of the `_bucketFirst` property.
|
|
84
|
-
*/
|
|
85
153
|
get bucketFirst() {
|
|
86
154
|
return this._bucketFirst;
|
|
87
155
|
}
|
|
88
|
-
/**
|
|
89
|
-
* The function returns the value of the protected variable _firstInBucket.
|
|
90
|
-
* @returns The method is returning the value of the variable `_firstInBucket`, which is of type
|
|
91
|
-
* `number`.
|
|
92
|
-
*/
|
|
93
156
|
get firstInBucket() {
|
|
94
157
|
return this._firstInBucket;
|
|
95
158
|
}
|
|
96
|
-
/**
|
|
97
|
-
* The function returns the value of the protected variable `_bucketLast`.
|
|
98
|
-
* @returns The value of the `_bucketLast` property, which is a number.
|
|
99
|
-
*/
|
|
100
159
|
get bucketLast() {
|
|
101
160
|
return this._bucketLast;
|
|
102
161
|
}
|
|
103
|
-
/**
|
|
104
|
-
* The function returns the value of the protected variable _lastInBucket.
|
|
105
|
-
* @returns The method is returning the value of the variable `_lastInBucket`, which is of type
|
|
106
|
-
* `number`.
|
|
107
|
-
*/
|
|
108
162
|
get lastInBucket() {
|
|
109
163
|
return this._lastInBucket;
|
|
110
164
|
}
|
|
111
|
-
/**
|
|
112
|
-
* The function returns the number of buckets.
|
|
113
|
-
* @returns The number of buckets.
|
|
114
|
-
*/
|
|
115
165
|
get bucketCount() {
|
|
116
166
|
return this._bucketCount;
|
|
117
167
|
}
|
|
118
|
-
/**
|
|
119
|
-
* The buckets function returns the buckets property of the object.
|
|
120
|
-
* @return The buckets property
|
|
121
|
-
*/
|
|
122
168
|
get buckets() {
|
|
123
169
|
return this._buckets;
|
|
124
170
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
* @return The number of values in the set
|
|
128
|
-
*/
|
|
129
|
-
get size() {
|
|
130
|
-
return this._size;
|
|
171
|
+
get length() {
|
|
172
|
+
return this._length;
|
|
131
173
|
}
|
|
132
174
|
/**
|
|
133
175
|
* The function returns the first element in a collection if it exists, otherwise it returns
|
|
@@ -135,7 +177,7 @@ class Deque extends base_1.IterableElementBase {
|
|
|
135
177
|
* @returns The first element of the collection, of type E, is being returned.
|
|
136
178
|
*/
|
|
137
179
|
get first() {
|
|
138
|
-
if (this.
|
|
180
|
+
if (this._length === 0)
|
|
139
181
|
return;
|
|
140
182
|
return this._buckets[this._bucketFirst][this._firstInBucket];
|
|
141
183
|
}
|
|
@@ -144,7 +186,7 @@ class Deque extends base_1.IterableElementBase {
|
|
|
144
186
|
* @return The last element in the array
|
|
145
187
|
*/
|
|
146
188
|
get last() {
|
|
147
|
-
if (this.
|
|
189
|
+
if (this._length === 0)
|
|
148
190
|
return;
|
|
149
191
|
return this._buckets[this._bucketLast][this._lastInBucket];
|
|
150
192
|
}
|
|
@@ -158,7 +200,7 @@ class Deque extends base_1.IterableElementBase {
|
|
|
158
200
|
* @returns The size of the data structure after the element has been pushed.
|
|
159
201
|
*/
|
|
160
202
|
push(element) {
|
|
161
|
-
if (this.
|
|
203
|
+
if (this._length) {
|
|
162
204
|
if (this._lastInBucket < this._bucketSize - 1) {
|
|
163
205
|
this._lastInBucket += 1;
|
|
164
206
|
}
|
|
@@ -173,9 +215,9 @@ class Deque extends base_1.IterableElementBase {
|
|
|
173
215
|
if (this._bucketLast === this._bucketFirst && this._lastInBucket === this._firstInBucket)
|
|
174
216
|
this._reallocate();
|
|
175
217
|
}
|
|
176
|
-
this.
|
|
218
|
+
this._length += 1;
|
|
177
219
|
this._buckets[this._bucketLast][this._lastInBucket] = element;
|
|
178
|
-
if (this._maxLen > 0 && this.
|
|
220
|
+
if (this._maxLen > 0 && this._length > this._maxLen)
|
|
179
221
|
this.shift();
|
|
180
222
|
return true;
|
|
181
223
|
}
|
|
@@ -188,10 +230,10 @@ class Deque extends base_1.IterableElementBase {
|
|
|
188
230
|
* @returns The element that was removed from the data structure is being returned.
|
|
189
231
|
*/
|
|
190
232
|
pop() {
|
|
191
|
-
if (this.
|
|
233
|
+
if (this._length === 0)
|
|
192
234
|
return;
|
|
193
235
|
const element = this._buckets[this._bucketLast][this._lastInBucket];
|
|
194
|
-
if (this.
|
|
236
|
+
if (this._length !== 1) {
|
|
195
237
|
if (this._lastInBucket > 0) {
|
|
196
238
|
this._lastInBucket -= 1;
|
|
197
239
|
}
|
|
@@ -204,7 +246,7 @@ class Deque extends base_1.IterableElementBase {
|
|
|
204
246
|
this._lastInBucket = this._bucketSize - 1;
|
|
205
247
|
}
|
|
206
248
|
}
|
|
207
|
-
this.
|
|
249
|
+
this._length -= 1;
|
|
208
250
|
return element;
|
|
209
251
|
}
|
|
210
252
|
/**
|
|
@@ -217,10 +259,10 @@ class Deque extends base_1.IterableElementBase {
|
|
|
217
259
|
* returned.
|
|
218
260
|
*/
|
|
219
261
|
shift() {
|
|
220
|
-
if (this.
|
|
262
|
+
if (this._length === 0)
|
|
221
263
|
return;
|
|
222
264
|
const element = this._buckets[this._bucketFirst][this._firstInBucket];
|
|
223
|
-
if (this.
|
|
265
|
+
if (this._length !== 1) {
|
|
224
266
|
if (this._firstInBucket < this._bucketSize - 1) {
|
|
225
267
|
this._firstInBucket += 1;
|
|
226
268
|
}
|
|
@@ -233,7 +275,7 @@ class Deque extends base_1.IterableElementBase {
|
|
|
233
275
|
this._firstInBucket = 0;
|
|
234
276
|
}
|
|
235
277
|
}
|
|
236
|
-
this.
|
|
278
|
+
this._length -= 1;
|
|
237
279
|
return element;
|
|
238
280
|
}
|
|
239
281
|
/**
|
|
@@ -247,7 +289,7 @@ class Deque extends base_1.IterableElementBase {
|
|
|
247
289
|
* @returns The size of the data structure after the element has been added.
|
|
248
290
|
*/
|
|
249
291
|
unshift(element) {
|
|
250
|
-
if (this.
|
|
292
|
+
if (this._length) {
|
|
251
293
|
if (this._firstInBucket > 0) {
|
|
252
294
|
this._firstInBucket -= 1;
|
|
253
295
|
}
|
|
@@ -262,9 +304,9 @@ class Deque extends base_1.IterableElementBase {
|
|
|
262
304
|
if (this._bucketFirst === this._bucketLast && this._firstInBucket === this._lastInBucket)
|
|
263
305
|
this._reallocate();
|
|
264
306
|
}
|
|
265
|
-
this.
|
|
307
|
+
this._length += 1;
|
|
266
308
|
this._buckets[this._bucketFirst][this._firstInBucket] = element;
|
|
267
|
-
if (this._maxLen > 0 && this.
|
|
309
|
+
if (this._maxLen > 0 && this._length > this._maxLen)
|
|
268
310
|
this.pop();
|
|
269
311
|
return true;
|
|
270
312
|
}
|
|
@@ -327,7 +369,7 @@ class Deque extends base_1.IterableElementBase {
|
|
|
327
369
|
* @returns A boolean value indicating whether the size of the object is 0 or not.
|
|
328
370
|
*/
|
|
329
371
|
isEmpty() {
|
|
330
|
-
return this.
|
|
372
|
+
return this._length === 0;
|
|
331
373
|
}
|
|
332
374
|
/**
|
|
333
375
|
* Time Complexity: O(1)
|
|
@@ -339,30 +381,9 @@ class Deque extends base_1.IterableElementBase {
|
|
|
339
381
|
clear() {
|
|
340
382
|
this._buckets = [new Array(this._bucketSize)];
|
|
341
383
|
this._bucketCount = 1;
|
|
342
|
-
this._bucketFirst = this._bucketLast = this.
|
|
384
|
+
this._bucketFirst = this._bucketLast = this._length = 0;
|
|
343
385
|
this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
|
|
344
386
|
}
|
|
345
|
-
/**
|
|
346
|
-
* The below function is a generator that yields elements from a collection one by one.
|
|
347
|
-
*/
|
|
348
|
-
*begin() {
|
|
349
|
-
let index = 0;
|
|
350
|
-
while (index < this._size) {
|
|
351
|
-
yield this.at(index);
|
|
352
|
-
index++;
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
/**
|
|
356
|
-
* The function `reverseBegin()` is a generator that yields elements in reverse order starting from
|
|
357
|
-
* the last element.
|
|
358
|
-
*/
|
|
359
|
-
*reverseBegin() {
|
|
360
|
-
let index = this._size - 1;
|
|
361
|
-
while (index >= 0) {
|
|
362
|
-
yield this.at(index);
|
|
363
|
-
index--;
|
|
364
|
-
}
|
|
365
|
-
}
|
|
366
387
|
/**
|
|
367
388
|
* Time Complexity: O(1)
|
|
368
389
|
* Space Complexity: O(1)
|
|
@@ -374,7 +395,7 @@ class Deque extends base_1.IterableElementBase {
|
|
|
374
395
|
* @returns The element at the specified position in the data structure is being returned.
|
|
375
396
|
*/
|
|
376
397
|
at(pos) {
|
|
377
|
-
(0, utils_1.rangeCheck)(pos, 0, this.
|
|
398
|
+
(0, utils_1.rangeCheck)(pos, 0, this._length - 1);
|
|
378
399
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
379
400
|
return this._buckets[bucketIndex][indexInBucket];
|
|
380
401
|
}
|
|
@@ -389,7 +410,7 @@ class Deque extends base_1.IterableElementBase {
|
|
|
389
410
|
* position in the data structure.
|
|
390
411
|
*/
|
|
391
412
|
setAt(pos, element) {
|
|
392
|
-
(0, utils_1.rangeCheck)(pos, 0, this.
|
|
413
|
+
(0, utils_1.rangeCheck)(pos, 0, this._length - 1);
|
|
393
414
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
394
415
|
this._buckets[bucketIndex][indexInBucket] = element;
|
|
395
416
|
return true;
|
|
@@ -410,19 +431,19 @@ class Deque extends base_1.IterableElementBase {
|
|
|
410
431
|
* @returns The size of the array after the insertion is being returned.
|
|
411
432
|
*/
|
|
412
433
|
addAt(pos, element, num = 1) {
|
|
413
|
-
const length = this.
|
|
434
|
+
const length = this._length;
|
|
414
435
|
(0, utils_1.rangeCheck)(pos, 0, length);
|
|
415
436
|
if (pos === 0) {
|
|
416
437
|
while (num--)
|
|
417
438
|
this.unshift(element);
|
|
418
439
|
}
|
|
419
|
-
else if (pos === this.
|
|
440
|
+
else if (pos === this._length) {
|
|
420
441
|
while (num--)
|
|
421
442
|
this.push(element);
|
|
422
443
|
}
|
|
423
444
|
else {
|
|
424
445
|
const arr = [];
|
|
425
|
-
for (let i = pos; i < this.
|
|
446
|
+
for (let i = pos; i < this._length; ++i) {
|
|
426
447
|
arr.push(this.at(i));
|
|
427
448
|
}
|
|
428
449
|
this.cut(pos - 1, true);
|
|
@@ -453,17 +474,69 @@ class Deque extends base_1.IterableElementBase {
|
|
|
453
474
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
454
475
|
this._bucketLast = bucketIndex;
|
|
455
476
|
this._lastInBucket = indexInBucket;
|
|
456
|
-
this.
|
|
477
|
+
this._length = pos + 1;
|
|
457
478
|
return this;
|
|
458
479
|
}
|
|
459
480
|
else {
|
|
460
|
-
const newDeque =
|
|
481
|
+
const newDeque = this._createInstance({
|
|
482
|
+
bucketSize: this._bucketSize,
|
|
483
|
+
toElementFn: this._toElementFn,
|
|
484
|
+
maxLen: this._maxLen
|
|
485
|
+
});
|
|
461
486
|
for (let i = 0; i <= pos; i++) {
|
|
462
487
|
newDeque.push(this.at(i));
|
|
463
488
|
}
|
|
464
489
|
return newDeque;
|
|
465
490
|
}
|
|
466
491
|
}
|
|
492
|
+
/**
|
|
493
|
+
* Time Complexity: O(n)
|
|
494
|
+
* Space Complexity: O(1)
|
|
495
|
+
*
|
|
496
|
+
* The `splice` function in TypeScript overrides the default behavior to remove and insert elements
|
|
497
|
+
* in a Deque data structure while ensuring the starting position and delete count are within bounds.
|
|
498
|
+
* @param {number} start - The `start` parameter in the `splice` method represents the index at which
|
|
499
|
+
* to start changing the array. Items will be removed or added starting from this index.
|
|
500
|
+
* @param {number} deleteCount - The `deleteCount` parameter in the `splice` method represents the
|
|
501
|
+
* number of elements to remove from the array starting at the specified `start` index. If
|
|
502
|
+
* `deleteCount` is not provided, it defaults to the number of elements from the `start` index to the
|
|
503
|
+
* end of the array (`
|
|
504
|
+
* @param {E[]} items - The `items` parameter in the `splice` method represents the elements that
|
|
505
|
+
* will be inserted into the deque at the specified `start` index. These elements will be inserted in
|
|
506
|
+
* place of the elements that are removed based on the `start` and `deleteCount` parameters.
|
|
507
|
+
* @returns The `splice` method is returning the array `deletedElements` which contains the elements
|
|
508
|
+
* that were removed from the Deque during the splice operation.
|
|
509
|
+
*/
|
|
510
|
+
splice(start, deleteCount = this._length - start, ...items) {
|
|
511
|
+
// Check whether the starting position is legal
|
|
512
|
+
(0, utils_1.rangeCheck)(start, 0, this._length);
|
|
513
|
+
// Adjust the value of deleteCount
|
|
514
|
+
if (deleteCount < 0)
|
|
515
|
+
deleteCount = 0;
|
|
516
|
+
if (start + deleteCount > this._length)
|
|
517
|
+
deleteCount = this._length - start;
|
|
518
|
+
// Save deleted elements
|
|
519
|
+
const deletedElements = this._createInstance();
|
|
520
|
+
// Add removed elements to the result
|
|
521
|
+
for (let i = 0; i < deleteCount; i++) {
|
|
522
|
+
deletedElements.push(this.at(start + i));
|
|
523
|
+
}
|
|
524
|
+
// Calculate the range that needs to be deleted
|
|
525
|
+
const elementsAfter = [];
|
|
526
|
+
for (let i = start + deleteCount; i < this._length; i++) {
|
|
527
|
+
elementsAfter.push(this.at(i));
|
|
528
|
+
}
|
|
529
|
+
// Adjust the length of the current Deque
|
|
530
|
+
this.cut(start - 1, true);
|
|
531
|
+
for (const item of items) {
|
|
532
|
+
this.push(item);
|
|
533
|
+
}
|
|
534
|
+
// Insert subsequent elements back
|
|
535
|
+
for (const element of elementsAfter) {
|
|
536
|
+
this.push(element);
|
|
537
|
+
}
|
|
538
|
+
return deletedElements;
|
|
539
|
+
}
|
|
467
540
|
/**
|
|
468
541
|
* Time Complexity: O(1)
|
|
469
542
|
* Space Complexity: O(1) or O(n)
|
|
@@ -487,14 +560,18 @@ class Deque extends base_1.IterableElementBase {
|
|
|
487
560
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
488
561
|
this._bucketFirst = bucketIndex;
|
|
489
562
|
this._firstInBucket = indexInBucket;
|
|
490
|
-
this.
|
|
563
|
+
this._length = this._length - pos;
|
|
491
564
|
return this;
|
|
492
565
|
}
|
|
493
566
|
else {
|
|
494
|
-
const newDeque =
|
|
567
|
+
const newDeque = this._createInstance({
|
|
568
|
+
bucketSize: this._bucketSize,
|
|
569
|
+
toElementFn: this._toElementFn,
|
|
570
|
+
maxLen: this._maxLen
|
|
571
|
+
});
|
|
495
572
|
if (pos < 0)
|
|
496
573
|
pos = 0;
|
|
497
|
-
for (let i = pos; i < this.
|
|
574
|
+
for (let i = pos; i < this._length; i++) {
|
|
498
575
|
newDeque.push(this.at(i));
|
|
499
576
|
}
|
|
500
577
|
return newDeque;
|
|
@@ -512,23 +589,32 @@ class Deque extends base_1.IterableElementBase {
|
|
|
512
589
|
* @returns The size of the data structure after the deletion operation is performed.
|
|
513
590
|
*/
|
|
514
591
|
deleteAt(pos) {
|
|
515
|
-
(0, utils_1.rangeCheck)(pos, 0, this.
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
592
|
+
(0, utils_1.rangeCheck)(pos, 0, this._length - 1);
|
|
593
|
+
let deleted;
|
|
594
|
+
if (pos === 0) {
|
|
595
|
+
//If it is the first element, use shift() directly
|
|
596
|
+
return this.shift();
|
|
597
|
+
}
|
|
598
|
+
else if (pos === this._length - 1) {
|
|
599
|
+
// If it is the last element, just use pop()
|
|
600
|
+
deleted = this.last;
|
|
519
601
|
this.pop();
|
|
602
|
+
return deleted;
|
|
603
|
+
}
|
|
520
604
|
else {
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
605
|
+
// Delete the middle element
|
|
606
|
+
const length = this._length - 1;
|
|
607
|
+
const { bucketIndex: targetBucket, indexInBucket: targetPointer } = this._getBucketAndPosition(pos);
|
|
608
|
+
deleted = this._buckets[targetBucket][targetPointer];
|
|
609
|
+
for (let i = pos; i < length; i++) {
|
|
610
|
+
const { bucketIndex: curBucket, indexInBucket: curPointer } = this._getBucketAndPosition(i);
|
|
611
|
+
const { bucketIndex: nextBucket, indexInBucket: nextPointer } = this._getBucketAndPosition(i + 1);
|
|
525
612
|
this._buckets[curBucket][curPointer] = this._buckets[nextBucket][nextPointer];
|
|
526
|
-
curBucket = nextBucket;
|
|
527
|
-
curPointer = nextPointer;
|
|
528
613
|
}
|
|
614
|
+
// Remove last duplicate element
|
|
529
615
|
this.pop();
|
|
616
|
+
return deleted;
|
|
530
617
|
}
|
|
531
|
-
return true;
|
|
532
618
|
}
|
|
533
619
|
/**
|
|
534
620
|
* Time Complexity: O(n)
|
|
@@ -541,7 +627,7 @@ class Deque extends base_1.IterableElementBase {
|
|
|
541
627
|
* @returns The size of the data structure after the element has been deleted.
|
|
542
628
|
*/
|
|
543
629
|
delete(element) {
|
|
544
|
-
const size = this.
|
|
630
|
+
const size = this._length;
|
|
545
631
|
if (size === 0)
|
|
546
632
|
return false;
|
|
547
633
|
let i = 0;
|
|
@@ -557,6 +643,42 @@ class Deque extends base_1.IterableElementBase {
|
|
|
557
643
|
this.cut(index - 1, true);
|
|
558
644
|
return true;
|
|
559
645
|
}
|
|
646
|
+
// /**
|
|
647
|
+
// * Time Complexity: O(n)
|
|
648
|
+
// * Space Complexity: O(1)
|
|
649
|
+
// *
|
|
650
|
+
// * This function overrides the indexOf method to search for an element within a custom data
|
|
651
|
+
// * structure.
|
|
652
|
+
// * @param {E} searchElement - The `searchElement` parameter is the element that you are searching for
|
|
653
|
+
// * within the data structure. The `indexOf` method will return the index of the first occurrence of
|
|
654
|
+
// * this element within the data structure.
|
|
655
|
+
// * @param {number} [fromIndex=0] - The `fromIndex` parameter in the `indexOf` method specifies the
|
|
656
|
+
// * index at which to start searching for the `searchElement` within the data structure. If provided,
|
|
657
|
+
// * the search will begin at this index instead of the beginning of the data structure.
|
|
658
|
+
// * @returns The indexOf method is returning the index of the searchElement if it is found in the data
|
|
659
|
+
// * structure, or -1 if the searchElement is not found.
|
|
660
|
+
// */
|
|
661
|
+
// override indexOf(searchElement: E, fromIndex: number = 0): number {
|
|
662
|
+
// let index = fromIndex;
|
|
663
|
+
// let bucketIndex = this._bucketFirst;
|
|
664
|
+
// let indexInBucket = this._firstInBucket + fromIndex;
|
|
665
|
+
//
|
|
666
|
+
// for (let i = 0; i < this._length; i++) {
|
|
667
|
+
// if (this._buckets[bucketIndex][indexInBucket] === searchElement) {
|
|
668
|
+
// return index;
|
|
669
|
+
// }
|
|
670
|
+
// index++;
|
|
671
|
+
// indexInBucket++;
|
|
672
|
+
// if (indexInBucket >= this._bucketSize) {
|
|
673
|
+
// bucketIndex++;
|
|
674
|
+
// indexInBucket = 0;
|
|
675
|
+
// }
|
|
676
|
+
// if (bucketIndex >= this._bucketCount) {
|
|
677
|
+
// bucketIndex = 0;
|
|
678
|
+
// }
|
|
679
|
+
// }
|
|
680
|
+
// return -1;
|
|
681
|
+
// }
|
|
560
682
|
/**
|
|
561
683
|
* Time Complexity: O(n)
|
|
562
684
|
* Space Complexity: O(1)
|
|
@@ -586,12 +708,12 @@ class Deque extends base_1.IterableElementBase {
|
|
|
586
708
|
* @returns The size of the modified array is being returned.
|
|
587
709
|
*/
|
|
588
710
|
unique() {
|
|
589
|
-
if (this.
|
|
711
|
+
if (this._length <= 1) {
|
|
590
712
|
return this;
|
|
591
713
|
}
|
|
592
714
|
let index = 1;
|
|
593
715
|
let prev = this.at(0);
|
|
594
|
-
for (let i = 1; i < this.
|
|
716
|
+
for (let i = 1; i < this._length; ++i) {
|
|
595
717
|
const cur = this.at(i);
|
|
596
718
|
if (cur !== prev) {
|
|
597
719
|
prev = cur;
|
|
@@ -601,27 +723,6 @@ class Deque extends base_1.IterableElementBase {
|
|
|
601
723
|
this.cut(index - 1, true);
|
|
602
724
|
return this;
|
|
603
725
|
}
|
|
604
|
-
/**
|
|
605
|
-
* Time Complexity: O(n log n)
|
|
606
|
-
* Space Complexity: O(n)
|
|
607
|
-
*
|
|
608
|
-
* The `sort` function sorts the elements in a data structure using a provided comparator function.
|
|
609
|
-
* @param [comparator] - The `comparator` parameter is a function that takes in two elements `x` and
|
|
610
|
-
* `y` of type `E` and returns a number. The comparator function is used to determine the order of
|
|
611
|
-
* the elements in the sorted array.
|
|
612
|
-
* @returns Deque<E>
|
|
613
|
-
*/
|
|
614
|
-
sort(comparator) {
|
|
615
|
-
const arr = [];
|
|
616
|
-
for (let i = 0; i < this._size; ++i) {
|
|
617
|
-
arr.push(this.at(i));
|
|
618
|
-
}
|
|
619
|
-
arr.sort(comparator);
|
|
620
|
-
for (let i = 0; i < this._size; ++i) {
|
|
621
|
-
this.setAt(i, arr[i]);
|
|
622
|
-
}
|
|
623
|
-
return this;
|
|
624
|
-
}
|
|
625
726
|
/**
|
|
626
727
|
* Time Complexity: O(n)
|
|
627
728
|
* Space Complexity: O(n)
|
|
@@ -629,10 +730,10 @@ class Deque extends base_1.IterableElementBase {
|
|
|
629
730
|
* The `shrinkToFit` function reorganizes the elements in an array-like data structure to minimize
|
|
630
731
|
* memory usage.
|
|
631
732
|
* @returns Nothing is being returned. The function is using the `return` statement to exit early if
|
|
632
|
-
* `this.
|
|
733
|
+
* `this._length` is 0, but it does not return any value.
|
|
633
734
|
*/
|
|
634
735
|
shrinkToFit() {
|
|
635
|
-
if (this.
|
|
736
|
+
if (this._length === 0)
|
|
636
737
|
return;
|
|
637
738
|
const newBuckets = [];
|
|
638
739
|
if (this._bucketFirst === this._bucketLast)
|
|
@@ -654,35 +755,6 @@ class Deque extends base_1.IterableElementBase {
|
|
|
654
755
|
this._bucketLast = newBuckets.length - 1;
|
|
655
756
|
this._buckets = newBuckets;
|
|
656
757
|
}
|
|
657
|
-
/**
|
|
658
|
-
* Time Complexity: O(n)
|
|
659
|
-
* Space Complexity: O(1)
|
|
660
|
-
*
|
|
661
|
-
* The function "indexOf" returns the index of the first occurrence of a given element in an array,
|
|
662
|
-
* or -1 if the element is not found.
|
|
663
|
-
* @param {E} element - The "element" parameter represents the element that you want to find the
|
|
664
|
-
* index of in the data structure.
|
|
665
|
-
* @returns The indexOf function returns the index of the first occurrence of the specified element
|
|
666
|
-
* in the data structure. If the element is not found, it returns -1.
|
|
667
|
-
*/
|
|
668
|
-
indexOf(element) {
|
|
669
|
-
for (let i = 0; i < this._size; ++i) {
|
|
670
|
-
if (this.at(i) === element) {
|
|
671
|
-
return i;
|
|
672
|
-
}
|
|
673
|
-
}
|
|
674
|
-
return -1;
|
|
675
|
-
}
|
|
676
|
-
/**
|
|
677
|
-
* Time Complexity: O(n)
|
|
678
|
-
* Space Complexity: O(n)
|
|
679
|
-
*
|
|
680
|
-
* The `toArray` function converts the elements of a data structure into an array.
|
|
681
|
-
* @returns The `toArray()` method is returning an array of elements of type `E`.
|
|
682
|
-
*/
|
|
683
|
-
toArray() {
|
|
684
|
-
return [...this];
|
|
685
|
-
}
|
|
686
758
|
/**
|
|
687
759
|
* Time Complexity: O(n)
|
|
688
760
|
* Space Complexity: O(n)
|
|
@@ -693,7 +765,11 @@ class Deque extends base_1.IterableElementBase {
|
|
|
693
765
|
* elements as the original deque (`this`) and the same bucket size.
|
|
694
766
|
*/
|
|
695
767
|
clone() {
|
|
696
|
-
return new Deque(this, {
|
|
768
|
+
return new Deque(this, {
|
|
769
|
+
bucketSize: this.bucketSize,
|
|
770
|
+
toElementFn: this.toElementFn,
|
|
771
|
+
maxLen: this._maxLen
|
|
772
|
+
});
|
|
697
773
|
}
|
|
698
774
|
/**
|
|
699
775
|
* Time Complexity: O(n)
|
|
@@ -712,7 +788,11 @@ class Deque extends base_1.IterableElementBase {
|
|
|
712
788
|
* satisfy the given predicate function.
|
|
713
789
|
*/
|
|
714
790
|
filter(predicate, thisArg) {
|
|
715
|
-
const newDeque =
|
|
791
|
+
const newDeque = this._createInstance({
|
|
792
|
+
bucketSize: this._bucketSize,
|
|
793
|
+
toElementFn: this.toElementFn,
|
|
794
|
+
maxLen: this._maxLen
|
|
795
|
+
});
|
|
716
796
|
let index = 0;
|
|
717
797
|
for (const el of this) {
|
|
718
798
|
if (predicate.call(thisArg, el, index, this)) {
|
|
@@ -741,7 +821,7 @@ class Deque extends base_1.IterableElementBase {
|
|
|
741
821
|
* @returns a new Deque object with elements of type EM and raw elements of type RM.
|
|
742
822
|
*/
|
|
743
823
|
map(callback, toElementFn, thisArg) {
|
|
744
|
-
const newDeque = new Deque([], { bucketSize: this._bucketSize, toElementFn });
|
|
824
|
+
const newDeque = new Deque([], { bucketSize: this._bucketSize, toElementFn, maxLen: this._maxLen });
|
|
745
825
|
let index = 0;
|
|
746
826
|
for (const el of this) {
|
|
747
827
|
newDeque.push(callback.call(thisArg, el, index, this));
|
|
@@ -757,7 +837,7 @@ class Deque extends base_1.IterableElementBase {
|
|
|
757
837
|
* object to be iterated over using a for...of loop.
|
|
758
838
|
*/
|
|
759
839
|
*_getIterator() {
|
|
760
|
-
for (let i = 0; i < this.
|
|
840
|
+
for (let i = 0; i < this._length; ++i) {
|
|
761
841
|
yield this.at(i);
|
|
762
842
|
}
|
|
763
843
|
}
|
|
@@ -814,5 +894,25 @@ class Deque extends base_1.IterableElementBase {
|
|
|
814
894
|
}
|
|
815
895
|
return { bucketIndex, indexInBucket };
|
|
816
896
|
}
|
|
897
|
+
/**
|
|
898
|
+
* The function `_createInstance` returns a new instance of the `Deque` class with the specified
|
|
899
|
+
* options.
|
|
900
|
+
* @param [options] - The `options` parameter in the `_createInstance` method is of type
|
|
901
|
+
* `DequeOptions<E, R>`, which is an optional parameter that allows you to pass additional
|
|
902
|
+
* configuration options when creating a new instance of the `Deque` class.
|
|
903
|
+
* @returns An instance of the `Deque` class with an empty array and the provided options, casted as
|
|
904
|
+
* `this`.
|
|
905
|
+
*/
|
|
906
|
+
_createInstance(options) {
|
|
907
|
+
return new Deque([], options);
|
|
908
|
+
}
|
|
909
|
+
/**
|
|
910
|
+
* This function returns an iterator that iterates over elements in reverse order.
|
|
911
|
+
*/
|
|
912
|
+
*_getReverseIterator() {
|
|
913
|
+
for (let i = this._length - 1; i > -1; i--) {
|
|
914
|
+
yield this.at(i);
|
|
915
|
+
}
|
|
916
|
+
}
|
|
817
917
|
}
|
|
818
918
|
exports.Deque = Deque;
|