@procedures/collections 0.47.1 → 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 (238) hide show
  1. package/CHANGELOG.md +18 -8
  2. package/README.md +2 -0
  3. package/dist/.tsbuildinfo +1 -1
  4. package/dist/index.d.ts +1 -59
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +3 -95
  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 +4 -4
  27. package/src/index.ts +338 -214
  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/summary-tree.d.ts +0 -146
  139. package/dist/impl/summary-tree.d.ts.map +0 -1
  140. package/dist/impl/summary-tree.js +0 -524
  141. package/dist/impl/summary-tree.js.map +0 -1
  142. package/dist/impl/tree-map.d.ts +0 -153
  143. package/dist/impl/tree-map.d.ts.map +0 -1
  144. package/dist/impl/tree-map.js +0 -788
  145. package/dist/impl/tree-map.js.map +0 -1
  146. package/dist/impl/tree-set.d.ts +0 -112
  147. package/dist/impl/tree-set.d.ts.map +0 -1
  148. package/dist/impl/tree-set.js +0 -374
  149. package/dist/impl/tree-set.js.map +0 -1
  150. package/dist/interfaces/collection.d.ts +0 -119
  151. package/dist/interfaces/collection.d.ts.map +0 -1
  152. package/dist/interfaces/collection.js +0 -8
  153. package/dist/interfaces/collection.js.map +0 -1
  154. package/dist/interfaces/list.d.ts +0 -247
  155. package/dist/interfaces/list.d.ts.map +0 -1
  156. package/dist/interfaces/list.js +0 -8
  157. package/dist/interfaces/list.js.map +0 -1
  158. package/dist/interfaces/map.d.ts +0 -344
  159. package/dist/interfaces/map.d.ts.map +0 -1
  160. package/dist/interfaces/map.js +0 -8
  161. package/dist/interfaces/map.js.map +0 -1
  162. package/dist/interfaces/queue.d.ts +0 -304
  163. package/dist/interfaces/queue.d.ts.map +0 -1
  164. package/dist/interfaces/queue.js +0 -8
  165. package/dist/interfaces/queue.js.map +0 -1
  166. package/dist/interfaces/set.d.ts +0 -137
  167. package/dist/interfaces/set.d.ts.map +0 -1
  168. package/dist/interfaces/set.js +0 -8
  169. package/dist/interfaces/set.js.map +0 -1
  170. package/dist/order/index.d.ts +0 -7
  171. package/dist/order/index.d.ts.map +0 -1
  172. package/dist/order/index.js +0 -7
  173. package/dist/order/index.js.map +0 -1
  174. package/dist/order/rank.d.ts +0 -100
  175. package/dist/order/rank.d.ts.map +0 -1
  176. package/dist/order/rank.js +0 -286
  177. package/dist/order/rank.js.map +0 -1
  178. package/dist/storage/listeners.d.ts +0 -39
  179. package/dist/storage/listeners.d.ts.map +0 -1
  180. package/dist/storage/listeners.js +0 -65
  181. package/dist/storage/listeners.js.map +0 -1
  182. package/dist/testing/check.d.ts +0 -25
  183. package/dist/testing/check.d.ts.map +0 -1
  184. package/dist/testing/check.js +0 -85
  185. package/dist/testing/check.js.map +0 -1
  186. package/dist/utils/defaults.d.ts +0 -135
  187. package/dist/utils/defaults.d.ts.map +0 -1
  188. package/dist/utils/defaults.js +0 -322
  189. package/dist/utils/defaults.js.map +0 -1
  190. package/dist/utils/factories.d.ts +0 -144
  191. package/dist/utils/factories.d.ts.map +0 -1
  192. package/dist/utils/factories.js +0 -293
  193. package/dist/utils/factories.js.map +0 -1
  194. package/dist/utils/helpers.d.ts +0 -137
  195. package/dist/utils/helpers.d.ts.map +0 -1
  196. package/dist/utils/helpers.js +0 -221
  197. package/dist/utils/helpers.js.map +0 -1
  198. package/src/async/async-queue.ts +0 -481
  199. package/src/async/channels.ts +0 -450
  200. package/src/async/settled.ts +0 -41
  201. package/src/behaviors/bounded.ts +0 -485
  202. package/src/behaviors/composite.ts +0 -99
  203. package/src/behaviors/evented.ts +0 -464
  204. package/src/behaviors/lru.ts +0 -447
  205. package/src/behaviors/readonly.ts +0 -236
  206. package/src/behaviors/safe.ts +0 -372
  207. package/src/behaviors/synchronized.ts +0 -316
  208. package/src/behaviors/ttl.ts +0 -462
  209. package/src/contracts/model.ts +0 -44
  210. package/src/core/effects.ts +0 -323
  211. package/src/core/events.ts +0 -356
  212. package/src/core/middleware.ts +0 -415
  213. package/src/core/policies.ts +0 -316
  214. package/src/core/traits.ts +0 -164
  215. package/src/fx/collectors.ts +0 -500
  216. package/src/fx/iter.ts +0 -536
  217. package/src/impl/array-deque.ts +0 -463
  218. package/src/impl/array-list.ts +0 -490
  219. package/src/impl/hash-map.ts +0 -623
  220. package/src/impl/hash-set.ts +0 -342
  221. package/src/impl/linked-hash-map.ts +0 -572
  222. package/src/impl/linked-list.ts +0 -606
  223. package/src/impl/priority-queue.ts +0 -448
  224. package/src/impl/summary-tree.ts +0 -586
  225. package/src/impl/tree-map.ts +0 -912
  226. package/src/impl/tree-set.ts +0 -469
  227. package/src/interfaces/collection.ts +0 -141
  228. package/src/interfaces/list.ts +0 -281
  229. package/src/interfaces/map.ts +0 -405
  230. package/src/interfaces/queue.ts +0 -353
  231. package/src/interfaces/set.ts +0 -158
  232. package/src/order/index.ts +0 -6
  233. package/src/order/rank.ts +0 -271
  234. package/src/storage/listeners.ts +0 -72
  235. package/src/testing/check.ts +0 -81
  236. package/src/utils/defaults.ts +0 -345
  237. package/src/utils/factories.ts +0 -357
  238. package/src/utils/helpers.ts +0 -248
@@ -1,316 +0,0 @@
1
- /**
2
- * Policy types for configurable collection behaviors.
3
- *
4
- * Policies define how collections handle edge cases like overflow,
5
- * underflow, and eviction. These are used by behavior decorators
6
- * to provide flexible, composable behavior.
7
- */
8
-
9
- // ============================================================================
10
- // Overflow Policies - what to do when a bounded collection is full
11
- // ============================================================================
12
-
13
- /**
14
- * Policy for handling overflow when adding to a full bounded collection.
15
- *
16
- * - 'throw': Throw an error when attempting to add to a full collection
17
- * - 'drop-oldest': Remove the oldest element and add the new one
18
- * - 'drop-newest': Ignore the new element (don't add it)
19
- * - 'reject': Return false/None but don't throw (graceful failure)
20
- * - 'grow': Automatically increase capacity (becomes unbounded)
21
- * - 'block': For async collections, wait until space is available
22
- */
23
- export type OverflowPolicy =
24
- | "throw"
25
- | "drop-oldest"
26
- | "drop-newest"
27
- | "reject"
28
- | "grow"
29
- | "block";
30
-
31
- /**
32
- * Context information provided when overflow occurs.
33
- */
34
- export interface OverflowContext<T> {
35
- /** The element that triggered the overflow. */
36
- readonly newElement: T;
37
- /** Current size of the collection. */
38
- readonly currentSize: number;
39
- /** Maximum capacity of the collection. */
40
- readonly capacity: number;
41
- /** Timestamp when overflow occurred. */
42
- readonly timestamp: number;
43
- }
44
-
45
- /**
46
- * Callback invoked when overflow occurs.
47
- * Can be used for logging, metrics, or custom handling.
48
- */
49
- export type OverflowHandler<T> = (context: OverflowContext<T>) => void;
50
-
51
- // ============================================================================
52
- // Underflow Policies - what to do when removing from an empty collection
53
- // ============================================================================
54
-
55
- /**
56
- * Policy for handling underflow when removing from an empty collection.
57
- *
58
- * - 'throw': Throw an error when attempting to remove from empty collection
59
- * - 'return-none': Return None/undefined (graceful failure)
60
- * - 'return-default': Return a default value provided by user
61
- * - 'block': For async collections, wait until an element is available
62
- */
63
- export type UnderflowPolicy =
64
- | "throw"
65
- | "return-none"
66
- | "return-default"
67
- | "block";
68
-
69
- /**
70
- * Context information provided when underflow occurs.
71
- */
72
- export interface UnderflowContext {
73
- /** The operation that triggered the underflow. */
74
- readonly operation: string;
75
- /** Timestamp when underflow occurred. */
76
- readonly timestamp: number;
77
- }
78
-
79
- /**
80
- * Callback invoked when underflow occurs.
81
- */
82
- export type UnderflowHandler = (context: UnderflowContext) => void;
83
-
84
- // ============================================================================
85
- // Eviction Policies - how to choose which element to remove
86
- // ============================================================================
87
-
88
- /**
89
- * Policy for choosing which element to evict from a bounded collection.
90
- *
91
- * - 'lru': Least Recently Used - evict element not accessed for longest time
92
- * - 'lfu': Least Frequently Used - evict element accessed least often
93
- * - 'fifo': First In First Out - evict oldest inserted element
94
- * - 'lifo': Last In First Out - evict newest inserted element (stack-like)
95
- * - 'random': Evict a random element
96
- * - 'ttl': Time To Live - evict elements that have expired
97
- * - 'priority': Evict element with lowest priority
98
- * - 'custom': User-provided eviction function
99
- */
100
- export type EvictionPolicy =
101
- | "lru"
102
- | "lfu"
103
- | "fifo"
104
- | "lifo"
105
- | "random"
106
- | "ttl"
107
- | "priority"
108
- | "custom";
109
-
110
- /**
111
- * Metadata tracked for eviction decisions.
112
- */
113
- export interface EvictionMetadata {
114
- /** When the element was inserted. */
115
- readonly insertedAt: number;
116
- /** When the element was last accessed. */
117
- lastAccessedAt: number;
118
- /** How many times the element has been accessed. */
119
- accessCount: number;
120
- /** Time-to-live expiration timestamp (if applicable). */
121
- readonly expiresAt?: number;
122
- /** Priority value (higher = more important, if applicable). */
123
- readonly priority?: number;
124
- }
125
-
126
- /**
127
- * Information about an element that can be evicted.
128
- */
129
- export interface EvictionCandidate<T> {
130
- /** The element value. */
131
- readonly element: T;
132
- /** Eviction metadata for this element. */
133
- readonly metadata: EvictionMetadata;
134
- }
135
-
136
- /**
137
- * Custom eviction function.
138
- * Given candidates, returns the element to evict.
139
- */
140
- export type EvictionSelector<T> = (
141
- candidates: EvictionCandidate<T>[]
142
- ) => T | undefined;
143
-
144
- // ============================================================================
145
- // Retry Policies - for async operations
146
- // ============================================================================
147
-
148
- /**
149
- * Policy for retrying failed async operations.
150
- */
151
- export interface RetryPolicy {
152
- /** Maximum number of retry attempts. */
153
- readonly maxAttempts: number;
154
- /** Initial delay in milliseconds. */
155
- readonly initialDelay: number;
156
- /** Backoff strategy. */
157
- readonly backoff: "none" | "linear" | "exponential";
158
- /** Maximum delay cap (for backoff). */
159
- readonly maxDelay?: number;
160
- /** Jitter to add randomness and avoid thundering herd. */
161
- readonly jitter?: boolean;
162
- }
163
-
164
- /**
165
- * Context for retry decisions.
166
- */
167
- export interface RetryContext {
168
- /** Current attempt number (1-indexed). */
169
- readonly attempt: number;
170
- /** The error that caused the retry. */
171
- readonly error: unknown;
172
- /** Elapsed time since first attempt. */
173
- readonly elapsedMs: number;
174
- }
175
-
176
- /**
177
- * Callback to determine if an error is retryable.
178
- */
179
- export type RetryPredicate = (context: RetryContext) => boolean;
180
-
181
- // ============================================================================
182
- // Conflict Resolution Policies - for concurrent modifications
183
- // ============================================================================
184
-
185
- /**
186
- * Policy for resolving conflicts when same key is modified concurrently.
187
- *
188
- * - 'last-write-wins': Most recent write takes precedence
189
- * - 'first-write-wins': First write takes precedence
190
- * - 'merge': Call merge function to combine values
191
- * - 'throw': Throw an error on conflict
192
- */
193
- export type ConflictResolutionPolicy =
194
- | "last-write-wins"
195
- | "first-write-wins"
196
- | "merge"
197
- | "throw";
198
-
199
- /**
200
- * Function to merge conflicting values.
201
- */
202
- export type MergeFunction<T> = (current: T, incoming: T) => T;
203
-
204
- // ============================================================================
205
- // Compaction Policies - for memory management
206
- // ============================================================================
207
-
208
- /**
209
- * Policy for when to compact internal storage.
210
- */
211
- export interface CompactionPolicy {
212
- /** Compact when capacity utilization drops below this ratio (0-1). */
213
- readonly minLoadFactor?: number;
214
- /** Compact when wasted space exceeds this many elements. */
215
- readonly maxWastedSpace?: number;
216
- /** Automatically compact on every removal. */
217
- readonly autoCompact?: boolean;
218
- }
219
-
220
- // ============================================================================
221
- // Growth Policies - for dynamic capacity
222
- // ============================================================================
223
-
224
- /**
225
- * Policy for how to grow capacity when needed.
226
- */
227
- export interface GrowthPolicy {
228
- /** Growth strategy. */
229
- readonly strategy: "double" | "linear" | "golden-ratio" | "custom";
230
- /** For linear growth, how many elements to add. */
231
- readonly linearIncrement?: number;
232
- /** For custom growth, a function to calculate new capacity. */
233
- readonly customGrowth?: (currentCapacity: number) => number;
234
- /** Maximum capacity limit (prevent unbounded growth). */
235
- readonly maxCapacity?: number;
236
- }
237
-
238
- // ============================================================================
239
- // Validation Policies - for type checking
240
- // ============================================================================
241
-
242
- /**
243
- * Validation action when an invalid element is encountered.
244
- *
245
- * - 'throw': Throw an error
246
- * - 'drop': Silently drop the invalid element
247
- * - 'coerce': Attempt to coerce to valid type
248
- * - 'log': Log warning but allow
249
- */
250
- export type ValidationPolicy = "throw" | "drop" | "coerce" | "log";
251
-
252
- /**
253
- * Context for validation failures.
254
- */
255
- export interface ValidationContext<T> {
256
- /** The invalid element. */
257
- readonly element: T;
258
- /** Validation error message/details. */
259
- readonly error: unknown;
260
- /** The operation that triggered validation. */
261
- readonly operation: string;
262
- }
263
-
264
- /**
265
- * Callback invoked on validation failures.
266
- */
267
- export type ValidationHandler<T> = (context: ValidationContext<T>) => void;
268
-
269
- // ============================================================================
270
- // Default policies
271
- // ============================================================================
272
-
273
- /**
274
- * Default overflow policy: throw an error.
275
- */
276
- export const DEFAULT_OVERFLOW_POLICY: OverflowPolicy = "throw";
277
-
278
- /**
279
- * Default underflow policy: throw an error.
280
- */
281
- export const DEFAULT_UNDERFLOW_POLICY: UnderflowPolicy = "throw";
282
-
283
- /**
284
- * Default eviction policy: FIFO (oldest first).
285
- */
286
- export const DEFAULT_EVICTION_POLICY: EvictionPolicy = "fifo";
287
-
288
- /**
289
- * Default retry policy: 3 attempts with exponential backoff.
290
- */
291
- export const DEFAULT_RETRY_POLICY: RetryPolicy = {
292
- maxAttempts: 3,
293
- initialDelay: 100,
294
- backoff: "exponential",
295
- maxDelay: 5000,
296
- jitter: true,
297
- };
298
-
299
- /**
300
- * Default conflict resolution: last write wins.
301
- */
302
- export const DEFAULT_CONFLICT_POLICY: ConflictResolutionPolicy =
303
- "last-write-wins";
304
-
305
- /**
306
- * Default growth policy: double capacity.
307
- */
308
- export const DEFAULT_GROWTH_POLICY: GrowthPolicy = {
309
- strategy: "double",
310
- maxCapacity: Number.MAX_SAFE_INTEGER,
311
- };
312
-
313
- /**
314
- * Default validation policy: throw on invalid.
315
- */
316
- export const DEFAULT_VALIDATION_POLICY: ValidationPolicy = "throw";
@@ -1,164 +0,0 @@
1
- /**
2
- * Core traits and capability interfaces for the collections framework.
3
- *
4
- * These traits define fundamental capabilities that collections can have,
5
- * following the Interface Segregation Principle (SOLID).
6
- */
7
-
8
- /**
9
- * Equality comparison function.
10
- * Returns true if two values are considered equal.
11
- *
12
- * @example
13
- * const primitiveEq: Eq<number> = (a, b) => a === b
14
- * const objectEq: Eq<Person> = (a, b) => a.id === b.id
15
- */
16
- export type Eq<T> = (a: T, b: T) => boolean;
17
-
18
- /**
19
- * Hash function for creating hash codes.
20
- * Returns a 32-bit integer hash of the value.
21
- *
22
- * Must satisfy: if eq(a, b) === true, then hash(a) === hash(b)
23
- *
24
- * @example
25
- * const stringHash: Hash<string> = (s) => {
26
- * let h = 0;
27
- * for (let i = 0; i < s.length; i++) {
28
- * h = ((h << 5) - h) + s.charCodeAt(i);
29
- * h = h & h; // Convert to 32-bit integer
30
- * }
31
- * return h;
32
- * }
33
- */
34
- export type Hash<T> = (value: T) => number;
35
-
36
- /**
37
- * Comparison function for ordering values.
38
- * Returns:
39
- * - negative number if a < b
40
- * - zero if a === b
41
- * - positive number if a > b
42
- *
43
- * Must be transitive and consistent with equality.
44
- *
45
- * @example
46
- * const numberCompare: Compare<number> = (a, b) => a - b
47
- * const reverseCompare = <T>(cmp: Compare<T>): Compare<T> => (a, b) => cmp(b, a)
48
- */
49
- export type Compare<T> = (a: T, b: T) => number;
50
-
51
- /**
52
- * Capability: Has a size property.
53
- */
54
- export interface Sized {
55
- /** The number of elements in the collection. */
56
- readonly size: number;
57
- }
58
-
59
- /**
60
- * Capability: Can be cleared (all elements removed).
61
- */
62
- export interface Clearable {
63
- /** Removes all elements from the collection. */
64
- clear(): void;
65
- }
66
-
67
- /**
68
- * Capability: Can check if empty.
69
- */
70
- export interface Emptiable {
71
- /** Returns true if the collection contains no elements. */
72
- readonly isEmpty: boolean;
73
- }
74
-
75
- /**
76
- * Capability: Can be cloned.
77
- */
78
- export interface Cloneable<T> {
79
- /** Creates a shallow copy of the collection. */
80
- clone(): T;
81
- }
82
-
83
- /**
84
- * Capability: Can be converted to an array.
85
- */
86
- export interface ArrayConvertible<T> {
87
- /** Returns an array containing all elements in the collection. */
88
- toArray(): T[];
89
- }
90
-
91
- /**
92
- * Base read-only collection capabilities.
93
- * Combines common traits that most collections share.
94
- */
95
- export interface ReadonlyCollectionBase<T>
96
- extends Sized,
97
- Emptiable,
98
- Iterable<T>,
99
- ArrayConvertible<T> {}
100
-
101
- /**
102
- * Base mutable collection capabilities.
103
- * Adds mutation operations to readonly base.
104
- */
105
- export interface MutableCollectionBase<T>
106
- extends ReadonlyCollectionBase<T>,
107
- Clearable {}
108
-
109
- /**
110
- * Equality provider - allows collections to specify custom equality.
111
- */
112
- export interface EqualityProvider<T> {
113
- /** The equality function used by this collection. */
114
- readonly eq: Eq<T>;
115
- }
116
-
117
- /**
118
- * Hash provider - allows collections to specify custom hashing.
119
- */
120
- export interface HashProvider<T> {
121
- /** The hash function used by this collection. */
122
- readonly hash: Hash<T>;
123
- }
124
-
125
- /**
126
- * Comparison provider - allows collections to specify custom ordering.
127
- */
128
- export interface ComparisonProvider<T> {
129
- /** The comparison function used by this collection. */
130
- readonly compare: Compare<T>;
131
- }
132
-
133
- /**
134
- * Bounded collection - has a maximum capacity.
135
- */
136
- export interface Bounded {
137
- /** The maximum number of elements this collection can hold. */
138
- readonly capacity: number;
139
-
140
- /** Returns true if the collection is at capacity. */
141
- readonly isFull: boolean;
142
-
143
- /** Returns the number of additional elements that can be added. */
144
- readonly remainingCapacity: number;
145
- }
146
-
147
- /**
148
- * Disposable resource - can be explicitly cleaned up.
149
- */
150
- export interface Disposable {
151
- /** Releases any resources held by this collection. */
152
- dispose(): void;
153
- }
154
-
155
- /**
156
- * Freezable collection - can be made immutable.
157
- */
158
- export interface Freezable<T> {
159
- /** Makes this collection immutable. Returns self for chaining. */
160
- freeze(): T;
161
-
162
- /** Returns true if this collection is frozen. */
163
- readonly isFrozen: boolean;
164
- }