red-black-tree-typed 1.53.6 → 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.d.ts +12 -0
- package/dist/common/index.js +28 -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 -331
- package/dist/data-structures/binary-tree/avl-tree.d.ts +103 -69
- package/dist/data-structures/binary-tree/avl-tree.js +131 -71
- 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 +309 -208
- package/dist/data-structures/binary-tree/binary-tree.js +382 -300
- package/dist/data-structures/binary-tree/bst.d.ts +245 -127
- package/dist/data-structures/binary-tree/bst.js +366 -163
- 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} +181 -108
- 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 +26 -9
- package/dist/data-structures/heap/heap.js +37 -17
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +64 -19
- package/dist/data-structures/linked-list/doubly-linked-list.js +92 -31
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +48 -12
- package/dist/data-structures/linked-list/singly-linked-list.js +74 -27
- 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 +111 -10
- package/dist/data-structures/trie/trie.js +123 -18
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- 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 +1 -4
- package/dist/types/data-structures/binary-tree/bst.d.ts +6 -5
- 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/dist/types/utils/utils.d.ts +10 -6
- package/dist/utils/utils.js +4 -2
- package/package.json +2 -2
- package/src/common/index.ts +25 -0
- 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 +152 -373
- package/src/data-structures/binary-tree/avl-tree.ts +164 -106
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -0
- package/src/data-structures/binary-tree/binary-tree.ts +563 -447
- package/src/data-structures/binary-tree/bst.ts +433 -237
- package/src/data-structures/binary-tree/index.ts +3 -1
- package/src/data-structures/binary-tree/{rb-tree.ts → red-black-tree.ts} +224 -146
- 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 +72 -49
- package/src/data-structures/linked-list/doubly-linked-list.ts +186 -118
- package/src/data-structures/linked-list/singly-linked-list.ts +81 -28
- 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 +123 -17
- package/src/index.ts +4 -3
- 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 +1 -6
- package/src/types/data-structures/binary-tree/bst.ts +8 -7
- 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/src/types/utils/utils.ts +16 -10
- package/src/utils/utils.ts +4 -2
- package/dist/data-structures/binary-tree/rb-tree.d.ts +0 -205
- package/src/types/data-structures/binary-tree/rb-tree.ts +0 -10
|
@@ -59,18 +59,16 @@ export class SinglyLinkedListNode<E = any> {
|
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
+
/**
|
|
63
|
+
*
|
|
64
|
+
*/
|
|
62
65
|
export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R, SinglyLinkedList<E, R>> {
|
|
63
|
-
constructor(
|
|
66
|
+
constructor(
|
|
67
|
+
elements: Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>> = [],
|
|
68
|
+
options?: SinglyLinkedListOptions<E, R>
|
|
69
|
+
) {
|
|
64
70
|
super(options);
|
|
65
|
-
|
|
66
|
-
for (const el of elements) {
|
|
67
|
-
if (this.toElementFn) {
|
|
68
|
-
this.push(this.toElementFn(el as R));
|
|
69
|
-
} else {
|
|
70
|
-
this.push(el as E);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
71
|
+
this.pushMany(elements);
|
|
74
72
|
}
|
|
75
73
|
|
|
76
74
|
protected _head: SinglyLinkedListNode<E> | undefined;
|
|
@@ -121,6 +119,23 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
121
119
|
return this._size;
|
|
122
120
|
}
|
|
123
121
|
|
|
122
|
+
/**
|
|
123
|
+
* Time Complexity: O(n)
|
|
124
|
+
* Space Complexity: O(n)
|
|
125
|
+
*
|
|
126
|
+
* The `fromArray` function creates a new SinglyLinkedList instance and populates it with the elements from the given
|
|
127
|
+
* array.
|
|
128
|
+
* @param {E[]} data - The `data` parameter is an array of elements of type `E`.
|
|
129
|
+
* @returns The `fromArray` function returns a `SinglyLinkedList` object.
|
|
130
|
+
*/
|
|
131
|
+
static fromArray<E>(data: E[]) {
|
|
132
|
+
const singlyLinkedList = new SinglyLinkedList<E>();
|
|
133
|
+
for (const item of data) {
|
|
134
|
+
singlyLinkedList.push(item);
|
|
135
|
+
}
|
|
136
|
+
return singlyLinkedList;
|
|
137
|
+
}
|
|
138
|
+
|
|
124
139
|
/**
|
|
125
140
|
* Time Complexity: O(1)
|
|
126
141
|
* Space Complexity: O(1)
|
|
@@ -211,6 +226,55 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
211
226
|
return true;
|
|
212
227
|
}
|
|
213
228
|
|
|
229
|
+
/**
|
|
230
|
+
* Time Complexity: O(k)
|
|
231
|
+
* Space Complexity: O(k)
|
|
232
|
+
*
|
|
233
|
+
* The function `pushMany` iterates over elements and pushes them into a data structure, applying a
|
|
234
|
+
* transformation function if provided.
|
|
235
|
+
* @param {Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>} elements - The `elements`
|
|
236
|
+
* parameter in the `pushMany` function can accept an iterable containing elements of type `E`, `R`,
|
|
237
|
+
* or `SinglyLinkedListNode<E>`.
|
|
238
|
+
* @returns The `pushMany` function returns an array of boolean values indicating whether each
|
|
239
|
+
* element was successfully pushed into the data structure.
|
|
240
|
+
*/
|
|
241
|
+
pushMany(elements: Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>) {
|
|
242
|
+
const ans: boolean[] = [];
|
|
243
|
+
for (const el of elements) {
|
|
244
|
+
if (this.toElementFn) {
|
|
245
|
+
ans.push(this.push(this.toElementFn(el as R)));
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
248
|
+
ans.push(this.push(el as E | SinglyLinkedListNode<E>));
|
|
249
|
+
}
|
|
250
|
+
return ans;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Time Complexity: O(k)
|
|
255
|
+
* Space Complexity: O(k)
|
|
256
|
+
*
|
|
257
|
+
* The function `unshiftMany` iterates over elements and adds them to a data structure, optionally
|
|
258
|
+
* converting them using a provided function.
|
|
259
|
+
* @param {Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>} elements - The `elements`
|
|
260
|
+
* parameter in the `unshiftMany` function can accept an iterable containing elements of type `E`,
|
|
261
|
+
* `R`, or `SinglyLinkedListNode<E>`. The function iterates over each element in the iterable and
|
|
262
|
+
* performs an `unshift` operation on the linked list for each
|
|
263
|
+
* @returns The `unshiftMany` function is returning an array of boolean values, where each value
|
|
264
|
+
* represents the result of calling the `unshift` method on the current instance of the class.
|
|
265
|
+
*/
|
|
266
|
+
unshiftMany(elements: Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>) {
|
|
267
|
+
const ans: boolean[] = [];
|
|
268
|
+
for (const el of elements) {
|
|
269
|
+
if (this.toElementFn) {
|
|
270
|
+
ans.push(this.unshift(this.toElementFn(el as R)));
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
ans.push(this.unshift(el as E | SinglyLinkedListNode<E>));
|
|
274
|
+
}
|
|
275
|
+
return ans;
|
|
276
|
+
}
|
|
277
|
+
|
|
214
278
|
/**
|
|
215
279
|
* Time Complexity: O(n)
|
|
216
280
|
* Space Complexity: O(1)
|
|
@@ -223,7 +287,7 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
223
287
|
* @returns The `get` method returns the value of the first node in the singly linked list that
|
|
224
288
|
* satisfies the provided predicate function. If no such node is found, it returns `undefined`.
|
|
225
289
|
*/
|
|
226
|
-
|
|
290
|
+
search(
|
|
227
291
|
elementNodeOrPredicate: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)
|
|
228
292
|
): E | undefined {
|
|
229
293
|
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
@@ -399,6 +463,9 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
399
463
|
}
|
|
400
464
|
|
|
401
465
|
/**
|
|
466
|
+
* Time Complexity: O(1)
|
|
467
|
+
* Space Complexity: O(1)
|
|
468
|
+
*
|
|
402
469
|
* The function checks if the length of a data structure is equal to zero and returns a boolean value indicating
|
|
403
470
|
* whether it is empty or not.
|
|
404
471
|
* @returns A boolean value indicating whether the length of the object is equal to 0.
|
|
@@ -408,6 +475,9 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
408
475
|
}
|
|
409
476
|
|
|
410
477
|
/**
|
|
478
|
+
* Time Complexity: O(1)
|
|
479
|
+
* Space Complexity: O(1)
|
|
480
|
+
*
|
|
411
481
|
* The `clear` function resets the linked list by setting the head, tail, and length to undefined and 0 respectively.
|
|
412
482
|
*/
|
|
413
483
|
clear(): void {
|
|
@@ -713,23 +783,6 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
713
783
|
}
|
|
714
784
|
}
|
|
715
785
|
|
|
716
|
-
/**
|
|
717
|
-
* Time Complexity: O(n)
|
|
718
|
-
* Space Complexity: O(n)
|
|
719
|
-
*
|
|
720
|
-
* The `fromArray` function creates a new SinglyLinkedList instance and populates it with the elements from the given
|
|
721
|
-
* array.
|
|
722
|
-
* @param {E[]} data - The `data` parameter is an array of elements of type `E`.
|
|
723
|
-
* @returns The `fromArray` function returns a `SinglyLinkedList` object.
|
|
724
|
-
*/
|
|
725
|
-
static fromArray<E>(data: E[]) {
|
|
726
|
-
const singlyLinkedList = new SinglyLinkedList<E>();
|
|
727
|
-
for (const item of data) {
|
|
728
|
-
singlyLinkedList.push(item);
|
|
729
|
-
}
|
|
730
|
-
return singlyLinkedList;
|
|
731
|
-
}
|
|
732
|
-
|
|
733
786
|
/**
|
|
734
787
|
* The _isPredicate function in TypeScript checks if the input is a function that takes a
|
|
735
788
|
* SinglyLinkedListNode as an argument and returns a boolean.
|
|
@@ -8,6 +8,9 @@
|
|
|
8
8
|
import type { Comparator, ElementCallback, PriorityQueueOptions } from '../../types';
|
|
9
9
|
import { PriorityQueue } from './priority-queue';
|
|
10
10
|
|
|
11
|
+
/**
|
|
12
|
+
*
|
|
13
|
+
*/
|
|
11
14
|
export class MaxPriorityQueue<E = any, R = any> extends PriorityQueue<E, R> {
|
|
12
15
|
/**
|
|
13
16
|
* The constructor initializes a PriorityQueue with optional elements and options, including a
|
|
@@ -8,6 +8,9 @@
|
|
|
8
8
|
import type { Comparator, ElementCallback, PriorityQueueOptions } from '../../types';
|
|
9
9
|
import { PriorityQueue } from './priority-queue';
|
|
10
10
|
|
|
11
|
+
/**
|
|
12
|
+
*
|
|
13
|
+
*/
|
|
11
14
|
export class MinPriorityQueue<E = any, R = any> extends PriorityQueue<E, R> {
|
|
12
15
|
/**
|
|
13
16
|
* The constructor initializes a PriorityQueue with optional elements and options, including a
|
|
@@ -53,14 +53,7 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
|
|
|
53
53
|
const needBucketNum = calcMinUnitsRequired(_size, this._bucketSize);
|
|
54
54
|
this._bucketFirst = this._bucketLast = (this._bucketCount >> 1) - (needBucketNum >> 1);
|
|
55
55
|
this._firstInBucket = this._lastInBucket = (this._bucketSize - (_size % this._bucketSize)) >> 1;
|
|
56
|
-
|
|
57
|
-
for (const el of elements) {
|
|
58
|
-
if (this.toElementFn) {
|
|
59
|
-
this.push(this.toElementFn(el as R));
|
|
60
|
-
} else {
|
|
61
|
-
this.push(el as E);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
56
|
+
this.pushMany(elements);
|
|
64
57
|
}
|
|
65
58
|
|
|
66
59
|
protected _bucketSize: number = 1 << 12;
|
|
@@ -230,6 +223,33 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
|
|
|
230
223
|
return element;
|
|
231
224
|
}
|
|
232
225
|
|
|
226
|
+
/**
|
|
227
|
+
* Time Complexity: O(1)
|
|
228
|
+
* Space Complexity: O(1)
|
|
229
|
+
*
|
|
230
|
+
* The `shift()` function removes and returns the first element from a data structure, updating the
|
|
231
|
+
* internal state variables accordingly.
|
|
232
|
+
* @returns The element that is being removed from the beginning of the data structure is being
|
|
233
|
+
* returned.
|
|
234
|
+
*/
|
|
235
|
+
shift(): E | undefined {
|
|
236
|
+
if (this._size === 0) return;
|
|
237
|
+
const element = this._buckets[this._bucketFirst][this._firstInBucket];
|
|
238
|
+
if (this._size !== 1) {
|
|
239
|
+
if (this._firstInBucket < this._bucketSize - 1) {
|
|
240
|
+
this._firstInBucket += 1;
|
|
241
|
+
} else if (this._bucketFirst < this._bucketCount - 1) {
|
|
242
|
+
this._bucketFirst += 1;
|
|
243
|
+
this._firstInBucket = 0;
|
|
244
|
+
} else {
|
|
245
|
+
this._bucketFirst = 0;
|
|
246
|
+
this._firstInBucket = 0;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
this._size -= 1;
|
|
250
|
+
return element;
|
|
251
|
+
}
|
|
252
|
+
|
|
233
253
|
/**
|
|
234
254
|
* Time Complexity: Amortized O(1)
|
|
235
255
|
* Space Complexity: O(n)
|
|
@@ -260,30 +280,54 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
|
|
|
260
280
|
}
|
|
261
281
|
|
|
262
282
|
/**
|
|
263
|
-
* Time Complexity: O(
|
|
264
|
-
* Space Complexity: O(
|
|
283
|
+
* Time Complexity: O(k)
|
|
284
|
+
* Space Complexity: O(k)
|
|
265
285
|
*
|
|
266
|
-
* The `
|
|
267
|
-
*
|
|
268
|
-
* @
|
|
269
|
-
*
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
286
|
+
* The function `pushMany` iterates over elements and pushes them into an array after applying a
|
|
287
|
+
* transformation function if provided.
|
|
288
|
+
* @param {IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>} elements - The `elements`
|
|
289
|
+
* parameter in the `pushMany` function is expected to be an iterable containing elements of type `E`
|
|
290
|
+
* or `R`. It can be either an `IterableWithSizeOrLength<E>` or an `IterableWithSizeOrLength<R>`. The
|
|
291
|
+
* function iterates over each element
|
|
292
|
+
* @returns The `pushMany` function is returning an array of boolean values, where each value
|
|
293
|
+
* represents the result of calling the `push` method on the current object instance with the
|
|
294
|
+
* corresponding element from the input `elements` iterable.
|
|
295
|
+
*/
|
|
296
|
+
pushMany(elements: IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>) {
|
|
297
|
+
const ans: boolean[] = [];
|
|
298
|
+
for (const el of elements) {
|
|
299
|
+
if (this.toElementFn) {
|
|
300
|
+
ans.push(this.push(this.toElementFn(el as R)));
|
|
280
301
|
} else {
|
|
281
|
-
this.
|
|
282
|
-
this._firstInBucket = 0;
|
|
302
|
+
ans.push(this.push(el as E));
|
|
283
303
|
}
|
|
284
304
|
}
|
|
285
|
-
|
|
286
|
-
|
|
305
|
+
return ans;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Time Complexity: O(k)
|
|
310
|
+
* Space Complexity: O(k)
|
|
311
|
+
*
|
|
312
|
+
* The `unshiftMany` function in TypeScript iterates over elements and adds them to the beginning of
|
|
313
|
+
* an array, optionally converting them using a provided function.
|
|
314
|
+
* @param {IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>} elements - The `elements`
|
|
315
|
+
* parameter in the `unshiftMany` function is an iterable containing elements of type `E` or `R`. It
|
|
316
|
+
* can be an array or any other iterable data structure that has a known size or length. The function
|
|
317
|
+
* iterates over each element in the `elements` iterable and
|
|
318
|
+
* @returns The `unshiftMany` function returns an array of boolean values indicating whether each
|
|
319
|
+
* element was successfully added to the beginning of the array.
|
|
320
|
+
*/
|
|
321
|
+
unshiftMany(elements: IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R> = []) {
|
|
322
|
+
const ans: boolean[] = [];
|
|
323
|
+
for (const el of elements) {
|
|
324
|
+
if (this.toElementFn) {
|
|
325
|
+
ans.push(this.unshift(this.toElementFn(el as R)));
|
|
326
|
+
} else {
|
|
327
|
+
ans.push(this.unshift(el as E));
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
return ans;
|
|
287
331
|
}
|
|
288
332
|
|
|
289
333
|
/**
|
|
@@ -25,12 +25,7 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
|
|
|
25
25
|
this._autoCompactRatio = autoCompactRatio;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
for (const el of elements) {
|
|
30
|
-
if (this.toElementFn) this.push(this.toElementFn(el as R));
|
|
31
|
-
else this.push(el as E);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
28
|
+
this.pushMany(elements);
|
|
34
29
|
}
|
|
35
30
|
|
|
36
31
|
protected _elements: E[] = [];
|
|
@@ -131,6 +126,26 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
|
|
|
131
126
|
return true;
|
|
132
127
|
}
|
|
133
128
|
|
|
129
|
+
/**
|
|
130
|
+
* Time Complexity: O(k)
|
|
131
|
+
* Space Complexity: O(k)
|
|
132
|
+
*
|
|
133
|
+
* The `pushMany` function iterates over elements and pushes them into an array after applying a
|
|
134
|
+
* transformation function if provided.
|
|
135
|
+
* @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the `pushMany` function
|
|
136
|
+
* is an iterable containing elements of type `E` or `R`.
|
|
137
|
+
* @returns The `pushMany` function is returning an array of boolean values indicating whether each
|
|
138
|
+
* element was successfully pushed into the data structure.
|
|
139
|
+
*/
|
|
140
|
+
pushMany(elements: Iterable<E> | Iterable<R>) {
|
|
141
|
+
const ans: boolean[] = [];
|
|
142
|
+
for (const el of elements) {
|
|
143
|
+
if (this.toElementFn) ans.push(this.push(this.toElementFn(el as R)));
|
|
144
|
+
else ans.push(this.push(el as E));
|
|
145
|
+
}
|
|
146
|
+
return ans;
|
|
147
|
+
}
|
|
148
|
+
|
|
134
149
|
/**
|
|
135
150
|
* Time Complexity: O(1)
|
|
136
151
|
* Space Complexity: O(1)
|
|
@@ -150,6 +165,9 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
|
|
|
150
165
|
}
|
|
151
166
|
|
|
152
167
|
/**
|
|
168
|
+
* Time Complexity: O(n)
|
|
169
|
+
* Space Complexity: O(1)
|
|
170
|
+
*
|
|
153
171
|
* The delete function removes an element from the list.
|
|
154
172
|
* @param {E} element - Specify the element to be deleted
|
|
155
173
|
* @return A boolean value indicating whether the element was successfully deleted or not
|
|
@@ -160,6 +178,9 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
|
|
|
160
178
|
}
|
|
161
179
|
|
|
162
180
|
/**
|
|
181
|
+
* Time Complexity: O(n)
|
|
182
|
+
* Space Complexity: O(1)
|
|
183
|
+
*
|
|
163
184
|
* The deleteAt function deletes the element at a given index.
|
|
164
185
|
* @param {number} index - Determine the index of the element to be deleted
|
|
165
186
|
* @return A boolean value
|
|
@@ -173,7 +194,12 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
|
|
|
173
194
|
* Time Complexity: O(1)
|
|
174
195
|
* Space Complexity: O(1)
|
|
175
196
|
*
|
|
176
|
-
*
|
|
197
|
+
* The `at` function returns the element at a specified index adjusted by an offset, or `undefined`
|
|
198
|
+
* if the index is out of bounds.
|
|
199
|
+
* @param {number} index - The `index` parameter represents the position of the element you want to
|
|
200
|
+
* retrieve from the data structure.
|
|
201
|
+
* @returns The `at` method is returning the element at the specified index adjusted by the offset
|
|
202
|
+
* `_offset`.
|
|
177
203
|
*/
|
|
178
204
|
at(index: number): E | undefined {
|
|
179
205
|
return this.elements[index + this._offset];
|
|
@@ -213,6 +239,9 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
|
|
|
213
239
|
}
|
|
214
240
|
|
|
215
241
|
/**
|
|
242
|
+
* Time Complexity: O(n)
|
|
243
|
+
* Space Complexity: O(1)
|
|
244
|
+
*
|
|
216
245
|
* The `compact` function in TypeScript slices the elements array based on the offset and resets the
|
|
217
246
|
* offset to zero.
|
|
218
247
|
* @returns The `compact()` method is returning a boolean value of `true`.
|
|
@@ -265,6 +294,20 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
|
|
|
265
294
|
/**
|
|
266
295
|
* Time Complexity: O(n)
|
|
267
296
|
* Space Complexity: O(n)
|
|
297
|
+
*
|
|
298
|
+
* The `map` function in TypeScript creates a new Queue by applying a callback function to each
|
|
299
|
+
* element in the original Queue.
|
|
300
|
+
* @param callback - The `callback` parameter is a function that will be applied to each element in
|
|
301
|
+
* the queue. It takes the current element, its index, and the queue itself as arguments, and returns
|
|
302
|
+
* a new element.
|
|
303
|
+
* @param [toElementFn] - The `toElementFn` parameter is an optional function that can be provided to
|
|
304
|
+
* convert a raw element of type `RM` to a new element of type `EM`. This function is used within the
|
|
305
|
+
* `map` method to transform each raw element before passing it to the `callback` function. If
|
|
306
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `map` function is used to specify the
|
|
307
|
+
* value of `this` when executing the `callback` function. It allows you to set the context (the
|
|
308
|
+
* value of `this`) within the callback function. If `thisArg` is provided, it will be
|
|
309
|
+
* @returns A new Queue object containing elements of type EM, which are the result of applying the
|
|
310
|
+
* callback function to each element in the original Queue object.
|
|
268
311
|
*/
|
|
269
312
|
map<EM, RM>(
|
|
270
313
|
callback: ElementCallback<E, R, EM, Queue<E, R>>,
|
|
@@ -19,15 +19,7 @@ import { IterableElementBase } from '../base';
|
|
|
19
19
|
export class Stack<E = any, R = any> extends IterableElementBase<E, R, Stack<E, R>> {
|
|
20
20
|
constructor(elements: Iterable<E> | Iterable<R> = [], options?: StackOptions<E, R>) {
|
|
21
21
|
super(options);
|
|
22
|
-
|
|
23
|
-
for (const el of elements) {
|
|
24
|
-
if (this.toElementFn) {
|
|
25
|
-
this.push(this.toElementFn(el as R));
|
|
26
|
-
} else {
|
|
27
|
-
this.push(el as E);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
}
|
|
22
|
+
this.pushMany(elements);
|
|
31
23
|
}
|
|
32
24
|
|
|
33
25
|
protected _elements: E[] = [];
|
|
@@ -48,11 +40,6 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R, Stack<E,
|
|
|
48
40
|
return this.elements.length;
|
|
49
41
|
}
|
|
50
42
|
|
|
51
|
-
/**
|
|
52
|
-
* Time Complexity: O(n)
|
|
53
|
-
* Space Complexity: O(n)
|
|
54
|
-
*/
|
|
55
|
-
|
|
56
43
|
/**
|
|
57
44
|
* Time Complexity: O(n)
|
|
58
45
|
* Space Complexity: O(n)
|
|
@@ -67,6 +54,9 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R, Stack<E,
|
|
|
67
54
|
}
|
|
68
55
|
|
|
69
56
|
/**
|
|
57
|
+
* Time Complexity: O(1)
|
|
58
|
+
* Space Complexity: O(1)
|
|
59
|
+
*
|
|
70
60
|
* The function checks if an array is empty and returns a boolean value.
|
|
71
61
|
* @returns A boolean value indicating whether the `_elements` array is empty or not.
|
|
72
62
|
*/
|
|
@@ -115,9 +105,36 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R, Stack<E,
|
|
|
115
105
|
}
|
|
116
106
|
|
|
117
107
|
/**
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
*
|
|
108
|
+
* Time Complexity: O(k)
|
|
109
|
+
* Space Complexity: O(1)
|
|
110
|
+
*
|
|
111
|
+
* The function `pushMany` iterates over elements and pushes them into an array after applying a
|
|
112
|
+
* transformation function if provided.
|
|
113
|
+
* @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the `pushMany` function
|
|
114
|
+
* is an iterable containing elements of type `E` or `R`. The function iterates over each element in
|
|
115
|
+
* the iterable and pushes it into the data structure. If a transformation function `toElementFn` is
|
|
116
|
+
* provided, it is used to
|
|
117
|
+
* @returns The `pushMany` function is returning an array of boolean values indicating whether each
|
|
118
|
+
* element was successfully pushed into the data structure.
|
|
119
|
+
*/
|
|
120
|
+
pushMany(elements: Iterable<E> | Iterable<R>) {
|
|
121
|
+
const ans: boolean[] = [];
|
|
122
|
+
for (const el of elements) {
|
|
123
|
+
if (this.toElementFn) {
|
|
124
|
+
ans.push(this.push(this.toElementFn(el as R)));
|
|
125
|
+
} else {
|
|
126
|
+
ans.push(this.push(el as E));
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return ans;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Time Complexity: O(n)
|
|
134
|
+
* Space Complexity: O(1)
|
|
135
|
+
*
|
|
136
|
+
* The toArray function returns a copy of the elements in an array.
|
|
137
|
+
* @returns An array of type E.
|
|
121
138
|
*/
|
|
122
139
|
delete(element: E): boolean {
|
|
123
140
|
const index = this.elements.indexOf(element);
|
|
@@ -125,9 +142,11 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R, Stack<E,
|
|
|
125
142
|
}
|
|
126
143
|
|
|
127
144
|
/**
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
*
|
|
145
|
+
* Time Complexity: O(n)
|
|
146
|
+
* Space Complexity: O(1)
|
|
147
|
+
*
|
|
148
|
+
* The toArray function returns a copy of the elements in an array.
|
|
149
|
+
* @returns An array of type E.
|
|
131
150
|
*/
|
|
132
151
|
deleteAt(index: number): boolean {
|
|
133
152
|
const spliced = this.elements.splice(index, 1);
|