@procedures/collections 0.46.0 → 0.47.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 (233) hide show
  1. package/CHANGELOG.md +42 -8
  2. package/README.md +30 -0
  3. package/dist/.tsbuildinfo +1 -1
  4. package/dist/index.d.ts +1 -58
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +3 -94
  7. package/dist/index.js.map +1 -1
  8. package/dist/storage/index.d.ts +2 -2
  9. package/dist/storage/index.d.ts.map +1 -1
  10. package/dist/storage/index.js +1 -1
  11. package/dist/storage/index.js.map +1 -1
  12. package/dist/storage/memory.d.ts +1 -1
  13. package/dist/storage/memory.d.ts.map +1 -1
  14. package/dist/storage/memory.js +1 -1
  15. package/dist/storage/memory.js.map +1 -1
  16. package/dist/testing/index.d.ts +1 -1
  17. package/dist/testing/index.d.ts.map +1 -1
  18. package/dist/testing/index.js +1 -1
  19. package/dist/testing/index.js.map +1 -1
  20. package/dist/testing/storage-contract.js +1 -1
  21. package/dist/testing/storage-contract.js.map +1 -1
  22. package/dist/undo/index.d.ts +1 -1
  23. package/dist/undo/index.js +1 -1
  24. package/dist/undo/undo-log.d.ts +1 -1
  25. package/dist/undo/undo-log.js +1 -1
  26. package/package.json +6 -6
  27. package/src/index.ts +338 -213
  28. package/src/storage/index.ts +2 -2
  29. package/src/storage/memory.ts +1 -1
  30. package/src/testing/index.ts +1 -1
  31. package/src/testing/storage-contract.ts +1 -1
  32. package/src/undo/index.ts +1 -1
  33. package/src/undo/undo-log.ts +1 -1
  34. package/dist/async/async-queue.d.ts +0 -170
  35. package/dist/async/async-queue.d.ts.map +0 -1
  36. package/dist/async/async-queue.js +0 -404
  37. package/dist/async/async-queue.js.map +0 -1
  38. package/dist/async/channels.d.ts +0 -211
  39. package/dist/async/channels.d.ts.map +0 -1
  40. package/dist/async/channels.js +0 -393
  41. package/dist/async/channels.js.map +0 -1
  42. package/dist/async/settled.d.ts +0 -23
  43. package/dist/async/settled.d.ts.map +0 -1
  44. package/dist/async/settled.js +0 -32
  45. package/dist/async/settled.js.map +0 -1
  46. package/dist/behaviors/bounded.d.ts +0 -79
  47. package/dist/behaviors/bounded.d.ts.map +0 -1
  48. package/dist/behaviors/bounded.js +0 -393
  49. package/dist/behaviors/bounded.js.map +0 -1
  50. package/dist/behaviors/composite.d.ts +0 -17
  51. package/dist/behaviors/composite.d.ts.map +0 -1
  52. package/dist/behaviors/composite.js +0 -93
  53. package/dist/behaviors/composite.js.map +0 -1
  54. package/dist/behaviors/evented.d.ts +0 -154
  55. package/dist/behaviors/evented.d.ts.map +0 -1
  56. package/dist/behaviors/evented.js +0 -299
  57. package/dist/behaviors/evented.js.map +0 -1
  58. package/dist/behaviors/lru.d.ts +0 -84
  59. package/dist/behaviors/lru.d.ts.map +0 -1
  60. package/dist/behaviors/lru.js +0 -376
  61. package/dist/behaviors/lru.js.map +0 -1
  62. package/dist/behaviors/readonly.d.ts +0 -73
  63. package/dist/behaviors/readonly.d.ts.map +0 -1
  64. package/dist/behaviors/readonly.js +0 -189
  65. package/dist/behaviors/readonly.js.map +0 -1
  66. package/dist/behaviors/safe.d.ts +0 -163
  67. package/dist/behaviors/safe.d.ts.map +0 -1
  68. package/dist/behaviors/safe.js +0 -176
  69. package/dist/behaviors/safe.js.map +0 -1
  70. package/dist/behaviors/synchronized.d.ts +0 -88
  71. package/dist/behaviors/synchronized.d.ts.map +0 -1
  72. package/dist/behaviors/synchronized.js +0 -285
  73. package/dist/behaviors/synchronized.js.map +0 -1
  74. package/dist/behaviors/ttl.d.ts +0 -118
  75. package/dist/behaviors/ttl.d.ts.map +0 -1
  76. package/dist/behaviors/ttl.js +0 -354
  77. package/dist/behaviors/ttl.js.map +0 -1
  78. package/dist/contracts/model.d.ts +0 -29
  79. package/dist/contracts/model.d.ts.map +0 -1
  80. package/dist/contracts/model.js +0 -26
  81. package/dist/contracts/model.js.map +0 -1
  82. package/dist/core/effects.d.ts +0 -176
  83. package/dist/core/effects.d.ts.map +0 -1
  84. package/dist/core/effects.js +0 -180
  85. package/dist/core/effects.js.map +0 -1
  86. package/dist/core/events.d.ts +0 -192
  87. package/dist/core/events.d.ts.map +0 -1
  88. package/dist/core/events.js +0 -291
  89. package/dist/core/events.js.map +0 -1
  90. package/dist/core/middleware.d.ts +0 -231
  91. package/dist/core/middleware.d.ts.map +0 -1
  92. package/dist/core/middleware.js +0 -161
  93. package/dist/core/middleware.js.map +0 -1
  94. package/dist/core/policies.d.ts +0 -220
  95. package/dist/core/policies.d.ts.map +0 -1
  96. package/dist/core/policies.js +0 -48
  97. package/dist/core/policies.js.map +0 -1
  98. package/dist/core/traits.d.ts +0 -142
  99. package/dist/core/traits.d.ts.map +0 -1
  100. package/dist/core/traits.js +0 -8
  101. package/dist/core/traits.js.map +0 -1
  102. package/dist/fx/collectors.d.ts +0 -193
  103. package/dist/fx/collectors.d.ts.map +0 -1
  104. package/dist/fx/collectors.js +0 -375
  105. package/dist/fx/collectors.js.map +0 -1
  106. package/dist/fx/iter.d.ts +0 -166
  107. package/dist/fx/iter.d.ts.map +0 -1
  108. package/dist/fx/iter.js +0 -421
  109. package/dist/fx/iter.js.map +0 -1
  110. package/dist/impl/array-deque.d.ts +0 -112
  111. package/dist/impl/array-deque.d.ts.map +0 -1
  112. package/dist/impl/array-deque.js +0 -380
  113. package/dist/impl/array-deque.js.map +0 -1
  114. package/dist/impl/array-list.d.ts +0 -101
  115. package/dist/impl/array-list.d.ts.map +0 -1
  116. package/dist/impl/array-list.js +0 -387
  117. package/dist/impl/array-list.js.map +0 -1
  118. package/dist/impl/hash-map.d.ts +0 -149
  119. package/dist/impl/hash-map.d.ts.map +0 -1
  120. package/dist/impl/hash-map.js +0 -473
  121. package/dist/impl/hash-map.js.map +0 -1
  122. package/dist/impl/hash-set.d.ts +0 -125
  123. package/dist/impl/hash-set.d.ts.map +0 -1
  124. package/dist/impl/hash-set.js +0 -264
  125. package/dist/impl/hash-set.js.map +0 -1
  126. package/dist/impl/linked-hash-map.d.ts +0 -141
  127. package/dist/impl/linked-hash-map.d.ts.map +0 -1
  128. package/dist/impl/linked-hash-map.js +0 -442
  129. package/dist/impl/linked-hash-map.js.map +0 -1
  130. package/dist/impl/linked-list.d.ts +0 -109
  131. package/dist/impl/linked-list.d.ts.map +0 -1
  132. package/dist/impl/linked-list.js +0 -494
  133. package/dist/impl/linked-list.js.map +0 -1
  134. package/dist/impl/priority-queue.d.ts +0 -139
  135. package/dist/impl/priority-queue.d.ts.map +0 -1
  136. package/dist/impl/priority-queue.js +0 -343
  137. package/dist/impl/priority-queue.js.map +0 -1
  138. package/dist/impl/tree-map.d.ts +0 -153
  139. package/dist/impl/tree-map.d.ts.map +0 -1
  140. package/dist/impl/tree-map.js +0 -788
  141. package/dist/impl/tree-map.js.map +0 -1
  142. package/dist/impl/tree-set.d.ts +0 -112
  143. package/dist/impl/tree-set.d.ts.map +0 -1
  144. package/dist/impl/tree-set.js +0 -374
  145. package/dist/impl/tree-set.js.map +0 -1
  146. package/dist/interfaces/collection.d.ts +0 -119
  147. package/dist/interfaces/collection.d.ts.map +0 -1
  148. package/dist/interfaces/collection.js +0 -8
  149. package/dist/interfaces/collection.js.map +0 -1
  150. package/dist/interfaces/list.d.ts +0 -247
  151. package/dist/interfaces/list.d.ts.map +0 -1
  152. package/dist/interfaces/list.js +0 -8
  153. package/dist/interfaces/list.js.map +0 -1
  154. package/dist/interfaces/map.d.ts +0 -344
  155. package/dist/interfaces/map.d.ts.map +0 -1
  156. package/dist/interfaces/map.js +0 -8
  157. package/dist/interfaces/map.js.map +0 -1
  158. package/dist/interfaces/queue.d.ts +0 -304
  159. package/dist/interfaces/queue.d.ts.map +0 -1
  160. package/dist/interfaces/queue.js +0 -8
  161. package/dist/interfaces/queue.js.map +0 -1
  162. package/dist/interfaces/set.d.ts +0 -137
  163. package/dist/interfaces/set.d.ts.map +0 -1
  164. package/dist/interfaces/set.js +0 -8
  165. package/dist/interfaces/set.js.map +0 -1
  166. package/dist/order/index.d.ts +0 -7
  167. package/dist/order/index.d.ts.map +0 -1
  168. package/dist/order/index.js +0 -7
  169. package/dist/order/index.js.map +0 -1
  170. package/dist/order/rank.d.ts +0 -100
  171. package/dist/order/rank.d.ts.map +0 -1
  172. package/dist/order/rank.js +0 -286
  173. package/dist/order/rank.js.map +0 -1
  174. package/dist/storage/listeners.d.ts +0 -39
  175. package/dist/storage/listeners.d.ts.map +0 -1
  176. package/dist/storage/listeners.js +0 -65
  177. package/dist/storage/listeners.js.map +0 -1
  178. package/dist/testing/check.d.ts +0 -25
  179. package/dist/testing/check.d.ts.map +0 -1
  180. package/dist/testing/check.js +0 -85
  181. package/dist/testing/check.js.map +0 -1
  182. package/dist/utils/defaults.d.ts +0 -135
  183. package/dist/utils/defaults.d.ts.map +0 -1
  184. package/dist/utils/defaults.js +0 -322
  185. package/dist/utils/defaults.js.map +0 -1
  186. package/dist/utils/factories.d.ts +0 -144
  187. package/dist/utils/factories.d.ts.map +0 -1
  188. package/dist/utils/factories.js +0 -293
  189. package/dist/utils/factories.js.map +0 -1
  190. package/dist/utils/helpers.d.ts +0 -137
  191. package/dist/utils/helpers.d.ts.map +0 -1
  192. package/dist/utils/helpers.js +0 -221
  193. package/dist/utils/helpers.js.map +0 -1
  194. package/src/async/async-queue.ts +0 -481
  195. package/src/async/channels.ts +0 -450
  196. package/src/async/settled.ts +0 -41
  197. package/src/behaviors/bounded.ts +0 -485
  198. package/src/behaviors/composite.ts +0 -99
  199. package/src/behaviors/evented.ts +0 -464
  200. package/src/behaviors/lru.ts +0 -447
  201. package/src/behaviors/readonly.ts +0 -236
  202. package/src/behaviors/safe.ts +0 -372
  203. package/src/behaviors/synchronized.ts +0 -316
  204. package/src/behaviors/ttl.ts +0 -462
  205. package/src/contracts/model.ts +0 -44
  206. package/src/core/effects.ts +0 -323
  207. package/src/core/events.ts +0 -356
  208. package/src/core/middleware.ts +0 -415
  209. package/src/core/policies.ts +0 -316
  210. package/src/core/traits.ts +0 -164
  211. package/src/fx/collectors.ts +0 -500
  212. package/src/fx/iter.ts +0 -536
  213. package/src/impl/array-deque.ts +0 -463
  214. package/src/impl/array-list.ts +0 -490
  215. package/src/impl/hash-map.ts +0 -623
  216. package/src/impl/hash-set.ts +0 -342
  217. package/src/impl/linked-hash-map.ts +0 -572
  218. package/src/impl/linked-list.ts +0 -606
  219. package/src/impl/priority-queue.ts +0 -448
  220. package/src/impl/tree-map.ts +0 -912
  221. package/src/impl/tree-set.ts +0 -469
  222. package/src/interfaces/collection.ts +0 -141
  223. package/src/interfaces/list.ts +0 -281
  224. package/src/interfaces/map.ts +0 -405
  225. package/src/interfaces/queue.ts +0 -353
  226. package/src/interfaces/set.ts +0 -158
  227. package/src/order/index.ts +0 -6
  228. package/src/order/rank.ts +0 -271
  229. package/src/storage/listeners.ts +0 -72
  230. package/src/testing/check.ts +0 -81
  231. package/src/utils/defaults.ts +0 -345
  232. package/src/utils/factories.ts +0 -357
  233. package/src/utils/helpers.ts +0 -248
@@ -1,490 +0,0 @@
1
- /**
2
- * ArrayList implementation - A dynamic array-backed list.
3
- *
4
- * Provides O(1) indexed access and amortized O(1) append.
5
- * Mirrors java.util.ArrayList.
6
- *
7
- * @example
8
- * const list = arrayList<number>()
9
- * list.add(1)
10
- * list.add(2)
11
- * list.add(3)
12
- * console.log(list.get(1)) // 2
13
- */
14
-
15
- import type { List, RandomAccess } from "../interfaces/list.js";
16
- import type { Eq, Compare } from "../core/traits.js";
17
- import { defaultEq, defaultCompare } from "../utils/defaults.js";
18
-
19
- /**
20
- * Options for creating an ArrayList.
21
- */
22
- export interface ArrayListOptions<T> {
23
- /**
24
- * Initial capacity of the underlying array.
25
- * @default 10
26
- */
27
- initialCapacity?: number;
28
-
29
- /**
30
- * Equality function for comparing elements.
31
- * @default defaultEq (===)
32
- */
33
- eq?: Eq<T>;
34
- }
35
-
36
- /**
37
- * ArrayList<T> - Resizable array implementation of the List interface.
38
- *
39
- * Provides fast random access (O(1)) and fast append (amortized O(1)).
40
- * Insertion and removal in the middle require shifting elements (O(n)).
41
- *
42
- * This is the most commonly used list implementation.
43
- */
44
- export class ArrayList<T> implements List<T>, RandomAccess {
45
- private _array: T[];
46
- private _size: number;
47
- private readonly _eq: Eq<T>;
48
-
49
- readonly supportsRandomAccess = true as const;
50
-
51
- constructor(options: ArrayListOptions<T> = {}) {
52
- const { initialCapacity = 10, eq = defaultEq } = options;
53
- this._array = new Array(initialCapacity);
54
- this._size = 0;
55
- this._eq = eq;
56
- }
57
-
58
- // ========================================================================
59
- // Size and capacity
60
- // ========================================================================
61
-
62
- get size(): number {
63
- return this._size;
64
- }
65
-
66
- get isEmpty(): boolean {
67
- return this._size === 0;
68
- }
69
-
70
- /**
71
- * Current capacity of the underlying array.
72
- */
73
- get capacity(): number {
74
- return this._array.length;
75
- }
76
-
77
- get eq(): Eq<T> {
78
- return this._eq;
79
- }
80
-
81
- // ========================================================================
82
- // Indexed access
83
- // ========================================================================
84
-
85
- get(index: number): T {
86
- this.checkIndex(index);
87
- return this._array[index]!;
88
- }
89
-
90
- set(index: number, element: T): T {
91
- this.checkIndex(index);
92
- const old = this._array[index]!;
93
- this._array[index] = element;
94
- return old!;
95
- }
96
-
97
- // ========================================================================
98
- // Search
99
- // ========================================================================
100
-
101
- contains(element: T): boolean {
102
- return this.indexOf(element) !== -1;
103
- }
104
-
105
- indexOf(element: T, fromIndex: number = 0): number {
106
- if (fromIndex < 0) fromIndex = 0;
107
- for (let i = fromIndex; i < this._size; i++) {
108
- if (this._eq(this._array[i]!, element)) {
109
- return i;
110
- }
111
- }
112
- return -1;
113
- }
114
-
115
- lastIndexOf(element: T, fromIndex?: number): number {
116
- const start =
117
- fromIndex !== undefined
118
- ? Math.min(fromIndex, this._size - 1)
119
- : this._size - 1;
120
- for (let i = start; i >= 0; i--) {
121
- if (this._eq(this._array[i]!, element)) {
122
- return i;
123
- }
124
- }
125
- return -1;
126
- }
127
-
128
- containsAll(other: Iterable<T>): boolean {
129
- for (const element of other) {
130
- if (!this.contains(element)) return false;
131
- }
132
- return true;
133
- }
134
-
135
- // ========================================================================
136
- // Addition
137
- // ========================================================================
138
-
139
- add(element: T): boolean {
140
- this.ensureCapacity(this._size + 1);
141
- this._array[this._size++] = element;
142
- return true;
143
- }
144
-
145
- push(element: T): void {
146
- this.add(element);
147
- }
148
-
149
- unshift(element: T): void {
150
- this.insert(0, element);
151
- }
152
-
153
- insert(index: number, element: T): void {
154
- this.checkInsertIndex(index);
155
- this.ensureCapacity(this._size + 1);
156
-
157
- // Shift elements to the right
158
- for (let i = this._size; i > index; i--) {
159
- this._array[i] = this._array[i - 1]!;
160
- }
161
-
162
- this._array[index] = element;
163
- this._size++;
164
- }
165
-
166
- insertAll(index: number, other: Iterable<T>): boolean {
167
- this.checkInsertIndex(index);
168
- const elements = Array.isArray(other) ? other : Array.from(other);
169
- if (elements.length === 0) return false;
170
-
171
- this.ensureCapacity(this._size + elements.length);
172
-
173
- // Shift existing elements to the right
174
- for (let i = this._size - 1; i >= index; i--) {
175
- this._array[i + elements.length] = this._array[i]!;
176
- }
177
-
178
- // Insert new elements
179
- for (let i = 0; i < elements.length; i++) {
180
- this._array[index + i] = elements[i];
181
- }
182
-
183
- this._size += elements.length;
184
- return true;
185
- }
186
-
187
- addAll(other: Iterable<T>): boolean {
188
- const elements = Array.isArray(other) ? other : Array.from(other);
189
- if (elements.length === 0) return false;
190
-
191
- this.ensureCapacity(this._size + elements.length);
192
-
193
- for (const element of elements) {
194
- this._array[this._size++] = element;
195
- }
196
-
197
- return true;
198
- }
199
-
200
- // ========================================================================
201
- // Removal
202
- // ========================================================================
203
-
204
- remove(element: T): boolean {
205
- const index = this.indexOf(element);
206
- if (index === -1) return false;
207
- this.removeAt(index);
208
- return true;
209
- }
210
-
211
- removeAt(index: number): T {
212
- this.checkIndex(index);
213
- const old = this._array[index]!;
214
-
215
- // Shift elements to the left
216
- for (let i = index; i < this._size - 1; i++) {
217
- this._array[i] = this._array[i + 1]!;
218
- }
219
-
220
- this._size--;
221
- // Clear the last element to allow GC
222
- this._array[this._size] = undefined as any;
223
-
224
- return old;
225
- }
226
-
227
- pop(): T {
228
- if (this.isEmpty) {
229
- throw new Error("Cannot pop from empty list");
230
- }
231
- return this.removeAt(this._size - 1);
232
- }
233
-
234
- shift(): T {
235
- if (this.isEmpty) {
236
- throw new Error("Cannot shift from empty list");
237
- }
238
- return this.removeAt(0);
239
- }
240
-
241
- removeRange(fromIndex: number, toIndex: number): void {
242
- this.checkRange(fromIndex, toIndex);
243
- const numToRemove = toIndex - fromIndex;
244
- if (numToRemove === 0) return;
245
-
246
- // Shift elements to the left
247
- for (let i = toIndex; i < this._size; i++) {
248
- this._array[i - numToRemove] = this._array[i]!;
249
- }
250
-
251
- // Clear removed elements
252
- for (let i = this._size - numToRemove; i < this._size; i++) {
253
- this._array[i] = undefined as any;
254
- }
255
-
256
- this._size -= numToRemove;
257
- }
258
-
259
- removeAll(other: Iterable<T>): boolean {
260
- const toRemove = new Set<T>();
261
- for (const element of other) {
262
- toRemove.add(element);
263
- }
264
-
265
- let writeIndex = 0;
266
- let modified = false;
267
-
268
- for (let readIndex = 0; readIndex < this._size; readIndex++) {
269
- const element = this._array[readIndex]!;
270
- let shouldRemove = false;
271
-
272
- // Check if element matches any in toRemove using custom equality
273
- for (const removeElement of toRemove) {
274
- if (this._eq(element, removeElement)) {
275
- shouldRemove = true;
276
- modified = true;
277
- break;
278
- }
279
- }
280
-
281
- if (!shouldRemove) {
282
- this._array[writeIndex++] = element;
283
- }
284
- }
285
-
286
- // Clear remaining elements
287
- for (let i = writeIndex; i < this._size; i++) {
288
- this._array[i] = undefined as any;
289
- }
290
-
291
- this._size = writeIndex;
292
- return modified;
293
- }
294
-
295
- retainAll(other: Iterable<T>): boolean {
296
- const toRetain = Array.from(other);
297
- let writeIndex = 0;
298
- let modified = false;
299
-
300
- for (let readIndex = 0; readIndex < this._size; readIndex++) {
301
- const element = this._array[readIndex]!;
302
- let shouldRetain = false;
303
-
304
- // Check if element matches any in toRetain using custom equality
305
- for (const retainElement of toRetain) {
306
- if (this._eq(element, retainElement)) {
307
- shouldRetain = true;
308
- break;
309
- }
310
- }
311
-
312
- if (shouldRetain) {
313
- this._array[writeIndex++] = element;
314
- } else {
315
- modified = true;
316
- }
317
- }
318
-
319
- // Clear remaining elements
320
- for (let i = writeIndex; i < this._size; i++) {
321
- this._array[i] = undefined as any;
322
- }
323
-
324
- this._size = writeIndex;
325
- return modified;
326
- }
327
-
328
- clear(): void {
329
- // Clear all elements to allow GC
330
- for (let i = 0; i < this._size; i++) {
331
- this._array[i] = undefined as any;
332
- }
333
- this._size = 0;
334
- }
335
-
336
- // ========================================================================
337
- // Ordering
338
- // ========================================================================
339
-
340
- sort(compare: Compare<T> = defaultCompare): void {
341
- // Sort only the used portion of the array
342
- const slice = this._array.slice(0, this._size);
343
- slice.sort(compare);
344
- for (let i = 0; i < this._size; i++) {
345
- this._array[i] = slice[i]!;
346
- }
347
- }
348
-
349
- reverse(): void {
350
- let left = 0;
351
- let right = this._size - 1;
352
- while (left < right) {
353
- const temp = this._array[left]!;
354
- this._array[left] = this._array[right]!;
355
- this._array[right] = temp;
356
- left++;
357
- right--;
358
- }
359
- }
360
-
361
- // ========================================================================
362
- // Views
363
- // ========================================================================
364
-
365
- first(): T {
366
- if (this.isEmpty) {
367
- throw new Error("List is empty");
368
- }
369
- return this._array[0]!;
370
- }
371
-
372
- last(): T {
373
- if (this.isEmpty) {
374
- throw new Error("List is empty");
375
- }
376
- return this._array[this._size - 1]!;
377
- }
378
-
379
- subList(fromIndex: number, toIndex: number): List<T> {
380
- this.checkRange(fromIndex, toIndex);
381
- // For now, return a copy. Later we can implement a view.
382
- const sub = new ArrayList<T>({ eq: this._eq });
383
- for (let i = fromIndex; i < toIndex; i++) {
384
- sub.add(this._array[i]!);
385
- }
386
- return sub;
387
- }
388
-
389
- // ========================================================================
390
- // Iteration
391
- // ========================================================================
392
-
393
- *[Symbol.iterator](): Iterator<T> {
394
- for (let i = 0; i < this._size; i++) {
395
- yield this._array[i]!;
396
- }
397
- }
398
-
399
- forEach(action: (element: T, index: number) => void): void {
400
- for (let i = 0; i < this._size; i++) {
401
- action(this._array[i]!, i);
402
- }
403
- }
404
-
405
- toArray(): T[] {
406
- return this._array.slice(0, this._size);
407
- }
408
-
409
- // ========================================================================
410
- // Capacity management
411
- // ========================================================================
412
-
413
- /**
414
- * Ensures that the capacity is at least the specified minimum.
415
- */
416
- ensureCapacity(minCapacity: number): void {
417
- if (minCapacity <= this._array.length) return;
418
-
419
- // Grow by 1.5x or to minCapacity, whichever is larger
420
- const newCapacity = Math.max(
421
- minCapacity,
422
- Math.floor(this._array.length * 1.5) + 1
423
- );
424
-
425
- const newArray = new Array(newCapacity);
426
- for (let i = 0; i < this._size; i++) {
427
- newArray[i] = this._array[i];
428
- }
429
- this._array = newArray;
430
- }
431
-
432
- /**
433
- * Trims the capacity to the current size to minimize memory usage.
434
- */
435
- trimToSize(): void {
436
- if (this._size < this._array.length) {
437
- this._array = this._array.slice(0, this._size);
438
- }
439
- }
440
-
441
- // ========================================================================
442
- // Private helpers
443
- // ========================================================================
444
-
445
- private checkIndex(index: number): void {
446
- if (index < 0 || index >= this._size) {
447
- throw new RangeError(
448
- `Index ${index} out of bounds for size ${this._size}`
449
- );
450
- }
451
- }
452
-
453
- private checkInsertIndex(index: number): void {
454
- if (index < 0 || index > this._size) {
455
- throw new RangeError(
456
- `Index ${index} out of bounds for insert (size ${this._size})`
457
- );
458
- }
459
- }
460
-
461
- private checkRange(fromIndex: number, toIndex: number): void {
462
- if (fromIndex < 0 || toIndex > this._size || fromIndex > toIndex) {
463
- throw new RangeError(
464
- `Invalid range [${fromIndex}, ${toIndex}) for size ${this._size}`
465
- );
466
- }
467
- }
468
- }
469
-
470
- /**
471
- * Factory function to create an ArrayList.
472
- *
473
- * @example
474
- * const list = arrayList<number>()
475
- * const listWithEq = arrayList<User>({ eq: (a, b) => a.id === b.id })
476
- * const listFromArray = arrayList([1, 2, 3])
477
- */
478
- export function arrayList<T>(
479
- optionsOrElements?: ArrayListOptions<T> | Iterable<T>
480
- ): ArrayList<T> {
481
- // Check if it's an iterable (not options)
482
- if (optionsOrElements && Symbol.iterator in optionsOrElements) {
483
- const list = new ArrayList<T>();
484
- list.addAll(optionsOrElements as Iterable<T>);
485
- return list;
486
- }
487
-
488
- const list = new ArrayList<T>(optionsOrElements as ArrayListOptions<T>);
489
- return list;
490
- }