@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,316 @@
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";
@@ -0,0 +1,164 @@
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
+ }