linked-list-typed 1.53.6 → 1.53.8

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.
Files changed (62) hide show
  1. package/README.md +6 -6
  2. package/dist/common/index.d.ts +12 -0
  3. package/dist/common/index.js +28 -0
  4. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
  5. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +9 -12
  6. package/dist/data-structures/binary-tree/avl-tree.js +2 -2
  7. package/dist/data-structures/binary-tree/binary-tree.d.ts +55 -20
  8. package/dist/data-structures/binary-tree/binary-tree.js +102 -68
  9. package/dist/data-structures/binary-tree/bst.d.ts +131 -37
  10. package/dist/data-structures/binary-tree/bst.js +222 -69
  11. package/dist/data-structures/binary-tree/index.d.ts +1 -1
  12. package/dist/data-structures/binary-tree/index.js +1 -1
  13. package/dist/data-structures/binary-tree/{rb-tree.d.ts → red-black-tree.d.ts} +53 -0
  14. package/dist/data-structures/binary-tree/{rb-tree.js → red-black-tree.js} +56 -3
  15. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
  16. package/dist/data-structures/binary-tree/tree-multi-map.js +7 -7
  17. package/dist/data-structures/hash/hash-map.d.ts +30 -0
  18. package/dist/data-structures/hash/hash-map.js +30 -0
  19. package/dist/data-structures/heap/heap.d.ts +26 -9
  20. package/dist/data-structures/heap/heap.js +37 -17
  21. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +54 -9
  22. package/dist/data-structures/linked-list/doubly-linked-list.js +80 -19
  23. package/dist/data-structures/linked-list/singly-linked-list.d.ts +35 -2
  24. package/dist/data-structures/linked-list/singly-linked-list.js +55 -11
  25. package/dist/data-structures/queue/deque.d.ts +37 -8
  26. package/dist/data-structures/queue/deque.js +73 -29
  27. package/dist/data-structures/queue/queue.d.ts +41 -1
  28. package/dist/data-structures/queue/queue.js +51 -9
  29. package/dist/data-structures/stack/stack.d.ts +27 -10
  30. package/dist/data-structures/stack/stack.js +39 -20
  31. package/dist/data-structures/trie/trie.d.ts +111 -6
  32. package/dist/data-structures/trie/trie.js +123 -14
  33. package/dist/index.d.ts +2 -1
  34. package/dist/index.js +2 -1
  35. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  36. package/dist/types/data-structures/binary-tree/bst.d.ts +3 -2
  37. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
  38. package/dist/types/utils/utils.d.ts +10 -6
  39. package/dist/utils/utils.js +4 -2
  40. package/package.json +2 -2
  41. package/src/common/index.ts +25 -0
  42. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +9 -11
  43. package/src/data-structures/binary-tree/avl-tree.ts +3 -2
  44. package/src/data-structures/binary-tree/binary-tree.ts +110 -66
  45. package/src/data-structures/binary-tree/bst.ts +232 -72
  46. package/src/data-structures/binary-tree/index.ts +1 -1
  47. package/src/data-structures/binary-tree/{rb-tree.ts → red-black-tree.ts} +56 -3
  48. package/src/data-structures/binary-tree/tree-multi-map.ts +6 -6
  49. package/src/data-structures/hash/hash-map.ts +30 -0
  50. package/src/data-structures/heap/heap.ts +72 -49
  51. package/src/data-structures/linked-list/doubly-linked-list.ts +173 -105
  52. package/src/data-structures/linked-list/singly-linked-list.ts +61 -11
  53. package/src/data-structures/queue/deque.ts +72 -28
  54. package/src/data-structures/queue/queue.ts +50 -7
  55. package/src/data-structures/stack/stack.ts +39 -20
  56. package/src/data-structures/trie/trie.ts +123 -13
  57. package/src/index.ts +2 -1
  58. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  59. package/src/types/data-structures/binary-tree/bst.ts +3 -2
  60. package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
  61. package/src/types/utils/utils.ts +16 -10
  62. package/src/utils/utils.ts +4 -2
@@ -64,13 +64,13 @@ class DoublyLinkedListNode {
64
64
  }
65
65
  exports.DoublyLinkedListNode = DoublyLinkedListNode;
66
66
  /**
67
- * 1. Node Structure: Each node contains three parts: a data field, a pointer (or reference) to the previous node, and a pointer to the next node. This structure allows traversal of the linked list in both directions.
67
+ *1. Node Structure: Each node contains three parts: a data field, a pointer (or reference) to the previous node, and a pointer to the next node. This structure allows traversal of the linked list in both directions.
68
68
  * 2. Bidirectional Traversal: Unlike singly linked lists, doubly linked lists can be easily traversed forwards or backwards. This makes insertions and deletions in the list more flexible and efficient.
69
69
  * 3. No Centralized Index: Unlike arrays, elements in a linked list are not stored contiguously, so there is no centralized index. Accessing elements in a linked list typically requires traversing from the head or tail node.
70
70
  * 4. High Efficiency in Insertion and Deletion: Adding or removing elements in a linked list does not require moving other elements, making these operations more efficient than in arrays.
71
71
  * @example
72
72
  * // text editor operation history
73
- * const actions = [
73
+ * const actions = [
74
74
  * { type: 'insert', content: 'first line of text' },
75
75
  * { type: 'insert', content: 'second line of text' },
76
76
  * { type: 'delete', content: 'delete the first line' }
@@ -82,7 +82,7 @@ exports.DoublyLinkedListNode = DoublyLinkedListNode;
82
82
  * console.log(editorHistory.last?.type); // 'insert'
83
83
  * @example
84
84
  * // Browser history
85
- * const browserHistory = new DoublyLinkedList<string>();
85
+ * const browserHistory = new DoublyLinkedList<string>();
86
86
  *
87
87
  * browserHistory.push('home page');
88
88
  * browserHistory.push('search page');
@@ -93,7 +93,7 @@ exports.DoublyLinkedListNode = DoublyLinkedListNode;
93
93
  * console.log(browserHistory.last); // 'search page'
94
94
  * @example
95
95
  * // Use DoublyLinkedList to implement music player
96
- * // Define the Song interface
96
+ * // Define the Song interface
97
97
  * interface Song {
98
98
  * title: string;
99
99
  * artist: string;
@@ -218,7 +218,7 @@ exports.DoublyLinkedListNode = DoublyLinkedListNode;
218
218
  * // ]
219
219
  * @example
220
220
  * // Use DoublyLinkedList to implement LRU cache
221
- * interface CacheEntry<K, V> {
221
+ * interface CacheEntry<K, V> {
222
222
  * key: K;
223
223
  * value: V;
224
224
  * }
@@ -380,7 +380,7 @@ exports.DoublyLinkedListNode = DoublyLinkedListNode;
380
380
  * console.log(cache.isEmpty); // true
381
381
  * @example
382
382
  * // finding lyrics by timestamp in Coldplay's "Fix You"
383
- * // Create a DoublyLinkedList to store song lyrics with timestamps
383
+ * // Create a DoublyLinkedList to store song lyrics with timestamps
384
384
  * const lyricsList = new DoublyLinkedList<{ time: number; text: string }>();
385
385
  *
386
386
  * // Detailed lyrics with precise timestamps (in milliseconds)
@@ -420,7 +420,7 @@ exports.DoublyLinkedListNode = DoublyLinkedListNode;
420
420
  * console.log(lateTimeLyric?.text); // 'And I will try to fix you'
421
421
  * @example
422
422
  * // cpu process schedules
423
- * class Process {
423
+ * class Process {
424
424
  * constructor(
425
425
  * public id: number,
426
426
  * public priority: number
@@ -496,20 +496,22 @@ exports.DoublyLinkedListNode = DoublyLinkedListNode;
496
496
  * console.log(scheduler.listProcesses()); // []
497
497
  */
498
498
  class DoublyLinkedList extends base_1.IterableElementBase {
499
+ /**
500
+ * This TypeScript constructor initializes a DoublyLinkedList with optional elements and options.
501
+ * @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the constructor is an
502
+ * iterable collection of elements of type `E` or `R`. It is used to initialize the DoublyLinkedList
503
+ * with the elements provided in the iterable. If no elements are provided, the default value is an
504
+ * empty iterable.
505
+ * @param [options] - The `options` parameter in the constructor is of type
506
+ * `DoublyLinkedListOptions<E, R>`. It is an optional parameter that allows you to pass additional
507
+ * configuration options to customize the behavior of the DoublyLinkedList.
508
+ */
499
509
  constructor(elements = [], options) {
500
510
  super(options);
501
511
  this._head = undefined;
502
512
  this._tail = undefined;
503
513
  this._size = 0;
504
- if (elements) {
505
- for (const el of elements) {
506
- if (this.toElementFn) {
507
- this.push(this.toElementFn(el));
508
- }
509
- else
510
- this.push(el);
511
- }
512
- }
514
+ this.pushMany(elements);
513
515
  }
514
516
  /**
515
517
  * The `head` function returns the first node of a doubly linked list.
@@ -663,6 +665,55 @@ class DoublyLinkedList extends base_1.IterableElementBase {
663
665
  this._size++;
664
666
  return true;
665
667
  }
668
+ /**
669
+ * Time Complexity: O(k)
670
+ * Space Complexity: O(k)
671
+ *
672
+ * The function `pushMany` iterates over elements and pushes them into a data structure, applying a
673
+ * transformation function if provided.
674
+ * @param {Iterable<E> | Iterable<R> | Iterable<DoublyLinkedListNode<E>>} elements - The `elements`
675
+ * parameter in the `pushMany` function can accept an iterable containing elements of type `E`, `R`,
676
+ * or `DoublyLinkedListNode<E>`. The function iterates over each element in the iterable and pushes
677
+ * it onto the linked list. If a transformation function `to
678
+ * @returns The `pushMany` function is returning an array of boolean values (`ans`) which indicate
679
+ * the success or failure of pushing each element into the data structure.
680
+ */
681
+ pushMany(elements) {
682
+ const ans = [];
683
+ for (const el of elements) {
684
+ if (this.toElementFn) {
685
+ ans.push(this.push(this.toElementFn(el)));
686
+ continue;
687
+ }
688
+ ans.push(this.push(el));
689
+ }
690
+ return ans;
691
+ }
692
+ /**
693
+ * Time Complexity: O(k)
694
+ * Space Complexity: O(k)
695
+ *
696
+ * The function `unshiftMany` iterates through a collection of elements and adds them to the
697
+ * beginning of a Doubly Linked List, returning an array of boolean values indicating the success of
698
+ * each insertion.
699
+ * @param {Iterable<E> | Iterable<R> | Iterable<DoublyLinkedListNode<E>>} elements - The `elements`
700
+ * parameter in the `unshiftMany` function can accept an iterable containing elements of type `E`,
701
+ * `R`, or `DoublyLinkedListNode<E>`. The function iterates over each element in the iterable and
702
+ * performs an `unshift` operation on the doubly linked list
703
+ * @returns The `unshiftMany` function returns an array of boolean values indicating the success of
704
+ * each unshift operation performed on the elements passed as input.
705
+ */
706
+ unshiftMany(elements) {
707
+ const ans = [];
708
+ for (const el of elements) {
709
+ if (this.toElementFn) {
710
+ ans.push(this.unshift(this.toElementFn(el)));
711
+ continue;
712
+ }
713
+ ans.push(this.unshift(el));
714
+ }
715
+ return ans;
716
+ }
666
717
  /**
667
718
  * Time Complexity: O(n)
668
719
  * Space Complexity: O(1)
@@ -784,7 +835,9 @@ class DoublyLinkedList extends base_1.IterableElementBase {
784
835
  * node was not found.
785
836
  */
786
837
  addBefore(existingElementOrNode, newElementOrNode) {
787
- const existingNode = this.getNode(existingElementOrNode);
838
+ const existingNode = this.isNode(existingElementOrNode)
839
+ ? existingElementOrNode
840
+ : this.getNode(existingElementOrNode);
788
841
  if (existingNode) {
789
842
  const newNode = this._ensureNode(newElementOrNode);
790
843
  newNode.prev = existingNode.prev;
@@ -818,7 +871,9 @@ class DoublyLinkedList extends base_1.IterableElementBase {
818
871
  * was not found in the linked list.
819
872
  */
820
873
  addAfter(existingElementOrNode, newElementOrNode) {
821
- const existingNode = this.getNode(existingElementOrNode);
874
+ const existingNode = this.isNode(existingElementOrNode)
875
+ ? existingElementOrNode
876
+ : this.getNode(existingElementOrNode);
822
877
  if (existingNode) {
823
878
  const newNode = this._ensureNode(newElementOrNode);
824
879
  newNode.next = existingNode.next;
@@ -956,7 +1011,7 @@ class DoublyLinkedList extends base_1.IterableElementBase {
956
1011
  * @returns The `get` method returns the value of the first node in the doubly linked list that
957
1012
  * satisfies the provided predicate function. If no such node is found, it returns `undefined`.
958
1013
  */
959
- get(elementNodeOrPredicate) {
1014
+ search(elementNodeOrPredicate) {
960
1015
  const predicate = this._ensurePredicate(elementNodeOrPredicate);
961
1016
  let current = this.head;
962
1017
  while (current) {
@@ -1110,6 +1165,12 @@ class DoublyLinkedList extends base_1.IterableElementBase {
1110
1165
  * Time Complexity: O(n)
1111
1166
  * Space Complexity: O(1)
1112
1167
  *
1168
+ * The function `countOccurrences` iterates through a doubly linked list and counts the occurrences
1169
+ * of a specified element or nodes that satisfy a given predicate.
1170
+ * @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementOrNode
1171
+ * - The `elementOrNode` parameter in the `countOccurrences` method can accept three types of values:
1172
+ * @returns The `countOccurrences` method returns the number of occurrences of the specified element,
1173
+ * node, or predicate function in the doubly linked list.
1113
1174
  */
1114
1175
  countOccurrences(elementOrNode) {
1115
1176
  const predicate = this._ensurePredicate(elementOrNode);
@@ -41,7 +41,7 @@ export declare class SinglyLinkedListNode<E = any> {
41
41
  set next(value: SinglyLinkedListNode<E> | undefined);
42
42
  }
43
43
  export declare class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R, SinglyLinkedList<E, R>> {
44
- constructor(elements?: Iterable<E> | Iterable<R>, options?: SinglyLinkedListOptions<E, R>);
44
+ constructor(elements?: Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>, options?: SinglyLinkedListOptions<E, R>);
45
45
  protected _head: SinglyLinkedListNode<E> | undefined;
46
46
  /**
47
47
  * The `head` function returns the first node of a singly linked list.
@@ -111,6 +111,33 @@ export declare class SinglyLinkedList<E = any, R = any> extends IterableElementB
111
111
  * @returns The `unshift` method is returning a boolean value, specifically `true`.
112
112
  */
113
113
  unshift(elementOrNode: E | SinglyLinkedListNode<E>): boolean;
114
+ /**
115
+ * Time Complexity: O(k)
116
+ * Space Complexity: O(k)
117
+ *
118
+ * The function `pushMany` iterates over elements and pushes them into a data structure, applying a
119
+ * transformation function if provided.
120
+ * @param {Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>} elements - The `elements`
121
+ * parameter in the `pushMany` function can accept an iterable containing elements of type `E`, `R`,
122
+ * or `SinglyLinkedListNode<E>`.
123
+ * @returns The `pushMany` function returns an array of boolean values indicating whether each
124
+ * element was successfully pushed into the data structure.
125
+ */
126
+ pushMany(elements: Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>): boolean[];
127
+ /**
128
+ * Time Complexity: O(k)
129
+ * Space Complexity: O(k)
130
+ *
131
+ * The function `unshiftMany` iterates over elements and adds them to a data structure, optionally
132
+ * converting them using a provided function.
133
+ * @param {Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>} elements - The `elements`
134
+ * parameter in the `unshiftMany` function can accept an iterable containing elements of type `E`,
135
+ * `R`, or `SinglyLinkedListNode<E>`. The function iterates over each element in the iterable and
136
+ * performs an `unshift` operation on the linked list for each
137
+ * @returns The `unshiftMany` function is returning an array of boolean values, where each value
138
+ * represents the result of calling the `unshift` method on the current instance of the class.
139
+ */
140
+ unshiftMany(elements: Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>): boolean[];
114
141
  /**
115
142
  * Time Complexity: O(n)
116
143
  * Space Complexity: O(1)
@@ -123,7 +150,7 @@ export declare class SinglyLinkedList<E = any, R = any> extends IterableElementB
123
150
  * @returns The `get` method returns the value of the first node in the singly linked list that
124
151
  * satisfies the provided predicate function. If no such node is found, it returns `undefined`.
125
152
  */
126
- get(elementNodeOrPredicate: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)): E | undefined;
153
+ search(elementNodeOrPredicate: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)): E | undefined;
127
154
  /**
128
155
  * Time Complexity: O(n)
129
156
  * Space Complexity: O(1)
@@ -199,12 +226,18 @@ export declare class SinglyLinkedList<E = any, R = any> extends IterableElementB
199
226
  */
200
227
  addAt(index: number, newElementOrNode: E | SinglyLinkedListNode<E>): boolean;
201
228
  /**
229
+ * Time Complexity: O(1)
230
+ * Space Complexity: O(1)
231
+ *
202
232
  * The function checks if the length of a data structure is equal to zero and returns a boolean value indicating
203
233
  * whether it is empty or not.
204
234
  * @returns A boolean value indicating whether the length of the object is equal to 0.
205
235
  */
206
236
  isEmpty(): boolean;
207
237
  /**
238
+ * Time Complexity: O(1)
239
+ * Space Complexity: O(1)
240
+ *
208
241
  * The `clear` function resets the linked list by setting the head, tail, and length to undefined and 0 respectively.
209
242
  */
210
243
  clear(): void;
@@ -49,16 +49,7 @@ class SinglyLinkedList extends base_1.IterableElementBase {
49
49
  constructor(elements = [], options) {
50
50
  super(options);
51
51
  this._size = 0;
52
- if (elements) {
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
- }
52
+ this.pushMany(elements);
62
53
  }
63
54
  /**
64
55
  * The `head` function returns the first node of a singly linked list.
@@ -188,6 +179,53 @@ class SinglyLinkedList extends base_1.IterableElementBase {
188
179
  this._size++;
189
180
  return true;
190
181
  }
182
+ /**
183
+ * Time Complexity: O(k)
184
+ * Space Complexity: O(k)
185
+ *
186
+ * The function `pushMany` iterates over elements and pushes them into a data structure, applying a
187
+ * transformation function if provided.
188
+ * @param {Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>} elements - The `elements`
189
+ * parameter in the `pushMany` function can accept an iterable containing elements of type `E`, `R`,
190
+ * or `SinglyLinkedListNode<E>`.
191
+ * @returns The `pushMany` function returns an array of boolean values indicating whether each
192
+ * element was successfully pushed into the data structure.
193
+ */
194
+ pushMany(elements) {
195
+ const ans = [];
196
+ for (const el of elements) {
197
+ if (this.toElementFn) {
198
+ ans.push(this.push(this.toElementFn(el)));
199
+ continue;
200
+ }
201
+ ans.push(this.push(el));
202
+ }
203
+ return ans;
204
+ }
205
+ /**
206
+ * Time Complexity: O(k)
207
+ * Space Complexity: O(k)
208
+ *
209
+ * The function `unshiftMany` iterates over elements and adds them to a data structure, optionally
210
+ * converting them using a provided function.
211
+ * @param {Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>} elements - The `elements`
212
+ * parameter in the `unshiftMany` function can accept an iterable containing elements of type `E`,
213
+ * `R`, or `SinglyLinkedListNode<E>`. The function iterates over each element in the iterable and
214
+ * performs an `unshift` operation on the linked list for each
215
+ * @returns The `unshiftMany` function is returning an array of boolean values, where each value
216
+ * represents the result of calling the `unshift` method on the current instance of the class.
217
+ */
218
+ unshiftMany(elements) {
219
+ const ans = [];
220
+ for (const el of elements) {
221
+ if (this.toElementFn) {
222
+ ans.push(this.unshift(this.toElementFn(el)));
223
+ continue;
224
+ }
225
+ ans.push(this.unshift(el));
226
+ }
227
+ return ans;
228
+ }
191
229
  /**
192
230
  * Time Complexity: O(n)
193
231
  * Space Complexity: O(1)
@@ -200,7 +238,7 @@ class SinglyLinkedList extends base_1.IterableElementBase {
200
238
  * @returns The `get` method returns the value of the first node in the singly linked list that
201
239
  * satisfies the provided predicate function. If no such node is found, it returns `undefined`.
202
240
  */
203
- get(elementNodeOrPredicate) {
241
+ search(elementNodeOrPredicate) {
204
242
  const predicate = this._ensurePredicate(elementNodeOrPredicate);
205
243
  let current = this.head;
206
244
  while (current) {
@@ -366,6 +404,9 @@ class SinglyLinkedList extends base_1.IterableElementBase {
366
404
  return true;
367
405
  }
368
406
  /**
407
+ * Time Complexity: O(1)
408
+ * Space Complexity: O(1)
409
+ *
369
410
  * The function checks if the length of a data structure is equal to zero and returns a boolean value indicating
370
411
  * whether it is empty or not.
371
412
  * @returns A boolean value indicating whether the length of the object is equal to 0.
@@ -374,6 +415,9 @@ class SinglyLinkedList extends base_1.IterableElementBase {
374
415
  return this._size === 0;
375
416
  }
376
417
  /**
418
+ * Time Complexity: O(1)
419
+ * Space Complexity: O(1)
420
+ *
377
421
  * The `clear` function resets the linked list by setting the head, tail, and length to undefined and 0 respectively.
378
422
  */
379
423
  clear() {
@@ -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(1)
131
- * Space Complexity: O(1)
140
+ * Time Complexity: O(k)
141
+ * Space Complexity: O(k)
132
142
  *
133
- * The `shift()` function removes and returns the first element from a data structure, updating the
134
- * internal state variables accordingly.
135
- * @returns The element that is being removed from the beginning of the data structure is being
136
- * returned.
137
- */
138
- shift(): E | undefined;
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
- for (const el of elements) {
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(1)
251
- * Space Complexity: O(1)
272
+ * Time Complexity: O(k)
273
+ * Space Complexity: O(k)
252
274
  *
253
- * The `shift()` function removes and returns the first element from a data structure, updating the
254
- * internal state variables accordingly.
255
- * @returns The element that is being removed from the beginning of the data structure is being
256
- * returned.
257
- */
258
- shift() {
259
- if (this._size === 0)
260
- return;
261
- const element = this._buckets[this._bucketFirst][this._firstInBucket];
262
- if (this._size !== 1) {
263
- if (this._firstInBucket < this._bucketSize - 1) {
264
- this._firstInBucket += 1;
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 if (this._bucketFirst < this._bucketCount - 1) {
267
- this._bucketFirst += 1;
268
- this._firstInBucket = 0;
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._bucketFirst = 0;
272
- this._firstInBucket = 0;
317
+ ans.push(this.unshift(el));
273
318
  }
274
319
  }
275
- this._size -= 1;
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
- * @param index
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
  /**