data-structure-typed 1.54.3 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (208) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/benchmark/report.html +26 -2
  3. package/benchmark/report.json +292 -42
  4. package/dist/cjs/data-structures/base/iterable-element-base.d.ts +14 -40
  5. package/dist/cjs/data-structures/base/iterable-element-base.js +14 -11
  6. package/dist/cjs/data-structures/base/iterable-element-base.js.map +1 -1
  7. package/dist/cjs/data-structures/base/linear-base.d.ts +277 -0
  8. package/dist/cjs/data-structures/base/linear-base.js +553 -0
  9. package/dist/cjs/data-structures/base/linear-base.js.map +1 -0
  10. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +12 -8
  11. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +50 -37
  12. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
  13. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +64 -0
  14. package/dist/cjs/data-structures/binary-tree/avl-tree.js +64 -0
  15. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  16. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +62 -0
  17. package/dist/cjs/data-structures/binary-tree/binary-tree.js +67 -5
  18. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  19. package/dist/cjs/data-structures/binary-tree/bst.d.ts +3 -3
  20. package/dist/cjs/data-structures/binary-tree/bst.js +14 -14
  21. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  22. package/dist/cjs/data-structures/binary-tree/red-black-tree.d.ts +1 -7
  23. package/dist/cjs/data-structures/binary-tree/red-black-tree.js +1 -7
  24. package/dist/cjs/data-structures/binary-tree/red-black-tree.js.map +1 -1
  25. package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +175 -14
  26. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +210 -40
  27. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
  28. package/dist/cjs/data-structures/graph/abstract-graph.js +16 -16
  29. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  30. package/dist/cjs/data-structures/hash/hash-map.d.ts +46 -0
  31. package/dist/cjs/data-structures/hash/hash-map.js +46 -0
  32. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  33. package/dist/cjs/data-structures/heap/heap.d.ts +3 -11
  34. package/dist/cjs/data-structures/heap/heap.js +0 -10
  35. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  36. package/dist/cjs/data-structures/heap/max-heap.d.ts +2 -2
  37. package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
  38. package/dist/cjs/data-structures/heap/min-heap.d.ts +2 -2
  39. package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
  40. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +65 -94
  41. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +131 -146
  42. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  43. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +145 -75
  44. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +283 -169
  45. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  46. package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -2
  47. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  48. package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -2
  49. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  50. package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +2 -2
  51. package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
  52. package/dist/cjs/data-structures/queue/deque.d.ts +130 -91
  53. package/dist/cjs/data-structures/queue/deque.js +269 -169
  54. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  55. package/dist/cjs/data-structures/queue/queue.d.ts +131 -40
  56. package/dist/cjs/data-structures/queue/queue.js +181 -50
  57. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  58. package/dist/cjs/data-structures/stack/stack.d.ts +124 -11
  59. package/dist/cjs/data-structures/stack/stack.js +121 -10
  60. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  61. package/dist/cjs/data-structures/trie/trie.d.ts +4 -3
  62. package/dist/cjs/data-structures/trie/trie.js +3 -0
  63. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  64. package/dist/cjs/types/data-structures/base/base.d.ts +9 -4
  65. package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
  66. package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
  67. package/dist/cjs/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
  68. package/dist/cjs/types/data-structures/linked-list/singly-linked-list.d.ts +2 -2
  69. package/dist/cjs/types/data-structures/queue/deque.d.ts +2 -3
  70. package/dist/cjs/types/data-structures/queue/queue.d.ts +2 -2
  71. package/dist/esm/data-structures/base/iterable-element-base.d.ts +14 -40
  72. package/dist/esm/data-structures/base/iterable-element-base.js +14 -11
  73. package/dist/esm/data-structures/base/iterable-element-base.js.map +1 -1
  74. package/dist/esm/data-structures/base/linear-base.d.ts +277 -0
  75. package/dist/esm/data-structures/base/linear-base.js +549 -0
  76. package/dist/esm/data-structures/base/linear-base.js.map +1 -0
  77. package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.d.ts +12 -8
  78. package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js +50 -36
  79. package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
  80. package/dist/esm/data-structures/binary-tree/avl-tree.d.ts +64 -0
  81. package/dist/esm/data-structures/binary-tree/avl-tree.js +64 -0
  82. package/dist/esm/data-structures/binary-tree/avl-tree.js.map +1 -1
  83. package/dist/esm/data-structures/binary-tree/binary-tree.d.ts +62 -0
  84. package/dist/esm/data-structures/binary-tree/binary-tree.js +67 -5
  85. package/dist/esm/data-structures/binary-tree/binary-tree.js.map +1 -1
  86. package/dist/esm/data-structures/binary-tree/bst.d.ts +3 -3
  87. package/dist/esm/data-structures/binary-tree/bst.js +14 -12
  88. package/dist/esm/data-structures/binary-tree/bst.js.map +1 -1
  89. package/dist/esm/data-structures/binary-tree/red-black-tree.d.ts +1 -7
  90. package/dist/esm/data-structures/binary-tree/red-black-tree.js +1 -7
  91. package/dist/esm/data-structures/binary-tree/red-black-tree.js.map +1 -1
  92. package/dist/esm/data-structures/binary-tree/tree-multi-map.d.ts +175 -14
  93. package/dist/esm/data-structures/binary-tree/tree-multi-map.js +210 -39
  94. package/dist/esm/data-structures/binary-tree/tree-multi-map.js.map +1 -1
  95. package/dist/esm/data-structures/graph/abstract-graph.js +16 -16
  96. package/dist/esm/data-structures/graph/abstract-graph.js.map +1 -1
  97. package/dist/esm/data-structures/hash/hash-map.d.ts +46 -0
  98. package/dist/esm/data-structures/hash/hash-map.js +46 -0
  99. package/dist/esm/data-structures/hash/hash-map.js.map +1 -1
  100. package/dist/esm/data-structures/heap/heap.d.ts +3 -11
  101. package/dist/esm/data-structures/heap/heap.js +0 -10
  102. package/dist/esm/data-structures/heap/heap.js.map +1 -1
  103. package/dist/esm/data-structures/heap/max-heap.d.ts +2 -2
  104. package/dist/esm/data-structures/heap/max-heap.js.map +1 -1
  105. package/dist/esm/data-structures/heap/min-heap.d.ts +2 -2
  106. package/dist/esm/data-structures/heap/min-heap.js.map +1 -1
  107. package/dist/esm/data-structures/linked-list/doubly-linked-list.d.ts +65 -94
  108. package/dist/esm/data-structures/linked-list/doubly-linked-list.js +132 -148
  109. package/dist/esm/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  110. package/dist/esm/data-structures/linked-list/singly-linked-list.d.ts +145 -75
  111. package/dist/esm/data-structures/linked-list/singly-linked-list.js +283 -170
  112. package/dist/esm/data-structures/linked-list/singly-linked-list.js.map +1 -1
  113. package/dist/esm/data-structures/priority-queue/max-priority-queue.d.ts +2 -2
  114. package/dist/esm/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  115. package/dist/esm/data-structures/priority-queue/min-priority-queue.d.ts +2 -2
  116. package/dist/esm/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  117. package/dist/esm/data-structures/priority-queue/priority-queue.d.ts +2 -2
  118. package/dist/esm/data-structures/priority-queue/priority-queue.js.map +1 -1
  119. package/dist/esm/data-structures/queue/deque.d.ts +130 -91
  120. package/dist/esm/data-structures/queue/deque.js +269 -169
  121. package/dist/esm/data-structures/queue/deque.js.map +1 -1
  122. package/dist/esm/data-structures/queue/queue.d.ts +131 -40
  123. package/dist/esm/data-structures/queue/queue.js +182 -51
  124. package/dist/esm/data-structures/queue/queue.js.map +1 -1
  125. package/dist/esm/data-structures/stack/stack.d.ts +124 -11
  126. package/dist/esm/data-structures/stack/stack.js +121 -10
  127. package/dist/esm/data-structures/stack/stack.js.map +1 -1
  128. package/dist/esm/data-structures/trie/trie.d.ts +4 -3
  129. package/dist/esm/data-structures/trie/trie.js +3 -0
  130. package/dist/esm/data-structures/trie/trie.js.map +1 -1
  131. package/dist/esm/types/data-structures/base/base.d.ts +9 -4
  132. package/dist/esm/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
  133. package/dist/esm/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
  134. package/dist/esm/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
  135. package/dist/esm/types/data-structures/linked-list/singly-linked-list.d.ts +2 -2
  136. package/dist/esm/types/data-structures/queue/deque.d.ts +2 -3
  137. package/dist/esm/types/data-structures/queue/queue.d.ts +2 -2
  138. package/dist/individuals/binary-tree/avl-tree-counter.mjs +4701 -0
  139. package/dist/individuals/binary-tree/avl-tree-multi-map.mjs +4514 -0
  140. package/dist/individuals/binary-tree/avl-tree.mjs +4321 -0
  141. package/dist/individuals/binary-tree/binary-tree.mjs +3097 -0
  142. package/dist/individuals/binary-tree/bst.mjs +3858 -0
  143. package/dist/individuals/binary-tree/red-black-tree.mjs +4391 -0
  144. package/dist/individuals/binary-tree/tree-counter.mjs +4806 -0
  145. package/dist/individuals/binary-tree/tree-multi-map.mjs +4582 -0
  146. package/dist/individuals/graph/directed-graph.mjs +2910 -0
  147. package/dist/individuals/graph/undirected-graph.mjs +2745 -0
  148. package/dist/individuals/hash/hash-map.mjs +1040 -0
  149. package/dist/individuals/heap/heap.mjs +909 -0
  150. package/dist/individuals/heap/max-heap.mjs +671 -0
  151. package/dist/individuals/heap/min-heap.mjs +659 -0
  152. package/dist/individuals/linked-list/doubly-linked-list.mjs +1495 -0
  153. package/dist/individuals/linked-list/singly-linked-list.mjs +1479 -0
  154. package/dist/individuals/priority-queue/max-priority-queue.mjs +768 -0
  155. package/dist/individuals/priority-queue/min-priority-queue.mjs +757 -0
  156. package/dist/individuals/priority-queue/priority-queue.mjs +670 -0
  157. package/dist/individuals/queue/deque.mjs +1262 -0
  158. package/dist/individuals/queue/queue.mjs +1865 -0
  159. package/dist/individuals/stack/stack.mjs +415 -0
  160. package/dist/individuals/trie/trie.mjs +687 -0
  161. package/dist/umd/data-structure-typed.js +1260 -649
  162. package/dist/umd/data-structure-typed.min.js +3 -3
  163. package/dist/umd/data-structure-typed.min.js.map +1 -1
  164. package/package.json +7 -7
  165. package/src/data-structures/base/iterable-element-base.ts +29 -20
  166. package/src/data-structures/base/linear-base.ts +649 -0
  167. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +51 -36
  168. package/src/data-structures/binary-tree/avl-tree.ts +64 -0
  169. package/src/data-structures/binary-tree/binary-tree.ts +5 -5
  170. package/src/data-structures/binary-tree/bst.ts +9 -9
  171. package/src/data-structures/binary-tree/tree-multi-map.ts +214 -40
  172. package/src/data-structures/graph/abstract-graph.ts +16 -16
  173. package/src/data-structures/hash/hash-map.ts +46 -0
  174. package/src/data-structures/heap/heap.ts +3 -14
  175. package/src/data-structures/heap/max-heap.ts +2 -2
  176. package/src/data-structures/heap/min-heap.ts +2 -2
  177. package/src/data-structures/linked-list/doubly-linked-list.ts +144 -160
  178. package/src/data-structures/linked-list/singly-linked-list.ts +307 -185
  179. package/src/data-structures/priority-queue/max-priority-queue.ts +2 -5
  180. package/src/data-structures/priority-queue/min-priority-queue.ts +2 -5
  181. package/src/data-structures/priority-queue/priority-queue.ts +2 -2
  182. package/src/data-structures/queue/deque.ts +286 -183
  183. package/src/data-structures/queue/queue.ts +196 -63
  184. package/src/data-structures/stack/stack.ts +124 -18
  185. package/src/data-structures/trie/trie.ts +7 -3
  186. package/src/types/data-structures/base/base.ts +17 -8
  187. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
  188. package/src/types/data-structures/binary-tree/tree-multi-map.ts +1 -1
  189. package/src/types/data-structures/linked-list/doubly-linked-list.ts +2 -2
  190. package/src/types/data-structures/linked-list/singly-linked-list.ts +2 -2
  191. package/src/types/data-structures/queue/deque.ts +2 -3
  192. package/src/types/data-structures/queue/queue.ts +2 -2
  193. package/test/integration/all-in-one.test.ts +1 -1
  194. package/test/integration/avl-tree.test.ts +1 -1
  195. package/test/integration/bst.test.ts +2 -2
  196. package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +168 -0
  197. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +15 -14
  198. package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +1 -1
  199. package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +165 -7
  200. package/test/unit/data-structures/graph/directed-graph.test.ts +37 -37
  201. package/test/unit/data-structures/graph/undirected-graph.test.ts +2 -2
  202. package/test/unit/data-structures/hash/hash-map.test.ts +135 -0
  203. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +135 -27
  204. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +183 -12
  205. package/test/unit/data-structures/queue/deque.test.ts +241 -60
  206. package/test/unit/data-structures/queue/queue.test.ts +332 -19
  207. package/test/unit/data-structures/stack/stack.test.ts +165 -0
  208. package/test/unit/unrestricted-interconversion.test.ts +1 -1
@@ -1,5 +1,5 @@
1
- import { IterableElementBase } from '../base';
2
1
  import { SinglyLinkedList } from '../linked-list';
2
+ import { LinearBase } from '../base/linear-base';
3
3
  /**
4
4
  * 1. First In, First Out (FIFO): The core feature of a queue is its first in, first out nature. The element added to the queue first will be the one to be removed first.
5
5
  * 2. Operations: The main operations include enqueue (adding an element to the end of the queue) and dequeue (removing and returning the element at the front of the queue). Typically, there is also a peek operation (looking at the front element without removing it).
@@ -8,8 +8,55 @@ import { SinglyLinkedList } from '../linked-list';
8
8
  * 5. Data Buffering: Acting as a buffer for data packets in network communication.
9
9
  * 6. Breadth-First Search (BFS): In traversal algorithms for graphs and trees, queues store elements that are to be visited.
10
10
  * 7. Real-time Queuing: Like queuing systems in banks or supermarkets.
11
+ * @example
12
+ * // Sliding Window using Queue
13
+ * const nums = [2, 3, 4, 1, 5];
14
+ * const k = 2;
15
+ * const queue = new Queue<number>();
16
+ *
17
+ * let maxSum = 0;
18
+ * let currentSum = 0;
19
+ *
20
+ * nums.forEach((num, i) => {
21
+ * queue.push(num);
22
+ * currentSum += num;
23
+ *
24
+ * if (queue.length > k) {
25
+ * currentSum -= queue.shift()!;
26
+ * }
27
+ *
28
+ * if (queue.length === k) {
29
+ * maxSum = Math.max(maxSum, currentSum);
30
+ * }
31
+ * });
32
+ *
33
+ * console.log(maxSum); // 7
34
+ * @example
35
+ * // Breadth-First Search (BFS) using Queue
36
+ * const graph: { [key in number]: number[] } = {
37
+ * 1: [2, 3],
38
+ * 2: [4, 5],
39
+ * 3: [],
40
+ * 4: [],
41
+ * 5: []
42
+ * };
43
+ *
44
+ * const queue = new Queue<number>();
45
+ * const visited: number[] = [];
46
+ *
47
+ * queue.push(1);
48
+ *
49
+ * while (!queue.isEmpty()) {
50
+ * const node = queue.shift()!;
51
+ * if (!visited.includes(node)) {
52
+ * visited.push(node);
53
+ * graph[node].forEach(neighbor => queue.push(neighbor));
54
+ * }
55
+ * }
56
+ *
57
+ * console.log(visited); // [1, 2, 3, 4, 5]
11
58
  */
12
- export class Queue extends IterableElementBase {
59
+ export class Queue extends LinearBase {
13
60
  constructor(elements = [], options) {
14
61
  super(options);
15
62
  if (options) {
@@ -19,38 +66,33 @@ export class Queue extends IterableElementBase {
19
66
  this.pushMany(elements);
20
67
  }
21
68
  _elements = [];
22
- /**
23
- * The elements function returns the elements of this set.
24
- * @return An array of the elements in the stack
25
- */
26
69
  get elements() {
27
70
  return this._elements;
28
71
  }
29
72
  _offset = 0;
30
- /**
31
- * The offset function returns the offset of the current page.
32
- * @return The value of the protected variable _offset
33
- */
34
73
  get offset() {
35
74
  return this._offset;
36
75
  }
37
- /**
38
- * The size function returns the number of elements in an array.
39
- * @returns {number} The size of the array, which is the difference between the length of the array and the offset.
40
- */
41
- get size() {
76
+ get length() {
42
77
  return this.elements.length - this.offset;
43
78
  }
79
+ _autoCompactRatio = 0.5;
80
+ get autoCompactRatio() {
81
+ return this._autoCompactRatio;
82
+ }
83
+ set autoCompactRatio(v) {
84
+ this._autoCompactRatio = v;
85
+ }
44
86
  /**
45
87
  * Time Complexity: O(1)
46
88
  * Space Complexity: O(1)
47
89
  *
48
90
  * The `first` function returns the first element of the array `_elements` if it exists, otherwise it returns `undefined`.
49
91
  * @returns The `get first()` method returns the first element of the data structure, represented by the `_elements` array at
50
- * the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
92
+ * the `_offset` index. If the data structure is empty (length is 0), it returns `undefined`.
51
93
  */
52
94
  get first() {
53
- return this.size > 0 ? this.elements[this.offset] : undefined;
95
+ return this.length > 0 ? this.elements[this.offset] : undefined;
54
96
  }
55
97
  /**
56
98
  * Time Complexity: O(1)
@@ -61,23 +103,7 @@ export class Queue extends IterableElementBase {
61
103
  * array is empty, it returns `undefined`.
62
104
  */
63
105
  get last() {
64
- return this.size > 0 ? this.elements[this.elements.length - 1] : undefined;
65
- }
66
- _autoCompactRatio = 0.5;
67
- /**
68
- * This function returns the value of the autoCompactRatio property.
69
- * @returns The `autoCompactRatio` property of the object, which is a number.
70
- */
71
- get autoCompactRatio() {
72
- return this._autoCompactRatio;
73
- }
74
- /**
75
- * The above function sets the autoCompactRatio property to a specified number in TypeScript.
76
- * @param {number} v - The parameter `v` represents the value that will be assigned to the
77
- * `_autoCompactRatio` property.
78
- */
79
- set autoCompactRatio(v) {
80
- this._autoCompactRatio = v;
106
+ return this.length > 0 ? this.elements[this.elements.length - 1] : undefined;
81
107
  }
82
108
  /**
83
109
  * Time Complexity: O(n)
@@ -102,6 +128,8 @@ export class Queue extends IterableElementBase {
102
128
  */
103
129
  push(element) {
104
130
  this.elements.push(element);
131
+ if (this._maxLen > 0 && this.length > this._maxLen)
132
+ this.shift();
105
133
  return true;
106
134
  }
107
135
  /**
@@ -134,7 +162,7 @@ export class Queue extends IterableElementBase {
134
162
  * @returns The function `shift()` returns either the first element in the queue or `undefined` if the queue is empty.
135
163
  */
136
164
  shift() {
137
- if (this.size === 0)
165
+ if (this.length === 0)
138
166
  return undefined;
139
167
  const first = this.first;
140
168
  this._offset += 1;
@@ -152,7 +180,7 @@ export class Queue extends IterableElementBase {
152
180
  */
153
181
  delete(element) {
154
182
  const index = this.elements.indexOf(element);
155
- return this.deleteAt(index);
183
+ return !!this.deleteAt(index);
156
184
  }
157
185
  /**
158
186
  * Time Complexity: O(n)
@@ -163,8 +191,9 @@ export class Queue extends IterableElementBase {
163
191
  * @return A boolean value
164
192
  */
165
193
  deleteAt(index) {
166
- const spliced = this.elements.splice(index, 1);
167
- return spliced.length === 1;
194
+ const deleted = this.elements[index];
195
+ this.elements.splice(index, 1);
196
+ return deleted;
168
197
  }
169
198
  /**
170
199
  * Time Complexity: O(1)
@@ -180,25 +209,68 @@ export class Queue extends IterableElementBase {
180
209
  at(index) {
181
210
  return this.elements[index + this._offset];
182
211
  }
212
+ /**
213
+ * Time Complexity: O(n)
214
+ * Space Complexity: O(1)
215
+ *
216
+ * The `reverse` function in TypeScript reverses the elements of an array starting from a specified
217
+ * offset.
218
+ * @returns The `reverse()` method is returning the modified object itself (`this`) after reversing
219
+ * the elements in the array and resetting the offset to 0.
220
+ */
221
+ reverse() {
222
+ this._elements = this.elements.slice(this.offset).reverse();
223
+ this._offset = 0;
224
+ return this;
225
+ }
226
+ /**
227
+ * Time Complexity: O(n)
228
+ * Space Complexity: O(1)
229
+ *
230
+ * The function `addAt` inserts a new element at a specified index in an array, returning true if
231
+ * successful and false if the index is out of bounds.
232
+ * @param {number} index - The `index` parameter represents the position at which the `newElement`
233
+ * should be added in the array.
234
+ * @param {E} newElement - The `newElement` parameter represents the element that you want to insert
235
+ * into the array at the specified index.
236
+ * @returns The `addAt` method returns a boolean value - `true` if the new element was successfully
237
+ * added at the specified index, and `false` if the index is out of bounds (less than 0 or greater
238
+ * than the length of the array).
239
+ */
240
+ addAt(index, newElement) {
241
+ if (index < 0 || index > this.length)
242
+ return false;
243
+ this._elements.splice(this.offset + index, 0, newElement);
244
+ return true;
245
+ }
183
246
  /**
184
247
  * Time Complexity: O(1)
185
248
  * Space Complexity: O(1)
186
249
  *
187
- * The function checks if a data structure is empty by comparing its size to zero.
188
- * @returns {boolean} A boolean value indicating whether the size of the object is 0 or not.
250
+ * The function `setAt` updates an element at a specified index in an array-like data structure.
251
+ * @param {number} index - The `index` parameter is a number that represents the position in the
252
+ * array where the new element will be set.
253
+ * @param {E} newElement - The `newElement` parameter represents the new value that you want to set
254
+ * at the specified index in the array.
255
+ * @returns The `setAt` method returns a boolean value - `true` if the element was successfully set
256
+ * at the specified index, and `false` if the index is out of bounds (less than 0 or greater than the
257
+ * length of the array).
189
258
  */
190
- isEmpty() {
191
- return this.size === 0;
259
+ setAt(index, newElement) {
260
+ if (index < 0 || index > this.length)
261
+ return false;
262
+ this._elements[this.offset + index] = newElement;
263
+ return true;
192
264
  }
193
265
  /**
194
266
  * Time Complexity: O(1)
195
- * Space Complexity: O(n)
267
+ * Space Complexity: O(1)
196
268
  *
197
- * The toArray() function returns an array of elements from the current offset to the end of the _elements array.
198
- * @returns An array of type E is being returned.
269
+ * The function checks if a data structure is empty by comparing its length to zero.
270
+ * @returns {boolean} A boolean value indicating whether the length of the object is 0 or not.
199
271
  */
200
- toArray() {
201
- return this.elements.slice(this.offset);
272
+ isEmpty() {
273
+ return this.length === 0;
202
274
  }
203
275
  /**
204
276
  * Time Complexity: O(1)
@@ -223,6 +295,34 @@ export class Queue extends IterableElementBase {
223
295
  this._offset = 0;
224
296
  return true;
225
297
  }
298
+ /**
299
+ * Time Complexity: O(n)
300
+ * Space Complexity: O(n)
301
+ *
302
+ * The function overrides the splice method to remove and insert elements in a queue-like data
303
+ * structure.
304
+ * @param {number} start - The `start` parameter in the `splice` method specifies the index at which
305
+ * to start changing the array. Items will be added or removed starting from this index.
306
+ * @param {number} [deleteCount=0] - The `deleteCount` parameter in the `splice` method specifies the
307
+ * number of elements to remove from the array starting at the specified `start` index. If
308
+ * `deleteCount` is not provided, it defaults to 0, meaning no elements will be removed but new
309
+ * elements can still be inserted at
310
+ * @param {E[]} items - The `items` parameter in the `splice` method represents the elements that
311
+ * will be added to the array at the specified `start` index. These elements will replace the
312
+ * existing elements starting from the `start` index for the `deleteCount` number of elements.
313
+ * @returns The `splice` method is returning the `removedQueue`, which is an instance of the same
314
+ * class as the original object.
315
+ */
316
+ splice(start, deleteCount = 0, ...items) {
317
+ const removedQueue = this._createInstance();
318
+ start = Math.max(0, Math.min(start, this.length));
319
+ deleteCount = Math.max(0, Math.min(deleteCount, this.length - start));
320
+ const globalStartIndex = this.offset + start;
321
+ const removedElements = this._elements.splice(globalStartIndex, deleteCount, ...items);
322
+ removedQueue.pushMany(removedElements);
323
+ this.compact();
324
+ return removedQueue;
325
+ }
226
326
  /**
227
327
  * Time Complexity: O(n)
228
328
  * Space Complexity: O(n)
@@ -231,7 +331,7 @@ export class Queue extends IterableElementBase {
231
331
  * @returns The `clone()` method is returning a new instance of the `Queue` class.
232
332
  */
233
333
  clone() {
234
- return new Queue(this.elements.slice(this.offset), { toElementFn: this.toElementFn });
334
+ return new Queue(this.elements.slice(this.offset), { toElementFn: this.toElementFn, maxLen: this._maxLen });
235
335
  }
236
336
  /**
237
337
  * Time Complexity: O(n)
@@ -250,7 +350,11 @@ export class Queue extends IterableElementBase {
250
350
  * satisfy the given predicate function.
251
351
  */
252
352
  filter(predicate, thisArg) {
253
- const newDeque = new Queue([], { toElementFn: this.toElementFn });
353
+ const newDeque = this._createInstance({
354
+ toElementFn: this._toElementFn,
355
+ autoCompactRatio: this._autoCompactRatio,
356
+ maxLen: this._maxLen
357
+ });
254
358
  let index = 0;
255
359
  for (const el of this) {
256
360
  if (predicate.call(thisArg, el, index, this)) {
@@ -279,7 +383,11 @@ export class Queue extends IterableElementBase {
279
383
  * callback function to each element in the original Queue object.
280
384
  */
281
385
  map(callback, toElementFn, thisArg) {
282
- const newDeque = new Queue([], { toElementFn });
386
+ const newDeque = new Queue([], {
387
+ toElementFn,
388
+ autoCompactRatio: this._autoCompactRatio,
389
+ maxLen: this._maxLen
390
+ });
283
391
  let index = 0;
284
392
  for (const el of this) {
285
393
  newDeque.push(callback.call(thisArg, el, index, this));
@@ -298,6 +406,29 @@ export class Queue extends IterableElementBase {
298
406
  yield item;
299
407
  }
300
408
  }
409
+ /**
410
+ * The function `_createInstance` returns a new instance of the `Queue` class with the specified
411
+ * options.
412
+ * @param [options] - The `options` parameter in the `_createInstance` method is of type
413
+ * `QueueOptions<E, R>`, which is used to configure the behavior of the queue being created. It
414
+ * allows you to specify settings or properties that can influence how the queue operates.
415
+ * @returns An instance of the `Queue` class with an empty array and the provided options is being
416
+ * returned.
417
+ */
418
+ _createInstance(options) {
419
+ return new Queue([], options);
420
+ }
421
+ /**
422
+ * The function `_getReverseIterator` returns an iterator that iterates over elements in reverse
423
+ * order.
424
+ */
425
+ *_getReverseIterator() {
426
+ for (let i = this.length - 1; i >= 0; i--) {
427
+ const cur = this.at(i); // `at()` handles the offset.
428
+ if (cur !== undefined)
429
+ yield cur;
430
+ }
431
+ }
301
432
  }
302
433
  /**
303
434
  * 1. First In, First Out (FIFO) Strategy: Like other queue implementations, LinkedListQueue follows the first in, first out principle, meaning the element that is added to the queue first will be the first to be removed.
@@ -315,7 +446,7 @@ export class LinkedListQueue extends SinglyLinkedList {
315
446
  * values as the original `LinkedListQueue`.
316
447
  */
317
448
  clone() {
318
- return new LinkedListQueue(this, { toElementFn: this.toElementFn });
449
+ return new LinkedListQueue(this, { toElementFn: this.toElementFn, maxLen: this._maxLen });
319
450
  }
320
451
  }
321
452
  //# sourceMappingURL=queue.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"queue.js","sourceRoot":"","sources":["../../../../src/data-structures/queue/queue.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAC9C,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAElD;;;;;;;;GAQG;AACH,MAAM,OAAO,KAAwB,SAAQ,mBAAsC;IACjF,YAAY,WAAsC,EAAE,EAAE,OAA4B;QAChF,KAAK,CAAC,OAAO,CAAC,CAAC;QAEf,IAAI,OAAO,EAAE,CAAC;YACZ,MAAM,EAAE,gBAAgB,GAAG,GAAG,EAAE,GAAG,OAAO,CAAC;YAC3C,IAAI,CAAC,iBAAiB,GAAG,gBAAgB,CAAC;QAC5C,CAAC;QAED,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;IAC1B,CAAC;IAES,SAAS,GAAQ,EAAE,CAAC;IAE9B;;;OAGG;IACH,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,SAAS,CAAC;IACxB,CAAC;IAES,OAAO,GAAW,CAAC,CAAC;IAE9B;;;OAGG;IACH,IAAI,MAAM;QACR,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;IAED;;;OAGG;IACH,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;IAC5C,CAAC;IAED;;;;;;;OAOG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AS;gBAAE,MAAM,GAAG,CAAC;QACnC,CAAC;IACH,CAAC;CACF;AAED;;;;;GAKG;AACH,MAAM,OAAO,eAAkC,SAAQ,gBAAsB;IAC3E;;;;;;;OAOG;IACM,KAAK;QACZ,OAAO,IAAI,eAAe,CAAO,IAAI,EAAE,EAAE,WAAW,EAAE,IAAI,CAAC,WAAW,EAAE,MAAM,EAAE,IAAI,CAAC,OAAO,EAAE,CAAS,CAAC;IAC1G,CAAC;CACF"}
@@ -14,8 +14,129 @@ import { IterableElementBase } from '../base';
14
14
  * 4. Function Calls: In most modern programming languages, the records of function calls are managed through a stack. When a function is called, its record (including parameters, local variables, and return address) is 'pushed' into the stack. When the function returns, its record is 'popped' from the stack.
15
15
  * 5. Expression Evaluation: Used for the evaluation of arithmetic or logical expressions, especially when dealing with parenthesis matching and operator precedence.
16
16
  * 6. Backtracking Algorithms: In problems where multiple branches need to be explored but only one branch can be explored at a time, stacks can be used to save the state at each branching point.
17
+ * @example
18
+ * // Balanced Parentheses or Brackets
19
+ * type ValidCharacters = ')' | '(' | ']' | '[' | '}' | '{';
20
+ *
21
+ * const stack = new Stack<string>();
22
+ * const input: ValidCharacters[] = '[({})]'.split('') as ValidCharacters[];
23
+ * const matches: { [key in ValidCharacters]?: ValidCharacters } = { ')': '(', ']': '[', '}': '{' };
24
+ * for (const char of input) {
25
+ * if ('([{'.includes(char)) {
26
+ * stack.push(char);
27
+ * } else if (')]}'.includes(char)) {
28
+ * if (stack.pop() !== matches[char]) {
29
+ * fail('Parentheses are not balanced');
30
+ * }
31
+ * }
32
+ * }
33
+ * console.log(stack.isEmpty()); // true
34
+ * @example
35
+ * // Expression Evaluation and Conversion
36
+ * const stack = new Stack<number>();
37
+ * const expression = [5, 3, '+']; // Equivalent to 5 + 3
38
+ * expression.forEach(token => {
39
+ * if (typeof token === 'number') {
40
+ * stack.push(token);
41
+ * } else {
42
+ * const b = stack.pop()!;
43
+ * const a = stack.pop()!;
44
+ * stack.push(token === '+' ? a + b : 0); // Only handling '+' here
45
+ * }
46
+ * });
47
+ * console.log(stack.pop()); // 8
48
+ * @example
49
+ * // Depth-First Search (DFS)
50
+ * const stack = new Stack<number>();
51
+ * const graph: { [key in number]: number[] } = { 1: [2, 3], 2: [4], 3: [5], 4: [], 5: [] };
52
+ * const visited: number[] = [];
53
+ * stack.push(1);
54
+ * while (!stack.isEmpty()) {
55
+ * const node = stack.pop()!;
56
+ * if (!visited.includes(node)) {
57
+ * visited.push(node);
58
+ * graph[node].forEach(neighbor => stack.push(neighbor));
59
+ * }
60
+ * }
61
+ * console.log(visited); // [1, 3, 5, 2, 4]
62
+ * @example
63
+ * // Backtracking Algorithms
64
+ * const stack = new Stack<[number, number]>();
65
+ * const maze = [
66
+ * ['S', ' ', 'X'],
67
+ * ['X', ' ', 'X'],
68
+ * [' ', ' ', 'E']
69
+ * ];
70
+ * const start: [number, number] = [0, 0];
71
+ * const end = [2, 2];
72
+ * const directions = [
73
+ * [0, 1], // To the right
74
+ * [1, 0], // down
75
+ * [0, -1], // left
76
+ * [-1, 0] // up
77
+ * ];
78
+ *
79
+ * const visited = new Set<string>(); // Used to record visited nodes
80
+ * stack.push(start);
81
+ * const path: number[][] = [];
82
+ *
83
+ * while (!stack.isEmpty()) {
84
+ * const [x, y] = stack.pop()!;
85
+ * if (visited.has(`${x},${y}`)) continue; // Skip already visited nodes
86
+ * visited.add(`${x},${y}`);
87
+ *
88
+ * path.push([x, y]);
89
+ *
90
+ * if (x === end[0] && y === end[1]) {
91
+ * break; // Find the end point and exit
92
+ * }
93
+ *
94
+ * for (const [dx, dy] of directions) {
95
+ * const nx = x + dx;
96
+ * const ny = y + dy;
97
+ * if (
98
+ * maze[nx]?.[ny] === ' ' || // feasible path
99
+ * maze[nx]?.[ny] === 'E' // destination
100
+ * ) {
101
+ * stack.push([nx, ny]);
102
+ * }
103
+ * }
104
+ * }
105
+ *
106
+ * expect(path).toContainEqual(end);
107
+ * @example
108
+ * // Function Call Stack
109
+ * const functionStack = new Stack<string>();
110
+ * functionStack.push('main');
111
+ * functionStack.push('foo');
112
+ * functionStack.push('bar');
113
+ * console.log(functionStack.pop()); // 'bar'
114
+ * console.log(functionStack.pop()); // 'foo'
115
+ * console.log(functionStack.pop()); // 'main'
116
+ * @example
117
+ * // Simplify File Paths
118
+ * const stack = new Stack<string>();
119
+ * const path = '/a/./b/../../c';
120
+ * path.split('/').forEach(segment => {
121
+ * if (segment === '..') stack.pop();
122
+ * else if (segment && segment !== '.') stack.push(segment);
123
+ * });
124
+ * console.log(stack.elements.join('/')); // 'c'
125
+ * @example
126
+ * // Stock Span Problem
127
+ * const stack = new Stack<number>();
128
+ * const prices = [100, 80, 60, 70, 60, 75, 85];
129
+ * const spans: number[] = [];
130
+ * prices.forEach((price, i) => {
131
+ * while (!stack.isEmpty() && prices[stack.peek()!] <= price) {
132
+ * stack.pop();
133
+ * }
134
+ * spans.push(stack.isEmpty() ? i + 1 : i - stack.peek()!);
135
+ * stack.push(i);
136
+ * });
137
+ * console.log(spans); // [1, 1, 1, 2, 1, 4, 6]
17
138
  */
18
- export declare class Stack<E = any, R = any> extends IterableElementBase<E, R, Stack<E, R>> {
139
+ export declare class Stack<E = any, R = any> extends IterableElementBase<E, R> {
19
140
  constructor(elements?: Iterable<E> | Iterable<R>, options?: StackOptions<E, R>);
20
141
  protected _elements: E[];
21
142
  /**
@@ -102,14 +223,6 @@ export declare class Stack<E = any, R = any> extends IterableElementBase<E, R, S
102
223
  * @returns An array of type E.
103
224
  */
104
225
  deleteAt(index: number): boolean;
105
- /**
106
- * Time Complexity: O(n)
107
- * Space Complexity: O(n)
108
- *
109
- * The toArray function returns a copy of the elements in an array.
110
- * @returns An array of type E.
111
- */
112
- toArray(): E[];
113
226
  /**
114
227
  * Time Complexity: O(1)
115
228
  * Space Complexity: O(1)
@@ -141,7 +254,7 @@ export declare class Stack<E = any, R = any> extends IterableElementBase<E, R, S
141
254
  * @returns The `filter` method is returning a new `Stack` object that contains the elements that
142
255
  * satisfy the given predicate function.
143
256
  */
144
- filter(predicate: ElementCallback<E, R, boolean, Stack<E, R>>, thisArg?: any): Stack<E, R>;
257
+ filter(predicate: ElementCallback<E, R, boolean>, thisArg?: any): Stack<E, R>;
145
258
  /**
146
259
  * Time Complexity: O(n)
147
260
  * Space Complexity: O(n)
@@ -159,7 +272,7 @@ export declare class Stack<E = any, R = any> extends IterableElementBase<E, R, S
159
272
  * value of
160
273
  * @returns a new Stack object with elements of type EM and raw elements of type RM.
161
274
  */
162
- map<EM, RM>(callback: ElementCallback<E, R, EM, Stack<E, R>>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): Stack<EM, RM>;
275
+ map<EM, RM>(callback: ElementCallback<E, R, EM>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): Stack<EM, RM>;
163
276
  /**
164
277
  * Time Complexity: O(n)
165
278
  * Space Complexity: O(n)