red-black-tree-typed 1.53.7 → 1.54.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/README.md +52 -0
- package/dist/common/index.js +5 -0
- package/dist/data-structures/base/iterable-entry-base.js +4 -4
- package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +213 -0
- package/dist/data-structures/binary-tree/avl-tree-counter.js +407 -0
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +71 -170
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +133 -328
- package/dist/data-structures/binary-tree/avl-tree.d.ts +103 -69
- package/dist/data-structures/binary-tree/avl-tree.js +130 -70
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +3 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +3 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +268 -202
- package/dist/data-structures/binary-tree/binary-tree.js +311 -263
- package/dist/data-structures/binary-tree/bst.d.ts +193 -139
- package/dist/data-structures/binary-tree/bst.js +248 -164
- package/dist/data-structures/binary-tree/index.d.ts +3 -1
- package/dist/data-structures/binary-tree/index.js +3 -1
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +286 -0
- package/dist/data-structures/binary-tree/{rb-tree.js → red-black-tree.js} +176 -107
- package/dist/data-structures/binary-tree/tree-counter.d.ts +212 -0
- package/dist/data-structures/binary-tree/tree-counter.js +444 -0
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +78 -170
- package/dist/data-structures/binary-tree/tree-multi-map.js +145 -367
- package/dist/data-structures/graph/abstract-graph.js +2 -2
- package/dist/data-structures/graph/directed-graph.d.ts +3 -0
- package/dist/data-structures/graph/directed-graph.js +3 -0
- package/dist/data-structures/graph/map-graph.d.ts +3 -0
- package/dist/data-structures/graph/map-graph.js +3 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +3 -0
- package/dist/data-structures/graph/undirected-graph.js +3 -0
- package/dist/data-structures/hash/hash-map.d.ts +31 -1
- package/dist/data-structures/hash/hash-map.js +35 -5
- package/dist/data-structures/heap/heap.d.ts +20 -3
- package/dist/data-structures/heap/heap.js +31 -11
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +46 -11
- package/dist/data-structures/linked-list/doubly-linked-list.js +68 -21
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +47 -11
- package/dist/data-structures/linked-list/singly-linked-list.js +73 -26
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +3 -0
- package/dist/data-structures/linked-list/skip-linked-list.js +3 -0
- package/dist/data-structures/matrix/matrix.d.ts +3 -0
- package/dist/data-structures/matrix/matrix.js +3 -0
- package/dist/data-structures/matrix/navigator.d.ts +3 -0
- package/dist/data-structures/matrix/navigator.js +3 -0
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +3 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +3 -0
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +3 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +3 -0
- package/dist/data-structures/queue/deque.d.ts +37 -8
- package/dist/data-structures/queue/deque.js +73 -29
- package/dist/data-structures/queue/queue.d.ts +41 -1
- package/dist/data-structures/queue/queue.js +51 -9
- package/dist/data-structures/stack/stack.d.ts +27 -10
- package/dist/data-structures/stack/stack.js +39 -20
- package/dist/data-structures/trie/trie.d.ts +8 -7
- package/dist/data-structures/trie/trie.js +8 -7
- package/dist/interfaces/binary-tree.d.ts +8 -8
- package/dist/types/data-structures/base/base.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/avl-tree-counter.js +2 -0
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -4
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -3
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -3
- package/dist/types/data-structures/binary-tree/bst.d.ts +4 -4
- package/dist/types/data-structures/binary-tree/index.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/index.js +2 -0
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +2 -5
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/tree-counter.js +2 -0
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -5
- package/package.json +2 -2
- package/src/common/index.ts +7 -1
- package/src/data-structures/base/iterable-entry-base.ts +4 -4
- package/src/data-structures/binary-tree/avl-tree-counter.ts +463 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +151 -370
- package/src/data-structures/binary-tree/avl-tree.ts +162 -105
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -0
- package/src/data-structures/binary-tree/binary-tree.ts +488 -416
- package/src/data-structures/binary-tree/bst.ts +326 -251
- package/src/data-structures/binary-tree/index.ts +3 -1
- package/src/data-structures/binary-tree/{rb-tree.ts → red-black-tree.ts} +219 -145
- package/src/data-structures/binary-tree/tree-counter.ts +504 -0
- package/src/data-structures/binary-tree/tree-multi-map.ts +159 -401
- package/src/data-structures/graph/abstract-graph.ts +2 -2
- package/src/data-structures/graph/directed-graph.ts +3 -0
- package/src/data-structures/graph/map-graph.ts +3 -0
- package/src/data-structures/graph/undirected-graph.ts +3 -0
- package/src/data-structures/hash/hash-map.ts +37 -7
- package/src/data-structures/heap/heap.ts +33 -10
- package/src/data-structures/linked-list/doubly-linked-list.ts +75 -21
- package/src/data-structures/linked-list/singly-linked-list.ts +80 -27
- package/src/data-structures/linked-list/skip-linked-list.ts +3 -0
- package/src/data-structures/matrix/matrix.ts +3 -0
- package/src/data-structures/matrix/navigator.ts +3 -0
- package/src/data-structures/priority-queue/max-priority-queue.ts +3 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +3 -0
- package/src/data-structures/queue/deque.ts +72 -28
- package/src/data-structures/queue/queue.ts +50 -7
- package/src/data-structures/stack/stack.ts +39 -20
- package/src/data-structures/trie/trie.ts +8 -7
- package/src/index.ts +2 -2
- package/src/interfaces/binary-tree.ts +10 -21
- package/src/types/data-structures/base/base.ts +1 -1
- package/src/types/data-structures/binary-tree/avl-tree-counter.ts +3 -0
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -6
- package/src/types/data-structures/binary-tree/avl-tree.ts +0 -5
- package/src/types/data-structures/binary-tree/binary-tree.ts +0 -5
- package/src/types/data-structures/binary-tree/bst.ts +6 -6
- package/src/types/data-structures/binary-tree/index.ts +3 -1
- package/src/types/data-structures/binary-tree/red-black-tree.ts +5 -0
- package/src/types/data-structures/binary-tree/tree-counter.ts +3 -0
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +2 -7
- package/dist/data-structures/binary-tree/rb-tree.d.ts +0 -209
- package/src/types/data-structures/binary-tree/rb-tree.ts +0 -10
|
@@ -45,20 +45,14 @@ class SinglyLinkedListNode {
|
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
exports.SinglyLinkedListNode = SinglyLinkedListNode;
|
|
48
|
+
/**
|
|
49
|
+
*
|
|
50
|
+
*/
|
|
48
51
|
class SinglyLinkedList extends base_1.IterableElementBase {
|
|
49
52
|
constructor(elements = [], options) {
|
|
50
53
|
super(options);
|
|
51
54
|
this._size = 0;
|
|
52
|
-
|
|
53
|
-
for (const el of elements) {
|
|
54
|
-
if (this.toElementFn) {
|
|
55
|
-
this.push(this.toElementFn(el));
|
|
56
|
-
}
|
|
57
|
-
else {
|
|
58
|
-
this.push(el);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}
|
|
55
|
+
this.pushMany(elements);
|
|
62
56
|
}
|
|
63
57
|
/**
|
|
64
58
|
* The `head` function returns the first node of a singly linked list.
|
|
@@ -99,6 +93,22 @@ class SinglyLinkedList extends base_1.IterableElementBase {
|
|
|
99
93
|
get size() {
|
|
100
94
|
return this._size;
|
|
101
95
|
}
|
|
96
|
+
/**
|
|
97
|
+
* Time Complexity: O(n)
|
|
98
|
+
* Space Complexity: O(n)
|
|
99
|
+
*
|
|
100
|
+
* The `fromArray` function creates a new SinglyLinkedList instance and populates it with the elements from the given
|
|
101
|
+
* array.
|
|
102
|
+
* @param {E[]} data - The `data` parameter is an array of elements of type `E`.
|
|
103
|
+
* @returns The `fromArray` function returns a `SinglyLinkedList` object.
|
|
104
|
+
*/
|
|
105
|
+
static fromArray(data) {
|
|
106
|
+
const singlyLinkedList = new SinglyLinkedList();
|
|
107
|
+
for (const item of data) {
|
|
108
|
+
singlyLinkedList.push(item);
|
|
109
|
+
}
|
|
110
|
+
return singlyLinkedList;
|
|
111
|
+
}
|
|
102
112
|
/**
|
|
103
113
|
* Time Complexity: O(1)
|
|
104
114
|
* Space Complexity: O(1)
|
|
@@ -188,6 +198,53 @@ class SinglyLinkedList extends base_1.IterableElementBase {
|
|
|
188
198
|
this._size++;
|
|
189
199
|
return true;
|
|
190
200
|
}
|
|
201
|
+
/**
|
|
202
|
+
* Time Complexity: O(k)
|
|
203
|
+
* Space Complexity: O(k)
|
|
204
|
+
*
|
|
205
|
+
* The function `pushMany` iterates over elements and pushes them into a data structure, applying a
|
|
206
|
+
* transformation function if provided.
|
|
207
|
+
* @param {Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>} elements - The `elements`
|
|
208
|
+
* parameter in the `pushMany` function can accept an iterable containing elements of type `E`, `R`,
|
|
209
|
+
* or `SinglyLinkedListNode<E>`.
|
|
210
|
+
* @returns The `pushMany` function returns an array of boolean values indicating whether each
|
|
211
|
+
* element was successfully pushed into the data structure.
|
|
212
|
+
*/
|
|
213
|
+
pushMany(elements) {
|
|
214
|
+
const ans = [];
|
|
215
|
+
for (const el of elements) {
|
|
216
|
+
if (this.toElementFn) {
|
|
217
|
+
ans.push(this.push(this.toElementFn(el)));
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
ans.push(this.push(el));
|
|
221
|
+
}
|
|
222
|
+
return ans;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Time Complexity: O(k)
|
|
226
|
+
* Space Complexity: O(k)
|
|
227
|
+
*
|
|
228
|
+
* The function `unshiftMany` iterates over elements and adds them to a data structure, optionally
|
|
229
|
+
* converting them using a provided function.
|
|
230
|
+
* @param {Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>} elements - The `elements`
|
|
231
|
+
* parameter in the `unshiftMany` function can accept an iterable containing elements of type `E`,
|
|
232
|
+
* `R`, or `SinglyLinkedListNode<E>`. The function iterates over each element in the iterable and
|
|
233
|
+
* performs an `unshift` operation on the linked list for each
|
|
234
|
+
* @returns The `unshiftMany` function is returning an array of boolean values, where each value
|
|
235
|
+
* represents the result of calling the `unshift` method on the current instance of the class.
|
|
236
|
+
*/
|
|
237
|
+
unshiftMany(elements) {
|
|
238
|
+
const ans = [];
|
|
239
|
+
for (const el of elements) {
|
|
240
|
+
if (this.toElementFn) {
|
|
241
|
+
ans.push(this.unshift(this.toElementFn(el)));
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
ans.push(this.unshift(el));
|
|
245
|
+
}
|
|
246
|
+
return ans;
|
|
247
|
+
}
|
|
191
248
|
/**
|
|
192
249
|
* Time Complexity: O(n)
|
|
193
250
|
* Space Complexity: O(1)
|
|
@@ -366,6 +423,9 @@ class SinglyLinkedList extends base_1.IterableElementBase {
|
|
|
366
423
|
return true;
|
|
367
424
|
}
|
|
368
425
|
/**
|
|
426
|
+
* Time Complexity: O(1)
|
|
427
|
+
* Space Complexity: O(1)
|
|
428
|
+
*
|
|
369
429
|
* The function checks if the length of a data structure is equal to zero and returns a boolean value indicating
|
|
370
430
|
* whether it is empty or not.
|
|
371
431
|
* @returns A boolean value indicating whether the length of the object is equal to 0.
|
|
@@ -374,6 +434,9 @@ class SinglyLinkedList extends base_1.IterableElementBase {
|
|
|
374
434
|
return this._size === 0;
|
|
375
435
|
}
|
|
376
436
|
/**
|
|
437
|
+
* Time Complexity: O(1)
|
|
438
|
+
* Space Complexity: O(1)
|
|
439
|
+
*
|
|
377
440
|
* The `clear` function resets the linked list by setting the head, tail, and length to undefined and 0 respectively.
|
|
378
441
|
*/
|
|
379
442
|
clear() {
|
|
@@ -646,22 +709,6 @@ class SinglyLinkedList extends base_1.IterableElementBase {
|
|
|
646
709
|
current = current.next;
|
|
647
710
|
}
|
|
648
711
|
}
|
|
649
|
-
/**
|
|
650
|
-
* Time Complexity: O(n)
|
|
651
|
-
* Space Complexity: O(n)
|
|
652
|
-
*
|
|
653
|
-
* The `fromArray` function creates a new SinglyLinkedList instance and populates it with the elements from the given
|
|
654
|
-
* array.
|
|
655
|
-
* @param {E[]} data - The `data` parameter is an array of elements of type `E`.
|
|
656
|
-
* @returns The `fromArray` function returns a `SinglyLinkedList` object.
|
|
657
|
-
*/
|
|
658
|
-
static fromArray(data) {
|
|
659
|
-
const singlyLinkedList = new SinglyLinkedList();
|
|
660
|
-
for (const item of data) {
|
|
661
|
-
singlyLinkedList.push(item);
|
|
662
|
-
}
|
|
663
|
-
return singlyLinkedList;
|
|
664
|
-
}
|
|
665
712
|
/**
|
|
666
713
|
* The _isPredicate function in TypeScript checks if the input is a function that takes a
|
|
667
714
|
* SinglyLinkedListNode as an argument and returns a boolean.
|
|
@@ -12,6 +12,9 @@ export declare class SkipListNode<K, V> {
|
|
|
12
12
|
forward: SkipListNode<K, V>[];
|
|
13
13
|
constructor(key: K, value: V, level: number);
|
|
14
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
*
|
|
17
|
+
*/
|
|
15
18
|
export declare class SkipList<K, V> {
|
|
16
19
|
/**
|
|
17
20
|
* The constructor function initializes a SkipLinkedList object with optional options and elements.
|
|
@@ -7,6 +7,9 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import type { Comparator, ElementCallback, PriorityQueueOptions } from '../../types';
|
|
9
9
|
import { PriorityQueue } from './priority-queue';
|
|
10
|
+
/**
|
|
11
|
+
*
|
|
12
|
+
*/
|
|
10
13
|
export declare class MaxPriorityQueue<E = any, R = any> extends PriorityQueue<E, R> {
|
|
11
14
|
/**
|
|
12
15
|
* The constructor initializes a PriorityQueue with optional elements and options, including a
|
|
@@ -2,6 +2,9 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.MaxPriorityQueue = void 0;
|
|
4
4
|
const priority_queue_1 = require("./priority-queue");
|
|
5
|
+
/**
|
|
6
|
+
*
|
|
7
|
+
*/
|
|
5
8
|
class MaxPriorityQueue extends priority_queue_1.PriorityQueue {
|
|
6
9
|
/**
|
|
7
10
|
* The constructor initializes a PriorityQueue with optional elements and options, including a
|
|
@@ -7,6 +7,9 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import type { Comparator, ElementCallback, PriorityQueueOptions } from '../../types';
|
|
9
9
|
import { PriorityQueue } from './priority-queue';
|
|
10
|
+
/**
|
|
11
|
+
*
|
|
12
|
+
*/
|
|
10
13
|
export declare class MinPriorityQueue<E = any, R = any> extends PriorityQueue<E, R> {
|
|
11
14
|
/**
|
|
12
15
|
* The constructor initializes a PriorityQueue with optional elements and options, including a
|
|
@@ -2,6 +2,9 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.MinPriorityQueue = void 0;
|
|
4
4
|
const priority_queue_1 = require("./priority-queue");
|
|
5
|
+
/**
|
|
6
|
+
*
|
|
7
|
+
*/
|
|
5
8
|
class MinPriorityQueue extends priority_queue_1.PriorityQueue {
|
|
6
9
|
/**
|
|
7
10
|
* The constructor initializes a PriorityQueue with optional elements and options, including a
|
|
@@ -115,6 +115,16 @@ export declare class Deque<E = any, R = any> extends IterableElementBase<E, R, D
|
|
|
115
115
|
* @returns The element that was removed from the data structure is being returned.
|
|
116
116
|
*/
|
|
117
117
|
pop(): E | undefined;
|
|
118
|
+
/**
|
|
119
|
+
* Time Complexity: O(1)
|
|
120
|
+
* Space Complexity: O(1)
|
|
121
|
+
*
|
|
122
|
+
* The `shift()` function removes and returns the first element from a data structure, updating the
|
|
123
|
+
* internal state variables accordingly.
|
|
124
|
+
* @returns The element that is being removed from the beginning of the data structure is being
|
|
125
|
+
* returned.
|
|
126
|
+
*/
|
|
127
|
+
shift(): E | undefined;
|
|
118
128
|
/**
|
|
119
129
|
* Time Complexity: Amortized O(1)
|
|
120
130
|
* Space Complexity: O(n)
|
|
@@ -127,15 +137,34 @@ export declare class Deque<E = any, R = any> extends IterableElementBase<E, R, D
|
|
|
127
137
|
*/
|
|
128
138
|
unshift(element: E): boolean;
|
|
129
139
|
/**
|
|
130
|
-
* Time Complexity: O(
|
|
131
|
-
* Space Complexity: O(
|
|
140
|
+
* Time Complexity: O(k)
|
|
141
|
+
* Space Complexity: O(k)
|
|
132
142
|
*
|
|
133
|
-
* The `
|
|
134
|
-
*
|
|
135
|
-
* @
|
|
136
|
-
*
|
|
137
|
-
|
|
138
|
-
|
|
143
|
+
* The function `pushMany` iterates over elements and pushes them into an array after applying a
|
|
144
|
+
* transformation function if provided.
|
|
145
|
+
* @param {IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>} elements - The `elements`
|
|
146
|
+
* parameter in the `pushMany` function is expected to be an iterable containing elements of type `E`
|
|
147
|
+
* or `R`. It can be either an `IterableWithSizeOrLength<E>` or an `IterableWithSizeOrLength<R>`. The
|
|
148
|
+
* function iterates over each element
|
|
149
|
+
* @returns The `pushMany` function is returning an array of boolean values, where each value
|
|
150
|
+
* represents the result of calling the `push` method on the current object instance with the
|
|
151
|
+
* corresponding element from the input `elements` iterable.
|
|
152
|
+
*/
|
|
153
|
+
pushMany(elements: IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>): boolean[];
|
|
154
|
+
/**
|
|
155
|
+
* Time Complexity: O(k)
|
|
156
|
+
* Space Complexity: O(k)
|
|
157
|
+
*
|
|
158
|
+
* The `unshiftMany` function in TypeScript iterates over elements and adds them to the beginning of
|
|
159
|
+
* an array, optionally converting them using a provided function.
|
|
160
|
+
* @param {IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>} elements - The `elements`
|
|
161
|
+
* parameter in the `unshiftMany` function is an iterable containing elements of type `E` or `R`. It
|
|
162
|
+
* can be an array or any other iterable data structure that has a known size or length. The function
|
|
163
|
+
* iterates over each element in the `elements` iterable and
|
|
164
|
+
* @returns The `unshiftMany` function returns an array of boolean values indicating whether each
|
|
165
|
+
* element was successfully added to the beginning of the array.
|
|
166
|
+
*/
|
|
167
|
+
unshiftMany(elements?: IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>): boolean[];
|
|
139
168
|
/**
|
|
140
169
|
* Time Complexity: O(1)
|
|
141
170
|
* Space Complexity: O(1)
|
|
@@ -60,14 +60,7 @@ class Deque extends base_1.IterableElementBase {
|
|
|
60
60
|
const needBucketNum = (0, utils_1.calcMinUnitsRequired)(_size, this._bucketSize);
|
|
61
61
|
this._bucketFirst = this._bucketLast = (this._bucketCount >> 1) - (needBucketNum >> 1);
|
|
62
62
|
this._firstInBucket = this._lastInBucket = (this._bucketSize - (_size % this._bucketSize)) >> 1;
|
|
63
|
-
|
|
64
|
-
if (this.toElementFn) {
|
|
65
|
-
this.push(this.toElementFn(el));
|
|
66
|
-
}
|
|
67
|
-
else {
|
|
68
|
-
this.push(el);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
63
|
+
this.pushMany(elements);
|
|
71
64
|
}
|
|
72
65
|
/**
|
|
73
66
|
* The bucketSize function returns the size of the bucket.
|
|
@@ -214,6 +207,35 @@ class Deque extends base_1.IterableElementBase {
|
|
|
214
207
|
this._size -= 1;
|
|
215
208
|
return element;
|
|
216
209
|
}
|
|
210
|
+
/**
|
|
211
|
+
* Time Complexity: O(1)
|
|
212
|
+
* Space Complexity: O(1)
|
|
213
|
+
*
|
|
214
|
+
* The `shift()` function removes and returns the first element from a data structure, updating the
|
|
215
|
+
* internal state variables accordingly.
|
|
216
|
+
* @returns The element that is being removed from the beginning of the data structure is being
|
|
217
|
+
* returned.
|
|
218
|
+
*/
|
|
219
|
+
shift() {
|
|
220
|
+
if (this._size === 0)
|
|
221
|
+
return;
|
|
222
|
+
const element = this._buckets[this._bucketFirst][this._firstInBucket];
|
|
223
|
+
if (this._size !== 1) {
|
|
224
|
+
if (this._firstInBucket < this._bucketSize - 1) {
|
|
225
|
+
this._firstInBucket += 1;
|
|
226
|
+
}
|
|
227
|
+
else if (this._bucketFirst < this._bucketCount - 1) {
|
|
228
|
+
this._bucketFirst += 1;
|
|
229
|
+
this._firstInBucket = 0;
|
|
230
|
+
}
|
|
231
|
+
else {
|
|
232
|
+
this._bucketFirst = 0;
|
|
233
|
+
this._firstInBucket = 0;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
this._size -= 1;
|
|
237
|
+
return element;
|
|
238
|
+
}
|
|
217
239
|
/**
|
|
218
240
|
* Time Complexity: Amortized O(1)
|
|
219
241
|
* Space Complexity: O(n)
|
|
@@ -247,33 +269,55 @@ class Deque extends base_1.IterableElementBase {
|
|
|
247
269
|
return true;
|
|
248
270
|
}
|
|
249
271
|
/**
|
|
250
|
-
* Time Complexity: O(
|
|
251
|
-
* Space Complexity: O(
|
|
272
|
+
* Time Complexity: O(k)
|
|
273
|
+
* Space Complexity: O(k)
|
|
252
274
|
*
|
|
253
|
-
* The `
|
|
254
|
-
*
|
|
255
|
-
* @
|
|
256
|
-
*
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
275
|
+
* The function `pushMany` iterates over elements and pushes them into an array after applying a
|
|
276
|
+
* transformation function if provided.
|
|
277
|
+
* @param {IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>} elements - The `elements`
|
|
278
|
+
* parameter in the `pushMany` function is expected to be an iterable containing elements of type `E`
|
|
279
|
+
* or `R`. It can be either an `IterableWithSizeOrLength<E>` or an `IterableWithSizeOrLength<R>`. The
|
|
280
|
+
* function iterates over each element
|
|
281
|
+
* @returns The `pushMany` function is returning an array of boolean values, where each value
|
|
282
|
+
* represents the result of calling the `push` method on the current object instance with the
|
|
283
|
+
* corresponding element from the input `elements` iterable.
|
|
284
|
+
*/
|
|
285
|
+
pushMany(elements) {
|
|
286
|
+
const ans = [];
|
|
287
|
+
for (const el of elements) {
|
|
288
|
+
if (this.toElementFn) {
|
|
289
|
+
ans.push(this.push(this.toElementFn(el)));
|
|
265
290
|
}
|
|
266
|
-
else
|
|
267
|
-
this.
|
|
268
|
-
|
|
291
|
+
else {
|
|
292
|
+
ans.push(this.push(el));
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
return ans;
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Time Complexity: O(k)
|
|
299
|
+
* Space Complexity: O(k)
|
|
300
|
+
*
|
|
301
|
+
* The `unshiftMany` function in TypeScript iterates over elements and adds them to the beginning of
|
|
302
|
+
* an array, optionally converting them using a provided function.
|
|
303
|
+
* @param {IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>} elements - The `elements`
|
|
304
|
+
* parameter in the `unshiftMany` function is an iterable containing elements of type `E` or `R`. It
|
|
305
|
+
* can be an array or any other iterable data structure that has a known size or length. The function
|
|
306
|
+
* iterates over each element in the `elements` iterable and
|
|
307
|
+
* @returns The `unshiftMany` function returns an array of boolean values indicating whether each
|
|
308
|
+
* element was successfully added to the beginning of the array.
|
|
309
|
+
*/
|
|
310
|
+
unshiftMany(elements = []) {
|
|
311
|
+
const ans = [];
|
|
312
|
+
for (const el of elements) {
|
|
313
|
+
if (this.toElementFn) {
|
|
314
|
+
ans.push(this.unshift(this.toElementFn(el)));
|
|
269
315
|
}
|
|
270
316
|
else {
|
|
271
|
-
this.
|
|
272
|
-
this._firstInBucket = 0;
|
|
317
|
+
ans.push(this.unshift(el));
|
|
273
318
|
}
|
|
274
319
|
}
|
|
275
|
-
|
|
276
|
-
return element;
|
|
320
|
+
return ans;
|
|
277
321
|
}
|
|
278
322
|
/**
|
|
279
323
|
* Time Complexity: O(1)
|
|
@@ -84,6 +84,18 @@ export declare class Queue<E = any, R = any> extends IterableElementBase<E, R, Q
|
|
|
84
84
|
* @returns Always returns true, indicating the element was successfully added.
|
|
85
85
|
*/
|
|
86
86
|
push(element: E): boolean;
|
|
87
|
+
/**
|
|
88
|
+
* Time Complexity: O(k)
|
|
89
|
+
* Space Complexity: O(k)
|
|
90
|
+
*
|
|
91
|
+
* The `pushMany` function iterates over elements and pushes them into an array after applying a
|
|
92
|
+
* transformation function if provided.
|
|
93
|
+
* @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the `pushMany` function
|
|
94
|
+
* is an iterable containing elements of type `E` or `R`.
|
|
95
|
+
* @returns The `pushMany` function is returning an array of boolean values indicating whether each
|
|
96
|
+
* element was successfully pushed into the data structure.
|
|
97
|
+
*/
|
|
98
|
+
pushMany(elements: Iterable<E> | Iterable<R>): boolean[];
|
|
87
99
|
/**
|
|
88
100
|
* Time Complexity: O(1)
|
|
89
101
|
* Space Complexity: O(1)
|
|
@@ -94,12 +106,18 @@ export declare class Queue<E = any, R = any> extends IterableElementBase<E, R, Q
|
|
|
94
106
|
*/
|
|
95
107
|
shift(): E | undefined;
|
|
96
108
|
/**
|
|
109
|
+
* Time Complexity: O(n)
|
|
110
|
+
* Space Complexity: O(1)
|
|
111
|
+
*
|
|
97
112
|
* The delete function removes an element from the list.
|
|
98
113
|
* @param {E} element - Specify the element to be deleted
|
|
99
114
|
* @return A boolean value indicating whether the element was successfully deleted or not
|
|
100
115
|
*/
|
|
101
116
|
delete(element: E): boolean;
|
|
102
117
|
/**
|
|
118
|
+
* Time Complexity: O(n)
|
|
119
|
+
* Space Complexity: O(1)
|
|
120
|
+
*
|
|
103
121
|
* The deleteAt function deletes the element at a given index.
|
|
104
122
|
* @param {number} index - Determine the index of the element to be deleted
|
|
105
123
|
* @return A boolean value
|
|
@@ -109,7 +127,12 @@ export declare class Queue<E = any, R = any> extends IterableElementBase<E, R, Q
|
|
|
109
127
|
* Time Complexity: O(1)
|
|
110
128
|
* Space Complexity: O(1)
|
|
111
129
|
*
|
|
112
|
-
*
|
|
130
|
+
* The `at` function returns the element at a specified index adjusted by an offset, or `undefined`
|
|
131
|
+
* if the index is out of bounds.
|
|
132
|
+
* @param {number} index - The `index` parameter represents the position of the element you want to
|
|
133
|
+
* retrieve from the data structure.
|
|
134
|
+
* @returns The `at` method is returning the element at the specified index adjusted by the offset
|
|
135
|
+
* `_offset`.
|
|
113
136
|
*/
|
|
114
137
|
at(index: number): E | undefined;
|
|
115
138
|
/**
|
|
@@ -136,6 +159,9 @@ export declare class Queue<E = any, R = any> extends IterableElementBase<E, R, Q
|
|
|
136
159
|
*/
|
|
137
160
|
clear(): void;
|
|
138
161
|
/**
|
|
162
|
+
* Time Complexity: O(n)
|
|
163
|
+
* Space Complexity: O(1)
|
|
164
|
+
*
|
|
139
165
|
* The `compact` function in TypeScript slices the elements array based on the offset and resets the
|
|
140
166
|
* offset to zero.
|
|
141
167
|
* @returns The `compact()` method is returning a boolean value of `true`.
|
|
@@ -169,6 +195,20 @@ export declare class Queue<E = any, R = any> extends IterableElementBase<E, R, Q
|
|
|
169
195
|
/**
|
|
170
196
|
* Time Complexity: O(n)
|
|
171
197
|
* Space Complexity: O(n)
|
|
198
|
+
*
|
|
199
|
+
* The `map` function in TypeScript creates a new Queue by applying a callback function to each
|
|
200
|
+
* element in the original Queue.
|
|
201
|
+
* @param callback - The `callback` parameter is a function that will be applied to each element in
|
|
202
|
+
* the queue. It takes the current element, its index, and the queue itself as arguments, and returns
|
|
203
|
+
* a new element.
|
|
204
|
+
* @param [toElementFn] - The `toElementFn` parameter is an optional function that can be provided to
|
|
205
|
+
* convert a raw element of type `RM` to a new element of type `EM`. This function is used within the
|
|
206
|
+
* `map` method to transform each raw element before passing it to the `callback` function. If
|
|
207
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `map` function is used to specify the
|
|
208
|
+
* value of `this` when executing the `callback` function. It allows you to set the context (the
|
|
209
|
+
* value of `this`) within the callback function. If `thisArg` is provided, it will be
|
|
210
|
+
* @returns A new Queue object containing elements of type EM, which are the result of applying the
|
|
211
|
+
* callback function to each element in the original Queue object.
|
|
172
212
|
*/
|
|
173
213
|
map<EM, RM>(callback: ElementCallback<E, R, EM, Queue<E, R>>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): Queue<EM, RM>;
|
|
174
214
|
/**
|
|
@@ -22,14 +22,7 @@ class Queue extends base_1.IterableElementBase {
|
|
|
22
22
|
const { autoCompactRatio = 0.5 } = options;
|
|
23
23
|
this._autoCompactRatio = autoCompactRatio;
|
|
24
24
|
}
|
|
25
|
-
|
|
26
|
-
for (const el of elements) {
|
|
27
|
-
if (this.toElementFn)
|
|
28
|
-
this.push(this.toElementFn(el));
|
|
29
|
-
else
|
|
30
|
-
this.push(el);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
25
|
+
this.pushMany(elements);
|
|
33
26
|
}
|
|
34
27
|
/**
|
|
35
28
|
* The elements function returns the elements of this set.
|
|
@@ -114,6 +107,27 @@ class Queue extends base_1.IterableElementBase {
|
|
|
114
107
|
this.elements.push(element);
|
|
115
108
|
return true;
|
|
116
109
|
}
|
|
110
|
+
/**
|
|
111
|
+
* Time Complexity: O(k)
|
|
112
|
+
* Space Complexity: O(k)
|
|
113
|
+
*
|
|
114
|
+
* The `pushMany` function iterates over elements and pushes them into an array after applying a
|
|
115
|
+
* transformation function if provided.
|
|
116
|
+
* @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the `pushMany` function
|
|
117
|
+
* is an iterable containing elements of type `E` or `R`.
|
|
118
|
+
* @returns The `pushMany` function is returning an array of boolean values indicating whether each
|
|
119
|
+
* element was successfully pushed into the data structure.
|
|
120
|
+
*/
|
|
121
|
+
pushMany(elements) {
|
|
122
|
+
const ans = [];
|
|
123
|
+
for (const el of elements) {
|
|
124
|
+
if (this.toElementFn)
|
|
125
|
+
ans.push(this.push(this.toElementFn(el)));
|
|
126
|
+
else
|
|
127
|
+
ans.push(this.push(el));
|
|
128
|
+
}
|
|
129
|
+
return ans;
|
|
130
|
+
}
|
|
117
131
|
/**
|
|
118
132
|
* Time Complexity: O(1)
|
|
119
133
|
* Space Complexity: O(1)
|
|
@@ -132,6 +146,9 @@ class Queue extends base_1.IterableElementBase {
|
|
|
132
146
|
return first;
|
|
133
147
|
}
|
|
134
148
|
/**
|
|
149
|
+
* Time Complexity: O(n)
|
|
150
|
+
* Space Complexity: O(1)
|
|
151
|
+
*
|
|
135
152
|
* The delete function removes an element from the list.
|
|
136
153
|
* @param {E} element - Specify the element to be deleted
|
|
137
154
|
* @return A boolean value indicating whether the element was successfully deleted or not
|
|
@@ -141,6 +158,9 @@ class Queue extends base_1.IterableElementBase {
|
|
|
141
158
|
return this.deleteAt(index);
|
|
142
159
|
}
|
|
143
160
|
/**
|
|
161
|
+
* Time Complexity: O(n)
|
|
162
|
+
* Space Complexity: O(1)
|
|
163
|
+
*
|
|
144
164
|
* The deleteAt function deletes the element at a given index.
|
|
145
165
|
* @param {number} index - Determine the index of the element to be deleted
|
|
146
166
|
* @return A boolean value
|
|
@@ -153,7 +173,12 @@ class Queue extends base_1.IterableElementBase {
|
|
|
153
173
|
* Time Complexity: O(1)
|
|
154
174
|
* Space Complexity: O(1)
|
|
155
175
|
*
|
|
156
|
-
*
|
|
176
|
+
* The `at` function returns the element at a specified index adjusted by an offset, or `undefined`
|
|
177
|
+
* if the index is out of bounds.
|
|
178
|
+
* @param {number} index - The `index` parameter represents the position of the element you want to
|
|
179
|
+
* retrieve from the data structure.
|
|
180
|
+
* @returns The `at` method is returning the element at the specified index adjusted by the offset
|
|
181
|
+
* `_offset`.
|
|
157
182
|
*/
|
|
158
183
|
at(index) {
|
|
159
184
|
return this.elements[index + this._offset];
|
|
@@ -189,6 +214,9 @@ class Queue extends base_1.IterableElementBase {
|
|
|
189
214
|
this._offset = 0;
|
|
190
215
|
}
|
|
191
216
|
/**
|
|
217
|
+
* Time Complexity: O(n)
|
|
218
|
+
* Space Complexity: O(1)
|
|
219
|
+
*
|
|
192
220
|
* The `compact` function in TypeScript slices the elements array based on the offset and resets the
|
|
193
221
|
* offset to zero.
|
|
194
222
|
* @returns The `compact()` method is returning a boolean value of `true`.
|
|
@@ -238,6 +266,20 @@ class Queue extends base_1.IterableElementBase {
|
|
|
238
266
|
/**
|
|
239
267
|
* Time Complexity: O(n)
|
|
240
268
|
* Space Complexity: O(n)
|
|
269
|
+
*
|
|
270
|
+
* The `map` function in TypeScript creates a new Queue by applying a callback function to each
|
|
271
|
+
* element in the original Queue.
|
|
272
|
+
* @param callback - The `callback` parameter is a function that will be applied to each element in
|
|
273
|
+
* the queue. It takes the current element, its index, and the queue itself as arguments, and returns
|
|
274
|
+
* a new element.
|
|
275
|
+
* @param [toElementFn] - The `toElementFn` parameter is an optional function that can be provided to
|
|
276
|
+
* convert a raw element of type `RM` to a new element of type `EM`. This function is used within the
|
|
277
|
+
* `map` method to transform each raw element before passing it to the `callback` function. If
|
|
278
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `map` function is used to specify the
|
|
279
|
+
* value of `this` when executing the `callback` function. It allows you to set the context (the
|
|
280
|
+
* value of `this`) within the callback function. If `thisArg` is provided, it will be
|
|
281
|
+
* @returns A new Queue object containing elements of type EM, which are the result of applying the
|
|
282
|
+
* callback function to each element in the original Queue object.
|
|
241
283
|
*/
|
|
242
284
|
map(callback, toElementFn, thisArg) {
|
|
243
285
|
const newDeque = new Queue([], { toElementFn });
|