data-structure-typed 1.49.0 → 1.49.2

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 (195) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +16 -16
  3. package/README_zh-CN.md +2 -2
  4. package/benchmark/report.html +46 -1
  5. package/benchmark/report.json +457 -22
  6. package/dist/cjs/data-structures/base/iterable-base.d.ts +11 -0
  7. package/dist/cjs/data-structures/base/iterable-base.js +21 -0
  8. package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
  9. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +10 -12
  10. package/dist/cjs/data-structures/binary-tree/avl-tree.js +3 -4
  11. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  12. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +58 -58
  13. package/dist/cjs/data-structures/binary-tree/binary-tree.js +6 -6
  14. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  15. package/dist/cjs/data-structures/binary-tree/bst.d.ts +15 -15
  16. package/dist/cjs/data-structures/binary-tree/bst.js +3 -3
  17. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  18. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +11 -11
  19. package/dist/cjs/data-structures/binary-tree/rb-tree.js +5 -6
  20. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  21. package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +14 -14
  22. package/dist/cjs/data-structures/binary-tree/tree-multimap.js +3 -3
  23. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
  24. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +9 -3
  25. package/dist/cjs/data-structures/graph/abstract-graph.js +27 -31
  26. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  27. package/dist/cjs/data-structures/graph/directed-graph.d.ts +8 -1
  28. package/dist/cjs/data-structures/graph/directed-graph.js +1 -8
  29. package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
  30. package/dist/cjs/data-structures/graph/map-graph.d.ts +1 -1
  31. package/dist/cjs/data-structures/graph/undirected-graph.d.ts +8 -1
  32. package/dist/cjs/data-structures/graph/undirected-graph.js +1 -8
  33. package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
  34. package/dist/cjs/data-structures/hash/hash-map.d.ts +22 -10
  35. package/dist/cjs/data-structures/hash/hash-map.js +28 -16
  36. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  37. package/dist/cjs/data-structures/hash/hash-table.d.ts +2 -2
  38. package/dist/cjs/data-structures/hash/hash-table.js.map +1 -1
  39. package/dist/cjs/data-structures/heap/heap.d.ts +20 -38
  40. package/dist/cjs/data-structures/heap/heap.js +22 -42
  41. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  42. package/dist/cjs/data-structures/heap/max-heap.d.ts +11 -1
  43. package/dist/cjs/data-structures/heap/max-heap.js +10 -7
  44. package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
  45. package/dist/cjs/data-structures/heap/min-heap.d.ts +11 -1
  46. package/dist/cjs/data-structures/heap/min-heap.js +10 -7
  47. package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
  48. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +95 -95
  49. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +132 -136
  50. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  51. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +18 -23
  52. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +42 -49
  53. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  54. package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +2 -2
  55. package/dist/cjs/data-structures/linked-list/skip-linked-list.js +2 -2
  56. package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
  57. package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +1 -1
  58. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +0 -7
  59. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  60. package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +1 -1
  61. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +0 -7
  62. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  63. package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +9 -1
  64. package/dist/cjs/data-structures/priority-queue/priority-queue.js +8 -7
  65. package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
  66. package/dist/cjs/data-structures/queue/deque.d.ts +76 -80
  67. package/dist/cjs/data-structures/queue/deque.js +106 -122
  68. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  69. package/dist/cjs/data-structures/queue/queue.d.ts +30 -16
  70. package/dist/cjs/data-structures/queue/queue.js +31 -24
  71. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  72. package/dist/cjs/data-structures/stack/stack.d.ts +17 -8
  73. package/dist/cjs/data-structures/stack/stack.js +9 -9
  74. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  75. package/dist/cjs/data-structures/trie/trie.d.ts +14 -5
  76. package/dist/cjs/data-structures/trie/trie.js +13 -13
  77. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  78. package/dist/cjs/interfaces/binary-tree.d.ts +4 -4
  79. package/dist/cjs/types/common.d.ts +32 -8
  80. package/dist/cjs/types/common.js +22 -1
  81. package/dist/cjs/types/common.js.map +1 -1
  82. package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -24
  83. package/dist/cjs/types/data-structures/binary-tree/binary-tree.js +0 -22
  84. package/dist/cjs/types/data-structures/binary-tree/binary-tree.js.map +1 -1
  85. package/dist/cjs/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
  86. package/dist/cjs/types/data-structures/binary-tree/tree-multimap.d.ts +1 -1
  87. package/dist/mjs/data-structures/base/iterable-base.d.ts +11 -0
  88. package/dist/mjs/data-structures/base/iterable-base.js +21 -0
  89. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +10 -12
  90. package/dist/mjs/data-structures/binary-tree/avl-tree.js +3 -4
  91. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +58 -58
  92. package/dist/mjs/data-structures/binary-tree/binary-tree.js +6 -6
  93. package/dist/mjs/data-structures/binary-tree/bst.d.ts +15 -15
  94. package/dist/mjs/data-structures/binary-tree/bst.js +3 -3
  95. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +11 -11
  96. package/dist/mjs/data-structures/binary-tree/rb-tree.js +5 -6
  97. package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +14 -14
  98. package/dist/mjs/data-structures/binary-tree/tree-multimap.js +3 -3
  99. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +9 -3
  100. package/dist/mjs/data-structures/graph/abstract-graph.js +27 -31
  101. package/dist/mjs/data-structures/graph/directed-graph.d.ts +8 -1
  102. package/dist/mjs/data-structures/graph/directed-graph.js +1 -8
  103. package/dist/mjs/data-structures/graph/map-graph.d.ts +1 -1
  104. package/dist/mjs/data-structures/graph/undirected-graph.d.ts +8 -1
  105. package/dist/mjs/data-structures/graph/undirected-graph.js +1 -8
  106. package/dist/mjs/data-structures/hash/hash-map.d.ts +22 -10
  107. package/dist/mjs/data-structures/hash/hash-map.js +27 -15
  108. package/dist/mjs/data-structures/hash/hash-table.d.ts +2 -2
  109. package/dist/mjs/data-structures/heap/heap.d.ts +20 -38
  110. package/dist/mjs/data-structures/heap/heap.js +23 -43
  111. package/dist/mjs/data-structures/heap/max-heap.d.ts +11 -1
  112. package/dist/mjs/data-structures/heap/max-heap.js +9 -6
  113. package/dist/mjs/data-structures/heap/min-heap.d.ts +11 -1
  114. package/dist/mjs/data-structures/heap/min-heap.js +9 -6
  115. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +95 -95
  116. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +132 -136
  117. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +18 -23
  118. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +43 -50
  119. package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +2 -2
  120. package/dist/mjs/data-structures/linked-list/skip-linked-list.js +2 -2
  121. package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +1 -1
  122. package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +0 -7
  123. package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +1 -1
  124. package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +0 -7
  125. package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +9 -1
  126. package/dist/mjs/data-structures/priority-queue/priority-queue.js +7 -6
  127. package/dist/mjs/data-structures/queue/deque.d.ts +76 -80
  128. package/dist/mjs/data-structures/queue/deque.js +106 -122
  129. package/dist/mjs/data-structures/queue/queue.d.ts +30 -16
  130. package/dist/mjs/data-structures/queue/queue.js +31 -24
  131. package/dist/mjs/data-structures/stack/stack.d.ts +17 -8
  132. package/dist/mjs/data-structures/stack/stack.js +10 -10
  133. package/dist/mjs/data-structures/trie/trie.d.ts +14 -5
  134. package/dist/mjs/data-structures/trie/trie.js +14 -14
  135. package/dist/mjs/interfaces/binary-tree.d.ts +4 -4
  136. package/dist/mjs/types/common.d.ts +32 -8
  137. package/dist/mjs/types/common.js +21 -0
  138. package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -24
  139. package/dist/mjs/types/data-structures/binary-tree/binary-tree.js +1 -21
  140. package/dist/mjs/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
  141. package/dist/mjs/types/data-structures/binary-tree/tree-multimap.d.ts +1 -1
  142. package/dist/umd/data-structure-typed.js +533 -558
  143. package/dist/umd/data-structure-typed.min.js +2 -2
  144. package/dist/umd/data-structure-typed.min.js.map +1 -1
  145. package/package.json +1 -1
  146. package/src/data-structures/base/iterable-base.ts +24 -0
  147. package/src/data-structures/binary-tree/avl-tree.ts +14 -14
  148. package/src/data-structures/binary-tree/binary-tree.ts +74 -77
  149. package/src/data-structures/binary-tree/bst.ts +18 -17
  150. package/src/data-structures/binary-tree/rb-tree.ts +17 -18
  151. package/src/data-structures/binary-tree/tree-multimap.ts +22 -20
  152. package/src/data-structures/graph/abstract-graph.ts +35 -25
  153. package/src/data-structures/graph/directed-graph.ts +2 -2
  154. package/src/data-structures/graph/map-graph.ts +1 -1
  155. package/src/data-structures/graph/undirected-graph.ts +2 -2
  156. package/src/data-structures/hash/hash-map.ts +40 -24
  157. package/src/data-structures/hash/hash-table.ts +3 -3
  158. package/src/data-structures/heap/heap.ts +33 -60
  159. package/src/data-structures/heap/max-heap.ts +11 -2
  160. package/src/data-structures/heap/min-heap.ts +11 -2
  161. package/src/data-structures/linked-list/doubly-linked-list.ts +147 -145
  162. package/src/data-structures/linked-list/singly-linked-list.ts +52 -52
  163. package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
  164. package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
  165. package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
  166. package/src/data-structures/priority-queue/priority-queue.ts +9 -2
  167. package/src/data-structures/queue/deque.ts +129 -144
  168. package/src/data-structures/queue/queue.ts +37 -26
  169. package/src/data-structures/stack/stack.ts +20 -14
  170. package/src/data-structures/trie/trie.ts +18 -13
  171. package/src/interfaces/binary-tree.ts +5 -5
  172. package/src/types/common.ts +37 -12
  173. package/src/types/data-structures/binary-tree/avl-tree.ts +0 -1
  174. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -26
  175. package/src/types/data-structures/binary-tree/bst.ts +0 -1
  176. package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
  177. package/src/types/data-structures/binary-tree/tree-multimap.ts +1 -1
  178. package/test/performance/data-structures/comparison/comparison.test.ts +6 -6
  179. package/test/performance/data-structures/heap/heap.test.ts +2 -2
  180. package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +2 -2
  181. package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +2 -2
  182. package/test/performance/data-structures/priority-queue/priority-queue.test.ts +2 -2
  183. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +2 -2
  184. package/test/unit/data-structures/graph/directed-graph.test.ts +21 -1
  185. package/test/unit/data-structures/graph/undirected-graph.test.ts +39 -1
  186. package/test/unit/data-structures/hash/hash-map.test.ts +2 -2
  187. package/test/unit/data-structures/heap/heap.test.ts +1 -1
  188. package/test/unit/data-structures/heap/min-heap.test.ts +1 -1
  189. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +30 -30
  190. package/test/unit/data-structures/linked-list/linked-list.test.ts +1 -1
  191. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +21 -21
  192. package/test/unit/data-structures/linked-list/skip-list.test.ts +2 -2
  193. package/test/unit/data-structures/queue/deque.test.ts +5 -5
  194. package/test/unit/data-structures/queue/queue.test.ts +4 -4
  195. package/test/unit/data-structures/trie/trie.test.ts +1 -1
@@ -1,17 +1,11 @@
1
- /**
2
- * data-structure-typed
3
- *
4
- * @author Tyler Zeng
5
- * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
- * @license MIT License
7
- */
8
- import { calcMinUnitsRequired, rangeCheck } from "../../utils";
9
1
  import { IterableElementBase } from "../base";
2
+ import { calcMinUnitsRequired, rangeCheck } from "../../utils";
10
3
  /**
11
- * Deque can provide random access with O(1) time complexity
12
- * Deque is usually more compact and efficient in memory usage because it does not require additional space to store pointers.
13
- * Deque may experience performance jitter, but DoublyLinkedList will not
14
- * Deque is implemented using a dynamic array. Inserting or deleting beyond both ends of the array may require moving elements or reallocating space.
4
+ * 1. Operations at Both Ends: Supports adding and removing elements at both the front and back of the queue. This allows it to be used as a stack (last in, first out) and a queue (first in, first out).
5
+ * 2. Efficient Random Access: Being based on an array, it offers fast random access capability, allowing constant time access to any element.
6
+ * 3. Continuous Memory Allocation: Since it is based on an array, all elements are stored contiguously in memory, which can bring cache friendliness and efficient memory access.
7
+ * 4. Efficiency: Adding and removing elements at both ends of a deque is usually very fast. However, when the dynamic array needs to expand, it may involve copying the entire array to a larger one, and this operation has a time complexity of O(n).
8
+ * 5. Performance jitter: Deque may experience performance jitter, but DoublyLinkedList will not
15
9
  */
16
10
  export class Deque extends IterableElementBase {
17
11
  _bucketFirst = 0;
@@ -79,95 +73,6 @@ export class Deque extends IterableElementBase {
79
73
  return;
80
74
  return this._buckets[this._bucketLast][this._lastInBucket];
81
75
  }
82
- /**
83
- * Time Complexity: O(1) - Removes the last element.
84
- * Space Complexity: O(1) - Operates in-place.
85
- */
86
- isEmpty() {
87
- return this.size === 0;
88
- }
89
- /**
90
- * Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
91
- * Space Complexity: O(n) - Due to potential resizing.
92
- */
93
- /**
94
- * Time Complexity: O(1)
95
- * Space Complexity: O(n) - In worst case, resizing doubles the array size.
96
- *
97
- * The addLast function adds an element to the end of an array.
98
- * @param {E} element - The element parameter represents the element that you want to add to the end of the
99
- * data structure.
100
- */
101
- addLast(element) {
102
- this.push(element);
103
- }
104
- /**
105
- * Time Complexity: O(1) - Removes the first element.
106
- * Space Complexity: O(1) - In-place operation.
107
- */
108
- /**
109
- * Time Complexity: O(1) - Removes the last element.
110
- * Space Complexity: O(1) - Operates in-place.
111
- *
112
- * The function "pollLast" removes and returns the last element of an array.
113
- * @returns The last element of the array is being returned.
114
- */
115
- pollLast() {
116
- return this.pop();
117
- }
118
- /**
119
- * Time Complexity: O(1).
120
- * Space Complexity: O(n) - Due to potential resizing.
121
- *
122
- * The "addFirst" function adds an element to the beginning of an array.
123
- * @param {E} element - The parameter "element" represents the element that you want to add to the
124
- * beginning of the data structure.
125
- */
126
- addFirst(element) {
127
- this.unshift(element);
128
- }
129
- /**
130
- * Time Complexity: O(1) - Removes the first element.
131
- * Space Complexity: O(1) - In-place operation.
132
- *
133
- * The function "pollFirst" removes and returns the first element of an array.
134
- * @returns The method `pollFirst()` is returning the first element of the array after removing it
135
- * from the beginning. If the array is empty, it will return `undefined`.
136
- */
137
- pollFirst() {
138
- return this.shift();
139
- }
140
- /**
141
- * The clear() function resets the state of the object by initializing all variables to their default
142
- * values.
143
- */
144
- clear() {
145
- this._buckets = [new Array(this._bucketSize)];
146
- this._bucketCount = 1;
147
- this._bucketFirst = this._bucketLast = this._size = 0;
148
- this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
149
- }
150
- /**
151
- * The below function is a generator that yields elements from a collection one by one.
152
- */
153
- *begin() {
154
- let index = 0;
155
- while (index < this.size) {
156
- yield this.getAt(index);
157
- index++;
158
- }
159
- }
160
- /**
161
- * The function `reverseBegin()` is a generator that yields elements in reverse order starting from
162
- * the last element.
163
- */
164
- *reverseBegin() {
165
- let index = this.size - 1;
166
- while (index >= 0) {
167
- yield this.getAt(index);
168
- index--;
169
- }
170
- }
171
76
  /**
172
77
  * Time Complexity - Amortized O(1) (possible reallocation)
173
78
  * Space Complexity - O(n) (due to potential resizing).
@@ -200,7 +105,7 @@ export class Deque extends IterableElementBase {
200
105
  }
201
106
  this._size += 1;
202
107
  this._buckets[this._bucketLast][this._lastInBucket] = element;
203
- return this.size;
108
+ return true;
204
109
  }
205
110
  /**
206
111
  * Time Complexity: O(1)
@@ -267,7 +172,7 @@ export class Deque extends IterableElementBase {
267
172
  }
268
173
  this._size += 1;
269
174
  this._buckets[this._bucketFirst][this._firstInBucket] = element;
270
- return this.size;
175
+ return true;
271
176
  }
272
177
  /**
273
178
  * Time Complexity: O(1)
@@ -302,6 +207,44 @@ export class Deque extends IterableElementBase {
302
207
  this._size -= 1;
303
208
  return element;
304
209
  }
210
+ /**
211
+ * Time Complexity: O(1) - Removes the last element.
212
+ * Space Complexity: O(1) - Operates in-place.
213
+ */
214
+ isEmpty() {
215
+ return this.size === 0;
216
+ }
217
+ /**
218
+ * The clear() function resets the state of the object by initializing all variables to their default
219
+ * values.
220
+ */
221
+ clear() {
222
+ this._buckets = [new Array(this._bucketSize)];
223
+ this._bucketCount = 1;
224
+ this._bucketFirst = this._bucketLast = this._size = 0;
225
+ this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
226
+ }
227
+ /**
228
+ * The below function is a generator that yields elements from a collection one by one.
229
+ */
230
+ *begin() {
231
+ let index = 0;
232
+ while (index < this.size) {
233
+ yield this.getAt(index);
234
+ index++;
235
+ }
236
+ }
237
+ /**
238
+ * The function `reverseBegin()` is a generator that yields elements in reverse order starting from
239
+ * the last element.
240
+ */
241
+ *reverseBegin() {
242
+ let index = this.size - 1;
243
+ while (index >= 0) {
244
+ yield this.getAt(index);
245
+ index--;
246
+ }
247
+ }
305
248
  /**
306
249
  * Time Complexity: O(1)
307
250
  * Space Complexity: O(1)
@@ -339,6 +282,7 @@ export class Deque extends IterableElementBase {
339
282
  rangeCheck(pos, 0, this.size - 1);
340
283
  const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
341
284
  this._buckets[bucketIndex][indexInBucket] = element;
285
+ return true;
342
286
  }
343
287
  /**
344
288
  * Time Complexity: O(n)
@@ -348,7 +292,7 @@ export class Deque extends IterableElementBase {
348
292
  * Time Complexity: O(n)
349
293
  * Space Complexity: O(n)
350
294
  *
351
- * The `insertAt` function inserts one or more elements at a specified position in an array-like data
295
+ * The `addAt` function inserts one or more elements at a specified position in an array-like data
352
296
  * structure.
353
297
  * @param {number} pos - The `pos` parameter represents the position at which the element(s) should
354
298
  * be inserted. It is of type `number`.
@@ -359,7 +303,7 @@ export class Deque extends IterableElementBase {
359
303
  * will be inserted once. However, you can provide a different value for `num` if you want
360
304
  * @returns The size of the array after the insertion is being returned.
361
305
  */
362
- insertAt(pos, element, num = 1) {
306
+ addAt(pos, element, num = 1) {
363
307
  const length = this.size;
364
308
  rangeCheck(pos, 0, length);
365
309
  if (pos === 0) {
@@ -381,7 +325,7 @@ export class Deque extends IterableElementBase {
381
325
  for (let i = 0; i < arr.length; ++i)
382
326
  this.push(arr[i]);
383
327
  }
384
- return this.size;
328
+ return true;
385
329
  }
386
330
  /**
387
331
  * Time Complexity: O(1)
@@ -440,7 +384,7 @@ export class Deque extends IterableElementBase {
440
384
  }
441
385
  this.pop();
442
386
  }
443
- return this.size;
387
+ return true;
444
388
  }
445
389
  /**
446
390
  * Time Complexity: O(n)
@@ -459,7 +403,7 @@ export class Deque extends IterableElementBase {
459
403
  delete(element) {
460
404
  const size = this.size;
461
405
  if (size === 0)
462
- return 0;
406
+ return false;
463
407
  let i = 0;
464
408
  let index = 0;
465
409
  while (i < size) {
@@ -471,7 +415,7 @@ export class Deque extends IterableElementBase {
471
415
  i += 1;
472
416
  }
473
417
  this.cut(index - 1);
474
- return this.size;
418
+ return true;
475
419
  }
476
420
  /**
477
421
  * Time Complexity: O(n)
@@ -511,7 +455,7 @@ export class Deque extends IterableElementBase {
511
455
  */
512
456
  unique() {
513
457
  if (this.size <= 1) {
514
- return this.size;
458
+ return this;
515
459
  }
516
460
  let index = 1;
517
461
  let prev = this.getAt(0);
@@ -523,7 +467,7 @@ export class Deque extends IterableElementBase {
523
467
  }
524
468
  }
525
469
  this.cut(index - 1);
526
- return this.size;
470
+ return this;
527
471
  }
528
472
  /**
529
473
  * Time Complexity: O(n log n)
@@ -537,7 +481,7 @@ export class Deque extends IterableElementBase {
537
481
  * @param [comparator] - The `comparator` parameter is a function that takes in two elements `x` and
538
482
  * `y` of type `E` and returns a number. The comparator function is used to determine the order of
539
483
  * the elements in the sorted array.
540
- * @returns The method is returning the sorted instance of the object on which the method is called.
484
+ * @returns Deque<E>
541
485
  */
542
486
  sort(comparator) {
543
487
  const arr = [];
@@ -608,7 +552,7 @@ export class Deque extends IterableElementBase {
608
552
  return element;
609
553
  }
610
554
  }
611
- return undefined;
555
+ return;
612
556
  }
613
557
  /**
614
558
  * Time Complexity: O(n)
@@ -645,11 +589,7 @@ export class Deque extends IterableElementBase {
645
589
  * @returns The `toArray()` method is returning an array of elements of type `E`.
646
590
  */
647
591
  toArray() {
648
- const arr = [];
649
- for (let i = 0; i < this.size; ++i) {
650
- arr.push(this.getAt(i));
651
- }
652
- return arr;
592
+ return [...this];
653
593
  }
654
594
  /**
655
595
  * Time Complexity: O(n)
@@ -709,11 +649,55 @@ export class Deque extends IterableElementBase {
709
649
  return newDeque;
710
650
  }
711
651
  /**
712
- * Time Complexity: O(n)
713
- * Space Complexity: O(n)
652
+ * Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
653
+ * Space Complexity: O(n) - Due to potential resizing.
654
+ */
655
+ /**
656
+ * Time Complexity: O(1)
657
+ * Space Complexity: O(n) - In worst case, resizing doubles the array size.
658
+ *
659
+ * The addLast function adds an element to the end of an array.
660
+ * @param {E} element - The element parameter represents the element that you want to add to the end of the
661
+ * data structure.
662
+ */
663
+ addLast(element) {
664
+ return this.push(element);
665
+ }
666
+ /**
667
+ * Time Complexity: O(1) - Removes the first element.
668
+ * Space Complexity: O(1) - In-place operation.
669
+ */
670
+ /**
671
+ * Time Complexity: O(1) - Removes the last element.
672
+ * Space Complexity: O(1) - Operates in-place.
673
+ *
674
+ * The function "pollLast" removes and returns the last element of an array.
675
+ * @returns The last element of the array is being returned.
714
676
  */
715
- print() {
716
- console.log([...this]);
677
+ pollLast() {
678
+ return this.pop();
679
+ }
680
+ /**
681
+ * Time Complexity: O(1).
682
+ * Space Complexity: O(n) - Due to potential resizing.
683
+ *
684
+ * The "addFirst" function adds an element to the beginning of an array.
685
+ * @param {E} element - The parameter "element" represents the element that you want to add to the
686
+ * beginning of the data structure.
687
+ */
688
+ addFirst(element) {
689
+ return this.unshift(element);
690
+ }
691
+ /**
692
+ * Time Complexity: O(1) - Removes the first element.
693
+ * Space Complexity: O(1) - In-place operation.
694
+ *
695
+ * The function "pollFirst" removes and returns the first element of an array.
696
+ * @returns The method `pollFirst()` is returning the first element of the array after removing it
697
+ * from the beginning. If the array is empty, it will return `undefined`.
698
+ */
699
+ pollFirst() {
700
+ return this.shift();
717
701
  }
718
702
  /**
719
703
  * Time Complexity: O(n)
@@ -3,9 +3,18 @@
3
3
  * @copyright Tyler Zeng <zrwusa@gmail.com>
4
4
  * @class
5
5
  */
6
- import { SinglyLinkedList } from '../linked-list';
6
+ import type { ElementCallback } from '../../types';
7
7
  import { IterableElementBase } from "../base";
8
- import { ElementCallback } from "../../types";
8
+ import { SinglyLinkedList } from '../linked-list';
9
+ /**
10
+ * 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.
11
+ * 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).
12
+ * 3. Uses: Queues are commonly used to manage a series of tasks or elements that need to be processed in order. For example, managing task queues in a multi-threaded environment, or in algorithms for data structures like trees and graphs for breadth-first search.
13
+ * 4. Task Scheduling: Managing the order of task execution in operating systems or applications.
14
+ * 5. Data Buffering: Acting as a buffer for data packets in network communication.
15
+ * 6. Breadth-First Search (BFS): In traversal algorithms for graphs and trees, queues store nodes that are to be visited.
16
+ * 7. Real-time Queuing: Like queuing systems in banks or supermarkets.
17
+ */
9
18
  export declare class Queue<E = any> extends IterableElementBase<E> {
10
19
  /**
11
20
  * The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0.
@@ -44,7 +53,7 @@ export declare class Queue<E = any> extends IterableElementBase<E> {
44
53
  * @param {E} element - The `element` parameter represents the element that you want to add to the queue.
45
54
  * @returns The `add` method is returning a `Queue<E>` object.
46
55
  */
47
- push(element: E): Queue<E>;
56
+ push(element: E): boolean;
48
57
  /**
49
58
  * Time Complexity: O(n) - where n is the number of elements in the queue. In the worst case, it may need to shift all elements to update the offset.
50
59
  * Space Complexity: O(1) - no additional space is used.
@@ -66,11 +75,11 @@ export declare class Queue<E = any> extends IterableElementBase<E> {
66
75
  * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
67
76
  * Space Complexity: O(1) - no additional space is used.
68
77
  *
69
- * The `getFirst` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`.
70
- * @returns The `getFirst()` method returns the first element of the data structure, represented by the `_nodes` array at
78
+ * The `first` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`.
79
+ * @returns The `get first()` method returns the first element of the data structure, represented by the `_nodes` array at
71
80
  * the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
72
81
  */
73
- getFirst(): E | undefined;
82
+ get first(): E | undefined;
74
83
  /**
75
84
  * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
76
85
  * Space Complexity: O(1) - no additional space is used.
@@ -92,11 +101,11 @@ export declare class Queue<E = any> extends IterableElementBase<E> {
92
101
  * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
93
102
  * Space Complexity: O(1) - no additional space is used.
94
103
  *
95
- * The `getLast` function returns the last element in an array-like data structure, or undefined if the structure is empty.
96
- * @returns The method `getLast()` returns the last element of the `_nodes` array if the array is not empty. If the
104
+ * The `last` function returns the last element in an array-like data structure, or undefined if the structure is empty.
105
+ * @returns The method `get last()` returns the last element of the `_nodes` array if the array is not empty. If the
97
106
  * array is empty, it returns `undefined`.
98
107
  */
99
- getLast(): E | undefined;
108
+ get last(): E | undefined;
100
109
  /**
101
110
  * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
102
111
  * Space Complexity: O(1) - no additional space is used.
@@ -121,7 +130,7 @@ export declare class Queue<E = any> extends IterableElementBase<E> {
121
130
  * The enqueue function adds a value to the end of a queue.
122
131
  * @param {E} value - The value parameter represents the value that you want to add to the queue.
123
132
  */
124
- enqueue(value: E): void;
133
+ enqueue(value: E): boolean;
125
134
  /**
126
135
  * Time Complexity: O(n) - same as shift().
127
136
  * Space Complexity: O(1) - same as shift().
@@ -185,7 +194,6 @@ export declare class Queue<E = any> extends IterableElementBase<E> {
185
194
  * @returns The `clone()` method is returning a new instance of the `Queue` class.
186
195
  */
187
196
  clone(): Queue<E>;
188
- print(): void;
189
197
  /**
190
198
  * Time Complexity: O(n)
191
199
  * Space Complexity: O(n)
@@ -230,24 +238,30 @@ export declare class Queue<E = any> extends IterableElementBase<E> {
230
238
  * Time Complexity: O(n)
231
239
  * Space Complexity: O(n)
232
240
  */
233
- protected _getIterator(): Generator<E, void, unknown>;
241
+ protected _getIterator(): IterableIterator<E>;
234
242
  }
243
+ /**
244
+ * 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.
245
+ * 2. Based on Linked List: LinkedListQueue uses a linked list to store elements. Each node in the linked list contains data and a pointer to the next node.
246
+ * 3. Memory Usage: Since each element requires additional space to store a pointer to the next element, linked lists may use more memory compared to arrays.
247
+ * 4. Frequent Enqueuing and Dequeuing Operations: If your application involves frequent enqueuing and dequeuing operations and is less concerned with random access, then LinkedListQueue is a good choice.
248
+ */
235
249
  export declare class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
236
250
  /**
237
251
  * The enqueue function adds a value to the end of an array.
238
252
  * @param {E} value - The value parameter represents the value that you want to add to the queue.
239
253
  */
240
- enqueue(value: E): void;
254
+ enqueue(value: E): boolean;
241
255
  /**
242
256
  * The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
243
257
  * @returns The method is returning the element at the front of the queue, or undefined if the queue is empty.
244
258
  */
245
259
  dequeue(): E | undefined;
246
260
  /**
247
- * The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
248
- * @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
261
+ * The `get first` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
262
+ * @returns The `get first()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
249
263
  */
250
- getFirst(): E | undefined;
264
+ get first(): E | undefined;
251
265
  /**
252
266
  * The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
253
267
  * @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
@@ -1,10 +1,14 @@
1
+ import { IterableElementBase } from "../base";
2
+ import { SinglyLinkedList } from '../linked-list';
1
3
  /**
2
- * @license MIT
3
- * @copyright Tyler Zeng <zrwusa@gmail.com>
4
- * @class
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
+ * 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).
6
+ * 3. Uses: Queues are commonly used to manage a series of tasks or elements that need to be processed in order. For example, managing task queues in a multi-threaded environment, or in algorithms for data structures like trees and graphs for breadth-first search.
7
+ * 4. Task Scheduling: Managing the order of task execution in operating systems or applications.
8
+ * 5. Data Buffering: Acting as a buffer for data packets in network communication.
9
+ * 6. Breadth-First Search (BFS): In traversal algorithms for graphs and trees, queues store nodes that are to be visited.
10
+ * 7. Real-time Queuing: Like queuing systems in banks or supermarkets.
5
11
  */
6
- import { SinglyLinkedList } from '../linked-list';
7
- import { IterableElementBase } from "../base";
8
12
  export class Queue extends IterableElementBase {
9
13
  /**
10
14
  * The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0.
@@ -57,7 +61,7 @@ export class Queue extends IterableElementBase {
57
61
  */
58
62
  push(element) {
59
63
  this.nodes.push(element);
60
- return this;
64
+ return true;
61
65
  }
62
66
  /**
63
67
  * Time Complexity: O(n) - where n is the number of elements in the queue. In the worst case, it may need to shift all elements to update the offset.
@@ -74,7 +78,7 @@ export class Queue extends IterableElementBase {
74
78
  shift() {
75
79
  if (this.size === 0)
76
80
  return undefined;
77
- const first = this.getFirst();
81
+ const first = this.first;
78
82
  this._offset += 1;
79
83
  if (this.offset * 2 < this.nodes.length)
80
84
  return first;
@@ -92,11 +96,11 @@ export class Queue extends IterableElementBase {
92
96
  * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
93
97
  * Space Complexity: O(1) - no additional space is used.
94
98
  *
95
- * The `getFirst` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`.
96
- * @returns The `getFirst()` method returns the first element of the data structure, represented by the `_nodes` array at
99
+ * The `first` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`.
100
+ * @returns The `get first()` method returns the first element of the data structure, represented by the `_nodes` array at
97
101
  * the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
98
102
  */
99
- getFirst() {
103
+ get first() {
100
104
  return this.size > 0 ? this.nodes[this.offset] : undefined;
101
105
  }
102
106
  /**
@@ -112,7 +116,7 @@ export class Queue extends IterableElementBase {
112
116
  * the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
113
117
  */
114
118
  peek() {
115
- return this.getFirst();
119
+ return this.first;
116
120
  }
117
121
  /**
118
122
  * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
@@ -122,11 +126,11 @@ export class Queue extends IterableElementBase {
122
126
  * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
123
127
  * Space Complexity: O(1) - no additional space is used.
124
128
  *
125
- * The `getLast` function returns the last element in an array-like data structure, or undefined if the structure is empty.
126
- * @returns The method `getLast()` returns the last element of the `_nodes` array if the array is not empty. If the
129
+ * The `last` function returns the last element in an array-like data structure, or undefined if the structure is empty.
130
+ * @returns The method `get last()` returns the last element of the `_nodes` array if the array is not empty. If the
127
131
  * array is empty, it returns `undefined`.
128
132
  */
129
- getLast() {
133
+ get last() {
130
134
  return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined;
131
135
  }
132
136
  /**
@@ -142,7 +146,7 @@ export class Queue extends IterableElementBase {
142
146
  * array is empty, it returns `undefined`.
143
147
  */
144
148
  peekLast() {
145
- return this.getLast();
149
+ return this.last;
146
150
  }
147
151
  /**
148
152
  * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
@@ -156,7 +160,7 @@ export class Queue extends IterableElementBase {
156
160
  * @param {E} value - The value parameter represents the value that you want to add to the queue.
157
161
  */
158
162
  enqueue(value) {
159
- this.push(value);
163
+ return this.push(value);
160
164
  }
161
165
  /**
162
166
  * Time Complexity: O(n) - same as shift().
@@ -234,9 +238,6 @@ export class Queue extends IterableElementBase {
234
238
  clone() {
235
239
  return new Queue(this.nodes.slice(this.offset));
236
240
  }
237
- print() {
238
- console.log([...this]);
239
- }
240
241
  /**
241
242
  * Time Complexity: O(n)
242
243
  * Space Complexity: O(n)
@@ -305,13 +306,19 @@ export class Queue extends IterableElementBase {
305
306
  }
306
307
  }
307
308
  }
309
+ /**
310
+ * 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.
311
+ * 2. Based on Linked List: LinkedListQueue uses a linked list to store elements. Each node in the linked list contains data and a pointer to the next node.
312
+ * 3. Memory Usage: Since each element requires additional space to store a pointer to the next element, linked lists may use more memory compared to arrays.
313
+ * 4. Frequent Enqueuing and Dequeuing Operations: If your application involves frequent enqueuing and dequeuing operations and is less concerned with random access, then LinkedListQueue is a good choice.
314
+ */
308
315
  export class LinkedListQueue extends SinglyLinkedList {
309
316
  /**
310
317
  * The enqueue function adds a value to the end of an array.
311
318
  * @param {E} value - The value parameter represents the value that you want to add to the queue.
312
319
  */
313
320
  enqueue(value) {
314
- this.push(value);
321
+ return this.push(value);
315
322
  }
316
323
  /**
317
324
  * The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
@@ -321,10 +328,10 @@ export class LinkedListQueue extends SinglyLinkedList {
321
328
  return this.shift();
322
329
  }
323
330
  /**
324
- * The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
325
- * @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
331
+ * The `get first` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
332
+ * @returns The `get first()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
326
333
  */
327
- getFirst() {
334
+ get first() {
328
335
  return this.head?.value;
329
336
  }
330
337
  /**
@@ -332,6 +339,6 @@ export class LinkedListQueue extends SinglyLinkedList {
332
339
  * @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
333
340
  */
334
341
  peek() {
335
- return this.getFirst();
342
+ return this.first;
336
343
  }
337
344
  }
@@ -1,9 +1,19 @@
1
- import { IterableElementBase } from "../base";
2
- import { ElementCallback } from "../../types";
3
1
  /**
4
- * @license MIT
5
- * @copyright Tyler Zeng <zrwusa@gmail.com>
6
- * @class
2
+ * data-structure-typed
3
+ *
4
+ * @author Tyler Zeng
5
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
7
+ */
8
+ import type { ElementCallback } from '../../types';
9
+ import { IterableElementBase } from '../base';
10
+ /**
11
+ * 1. Last In, First Out (LIFO): The core characteristic of a stack is its last in, first out nature, meaning the last element added to the stack will be the first to be removed.
12
+ * 2. Uses: Stacks are commonly used for managing a series of tasks or elements that need to be processed in a last in, first out manner. They are widely used in various scenarios, such as in function calls in programming languages, evaluation of arithmetic expressions, and backtracking algorithms.
13
+ * 3. Performance: Stack operations are typically O(1) in time complexity, meaning that regardless of the stack's size, adding, removing, and viewing the top element are very fast operations.
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
+ * 5. Expression Evaluation: Used for the evaluation of arithmetic or logical expressions, especially when dealing with parenthesis matching and operator precedence.
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.
7
17
  */
8
18
  export declare class Stack<E = any> extends IterableElementBase<E> {
9
19
  /**
@@ -63,7 +73,7 @@ export declare class Stack<E = any> extends IterableElementBase<E> {
63
73
  * @param {E} element - The parameter "element" is of type E, which means it can be any data type.
64
74
  * @returns The `push` method is returning the updated `Stack<E>` object.
65
75
  */
66
- push(element: E): Stack<E>;
76
+ push(element: E): boolean;
67
77
  /**
68
78
  * Time Complexity: O(1), as it only involves accessing the last element of the array.
69
79
  * Space Complexity: O(1), as it does not use any additional space.
@@ -144,10 +154,9 @@ export declare class Stack<E = any> extends IterableElementBase<E> {
144
154
  * @returns The `map` method is returning a new `Stack` object.
145
155
  */
146
156
  map<T>(callback: ElementCallback<E, T>, thisArg?: any): Stack<T>;
147
- print(): void;
148
157
  /**
149
158
  * Custom iterator for the Stack class.
150
159
  * @returns An iterator object.
151
160
  */
152
- protected _getIterator(): Generator<E, void, unknown>;
161
+ protected _getIterator(): IterableIterator<E>;
153
162
  }