@procedures/collections 0.0.0-stage → 0.2.0

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 (205) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/LICENSE +21 -0
  3. package/README.md +394 -2
  4. package/dist/.tsbuildinfo +1 -0
  5. package/dist/async/async-queue.d.ts +155 -0
  6. package/dist/async/async-queue.d.ts.map +1 -0
  7. package/dist/async/async-queue.js +342 -0
  8. package/dist/async/async-queue.js.map +1 -0
  9. package/dist/async/channels.d.ts +188 -0
  10. package/dist/async/channels.d.ts.map +1 -0
  11. package/dist/async/channels.js +314 -0
  12. package/dist/async/channels.js.map +1 -0
  13. package/dist/behaviors/bounded.d.ts +79 -0
  14. package/dist/behaviors/bounded.d.ts.map +1 -0
  15. package/dist/behaviors/bounded.js +384 -0
  16. package/dist/behaviors/bounded.js.map +1 -0
  17. package/dist/behaviors/composite.d.ts +17 -0
  18. package/dist/behaviors/composite.d.ts.map +1 -0
  19. package/dist/behaviors/composite.js +93 -0
  20. package/dist/behaviors/composite.js.map +1 -0
  21. package/dist/behaviors/evented.d.ts +154 -0
  22. package/dist/behaviors/evented.d.ts.map +1 -0
  23. package/dist/behaviors/evented.js +299 -0
  24. package/dist/behaviors/evented.js.map +1 -0
  25. package/dist/behaviors/lru.d.ts +84 -0
  26. package/dist/behaviors/lru.d.ts.map +1 -0
  27. package/dist/behaviors/lru.js +376 -0
  28. package/dist/behaviors/lru.js.map +1 -0
  29. package/dist/behaviors/readonly.d.ts +73 -0
  30. package/dist/behaviors/readonly.d.ts.map +1 -0
  31. package/dist/behaviors/readonly.js +177 -0
  32. package/dist/behaviors/readonly.js.map +1 -0
  33. package/dist/behaviors/safe.d.ts +163 -0
  34. package/dist/behaviors/safe.d.ts.map +1 -0
  35. package/dist/behaviors/safe.js +176 -0
  36. package/dist/behaviors/safe.js.map +1 -0
  37. package/dist/behaviors/synchronized.d.ts +88 -0
  38. package/dist/behaviors/synchronized.d.ts.map +1 -0
  39. package/dist/behaviors/synchronized.js +285 -0
  40. package/dist/behaviors/synchronized.js.map +1 -0
  41. package/dist/behaviors/ttl.d.ts +118 -0
  42. package/dist/behaviors/ttl.d.ts.map +1 -0
  43. package/dist/behaviors/ttl.js +354 -0
  44. package/dist/behaviors/ttl.js.map +1 -0
  45. package/dist/contracts/model.d.ts +29 -0
  46. package/dist/contracts/model.d.ts.map +1 -0
  47. package/dist/contracts/model.js +26 -0
  48. package/dist/contracts/model.js.map +1 -0
  49. package/dist/core/effects.d.ts +176 -0
  50. package/dist/core/effects.d.ts.map +1 -0
  51. package/dist/core/effects.js +180 -0
  52. package/dist/core/effects.js.map +1 -0
  53. package/dist/core/events.d.ts +192 -0
  54. package/dist/core/events.d.ts.map +1 -0
  55. package/dist/core/events.js +291 -0
  56. package/dist/core/events.js.map +1 -0
  57. package/dist/core/middleware.d.ts +231 -0
  58. package/dist/core/middleware.d.ts.map +1 -0
  59. package/dist/core/middleware.js +161 -0
  60. package/dist/core/middleware.js.map +1 -0
  61. package/dist/core/policies.d.ts +220 -0
  62. package/dist/core/policies.d.ts.map +1 -0
  63. package/dist/core/policies.js +48 -0
  64. package/dist/core/policies.js.map +1 -0
  65. package/dist/core/traits.d.ts +142 -0
  66. package/dist/core/traits.d.ts.map +1 -0
  67. package/dist/core/traits.js +8 -0
  68. package/dist/core/traits.js.map +1 -0
  69. package/dist/fx/collectors.d.ts +193 -0
  70. package/dist/fx/collectors.d.ts.map +1 -0
  71. package/dist/fx/collectors.js +375 -0
  72. package/dist/fx/collectors.js.map +1 -0
  73. package/dist/fx/iter.d.ts +166 -0
  74. package/dist/fx/iter.d.ts.map +1 -0
  75. package/dist/fx/iter.js +421 -0
  76. package/dist/fx/iter.js.map +1 -0
  77. package/dist/impl/array-deque.d.ts +112 -0
  78. package/dist/impl/array-deque.d.ts.map +1 -0
  79. package/dist/impl/array-deque.js +380 -0
  80. package/dist/impl/array-deque.js.map +1 -0
  81. package/dist/impl/array-list.d.ts +101 -0
  82. package/dist/impl/array-list.d.ts.map +1 -0
  83. package/dist/impl/array-list.js +387 -0
  84. package/dist/impl/array-list.js.map +1 -0
  85. package/dist/impl/hash-map.d.ts +149 -0
  86. package/dist/impl/hash-map.d.ts.map +1 -0
  87. package/dist/impl/hash-map.js +473 -0
  88. package/dist/impl/hash-map.js.map +1 -0
  89. package/dist/impl/hash-set.d.ts +125 -0
  90. package/dist/impl/hash-set.d.ts.map +1 -0
  91. package/dist/impl/hash-set.js +264 -0
  92. package/dist/impl/hash-set.js.map +1 -0
  93. package/dist/impl/linked-hash-map.d.ts +141 -0
  94. package/dist/impl/linked-hash-map.d.ts.map +1 -0
  95. package/dist/impl/linked-hash-map.js +442 -0
  96. package/dist/impl/linked-hash-map.js.map +1 -0
  97. package/dist/impl/linked-list.d.ts +109 -0
  98. package/dist/impl/linked-list.d.ts.map +1 -0
  99. package/dist/impl/linked-list.js +494 -0
  100. package/dist/impl/linked-list.js.map +1 -0
  101. package/dist/impl/priority-queue.d.ts +139 -0
  102. package/dist/impl/priority-queue.d.ts.map +1 -0
  103. package/dist/impl/priority-queue.js +343 -0
  104. package/dist/impl/priority-queue.js.map +1 -0
  105. package/dist/impl/tree-map.d.ts +153 -0
  106. package/dist/impl/tree-map.d.ts.map +1 -0
  107. package/dist/impl/tree-map.js +788 -0
  108. package/dist/impl/tree-map.js.map +1 -0
  109. package/dist/impl/tree-set.d.ts +112 -0
  110. package/dist/impl/tree-set.d.ts.map +1 -0
  111. package/dist/impl/tree-set.js +374 -0
  112. package/dist/impl/tree-set.js.map +1 -0
  113. package/dist/index.d.ts +106 -0
  114. package/dist/index.d.ts.map +1 -0
  115. package/dist/index.js +139 -0
  116. package/dist/index.js.map +1 -0
  117. package/dist/interfaces/collection.d.ts +119 -0
  118. package/dist/interfaces/collection.d.ts.map +1 -0
  119. package/dist/interfaces/collection.js +8 -0
  120. package/dist/interfaces/collection.js.map +1 -0
  121. package/dist/interfaces/list.d.ts +247 -0
  122. package/dist/interfaces/list.d.ts.map +1 -0
  123. package/dist/interfaces/list.js +8 -0
  124. package/dist/interfaces/list.js.map +1 -0
  125. package/dist/interfaces/map.d.ts +344 -0
  126. package/dist/interfaces/map.d.ts.map +1 -0
  127. package/dist/interfaces/map.js +8 -0
  128. package/dist/interfaces/map.js.map +1 -0
  129. package/dist/interfaces/queue.d.ts +304 -0
  130. package/dist/interfaces/queue.d.ts.map +1 -0
  131. package/dist/interfaces/queue.js +8 -0
  132. package/dist/interfaces/queue.js.map +1 -0
  133. package/dist/interfaces/set.d.ts +137 -0
  134. package/dist/interfaces/set.d.ts.map +1 -0
  135. package/dist/interfaces/set.js +8 -0
  136. package/dist/interfaces/set.js.map +1 -0
  137. package/dist/register.d.ts +8 -0
  138. package/dist/register.d.ts.map +1 -0
  139. package/dist/register.js +8 -0
  140. package/dist/register.js.map +1 -0
  141. package/dist/storage/index.d.ts +15 -0
  142. package/dist/storage/index.d.ts.map +1 -0
  143. package/dist/storage/index.js +14 -0
  144. package/dist/storage/index.js.map +1 -0
  145. package/dist/storage/interface.d.ts +141 -0
  146. package/dist/storage/interface.d.ts.map +1 -0
  147. package/dist/storage/interface.js +23 -0
  148. package/dist/storage/interface.js.map +1 -0
  149. package/dist/storage/memory.d.ts +68 -0
  150. package/dist/storage/memory.d.ts.map +1 -0
  151. package/dist/storage/memory.js +154 -0
  152. package/dist/storage/memory.js.map +1 -0
  153. package/dist/utils/defaults.d.ts +135 -0
  154. package/dist/utils/defaults.d.ts.map +1 -0
  155. package/dist/utils/defaults.js +322 -0
  156. package/dist/utils/defaults.js.map +1 -0
  157. package/dist/utils/factories.d.ts +144 -0
  158. package/dist/utils/factories.d.ts.map +1 -0
  159. package/dist/utils/factories.js +293 -0
  160. package/dist/utils/factories.js.map +1 -0
  161. package/dist/utils/helpers.d.ts +137 -0
  162. package/dist/utils/helpers.d.ts.map +1 -0
  163. package/dist/utils/helpers.js +221 -0
  164. package/dist/utils/helpers.js.map +1 -0
  165. package/package.json +68 -3
  166. package/src/async/async-queue.ts +417 -0
  167. package/src/async/channels.ts +374 -0
  168. package/src/behaviors/bounded.ts +477 -0
  169. package/src/behaviors/composite.ts +99 -0
  170. package/src/behaviors/evented.ts +464 -0
  171. package/src/behaviors/lru.ts +447 -0
  172. package/src/behaviors/readonly.ts +224 -0
  173. package/src/behaviors/safe.ts +372 -0
  174. package/src/behaviors/synchronized.ts +316 -0
  175. package/src/behaviors/ttl.ts +462 -0
  176. package/src/contracts/model.ts +44 -0
  177. package/src/core/effects.ts +323 -0
  178. package/src/core/events.ts +356 -0
  179. package/src/core/middleware.ts +415 -0
  180. package/src/core/policies.ts +316 -0
  181. package/src/core/traits.ts +164 -0
  182. package/src/fx/collectors.ts +500 -0
  183. package/src/fx/iter.ts +536 -0
  184. package/src/impl/array-deque.ts +463 -0
  185. package/src/impl/array-list.ts +490 -0
  186. package/src/impl/hash-map.ts +623 -0
  187. package/src/impl/hash-set.ts +342 -0
  188. package/src/impl/linked-hash-map.ts +572 -0
  189. package/src/impl/linked-list.ts +606 -0
  190. package/src/impl/priority-queue.ts +448 -0
  191. package/src/impl/tree-map.ts +912 -0
  192. package/src/impl/tree-set.ts +469 -0
  193. package/src/index.ts +277 -0
  194. package/src/interfaces/collection.ts +141 -0
  195. package/src/interfaces/list.ts +281 -0
  196. package/src/interfaces/map.ts +405 -0
  197. package/src/interfaces/queue.ts +353 -0
  198. package/src/interfaces/set.ts +158 -0
  199. package/src/register.ts +8 -0
  200. package/src/storage/index.ts +16 -0
  201. package/src/storage/interface.ts +179 -0
  202. package/src/storage/memory.ts +180 -0
  203. package/src/utils/defaults.ts +345 -0
  204. package/src/utils/factories.ts +357 -0
  205. package/src/utils/helpers.ts +248 -0
@@ -0,0 +1,342 @@
1
+ /**
2
+ * HashSet implementation - A set backed by a HashMap.
3
+ *
4
+ * Provides O(1) average case for add, remove, and contains operations.
5
+ * Elements are unique based on custom equality and hash functions.
6
+ * Mirrors java.util.HashSet.
7
+ *
8
+ * @example
9
+ * const set = hashSet<number>()
10
+ * set.add(1)
11
+ * set.add(2)
12
+ * set.add(1) // Returns false, already present
13
+ * set.size // 2
14
+ */
15
+
16
+ import type { Set } from "../interfaces/set.js";
17
+ import type { Eq, Hash } from "../core/traits.js";
18
+ import { HashMap } from "./hash-map.js";
19
+ import { defaultEq, defaultHash } from "../utils/defaults.js";
20
+
21
+ /**
22
+ * Dummy value used for the HashMap backing the set.
23
+ */
24
+ const PRESENT = Symbol("PRESENT");
25
+
26
+ /**
27
+ * Options for creating a HashSet.
28
+ */
29
+ export interface HashSetOptions<T> {
30
+ /**
31
+ * Initial capacity.
32
+ * @default 16
33
+ */
34
+ initialCapacity?: number;
35
+
36
+ /**
37
+ * Load factor threshold for resizing.
38
+ * @default 0.75
39
+ */
40
+ loadFactor?: number;
41
+
42
+ /**
43
+ * Equality function for elements.
44
+ * @default defaultEq (===)
45
+ */
46
+ eq?: Eq<T>;
47
+
48
+ /**
49
+ * Hash function for elements.
50
+ * @default defaultHash
51
+ */
52
+ hash?: Hash<T>;
53
+ }
54
+
55
+ /**
56
+ * HashSet<T> - Hash table implementation of the Set interface.
57
+ *
58
+ * Backed by a HashMap where values are a dummy constant.
59
+ * Provides constant-time performance for basic operations.
60
+ *
61
+ * Not thread-safe. Use synchronized() behavior for concurrent access.
62
+ */
63
+ export class HashSet<T> implements Set<T> {
64
+ private readonly _map: HashMap<T, symbol>;
65
+ private readonly _eq: Eq<T>;
66
+
67
+ constructor(options: HashSetOptions<T> = {}) {
68
+ const {
69
+ initialCapacity = 16,
70
+ loadFactor = 0.75,
71
+ eq = defaultEq,
72
+ hash = defaultHash,
73
+ } = options;
74
+
75
+ this._eq = eq;
76
+ this._map = new HashMap<T, symbol>({
77
+ initialCapacity,
78
+ loadFactor,
79
+ keyEq: eq,
80
+ keyHash: hash,
81
+ });
82
+ }
83
+
84
+ // ========================================================================
85
+ // Size
86
+ // ========================================================================
87
+
88
+ get size(): number {
89
+ return this._map.size;
90
+ }
91
+
92
+ get isEmpty(): boolean {
93
+ return this._map.isEmpty;
94
+ }
95
+
96
+ get eq(): Eq<T> {
97
+ return this._eq;
98
+ }
99
+
100
+ // ========================================================================
101
+ // Core set operations
102
+ // ========================================================================
103
+
104
+ add(element: T): boolean {
105
+ const hadElement = this._map.has(element);
106
+ this._map.set(element, PRESENT);
107
+ return !hadElement;
108
+ }
109
+
110
+ remove(element: T): boolean {
111
+ return this._map.delete(element) !== undefined;
112
+ }
113
+
114
+ contains(element: T): boolean {
115
+ return this._map.has(element);
116
+ }
117
+
118
+ // ========================================================================
119
+ // Bulk operations
120
+ // ========================================================================
121
+
122
+ addAll(other: Iterable<T>): boolean {
123
+ let modified = false;
124
+ for (const element of other) {
125
+ if (this.add(element)) {
126
+ modified = true;
127
+ }
128
+ }
129
+ return modified;
130
+ }
131
+
132
+ removeAll(other: Iterable<T>): boolean {
133
+ let modified = false;
134
+ for (const element of other) {
135
+ if (this.remove(element)) {
136
+ modified = true;
137
+ }
138
+ }
139
+ return modified;
140
+ }
141
+
142
+ retainAll(other: Iterable<T>): boolean {
143
+ const toRetain = new globalThis.Set(other);
144
+ const toRemove: T[] = [];
145
+
146
+ for (const element of this) {
147
+ let shouldRetain = false;
148
+ for (const retainElement of toRetain) {
149
+ if (this._eq(element, retainElement)) {
150
+ shouldRetain = true;
151
+ break;
152
+ }
153
+ }
154
+ if (!shouldRetain) {
155
+ toRemove.push(element);
156
+ }
157
+ }
158
+
159
+ let modified = false;
160
+ for (const element of toRemove) {
161
+ this.remove(element);
162
+ modified = true;
163
+ }
164
+
165
+ return modified;
166
+ }
167
+
168
+ containsAll(other: Iterable<T>): boolean {
169
+ for (const element of other) {
170
+ if (!this.contains(element)) {
171
+ return false;
172
+ }
173
+ }
174
+ return true;
175
+ }
176
+
177
+ clear(): void {
178
+ this._map.clear();
179
+ }
180
+
181
+ // ========================================================================
182
+ // Set operations (mathematical)
183
+ // ========================================================================
184
+
185
+ /**
186
+ * Returns a new set that is the union of this set and the other set.
187
+ * Contains all elements from both sets.
188
+ */
189
+ union(other: Iterable<T>): HashSet<T> {
190
+ const result = this.clone();
191
+ result.addAll(other);
192
+ return result;
193
+ }
194
+
195
+ /**
196
+ * Returns a new set that is the intersection of this set and the other set.
197
+ * Contains only elements present in both sets.
198
+ */
199
+ intersection(other: Iterable<T>): HashSet<T> {
200
+ const result = new HashSet<T>({ eq: this._eq });
201
+ for (const element of this) {
202
+ for (const otherElement of other) {
203
+ if (this._eq(element, otherElement)) {
204
+ result.add(element);
205
+ break;
206
+ }
207
+ }
208
+ }
209
+ return result;
210
+ }
211
+
212
+ /**
213
+ * Returns a new set that is the difference of this set and the other set.
214
+ * Contains elements in this set but not in the other set.
215
+ */
216
+ difference(other: Iterable<T>): HashSet<T> {
217
+ const result = this.clone();
218
+ result.removeAll(other);
219
+ return result;
220
+ }
221
+
222
+ /**
223
+ * Returns a new set that is the symmetric difference of this set and the other set.
224
+ * Contains elements in either set but not in both.
225
+ */
226
+ symmetricDifference(other: Iterable<T>): HashSet<T> {
227
+ const result = this.union(other);
228
+ const inter = this.intersection(other);
229
+ result.removeAll(inter);
230
+ return result;
231
+ }
232
+
233
+ /**
234
+ * Returns true if this set is a subset of the other set.
235
+ * All elements in this set are also in the other set.
236
+ */
237
+ isSubsetOf(other: Iterable<T>): boolean {
238
+ const otherSet = new HashSet<T>({ eq: this._eq });
239
+ otherSet.addAll(other);
240
+
241
+ for (const element of this) {
242
+ if (!otherSet.contains(element)) {
243
+ return false;
244
+ }
245
+ }
246
+ return true;
247
+ }
248
+
249
+ /**
250
+ * Returns true if this set is a superset of the other set.
251
+ * All elements in the other set are also in this set.
252
+ */
253
+ isSupersetOf(other: Iterable<T>): boolean {
254
+ for (const element of other) {
255
+ if (!this.contains(element)) {
256
+ return false;
257
+ }
258
+ }
259
+ return true;
260
+ }
261
+
262
+ /**
263
+ * Returns true if this set and the other set have no elements in common.
264
+ */
265
+ isDisjointFrom(other: Iterable<T>): boolean {
266
+ for (const element of other) {
267
+ if (this.contains(element)) {
268
+ return false;
269
+ }
270
+ }
271
+ return true;
272
+ }
273
+
274
+ // ========================================================================
275
+ // Iteration
276
+ // ========================================================================
277
+
278
+ *[Symbol.iterator](): Iterator<T> {
279
+ for (const entry of this._map) {
280
+ yield entry.key;
281
+ }
282
+ }
283
+
284
+ forEach(action: (element: T, index: number) => void): void {
285
+ let index = 0;
286
+ for (const element of this) {
287
+ action(element, index++);
288
+ }
289
+ }
290
+
291
+ toArray(): T[] {
292
+ return Array.from(this);
293
+ }
294
+
295
+ // ========================================================================
296
+ // Utility methods
297
+ // ========================================================================
298
+
299
+ /**
300
+ * Creates a shallow copy of this set.
301
+ */
302
+ clone(): HashSet<T> {
303
+ const clone = new HashSet<T>({ eq: this._eq });
304
+ clone.addAll(this);
305
+ return clone;
306
+ }
307
+
308
+ /**
309
+ * Returns statistics about the underlying hash table.
310
+ */
311
+ getStats(): ReturnType<HashMap<T, symbol>["getStats"]> {
312
+ return this._map.getStats();
313
+ }
314
+
315
+ /**
316
+ * Returns a string representation for debugging.
317
+ */
318
+ toString(): string {
319
+ return `HashSet{${this.toArray().join(", ")}}`;
320
+ }
321
+ }
322
+
323
+ /**
324
+ * Factory function to create a HashSet.
325
+ *
326
+ * @example
327
+ * const set = hashSet<number>()
328
+ * const setFromArray = hashSet([1, 2, 3, 2, 1]) // {1, 2, 3}
329
+ * const setWithOptions = hashSet<string>({ initialCapacity: 32 })
330
+ */
331
+ export function hashSet<T>(
332
+ optionsOrElements?: HashSetOptions<T> | Iterable<T>
333
+ ): HashSet<T> {
334
+ // Check if it's an iterable (not options)
335
+ if (optionsOrElements && Symbol.iterator in optionsOrElements) {
336
+ const set = new HashSet<T>();
337
+ set.addAll(optionsOrElements as Iterable<T>);
338
+ return set;
339
+ }
340
+
341
+ return new HashSet<T>(optionsOrElements as HashSetOptions<T>);
342
+ }