@mark1russell7/collections 0.0.0-bootstrap.0 → 0.1.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 (68) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +77 -1
  3. package/build/dag/index.d.ts +144 -0
  4. package/build/dag/index.d.ts.map +1 -0
  5. package/build/dag/index.js +294 -0
  6. package/build/dag/index.js.map +1 -0
  7. package/build/graph/index.d.ts +20 -0
  8. package/build/graph/index.d.ts.map +1 -0
  9. package/build/graph/index.js +48 -0
  10. package/build/graph/index.js.map +1 -0
  11. package/build/index.d.ts +4952 -0
  12. package/build/index.d.ts.map +1 -0
  13. package/build/index.js +7811 -0
  14. package/build/index.js.map +1 -0
  15. package/build/testing/index.d.ts +25 -0
  16. package/build/testing/index.d.ts.map +1 -0
  17. package/build/testing/index.js +72 -0
  18. package/build/testing/index.js.map +1 -0
  19. package/package.json +71 -4
  20. package/src/async/async-queue.ts +481 -0
  21. package/src/async/channels.ts +450 -0
  22. package/src/async/settled.ts +41 -0
  23. package/src/behaviors/bounded.ts +485 -0
  24. package/src/behaviors/composite.ts +99 -0
  25. package/src/behaviors/evented.ts +464 -0
  26. package/src/behaviors/lru.ts +449 -0
  27. package/src/behaviors/readonly.ts +236 -0
  28. package/src/behaviors/safe.ts +358 -0
  29. package/src/behaviors/synchronized.ts +316 -0
  30. package/src/behaviors/ttl.ts +464 -0
  31. package/src/core/effects.ts +327 -0
  32. package/src/core/events.ts +363 -0
  33. package/src/core/middleware.ts +413 -0
  34. package/src/core/policies.ts +316 -0
  35. package/src/core/traits.ts +164 -0
  36. package/src/dag/builder.ts +124 -0
  37. package/src/dag/executor.ts +190 -0
  38. package/src/dag/index.ts +14 -0
  39. package/src/dag/traversal.ts +129 -0
  40. package/src/dag/types.ts +87 -0
  41. package/src/fx/collectors.ts +500 -0
  42. package/src/fx/iter.ts +536 -0
  43. package/src/graph/hub-order.ts +53 -0
  44. package/src/graph/index.ts +5 -0
  45. package/src/impl/array-deque.ts +463 -0
  46. package/src/impl/array-list.ts +490 -0
  47. package/src/impl/hash-map.ts +623 -0
  48. package/src/impl/hash-set.ts +342 -0
  49. package/src/impl/linked-hash-map.ts +572 -0
  50. package/src/impl/linked-list.ts +610 -0
  51. package/src/impl/priority-queue.ts +448 -0
  52. package/src/impl/summary-tree.ts +594 -0
  53. package/src/impl/tree-map.ts +913 -0
  54. package/src/impl/tree-set.ts +469 -0
  55. package/src/index.ts +237 -0
  56. package/src/interfaces/collection.ts +141 -0
  57. package/src/interfaces/list.ts +281 -0
  58. package/src/interfaces/map.ts +405 -0
  59. package/src/interfaces/queue.ts +353 -0
  60. package/src/interfaces/set.ts +158 -0
  61. package/src/order/index.ts +6 -0
  62. package/src/order/rank.ts +271 -0
  63. package/src/storage/listeners.ts +72 -0
  64. package/src/testing/check.ts +81 -0
  65. package/src/testing/index.ts +7 -0
  66. package/src/utils/defaults.ts +345 -0
  67. package/src/utils/factories.ts +357 -0
  68. package/src/utils/helpers.ts +248 -0
@@ -0,0 +1,405 @@
1
+ /**
2
+ * Map interfaces that mirror Java's Map interface hierarchy.
3
+ *
4
+ * A map holds key-value pairs and gives efficient lookup by key.
5
+ * Keys are unique within a map.
6
+ */
7
+
8
+ import type { Eq, Compare } from "../core/traits.js";
9
+ import type { ReadonlyCollection } from "./collection.js";
10
+
11
+ /**
12
+ * Entry<K, V> - An immutable key-value pair from a map.
13
+ *
14
+ * Unlike java.util.Map.Entry which has setValue(), our entries are immutable
15
+ * snapshots. To update a value, use map.set(key, newValue) instead.
16
+ *
17
+ * This prevents accidental mutation and makes entries safe to share.
18
+ */
19
+ export interface Entry<K, V> {
20
+ readonly key: K;
21
+ readonly value: V;
22
+ }
23
+
24
+ /**
25
+ * ReadonlyMapLike<K, V> - Readonly key-value mapping.
26
+ *
27
+ * This type gives read-only access to map operations.
28
+ * Note: We use "MapLike" to avoid conflicts with TypeScript's built-in Map type.
29
+ */
30
+ export interface ReadonlyMapLike<K, V> extends Iterable<Entry<K, V>> {
31
+ /**
32
+ * This value is the number of key-value pairs in this map.
33
+ */
34
+ readonly size: number;
35
+
36
+ /**
37
+ * This value is true if this map contains no key-value mappings.
38
+ */
39
+ readonly isEmpty: boolean;
40
+
41
+ /**
42
+ * This method gives true if this map contains a mapping for the specified key.
43
+ */
44
+ has(key: K): boolean;
45
+
46
+ /**
47
+ * This method gives the value to which the specified key is mapped.
48
+ *
49
+ * @throws Error if key is not present
50
+ */
51
+ get(key: K): V;
52
+
53
+ /**
54
+ * This method gives the value for the key, or undefined if not present.
55
+ */
56
+ getOrUndefined(key: K): V | undefined;
57
+
58
+ /**
59
+ * This method gives the value for the key, or the default value if not present.
60
+ */
61
+ getOrDefault(key: K, defaultValue: V): V;
62
+
63
+ /**
64
+ * This method gives true if this map maps one or more keys to the specified value.
65
+ */
66
+ containsValue(value: V): boolean;
67
+
68
+ /**
69
+ * This method gives an iterable view of the keys in this map.
70
+ */
71
+ keys(): Iterable<K>;
72
+
73
+ /**
74
+ * This method gives an iterable view of the values in this map.
75
+ */
76
+ values(): Iterable<V>;
77
+
78
+ /**
79
+ * This method gives an iterable view of the key-value pairs in this map.
80
+ */
81
+ entries(): Iterable<Entry<K, V>>;
82
+
83
+ /**
84
+ * This method does the given action for each key-value pair.
85
+ */
86
+ forEach(action: (value: V, key: K, map: this) => void): void;
87
+
88
+ /**
89
+ * This value is the equality function used for keys.
90
+ */
91
+ readonly keyEq: Eq<K>;
92
+
93
+ /**
94
+ * This value is the equality function used for values.
95
+ */
96
+ readonly valueEq: Eq<V>;
97
+ }
98
+
99
+ /**
100
+ * MapLike<K, V> - Mutable key-value mapping.
101
+ *
102
+ * Mirrors java.util.Map.
103
+ */
104
+ export interface MapLike<K, V> extends ReadonlyMapLike<K, V> {
105
+ /**
106
+ * This method associates the specified value with the specified key in this map.
107
+ * If the map previously contained a mapping for the key, the old value is replaced.
108
+ *
109
+ * @param key The key
110
+ * @param value The value
111
+ * @returns The previous value associated with key, or undefined if none
112
+ */
113
+ set(key: K, value: V): V | undefined;
114
+
115
+ /**
116
+ * Associates value with key only if key is not already present.
117
+ *
118
+ * @returns The current value (existing or newly set)
119
+ */
120
+ setIfAbsent(key: K, value: V): V;
121
+
122
+ /**
123
+ * This method removes the mapping for a key if it is present.
124
+ *
125
+ * @returns The value that was associated with key, or undefined if none
126
+ */
127
+ delete(key: K): V | undefined;
128
+
129
+ /**
130
+ * This method removes the entry for key only if the key has the specified value.
131
+ *
132
+ * @returns true if the entry was removed
133
+ */
134
+ deleteEntry(key: K, value: V): boolean;
135
+
136
+ /**
137
+ * This method replaces the entry for key only if the key has a value.
138
+ *
139
+ * @returns The previous value, or undefined if key was not present
140
+ */
141
+ replace(key: K, value: V): V | undefined;
142
+
143
+ /**
144
+ * This method replaces the entry for key only if the key has the specified oldValue.
145
+ *
146
+ * @returns true if the value was replaced
147
+ */
148
+ replaceEntry(key: K, oldValue: V, newValue: V): boolean;
149
+
150
+ /**
151
+ * If the specified key is not already associated with a value,
152
+ * computes its value using the given function and enters it into the map.
153
+ *
154
+ * @returns The current (existing or computed) value associated with the key
155
+ */
156
+ computeIfAbsent(key: K, mappingFunction: (key: K) => V): V;
157
+
158
+ /**
159
+ * If the value for the specified key is present, tries to compute
160
+ * a new mapping given the key and its current mapped value.
161
+ *
162
+ * @returns The new value associated with the key, or undefined if none
163
+ */
164
+ computeIfPresent(key: K, remappingFunction: (key: K, value: V) => V | undefined): V | undefined;
165
+
166
+ /**
167
+ * This method tries to compute a mapping for the specified key and its current mapped value.
168
+ *
169
+ * @returns The new value associated with the key, or undefined if none
170
+ */
171
+ compute(key: K, remappingFunction: (key: K, value: V | undefined) => V | undefined): V | undefined;
172
+
173
+ /**
174
+ * If the specified key is not already associated with a value or is
175
+ * associated with undefined, associates it with the given non-undefined value.
176
+ * Otherwise, replaces the value with the results of the given remapping function.
177
+ *
178
+ * @returns The new value associated with the key, or undefined if none
179
+ */
180
+ merge(key: K, value: V, remappingFunction: (oldValue: V, newValue: V) => V | undefined): V | undefined;
181
+
182
+ /**
183
+ * This method copies all mappings from the specified map to this map.
184
+ */
185
+ putAll(other: ReadonlyMapLike<K, V> | Iterable<Entry<K, V>>): void;
186
+
187
+ /**
188
+ * This method removes all mappings from this map.
189
+ */
190
+ clear(): void;
191
+ }
192
+
193
+ /**
194
+ * SortedMap<K, V> - Map that maintains keys in sorted order.
195
+ *
196
+ * Mirrors java.util.SortedMap.
197
+ */
198
+ export interface SortedMap<K, V> extends MapLike<K, V> {
199
+ /**
200
+ * This value is the comparison function used to order keys.
201
+ */
202
+ readonly comparator: Compare<K>;
203
+
204
+ /**
205
+ * This method gives the first (lowest) key in this map.
206
+ *
207
+ * @throws Error if map is empty
208
+ */
209
+ firstKey(): K;
210
+
211
+ /**
212
+ * This method gives the last (highest) key in this map.
213
+ *
214
+ * @throws Error if map is empty
215
+ */
216
+ lastKey(): K;
217
+
218
+ /**
219
+ * This method gives a view of the portion of this map whose keys are less than toKey.
220
+ */
221
+ headMap(toKey: K): SortedMap<K, V>;
222
+
223
+ /**
224
+ * This method gives a view of the portion of this map whose keys are greater than or equal to fromKey.
225
+ */
226
+ tailMap(fromKey: K): SortedMap<K, V>;
227
+
228
+ /**
229
+ * This method gives a view of the portion of this map whose keys range from fromKey to toKey.
230
+ */
231
+ subMap(fromKey: K, toKey: K): SortedMap<K, V>;
232
+ }
233
+
234
+ /**
235
+ * NavigableMap<K, V> - Extended sorted map with navigation methods.
236
+ *
237
+ * Mirrors java.util.NavigableMap.
238
+ */
239
+ export interface NavigableMap<K, V> extends SortedMap<K, V> {
240
+ /**
241
+ * This method gives the greatest key less than or equal to the given key, or undefined if none.
242
+ */
243
+ floorKey(key: K): K | undefined;
244
+
245
+ /**
246
+ * This method gives the least key greater than or equal to the given key, or undefined if none.
247
+ */
248
+ ceilingKey(key: K): K | undefined;
249
+
250
+ /**
251
+ * This method gives the greatest key strictly less than the given key, or undefined if none.
252
+ */
253
+ lowerKey(key: K): K | undefined;
254
+
255
+ /**
256
+ * This method gives the least key strictly greater than the given key, or undefined if none.
257
+ */
258
+ higherKey(key: K): K | undefined;
259
+
260
+ /**
261
+ * This method gives the entry for the greatest key less than or equal to the given key, or undefined.
262
+ */
263
+ floorEntry(key: K): Entry<K, V> | undefined;
264
+
265
+ /**
266
+ * This method gives the entry for the least key greater than or equal to the given key, or undefined.
267
+ */
268
+ ceilingEntry(key: K): Entry<K, V> | undefined;
269
+
270
+ /**
271
+ * This method gives the entry for the greatest key strictly less than the given key, or undefined.
272
+ */
273
+ lowerEntry(key: K): Entry<K, V> | undefined;
274
+
275
+ /**
276
+ * This method gives the entry for the least key strictly greater than the given key, or undefined.
277
+ */
278
+ higherEntry(key: K): Entry<K, V> | undefined;
279
+
280
+ /**
281
+ * This method gives the first (lowest) entry in this map, or undefined if empty.
282
+ */
283
+ firstEntry(): Entry<K, V> | undefined;
284
+
285
+ /**
286
+ * This method gives the last (highest) entry in this map, or undefined if empty.
287
+ */
288
+ lastEntry(): Entry<K, V> | undefined;
289
+
290
+ /**
291
+ * Removes and gives the first (lowest) entry, or undefined if empty.
292
+ */
293
+ pollFirstEntry(): Entry<K, V> | undefined;
294
+
295
+ /**
296
+ * Removes and gives the last (highest) entry, or undefined if empty.
297
+ */
298
+ pollLastEntry(): Entry<K, V> | undefined;
299
+
300
+ /**
301
+ * This method gives a reverse order view of this map.
302
+ */
303
+ descendingMap(): NavigableMap<K, V>;
304
+
305
+ /**
306
+ * This method gives a reverse order iterable of the keys.
307
+ */
308
+ descendingKeys(): Iterable<K>;
309
+ }
310
+
311
+ /**
312
+ * MultiMap<K, V> - Map that can hold multiple values per key.
313
+ *
314
+ * Not in standard Java Collections, but commonly used.
315
+ */
316
+ export interface MultiMap<K, V> {
317
+ readonly size: number;
318
+ readonly isEmpty: boolean;
319
+
320
+ /**
321
+ * This method gives true if the map contains at least one value for the key.
322
+ */
323
+ has(key: K): boolean;
324
+
325
+ /**
326
+ * This method gives all values associated with the key.
327
+ */
328
+ get(key: K): ReadonlyCollection<V>;
329
+
330
+ /**
331
+ * This method adds a value to the collection of values for the key.
332
+ */
333
+ put(key: K, value: V): boolean;
334
+
335
+ /**
336
+ * This method adds all values to the collection of values for the key.
337
+ */
338
+ putAll(key: K, values: Iterable<V>): boolean;
339
+
340
+ /**
341
+ * This method removes a single occurrence of value from the key's collection.
342
+ */
343
+ remove(key: K, value: V): boolean;
344
+
345
+ /**
346
+ * This method removes all values for the key.
347
+ */
348
+ removeAll(key: K): ReadonlyCollection<V>;
349
+
350
+ /**
351
+ * This method replaces all values for the key with the given values.
352
+ */
353
+ replaceAll(key: K, values: Iterable<V>): ReadonlyCollection<V>;
354
+
355
+ /**
356
+ * This method gives all keys that have at least one value.
357
+ */
358
+ keys(): Iterable<K>;
359
+
360
+ /**
361
+ * This method gives all entries (key with one of its values).
362
+ */
363
+ entries(): Iterable<Entry<K, V>>;
364
+
365
+ /**
366
+ * This method removes all mappings.
367
+ */
368
+ clear(): void;
369
+ }
370
+
371
+ /**
372
+ * BiMap<K, V> - Bidirectional map (1:1 mapping).
373
+ *
374
+ * Both keys and values are unique.
375
+ * With this type, lookup is by key or by value.
376
+ */
377
+ export interface BiMap<K, V> extends MapLike<K, V> {
378
+ /**
379
+ * This method gives the inverse view of this bimap (swaps keys and values).
380
+ */
381
+ inverse(): BiMap<V, K>;
382
+
383
+ /**
384
+ * This method gives the key mapped to the specified value, or undefined if none.
385
+ */
386
+ getKey(value: V): K | undefined;
387
+
388
+ /**
389
+ * This method forces the put by removing any existing entry with the same value.
390
+ */
391
+ forcePut(key: K, value: V): V | undefined;
392
+ }
393
+
394
+ /**
395
+ * WeakMapLike<K extends object, V> - Map with weak key references.
396
+ *
397
+ * Mirrors JavaScript's WeakMap but with our interface conventions.
398
+ * Keys must be objects and are held weakly (so garbage collection can remove them).
399
+ */
400
+ export interface WeakMapLike<K extends object, V> {
401
+ has(key: K): boolean;
402
+ get(key: K): V | undefined;
403
+ set(key: K, value: V): this;
404
+ delete(key: K): boolean;
405
+ }