@mark1russell7/collections 0.0.0-stage → 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.
- package/LICENSE +21 -0
- package/README.md +78 -2
- package/build/dag/index.d.ts +144 -0
- package/build/dag/index.d.ts.map +1 -0
- package/build/dag/index.js +294 -0
- package/build/dag/index.js.map +1 -0
- package/build/graph/index.d.ts +20 -0
- package/build/graph/index.d.ts.map +1 -0
- package/build/graph/index.js +48 -0
- package/build/graph/index.js.map +1 -0
- package/build/index.d.ts +4952 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +7811 -0
- package/build/index.js.map +1 -0
- package/build/testing/index.d.ts +25 -0
- package/build/testing/index.d.ts.map +1 -0
- package/build/testing/index.js +72 -0
- package/build/testing/index.js.map +1 -0
- package/package.json +77 -3
- package/src/async/async-queue.ts +481 -0
- package/src/async/channels.ts +450 -0
- package/src/async/settled.ts +41 -0
- package/src/behaviors/bounded.ts +485 -0
- package/src/behaviors/composite.ts +99 -0
- package/src/behaviors/evented.ts +464 -0
- package/src/behaviors/lru.ts +449 -0
- package/src/behaviors/readonly.ts +236 -0
- package/src/behaviors/safe.ts +358 -0
- package/src/behaviors/synchronized.ts +316 -0
- package/src/behaviors/ttl.ts +464 -0
- package/src/core/effects.ts +327 -0
- package/src/core/events.ts +363 -0
- package/src/core/middleware.ts +413 -0
- package/src/core/policies.ts +316 -0
- package/src/core/traits.ts +164 -0
- package/src/dag/builder.ts +124 -0
- package/src/dag/executor.ts +190 -0
- package/src/dag/index.ts +14 -0
- package/src/dag/traversal.ts +129 -0
- package/src/dag/types.ts +87 -0
- package/src/fx/collectors.ts +500 -0
- package/src/fx/iter.ts +536 -0
- package/src/graph/hub-order.ts +53 -0
- package/src/graph/index.ts +5 -0
- package/src/impl/array-deque.ts +463 -0
- package/src/impl/array-list.ts +490 -0
- package/src/impl/hash-map.ts +623 -0
- package/src/impl/hash-set.ts +342 -0
- package/src/impl/linked-hash-map.ts +572 -0
- package/src/impl/linked-list.ts +610 -0
- package/src/impl/priority-queue.ts +448 -0
- package/src/impl/summary-tree.ts +594 -0
- package/src/impl/tree-map.ts +913 -0
- package/src/impl/tree-set.ts +469 -0
- package/src/index.ts +237 -0
- package/src/interfaces/collection.ts +141 -0
- package/src/interfaces/list.ts +281 -0
- package/src/interfaces/map.ts +405 -0
- package/src/interfaces/queue.ts +353 -0
- package/src/interfaces/set.ts +158 -0
- package/src/order/index.ts +6 -0
- package/src/order/rank.ts +271 -0
- package/src/storage/listeners.ts +72 -0
- package/src/testing/check.ts +81 -0
- package/src/testing/index.ts +7 -0
- package/src/utils/defaults.ts +345 -0
- package/src/utils/factories.ts +357 -0
- package/src/utils/helpers.ts +248 -0
package/build/index.d.ts
ADDED
|
@@ -0,0 +1,4952 @@
|
|
|
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
|
+
* Equality comparison function.
|
|
9
|
+
* This type gives true if two values are considered equal.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const primitiveEq: Eq<number> = (a, b) => a === b
|
|
13
|
+
* const objectEq: Eq<Person> = (a, b) => a.id === b.id
|
|
14
|
+
*/
|
|
15
|
+
export type Eq<T> = (a: T, b: T) => boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Hash function for making hash codes.
|
|
18
|
+
* This type gives a 32-bit integer hash of the value.
|
|
19
|
+
*
|
|
20
|
+
* The function must satisfy: if eq(a, b) === true, then hash(a) === hash(b)
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* const stringHash: Hash<string> = (s) => {
|
|
24
|
+
* let h = 0;
|
|
25
|
+
* for (let i = 0; i < s.length; i++) {
|
|
26
|
+
* h = ((h << 5) - h) + s.charCodeAt(i);
|
|
27
|
+
* h = h & h; // Convert to 32-bit integer
|
|
28
|
+
* }
|
|
29
|
+
* return h;
|
|
30
|
+
* }
|
|
31
|
+
*/
|
|
32
|
+
export type Hash<T> = (value: T) => number;
|
|
33
|
+
/**
|
|
34
|
+
* Comparison function for ordering values.
|
|
35
|
+
* This type gives:
|
|
36
|
+
* - negative number if a < b
|
|
37
|
+
* - zero if a === b
|
|
38
|
+
* - positive number if a > b
|
|
39
|
+
*
|
|
40
|
+
* The function must be transitive and consistent with equality.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* const numberCompare: Compare<number> = (a, b) => a - b
|
|
44
|
+
* const reverseCompare = <T>(cmp: Compare<T>): Compare<T> => (a, b) => cmp(b, a)
|
|
45
|
+
*/
|
|
46
|
+
export type Compare<T> = (a: T, b: T) => number;
|
|
47
|
+
/**
|
|
48
|
+
* Capability: Has a size property.
|
|
49
|
+
*/
|
|
50
|
+
export interface Sized {
|
|
51
|
+
/** The number of elements in the collection. */
|
|
52
|
+
readonly size: number;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Capability: Can be cleared (all elements removed).
|
|
56
|
+
*/
|
|
57
|
+
export interface Clearable {
|
|
58
|
+
/** This method removes all elements from the collection. */
|
|
59
|
+
clear(): void;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Capability: Can check if empty.
|
|
63
|
+
*/
|
|
64
|
+
export interface Emptiable {
|
|
65
|
+
/** This value gives true if the collection contains no elements. */
|
|
66
|
+
readonly isEmpty: boolean;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Capability: Can be cloned.
|
|
70
|
+
*/
|
|
71
|
+
export interface Cloneable<T> {
|
|
72
|
+
/** This method makes a shallow copy of the collection. */
|
|
73
|
+
clone(): T;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Capability: Can be converted to an array.
|
|
77
|
+
*/
|
|
78
|
+
export interface ArrayConvertible<T> {
|
|
79
|
+
/** This method gives an array containing all elements in the collection. */
|
|
80
|
+
toArray(): T[];
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Base read-only collection capabilities.
|
|
84
|
+
* This type combines common traits that most collections share.
|
|
85
|
+
*/
|
|
86
|
+
export interface ReadonlyCollectionBase<T> extends Sized, Emptiable, Iterable<T>, ArrayConvertible<T> {}
|
|
87
|
+
/**
|
|
88
|
+
* Base mutable collection capabilities.
|
|
89
|
+
* This type adds mutation operations to readonly base.
|
|
90
|
+
*/
|
|
91
|
+
export interface MutableCollectionBase<T> extends ReadonlyCollectionBase<T>, Clearable {}
|
|
92
|
+
/**
|
|
93
|
+
* Equality provider - with it, collections can specify custom equality.
|
|
94
|
+
*/
|
|
95
|
+
export interface EqualityProvider<T> {
|
|
96
|
+
/** The equality function used by this collection. */
|
|
97
|
+
readonly eq: Eq<T>;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Hash provider - with it, collections can specify custom hashing.
|
|
101
|
+
*/
|
|
102
|
+
export interface HashProvider<T> {
|
|
103
|
+
/** The hash function used by this collection. */
|
|
104
|
+
readonly hash: Hash<T>;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Comparison provider - with it, collections can specify custom ordering.
|
|
108
|
+
*/
|
|
109
|
+
export interface ComparisonProvider<T> {
|
|
110
|
+
/** The comparison function used by this collection. */
|
|
111
|
+
readonly compare: Compare<T>;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Bounded collection - has a maximum capacity.
|
|
115
|
+
*/
|
|
116
|
+
export interface Bounded {
|
|
117
|
+
/** The maximum number of elements this collection can hold. */
|
|
118
|
+
readonly capacity: number;
|
|
119
|
+
/** This value gives true if the collection is at capacity. */
|
|
120
|
+
readonly isFull: boolean;
|
|
121
|
+
/** This value gives the number of elements that the collection can still take. */
|
|
122
|
+
readonly remainingCapacity: number;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Disposable resource - can be explicitly cleaned up.
|
|
126
|
+
*/
|
|
127
|
+
export interface Disposable {
|
|
128
|
+
/** This method releases any resources held by this collection. */
|
|
129
|
+
dispose(): void;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Freezable collection - can be made immutable.
|
|
133
|
+
*/
|
|
134
|
+
export interface Freezable<T> {
|
|
135
|
+
/** This method makes this collection immutable. This method gives self for chaining. */
|
|
136
|
+
freeze(): T;
|
|
137
|
+
/** This value gives true if this collection is frozen. */
|
|
138
|
+
readonly isFrozen: boolean;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Functional effect types for error handling and optional values.
|
|
142
|
+
*
|
|
143
|
+
* This module gives Option<T> and Result<T,E> types for explicit, type-safe
|
|
144
|
+
* error handling without exceptions. This module includes async variants.
|
|
145
|
+
*/
|
|
146
|
+
/**
|
|
147
|
+
* This type represents the absence of a value.
|
|
148
|
+
*/
|
|
149
|
+
export interface None {
|
|
150
|
+
readonly _tag: "None";
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* This type represents the presence of a value.
|
|
154
|
+
*/
|
|
155
|
+
export interface Some<T> {
|
|
156
|
+
readonly _tag: "Some";
|
|
157
|
+
readonly value: T;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Option<T> represents a value that is present or absent.
|
|
161
|
+
* This is a type-safe alternative to null/undefined.
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* function findUser(id: string): Option<User> {
|
|
165
|
+
* const user = db.get(id);
|
|
166
|
+
* return user ? Some(user) : None;
|
|
167
|
+
* }
|
|
168
|
+
*/
|
|
169
|
+
export type Option<T> = None | Some<T>;
|
|
170
|
+
/**
|
|
171
|
+
* The singleton None value.
|
|
172
|
+
*/
|
|
173
|
+
export declare const None: None;
|
|
174
|
+
/**
|
|
175
|
+
* This function constructs a Some value containing the given value.
|
|
176
|
+
*/
|
|
177
|
+
export declare const Some: <T>(value: T) => Some<T>;
|
|
178
|
+
/**
|
|
179
|
+
* Type guard to check if an Option is None.
|
|
180
|
+
*/
|
|
181
|
+
export declare const isNone: <T>(opt: Option<T>) => opt is None;
|
|
182
|
+
/**
|
|
183
|
+
* Type guard to check if an Option is Some.
|
|
184
|
+
*/
|
|
185
|
+
export declare const isSome: <T>(opt: Option<T>) => opt is Some<T>;
|
|
186
|
+
/**
|
|
187
|
+
* This function extracts the value from a Some, or gives the default value if None.
|
|
188
|
+
*/
|
|
189
|
+
export declare const getOrElse: <T>(opt: Option<T>, defaultValue: T) => T;
|
|
190
|
+
/**
|
|
191
|
+
* This function extracts the value from a Some, or throws an error if None.
|
|
192
|
+
*/
|
|
193
|
+
export declare const unwrap: <T>(opt: Option<T>) => T;
|
|
194
|
+
/**
|
|
195
|
+
* This function maps a function over the value in a Some, or gives None.
|
|
196
|
+
*/
|
|
197
|
+
export declare const mapOption: <T, U>(opt: Option<T>, f: (value: T) => U) => Option<U>;
|
|
198
|
+
/**
|
|
199
|
+
* Chains Option-returning operations.
|
|
200
|
+
*/
|
|
201
|
+
export declare const flatMapOption: <T, U>(opt: Option<T>, f: (value: T) => Option<U>) => Option<U>;
|
|
202
|
+
/**
|
|
203
|
+
* This function converts a nullable value to an Option.
|
|
204
|
+
*/
|
|
205
|
+
export declare const fromNullable: <T>(value: T | null | undefined) => Option<T>;
|
|
206
|
+
/**
|
|
207
|
+
* This function converts an Option to a nullable value.
|
|
208
|
+
*/
|
|
209
|
+
export declare const toNullable: <T>(opt: Option<T>) => T | undefined;
|
|
210
|
+
/**
|
|
211
|
+
* This type represents a successful result containing a value.
|
|
212
|
+
*/
|
|
213
|
+
export interface Ok<T> {
|
|
214
|
+
readonly _tag: "Ok";
|
|
215
|
+
readonly value: T;
|
|
216
|
+
/** This method calls the `Ok` case with the value. */
|
|
217
|
+
match<U>(cases: {
|
|
218
|
+
Ok: (value: T) => U;
|
|
219
|
+
Err: (error: never) => U;
|
|
220
|
+
}): U;
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* This type represents a failed result containing an error.
|
|
224
|
+
*/
|
|
225
|
+
export interface Err<E> {
|
|
226
|
+
readonly _tag: "Err";
|
|
227
|
+
readonly error: E;
|
|
228
|
+
/** This method calls the `Err` case with the error. */
|
|
229
|
+
match<U>(cases: {
|
|
230
|
+
Ok: (value: never) => U;
|
|
231
|
+
Err: (error: E) => U;
|
|
232
|
+
}): U;
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Result<T, E> represents the result of an operation that can fail.
|
|
236
|
+
* It contains either a success value (Ok) or an error value (Err).
|
|
237
|
+
*
|
|
238
|
+
* @example
|
|
239
|
+
* function divide(a: number, b: number): Result<number, string> {
|
|
240
|
+
* if (b === 0) return Err("Division by zero");
|
|
241
|
+
* return Ok(a / b);
|
|
242
|
+
* }
|
|
243
|
+
*/
|
|
244
|
+
export type Result<T, E> = Ok<T> | Err<E>;
|
|
245
|
+
/**
|
|
246
|
+
* This function constructs an Ok result containing the given value.
|
|
247
|
+
*/
|
|
248
|
+
export declare const Ok: <T>(value: T) => Ok<T>;
|
|
249
|
+
/**
|
|
250
|
+
* This function constructs an Err result containing the given error.
|
|
251
|
+
*/
|
|
252
|
+
export declare const Err: <E>(error: E) => Err<E>;
|
|
253
|
+
/**
|
|
254
|
+
* Type guard to check if a Result is Ok.
|
|
255
|
+
*/
|
|
256
|
+
export declare const isOk: <T, E>(result: Result<T, E>) => result is Ok<T>;
|
|
257
|
+
/**
|
|
258
|
+
* Type guard to check if a Result is Err.
|
|
259
|
+
*/
|
|
260
|
+
export declare const isErr: <T, E>(result: Result<T, E>) => result is Err<E>;
|
|
261
|
+
/**
|
|
262
|
+
* This function extracts the value from an Ok, or gives the default value if Err.
|
|
263
|
+
*/
|
|
264
|
+
export declare const getOrElseResult: <T, E>(result: Result<T, E>, defaultValue: T) => T;
|
|
265
|
+
/**
|
|
266
|
+
* This function extracts the value from an Ok, or throws the error if Err.
|
|
267
|
+
*/
|
|
268
|
+
export declare const unwrapResult: <T, E>(result: Result<T, E>) => T;
|
|
269
|
+
/**
|
|
270
|
+
* This function maps a function over the value in an Ok, or gives the Err unchanged.
|
|
271
|
+
*/
|
|
272
|
+
export declare const mapResult: <T, U, E>(result: Result<T, E>, f: (value: T) => U) => Result<U, E>;
|
|
273
|
+
/**
|
|
274
|
+
* This function maps a function over the error in an Err, or gives the Ok unchanged.
|
|
275
|
+
*/
|
|
276
|
+
export declare const mapError: <T, E, F>(result: Result<T, E>, f: (error: E) => F) => Result<T, F>;
|
|
277
|
+
/**
|
|
278
|
+
* Chains Result-returning operations.
|
|
279
|
+
*/
|
|
280
|
+
export declare const flatMapResult: <T, U, E>(result: Result<T, E>, f: (value: T) => Result<U, E>) => Result<U, E>;
|
|
281
|
+
/**
|
|
282
|
+
* This function converts a throwing function into a Result-returning function.
|
|
283
|
+
*/
|
|
284
|
+
export declare const tryCatch: <T, E = unknown>(fn: () => T, onError?: (error: unknown) => E) => Result<T, E>;
|
|
285
|
+
/**
|
|
286
|
+
* This function converts an async throwing function into a Result-returning async function.
|
|
287
|
+
*/
|
|
288
|
+
export declare const tryCatchAsync: <T, E = unknown>(fn: () => Promise<T>, onError?: (error: unknown) => E) => Promise<Result<T, E>>;
|
|
289
|
+
/**
|
|
290
|
+
* Async variant of Option<T>.
|
|
291
|
+
*/
|
|
292
|
+
export type AsyncOption<T> = Promise<Option<T>>;
|
|
293
|
+
/**
|
|
294
|
+
* Async variant of Result<T, E>.
|
|
295
|
+
*/
|
|
296
|
+
export type AsyncResult<T, E> = Promise<Result<T, E>>;
|
|
297
|
+
/**
|
|
298
|
+
* This function maps a function over an async Option.
|
|
299
|
+
*/
|
|
300
|
+
export declare const mapAsyncOption: <T, U>(opt: AsyncOption<T>, f: (value: T) => U | Promise<U>) => AsyncOption<U>;
|
|
301
|
+
/**
|
|
302
|
+
* This function maps a function over an async Result.
|
|
303
|
+
*/
|
|
304
|
+
export declare const mapAsyncResult: <T, U, E>(result: AsyncResult<T, E>, f: (value: T) => U | Promise<U>) => AsyncResult<U, E>;
|
|
305
|
+
/**
|
|
306
|
+
* This function converts an Option to a Result.
|
|
307
|
+
* None becomes Err with the given error.
|
|
308
|
+
*/
|
|
309
|
+
export declare const optionToResult: <T, E>(opt: Option<T>, error: E) => Result<T, E>;
|
|
310
|
+
/**
|
|
311
|
+
* This function converts a Result to an Option.
|
|
312
|
+
* Err becomes None, discarding the error.
|
|
313
|
+
*/
|
|
314
|
+
export declare const resultToOption: <T, E>(result: Result<T, E>) => Option<T>;
|
|
315
|
+
/**
|
|
316
|
+
* This function combines multiple Options into an Option of an array.
|
|
317
|
+
* This function gives None if any input is None.
|
|
318
|
+
*/
|
|
319
|
+
export declare const sequence: <T>(opts: Option<T>[]) => Option<T[]>;
|
|
320
|
+
/**
|
|
321
|
+
* This function combines multiple Results into a Result of an array.
|
|
322
|
+
* This function gives the first Err encountered, or Ok with all values.
|
|
323
|
+
*/
|
|
324
|
+
export declare const sequenceResults: <T, E>(results: Result<T, E>[]) => Result<T[], E>;
|
|
325
|
+
/**
|
|
326
|
+
* Policy types for configurable collection behaviors.
|
|
327
|
+
*
|
|
328
|
+
* Policies define how collections handle edge cases like overflow,
|
|
329
|
+
* underflow, and eviction. These are used by behavior decorators
|
|
330
|
+
* to give flexible, composable behavior.
|
|
331
|
+
*/
|
|
332
|
+
/**
|
|
333
|
+
* Policy for handling overflow when adding to a full bounded collection.
|
|
334
|
+
*
|
|
335
|
+
* - 'throw': Throw an error when trying to add to a full collection
|
|
336
|
+
* - 'drop-oldest': Remove the oldest element and add the new one
|
|
337
|
+
* - 'drop-newest': Ignore the new element (don't add it)
|
|
338
|
+
* - 'reject': Return false/None but don't throw (graceful failure)
|
|
339
|
+
* - 'grow': Automatically increase capacity (becomes unbounded)
|
|
340
|
+
* - 'block': For async collections, wait until space is available
|
|
341
|
+
*/
|
|
342
|
+
export type OverflowPolicy = "throw" | "drop-oldest" | "drop-newest" | "reject" | "grow" | "block";
|
|
343
|
+
/**
|
|
344
|
+
* Context information given when overflow occurs.
|
|
345
|
+
*/
|
|
346
|
+
export interface OverflowContext<T> {
|
|
347
|
+
/** The element that triggered the overflow. */
|
|
348
|
+
readonly newElement: T;
|
|
349
|
+
/** Current size of the collection. */
|
|
350
|
+
readonly currentSize: number;
|
|
351
|
+
/** Maximum capacity of the collection. */
|
|
352
|
+
readonly capacity: number;
|
|
353
|
+
/** Timestamp when overflow occurred. */
|
|
354
|
+
readonly timestamp: number;
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* Callback invoked when overflow occurs.
|
|
358
|
+
* This function can log, record metrics or do custom handling.
|
|
359
|
+
*/
|
|
360
|
+
export type OverflowHandler<T> = (context: OverflowContext<T>) => void;
|
|
361
|
+
/**
|
|
362
|
+
* Policy for handling underflow when removing from an empty collection.
|
|
363
|
+
*
|
|
364
|
+
* - 'throw': Throw an error when trying to remove from empty collection
|
|
365
|
+
* - 'return-none': Return None/undefined (graceful failure)
|
|
366
|
+
* - 'return-default': Give a default value given by user
|
|
367
|
+
* - 'block': For async collections, wait until an element is available
|
|
368
|
+
*/
|
|
369
|
+
export type UnderflowPolicy = "throw" | "return-none" | "return-default" | "block";
|
|
370
|
+
/**
|
|
371
|
+
* Context information given when underflow occurs.
|
|
372
|
+
*/
|
|
373
|
+
export interface UnderflowContext {
|
|
374
|
+
/** The operation that triggered the underflow. */
|
|
375
|
+
readonly operation: string;
|
|
376
|
+
/** Timestamp when underflow occurred. */
|
|
377
|
+
readonly timestamp: number;
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* Callback invoked when underflow occurs.
|
|
381
|
+
*/
|
|
382
|
+
export type UnderflowHandler = (context: UnderflowContext) => void;
|
|
383
|
+
/**
|
|
384
|
+
* Policy for choosing which element to evict from a bounded collection.
|
|
385
|
+
*
|
|
386
|
+
* - 'lru': Least Recently Used - evict element not accessed for longest time
|
|
387
|
+
* - 'lfu': Least Frequently Used - evict element accessed least often
|
|
388
|
+
* - 'fifo': First In First Out - evict oldest inserted element
|
|
389
|
+
* - 'lifo': Last In First Out - evict newest inserted element (stack-like)
|
|
390
|
+
* - 'random': Evict a random element
|
|
391
|
+
* - 'ttl': Time To Live - evict elements that have expired
|
|
392
|
+
* - 'priority': Evict element with lowest priority
|
|
393
|
+
* - 'custom': User-provided eviction function
|
|
394
|
+
*/
|
|
395
|
+
export type EvictionPolicy = "lru" | "lfu" | "fifo" | "lifo" | "random" | "ttl" | "priority" | "custom";
|
|
396
|
+
/**
|
|
397
|
+
* Metadata tracked for eviction decisions.
|
|
398
|
+
*/
|
|
399
|
+
export interface EvictionMetadata {
|
|
400
|
+
/** When the element was inserted. */
|
|
401
|
+
readonly insertedAt: number;
|
|
402
|
+
/** When the element was last accessed. */
|
|
403
|
+
lastAccessedAt: number;
|
|
404
|
+
/** How many times the element has been accessed. */
|
|
405
|
+
accessCount: number;
|
|
406
|
+
/** Time-to-live expiration timestamp (if applicable). */
|
|
407
|
+
readonly expiresAt?: number;
|
|
408
|
+
/** Priority value (higher = more important, if applicable). */
|
|
409
|
+
readonly priority?: number;
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* Information about an element that can be evicted.
|
|
413
|
+
*/
|
|
414
|
+
export interface EvictionCandidate<T> {
|
|
415
|
+
/** The element value. */
|
|
416
|
+
readonly element: T;
|
|
417
|
+
/** Eviction metadata for this element. */
|
|
418
|
+
readonly metadata: EvictionMetadata;
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* Custom eviction function.
|
|
422
|
+
* Given candidates, gives the element to evict.
|
|
423
|
+
*/
|
|
424
|
+
export type EvictionSelector<T> = (candidates: EvictionCandidate<T>[]) => T | undefined;
|
|
425
|
+
/**
|
|
426
|
+
* Policy for retrying failed async operations.
|
|
427
|
+
*/
|
|
428
|
+
export interface RetryPolicy {
|
|
429
|
+
/** Maximum number of retry tries. */
|
|
430
|
+
readonly maxAttempts: number;
|
|
431
|
+
/** Initial delay in milliseconds. */
|
|
432
|
+
readonly initialDelay: number;
|
|
433
|
+
/** Backoff strategy. */
|
|
434
|
+
readonly backoff: "none" | "linear" | "exponential";
|
|
435
|
+
/** Maximum delay cap (for backoff). */
|
|
436
|
+
readonly maxDelay?: number;
|
|
437
|
+
/** Jitter to add randomness and avoid thundering herd. */
|
|
438
|
+
readonly jitter?: boolean;
|
|
439
|
+
}
|
|
440
|
+
/**
|
|
441
|
+
* Context for retry decisions.
|
|
442
|
+
*/
|
|
443
|
+
export interface RetryContext {
|
|
444
|
+
/** Current try number (1-indexed). */
|
|
445
|
+
readonly attempt: number;
|
|
446
|
+
/** The error that caused the retry. */
|
|
447
|
+
readonly error: unknown;
|
|
448
|
+
/** Elapsed time since first try. */
|
|
449
|
+
readonly elapsedMs: number;
|
|
450
|
+
}
|
|
451
|
+
/**
|
|
452
|
+
* Callback to determine if an error is retryable.
|
|
453
|
+
*/
|
|
454
|
+
export type RetryPredicate = (context: RetryContext) => boolean;
|
|
455
|
+
/**
|
|
456
|
+
* Policy for resolving conflicts when same key is changed concurrently.
|
|
457
|
+
*
|
|
458
|
+
* - 'last-write-wins': Most recent write takes precedence
|
|
459
|
+
* - 'first-write-wins': First write takes precedence
|
|
460
|
+
* - 'merge': Call merge function to combine values
|
|
461
|
+
* - 'throw': Throw an error on conflict
|
|
462
|
+
*/
|
|
463
|
+
export type ConflictResolutionPolicy = "last-write-wins" | "first-write-wins" | "merge" | "throw";
|
|
464
|
+
/**
|
|
465
|
+
* Function to merge conflicting values.
|
|
466
|
+
*/
|
|
467
|
+
export type MergeFunction<T> = (current: T, incoming: T) => T;
|
|
468
|
+
/**
|
|
469
|
+
* Policy for when to compact internal storage.
|
|
470
|
+
*/
|
|
471
|
+
export interface CompactionPolicy {
|
|
472
|
+
/** Compact when the used part of the capacity drops below this ratio (0-1). */
|
|
473
|
+
readonly minLoadFactor?: number;
|
|
474
|
+
/** Compact when wasted space exceeds this many elements. */
|
|
475
|
+
readonly maxWastedSpace?: number;
|
|
476
|
+
/** Automatically compact on every removal. */
|
|
477
|
+
readonly autoCompact?: boolean;
|
|
478
|
+
}
|
|
479
|
+
/**
|
|
480
|
+
* Policy for how to grow capacity when needed.
|
|
481
|
+
*/
|
|
482
|
+
export interface GrowthPolicy {
|
|
483
|
+
/** Growth strategy. */
|
|
484
|
+
readonly strategy: "double" | "linear" | "golden-ratio" | "custom";
|
|
485
|
+
/** For linear growth, how many elements to add. */
|
|
486
|
+
readonly linearIncrement?: number;
|
|
487
|
+
/** For custom growth, a function to calculate new capacity. */
|
|
488
|
+
readonly customGrowth?: (currentCapacity: number) => number;
|
|
489
|
+
/** Maximum capacity limit (prevent unbounded growth). */
|
|
490
|
+
readonly maxCapacity?: number;
|
|
491
|
+
}
|
|
492
|
+
/**
|
|
493
|
+
* Validation action when an invalid element is encountered.
|
|
494
|
+
*
|
|
495
|
+
* - 'throw': Throw an error
|
|
496
|
+
* - 'drop': Silently drop the invalid element
|
|
497
|
+
* - 'coerce': Try to coerce to valid type
|
|
498
|
+
* - 'log': Log a warning and continue
|
|
499
|
+
*/
|
|
500
|
+
export type ValidationPolicy = "throw" | "drop" | "coerce" | "log";
|
|
501
|
+
/**
|
|
502
|
+
* Context for validation failures.
|
|
503
|
+
*/
|
|
504
|
+
export interface ValidationContext<T> {
|
|
505
|
+
/** The invalid element. */
|
|
506
|
+
readonly element: T;
|
|
507
|
+
/** Validation error message/details. */
|
|
508
|
+
readonly error: unknown;
|
|
509
|
+
/** The operation that triggered validation. */
|
|
510
|
+
readonly operation: string;
|
|
511
|
+
}
|
|
512
|
+
/**
|
|
513
|
+
* Callback invoked on validation failures.
|
|
514
|
+
*/
|
|
515
|
+
export type ValidationHandler<T> = (context: ValidationContext<T>) => void;
|
|
516
|
+
/**
|
|
517
|
+
* Default overflow policy: throw an error.
|
|
518
|
+
*/
|
|
519
|
+
export declare const DEFAULT_OVERFLOW_POLICY: OverflowPolicy;
|
|
520
|
+
/**
|
|
521
|
+
* Default underflow policy: throw an error.
|
|
522
|
+
*/
|
|
523
|
+
export declare const DEFAULT_UNDERFLOW_POLICY: UnderflowPolicy;
|
|
524
|
+
/**
|
|
525
|
+
* Default eviction policy: FIFO (oldest first).
|
|
526
|
+
*/
|
|
527
|
+
export declare const DEFAULT_EVICTION_POLICY: EvictionPolicy;
|
|
528
|
+
/**
|
|
529
|
+
* Default retry policy: 3 tries with exponential backoff.
|
|
530
|
+
*/
|
|
531
|
+
export declare const DEFAULT_RETRY_POLICY: RetryPolicy;
|
|
532
|
+
/**
|
|
533
|
+
* Default conflict resolution: last write wins.
|
|
534
|
+
*/
|
|
535
|
+
export declare const DEFAULT_CONFLICT_POLICY: ConflictResolutionPolicy;
|
|
536
|
+
/**
|
|
537
|
+
* Default growth policy: double capacity.
|
|
538
|
+
*/
|
|
539
|
+
export declare const DEFAULT_GROWTH_POLICY: GrowthPolicy;
|
|
540
|
+
/**
|
|
541
|
+
* Default validation policy: throw on invalid.
|
|
542
|
+
*/
|
|
543
|
+
export declare const DEFAULT_VALIDATION_POLICY: ValidationPolicy;
|
|
544
|
+
/**
|
|
545
|
+
* ReadonlyCollection<T> - The root interface for readonly collections.
|
|
546
|
+
*
|
|
547
|
+
* Mirrors java.util.Collection but without mutation operations.
|
|
548
|
+
* All collections must extend this or a more specific readonly interface.
|
|
549
|
+
*/
|
|
550
|
+
export interface ReadonlyCollection<T> extends Iterable<T> {
|
|
551
|
+
/**
|
|
552
|
+
* This value is the number of elements in this collection.
|
|
553
|
+
*/
|
|
554
|
+
readonly size: number;
|
|
555
|
+
/**
|
|
556
|
+
* This value is true if this collection contains no elements.
|
|
557
|
+
*/
|
|
558
|
+
readonly isEmpty: boolean;
|
|
559
|
+
/**
|
|
560
|
+
* This method gives true if this collection contains the specified element.
|
|
561
|
+
* This method uses the collection's equality function to compare elements.
|
|
562
|
+
*
|
|
563
|
+
* @param element The element to check for
|
|
564
|
+
*/
|
|
565
|
+
contains(element: T): boolean;
|
|
566
|
+
/**
|
|
567
|
+
* This method gives true if this collection contains all elements in the specified collection.
|
|
568
|
+
*
|
|
569
|
+
* @param other The collection whose elements to check for
|
|
570
|
+
*/
|
|
571
|
+
containsAll(other: Iterable<T>): boolean;
|
|
572
|
+
/**
|
|
573
|
+
* This method gives an array containing all elements in this collection.
|
|
574
|
+
* The given array is safe to change (it's a copy).
|
|
575
|
+
*/
|
|
576
|
+
toArray(): T[];
|
|
577
|
+
/**
|
|
578
|
+
* This method does the given action for each element.
|
|
579
|
+
*
|
|
580
|
+
* @param action The action to do for each element
|
|
581
|
+
*/
|
|
582
|
+
forEach(action: (element: T, index: number) => void): void;
|
|
583
|
+
/**
|
|
584
|
+
* This value is the equality function used by this collection.
|
|
585
|
+
*/
|
|
586
|
+
readonly eq: Eq<T>;
|
|
587
|
+
}
|
|
588
|
+
/**
|
|
589
|
+
* Collection<T> - The root interface for mutable collections.
|
|
590
|
+
*
|
|
591
|
+
* Mirrors java.util.Collection with mutation operations.
|
|
592
|
+
* All mutable collections must extend this or a more specific interface.
|
|
593
|
+
*/
|
|
594
|
+
export interface Collection<T> extends ReadonlyCollection<T> {
|
|
595
|
+
/**
|
|
596
|
+
* This method adds an element to this collection.
|
|
597
|
+
*
|
|
598
|
+
* This method gives true if the collection was changed as a result of this call.
|
|
599
|
+
* Some collections (like Set) can give false if the element already exists.
|
|
600
|
+
*
|
|
601
|
+
* @param element The element to add
|
|
602
|
+
* @returns true if the collection changed
|
|
603
|
+
* @throws Error if collection is full (bounded) and policy is 'throw'
|
|
604
|
+
*/
|
|
605
|
+
add(element: T): boolean;
|
|
606
|
+
/**
|
|
607
|
+
* This method adds all elements from the specified collection to this collection.
|
|
608
|
+
*
|
|
609
|
+
* @param other The collection whose elements to add
|
|
610
|
+
* @returns true if the collection changed
|
|
611
|
+
*/
|
|
612
|
+
addAll(other: Iterable<T>): boolean;
|
|
613
|
+
/**
|
|
614
|
+
* This method removes a single instance of the specified element from this collection.
|
|
615
|
+
*
|
|
616
|
+
* This method gives true if an element was removed as a result of this call.
|
|
617
|
+
*
|
|
618
|
+
* @param element The element to remove
|
|
619
|
+
* @returns true if an element was removed
|
|
620
|
+
*/
|
|
621
|
+
remove(element: T): boolean;
|
|
622
|
+
/**
|
|
623
|
+
* This method removes all elements from this collection that are also in the specified collection.
|
|
624
|
+
*
|
|
625
|
+
* @param other The collection whose elements to remove
|
|
626
|
+
* @returns true if the collection changed
|
|
627
|
+
*/
|
|
628
|
+
removeAll(other: Iterable<T>): boolean;
|
|
629
|
+
/**
|
|
630
|
+
* This method retains only the elements in this collection that are in the specified collection.
|
|
631
|
+
* In other words, removes all elements not in the specified collection.
|
|
632
|
+
*
|
|
633
|
+
* @param other The collection of elements to retain
|
|
634
|
+
* @returns true if the collection changed
|
|
635
|
+
*/
|
|
636
|
+
retainAll(other: Iterable<T>): boolean;
|
|
637
|
+
/**
|
|
638
|
+
* This method removes all elements from this collection.
|
|
639
|
+
* The collection will be empty after this call gives.
|
|
640
|
+
*/
|
|
641
|
+
clear(): void;
|
|
642
|
+
}
|
|
643
|
+
/**
|
|
644
|
+
* Utility type to check if a collection is readonly.
|
|
645
|
+
*/
|
|
646
|
+
export type IsReadonly<C> = C extends ReadonlyCollection<any> ? C extends Collection<any> ? false : true : false;
|
|
647
|
+
/**
|
|
648
|
+
* Extract element type from a collection.
|
|
649
|
+
*/
|
|
650
|
+
export type ElementOf<C> = C extends ReadonlyCollection<infer T> ? T : never;
|
|
651
|
+
/**
|
|
652
|
+
* Make a collection readonly by removing mutation methods.
|
|
653
|
+
*/
|
|
654
|
+
export type Readonly<C extends Collection<any>> = C extends Collection<infer T> ? ReadonlyCollection<T> : never;
|
|
655
|
+
/**
|
|
656
|
+
* A middleware is a function that takes a collection and gives
|
|
657
|
+
* an enhanced version of that collection.
|
|
658
|
+
*
|
|
659
|
+
* This maps to the unified middleware pattern where:
|
|
660
|
+
* - The "context" is the collection itself (passed directly, not in a context object)
|
|
661
|
+
* - Execution is synchronous (no Promises)
|
|
662
|
+
* - Type transformation is tracked through In -> Out
|
|
663
|
+
*
|
|
664
|
+
* Middleware can:
|
|
665
|
+
* - Intercept method calls
|
|
666
|
+
* - Add new capabilities
|
|
667
|
+
* - Transform inputs/outputs
|
|
668
|
+
* - Delegate to the next layer
|
|
669
|
+
*
|
|
670
|
+
* @template In The input collection type
|
|
671
|
+
* @template Out The output collection type (defaults to In if not specified)
|
|
672
|
+
*/
|
|
673
|
+
export type Middleware<In = any, Out extends In = In> = (next: In) => Out;
|
|
674
|
+
/**
|
|
675
|
+
* Composes multiple middleware layers into a single enhancer function.
|
|
676
|
+
*
|
|
677
|
+
* This function uses reduceRight to make an "onion" pattern where the first middleware
|
|
678
|
+
* is the outermost layer (runs first when calling into the collection).
|
|
679
|
+
*
|
|
680
|
+
* The type system properly tracks capability additions through composition.
|
|
681
|
+
*
|
|
682
|
+
* @example
|
|
683
|
+
* // Layer order (outer to inner):
|
|
684
|
+
* // 1. safe - converts throws to Option/Result
|
|
685
|
+
* // 2. evented - emits events on mutations
|
|
686
|
+
* // 3. bounded - enforces capacity limits
|
|
687
|
+
* // 4. base - the underlying ArrayList
|
|
688
|
+
* const enhancer = compose(
|
|
689
|
+
* safeList(),
|
|
690
|
+
* eventedList(),
|
|
691
|
+
* boundedList({ capacity: 100 })
|
|
692
|
+
* )
|
|
693
|
+
* const list = enhancer(arrayList<number>())
|
|
694
|
+
* // Type: List<number> & SafeCollection & EventedCollection & BoundedCollection
|
|
695
|
+
*
|
|
696
|
+
* @param layers The middleware layers to compose
|
|
697
|
+
* @returns A function that applies all layers to a base collection
|
|
698
|
+
*/
|
|
699
|
+
export declare function compose<C, R1 extends C>(m1: Middleware<C, R1>): (base: C) => R1;
|
|
700
|
+
export declare function compose<C, R1 extends C, R2 extends R1>(m1: Middleware<R1, R2>, m2: Middleware<C, R1>): (base: C) => R2;
|
|
701
|
+
export declare function compose<C, R1 extends C, R2 extends R1, R3 extends R2>(m1: Middleware<R2, R3>, m2: Middleware<R1, R2>, m3: Middleware<C, R1>): (base: C) => R3;
|
|
702
|
+
export declare function compose<C, R1 extends C, R2 extends R1, R3 extends R2, R4 extends R3>(m1: Middleware<R3, R4>, m2: Middleware<R2, R3>, m3: Middleware<R1, R2>, m4: Middleware<C, R1>): (base: C) => R4;
|
|
703
|
+
export declare function compose<C, R1 extends C, R2 extends R1, R3 extends R2, R4 extends R3, R5 extends R4>(m1: Middleware<R4, R5>, m2: Middleware<R3, R4>, m3: Middleware<R2, R3>, m4: Middleware<R1, R2>, m5: Middleware<C, R1>): (base: C) => R5;
|
|
704
|
+
export declare function compose<C>(...layers: Middleware<any, any>[]): (base: C) => any;
|
|
705
|
+
/**
|
|
706
|
+
* Utility to check if a collection has a specific capability.
|
|
707
|
+
* Useful for middleware that wants to conditionally enhance behavior.
|
|
708
|
+
*
|
|
709
|
+
* @example
|
|
710
|
+
* if (hasCapability(collection, 'on')) {
|
|
711
|
+
* // Collection supports events
|
|
712
|
+
* collection.on('add', handler)
|
|
713
|
+
* }
|
|
714
|
+
*/
|
|
715
|
+
export declare const hasCapability: <C extends Collection<any>, K extends keyof any>(collection: C, capability: K) => collection is C & Record<K, unknown>;
|
|
716
|
+
/**
|
|
717
|
+
* Marker interface for collections that support events.
|
|
718
|
+
* Middleware can use this to detect event capability.
|
|
719
|
+
*/
|
|
720
|
+
export interface Evented<E extends Record<string, any>> {
|
|
721
|
+
/**
|
|
722
|
+
* Subscribe to an event.
|
|
723
|
+
* @returns Unsubscribe function
|
|
724
|
+
*/
|
|
725
|
+
on<K extends keyof E>(event: K, handler: (payload: E[K]) => void): () => void;
|
|
726
|
+
}
|
|
727
|
+
/**
|
|
728
|
+
* Marker interface for collections that support async operations.
|
|
729
|
+
*/
|
|
730
|
+
export interface Async<T> {
|
|
731
|
+
/**
|
|
732
|
+
* Async iterator for consuming elements.
|
|
733
|
+
*/
|
|
734
|
+
[Symbol.asyncIterator](): AsyncIterator<T>;
|
|
735
|
+
}
|
|
736
|
+
/**
|
|
737
|
+
* Marker interface for collections that are bounded.
|
|
738
|
+
*/
|
|
739
|
+
export interface BoundedCollection {
|
|
740
|
+
/** Maximum capacity */
|
|
741
|
+
readonly capacity: number;
|
|
742
|
+
/** If collection is at capacity */
|
|
743
|
+
readonly isFull: boolean;
|
|
744
|
+
/** Remaining space */
|
|
745
|
+
readonly remainingCapacity: number;
|
|
746
|
+
}
|
|
747
|
+
/**
|
|
748
|
+
* Marker interface for collections that support safe (Option/Result) operations.
|
|
749
|
+
*/
|
|
750
|
+
export interface SafeCollection {
|
|
751
|
+
/**
|
|
752
|
+
* Namespace for safe operations that return Option/Result instead of throwing.
|
|
753
|
+
*/
|
|
754
|
+
readonly safe: unknown;
|
|
755
|
+
}
|
|
756
|
+
/**
|
|
757
|
+
* Type helper to extract the element type from a collection.
|
|
758
|
+
*/
|
|
759
|
+
export type ElementType<C> = C extends {
|
|
760
|
+
[Symbol.iterator](): Iterator<infer T>;
|
|
761
|
+
} ? T : C extends {
|
|
762
|
+
get(index: number): infer T;
|
|
763
|
+
} ? T : C extends {
|
|
764
|
+
peek(): infer T;
|
|
765
|
+
} ? T : never;
|
|
766
|
+
/**
|
|
767
|
+
* Type helper to make all methods of a type return their async equivalents.
|
|
768
|
+
*/
|
|
769
|
+
export type Asyncify<C> = { [K in keyof C]: C[K] extends ((...args: infer Args) => infer R) ? (...args: Args) => Promise<R> : C[K]; };
|
|
770
|
+
/**
|
|
771
|
+
* Helper to make a proxy that intercepts method calls.
|
|
772
|
+
* Useful for implementing middleware that needs to wrap method invocations.
|
|
773
|
+
*
|
|
774
|
+
* @example
|
|
775
|
+
* const logged = interceptMethods(collection, (target, method, args) => {
|
|
776
|
+
* console.log(`Calling ${String(method)} with`, args)
|
|
777
|
+
* const result = Reflect.apply(target[method], target, args)
|
|
778
|
+
* console.log(`Result:`, result)
|
|
779
|
+
* return result
|
|
780
|
+
* })
|
|
781
|
+
*/
|
|
782
|
+
export declare const interceptMethods: <C extends object>(target: C, interceptor: (target: C, method: keyof C, args: unknown[]) => unknown) => C;
|
|
783
|
+
/**
|
|
784
|
+
* Helper to make a readonly view of a collection by blocking mutating methods.
|
|
785
|
+
*
|
|
786
|
+
* @param mutatingMethods Set of method names that the middleware blocks
|
|
787
|
+
* @param errorMessage Custom error message to throw
|
|
788
|
+
*/
|
|
789
|
+
export declare const blockMethods: <C extends object>(target: C, mutatingMethods: Set<string | symbol>, errorMessage?: string) => C;
|
|
790
|
+
/**
|
|
791
|
+
* Helper to delegate method calls to a target while maintaining correct context.
|
|
792
|
+
* Useful when wrapping collections.
|
|
793
|
+
*/
|
|
794
|
+
export declare const delegate: <C extends object, T extends C>(target: T, wrapper: Partial<C>) => C;
|
|
795
|
+
/**
|
|
796
|
+
* Utility type to add optional capabilities to a collection type.
|
|
797
|
+
*/
|
|
798
|
+
export type WithCapability<C, Cap> = C & Cap;
|
|
799
|
+
/**
|
|
800
|
+
* Builder pattern helper for fluent API construction.
|
|
801
|
+
*
|
|
802
|
+
* @example
|
|
803
|
+
* class ListBuilder<T> {
|
|
804
|
+
* private middleware: Middleware<List<T>>[] = []
|
|
805
|
+
*
|
|
806
|
+
* bounded(capacity: number) {
|
|
807
|
+
* this.middleware.push(boundedList({ capacity }))
|
|
808
|
+
* return this
|
|
809
|
+
* }
|
|
810
|
+
*
|
|
811
|
+
* evented() {
|
|
812
|
+
* this.middleware.push(eventedList())
|
|
813
|
+
* return this
|
|
814
|
+
* }
|
|
815
|
+
*
|
|
816
|
+
* build(): List<T> {
|
|
817
|
+
* return compose(...this.middleware)(arrayList<T>())
|
|
818
|
+
* }
|
|
819
|
+
* }
|
|
820
|
+
*/
|
|
821
|
+
export declare abstract class CollectionBuilder<C> {
|
|
822
|
+
protected middleware: Middleware<C>[];
|
|
823
|
+
/**
|
|
824
|
+
* Apply a custom middleware.
|
|
825
|
+
*/
|
|
826
|
+
use(middleware: Middleware<C>): this;
|
|
827
|
+
/**
|
|
828
|
+
* Build the final collection by composing all middleware with the base.
|
|
829
|
+
*/
|
|
830
|
+
abstract build(): C;
|
|
831
|
+
}
|
|
832
|
+
/**
|
|
833
|
+
* Utility to make a middleware that only enhances specific methods.
|
|
834
|
+
*
|
|
835
|
+
* @example
|
|
836
|
+
* const logAdd = enhanceMethod('add', (original, args) => {
|
|
837
|
+
* console.log('Adding:', args[0])
|
|
838
|
+
* return original(...args)
|
|
839
|
+
* })
|
|
840
|
+
*/
|
|
841
|
+
export declare const enhanceMethod: <C extends object, K extends keyof C & string>(methodName: K, enhancer: (original: C[K] extends ((...args: infer Args) => infer R) ? (...args: Args) => R : never, args: C[K] extends ((...args: infer Args) => any) ? Args : never) => C[K] extends ((...args: any[]) => infer R) ? R : never) => Middleware<C>;
|
|
842
|
+
/**
|
|
843
|
+
* This function combines multiple middleware into a single middleware.
|
|
844
|
+
* Useful for making reusable middleware bundles.
|
|
845
|
+
*
|
|
846
|
+
* Bundle has the same composition semantics as compose - the first middleware
|
|
847
|
+
* is the outermost layer, and types are tracked through the chain.
|
|
848
|
+
*
|
|
849
|
+
* @example
|
|
850
|
+
* const standardBehaviors = bundle(
|
|
851
|
+
* boundedList({ capacity: 1000 }),
|
|
852
|
+
* eventedList(),
|
|
853
|
+
* safeList()
|
|
854
|
+
* )
|
|
855
|
+
*
|
|
856
|
+
* const list = compose(standardBehaviors)(arrayList<number>())
|
|
857
|
+
*/
|
|
858
|
+
export declare function bundle<C, R1 extends C>(m1: Middleware<C, R1>): Middleware<C, R1>;
|
|
859
|
+
export declare function bundle<C, R1 extends C, R2 extends R1>(m1: Middleware<R1, R2>, m2: Middleware<C, R1>): Middleware<C, R2>;
|
|
860
|
+
export declare function bundle<C, R1 extends C, R2 extends R1, R3 extends R2>(m1: Middleware<R2, R3>, m2: Middleware<R1, R2>, m3: Middleware<C, R1>): Middleware<C, R3>;
|
|
861
|
+
export declare function bundle<C, R1 extends C, R2 extends R1, R3 extends R2, R4 extends R3>(m1: Middleware<R3, R4>, m2: Middleware<R2, R3>, m3: Middleware<R1, R2>, m4: Middleware<C, R1>): Middleware<C, R4>;
|
|
862
|
+
export declare function bundle<C, R1 extends C, R2 extends R1, R3 extends R2, R4 extends R3, R5 extends R4>(m1: Middleware<R4, R5>, m2: Middleware<R3, R4>, m3: Middleware<R2, R3>, m4: Middleware<R1, R2>, m5: Middleware<C, R1>): Middleware<C, R5>;
|
|
863
|
+
export declare function bundle<C>(...middleware: Middleware<any, any>[]): Middleware<C, any>;
|
|
864
|
+
/**
|
|
865
|
+
* Typed event emitter system for collections.
|
|
866
|
+
*
|
|
867
|
+
* This function gives a lightweight, type-safe event system for mutation events
|
|
868
|
+
* and other observable behavior. Events are strongly typed using
|
|
869
|
+
* TypeScript's mapped types.
|
|
870
|
+
*/
|
|
871
|
+
/**
|
|
872
|
+
* A listener function that receives an event payload.
|
|
873
|
+
*/
|
|
874
|
+
export type Listener<P> = (payload: P) => void;
|
|
875
|
+
/**
|
|
876
|
+
* A function to unsubscribe from an event.
|
|
877
|
+
*/
|
|
878
|
+
export type Unsubscribe = () => void;
|
|
879
|
+
/**
|
|
880
|
+
* Event map defining event names and their payload types.
|
|
881
|
+
*
|
|
882
|
+
* @example
|
|
883
|
+
* interface MyEvents {
|
|
884
|
+
* add: { value: number; index: number }
|
|
885
|
+
* remove: { value: number }
|
|
886
|
+
* clear: {}
|
|
887
|
+
* }
|
|
888
|
+
*/
|
|
889
|
+
export type EventMap = Record<string, any>;
|
|
890
|
+
/**
|
|
891
|
+
* Type-safe event emitter.
|
|
892
|
+
*
|
|
893
|
+
* This class supports subscribing to events, emitting events, and managing listeners.
|
|
894
|
+
* All event names and payloads are type-checked at compile time.
|
|
895
|
+
*
|
|
896
|
+
* @example
|
|
897
|
+
* interface ListEvents<T> {
|
|
898
|
+
* add: { value: T; index: number }
|
|
899
|
+
* remove: { value: T; index: number }
|
|
900
|
+
* clear: {}
|
|
901
|
+
* }
|
|
902
|
+
*
|
|
903
|
+
* const emitter = new Emitter<ListEvents<number>>()
|
|
904
|
+
*
|
|
905
|
+
* const unsub = emitter.on('add', ({ value, index }) => {
|
|
906
|
+
* console.log(`Added ${value} at ${index}`)
|
|
907
|
+
* })
|
|
908
|
+
*
|
|
909
|
+
* emitter.emit('add', { value: 42, index: 0 })
|
|
910
|
+
* unsub() // Unsubscribe
|
|
911
|
+
*/
|
|
912
|
+
export declare class Emitter<E extends EventMap> {
|
|
913
|
+
private listeners;
|
|
914
|
+
/**
|
|
915
|
+
* Subscribe to an event.
|
|
916
|
+
*
|
|
917
|
+
* @param event The event name
|
|
918
|
+
* @param listener The callback to invoke when event is emitted
|
|
919
|
+
* @returns A function to unsubscribe
|
|
920
|
+
*/
|
|
921
|
+
on<K extends keyof E>(event: K, listener: Listener<E[K]>): Unsubscribe;
|
|
922
|
+
/**
|
|
923
|
+
* Subscribe to an event, but automatically unsubscribe after first emission.
|
|
924
|
+
*
|
|
925
|
+
* @param event The event name
|
|
926
|
+
* @param listener The callback to invoke once
|
|
927
|
+
* @returns A function to unsubscribe (in case you want to cancel before emission)
|
|
928
|
+
*/
|
|
929
|
+
once<K extends keyof E>(event: K, listener: Listener<E[K]>): Unsubscribe;
|
|
930
|
+
/**
|
|
931
|
+
* Emit an event to all registered listeners.
|
|
932
|
+
*
|
|
933
|
+
* @param event The event name
|
|
934
|
+
* @param payload The event payload
|
|
935
|
+
*/
|
|
936
|
+
emit<K extends keyof E>(event: K, payload: E[K]): void;
|
|
937
|
+
/**
|
|
938
|
+
* Remove all listeners for a specific event, or all events if no event specified.
|
|
939
|
+
*
|
|
940
|
+
* @param event Optional event name to clear listeners for
|
|
941
|
+
*/
|
|
942
|
+
off<K extends keyof E>(event?: K): void;
|
|
943
|
+
/**
|
|
944
|
+
* Get the number of listeners for a specific event.
|
|
945
|
+
*
|
|
946
|
+
* @param event The event name
|
|
947
|
+
* @returns Number of registered listeners
|
|
948
|
+
*/
|
|
949
|
+
listenerCount<K extends keyof E>(event: K): number;
|
|
950
|
+
/**
|
|
951
|
+
* Get all event names that have at least one listener.
|
|
952
|
+
*/
|
|
953
|
+
eventNames(): (keyof E)[];
|
|
954
|
+
/**
|
|
955
|
+
* Check if there are any listeners for a specific event.
|
|
956
|
+
*/
|
|
957
|
+
hasListeners<K extends keyof E>(event: K): boolean;
|
|
958
|
+
/**
|
|
959
|
+
* Remove all listeners and clean up resources.
|
|
960
|
+
*/
|
|
961
|
+
dispose(): void;
|
|
962
|
+
}
|
|
963
|
+
/**
|
|
964
|
+
* Make an event emitter for a specific event map.
|
|
965
|
+
*
|
|
966
|
+
* This is a convenience factory function.
|
|
967
|
+
*
|
|
968
|
+
* @example
|
|
969
|
+
* const events = createEmitter<ListEvents<number>>()
|
|
970
|
+
*/
|
|
971
|
+
export declare const createEmitter: <E extends EventMap>() => Emitter<E>;
|
|
972
|
+
/**
|
|
973
|
+
* Event batching utility for reducing event noise.
|
|
974
|
+
* This class collects events and emits them in batches.
|
|
975
|
+
*
|
|
976
|
+
* @example
|
|
977
|
+
* const batcher = new EventBatcher(emitter, 'add', 100)
|
|
978
|
+
* batcher.queue({ value: 1, index: 0 })
|
|
979
|
+
* batcher.queue({ value: 2, index: 1 })
|
|
980
|
+
* // After 100ms or flush(), emits single 'batch-add' event with both items
|
|
981
|
+
*/
|
|
982
|
+
export declare class EventBatcher<E extends EventMap, K extends keyof E> {
|
|
983
|
+
private emitter;
|
|
984
|
+
private event;
|
|
985
|
+
private delayMs;
|
|
986
|
+
private batch;
|
|
987
|
+
private timer;
|
|
988
|
+
constructor(emitter: Emitter<E & { [P in `batch-${K & string}`]: E[K][]; }>, event: K, delayMs: number);
|
|
989
|
+
/**
|
|
990
|
+
* Add an event to the current batch.
|
|
991
|
+
*/
|
|
992
|
+
queue(payload: E[K]): void;
|
|
993
|
+
/**
|
|
994
|
+
* Immediately emit all batched events.
|
|
995
|
+
*/
|
|
996
|
+
flush(): void;
|
|
997
|
+
/**
|
|
998
|
+
* Clean up resources.
|
|
999
|
+
*/
|
|
1000
|
+
dispose(): void;
|
|
1001
|
+
}
|
|
1002
|
+
/**
|
|
1003
|
+
* Async event emitter that gives promises.
|
|
1004
|
+
* This class waits for all async listeners to complete.
|
|
1005
|
+
*
|
|
1006
|
+
* @example
|
|
1007
|
+
* const emitter = new AsyncEmitter<{ save: { data: string } }>()
|
|
1008
|
+
*
|
|
1009
|
+
* emitter.on('save', async ({ data }) => {
|
|
1010
|
+
* await database.save(data)
|
|
1011
|
+
* })
|
|
1012
|
+
*
|
|
1013
|
+
* await emitter.emit('save', { data: 'test' }) // Waits for save
|
|
1014
|
+
*/
|
|
1015
|
+
export declare class AsyncEmitter<E extends EventMap> {
|
|
1016
|
+
private listeners;
|
|
1017
|
+
on<K extends keyof E>(event: K, listener: Listener<E[K]>): Unsubscribe;
|
|
1018
|
+
/**
|
|
1019
|
+
* Emit an event and wait for all async listeners to complete.
|
|
1020
|
+
*
|
|
1021
|
+
* @param event The event name
|
|
1022
|
+
* @param payload The event payload
|
|
1023
|
+
* @returns Promise that resolves when all listeners complete
|
|
1024
|
+
*/
|
|
1025
|
+
emit<K extends keyof E>(event: K, payload: E[K]): Promise<void>;
|
|
1026
|
+
off<K extends keyof E>(event?: K): void;
|
|
1027
|
+
listenerCount<K extends keyof E>(event: K): number;
|
|
1028
|
+
eventNames(): (keyof E)[];
|
|
1029
|
+
dispose(): void;
|
|
1030
|
+
}
|
|
1031
|
+
/**
|
|
1032
|
+
* Utility to forward events from one emitter to another.
|
|
1033
|
+
* Useful for composing event sources.
|
|
1034
|
+
*
|
|
1035
|
+
* @example
|
|
1036
|
+
* const child = new Emitter<ChildEvents>()
|
|
1037
|
+
* const parent = new Emitter<ParentEvents>()
|
|
1038
|
+
*
|
|
1039
|
+
* forwardEvent(child, parent, 'childEvent', 'parentEvent')
|
|
1040
|
+
*/
|
|
1041
|
+
export declare const forwardEvent: <E1 extends EventMap, E2 extends EventMap, K1 extends keyof E1, K2 extends keyof E2>(source: Emitter<E1>, target: Emitter<E2>, sourceEvent: K1, targetEvent: K2, transform?: (payload: E1[K1]) => E2[K2]) => Unsubscribe;
|
|
1042
|
+
/**
|
|
1043
|
+
* Make a filtered event subscription that only fires when predicate is true.
|
|
1044
|
+
*
|
|
1045
|
+
* @example
|
|
1046
|
+
* const emitter = new Emitter<{ change: { value: number } }>()
|
|
1047
|
+
*
|
|
1048
|
+
* onWhen(emitter, 'change', ({ value }) => value > 10, ({ value }) => {
|
|
1049
|
+
* console.log('Value exceeded 10:', value)
|
|
1050
|
+
* })
|
|
1051
|
+
*/
|
|
1052
|
+
export declare const onWhen: <E extends EventMap, K extends keyof E>(emitter: Emitter<E>, event: K, predicate: (payload: E[K]) => boolean, listener: Listener<E[K]>) => Unsubscribe;
|
|
1053
|
+
/**
|
|
1054
|
+
* ReadonlyList<T> - Readonly ordered collection with indexed access.
|
|
1055
|
+
*
|
|
1056
|
+
* Mirrors java.util.List but without mutation operations.
|
|
1057
|
+
* Elements maintain their insertion order and can be accessed by index.
|
|
1058
|
+
*/
|
|
1059
|
+
export interface ReadonlyList<T> extends ReadonlyCollection<T> {
|
|
1060
|
+
/**
|
|
1061
|
+
* This method gives the element at the specified position.
|
|
1062
|
+
*
|
|
1063
|
+
* @param index The index of the element to give (0-based)
|
|
1064
|
+
* @returns The element at the specified position
|
|
1065
|
+
* @throws RangeError if index is out of bounds
|
|
1066
|
+
*/
|
|
1067
|
+
get(index: number): T;
|
|
1068
|
+
/**
|
|
1069
|
+
* This method gives the index of the first occurrence of the specified element,
|
|
1070
|
+
* or -1 if not found.
|
|
1071
|
+
*
|
|
1072
|
+
* @param element The element to search for
|
|
1073
|
+
* @param fromIndex Optional starting index for the search
|
|
1074
|
+
*/
|
|
1075
|
+
indexOf(element: T, fromIndex?: number): number;
|
|
1076
|
+
/**
|
|
1077
|
+
* This method gives the index of the last occurrence of the specified element,
|
|
1078
|
+
* or -1 if not found.
|
|
1079
|
+
*
|
|
1080
|
+
* @param element The element to search for
|
|
1081
|
+
* @param fromIndex Optional starting index for backwards search
|
|
1082
|
+
*/
|
|
1083
|
+
lastIndexOf(element: T, fromIndex?: number): number;
|
|
1084
|
+
/**
|
|
1085
|
+
* This method gives a view of the portion of this list between fromIndex (inclusive)
|
|
1086
|
+
* and toIndex (exclusive).
|
|
1087
|
+
*
|
|
1088
|
+
* Changes to the sublist are reflected in the original list.
|
|
1089
|
+
*
|
|
1090
|
+
* @param fromIndex Start index (inclusive)
|
|
1091
|
+
* @param toIndex End index (exclusive)
|
|
1092
|
+
*/
|
|
1093
|
+
subList(fromIndex: number, toIndex: number): ReadonlyList<T>;
|
|
1094
|
+
/**
|
|
1095
|
+
* This method gives the first element in the list.
|
|
1096
|
+
*
|
|
1097
|
+
* @throws Error if list is empty
|
|
1098
|
+
*/
|
|
1099
|
+
first(): T;
|
|
1100
|
+
/**
|
|
1101
|
+
* This method gives the last element in the list.
|
|
1102
|
+
*
|
|
1103
|
+
* @throws Error if list is empty
|
|
1104
|
+
*/
|
|
1105
|
+
last(): T;
|
|
1106
|
+
}
|
|
1107
|
+
/**
|
|
1108
|
+
* List<T> - Mutable ordered collection with indexed access.
|
|
1109
|
+
*
|
|
1110
|
+
* Mirrors java.util.List with full mutation capabilities.
|
|
1111
|
+
* This type gives methods to insert, replace, and remove elements by index.
|
|
1112
|
+
*/
|
|
1113
|
+
export interface List<T> extends ReadonlyList<T>, Collection<T> {
|
|
1114
|
+
/**
|
|
1115
|
+
* This method replaces the element at the specified position.
|
|
1116
|
+
*
|
|
1117
|
+
* @param index The index of the element to replace
|
|
1118
|
+
* @param element The new element
|
|
1119
|
+
* @returns The element previously at the specified position
|
|
1120
|
+
* @throws RangeError if index is out of bounds
|
|
1121
|
+
*/
|
|
1122
|
+
set(index: number, element: T): T;
|
|
1123
|
+
/**
|
|
1124
|
+
* This method inserts an element at the specified position.
|
|
1125
|
+
* Shifts elements at and after the position to the right.
|
|
1126
|
+
*
|
|
1127
|
+
* @param index The index at which to insert
|
|
1128
|
+
* @param element The element to insert
|
|
1129
|
+
* @throws RangeError if index is out of bounds (0 to size inclusive)
|
|
1130
|
+
*/
|
|
1131
|
+
insert(index: number, element: T): void;
|
|
1132
|
+
/**
|
|
1133
|
+
* This method inserts all elements from the specified collection at the specified position.
|
|
1134
|
+
*
|
|
1135
|
+
* @param index The index at which to insert
|
|
1136
|
+
* @param other The collection whose elements to insert
|
|
1137
|
+
* @returns true if the list changed
|
|
1138
|
+
*/
|
|
1139
|
+
insertAll(index: number, other: Iterable<T>): boolean;
|
|
1140
|
+
/**
|
|
1141
|
+
* This method removes the element at the specified position.
|
|
1142
|
+
* Shifts elements after the position to the left.
|
|
1143
|
+
*
|
|
1144
|
+
* @param index The index of the element to remove
|
|
1145
|
+
* @returns The element that was removed
|
|
1146
|
+
* @throws RangeError if index is out of bounds
|
|
1147
|
+
*/
|
|
1148
|
+
removeAt(index: number): T;
|
|
1149
|
+
/**
|
|
1150
|
+
* This method removes elements in the specified index range.
|
|
1151
|
+
*
|
|
1152
|
+
* @param fromIndex Start index (inclusive)
|
|
1153
|
+
* @param toIndex End index (exclusive)
|
|
1154
|
+
*/
|
|
1155
|
+
removeRange(fromIndex: number, toIndex: number): void;
|
|
1156
|
+
/**
|
|
1157
|
+
* This method sorts this list according to the specified comparator.
|
|
1158
|
+
*
|
|
1159
|
+
* @param compare The comparison function
|
|
1160
|
+
*/
|
|
1161
|
+
sort(compare?: Compare<T>): void;
|
|
1162
|
+
/**
|
|
1163
|
+
* This method reverses the order of elements in this list.
|
|
1164
|
+
*/
|
|
1165
|
+
reverse(): void;
|
|
1166
|
+
/**
|
|
1167
|
+
* This method gives a mutable sublist view.
|
|
1168
|
+
*
|
|
1169
|
+
* @param fromIndex Start index (inclusive)
|
|
1170
|
+
* @param toIndex End index (exclusive)
|
|
1171
|
+
*/
|
|
1172
|
+
subList(fromIndex: number, toIndex: number): List<T>;
|
|
1173
|
+
/**
|
|
1174
|
+
* This method appends an element to the end of this list.
|
|
1175
|
+
* Equivalent to add() but doesn't give boolean.
|
|
1176
|
+
*
|
|
1177
|
+
* @param element The element to append
|
|
1178
|
+
*/
|
|
1179
|
+
push(element: T): void;
|
|
1180
|
+
/**
|
|
1181
|
+
* Removes and gives the last element in this list.
|
|
1182
|
+
*
|
|
1183
|
+
* @returns The last element
|
|
1184
|
+
* @throws Error if list is empty
|
|
1185
|
+
*/
|
|
1186
|
+
pop(): T;
|
|
1187
|
+
/**
|
|
1188
|
+
* Removes and gives the first element in this list.
|
|
1189
|
+
*
|
|
1190
|
+
* @returns The first element
|
|
1191
|
+
* @throws Error if list is empty
|
|
1192
|
+
*/
|
|
1193
|
+
shift(): T;
|
|
1194
|
+
/**
|
|
1195
|
+
* This method prepends an element to the beginning of this list.
|
|
1196
|
+
*
|
|
1197
|
+
* @param element The element to prepend
|
|
1198
|
+
*/
|
|
1199
|
+
unshift(element: T): void;
|
|
1200
|
+
}
|
|
1201
|
+
/**
|
|
1202
|
+
* RandomAccess - Marker interface for lists with O(1) indexed access.
|
|
1203
|
+
*
|
|
1204
|
+
* This type shows that the list gives fast (constant-time) random access.
|
|
1205
|
+
* Examples: ArrayList (yes), LinkedList (no).
|
|
1206
|
+
*/
|
|
1207
|
+
export interface RandomAccess {
|
|
1208
|
+
/**
|
|
1209
|
+
* This marker property shows random access capability.
|
|
1210
|
+
* Always true for collections implementing this interface.
|
|
1211
|
+
*/
|
|
1212
|
+
readonly supportsRandomAccess: true;
|
|
1213
|
+
}
|
|
1214
|
+
/**
|
|
1215
|
+
* Stack<T> - LIFO (Last-In-First-Out) collection.
|
|
1216
|
+
*
|
|
1217
|
+
* A stack of elements with push/pop operations.
|
|
1218
|
+
* A specialized list or a standalone class can implement this type.
|
|
1219
|
+
*/
|
|
1220
|
+
export interface Stack<T> extends Iterable<T> {
|
|
1221
|
+
/** Number of elements */
|
|
1222
|
+
readonly size: number;
|
|
1223
|
+
/** True if empty */
|
|
1224
|
+
readonly isEmpty: boolean;
|
|
1225
|
+
/**
|
|
1226
|
+
* This method pushes an element onto the stack.
|
|
1227
|
+
*/
|
|
1228
|
+
push(element: T): void;
|
|
1229
|
+
/**
|
|
1230
|
+
* Removes and gives the top element.
|
|
1231
|
+
*
|
|
1232
|
+
* @throws Error if stack is empty
|
|
1233
|
+
*/
|
|
1234
|
+
pop(): T;
|
|
1235
|
+
/**
|
|
1236
|
+
* This method gives the top element without removing it.
|
|
1237
|
+
*
|
|
1238
|
+
* @throws Error if stack is empty
|
|
1239
|
+
*/
|
|
1240
|
+
peek(): T;
|
|
1241
|
+
/**
|
|
1242
|
+
* This method removes all elements.
|
|
1243
|
+
*/
|
|
1244
|
+
clear(): void;
|
|
1245
|
+
/**
|
|
1246
|
+
* This method searches for an element and gives its distance from the top.
|
|
1247
|
+
* This method gives -1 if not found.
|
|
1248
|
+
*/
|
|
1249
|
+
search(element: T): number;
|
|
1250
|
+
}
|
|
1251
|
+
/**
|
|
1252
|
+
* Vector<T> - Synchronized growable array.
|
|
1253
|
+
*
|
|
1254
|
+
* Like ArrayList but with synchronized access.
|
|
1255
|
+
* This interface is a legacy of Java. Use compose(synchronized())(arrayList()) instead.
|
|
1256
|
+
*/
|
|
1257
|
+
export interface Vector<T> extends List<T> {
|
|
1258
|
+
/**
|
|
1259
|
+
* Current capacity of the underlying array.
|
|
1260
|
+
*/
|
|
1261
|
+
readonly capacity: number;
|
|
1262
|
+
/**
|
|
1263
|
+
* Trims capacity to current size to minimize memory usage.
|
|
1264
|
+
*/
|
|
1265
|
+
trimToSize(): void;
|
|
1266
|
+
/**
|
|
1267
|
+
* This method increases capacity to make sure that it can hold at least the specified number of elements.
|
|
1268
|
+
*/
|
|
1269
|
+
ensureCapacity(minCapacity: number): void;
|
|
1270
|
+
}
|
|
1271
|
+
/**
|
|
1272
|
+
* CircularList<T> - List with circular/ring buffer behavior.
|
|
1273
|
+
*
|
|
1274
|
+
* When capacity is reached, oldest elements are overwritten.
|
|
1275
|
+
* Useful for fixed-size buffers like logs or history.
|
|
1276
|
+
*/
|
|
1277
|
+
export interface CircularList<T> extends List<T> {
|
|
1278
|
+
/**
|
|
1279
|
+
* Maximum capacity of the circular list.
|
|
1280
|
+
*/
|
|
1281
|
+
readonly capacity: number;
|
|
1282
|
+
/**
|
|
1283
|
+
* This value is true if the list has reached capacity.
|
|
1284
|
+
*/
|
|
1285
|
+
readonly isFull: boolean;
|
|
1286
|
+
/**
|
|
1287
|
+
* Number of elements that have been overwritten.
|
|
1288
|
+
*/
|
|
1289
|
+
readonly overwriteCount: number;
|
|
1290
|
+
}
|
|
1291
|
+
/**
|
|
1292
|
+
* Entry<K, V> - An immutable key-value pair from a map.
|
|
1293
|
+
*
|
|
1294
|
+
* Unlike java.util.Map.Entry which has setValue(), our entries are immutable
|
|
1295
|
+
* snapshots. To update a value, use map.set(key, newValue) instead.
|
|
1296
|
+
*
|
|
1297
|
+
* This prevents accidental mutation and makes entries safe to share.
|
|
1298
|
+
*/
|
|
1299
|
+
export interface Entry<K, V> {
|
|
1300
|
+
readonly key: K;
|
|
1301
|
+
readonly value: V;
|
|
1302
|
+
}
|
|
1303
|
+
/**
|
|
1304
|
+
* ReadonlyMapLike<K, V> - Readonly key-value mapping.
|
|
1305
|
+
*
|
|
1306
|
+
* This type gives read-only access to map operations.
|
|
1307
|
+
* Note: We use "MapLike" to avoid conflicts with TypeScript's built-in Map type.
|
|
1308
|
+
*/
|
|
1309
|
+
export interface ReadonlyMapLike<K, V> extends Iterable<Entry<K, V>> {
|
|
1310
|
+
/**
|
|
1311
|
+
* This value is the number of key-value pairs in this map.
|
|
1312
|
+
*/
|
|
1313
|
+
readonly size: number;
|
|
1314
|
+
/**
|
|
1315
|
+
* This value is true if this map contains no key-value mappings.
|
|
1316
|
+
*/
|
|
1317
|
+
readonly isEmpty: boolean;
|
|
1318
|
+
/**
|
|
1319
|
+
* This method gives true if this map contains a mapping for the specified key.
|
|
1320
|
+
*/
|
|
1321
|
+
has(key: K): boolean;
|
|
1322
|
+
/**
|
|
1323
|
+
* This method gives the value to which the specified key is mapped.
|
|
1324
|
+
*
|
|
1325
|
+
* @throws Error if key is not present
|
|
1326
|
+
*/
|
|
1327
|
+
get(key: K): V;
|
|
1328
|
+
/**
|
|
1329
|
+
* This method gives the value for the key, or undefined if not present.
|
|
1330
|
+
*/
|
|
1331
|
+
getOrUndefined(key: K): V | undefined;
|
|
1332
|
+
/**
|
|
1333
|
+
* This method gives the value for the key, or the default value if not present.
|
|
1334
|
+
*/
|
|
1335
|
+
getOrDefault(key: K, defaultValue: V): V;
|
|
1336
|
+
/**
|
|
1337
|
+
* This method gives true if this map maps one or more keys to the specified value.
|
|
1338
|
+
*/
|
|
1339
|
+
containsValue(value: V): boolean;
|
|
1340
|
+
/**
|
|
1341
|
+
* This method gives an iterable view of the keys in this map.
|
|
1342
|
+
*/
|
|
1343
|
+
keys(): Iterable<K>;
|
|
1344
|
+
/**
|
|
1345
|
+
* This method gives an iterable view of the values in this map.
|
|
1346
|
+
*/
|
|
1347
|
+
values(): Iterable<V>;
|
|
1348
|
+
/**
|
|
1349
|
+
* This method gives an iterable view of the key-value pairs in this map.
|
|
1350
|
+
*/
|
|
1351
|
+
entries(): Iterable<Entry<K, V>>;
|
|
1352
|
+
/**
|
|
1353
|
+
* This method does the given action for each key-value pair.
|
|
1354
|
+
*/
|
|
1355
|
+
forEach(action: (value: V, key: K, map: this) => void): void;
|
|
1356
|
+
/**
|
|
1357
|
+
* This value is the equality function used for keys.
|
|
1358
|
+
*/
|
|
1359
|
+
readonly keyEq: Eq<K>;
|
|
1360
|
+
/**
|
|
1361
|
+
* This value is the equality function used for values.
|
|
1362
|
+
*/
|
|
1363
|
+
readonly valueEq: Eq<V>;
|
|
1364
|
+
}
|
|
1365
|
+
/**
|
|
1366
|
+
* MapLike<K, V> - Mutable key-value mapping.
|
|
1367
|
+
*
|
|
1368
|
+
* Mirrors java.util.Map.
|
|
1369
|
+
*/
|
|
1370
|
+
export interface MapLike<K, V> extends ReadonlyMapLike<K, V> {
|
|
1371
|
+
/**
|
|
1372
|
+
* This method associates the specified value with the specified key in this map.
|
|
1373
|
+
* If the map previously contained a mapping for the key, the old value is replaced.
|
|
1374
|
+
*
|
|
1375
|
+
* @param key The key
|
|
1376
|
+
* @param value The value
|
|
1377
|
+
* @returns The previous value associated with key, or undefined if none
|
|
1378
|
+
*/
|
|
1379
|
+
set(key: K, value: V): V | undefined;
|
|
1380
|
+
/**
|
|
1381
|
+
* Associates value with key only if key is not already present.
|
|
1382
|
+
*
|
|
1383
|
+
* @returns The current value (existing or newly set)
|
|
1384
|
+
*/
|
|
1385
|
+
setIfAbsent(key: K, value: V): V;
|
|
1386
|
+
/**
|
|
1387
|
+
* This method removes the mapping for a key if it is present.
|
|
1388
|
+
*
|
|
1389
|
+
* @returns The value that was associated with key, or undefined if none
|
|
1390
|
+
*/
|
|
1391
|
+
delete(key: K): V | undefined;
|
|
1392
|
+
/**
|
|
1393
|
+
* This method removes the entry for key only if the key has the specified value.
|
|
1394
|
+
*
|
|
1395
|
+
* @returns true if the entry was removed
|
|
1396
|
+
*/
|
|
1397
|
+
deleteEntry(key: K, value: V): boolean;
|
|
1398
|
+
/**
|
|
1399
|
+
* This method replaces the entry for key only if the key has a value.
|
|
1400
|
+
*
|
|
1401
|
+
* @returns The previous value, or undefined if key was not present
|
|
1402
|
+
*/
|
|
1403
|
+
replace(key: K, value: V): V | undefined;
|
|
1404
|
+
/**
|
|
1405
|
+
* This method replaces the entry for key only if the key has the specified oldValue.
|
|
1406
|
+
*
|
|
1407
|
+
* @returns true if the value was replaced
|
|
1408
|
+
*/
|
|
1409
|
+
replaceEntry(key: K, oldValue: V, newValue: V): boolean;
|
|
1410
|
+
/**
|
|
1411
|
+
* If the specified key is not already associated with a value,
|
|
1412
|
+
* computes its value using the given function and enters it into the map.
|
|
1413
|
+
*
|
|
1414
|
+
* @returns The current (existing or computed) value associated with the key
|
|
1415
|
+
*/
|
|
1416
|
+
computeIfAbsent(key: K, mappingFunction: (key: K) => V): V;
|
|
1417
|
+
/**
|
|
1418
|
+
* If the value for the specified key is present, tries to compute
|
|
1419
|
+
* a new mapping given the key and its current mapped value.
|
|
1420
|
+
*
|
|
1421
|
+
* @returns The new value associated with the key, or undefined if none
|
|
1422
|
+
*/
|
|
1423
|
+
computeIfPresent(key: K, remappingFunction: (key: K, value: V) => V | undefined): V | undefined;
|
|
1424
|
+
/**
|
|
1425
|
+
* This method tries to compute a mapping for the specified key and its current mapped value.
|
|
1426
|
+
*
|
|
1427
|
+
* @returns The new value associated with the key, or undefined if none
|
|
1428
|
+
*/
|
|
1429
|
+
compute(key: K, remappingFunction: (key: K, value: V | undefined) => V | undefined): V | undefined;
|
|
1430
|
+
/**
|
|
1431
|
+
* If the specified key is not already associated with a value or is
|
|
1432
|
+
* associated with undefined, associates it with the given non-undefined value.
|
|
1433
|
+
* Otherwise, replaces the value with the results of the given remapping function.
|
|
1434
|
+
*
|
|
1435
|
+
* @returns The new value associated with the key, or undefined if none
|
|
1436
|
+
*/
|
|
1437
|
+
merge(key: K, value: V, remappingFunction: (oldValue: V, newValue: V) => V | undefined): V | undefined;
|
|
1438
|
+
/**
|
|
1439
|
+
* This method copies all mappings from the specified map to this map.
|
|
1440
|
+
*/
|
|
1441
|
+
putAll(other: ReadonlyMapLike<K, V> | Iterable<Entry<K, V>>): void;
|
|
1442
|
+
/**
|
|
1443
|
+
* This method removes all mappings from this map.
|
|
1444
|
+
*/
|
|
1445
|
+
clear(): void;
|
|
1446
|
+
}
|
|
1447
|
+
/**
|
|
1448
|
+
* SortedMap<K, V> - Map that maintains keys in sorted order.
|
|
1449
|
+
*
|
|
1450
|
+
* Mirrors java.util.SortedMap.
|
|
1451
|
+
*/
|
|
1452
|
+
export interface SortedMap<K, V> extends MapLike<K, V> {
|
|
1453
|
+
/**
|
|
1454
|
+
* This value is the comparison function used to order keys.
|
|
1455
|
+
*/
|
|
1456
|
+
readonly comparator: Compare<K>;
|
|
1457
|
+
/**
|
|
1458
|
+
* This method gives the first (lowest) key in this map.
|
|
1459
|
+
*
|
|
1460
|
+
* @throws Error if map is empty
|
|
1461
|
+
*/
|
|
1462
|
+
firstKey(): K;
|
|
1463
|
+
/**
|
|
1464
|
+
* This method gives the last (highest) key in this map.
|
|
1465
|
+
*
|
|
1466
|
+
* @throws Error if map is empty
|
|
1467
|
+
*/
|
|
1468
|
+
lastKey(): K;
|
|
1469
|
+
/**
|
|
1470
|
+
* This method gives a view of the portion of this map whose keys are less than toKey.
|
|
1471
|
+
*/
|
|
1472
|
+
headMap(toKey: K): SortedMap<K, V>;
|
|
1473
|
+
/**
|
|
1474
|
+
* This method gives a view of the portion of this map whose keys are greater than or equal to fromKey.
|
|
1475
|
+
*/
|
|
1476
|
+
tailMap(fromKey: K): SortedMap<K, V>;
|
|
1477
|
+
/**
|
|
1478
|
+
* This method gives a view of the portion of this map whose keys range from fromKey to toKey.
|
|
1479
|
+
*/
|
|
1480
|
+
subMap(fromKey: K, toKey: K): SortedMap<K, V>;
|
|
1481
|
+
}
|
|
1482
|
+
/**
|
|
1483
|
+
* NavigableMap<K, V> - Extended sorted map with navigation methods.
|
|
1484
|
+
*
|
|
1485
|
+
* Mirrors java.util.NavigableMap.
|
|
1486
|
+
*/
|
|
1487
|
+
export interface NavigableMap<K, V> extends SortedMap<K, V> {
|
|
1488
|
+
/**
|
|
1489
|
+
* This method gives the greatest key less than or equal to the given key, or undefined if none.
|
|
1490
|
+
*/
|
|
1491
|
+
floorKey(key: K): K | undefined;
|
|
1492
|
+
/**
|
|
1493
|
+
* This method gives the least key greater than or equal to the given key, or undefined if none.
|
|
1494
|
+
*/
|
|
1495
|
+
ceilingKey(key: K): K | undefined;
|
|
1496
|
+
/**
|
|
1497
|
+
* This method gives the greatest key strictly less than the given key, or undefined if none.
|
|
1498
|
+
*/
|
|
1499
|
+
lowerKey(key: K): K | undefined;
|
|
1500
|
+
/**
|
|
1501
|
+
* This method gives the least key strictly greater than the given key, or undefined if none.
|
|
1502
|
+
*/
|
|
1503
|
+
higherKey(key: K): K | undefined;
|
|
1504
|
+
/**
|
|
1505
|
+
* This method gives the entry for the greatest key less than or equal to the given key, or undefined.
|
|
1506
|
+
*/
|
|
1507
|
+
floorEntry(key: K): Entry<K, V> | undefined;
|
|
1508
|
+
/**
|
|
1509
|
+
* This method gives the entry for the least key greater than or equal to the given key, or undefined.
|
|
1510
|
+
*/
|
|
1511
|
+
ceilingEntry(key: K): Entry<K, V> | undefined;
|
|
1512
|
+
/**
|
|
1513
|
+
* This method gives the entry for the greatest key strictly less than the given key, or undefined.
|
|
1514
|
+
*/
|
|
1515
|
+
lowerEntry(key: K): Entry<K, V> | undefined;
|
|
1516
|
+
/**
|
|
1517
|
+
* This method gives the entry for the least key strictly greater than the given key, or undefined.
|
|
1518
|
+
*/
|
|
1519
|
+
higherEntry(key: K): Entry<K, V> | undefined;
|
|
1520
|
+
/**
|
|
1521
|
+
* This method gives the first (lowest) entry in this map, or undefined if empty.
|
|
1522
|
+
*/
|
|
1523
|
+
firstEntry(): Entry<K, V> | undefined;
|
|
1524
|
+
/**
|
|
1525
|
+
* This method gives the last (highest) entry in this map, or undefined if empty.
|
|
1526
|
+
*/
|
|
1527
|
+
lastEntry(): Entry<K, V> | undefined;
|
|
1528
|
+
/**
|
|
1529
|
+
* Removes and gives the first (lowest) entry, or undefined if empty.
|
|
1530
|
+
*/
|
|
1531
|
+
pollFirstEntry(): Entry<K, V> | undefined;
|
|
1532
|
+
/**
|
|
1533
|
+
* Removes and gives the last (highest) entry, or undefined if empty.
|
|
1534
|
+
*/
|
|
1535
|
+
pollLastEntry(): Entry<K, V> | undefined;
|
|
1536
|
+
/**
|
|
1537
|
+
* This method gives a reverse order view of this map.
|
|
1538
|
+
*/
|
|
1539
|
+
descendingMap(): NavigableMap<K, V>;
|
|
1540
|
+
/**
|
|
1541
|
+
* This method gives a reverse order iterable of the keys.
|
|
1542
|
+
*/
|
|
1543
|
+
descendingKeys(): Iterable<K>;
|
|
1544
|
+
}
|
|
1545
|
+
/**
|
|
1546
|
+
* MultiMap<K, V> - Map that can hold multiple values per key.
|
|
1547
|
+
*
|
|
1548
|
+
* Not in standard Java Collections, but commonly used.
|
|
1549
|
+
*/
|
|
1550
|
+
export interface MultiMap<K, V> {
|
|
1551
|
+
readonly size: number;
|
|
1552
|
+
readonly isEmpty: boolean;
|
|
1553
|
+
/**
|
|
1554
|
+
* This method gives true if the map contains at least one value for the key.
|
|
1555
|
+
*/
|
|
1556
|
+
has(key: K): boolean;
|
|
1557
|
+
/**
|
|
1558
|
+
* This method gives all values associated with the key.
|
|
1559
|
+
*/
|
|
1560
|
+
get(key: K): ReadonlyCollection<V>;
|
|
1561
|
+
/**
|
|
1562
|
+
* This method adds a value to the collection of values for the key.
|
|
1563
|
+
*/
|
|
1564
|
+
put(key: K, value: V): boolean;
|
|
1565
|
+
/**
|
|
1566
|
+
* This method adds all values to the collection of values for the key.
|
|
1567
|
+
*/
|
|
1568
|
+
putAll(key: K, values: Iterable<V>): boolean;
|
|
1569
|
+
/**
|
|
1570
|
+
* This method removes a single occurrence of value from the key's collection.
|
|
1571
|
+
*/
|
|
1572
|
+
remove(key: K, value: V): boolean;
|
|
1573
|
+
/**
|
|
1574
|
+
* This method removes all values for the key.
|
|
1575
|
+
*/
|
|
1576
|
+
removeAll(key: K): ReadonlyCollection<V>;
|
|
1577
|
+
/**
|
|
1578
|
+
* This method replaces all values for the key with the given values.
|
|
1579
|
+
*/
|
|
1580
|
+
replaceAll(key: K, values: Iterable<V>): ReadonlyCollection<V>;
|
|
1581
|
+
/**
|
|
1582
|
+
* This method gives all keys that have at least one value.
|
|
1583
|
+
*/
|
|
1584
|
+
keys(): Iterable<K>;
|
|
1585
|
+
/**
|
|
1586
|
+
* This method gives all entries (key with one of its values).
|
|
1587
|
+
*/
|
|
1588
|
+
entries(): Iterable<Entry<K, V>>;
|
|
1589
|
+
/**
|
|
1590
|
+
* This method removes all mappings.
|
|
1591
|
+
*/
|
|
1592
|
+
clear(): void;
|
|
1593
|
+
}
|
|
1594
|
+
/**
|
|
1595
|
+
* BiMap<K, V> - Bidirectional map (1:1 mapping).
|
|
1596
|
+
*
|
|
1597
|
+
* Both keys and values are unique.
|
|
1598
|
+
* With this type, lookup is by key or by value.
|
|
1599
|
+
*/
|
|
1600
|
+
export interface BiMap<K, V> extends MapLike<K, V> {
|
|
1601
|
+
/**
|
|
1602
|
+
* This method gives the inverse view of this bimap (swaps keys and values).
|
|
1603
|
+
*/
|
|
1604
|
+
inverse(): BiMap<V, K>;
|
|
1605
|
+
/**
|
|
1606
|
+
* This method gives the key mapped to the specified value, or undefined if none.
|
|
1607
|
+
*/
|
|
1608
|
+
getKey(value: V): K | undefined;
|
|
1609
|
+
/**
|
|
1610
|
+
* This method forces the put by removing any existing entry with the same value.
|
|
1611
|
+
*/
|
|
1612
|
+
forcePut(key: K, value: V): V | undefined;
|
|
1613
|
+
}
|
|
1614
|
+
/**
|
|
1615
|
+
* WeakMapLike<K extends object, V> - Map with weak key references.
|
|
1616
|
+
*
|
|
1617
|
+
* Mirrors JavaScript's WeakMap but with our interface conventions.
|
|
1618
|
+
* Keys must be objects and are held weakly (so garbage collection can remove them).
|
|
1619
|
+
*/
|
|
1620
|
+
export interface WeakMapLike<K extends object, V> {
|
|
1621
|
+
has(key: K): boolean;
|
|
1622
|
+
get(key: K): V | undefined;
|
|
1623
|
+
set(key: K, value: V): this;
|
|
1624
|
+
delete(key: K): boolean;
|
|
1625
|
+
}
|
|
1626
|
+
/**
|
|
1627
|
+
* ReadonlySet<T> - Readonly set with no duplicates.
|
|
1628
|
+
*/
|
|
1629
|
+
export interface ReadonlySet<T> extends ReadonlyCollection<T> {
|
|
1630
|
+
/**
|
|
1631
|
+
* This method gives true if this set contains the specified element.
|
|
1632
|
+
*/
|
|
1633
|
+
contains(element: T): boolean;
|
|
1634
|
+
/**
|
|
1635
|
+
* This method gives true if this set contains all elements in the specified collection.
|
|
1636
|
+
*/
|
|
1637
|
+
containsAll(other: Iterable<T>): boolean;
|
|
1638
|
+
}
|
|
1639
|
+
/**
|
|
1640
|
+
* Set<T> - Mutable set with no duplicates.
|
|
1641
|
+
*
|
|
1642
|
+
* Mirrors java.util.Set.
|
|
1643
|
+
* Elements are unique based on the set's equality function.
|
|
1644
|
+
*/
|
|
1645
|
+
interface Set$1<T> extends ReadonlySet<T>, Collection<T> {
|
|
1646
|
+
/**
|
|
1647
|
+
* This method adds the specified element to this set if it is not already present.
|
|
1648
|
+
*
|
|
1649
|
+
* @returns true if this set did not already contain the element
|
|
1650
|
+
*/
|
|
1651
|
+
add(element: T): boolean;
|
|
1652
|
+
/**
|
|
1653
|
+
* This method removes the specified element from this set if it is present.
|
|
1654
|
+
*
|
|
1655
|
+
* @returns true if this set contained the element
|
|
1656
|
+
*/
|
|
1657
|
+
remove(element: T): boolean;
|
|
1658
|
+
/**
|
|
1659
|
+
* This method adds all elements in the specified collection to this set.
|
|
1660
|
+
* This is a union operation.
|
|
1661
|
+
*
|
|
1662
|
+
* @returns true if this set changed
|
|
1663
|
+
*/
|
|
1664
|
+
addAll(other: Iterable<T>): boolean;
|
|
1665
|
+
/**
|
|
1666
|
+
* This method removes from this set all of its elements that are contained in the specified collection.
|
|
1667
|
+
* This is a difference operation.
|
|
1668
|
+
*
|
|
1669
|
+
* @returns true if this set changed
|
|
1670
|
+
*/
|
|
1671
|
+
removeAll(other: Iterable<T>): boolean;
|
|
1672
|
+
/**
|
|
1673
|
+
* This method retains only the elements in this set that are contained in the specified collection.
|
|
1674
|
+
* This is an intersection operation.
|
|
1675
|
+
*
|
|
1676
|
+
* @returns true if this set changed
|
|
1677
|
+
*/
|
|
1678
|
+
retainAll(other: Iterable<T>): boolean;
|
|
1679
|
+
}
|
|
1680
|
+
/**
|
|
1681
|
+
* SortedSet<T> - Set that maintains elements in sorted order.
|
|
1682
|
+
*
|
|
1683
|
+
* Mirrors java.util.SortedSet.
|
|
1684
|
+
*/
|
|
1685
|
+
export interface SortedSet<T> extends Set$1<T> {
|
|
1686
|
+
/**
|
|
1687
|
+
* This value is the comparator used to order elements in this set.
|
|
1688
|
+
*/
|
|
1689
|
+
readonly comparator: Compare<T>;
|
|
1690
|
+
/**
|
|
1691
|
+
* This method gives the first (lowest) element in this set.
|
|
1692
|
+
*
|
|
1693
|
+
* @throws Error if set is empty
|
|
1694
|
+
*/
|
|
1695
|
+
first(): T;
|
|
1696
|
+
/**
|
|
1697
|
+
* This method gives the last (highest) element in this set.
|
|
1698
|
+
*
|
|
1699
|
+
* @throws Error if set is empty
|
|
1700
|
+
*/
|
|
1701
|
+
last(): T;
|
|
1702
|
+
/**
|
|
1703
|
+
* This method gives a view of the portion of this set whose elements are strictly less than toElement.
|
|
1704
|
+
*/
|
|
1705
|
+
headSet(toElement: T): SortedSet<T>;
|
|
1706
|
+
/**
|
|
1707
|
+
* This method gives a view of the portion of this set whose elements are greater than or equal to fromElement.
|
|
1708
|
+
*/
|
|
1709
|
+
tailSet(fromElement: T): SortedSet<T>;
|
|
1710
|
+
/**
|
|
1711
|
+
* This method gives a view of the portion of this set whose elements range from fromElement to toElement.
|
|
1712
|
+
*/
|
|
1713
|
+
subSet(fromElement: T, toElement: T): SortedSet<T>;
|
|
1714
|
+
}
|
|
1715
|
+
/**
|
|
1716
|
+
* NavigableSet<T> - Extended sorted set with navigation methods.
|
|
1717
|
+
*
|
|
1718
|
+
* Mirrors java.util.NavigableSet.
|
|
1719
|
+
*/
|
|
1720
|
+
export interface NavigableSet<T> extends SortedSet<T> {
|
|
1721
|
+
/**
|
|
1722
|
+
* This method gives the greatest element less than or equal to the given element, or undefined if none.
|
|
1723
|
+
*/
|
|
1724
|
+
floor(element: T): T | undefined;
|
|
1725
|
+
/**
|
|
1726
|
+
* This method gives the least element greater than or equal to the given element, or undefined if none.
|
|
1727
|
+
*/
|
|
1728
|
+
ceiling(element: T): T | undefined;
|
|
1729
|
+
/**
|
|
1730
|
+
* This method gives the greatest element strictly less than the given element, or undefined if none.
|
|
1731
|
+
*/
|
|
1732
|
+
lower(element: T): T | undefined;
|
|
1733
|
+
/**
|
|
1734
|
+
* This method gives the least element strictly greater than the given element, or undefined if none.
|
|
1735
|
+
*/
|
|
1736
|
+
higher(element: T): T | undefined;
|
|
1737
|
+
/**
|
|
1738
|
+
* Retrieves and removes the first (lowest) element, or gives undefined if empty.
|
|
1739
|
+
*/
|
|
1740
|
+
pollFirst(): T | undefined;
|
|
1741
|
+
/**
|
|
1742
|
+
* Retrieves and removes the last (highest) element, or gives undefined if empty.
|
|
1743
|
+
*/
|
|
1744
|
+
pollLast(): T | undefined;
|
|
1745
|
+
/**
|
|
1746
|
+
* This method gives a reverse order view of this set.
|
|
1747
|
+
*/
|
|
1748
|
+
descendingSet(): NavigableSet<T>;
|
|
1749
|
+
/**
|
|
1750
|
+
* This method gives a reverse order iterator over the elements in this set.
|
|
1751
|
+
*/
|
|
1752
|
+
descendingIterator(): Iterator<T>;
|
|
1753
|
+
}
|
|
1754
|
+
/**
|
|
1755
|
+
* A set of listeners for the events of a storage: the change batches of `observe()`, or the status
|
|
1756
|
+
* of a sync. A storage package uses it, so each storage treats its listeners in the same way.
|
|
1757
|
+
*/
|
|
1758
|
+
/** The function that gets the error of a listener. */
|
|
1759
|
+
type ListenerErrorHandler = (error: unknown) => void;
|
|
1760
|
+
/**
|
|
1761
|
+
* The default handler of the error of a listener. It throws the error again in a microtask, as an
|
|
1762
|
+
* `EventTarget` reports the error of a listener. Thus the error is not lost, and the write that
|
|
1763
|
+
* sent the event does not fail.
|
|
1764
|
+
*/
|
|
1765
|
+
export declare const reportListenerError: ListenerErrorHandler;
|
|
1766
|
+
/**
|
|
1767
|
+
* The listeners of one kind of event.
|
|
1768
|
+
*
|
|
1769
|
+
* - `emit()` gives the event to each listener, in the order of `add()`.
|
|
1770
|
+
* - A listener that `add()` gets during an `emit()` gets the next event, not this one.
|
|
1771
|
+
* - The error of a listener goes to the error handler. The other listeners get the event.
|
|
1772
|
+
* - The same function can be added two times. Each `add()` gives its own function that removes it.
|
|
1773
|
+
*/
|
|
1774
|
+
export declare class StorageListeners<E> {
|
|
1775
|
+
private readonly listeners;
|
|
1776
|
+
private readonly onError;
|
|
1777
|
+
/** @param onError - The function that gets the error of a listener. The default is `reportListenerError`. */
|
|
1778
|
+
constructor(onError?: ListenerErrorHandler);
|
|
1779
|
+
/** The number of listeners. */
|
|
1780
|
+
get size(): number;
|
|
1781
|
+
/**
|
|
1782
|
+
* This method adds a listener.
|
|
1783
|
+
*
|
|
1784
|
+
* @returns The function that removes the listener. A second use of it does nothing.
|
|
1785
|
+
*/
|
|
1786
|
+
add(listener: (event: E) => void): () => void;
|
|
1787
|
+
/** This method gives the event to each listener. */
|
|
1788
|
+
emit(event: E): void;
|
|
1789
|
+
/** This method removes each listener. */
|
|
1790
|
+
clear(): void;
|
|
1791
|
+
}
|
|
1792
|
+
/**
|
|
1793
|
+
* The keys of an ordered list: fractional indexing with base-62 keys. A
|
|
1794
|
+
* key is a string. Each item of the list keeps one, and the list sorts by it. An item that moves
|
|
1795
|
+
* between two others gets a key between theirs, so a move writes one item and no other.
|
|
1796
|
+
*
|
|
1797
|
+
* - A key has an integer part and a fraction part. The first character of the integer part gives its
|
|
1798
|
+
* length. An insertion at an end changes the integer part, so the keys stay short.
|
|
1799
|
+
* - An insertion between two keys uses the fraction part, which can grow without a limit. Thus the keys
|
|
1800
|
+
* do not run out, and the list needs no rebalance.
|
|
1801
|
+
* - Compare two keys by the code units of the strings (`a < b`), not with `localeCompare`.
|
|
1802
|
+
* - Two writers that move items into the same gap at the same time can get the same key. Sort by the
|
|
1803
|
+
* key, then by the id of the item, so each reader sees one order.
|
|
1804
|
+
*
|
|
1805
|
+
* The algorithm is the one of the `fractional-indexing` package of Rocicorp (CC0), after the article
|
|
1806
|
+
* "Implementing Fractional Indexing" of David Greenspan.
|
|
1807
|
+
*/
|
|
1808
|
+
/** The digits of a key, in ascending order of their code units. */
|
|
1809
|
+
export declare const KEY_DIGITS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
|
1810
|
+
/**
|
|
1811
|
+
* This function gives a key strictly between two keys. Without `prev`, the key comes before `next`.
|
|
1812
|
+
* Without `next`, it comes after `prev`. Without both, it gives the first key of a list ("a0").
|
|
1813
|
+
*
|
|
1814
|
+
* @throws RangeError when a key is not valid, or when `prev` is not before `next`
|
|
1815
|
+
*
|
|
1816
|
+
* @example
|
|
1817
|
+
* ```ts
|
|
1818
|
+
* keyBetween(undefined, undefined); // "a0"
|
|
1819
|
+
* keyBetween("a0", undefined); // "a1", after a0
|
|
1820
|
+
* keyBetween("a0", "a1"); // "a0V"
|
|
1821
|
+
* ```
|
|
1822
|
+
*/
|
|
1823
|
+
export declare function keyBetween(prev: string | null | undefined, next: string | null | undefined): string;
|
|
1824
|
+
/**
|
|
1825
|
+
* This function gives `count` keys in ascending order, strictly between two keys.
|
|
1826
|
+
* Use it to put several items at one place, or to give keys to a new list.
|
|
1827
|
+
*
|
|
1828
|
+
* @throws RangeError as `keyBetween`, or when `count` is not an integer of 0 or more
|
|
1829
|
+
*/
|
|
1830
|
+
export declare function keysBetween(prev: string | null | undefined, next: string | null | undefined, count: number): string[];
|
|
1831
|
+
/** The options of `isValidKey`. */
|
|
1832
|
+
interface KeyCheckOptions {
|
|
1833
|
+
/** The longest key that the check accepts. The default is 1024 characters. */
|
|
1834
|
+
readonly maxLength?: number;
|
|
1835
|
+
}
|
|
1836
|
+
/**
|
|
1837
|
+
* This function tells if a value is a valid key, for example a key that a client writes. A key that
|
|
1838
|
+
* is longer than `maxLength` is not valid.
|
|
1839
|
+
*/
|
|
1840
|
+
export declare function isValidKey(key: unknown, options?: KeyCheckOptions): key is string;
|
|
1841
|
+
/** An item of an ordered list: its id and its key. */
|
|
1842
|
+
interface KeyedItem {
|
|
1843
|
+
readonly id: string;
|
|
1844
|
+
readonly key: string;
|
|
1845
|
+
}
|
|
1846
|
+
/**
|
|
1847
|
+
* This function gives the writes of a move: the new key of the item that moves, and new keys for the
|
|
1848
|
+
* neighbors that need them. Give the other items of the list in order (by key, then by id), without
|
|
1849
|
+
* the item that moves. Also give the index of the drop, from 0 to their number (`dropIndex`).
|
|
1850
|
+
*
|
|
1851
|
+
* Two writers can give two items one key. A drop between two such items has no key between them.
|
|
1852
|
+
* So the helper first gives new keys to the items after the drop that have the key of the item before
|
|
1853
|
+
* it. Each write is `{ id, key }`. The writes of the neighbors come first, and the write of the item
|
|
1854
|
+
* that moves comes last.
|
|
1855
|
+
*
|
|
1856
|
+
* @throws RangeError when the index is outside the list, when the list holds the item that moves, or
|
|
1857
|
+
* when a key is not valid
|
|
1858
|
+
*
|
|
1859
|
+
* @example
|
|
1860
|
+
* ```ts
|
|
1861
|
+
* const index = dropIndex(pointer, others.map(centerOf));
|
|
1862
|
+
* for (const { id, key } of keysForDrop(others, index, dragged.id)) await save(id, { rank: key });
|
|
1863
|
+
* ```
|
|
1864
|
+
*/
|
|
1865
|
+
export declare function keysForDrop(items: readonly KeyedItem[], index: number, movingId: string): Array<{
|
|
1866
|
+
id: string;
|
|
1867
|
+
key: string;
|
|
1868
|
+
}>;
|
|
1869
|
+
/** The order of two keys: the code units of the strings, as the keys need (not `localeCompare`). */
|
|
1870
|
+
export declare function compareKeys(a: string, b: string): number;
|
|
1871
|
+
/** A point, for example the center of an item on the screen. */
|
|
1872
|
+
interface Point {
|
|
1873
|
+
readonly x: number;
|
|
1874
|
+
readonly y: number;
|
|
1875
|
+
}
|
|
1876
|
+
/**
|
|
1877
|
+
* This function gives the index at which a point goes among the items of a list on the screen.
|
|
1878
|
+
* The reading order is the rows from top to bottom, and each row from left to right.
|
|
1879
|
+
* The index is that of the first item whose center comes after the point, from 0 to the number of items. A center within
|
|
1880
|
+
* `rowTolerance` of the point on the vertical axis is in the row of the point.
|
|
1881
|
+
*
|
|
1882
|
+
* Give the centers in reading order, the same order as the keys, and without the item that moves.
|
|
1883
|
+
*
|
|
1884
|
+
* @example
|
|
1885
|
+
* ```ts
|
|
1886
|
+
* const index = dropIndex(pointer, others.map(centerOf));
|
|
1887
|
+
* const key = keyBetween(keys[index - 1], keys[index]);
|
|
1888
|
+
* ```
|
|
1889
|
+
*/
|
|
1890
|
+
export declare function dropIndex(point: Point, centers: readonly Point[], rowTolerance?: number): number;
|
|
1891
|
+
/**
|
|
1892
|
+
* ReadonlyQueue<T> - Readonly FIFO (First-In-First-Out) queue.
|
|
1893
|
+
*
|
|
1894
|
+
* This type gives read-only access to queue operations.
|
|
1895
|
+
*/
|
|
1896
|
+
interface ReadonlyQueue<T> extends ReadonlyCollection<T> {
|
|
1897
|
+
/**
|
|
1898
|
+
* Retrieves, but does not remove, the head of this queue.
|
|
1899
|
+
*
|
|
1900
|
+
* @returns The head element
|
|
1901
|
+
* @throws Error if queue is empty
|
|
1902
|
+
*/
|
|
1903
|
+
peek(): T;
|
|
1904
|
+
/**
|
|
1905
|
+
* This method gives the head element without throwing if empty.
|
|
1906
|
+
* This method gives undefined if queue is empty.
|
|
1907
|
+
*/
|
|
1908
|
+
peekOrUndefined(): T | undefined;
|
|
1909
|
+
}
|
|
1910
|
+
/**
|
|
1911
|
+
* Queue<T> - FIFO (First-In-First-Out) queue.
|
|
1912
|
+
*
|
|
1913
|
+
* Mirrors java.util.Queue.
|
|
1914
|
+
* Elements are processed in the order they were added.
|
|
1915
|
+
*/
|
|
1916
|
+
interface Queue<T> extends ReadonlyQueue<T>, Collection<T> {
|
|
1917
|
+
/**
|
|
1918
|
+
* This method inserts an element into the queue.
|
|
1919
|
+
* For bounded queues with overflow='throw', throws if queue is full.
|
|
1920
|
+
*
|
|
1921
|
+
* @param element The element to add
|
|
1922
|
+
* @returns true if element was added
|
|
1923
|
+
* @throws Error if queue is full (bounded with throw policy)
|
|
1924
|
+
*/
|
|
1925
|
+
offer(element: T): boolean;
|
|
1926
|
+
/**
|
|
1927
|
+
* Retrieves and removes the head of this queue.
|
|
1928
|
+
*
|
|
1929
|
+
* @returns The head element
|
|
1930
|
+
* @throws Error if queue is empty
|
|
1931
|
+
*/
|
|
1932
|
+
poll(): T;
|
|
1933
|
+
/**
|
|
1934
|
+
* Retrieves and removes the head of this queue, or gives undefined if empty.
|
|
1935
|
+
*/
|
|
1936
|
+
pollOrUndefined(): T | undefined;
|
|
1937
|
+
/**
|
|
1938
|
+
* Alias for offer(). This method adds element to end of queue.
|
|
1939
|
+
* This method is here for consistency with standard queue terminology.
|
|
1940
|
+
*/
|
|
1941
|
+
enqueue(element: T): boolean;
|
|
1942
|
+
/**
|
|
1943
|
+
* Alias for poll(). Removes and gives element from front of queue.
|
|
1944
|
+
* This method is here for consistency with standard queue terminology.
|
|
1945
|
+
*/
|
|
1946
|
+
dequeue(): T;
|
|
1947
|
+
}
|
|
1948
|
+
/**
|
|
1949
|
+
* Deque<T> - Double-ended queue.
|
|
1950
|
+
*
|
|
1951
|
+
* Mirrors java.util.Deque.
|
|
1952
|
+
* This type supports insertion and removal at both ends.
|
|
1953
|
+
* This type operates as a queue (FIFO) or a stack (LIFO).
|
|
1954
|
+
*/
|
|
1955
|
+
interface Deque<T> extends Queue<T> {
|
|
1956
|
+
/**
|
|
1957
|
+
* This method inserts element at the front of this deque.
|
|
1958
|
+
*
|
|
1959
|
+
* @param element The element to add
|
|
1960
|
+
* @throws Error if deque is full (bounded with throw policy)
|
|
1961
|
+
*/
|
|
1962
|
+
addFirst(element: T): void;
|
|
1963
|
+
/**
|
|
1964
|
+
* This method inserts element at the front, giving false if full.
|
|
1965
|
+
*/
|
|
1966
|
+
offerFirst(element: T): boolean;
|
|
1967
|
+
/**
|
|
1968
|
+
* Retrieves and removes the first element.
|
|
1969
|
+
*
|
|
1970
|
+
* @throws Error if deque is empty
|
|
1971
|
+
*/
|
|
1972
|
+
removeFirst(): T;
|
|
1973
|
+
/**
|
|
1974
|
+
* Retrieves and removes the first element, or undefined if empty.
|
|
1975
|
+
*/
|
|
1976
|
+
pollFirst(): T | undefined;
|
|
1977
|
+
/**
|
|
1978
|
+
* Retrieves but does not remove the first element.
|
|
1979
|
+
*
|
|
1980
|
+
* @throws Error if deque is empty
|
|
1981
|
+
*/
|
|
1982
|
+
peekFirst(): T;
|
|
1983
|
+
/**
|
|
1984
|
+
* Retrieves but does not remove the first element, or undefined if empty.
|
|
1985
|
+
*/
|
|
1986
|
+
peekFirstOrUndefined(): T | undefined;
|
|
1987
|
+
/**
|
|
1988
|
+
* This method inserts element at the end of this deque.
|
|
1989
|
+
*
|
|
1990
|
+
* @param element The element to add
|
|
1991
|
+
* @throws Error if deque is full (bounded with throw policy)
|
|
1992
|
+
*/
|
|
1993
|
+
addLast(element: T): void;
|
|
1994
|
+
/**
|
|
1995
|
+
* This method inserts element at the end, giving false if full.
|
|
1996
|
+
*/
|
|
1997
|
+
offerLast(element: T): boolean;
|
|
1998
|
+
/**
|
|
1999
|
+
* Retrieves and removes the last element.
|
|
2000
|
+
*
|
|
2001
|
+
* @throws Error if deque is empty
|
|
2002
|
+
*/
|
|
2003
|
+
removeLast(): T;
|
|
2004
|
+
/**
|
|
2005
|
+
* Retrieves and removes the last element, or undefined if empty.
|
|
2006
|
+
*/
|
|
2007
|
+
pollLast(): T | undefined;
|
|
2008
|
+
/**
|
|
2009
|
+
* Retrieves but does not remove the last element.
|
|
2010
|
+
*
|
|
2011
|
+
* @throws Error if deque is empty
|
|
2012
|
+
*/
|
|
2013
|
+
peekLast(): T;
|
|
2014
|
+
/**
|
|
2015
|
+
* Retrieves but does not remove the last element, or undefined if empty.
|
|
2016
|
+
*/
|
|
2017
|
+
peekLastOrUndefined(): T | undefined;
|
|
2018
|
+
/**
|
|
2019
|
+
* This method pushes element onto the stack represented by this deque.
|
|
2020
|
+
* Equivalent to addLast().
|
|
2021
|
+
*
|
|
2022
|
+
* @param element The element to push
|
|
2023
|
+
*/
|
|
2024
|
+
push(element: T): void;
|
|
2025
|
+
/**
|
|
2026
|
+
* This method pops an element from the stack represented by this deque.
|
|
2027
|
+
* Equivalent to removeLast().
|
|
2028
|
+
*
|
|
2029
|
+
* @returns The popped element
|
|
2030
|
+
* @throws Error if deque is empty
|
|
2031
|
+
*/
|
|
2032
|
+
pop(): T;
|
|
2033
|
+
}
|
|
2034
|
+
/**
|
|
2035
|
+
* BlockingQueue<T> - Queue that supports blocking operations.
|
|
2036
|
+
*
|
|
2037
|
+
* Mirrors java.util.concurrent.BlockingQueue.
|
|
2038
|
+
* Useful for producer-consumer patterns.
|
|
2039
|
+
*/
|
|
2040
|
+
interface BlockingQueue<T> extends Queue<T> {
|
|
2041
|
+
/**
|
|
2042
|
+
* This method inserts element, waiting if necessary for space to become available.
|
|
2043
|
+
*
|
|
2044
|
+
* @param element The element to add
|
|
2045
|
+
* @param timeoutMs Optional timeout in milliseconds
|
|
2046
|
+
* @returns Promise that resolves to true when element is added
|
|
2047
|
+
* @throws Error if timeout expires
|
|
2048
|
+
*/
|
|
2049
|
+
put(element: T, timeoutMs?: number): Promise<void>;
|
|
2050
|
+
/**
|
|
2051
|
+
* Retrieves and removes the head, waiting if necessary until an element is available.
|
|
2052
|
+
*
|
|
2053
|
+
* @param timeoutMs Optional timeout in milliseconds
|
|
2054
|
+
* @returns Promise that resolves to the head element
|
|
2055
|
+
* @throws Error if timeout expires
|
|
2056
|
+
*/
|
|
2057
|
+
take(timeoutMs?: number): Promise<T>;
|
|
2058
|
+
/**
|
|
2059
|
+
* This value is the remaining capacity.
|
|
2060
|
+
* This value is Infinity for unbounded queues.
|
|
2061
|
+
*/
|
|
2062
|
+
readonly remainingCapacity: number;
|
|
2063
|
+
/**
|
|
2064
|
+
* This method removes all available elements and adds them to the given array.
|
|
2065
|
+
* This method gives the number of elements transferred.
|
|
2066
|
+
*/
|
|
2067
|
+
drainTo(target: T[], maxElements?: number): number;
|
|
2068
|
+
}
|
|
2069
|
+
/**
|
|
2070
|
+
* PriorityQueue<T> - Queue that orders elements by priority.
|
|
2071
|
+
*
|
|
2072
|
+
* Mirrors java.util.PriorityQueue.
|
|
2073
|
+
* Elements are removed in priority order (not insertion order).
|
|
2074
|
+
* A comparison function is necessary for this type to determine priority.
|
|
2075
|
+
*/
|
|
2076
|
+
interface PriorityQueue<T> extends Queue<T> {
|
|
2077
|
+
/**
|
|
2078
|
+
* The comparison function used to determine priority.
|
|
2079
|
+
* Elements with lower compare value have higher priority.
|
|
2080
|
+
*/
|
|
2081
|
+
readonly compare: Compare<T>;
|
|
2082
|
+
/**
|
|
2083
|
+
* This value is the comparator used to order elements, or undefined if natural ordering.
|
|
2084
|
+
*/
|
|
2085
|
+
readonly comparator: Compare<T> | undefined;
|
|
2086
|
+
}
|
|
2087
|
+
/**
|
|
2088
|
+
* AsyncQueue<T> - Async iterable queue.
|
|
2089
|
+
*
|
|
2090
|
+
* This type is for async/await patterns and streaming data.
|
|
2091
|
+
* This type implements AsyncIterable for use with for-await-of loops.
|
|
2092
|
+
*/
|
|
2093
|
+
interface AsyncQueue$1<T> extends AsyncIterable<T> {
|
|
2094
|
+
/** This value is the number of elements in the queue. */
|
|
2095
|
+
readonly size: number;
|
|
2096
|
+
/** True if queue is empty */
|
|
2097
|
+
readonly isEmpty: boolean;
|
|
2098
|
+
/**
|
|
2099
|
+
* This method adds element to queue.
|
|
2100
|
+
* For bounded queues, this method can wait for space to become available.
|
|
2101
|
+
*/
|
|
2102
|
+
put(element: T): Promise<void>;
|
|
2103
|
+
/**
|
|
2104
|
+
* Removes and gives next element.
|
|
2105
|
+
* Waits for an element if queue is empty.
|
|
2106
|
+
*/
|
|
2107
|
+
take(): Promise<T>;
|
|
2108
|
+
/**
|
|
2109
|
+
* This method tries to add element without waiting.
|
|
2110
|
+
* This method gives false if queue is full.
|
|
2111
|
+
*/
|
|
2112
|
+
tryPut(element: T): boolean;
|
|
2113
|
+
/**
|
|
2114
|
+
* This method tries to remove element without waiting.
|
|
2115
|
+
* This method gives undefined if queue is empty.
|
|
2116
|
+
*/
|
|
2117
|
+
tryTake(): T | undefined;
|
|
2118
|
+
/**
|
|
2119
|
+
* This method gives next element without removing it.
|
|
2120
|
+
* Waits for an element if queue is empty.
|
|
2121
|
+
*/
|
|
2122
|
+
peek(): Promise<T>;
|
|
2123
|
+
/**
|
|
2124
|
+
* This method closes the queue, preventing further additions.
|
|
2125
|
+
* After this method, consumers can still take the existing elements.
|
|
2126
|
+
*/
|
|
2127
|
+
close(): void;
|
|
2128
|
+
/**
|
|
2129
|
+
* This value is true if queue is closed.
|
|
2130
|
+
*/
|
|
2131
|
+
readonly isClosed: boolean;
|
|
2132
|
+
}
|
|
2133
|
+
/**
|
|
2134
|
+
* TransferQueue<T> - Queue where producers can wait for consumers.
|
|
2135
|
+
*
|
|
2136
|
+
* Mirrors java.util.concurrent.TransferQueue.
|
|
2137
|
+
* Useful for direct handoff between producer and consumer threads.
|
|
2138
|
+
*/
|
|
2139
|
+
interface TransferQueue<T> extends BlockingQueue<T> {
|
|
2140
|
+
/**
|
|
2141
|
+
* Transfers element to consumer, waiting if necessary.
|
|
2142
|
+
* This method waits for a consumer to receive the element.
|
|
2143
|
+
*
|
|
2144
|
+
* @param element The element to transfer
|
|
2145
|
+
* @returns Promise that resolves when element is received
|
|
2146
|
+
*/
|
|
2147
|
+
transfer(element: T): Promise<void>;
|
|
2148
|
+
/**
|
|
2149
|
+
* This method tries to transfer element to waiting consumer.
|
|
2150
|
+
* This method gives false immediately if no consumer is waiting.
|
|
2151
|
+
*
|
|
2152
|
+
* @param element The element to transfer
|
|
2153
|
+
* @param timeoutMs Optional timeout in milliseconds
|
|
2154
|
+
*/
|
|
2155
|
+
tryTransfer(element: T, timeoutMs?: number): Promise<boolean>;
|
|
2156
|
+
/**
|
|
2157
|
+
* This value is true if there are consumers waiting to receive elements.
|
|
2158
|
+
*/
|
|
2159
|
+
readonly hasWaitingConsumer: boolean;
|
|
2160
|
+
/**
|
|
2161
|
+
* This value is an estimate of the number of consumers waiting to receive elements.
|
|
2162
|
+
*/
|
|
2163
|
+
readonly waitingConsumerCount: number;
|
|
2164
|
+
}
|
|
2165
|
+
/**
|
|
2166
|
+
* DelayQueue<T> - Queue where elements become available after a delay.
|
|
2167
|
+
*
|
|
2168
|
+
* Mirrors java.util.concurrent.DelayQueue.
|
|
2169
|
+
* Elements can only be removed once their delay has expired.
|
|
2170
|
+
*/
|
|
2171
|
+
interface DelayQueue<T> extends BlockingQueue<T> {
|
|
2172
|
+
/**
|
|
2173
|
+
* This method adds element with specified delay.
|
|
2174
|
+
*
|
|
2175
|
+
* @param element The element to add
|
|
2176
|
+
* @param delayMs Delay in milliseconds before element becomes available (defaults to 0)
|
|
2177
|
+
*/
|
|
2178
|
+
offer(element: T, delayMs?: number): boolean;
|
|
2179
|
+
/**
|
|
2180
|
+
* This value is the delay (in ms) until next element becomes available.
|
|
2181
|
+
* The delay is `0` when the element is available.
|
|
2182
|
+
* This value is Infinity if queue is empty.
|
|
2183
|
+
*/
|
|
2184
|
+
readonly nextDelay: number;
|
|
2185
|
+
}
|
|
2186
|
+
/**
|
|
2187
|
+
* Options for making an ArrayList.
|
|
2188
|
+
*/
|
|
2189
|
+
export interface ArrayListOptions<T> {
|
|
2190
|
+
/**
|
|
2191
|
+
* Initial capacity of the underlying array.
|
|
2192
|
+
* @default 10
|
|
2193
|
+
*/
|
|
2194
|
+
initialCapacity?: number;
|
|
2195
|
+
/**
|
|
2196
|
+
* Equality function for comparing elements.
|
|
2197
|
+
* @default defaultEq (===)
|
|
2198
|
+
*/
|
|
2199
|
+
eq?: Eq<T>;
|
|
2200
|
+
}
|
|
2201
|
+
/**
|
|
2202
|
+
* ArrayList<T> - Resizable array implementation of the List interface.
|
|
2203
|
+
*
|
|
2204
|
+
* This class gives fast random access (O(1)) and fast append (amortized O(1)).
|
|
2205
|
+
* Insertion and removal in the middle shift elements (O(n)).
|
|
2206
|
+
*
|
|
2207
|
+
* This is the most commonly used list implementation.
|
|
2208
|
+
*/
|
|
2209
|
+
export declare class ArrayList<T> implements List<T>, RandomAccess {
|
|
2210
|
+
private _array;
|
|
2211
|
+
private _size;
|
|
2212
|
+
private readonly _eq;
|
|
2213
|
+
readonly supportsRandomAccess: true;
|
|
2214
|
+
constructor(options?: ArrayListOptions<T>);
|
|
2215
|
+
get size(): number;
|
|
2216
|
+
get isEmpty(): boolean;
|
|
2217
|
+
/**
|
|
2218
|
+
* Current capacity of the underlying array.
|
|
2219
|
+
*/
|
|
2220
|
+
get capacity(): number;
|
|
2221
|
+
get eq(): Eq<T>;
|
|
2222
|
+
get(index: number): T;
|
|
2223
|
+
set(index: number, element: T): T;
|
|
2224
|
+
contains(element: T): boolean;
|
|
2225
|
+
indexOf(element: T, fromIndex?: number): number;
|
|
2226
|
+
lastIndexOf(element: T, fromIndex?: number): number;
|
|
2227
|
+
containsAll(other: Iterable<T>): boolean;
|
|
2228
|
+
add(element: T): boolean;
|
|
2229
|
+
push(element: T): void;
|
|
2230
|
+
unshift(element: T): void;
|
|
2231
|
+
insert(index: number, element: T): void;
|
|
2232
|
+
insertAll(index: number, other: Iterable<T>): boolean;
|
|
2233
|
+
addAll(other: Iterable<T>): boolean;
|
|
2234
|
+
remove(element: T): boolean;
|
|
2235
|
+
removeAt(index: number): T;
|
|
2236
|
+
pop(): T;
|
|
2237
|
+
shift(): T;
|
|
2238
|
+
removeRange(fromIndex: number, toIndex: number): void;
|
|
2239
|
+
removeAll(other: Iterable<T>): boolean;
|
|
2240
|
+
retainAll(other: Iterable<T>): boolean;
|
|
2241
|
+
clear(): void;
|
|
2242
|
+
sort(compare?: Compare<T>): void;
|
|
2243
|
+
reverse(): void;
|
|
2244
|
+
first(): T;
|
|
2245
|
+
last(): T;
|
|
2246
|
+
subList(fromIndex: number, toIndex: number): List<T>;
|
|
2247
|
+
[Symbol.iterator](): Iterator<T>;
|
|
2248
|
+
forEach(action: (element: T, index: number) => void): void;
|
|
2249
|
+
toArray(): T[];
|
|
2250
|
+
/**
|
|
2251
|
+
* This method makes sure that the capacity is at least the specified minimum.
|
|
2252
|
+
*/
|
|
2253
|
+
ensureCapacity(minCapacity: number): void;
|
|
2254
|
+
/**
|
|
2255
|
+
* This method trims the capacity to the current size to minimize memory usage.
|
|
2256
|
+
*/
|
|
2257
|
+
trimToSize(): void;
|
|
2258
|
+
private checkIndex;
|
|
2259
|
+
private checkInsertIndex;
|
|
2260
|
+
private checkRange;
|
|
2261
|
+
}
|
|
2262
|
+
/**
|
|
2263
|
+
* Factory function to make an ArrayList.
|
|
2264
|
+
*
|
|
2265
|
+
* @example
|
|
2266
|
+
* const list = arrayList<number>()
|
|
2267
|
+
* const listWithEq = arrayList<User>({ eq: (a, b) => a.id === b.id })
|
|
2268
|
+
* const listFromArray = arrayList([1, 2, 3])
|
|
2269
|
+
*/
|
|
2270
|
+
export declare function arrayList<T>(optionsOrElements?: ArrayListOptions<T> | Iterable<T>): ArrayList<T>;
|
|
2271
|
+
/**
|
|
2272
|
+
* Options for making an ArrayDeque.
|
|
2273
|
+
*/
|
|
2274
|
+
export interface ArrayDequeOptions<T> {
|
|
2275
|
+
/**
|
|
2276
|
+
* Initial capacity of the underlying array.
|
|
2277
|
+
* This value must be a power of 2 for efficient modulo operations.
|
|
2278
|
+
* @default 16
|
|
2279
|
+
*/
|
|
2280
|
+
initialCapacity?: number;
|
|
2281
|
+
/**
|
|
2282
|
+
* Equality function for comparing elements.
|
|
2283
|
+
* @default defaultEq (===)
|
|
2284
|
+
*/
|
|
2285
|
+
eq?: Eq<T>;
|
|
2286
|
+
}
|
|
2287
|
+
/**
|
|
2288
|
+
* ArrayDeque<T> - Ring buffer implementation of Deque.
|
|
2289
|
+
*
|
|
2290
|
+
* This class uses a circular array to give O(1) operations at both ends.
|
|
2291
|
+
* The array grows automatically when full.
|
|
2292
|
+
*
|
|
2293
|
+
* Not thread-safe. Use synchronized() behavior for concurrent access.
|
|
2294
|
+
*/
|
|
2295
|
+
export declare class ArrayDeque<T> implements Deque<T> {
|
|
2296
|
+
private _elements;
|
|
2297
|
+
private _head;
|
|
2298
|
+
private _tail;
|
|
2299
|
+
private readonly _eq;
|
|
2300
|
+
constructor(options?: ArrayDequeOptions<T>);
|
|
2301
|
+
get size(): number;
|
|
2302
|
+
get isEmpty(): boolean;
|
|
2303
|
+
get eq(): Eq<T>;
|
|
2304
|
+
addFirst(element: T): void;
|
|
2305
|
+
offerFirst(element: T): boolean;
|
|
2306
|
+
removeFirst(): T;
|
|
2307
|
+
pollFirst(): T | undefined;
|
|
2308
|
+
peekFirst(): T;
|
|
2309
|
+
peekFirstOrUndefined(): T | undefined;
|
|
2310
|
+
addLast(element: T): void;
|
|
2311
|
+
offerLast(element: T): boolean;
|
|
2312
|
+
removeLast(): T;
|
|
2313
|
+
pollLast(): T | undefined;
|
|
2314
|
+
peekLast(): T;
|
|
2315
|
+
peekLastOrUndefined(): T | undefined;
|
|
2316
|
+
push(element: T): void;
|
|
2317
|
+
pop(): T;
|
|
2318
|
+
offer(element: T): boolean;
|
|
2319
|
+
poll(): T;
|
|
2320
|
+
peek(): T;
|
|
2321
|
+
peekOrUndefined(): T | undefined;
|
|
2322
|
+
pollOrUndefined(): T | undefined;
|
|
2323
|
+
enqueue(element: T): boolean;
|
|
2324
|
+
dequeue(): T;
|
|
2325
|
+
add(element: T): boolean;
|
|
2326
|
+
addAll(other: Iterable<T>): boolean;
|
|
2327
|
+
contains(element: T): boolean;
|
|
2328
|
+
containsAll(other: Iterable<T>): boolean;
|
|
2329
|
+
remove(element: T): boolean;
|
|
2330
|
+
/**
|
|
2331
|
+
* This method removes the element at a position from the head. The shorter side moves one place.
|
|
2332
|
+
* (Before, a bitwise expression chose the side, and it was not a valid test.)
|
|
2333
|
+
*/
|
|
2334
|
+
private removeAtOffset;
|
|
2335
|
+
removeAll(other: Iterable<T>): boolean;
|
|
2336
|
+
retainAll(other: Iterable<T>): boolean;
|
|
2337
|
+
clear(): void;
|
|
2338
|
+
[Symbol.iterator](): Iterator<T>;
|
|
2339
|
+
forEach(action: (element: T, index: number) => void): void;
|
|
2340
|
+
toArray(): T[];
|
|
2341
|
+
/**
|
|
2342
|
+
* This method doubles the capacity of the deque.
|
|
2343
|
+
*/
|
|
2344
|
+
private grow;
|
|
2345
|
+
/**
|
|
2346
|
+
* This method gives the next power of 2 >= n.
|
|
2347
|
+
*/
|
|
2348
|
+
private nextPowerOfTwo;
|
|
2349
|
+
/**
|
|
2350
|
+
* This value is the current capacity of the underlying array.
|
|
2351
|
+
*/
|
|
2352
|
+
get capacity(): number;
|
|
2353
|
+
/**
|
|
2354
|
+
* This method gives a string representation for debugging.
|
|
2355
|
+
*/
|
|
2356
|
+
toString(): string;
|
|
2357
|
+
}
|
|
2358
|
+
/**
|
|
2359
|
+
* Factory function to make an ArrayDeque.
|
|
2360
|
+
*
|
|
2361
|
+
* @example
|
|
2362
|
+
* const deque = arrayDeque<number>()
|
|
2363
|
+
* const dequeFromArray = arrayDeque([1, 2, 3])
|
|
2364
|
+
* const dequeWithOptions = arrayDeque<string>({ initialCapacity: 32 })
|
|
2365
|
+
*/
|
|
2366
|
+
export declare function arrayDeque<T>(optionsOrElements?: ArrayDequeOptions<T> | Iterable<T>): ArrayDeque<T>;
|
|
2367
|
+
/**
|
|
2368
|
+
* Options for making a HashMap.
|
|
2369
|
+
*/
|
|
2370
|
+
export interface HashMapOptions<K, V> {
|
|
2371
|
+
/**
|
|
2372
|
+
* Initial capacity (number of buckets).
|
|
2373
|
+
* This value must be a power of 2.
|
|
2374
|
+
* @default 16
|
|
2375
|
+
*/
|
|
2376
|
+
initialCapacity?: number;
|
|
2377
|
+
/**
|
|
2378
|
+
* Load factor threshold for resizing (0-1).
|
|
2379
|
+
* When size > capacity * loadFactor, the map resizes.
|
|
2380
|
+
* @default 0.75
|
|
2381
|
+
*/
|
|
2382
|
+
loadFactor?: number;
|
|
2383
|
+
/**
|
|
2384
|
+
* Equality function for keys.
|
|
2385
|
+
* @default defaultEq (===)
|
|
2386
|
+
*/
|
|
2387
|
+
keyEq?: Eq<K>;
|
|
2388
|
+
/**
|
|
2389
|
+
* Hash function for keys.
|
|
2390
|
+
* @default defaultHash
|
|
2391
|
+
*/
|
|
2392
|
+
keyHash?: Hash<K>;
|
|
2393
|
+
/**
|
|
2394
|
+
* Equality function for values (used in containsValue).
|
|
2395
|
+
* @default defaultEq (===)
|
|
2396
|
+
*/
|
|
2397
|
+
valueEq?: Eq<V>;
|
|
2398
|
+
}
|
|
2399
|
+
/**
|
|
2400
|
+
* HashMap<K, V> - Hash table implementation with custom equality/hashing.
|
|
2401
|
+
*
|
|
2402
|
+
* This class uses separate chaining for collision resolution.
|
|
2403
|
+
* Automatically resizes when load factor is exceeded.
|
|
2404
|
+
*
|
|
2405
|
+
* Not thread-safe. Use synchronized() behavior for concurrent access.
|
|
2406
|
+
*/
|
|
2407
|
+
export declare class HashMap<K, V> implements MapLike<K, V> {
|
|
2408
|
+
private _buckets;
|
|
2409
|
+
private _size;
|
|
2410
|
+
private readonly _loadFactor;
|
|
2411
|
+
private readonly _keyEq;
|
|
2412
|
+
private readonly _keyHash;
|
|
2413
|
+
private readonly _valueEq;
|
|
2414
|
+
private _threshold;
|
|
2415
|
+
/**
|
|
2416
|
+
* The count of structural changes (a key added or removed, a clear, a resize). An iterator
|
|
2417
|
+
* records it at its start and throws when it changes. Before, a resize during iteration made
|
|
2418
|
+
* the iterator skip or repeat entries (BUGS-2026-07 L15).
|
|
2419
|
+
*/
|
|
2420
|
+
private _modCount;
|
|
2421
|
+
constructor(options?: HashMapOptions<K, V>);
|
|
2422
|
+
get size(): number;
|
|
2423
|
+
get isEmpty(): boolean;
|
|
2424
|
+
get keyEq(): Eq<K>;
|
|
2425
|
+
get valueEq(): Eq<V>;
|
|
2426
|
+
has(key: K): boolean;
|
|
2427
|
+
get(key: K): V;
|
|
2428
|
+
getOrUndefined(key: K): V | undefined;
|
|
2429
|
+
getOrDefault(key: K, defaultValue: V): V;
|
|
2430
|
+
containsValue(value: V): boolean;
|
|
2431
|
+
set(key: K, value: V): V | undefined;
|
|
2432
|
+
setIfAbsent(key: K, value: V): V;
|
|
2433
|
+
delete(key: K): V | undefined;
|
|
2434
|
+
deleteEntry(key: K, value: V): boolean;
|
|
2435
|
+
replace(key: K, value: V): V | undefined;
|
|
2436
|
+
replaceEntry(key: K, oldValue: V, newValue: V): boolean;
|
|
2437
|
+
computeIfAbsent(key: K, mappingFunction: (key: K) => V): V;
|
|
2438
|
+
computeIfPresent(key: K, remappingFunction: (key: K, value: V) => V | undefined): V | undefined;
|
|
2439
|
+
compute(key: K, remappingFunction: (key: K, value: V | undefined) => V | undefined): V | undefined;
|
|
2440
|
+
merge(key: K, value: V, remappingFunction: (oldValue: V, newValue: V) => V | undefined): V | undefined;
|
|
2441
|
+
putAll(other: MapLike<K, V> | Iterable<Entry<K, V>>): void;
|
|
2442
|
+
clear(): void;
|
|
2443
|
+
keys(): IterableIterator<K>;
|
|
2444
|
+
values(): IterableIterator<V>;
|
|
2445
|
+
entries(): IterableIterator<Entry<K, V>>;
|
|
2446
|
+
[Symbol.iterator](): Iterator<Entry<K, V>>;
|
|
2447
|
+
forEach(action: (value: V, key: K, map: this) => void): void;
|
|
2448
|
+
/** This method throws when the map changed structurally after an iterator started. */
|
|
2449
|
+
private checkUnchanged;
|
|
2450
|
+
/**
|
|
2451
|
+
* This method gives the bucket index for a given hash.
|
|
2452
|
+
*/
|
|
2453
|
+
private bucketIndex;
|
|
2454
|
+
/**
|
|
2455
|
+
* This method resizes the hash table to double its current capacity.
|
|
2456
|
+
*/
|
|
2457
|
+
private resize;
|
|
2458
|
+
/**
|
|
2459
|
+
* This method gives the next power of 2 >= n.
|
|
2460
|
+
*/
|
|
2461
|
+
private nextPowerOfTwo;
|
|
2462
|
+
/**
|
|
2463
|
+
* This value is the current number of buckets.
|
|
2464
|
+
*/
|
|
2465
|
+
get capacity(): number;
|
|
2466
|
+
/**
|
|
2467
|
+
* This value is the current load factor (size / capacity).
|
|
2468
|
+
*/
|
|
2469
|
+
get loadFactor(): number;
|
|
2470
|
+
/**
|
|
2471
|
+
* This method gives statistics about bucket distribution (for debugging).
|
|
2472
|
+
*/
|
|
2473
|
+
getStats(): {
|
|
2474
|
+
size: number;
|
|
2475
|
+
capacity: number;
|
|
2476
|
+
loadFactor: number;
|
|
2477
|
+
avgChainLength: number;
|
|
2478
|
+
maxChainLength: number;
|
|
2479
|
+
emptyBuckets: number;
|
|
2480
|
+
};
|
|
2481
|
+
}
|
|
2482
|
+
/**
|
|
2483
|
+
* Factory function to make a HashMap.
|
|
2484
|
+
*
|
|
2485
|
+
* @example
|
|
2486
|
+
* const map = hashMap<string, number>()
|
|
2487
|
+
* const mapWithOptions = hashMap<User, Data>({
|
|
2488
|
+
* keyEq: (a, b) => a.id === b.id,
|
|
2489
|
+
* keyHash: (u) => hashString(u.id)
|
|
2490
|
+
* })
|
|
2491
|
+
* const mapFromEntries = hashMap([['a', 1], ['b', 2]])
|
|
2492
|
+
*/
|
|
2493
|
+
export declare function hashMap<K, V>(optionsOrEntries?: HashMapOptions<K, V> | Iterable<readonly [K, V]>): HashMap<K, V>;
|
|
2494
|
+
/**
|
|
2495
|
+
* Options for making a LinkedHashMap.
|
|
2496
|
+
*/
|
|
2497
|
+
export interface LinkedHashMapOptions<K, V> {
|
|
2498
|
+
/**
|
|
2499
|
+
* Initial capacity of the hash table.
|
|
2500
|
+
* @default 16
|
|
2501
|
+
*/
|
|
2502
|
+
initialCapacity?: number;
|
|
2503
|
+
/**
|
|
2504
|
+
* Load factor before resizing.
|
|
2505
|
+
* @default 0.75
|
|
2506
|
+
*/
|
|
2507
|
+
loadFactor?: number;
|
|
2508
|
+
/**
|
|
2509
|
+
* Equality function for keys.
|
|
2510
|
+
* @default defaultEq
|
|
2511
|
+
*/
|
|
2512
|
+
eq?: Eq<K>;
|
|
2513
|
+
/**
|
|
2514
|
+
* Hash function for keys.
|
|
2515
|
+
* @default defaultHash
|
|
2516
|
+
*/
|
|
2517
|
+
hash?: Hash<K>;
|
|
2518
|
+
/**
|
|
2519
|
+
* Equality function for values.
|
|
2520
|
+
* @default defaultEq
|
|
2521
|
+
*/
|
|
2522
|
+
valueEq?: Eq<V>;
|
|
2523
|
+
/**
|
|
2524
|
+
* If true, maintain access order instead of insertion order.
|
|
2525
|
+
* Useful for LRU caches.
|
|
2526
|
+
* @default false
|
|
2527
|
+
*/
|
|
2528
|
+
accessOrder?: boolean;
|
|
2529
|
+
}
|
|
2530
|
+
/**
|
|
2531
|
+
* LinkedHashMap<K, V> - Hash map with predictable iteration order.
|
|
2532
|
+
*
|
|
2533
|
+
* This class maintains a doubly-linked list of entries to preserve insertion order
|
|
2534
|
+
* (or access order if accessOrder is true).
|
|
2535
|
+
*/
|
|
2536
|
+
export declare class LinkedHashMap<K, V> implements MapLike<K, V> {
|
|
2537
|
+
private _buckets;
|
|
2538
|
+
private _size;
|
|
2539
|
+
private readonly _loadFactor;
|
|
2540
|
+
private readonly _eq;
|
|
2541
|
+
private readonly _hash;
|
|
2542
|
+
private readonly _valueEq;
|
|
2543
|
+
private readonly _accessOrder;
|
|
2544
|
+
private _head;
|
|
2545
|
+
private _tail;
|
|
2546
|
+
constructor(options?: LinkedHashMapOptions<K, V>);
|
|
2547
|
+
get size(): number;
|
|
2548
|
+
get isEmpty(): boolean;
|
|
2549
|
+
get eq(): Eq<K>;
|
|
2550
|
+
get keyEq(): Eq<K>;
|
|
2551
|
+
get valueEq(): Eq<V>;
|
|
2552
|
+
get hash(): Hash<K>;
|
|
2553
|
+
has(key: K): boolean;
|
|
2554
|
+
containsKey(key: K): boolean;
|
|
2555
|
+
containsValue(value: V): boolean;
|
|
2556
|
+
get(key: K): V;
|
|
2557
|
+
getOrUndefined(key: K): V | undefined;
|
|
2558
|
+
getOrDefault(key: K, defaultValue: V): V;
|
|
2559
|
+
set(key: K, value: V): V | undefined;
|
|
2560
|
+
setIfAbsent(key: K, value: V): V;
|
|
2561
|
+
replace(key: K, value: V): V | undefined;
|
|
2562
|
+
replaceEntry(key: K, oldValue: V, newValue: V): boolean;
|
|
2563
|
+
delete(key: K): V | undefined;
|
|
2564
|
+
deleteEntry(key: K, value: V): boolean;
|
|
2565
|
+
clear(): void;
|
|
2566
|
+
putAll(other: MapLike<K, V> | Iterable<Entry<K, V>>): void;
|
|
2567
|
+
computeIfAbsent(key: K, mappingFunction: (key: K) => V): V;
|
|
2568
|
+
computeIfPresent(key: K, remappingFunction: (key: K, value: V) => V | undefined): V | undefined;
|
|
2569
|
+
compute(key: K, remappingFunction: (key: K, value: V | undefined) => V | undefined): V | undefined;
|
|
2570
|
+
merge(key: K, value: V, remappingFunction: (oldValue: V, newValue: V) => V | undefined): V | undefined;
|
|
2571
|
+
keys(): IterableIterator<K>;
|
|
2572
|
+
values(): IterableIterator<V>;
|
|
2573
|
+
entries(): IterableIterator<Entry<K, V>>;
|
|
2574
|
+
[Symbol.iterator](): Iterator<Entry<K, V>>;
|
|
2575
|
+
forEach(action: (value: V, key: K, map: this) => void): void;
|
|
2576
|
+
toArray(): Entry<K, V>[];
|
|
2577
|
+
private getNode;
|
|
2578
|
+
private getBucketIndex;
|
|
2579
|
+
private removeFromBucket;
|
|
2580
|
+
private addToEnd;
|
|
2581
|
+
private removeFromList;
|
|
2582
|
+
private moveToEnd;
|
|
2583
|
+
private resize;
|
|
2584
|
+
toString(): string;
|
|
2585
|
+
}
|
|
2586
|
+
/**
|
|
2587
|
+
* Factory function to make a LinkedHashMap.
|
|
2588
|
+
*
|
|
2589
|
+
* @example
|
|
2590
|
+
* const map = linkedHashMap<string, number>()
|
|
2591
|
+
* map.set('a', 1)
|
|
2592
|
+
* map.set('b', 2)
|
|
2593
|
+
*
|
|
2594
|
+
* @example
|
|
2595
|
+
* // With initial entries
|
|
2596
|
+
* const map = linkedHashMap<string, number>([
|
|
2597
|
+
* ['a', 1],
|
|
2598
|
+
* ['b', 2]
|
|
2599
|
+
* ])
|
|
2600
|
+
*
|
|
2601
|
+
* @example
|
|
2602
|
+
* // LRU cache with access order
|
|
2603
|
+
* const lru = linkedHashMap<string, number>({ accessOrder: true })
|
|
2604
|
+
*/
|
|
2605
|
+
export declare function linkedHashMap<K, V>(optionsOrEntries?: LinkedHashMapOptions<K, V> | Iterable<[K, V]>): LinkedHashMap<K, V>;
|
|
2606
|
+
/**
|
|
2607
|
+
* Options for making a TreeMap.
|
|
2608
|
+
*/
|
|
2609
|
+
export interface TreeMapOptions<K, V> {
|
|
2610
|
+
/**
|
|
2611
|
+
* Comparison function for keys.
|
|
2612
|
+
* @default defaultCompare
|
|
2613
|
+
*/
|
|
2614
|
+
compare?: Compare<K>;
|
|
2615
|
+
/**
|
|
2616
|
+
* Equality function for keys.
|
|
2617
|
+
* @default defaultEq
|
|
2618
|
+
*/
|
|
2619
|
+
eq?: Eq<K>;
|
|
2620
|
+
/**
|
|
2621
|
+
* Equality function for values.
|
|
2622
|
+
* @default defaultEq
|
|
2623
|
+
*/
|
|
2624
|
+
valueEq?: Eq<V>;
|
|
2625
|
+
}
|
|
2626
|
+
/**
|
|
2627
|
+
* TreeMap<K, V> - Red-black tree based sorted map.
|
|
2628
|
+
*
|
|
2629
|
+
* Maintains keys in sorted order using a self-balancing binary search tree.
|
|
2630
|
+
* This class gives O(log n) operations and navigation methods.
|
|
2631
|
+
*/
|
|
2632
|
+
export declare class TreeMap<K, V> implements NavigableMap<K, V> {
|
|
2633
|
+
private _root;
|
|
2634
|
+
private _size;
|
|
2635
|
+
private readonly _compare;
|
|
2636
|
+
private readonly _eq;
|
|
2637
|
+
private readonly _valueEq;
|
|
2638
|
+
constructor(options?: TreeMapOptions<K, V>);
|
|
2639
|
+
get size(): number;
|
|
2640
|
+
get isEmpty(): boolean;
|
|
2641
|
+
get comparator(): Compare<K>;
|
|
2642
|
+
get keyEq(): Eq<K>;
|
|
2643
|
+
get valueEq(): Eq<V>;
|
|
2644
|
+
has(key: K): boolean;
|
|
2645
|
+
containsKey(key: K): boolean;
|
|
2646
|
+
containsValue(value: V): boolean;
|
|
2647
|
+
get(key: K): V;
|
|
2648
|
+
getOrUndefined(key: K): V | undefined;
|
|
2649
|
+
getOrDefault(key: K, defaultValue: V): V;
|
|
2650
|
+
set(key: K, value: V): V | undefined;
|
|
2651
|
+
setIfAbsent(key: K, value: V): V;
|
|
2652
|
+
replace(key: K, value: V): V | undefined;
|
|
2653
|
+
replaceEntry(key: K, oldValue: V, newValue: V): boolean;
|
|
2654
|
+
delete(key: K): V | undefined;
|
|
2655
|
+
deleteEntry(key: K, value: V): boolean;
|
|
2656
|
+
clear(): void;
|
|
2657
|
+
putAll(other: NavigableMap<K, V> | Iterable<Entry<K, V>>): void;
|
|
2658
|
+
computeIfAbsent(key: K, mappingFunction: (key: K) => V): V;
|
|
2659
|
+
computeIfPresent(key: K, remappingFunction: (key: K, value: V) => V | undefined): V | undefined;
|
|
2660
|
+
compute(key: K, remappingFunction: (key: K, value: V | undefined) => V | undefined): V | undefined;
|
|
2661
|
+
merge(key: K, value: V, remappingFunction: (oldValue: V, newValue: V) => V | undefined): V | undefined;
|
|
2662
|
+
firstKey(): K;
|
|
2663
|
+
lastKey(): K;
|
|
2664
|
+
headMap(toKey: K): SortedMap<K, V>;
|
|
2665
|
+
tailMap(fromKey: K): SortedMap<K, V>;
|
|
2666
|
+
subMap(fromKey: K, toKey: K): SortedMap<K, V>;
|
|
2667
|
+
floorKey(key: K): K | undefined;
|
|
2668
|
+
ceilingKey(key: K): K | undefined;
|
|
2669
|
+
lowerKey(key: K): K | undefined;
|
|
2670
|
+
higherKey(key: K): K | undefined;
|
|
2671
|
+
floorEntry(key: K): Entry<K, V> | undefined;
|
|
2672
|
+
ceilingEntry(key: K): Entry<K, V> | undefined;
|
|
2673
|
+
lowerEntry(key: K): Entry<K, V> | undefined;
|
|
2674
|
+
higherEntry(key: K): Entry<K, V> | undefined;
|
|
2675
|
+
firstEntry(): Entry<K, V> | undefined;
|
|
2676
|
+
lastEntry(): Entry<K, V> | undefined;
|
|
2677
|
+
pollFirstEntry(): Entry<K, V> | undefined;
|
|
2678
|
+
pollLastEntry(): Entry<K, V> | undefined;
|
|
2679
|
+
descendingMap(): NavigableMap<K, V>;
|
|
2680
|
+
descendingKeys(): IterableIterator<K>;
|
|
2681
|
+
keys(): IterableIterator<K>;
|
|
2682
|
+
values(): IterableIterator<V>;
|
|
2683
|
+
entries(): IterableIterator<Entry<K, V>>;
|
|
2684
|
+
[Symbol.iterator](): Iterator<Entry<K, V>>;
|
|
2685
|
+
forEach(action: (value: V, key: K, map: this) => void): void;
|
|
2686
|
+
toArray(): Entry<K, V>[];
|
|
2687
|
+
private createNode;
|
|
2688
|
+
private getNode;
|
|
2689
|
+
private getFirstNode;
|
|
2690
|
+
private getLastNode;
|
|
2691
|
+
private findValue;
|
|
2692
|
+
private inOrderTraversal;
|
|
2693
|
+
private reverseInOrderTraversal;
|
|
2694
|
+
private rotateLeft;
|
|
2695
|
+
private rotateRight;
|
|
2696
|
+
private fixAfterInsertion;
|
|
2697
|
+
/**
|
|
2698
|
+
* This method deletes a node (the CLRS algorithm, as in java.util.TreeMap). A null leaf counts as black.
|
|
2699
|
+
*
|
|
2700
|
+
* Before (BUGS-2026-07 L10): the deletion of a black leaf skipped the fix-up, so the black
|
|
2701
|
+
* heights drifted and the tree became unbalanced. Also, the fix-up treated a null leaf as
|
|
2702
|
+
* "not black", so it chose the wrong case and rotated at null (a TypeError).
|
|
2703
|
+
*/
|
|
2704
|
+
private deleteNode;
|
|
2705
|
+
private fixAfterDeletion;
|
|
2706
|
+
toString(): string;
|
|
2707
|
+
}
|
|
2708
|
+
/**
|
|
2709
|
+
* Factory function to make a TreeMap.
|
|
2710
|
+
*
|
|
2711
|
+
* @example
|
|
2712
|
+
* const map = treeMap<string, number>()
|
|
2713
|
+
* map.set('c', 3)
|
|
2714
|
+
* map.set('a', 1)
|
|
2715
|
+
* map.set('b', 2)
|
|
2716
|
+
* // Iteration order: a, b, c
|
|
2717
|
+
*
|
|
2718
|
+
* @example
|
|
2719
|
+
* // With initial entries
|
|
2720
|
+
* const map = treeMap<string, number>([
|
|
2721
|
+
* ['c', 3],
|
|
2722
|
+
* ['a', 1],
|
|
2723
|
+
* ['b', 2]
|
|
2724
|
+
* ])
|
|
2725
|
+
*
|
|
2726
|
+
* @example
|
|
2727
|
+
* // Custom comparator for descending order
|
|
2728
|
+
* const map = treeMap<number, string>({ compare: (a, b) => b - a })
|
|
2729
|
+
*/
|
|
2730
|
+
export declare function treeMap<K, V>(optionsOrEntries?: TreeMapOptions<K, V> | Iterable<[K, V]>): TreeMap<K, V>;
|
|
2731
|
+
/**
|
|
2732
|
+
* Options for making a TreeSet.
|
|
2733
|
+
*/
|
|
2734
|
+
export interface TreeSetOptions<T> {
|
|
2735
|
+
/**
|
|
2736
|
+
* Comparison function for elements.
|
|
2737
|
+
* @default defaultCompare
|
|
2738
|
+
*/
|
|
2739
|
+
compare?: Compare<T>;
|
|
2740
|
+
/**
|
|
2741
|
+
* Equality function for elements.
|
|
2742
|
+
* @default defaultEq
|
|
2743
|
+
*/
|
|
2744
|
+
eq?: Eq<T>;
|
|
2745
|
+
}
|
|
2746
|
+
/**
|
|
2747
|
+
* TreeSet<T> - Red-black tree based sorted set.
|
|
2748
|
+
*
|
|
2749
|
+
* Maintains elements in sorted order using a TreeMap as backing structure.
|
|
2750
|
+
* This class gives O(log n) operations and navigation methods.
|
|
2751
|
+
*/
|
|
2752
|
+
export declare class TreeSet<T> implements NavigableSet<T> {
|
|
2753
|
+
private readonly _map;
|
|
2754
|
+
private readonly _compare;
|
|
2755
|
+
private readonly _eq;
|
|
2756
|
+
constructor(options?: TreeSetOptions<T>);
|
|
2757
|
+
get size(): number;
|
|
2758
|
+
get isEmpty(): boolean;
|
|
2759
|
+
get comparator(): Compare<T>;
|
|
2760
|
+
get eq(): Eq<T>;
|
|
2761
|
+
has(element: T): boolean;
|
|
2762
|
+
contains(element: T): boolean;
|
|
2763
|
+
containsAll(other: Iterable<T>): boolean;
|
|
2764
|
+
add(element: T): boolean;
|
|
2765
|
+
remove(element: T): boolean;
|
|
2766
|
+
clear(): void;
|
|
2767
|
+
addAll(elements: Iterable<T>): boolean;
|
|
2768
|
+
removeAll(elements: Iterable<T>): boolean;
|
|
2769
|
+
retainAll(elements: Iterable<T>): boolean;
|
|
2770
|
+
union(other: Iterable<T>): TreeSet<T>;
|
|
2771
|
+
intersection(other: Iterable<T>): TreeSet<T>;
|
|
2772
|
+
difference(other: Iterable<T>): TreeSet<T>;
|
|
2773
|
+
symmetricDifference(other: Iterable<T>): TreeSet<T>;
|
|
2774
|
+
isSubsetOf(other: Iterable<T>): boolean;
|
|
2775
|
+
isSupersetOf(other: Iterable<T>): boolean;
|
|
2776
|
+
isDisjointFrom(other: Iterable<T>): boolean;
|
|
2777
|
+
first(): T;
|
|
2778
|
+
last(): T;
|
|
2779
|
+
headSet(toElement: T): SortedSet<T>;
|
|
2780
|
+
tailSet(fromElement: T): SortedSet<T>;
|
|
2781
|
+
subSet(fromElement: T, toElement: T): SortedSet<T>;
|
|
2782
|
+
floor(element: T): T | undefined;
|
|
2783
|
+
ceiling(element: T): T | undefined;
|
|
2784
|
+
lower(element: T): T | undefined;
|
|
2785
|
+
higher(element: T): T | undefined;
|
|
2786
|
+
pollFirst(): T | undefined;
|
|
2787
|
+
pollLast(): T | undefined;
|
|
2788
|
+
descendingSet(): NavigableSet<T>;
|
|
2789
|
+
descendingIterator(): IterableIterator<T>;
|
|
2790
|
+
[Symbol.iterator](): Iterator<T>;
|
|
2791
|
+
forEach(action: (element: T, index: number) => void): void;
|
|
2792
|
+
toArray(): T[];
|
|
2793
|
+
toString(): string;
|
|
2794
|
+
}
|
|
2795
|
+
/**
|
|
2796
|
+
* Factory function to make a TreeSet.
|
|
2797
|
+
*
|
|
2798
|
+
* @example
|
|
2799
|
+
* const set = treeSet<string>()
|
|
2800
|
+
* set.add('c')
|
|
2801
|
+
* set.add('a')
|
|
2802
|
+
* set.add('b')
|
|
2803
|
+
* // Iteration order: a, b, c
|
|
2804
|
+
*
|
|
2805
|
+
* @example
|
|
2806
|
+
* // With initial elements
|
|
2807
|
+
* const set = treeSet(['c', 'a', 'b'])
|
|
2808
|
+
* // Iteration order: a, b, c
|
|
2809
|
+
*
|
|
2810
|
+
* @example
|
|
2811
|
+
* // Custom comparator for descending order
|
|
2812
|
+
* const set = treeSet<number>({ compare: (a, b) => b - a })
|
|
2813
|
+
*/
|
|
2814
|
+
export declare function treeSet<T>(optionsOrElements?: TreeSetOptions<T> | Iterable<T>): TreeSet<T>;
|
|
2815
|
+
/**
|
|
2816
|
+
* Options for making a LinkedList.
|
|
2817
|
+
*/
|
|
2818
|
+
export interface LinkedListOptions<T> {
|
|
2819
|
+
/**
|
|
2820
|
+
* Equality function for comparing elements.
|
|
2821
|
+
* @default defaultEq (===)
|
|
2822
|
+
*/
|
|
2823
|
+
eq?: Eq<T>;
|
|
2824
|
+
}
|
|
2825
|
+
/**
|
|
2826
|
+
* LinkedList<T> - Doubly-linked list implementation.
|
|
2827
|
+
*
|
|
2828
|
+
* Efficient for:
|
|
2829
|
+
* - Insertion/removal at ends: O(1)
|
|
2830
|
+
* - Insertion/removal at known position: O(1)
|
|
2831
|
+
* - Sequential iteration: O(n)
|
|
2832
|
+
*
|
|
2833
|
+
* Not efficient for:
|
|
2834
|
+
* - Random access by index: O(n)
|
|
2835
|
+
* - Search: O(n)
|
|
2836
|
+
*
|
|
2837
|
+
* This class implements both List and Deque interfaces.
|
|
2838
|
+
*/
|
|
2839
|
+
export declare class LinkedList<T> implements List<T>, Deque<T> {
|
|
2840
|
+
private _head;
|
|
2841
|
+
private _tail;
|
|
2842
|
+
private _size;
|
|
2843
|
+
private readonly _eq;
|
|
2844
|
+
constructor(options?: LinkedListOptions<T>);
|
|
2845
|
+
get size(): number;
|
|
2846
|
+
get isEmpty(): boolean;
|
|
2847
|
+
get eq(): Eq<T>;
|
|
2848
|
+
addFirst(element: T): void;
|
|
2849
|
+
addLast(element: T): void;
|
|
2850
|
+
removeFirst(): T;
|
|
2851
|
+
removeLast(): T;
|
|
2852
|
+
pollFirst(): T | undefined;
|
|
2853
|
+
pollLast(): T | undefined;
|
|
2854
|
+
peekFirst(): T;
|
|
2855
|
+
peekLast(): T;
|
|
2856
|
+
peekFirstOrUndefined(): T | undefined;
|
|
2857
|
+
peekLastOrUndefined(): T | undefined;
|
|
2858
|
+
offerFirst(element: T): boolean;
|
|
2859
|
+
offerLast(element: T): boolean;
|
|
2860
|
+
push(element: T): void;
|
|
2861
|
+
pop(): T;
|
|
2862
|
+
offer(element: T): boolean;
|
|
2863
|
+
poll(): T;
|
|
2864
|
+
peek(): T;
|
|
2865
|
+
peekOrUndefined(): T | undefined;
|
|
2866
|
+
pollOrUndefined(): T | undefined;
|
|
2867
|
+
enqueue(element: T): boolean;
|
|
2868
|
+
dequeue(): T;
|
|
2869
|
+
get(index: number): T;
|
|
2870
|
+
set(index: number, element: T): T;
|
|
2871
|
+
insert(index: number, element: T): void;
|
|
2872
|
+
insertAll(index: number, other: Iterable<T>): boolean;
|
|
2873
|
+
removeAt(index: number): T;
|
|
2874
|
+
removeRange(fromIndex: number, toIndex: number): void;
|
|
2875
|
+
indexOf(element: T, fromIndex?: number): number;
|
|
2876
|
+
lastIndexOf(element: T, fromIndex?: number): number;
|
|
2877
|
+
first(): T;
|
|
2878
|
+
last(): T;
|
|
2879
|
+
shift(): T;
|
|
2880
|
+
unshift(element: T): void;
|
|
2881
|
+
add(element: T): boolean;
|
|
2882
|
+
addAll(other: Iterable<T>): boolean;
|
|
2883
|
+
contains(element: T): boolean;
|
|
2884
|
+
containsAll(other: Iterable<T>): boolean;
|
|
2885
|
+
remove(element: T): boolean;
|
|
2886
|
+
removeAll(other: Iterable<T>): boolean;
|
|
2887
|
+
retainAll(other: Iterable<T>): boolean;
|
|
2888
|
+
clear(): void;
|
|
2889
|
+
sort(compare?: Compare<T>): void;
|
|
2890
|
+
reverse(): void;
|
|
2891
|
+
subList(fromIndex: number, toIndex: number): List<T>;
|
|
2892
|
+
[Symbol.iterator](): Iterator<T>;
|
|
2893
|
+
forEach(action: (element: T, index: number) => void): void;
|
|
2894
|
+
toArray(): T[];
|
|
2895
|
+
private getNode;
|
|
2896
|
+
private unlinkNode;
|
|
2897
|
+
toString(): string;
|
|
2898
|
+
}
|
|
2899
|
+
/**
|
|
2900
|
+
* Factory function to make a LinkedList.
|
|
2901
|
+
*
|
|
2902
|
+
* @example
|
|
2903
|
+
* const list = linkedList<number>()
|
|
2904
|
+
* const listFromArray = linkedList([1, 2, 3])
|
|
2905
|
+
*/
|
|
2906
|
+
export declare function linkedList<T>(optionsOrElements?: LinkedListOptions<T> | Iterable<T>): LinkedList<T>;
|
|
2907
|
+
/**
|
|
2908
|
+
* Options for making a HashSet.
|
|
2909
|
+
*/
|
|
2910
|
+
export interface HashSetOptions<T> {
|
|
2911
|
+
/**
|
|
2912
|
+
* Initial capacity.
|
|
2913
|
+
* @default 16
|
|
2914
|
+
*/
|
|
2915
|
+
initialCapacity?: number;
|
|
2916
|
+
/**
|
|
2917
|
+
* Load factor threshold for resizing.
|
|
2918
|
+
* @default 0.75
|
|
2919
|
+
*/
|
|
2920
|
+
loadFactor?: number;
|
|
2921
|
+
/**
|
|
2922
|
+
* Equality function for elements.
|
|
2923
|
+
* @default defaultEq (===)
|
|
2924
|
+
*/
|
|
2925
|
+
eq?: Eq<T>;
|
|
2926
|
+
/**
|
|
2927
|
+
* Hash function for elements.
|
|
2928
|
+
* @default defaultHash
|
|
2929
|
+
*/
|
|
2930
|
+
hash?: Hash<T>;
|
|
2931
|
+
}
|
|
2932
|
+
/**
|
|
2933
|
+
* HashSet<T> - Hash table implementation of the Set interface.
|
|
2934
|
+
*
|
|
2935
|
+
* A HashMap holds the elements. Its values are a dummy constant.
|
|
2936
|
+
* This class gives constant-time performance for basic operations.
|
|
2937
|
+
*
|
|
2938
|
+
* Not thread-safe. Use synchronized() behavior for concurrent access.
|
|
2939
|
+
*/
|
|
2940
|
+
export declare class HashSet<T> implements Set$1<T> {
|
|
2941
|
+
private readonly _map;
|
|
2942
|
+
private readonly _eq;
|
|
2943
|
+
constructor(options?: HashSetOptions<T>);
|
|
2944
|
+
get size(): number;
|
|
2945
|
+
get isEmpty(): boolean;
|
|
2946
|
+
get eq(): Eq<T>;
|
|
2947
|
+
add(element: T): boolean;
|
|
2948
|
+
remove(element: T): boolean;
|
|
2949
|
+
contains(element: T): boolean;
|
|
2950
|
+
addAll(other: Iterable<T>): boolean;
|
|
2951
|
+
removeAll(other: Iterable<T>): boolean;
|
|
2952
|
+
retainAll(other: Iterable<T>): boolean;
|
|
2953
|
+
containsAll(other: Iterable<T>): boolean;
|
|
2954
|
+
clear(): void;
|
|
2955
|
+
/**
|
|
2956
|
+
* This method gives a new set that is the union of this set and the other set.
|
|
2957
|
+
* The new set contains all elements from both sets.
|
|
2958
|
+
*/
|
|
2959
|
+
union(other: Iterable<T>): HashSet<T>;
|
|
2960
|
+
/**
|
|
2961
|
+
* This method gives a new set that is the intersection of this set and the other set.
|
|
2962
|
+
* The new set contains only elements present in both sets.
|
|
2963
|
+
*/
|
|
2964
|
+
intersection(other: Iterable<T>): HashSet<T>;
|
|
2965
|
+
/**
|
|
2966
|
+
* This method gives a new set that is the difference of this set and the other set.
|
|
2967
|
+
* The new set contains elements in this set but not in the other set.
|
|
2968
|
+
*/
|
|
2969
|
+
difference(other: Iterable<T>): HashSet<T>;
|
|
2970
|
+
/**
|
|
2971
|
+
* This method gives a new set that is the symmetric difference of this set and the other set.
|
|
2972
|
+
* The new set contains elements in either set but not in both.
|
|
2973
|
+
*/
|
|
2974
|
+
symmetricDifference(other: Iterable<T>): HashSet<T>;
|
|
2975
|
+
/**
|
|
2976
|
+
* This method gives true if this set is a subset of the other set.
|
|
2977
|
+
* All elements in this set are also in the other set.
|
|
2978
|
+
*/
|
|
2979
|
+
isSubsetOf(other: Iterable<T>): boolean;
|
|
2980
|
+
/**
|
|
2981
|
+
* This method gives true if this set is a superset of the other set.
|
|
2982
|
+
* All elements in the other set are also in this set.
|
|
2983
|
+
*/
|
|
2984
|
+
isSupersetOf(other: Iterable<T>): boolean;
|
|
2985
|
+
/**
|
|
2986
|
+
* This method gives true if this set and the other set have no elements in common.
|
|
2987
|
+
*/
|
|
2988
|
+
isDisjointFrom(other: Iterable<T>): boolean;
|
|
2989
|
+
[Symbol.iterator](): Iterator<T>;
|
|
2990
|
+
forEach(action: (element: T, index: number) => void): void;
|
|
2991
|
+
toArray(): T[];
|
|
2992
|
+
/**
|
|
2993
|
+
* This method makes a shallow copy of this set.
|
|
2994
|
+
*/
|
|
2995
|
+
clone(): HashSet<T>;
|
|
2996
|
+
/**
|
|
2997
|
+
* This method gives statistics about the underlying hash table.
|
|
2998
|
+
*/
|
|
2999
|
+
getStats(): ReturnType<HashMap<T, symbol>["getStats"]>;
|
|
3000
|
+
/**
|
|
3001
|
+
* This method gives a string representation for debugging.
|
|
3002
|
+
*/
|
|
3003
|
+
toString(): string;
|
|
3004
|
+
}
|
|
3005
|
+
/**
|
|
3006
|
+
* Factory function to make a HashSet.
|
|
3007
|
+
*
|
|
3008
|
+
* @example
|
|
3009
|
+
* const set = hashSet<number>()
|
|
3010
|
+
* const setFromArray = hashSet([1, 2, 3, 2, 1]) // {1, 2, 3}
|
|
3011
|
+
* const setWithOptions = hashSet<string>({ initialCapacity: 32 })
|
|
3012
|
+
*/
|
|
3013
|
+
export declare function hashSet<T>(optionsOrElements?: HashSetOptions<T> | Iterable<T>): HashSet<T>;
|
|
3014
|
+
/**
|
|
3015
|
+
* Options for making a PriorityQueue.
|
|
3016
|
+
*/
|
|
3017
|
+
export interface PriorityQueueOptions<T> {
|
|
3018
|
+
/**
|
|
3019
|
+
* Initial capacity of the heap.
|
|
3020
|
+
* @default 11
|
|
3021
|
+
*/
|
|
3022
|
+
initialCapacity?: number;
|
|
3023
|
+
/**
|
|
3024
|
+
* Comparator for ordering elements.
|
|
3025
|
+
* Lower values have higher priority (min heap).
|
|
3026
|
+
* @default defaultCompare
|
|
3027
|
+
*/
|
|
3028
|
+
compare?: Compare<T>;
|
|
3029
|
+
/**
|
|
3030
|
+
* Equality function for elements.
|
|
3031
|
+
* @default defaultEq
|
|
3032
|
+
*/
|
|
3033
|
+
eq?: Eq<T>;
|
|
3034
|
+
}
|
|
3035
|
+
/**
|
|
3036
|
+
* PriorityQueue<T> - Binary heap implementation of priority queue.
|
|
3037
|
+
*
|
|
3038
|
+
* Elements are always retrieved in priority order (according to comparator).
|
|
3039
|
+
* Not a FIFO queue - order depends on priority, not insertion order.
|
|
3040
|
+
*
|
|
3041
|
+
* The heap is a min heap by default (smallest element has highest priority).
|
|
3042
|
+
* For max heap, use reversed comparator: (a, b) => compare(b, a)
|
|
3043
|
+
*
|
|
3044
|
+
* Not thread-safe. Use synchronized() behavior for concurrent access.
|
|
3045
|
+
*/
|
|
3046
|
+
declare class PriorityQueue$1<T> implements PriorityQueue<T> {
|
|
3047
|
+
private _heap;
|
|
3048
|
+
private readonly _compare;
|
|
3049
|
+
private readonly _eq;
|
|
3050
|
+
constructor(options?: PriorityQueueOptions<T>);
|
|
3051
|
+
get size(): number;
|
|
3052
|
+
get isEmpty(): boolean;
|
|
3053
|
+
get eq(): Eq<T>;
|
|
3054
|
+
get compare(): Compare<T>;
|
|
3055
|
+
get comparator(): Compare<T>;
|
|
3056
|
+
offer(element: T): boolean;
|
|
3057
|
+
poll(): T;
|
|
3058
|
+
pollOrUndefined(): T | undefined;
|
|
3059
|
+
peek(): T;
|
|
3060
|
+
peekOrUndefined(): T | undefined;
|
|
3061
|
+
enqueue(element: T): boolean;
|
|
3062
|
+
dequeue(): T;
|
|
3063
|
+
add(element: T): boolean;
|
|
3064
|
+
addAll(other: Iterable<T>): boolean;
|
|
3065
|
+
contains(element: T): boolean;
|
|
3066
|
+
containsAll(other: Iterable<T>): boolean;
|
|
3067
|
+
remove(element: T): boolean;
|
|
3068
|
+
removeAll(other: Iterable<T>): boolean;
|
|
3069
|
+
retainAll(other: Iterable<T>): boolean;
|
|
3070
|
+
clear(): void;
|
|
3071
|
+
/**
|
|
3072
|
+
* Iterator gives elements in arbitrary order (not priority order).
|
|
3073
|
+
* To get elements in priority order, repeatedly call poll().
|
|
3074
|
+
*/
|
|
3075
|
+
[Symbol.iterator](): Iterator<T>;
|
|
3076
|
+
forEach(action: (element: T, index: number) => void): void;
|
|
3077
|
+
/**
|
|
3078
|
+
* This method gives elements in arbitrary heap order (not sorted).
|
|
3079
|
+
*/
|
|
3080
|
+
toArray(): T[];
|
|
3081
|
+
/**
|
|
3082
|
+
* This method gives elements in priority order (sorted).
|
|
3083
|
+
* This is a destructive operation that drains the queue.
|
|
3084
|
+
*/
|
|
3085
|
+
toSortedArray(): T[];
|
|
3086
|
+
/**
|
|
3087
|
+
* This method bubbles an element up the heap to maintain heap property.
|
|
3088
|
+
*/
|
|
3089
|
+
private bubbleUp;
|
|
3090
|
+
/**
|
|
3091
|
+
* This method bubbles an element down the heap to maintain heap property.
|
|
3092
|
+
*
|
|
3093
|
+
* The element moves down through a hole: a smaller child moves up into the hole, and the
|
|
3094
|
+
* element goes into the last hole. The children are compared with the element.
|
|
3095
|
+
* Before, they were compared with `_heap[index]`. After the first step, that value is a copy of
|
|
3096
|
+
* the child that moved up. Thus a poll could leave a larger element above a smaller one.
|
|
3097
|
+
*/
|
|
3098
|
+
private bubbleDown;
|
|
3099
|
+
/**
|
|
3100
|
+
* This method rebuilds the heap from an arbitrary array.
|
|
3101
|
+
* This method uses Floyd's algorithm (O(n) time).
|
|
3102
|
+
*/
|
|
3103
|
+
private heapify;
|
|
3104
|
+
/**
|
|
3105
|
+
* This method makes a shallow copy of this priority queue.
|
|
3106
|
+
*/
|
|
3107
|
+
clone(): PriorityQueue$1<T>;
|
|
3108
|
+
/**
|
|
3109
|
+
* This method validates the heap property (for debugging).
|
|
3110
|
+
* This method gives true if the heap is valid.
|
|
3111
|
+
*/
|
|
3112
|
+
validate(): boolean;
|
|
3113
|
+
/**
|
|
3114
|
+
* This method gives a string representation for debugging.
|
|
3115
|
+
*/
|
|
3116
|
+
toString(): string;
|
|
3117
|
+
}
|
|
3118
|
+
/**
|
|
3119
|
+
* Factory function to make a PriorityQueue.
|
|
3120
|
+
*
|
|
3121
|
+
* @example
|
|
3122
|
+
* // Min heap (default)
|
|
3123
|
+
* const minHeap = priorityQueue<number>()
|
|
3124
|
+
*
|
|
3125
|
+
* // Max heap
|
|
3126
|
+
* const maxHeap = priorityQueue<number>((a, b) => b - a)
|
|
3127
|
+
*
|
|
3128
|
+
* // From array
|
|
3129
|
+
* const pq = priorityQueue([5, 2, 8, 1, 9])
|
|
3130
|
+
*
|
|
3131
|
+
* // With custom comparator
|
|
3132
|
+
* const pq = priorityQueue<Task>((a, b) => a.priority - b.priority)
|
|
3133
|
+
*/
|
|
3134
|
+
export declare function priorityQueue<T>(optionsOrElementsOrComparator?: PriorityQueueOptions<T> | Iterable<T> | Compare<T>): PriorityQueue$1<T>;
|
|
3135
|
+
/**
|
|
3136
|
+
* SummaryTree: a persistent sequence of leaves with a summary of each part.
|
|
3137
|
+
*
|
|
3138
|
+
* The tree is a balanced binary tree (AVL) with one leaf in each node. Each node keeps the summary of its subtree.
|
|
3139
|
+
* A summary is a value of a monoid: the caller gives the monoid as data, with `empty` and `combine`. The order of the
|
|
3140
|
+
* leaves is kept, so `combine` must be associative, but it can be non-commutative.
|
|
3141
|
+
*
|
|
3142
|
+
* Each operation gives a new tree and keeps the old tree. The two trees share the nodes that did not change.
|
|
3143
|
+
* Concatenation, a split at an index or at a summary search, an edit and a lookup are O(log n).
|
|
3144
|
+
*
|
|
3145
|
+
* The algorithms are the "join" algorithms of Blelloch, Ferizovic and Sun, "Just Join for Parallel Ordered Sets"
|
|
3146
|
+
* (SPAA 2016).
|
|
3147
|
+
*
|
|
3148
|
+
* @example
|
|
3149
|
+
* // The number of leaves and the total length of the strings
|
|
3150
|
+
* const spec = {
|
|
3151
|
+
* monoid: { empty: { count: 0, length: 0 }, combine: (a, b) => ({ count: a.count + b.count, length: a.length + b.length }) },
|
|
3152
|
+
* measure: (leaf: string) => ({ count: 1, length: leaf.length }),
|
|
3153
|
+
* };
|
|
3154
|
+
* const tree = summaryTree(spec, ["ab", "cde", "f"]);
|
|
3155
|
+
* tree.summary; // { count: 3, length: 6 }
|
|
3156
|
+
* const [left, right] = tree.split((prefix) => prefix.length > 2); // left: ["ab"], right: ["cde", "f"]
|
|
3157
|
+
*/
|
|
3158
|
+
/** A monoid as data: the identity value `empty` and an associative operation `combine`. */
|
|
3159
|
+
interface SummaryMonoid<Summary> {
|
|
3160
|
+
/** The summary of a sequence with no leaf. `combine(empty, s)` and `combine(s, empty)` are equal to `s`. */
|
|
3161
|
+
readonly empty: Summary;
|
|
3162
|
+
/** This function gives the summary of a sequence from the summaries of its two parts, left first. */
|
|
3163
|
+
readonly combine: (left: Summary, right: Summary) => Summary;
|
|
3164
|
+
}
|
|
3165
|
+
/** The data that a tree needs: the monoid of the summaries and the summary of one leaf. */
|
|
3166
|
+
interface SummarySpec<Leaf, Summary> {
|
|
3167
|
+
readonly monoid: SummaryMonoid<Summary>;
|
|
3168
|
+
/** This function gives the summary of one leaf. */
|
|
3169
|
+
readonly measure: (leaf: Leaf) => Summary;
|
|
3170
|
+
/**
|
|
3171
|
+
* This optional function joins two neighbor leaves at a seam into one leaf, or gives `undefined` to keep both. A seam
|
|
3172
|
+
* is the place where `concat`, `append`, `prepend`, `splice`, `insert` or `remove` puts two leaves next to each other.
|
|
3173
|
+
* The tree calls it once at each seam, and not between the leaves that one call inserts. `set` does not call it.
|
|
3174
|
+
*/
|
|
3175
|
+
readonly merge?: (left: Leaf, right: Leaf) => Leaf | undefined;
|
|
3176
|
+
}
|
|
3177
|
+
/** The result of a summary search: the first leaf whose prefix summary satisfies the predicate. */
|
|
3178
|
+
interface SummarySearch<Leaf, Summary> {
|
|
3179
|
+
/** The index of the leaf. */
|
|
3180
|
+
readonly index: number;
|
|
3181
|
+
readonly leaf: Leaf;
|
|
3182
|
+
/** The summary of the leaves before the leaf. */
|
|
3183
|
+
readonly before: Summary;
|
|
3184
|
+
/** The summary of the leaves before the leaf, and of the leaf. */
|
|
3185
|
+
readonly through: Summary;
|
|
3186
|
+
}
|
|
3187
|
+
/** The result of `locate`: the leaf that holds a position of an additive key, and the position in that leaf. */
|
|
3188
|
+
interface SummaryLocation<Leaf> {
|
|
3189
|
+
/** The index of the leaf. */
|
|
3190
|
+
readonly index: number;
|
|
3191
|
+
readonly leaf: Leaf;
|
|
3192
|
+
/** The position in the leaf: the target minus the key of the leaves before the leaf. */
|
|
3193
|
+
readonly offset: number;
|
|
3194
|
+
}
|
|
3195
|
+
/**
|
|
3196
|
+
* A persistent sequence of leaves with a summary of each part. Make one with `summaryTree`. Each method that changes
|
|
3197
|
+
* the sequence gives a new tree.
|
|
3198
|
+
*/
|
|
3199
|
+
export declare class SummaryTree<Leaf, Summary> implements Iterable<Leaf> {
|
|
3200
|
+
private readonly builder;
|
|
3201
|
+
private readonly root;
|
|
3202
|
+
private constructor();
|
|
3203
|
+
/** This function makes a tree of a spec from leaves, as `summaryTree` does. */
|
|
3204
|
+
static of<Leaf, Summary>(spec: SummarySpec<Leaf, Summary>, leaves?: Iterable<Leaf>): SummaryTree<Leaf, Summary>;
|
|
3205
|
+
private wrap;
|
|
3206
|
+
private treeOf;
|
|
3207
|
+
/** The spec of the tree: its monoid and its measure. */
|
|
3208
|
+
get spec(): SummarySpec<Leaf, Summary>;
|
|
3209
|
+
/** The number of leaves. */
|
|
3210
|
+
get size(): number;
|
|
3211
|
+
get isEmpty(): boolean;
|
|
3212
|
+
/** The summary of all the leaves, in their order. An empty tree has the summary `monoid.empty`. */
|
|
3213
|
+
get summary(): Summary;
|
|
3214
|
+
/** The height of the tree: 0 for an empty tree. The height is at most 1.45 log2(size + 2). */
|
|
3215
|
+
get height(): number;
|
|
3216
|
+
/**
|
|
3217
|
+
* This function gives the leaf at an index, or `undefined` when the index is not in the tree. A negative index counts
|
|
3218
|
+
* from the end, as `Array.prototype.at` does.
|
|
3219
|
+
*/
|
|
3220
|
+
at(index: number): Leaf | undefined;
|
|
3221
|
+
/** This function gives the summary of the leaves from `start` to `end`, with `end` not included. */
|
|
3222
|
+
summaryOf(start?: number, end?: number): Summary;
|
|
3223
|
+
/** This function gives the tree of the leaves of this tree, then the leaves of `other`. The two trees must have the same spec. */
|
|
3224
|
+
concat(other: SummaryTree<Leaf, Summary> | Iterable<Leaf>): SummaryTree<Leaf, Summary>;
|
|
3225
|
+
/** This function splits the tree into its first `index` leaves and the other leaves. */
|
|
3226
|
+
splitAt(index: number): [SummaryTree<Leaf, Summary>, SummaryTree<Leaf, Summary>];
|
|
3227
|
+
/**
|
|
3228
|
+
* This function splits the tree before the first leaf whose prefix summary satisfies the predicate. The prefix of a
|
|
3229
|
+
* leaf includes the leaf. The predicate must be monotone: after it is true for a prefix, it is true for each longer
|
|
3230
|
+
* prefix. When no prefix satisfies it, the right tree is empty.
|
|
3231
|
+
*/
|
|
3232
|
+
split(predicate: (prefix: Summary) => boolean): [SummaryTree<Leaf, Summary>, SummaryTree<Leaf, Summary>];
|
|
3233
|
+
/**
|
|
3234
|
+
* This function finds the first leaf whose prefix summary satisfies a monotone predicate, as `split` does. It changes
|
|
3235
|
+
* nothing. It gives `undefined` when no prefix satisfies the predicate.
|
|
3236
|
+
*/
|
|
3237
|
+
search(predicate: (prefix: Summary) => boolean): SummarySearch<Leaf, Summary> | undefined;
|
|
3238
|
+
/**
|
|
3239
|
+
* This function finds the leaf that holds a position of an additive key, and it calls no `combine`. The key must be
|
|
3240
|
+
* additive and not negative: `key(combine(a, b))` is `key(a) + key(b)`. The function descends by the key of each left
|
|
3241
|
+
* subtree and of each leaf, and subtracts it from the target. It gives the first leaf whose prefix key is more than
|
|
3242
|
+
* the target, or `undefined` when the target is not in `[0, key(summary))`.
|
|
3243
|
+
*
|
|
3244
|
+
* @example
|
|
3245
|
+
* // The leaf and the offset in it of the code unit at position 1000 of a rope
|
|
3246
|
+
* const at = rope.locate((s) => s.length, 1000);
|
|
3247
|
+
*/
|
|
3248
|
+
locate(key: (summary: Summary) => number, target: number): SummaryLocation<Leaf> | undefined;
|
|
3249
|
+
/** This function gives the tree of the leaves from `start` to `end`, with `end` not included. A negative index counts from the end. */
|
|
3250
|
+
slice(start?: number, end?: number): SummaryTree<Leaf, Summary>;
|
|
3251
|
+
/**
|
|
3252
|
+
* This function removes `deleteCount` leaves at `start` and puts the leaves of `inserted` there, as
|
|
3253
|
+
* `Array.prototype.splice` does. It gives the new tree, not the removed leaves.
|
|
3254
|
+
*/
|
|
3255
|
+
splice(start: number, deleteCount?: number, inserted?: SummaryTree<Leaf, Summary> | Iterable<Leaf>): SummaryTree<Leaf, Summary>;
|
|
3256
|
+
/** This function puts the leaves of `inserted` before the leaf at `index`. */
|
|
3257
|
+
insert(index: number, inserted: SummaryTree<Leaf, Summary> | Iterable<Leaf>): SummaryTree<Leaf, Summary>;
|
|
3258
|
+
/** This function removes the leaves from `start` to `end`, with `end` not included. */
|
|
3259
|
+
remove(start: number, end?: number): SummaryTree<Leaf, Summary>;
|
|
3260
|
+
/**
|
|
3261
|
+
* This function replaces the leaf at an index. A negative index counts from the end, as `Array.prototype.with` does.
|
|
3262
|
+
* An index that is not in the tree changes nothing. It copies only the path to the leaf, and it does not call
|
|
3263
|
+
* `spec.merge`.
|
|
3264
|
+
*/
|
|
3265
|
+
set(index: number, leaf: Leaf): SummaryTree<Leaf, Summary>;
|
|
3266
|
+
/** This function adds leaves after the last leaf. */
|
|
3267
|
+
append(...leaves: Leaf[]): SummaryTree<Leaf, Summary>;
|
|
3268
|
+
/** This function adds leaves before the first leaf. */
|
|
3269
|
+
prepend(...leaves: Leaf[]): SummaryTree<Leaf, Summary>;
|
|
3270
|
+
/** This function gives the leaves in their order. */
|
|
3271
|
+
[Symbol.iterator](): Iterator<Leaf>;
|
|
3272
|
+
/** This function gives the leaves in an array. */
|
|
3273
|
+
toArray(): Leaf[];
|
|
3274
|
+
}
|
|
3275
|
+
/**
|
|
3276
|
+
* This function makes a persistent tree of leaves with a summary of each part. Use the same `spec` object for each
|
|
3277
|
+
* tree that `concat` or `splice` joins.
|
|
3278
|
+
*/
|
|
3279
|
+
export declare function summaryTree<Leaf, Summary>(spec: SummarySpec<Leaf, Summary>, leaves?: Iterable<Leaf>): SummaryTree<Leaf, Summary>;
|
|
3280
|
+
/**
|
|
3281
|
+
* Options for bounded behavior.
|
|
3282
|
+
*/
|
|
3283
|
+
interface BoundedOptions<T> {
|
|
3284
|
+
/**
|
|
3285
|
+
* Maximum number of elements the collection can hold.
|
|
3286
|
+
*/
|
|
3287
|
+
capacity: number;
|
|
3288
|
+
/**
|
|
3289
|
+
* Policy for handling overflow when collection is full.
|
|
3290
|
+
* @default 'throw'
|
|
3291
|
+
*/
|
|
3292
|
+
policy?: OverflowPolicy;
|
|
3293
|
+
/**
|
|
3294
|
+
* Optional callback invoked when overflow occurs.
|
|
3295
|
+
* The behavior calls this function before it applies the policy.
|
|
3296
|
+
*/
|
|
3297
|
+
onOverflow?: OverflowHandler<T>;
|
|
3298
|
+
}
|
|
3299
|
+
/**
|
|
3300
|
+
* Marker interface for bounded collections.
|
|
3301
|
+
*/
|
|
3302
|
+
interface BoundedCollection$1 {
|
|
3303
|
+
readonly capacity: number;
|
|
3304
|
+
readonly isFull: boolean;
|
|
3305
|
+
readonly remainingCapacity: number;
|
|
3306
|
+
}
|
|
3307
|
+
/**
|
|
3308
|
+
* This function makes a bounded List middleware.
|
|
3309
|
+
*
|
|
3310
|
+
* Intercepts add operations to enforce capacity limits.
|
|
3311
|
+
* This function supports multiple overflow policies.
|
|
3312
|
+
*/
|
|
3313
|
+
export declare function boundedList<T>(options: BoundedOptions<T>): Middleware<List<T>, List<T> & BoundedCollection$1>;
|
|
3314
|
+
/**
|
|
3315
|
+
* This function makes a bounded Queue middleware.
|
|
3316
|
+
*/
|
|
3317
|
+
export declare function boundedQueue<T>(options: BoundedOptions<T>): Middleware<Queue<T>, Queue<T> & BoundedCollection$1>;
|
|
3318
|
+
/**
|
|
3319
|
+
* This function makes a bounded Deque middleware.
|
|
3320
|
+
*/
|
|
3321
|
+
export declare function boundedDeque<T>(options: BoundedOptions<T>): Middleware<Deque<T>, Deque<T> & BoundedCollection$1>;
|
|
3322
|
+
/**
|
|
3323
|
+
* This function makes a bounded Map middleware.
|
|
3324
|
+
*/
|
|
3325
|
+
export declare function boundedMap<K, V>(options: Omit<BoundedOptions<V>, "onOverflow"> & {
|
|
3326
|
+
onOverflow?: (context: Omit<OverflowContext<V>, "newElement"> & {
|
|
3327
|
+
key: K;
|
|
3328
|
+
value: V;
|
|
3329
|
+
}) => void;
|
|
3330
|
+
}): Middleware<MapLike<K, V>, MapLike<K, V> & BoundedCollection$1>;
|
|
3331
|
+
/**
|
|
3332
|
+
* Generic bounded collection middleware.
|
|
3333
|
+
* Works with any collection type.
|
|
3334
|
+
*/
|
|
3335
|
+
export declare function bounded<C extends Collection<any>>(options: BoundedOptions<any>): Middleware<C, C & BoundedCollection$1>;
|
|
3336
|
+
/**
|
|
3337
|
+
* Options for readonly behavior.
|
|
3338
|
+
*/
|
|
3339
|
+
export interface ReadonlyOptions {
|
|
3340
|
+
/**
|
|
3341
|
+
* Custom error message to throw on mutation tries.
|
|
3342
|
+
* @default 'Cannot modify readonly collection'
|
|
3343
|
+
*/
|
|
3344
|
+
errorMessage?: string;
|
|
3345
|
+
}
|
|
3346
|
+
/**
|
|
3347
|
+
* This function makes a readonly List middleware.
|
|
3348
|
+
*
|
|
3349
|
+
* All mutation methods will throw an error.
|
|
3350
|
+
* Read operations work normally.
|
|
3351
|
+
*
|
|
3352
|
+
* @example
|
|
3353
|
+
* const readonlyNumbers = compose(
|
|
3354
|
+
* readonlyList()
|
|
3355
|
+
* )(arrayList([1, 2, 3]))
|
|
3356
|
+
*/
|
|
3357
|
+
export declare function readonlyList<T>(options?: ReadonlyOptions): Middleware<ReadonlyList<T>>;
|
|
3358
|
+
/**
|
|
3359
|
+
* This function makes a readonly Queue middleware.
|
|
3360
|
+
*/
|
|
3361
|
+
export declare function readonlyQueue<T>(options?: ReadonlyOptions): Middleware<ReadonlyQueue<T>>;
|
|
3362
|
+
/**
|
|
3363
|
+
* This function makes a readonly Map middleware.
|
|
3364
|
+
*/
|
|
3365
|
+
export declare function readonlyMap<K, V>(options?: ReadonlyOptions): Middleware<ReadonlyMapLike<K, V>>;
|
|
3366
|
+
/**
|
|
3367
|
+
* This function makes a readonly Collection middleware.
|
|
3368
|
+
*
|
|
3369
|
+
* Generic version that works with any collection type.
|
|
3370
|
+
*/
|
|
3371
|
+
export declare function readonlyCollection<T>(options?: ReadonlyOptions): Middleware<ReadonlyCollection<T>>;
|
|
3372
|
+
/**
|
|
3373
|
+
* Generic readonly middleware.
|
|
3374
|
+
* Alias for readonlyCollection.
|
|
3375
|
+
*/
|
|
3376
|
+
export declare function readonly<C extends object>(options?: ReadonlyOptions): Middleware<C>;
|
|
3377
|
+
/**
|
|
3378
|
+
* Alias for Java compatibility: unmodifiableList
|
|
3379
|
+
*/
|
|
3380
|
+
export declare const unmodifiableList: typeof readonlyList;
|
|
3381
|
+
/**
|
|
3382
|
+
* Alias for Java compatibility: unmodifiableMap
|
|
3383
|
+
*/
|
|
3384
|
+
export declare const unmodifiableMap: typeof readonlyMap;
|
|
3385
|
+
/**
|
|
3386
|
+
* Alias for Java compatibility: unmodifiableCollection
|
|
3387
|
+
*/
|
|
3388
|
+
export declare const unmodifiableCollection: typeof readonlyCollection;
|
|
3389
|
+
/**
|
|
3390
|
+
* Safe operations for List.
|
|
3391
|
+
*/
|
|
3392
|
+
export interface SafeListOperations<T> {
|
|
3393
|
+
/**
|
|
3394
|
+
* Get element at index, giving Option.
|
|
3395
|
+
*/
|
|
3396
|
+
get(index: number): Option<T>;
|
|
3397
|
+
/**
|
|
3398
|
+
* Set element at index, giving Result with old value or error.
|
|
3399
|
+
*/
|
|
3400
|
+
set(index: number, element: T): Result<T, string>;
|
|
3401
|
+
/**
|
|
3402
|
+
* Remove element at index, giving Result with removed value or error.
|
|
3403
|
+
*/
|
|
3404
|
+
removeAt(index: number): Result<T, string>;
|
|
3405
|
+
/**
|
|
3406
|
+
* Get first element as Option.
|
|
3407
|
+
*/
|
|
3408
|
+
first(): Option<T>;
|
|
3409
|
+
/**
|
|
3410
|
+
* Get last element as Option.
|
|
3411
|
+
*/
|
|
3412
|
+
last(): Option<T>;
|
|
3413
|
+
/**
|
|
3414
|
+
* Pop element, giving Option.
|
|
3415
|
+
*/
|
|
3416
|
+
pop(): Option<T>;
|
|
3417
|
+
/**
|
|
3418
|
+
* Shift element, giving Option.
|
|
3419
|
+
*/
|
|
3420
|
+
shift(): Option<T>;
|
|
3421
|
+
}
|
|
3422
|
+
/**
|
|
3423
|
+
* Safe operations for Queue.
|
|
3424
|
+
*/
|
|
3425
|
+
export interface SafeQueueOperations<T> {
|
|
3426
|
+
/**
|
|
3427
|
+
* Peek at head element as Option.
|
|
3428
|
+
*/
|
|
3429
|
+
peek(): Option<T>;
|
|
3430
|
+
/**
|
|
3431
|
+
* Poll element as Option.
|
|
3432
|
+
*/
|
|
3433
|
+
poll(): Option<T>;
|
|
3434
|
+
/**
|
|
3435
|
+
* Dequeue element as Option.
|
|
3436
|
+
*/
|
|
3437
|
+
dequeue(): Option<T>;
|
|
3438
|
+
}
|
|
3439
|
+
/**
|
|
3440
|
+
* Safe operations for Deque.
|
|
3441
|
+
*/
|
|
3442
|
+
export interface SafeDequeOperations<T> extends SafeQueueOperations<T> {
|
|
3443
|
+
/**
|
|
3444
|
+
* Peek at first element as Option.
|
|
3445
|
+
*/
|
|
3446
|
+
peekFirst(): Option<T>;
|
|
3447
|
+
/**
|
|
3448
|
+
* Peek at last element as Option.
|
|
3449
|
+
*/
|
|
3450
|
+
peekLast(): Option<T>;
|
|
3451
|
+
/**
|
|
3452
|
+
* Poll first element as Option.
|
|
3453
|
+
*/
|
|
3454
|
+
pollFirst(): Option<T>;
|
|
3455
|
+
/**
|
|
3456
|
+
* Poll last element as Option.
|
|
3457
|
+
*/
|
|
3458
|
+
pollLast(): Option<T>;
|
|
3459
|
+
/**
|
|
3460
|
+
* Pop element as Option.
|
|
3461
|
+
*/
|
|
3462
|
+
pop(): Option<T>;
|
|
3463
|
+
}
|
|
3464
|
+
/**
|
|
3465
|
+
* Safe operations for Map.
|
|
3466
|
+
*/
|
|
3467
|
+
export interface SafeMapOperations<K, V> {
|
|
3468
|
+
/**
|
|
3469
|
+
* Get value for key as Option.
|
|
3470
|
+
*/
|
|
3471
|
+
get(key: K): Option<V>;
|
|
3472
|
+
/**
|
|
3473
|
+
* Set value for key, giving Result with old value or success.
|
|
3474
|
+
*/
|
|
3475
|
+
set(key: K, value: V): Result<V | undefined, string>;
|
|
3476
|
+
/**
|
|
3477
|
+
* Delete key, giving Result with deleted value or error.
|
|
3478
|
+
*/
|
|
3479
|
+
delete(key: K): Result<V | undefined, string>;
|
|
3480
|
+
}
|
|
3481
|
+
/**
|
|
3482
|
+
* This function makes a safe List middleware.
|
|
3483
|
+
*
|
|
3484
|
+
* This function exposes a `safe` property with Option/Result-based operations.
|
|
3485
|
+
*/
|
|
3486
|
+
export declare function safeList<T>(): Middleware<List<T>, List<T> & {
|
|
3487
|
+
safe: SafeListOperations<T>;
|
|
3488
|
+
}>;
|
|
3489
|
+
/**
|
|
3490
|
+
* This function makes a safe Queue middleware.
|
|
3491
|
+
*/
|
|
3492
|
+
export declare function safeQueue<T>(): Middleware<Queue<T>, Queue<T> & {
|
|
3493
|
+
safe: SafeQueueOperations<T>;
|
|
3494
|
+
}>;
|
|
3495
|
+
/**
|
|
3496
|
+
* This function makes a safe Deque middleware.
|
|
3497
|
+
*/
|
|
3498
|
+
export declare function safeDeque<T>(): Middleware<Deque<T>, Deque<T> & {
|
|
3499
|
+
safe: SafeDequeOperations<T>;
|
|
3500
|
+
}>;
|
|
3501
|
+
/**
|
|
3502
|
+
* This function makes a safe Map middleware.
|
|
3503
|
+
*/
|
|
3504
|
+
export declare function safeMap<K, V>(): Middleware<MapLike<K, V>, MapLike<K, V> & {
|
|
3505
|
+
safe: SafeMapOperations<K, V>;
|
|
3506
|
+
}>;
|
|
3507
|
+
/**
|
|
3508
|
+
* Generic safe middleware that works with any collection.
|
|
3509
|
+
*/
|
|
3510
|
+
export declare function safe<C extends object>(): Middleware<C>;
|
|
3511
|
+
/**
|
|
3512
|
+
* Events emitted by List collections.
|
|
3513
|
+
*/
|
|
3514
|
+
export interface ListEvents<T> {
|
|
3515
|
+
/** Element was added */
|
|
3516
|
+
add: {
|
|
3517
|
+
value: T;
|
|
3518
|
+
index?: number;
|
|
3519
|
+
};
|
|
3520
|
+
/** Element was removed */
|
|
3521
|
+
remove: {
|
|
3522
|
+
value: T;
|
|
3523
|
+
index?: number;
|
|
3524
|
+
};
|
|
3525
|
+
/** Element was replaced */
|
|
3526
|
+
set: {
|
|
3527
|
+
oldValue: T;
|
|
3528
|
+
newValue: T;
|
|
3529
|
+
index: number;
|
|
3530
|
+
};
|
|
3531
|
+
/** Collection was cleared */
|
|
3532
|
+
clear: {};
|
|
3533
|
+
/** Collection was sorted */
|
|
3534
|
+
sort: {};
|
|
3535
|
+
/** Collection was reversed */
|
|
3536
|
+
reverse: {};
|
|
3537
|
+
}
|
|
3538
|
+
/**
|
|
3539
|
+
* Events emitted by Queue collections.
|
|
3540
|
+
*/
|
|
3541
|
+
export interface QueueEvents<T> {
|
|
3542
|
+
/** Element was added to queue */
|
|
3543
|
+
enqueue: {
|
|
3544
|
+
value: T;
|
|
3545
|
+
};
|
|
3546
|
+
/** Element was removed from queue */
|
|
3547
|
+
dequeue: {
|
|
3548
|
+
value: T;
|
|
3549
|
+
};
|
|
3550
|
+
/** Collection was cleared */
|
|
3551
|
+
clear: {};
|
|
3552
|
+
}
|
|
3553
|
+
/**
|
|
3554
|
+
* Events emitted by Deque collections.
|
|
3555
|
+
*/
|
|
3556
|
+
export interface DequeEvents<T> {
|
|
3557
|
+
/** Element was added to front */
|
|
3558
|
+
addFirst: {
|
|
3559
|
+
value: T;
|
|
3560
|
+
};
|
|
3561
|
+
/** Element was added to back */
|
|
3562
|
+
addLast: {
|
|
3563
|
+
value: T;
|
|
3564
|
+
};
|
|
3565
|
+
/** Element was removed from front */
|
|
3566
|
+
removeFirst: {
|
|
3567
|
+
value: T;
|
|
3568
|
+
};
|
|
3569
|
+
/** Element was removed from back */
|
|
3570
|
+
removeLast: {
|
|
3571
|
+
value: T;
|
|
3572
|
+
};
|
|
3573
|
+
/** Collection was cleared */
|
|
3574
|
+
clear: {};
|
|
3575
|
+
}
|
|
3576
|
+
/**
|
|
3577
|
+
* Events emitted by Map collections.
|
|
3578
|
+
*/
|
|
3579
|
+
export interface MapEvents<K, V> {
|
|
3580
|
+
/** Key-value pair was set */
|
|
3581
|
+
set: {
|
|
3582
|
+
key: K;
|
|
3583
|
+
value: V;
|
|
3584
|
+
oldValue?: V;
|
|
3585
|
+
};
|
|
3586
|
+
/** Key was deleted */
|
|
3587
|
+
delete: {
|
|
3588
|
+
key: K;
|
|
3589
|
+
value: V;
|
|
3590
|
+
};
|
|
3591
|
+
/** Collection was cleared */
|
|
3592
|
+
clear: {};
|
|
3593
|
+
}
|
|
3594
|
+
/**
|
|
3595
|
+
* Generic collection events.
|
|
3596
|
+
*/
|
|
3597
|
+
export interface CollectionEvents<T> {
|
|
3598
|
+
/** Element was added */
|
|
3599
|
+
add: {
|
|
3600
|
+
value: T;
|
|
3601
|
+
};
|
|
3602
|
+
/** Element was removed */
|
|
3603
|
+
remove: {
|
|
3604
|
+
value: T;
|
|
3605
|
+
};
|
|
3606
|
+
/** Collection was cleared */
|
|
3607
|
+
clear: {};
|
|
3608
|
+
}
|
|
3609
|
+
/**
|
|
3610
|
+
* This function makes an evented List middleware.
|
|
3611
|
+
*
|
|
3612
|
+
* This function emits events for add, remove, set, clear, sort, and reverse operations.
|
|
3613
|
+
*/
|
|
3614
|
+
export declare function eventedList<T>(): Middleware<List<T>, List<T> & {
|
|
3615
|
+
on<E extends keyof ListEvents<T>>(event: E, listener: (payload: ListEvents<T>[E]) => void): Unsubscribe;
|
|
3616
|
+
}>;
|
|
3617
|
+
/**
|
|
3618
|
+
* This function makes an evented Queue middleware.
|
|
3619
|
+
*/
|
|
3620
|
+
export declare function eventedQueue<T>(): Middleware<Queue<T>, Queue<T> & {
|
|
3621
|
+
on<E extends keyof QueueEvents<T>>(event: E, listener: (payload: QueueEvents<T>[E]) => void): Unsubscribe;
|
|
3622
|
+
}>;
|
|
3623
|
+
/**
|
|
3624
|
+
* This function makes an evented Deque middleware.
|
|
3625
|
+
*/
|
|
3626
|
+
export declare function eventedDeque<T>(): Middleware<Deque<T>, Deque<T> & {
|
|
3627
|
+
on<E extends keyof DequeEvents<T>>(event: E, listener: (payload: DequeEvents<T>[E]) => void): Unsubscribe;
|
|
3628
|
+
}>;
|
|
3629
|
+
/**
|
|
3630
|
+
* This function makes an evented Map middleware.
|
|
3631
|
+
*/
|
|
3632
|
+
export declare function eventedMap<K, V>(): Middleware<MapLike<K, V>, MapLike<K, V> & {
|
|
3633
|
+
on<E extends keyof MapEvents<K, V>>(event: E, listener: (payload: MapEvents<K, V>[E]) => void): Unsubscribe;
|
|
3634
|
+
}>;
|
|
3635
|
+
/**
|
|
3636
|
+
* Generic evented collection middleware.
|
|
3637
|
+
*/
|
|
3638
|
+
export declare function evented<T>(): Middleware<Collection<T>, Collection<T> & {
|
|
3639
|
+
on<E extends keyof CollectionEvents<T>>(event: E, listener: (payload: CollectionEvents<T>[E]) => void): Unsubscribe;
|
|
3640
|
+
}>;
|
|
3641
|
+
/**
|
|
3642
|
+
* This function makes a synchronized middleware that locks all method calls.
|
|
3643
|
+
*
|
|
3644
|
+
* Every method call on the collection is wrapped with a mutex,
|
|
3645
|
+
* making sure that atomic execution even in concurrent async scenarios.
|
|
3646
|
+
*
|
|
3647
|
+
* @example
|
|
3648
|
+
* const syncList = compose(
|
|
3649
|
+
* synchronized()
|
|
3650
|
+
* )(arrayList<number>())
|
|
3651
|
+
*
|
|
3652
|
+
* // These additions are guaranteed to execute in order
|
|
3653
|
+
* await Promise.all([
|
|
3654
|
+
* syncList.add(1),
|
|
3655
|
+
* syncList.add(2),
|
|
3656
|
+
* syncList.add(3)
|
|
3657
|
+
* ])
|
|
3658
|
+
*/
|
|
3659
|
+
export declare function synchronized<C extends object>(): Middleware<C>;
|
|
3660
|
+
/**
|
|
3661
|
+
* Alias for Java compatibility: synchronizedList
|
|
3662
|
+
*/
|
|
3663
|
+
export declare const synchronizedList: typeof synchronized;
|
|
3664
|
+
/**
|
|
3665
|
+
* Alias for Java compatibility: synchronizedMap
|
|
3666
|
+
*/
|
|
3667
|
+
export declare const synchronizedMap: typeof synchronized;
|
|
3668
|
+
/**
|
|
3669
|
+
* Alias for Java compatibility: synchronizedCollection
|
|
3670
|
+
*/
|
|
3671
|
+
export declare const synchronizedCollection: typeof synchronized;
|
|
3672
|
+
/**
|
|
3673
|
+
* Alias for Java compatibility: synchronizedSet
|
|
3674
|
+
*/
|
|
3675
|
+
export declare const synchronizedSet: typeof synchronized;
|
|
3676
|
+
/**
|
|
3677
|
+
* Advanced synchronized middleware with reentrant locking.
|
|
3678
|
+
*
|
|
3679
|
+
* With this function, the same "thread" (async context) can acquire the lock multiple times.
|
|
3680
|
+
* Useful for methods that call other synchronized methods internally.
|
|
3681
|
+
*
|
|
3682
|
+
* Note: JavaScript has no true thread IDs. This function uses async context
|
|
3683
|
+
* tracking if available. If not, the behavior is non-reentrant.
|
|
3684
|
+
*/
|
|
3685
|
+
export declare function reentrantSynchronized<C extends object>(): Middleware<C>;
|
|
3686
|
+
/**
|
|
3687
|
+
* Read-write lock implementation for collections.
|
|
3688
|
+
*
|
|
3689
|
+
* With this function, multiple concurrent readers can read, but a writer is exclusive.
|
|
3690
|
+
* More efficient than full synchronization for read-heavy workloads.
|
|
3691
|
+
*
|
|
3692
|
+
* @example
|
|
3693
|
+
* const list = compose(
|
|
3694
|
+
* readWriteLock()
|
|
3695
|
+
* )(arrayList<number>())
|
|
3696
|
+
*
|
|
3697
|
+
* // Multiple reads can happen concurrently
|
|
3698
|
+
* await Promise.all([
|
|
3699
|
+
* list.get(0),
|
|
3700
|
+
* list.get(1),
|
|
3701
|
+
* list.get(2)
|
|
3702
|
+
* ])
|
|
3703
|
+
*/
|
|
3704
|
+
export declare function readWriteLock<C extends object>(): Middleware<C>;
|
|
3705
|
+
/**
|
|
3706
|
+
* Options for LRU behavior.
|
|
3707
|
+
*/
|
|
3708
|
+
export interface LRUOptions {
|
|
3709
|
+
/**
|
|
3710
|
+
* Maximum capacity. When exceeded, least recently used items are evicted.
|
|
3711
|
+
*/
|
|
3712
|
+
capacity: number;
|
|
3713
|
+
/**
|
|
3714
|
+
* Optional callback invoked when an item is evicted.
|
|
3715
|
+
*/
|
|
3716
|
+
onEvict?: (info: {
|
|
3717
|
+
key: any;
|
|
3718
|
+
value: any;
|
|
3719
|
+
timestamp: number;
|
|
3720
|
+
}) => void;
|
|
3721
|
+
}
|
|
3722
|
+
/**
|
|
3723
|
+
* This function makes an LRU Map middleware.
|
|
3724
|
+
*
|
|
3725
|
+
* This function maintains a doubly-linked list to track access order.
|
|
3726
|
+
* Most recently accessed items are at the head.
|
|
3727
|
+
* When capacity is exceeded, items at the tail are evicted.
|
|
3728
|
+
*/
|
|
3729
|
+
export declare function lruMap<K, V>(options: LRUOptions): Middleware<MapLike<K, V>, MapLike<K, V> & {
|
|
3730
|
+
readonly isFull: boolean;
|
|
3731
|
+
}>;
|
|
3732
|
+
/**
|
|
3733
|
+
* This class makes an LRU cache with a simpler API.
|
|
3734
|
+
* This is a standalone LRU cache implementation.
|
|
3735
|
+
*
|
|
3736
|
+
* @example
|
|
3737
|
+
* const cache = lruCache<string, Data>(100)
|
|
3738
|
+
* cache.set('key', value)
|
|
3739
|
+
* cache.get('key') // Moves to front
|
|
3740
|
+
*/
|
|
3741
|
+
export declare class LRUCache<K, V> {
|
|
3742
|
+
private readonly capacity;
|
|
3743
|
+
private readonly onEvict;
|
|
3744
|
+
private map;
|
|
3745
|
+
private head;
|
|
3746
|
+
private tail;
|
|
3747
|
+
constructor(capacity: number, onEvict?: ((key: K, value: V) => void) | undefined);
|
|
3748
|
+
get size(): number;
|
|
3749
|
+
get isFull(): boolean;
|
|
3750
|
+
get(key: K): V | undefined;
|
|
3751
|
+
set(key: K, value: V): void;
|
|
3752
|
+
has(key: K): boolean;
|
|
3753
|
+
delete(key: K): boolean;
|
|
3754
|
+
clear(): void;
|
|
3755
|
+
private moveToHead;
|
|
3756
|
+
private removeNode;
|
|
3757
|
+
private evictLRU;
|
|
3758
|
+
/**
|
|
3759
|
+
* This method gives keys in LRU order (most recent first).
|
|
3760
|
+
*/
|
|
3761
|
+
keys(): IterableIterator<K>;
|
|
3762
|
+
/**
|
|
3763
|
+
* This method gives entries in LRU order (most recent first).
|
|
3764
|
+
*/
|
|
3765
|
+
entries(): IterableIterator<[K, V]>;
|
|
3766
|
+
}
|
|
3767
|
+
/**
|
|
3768
|
+
* Factory function to make an LRU cache.
|
|
3769
|
+
*/
|
|
3770
|
+
export declare function lruCache<K, V>(capacity: number, onEvict?: (key: K, value: V) => void): LRUCache<K, V>;
|
|
3771
|
+
/**
|
|
3772
|
+
* Options for TTL behavior.
|
|
3773
|
+
*/
|
|
3774
|
+
export interface TTLOptions {
|
|
3775
|
+
/**
|
|
3776
|
+
* Time-to-live in milliseconds.
|
|
3777
|
+
* Elements expire after this duration.
|
|
3778
|
+
*/
|
|
3779
|
+
ttl: number;
|
|
3780
|
+
/**
|
|
3781
|
+
* Interval for checking expired elements (in milliseconds).
|
|
3782
|
+
* @default 1000 (check every second)
|
|
3783
|
+
*/
|
|
3784
|
+
checkInterval?: number;
|
|
3785
|
+
/**
|
|
3786
|
+
* Optional callback invoked when an element expires.
|
|
3787
|
+
*/
|
|
3788
|
+
onExpire?: (info: {
|
|
3789
|
+
key: any;
|
|
3790
|
+
value: any;
|
|
3791
|
+
createdAt: number;
|
|
3792
|
+
expiredAt: number;
|
|
3793
|
+
}) => void;
|
|
3794
|
+
}
|
|
3795
|
+
/**
|
|
3796
|
+
* This function makes a TTL Map middleware.
|
|
3797
|
+
*
|
|
3798
|
+
* Automatically removes entries after TTL expires.
|
|
3799
|
+
* This function uses a background interval to check for expired entries.
|
|
3800
|
+
*/
|
|
3801
|
+
export declare function ttlMap<K, V>(options: TTLOptions): Middleware<MapLike<K, V>, MapLike<K, V> & {
|
|
3802
|
+
readonly dispose: () => void;
|
|
3803
|
+
}>;
|
|
3804
|
+
/**
|
|
3805
|
+
* This function makes a TTL Collection middleware.
|
|
3806
|
+
*
|
|
3807
|
+
* Note: This is less precise than TTL map because collections don't have keys.
|
|
3808
|
+
* Elements are tracked by reference, which can fail for primitive types.
|
|
3809
|
+
*/
|
|
3810
|
+
export declare function ttlCollection<T>(options: Omit<TTLOptions, "onExpire"> & {
|
|
3811
|
+
onExpire?: (info: {
|
|
3812
|
+
element: T;
|
|
3813
|
+
createdAt: number;
|
|
3814
|
+
expiredAt: number;
|
|
3815
|
+
}) => void;
|
|
3816
|
+
}): Middleware<Collection<T>, Collection<T> & {
|
|
3817
|
+
readonly dispose: () => void;
|
|
3818
|
+
}>;
|
|
3819
|
+
/**
|
|
3820
|
+
* Standalone TTL cache with Map-like API.
|
|
3821
|
+
*
|
|
3822
|
+
* @example
|
|
3823
|
+
* const cache = new TTLCache<string, number>(60000) // 60 second TTL
|
|
3824
|
+
* cache.set('key', 42)
|
|
3825
|
+
* setTimeout(() => {
|
|
3826
|
+
* cache.get('key') // undefined (expired)
|
|
3827
|
+
* }, 61000)
|
|
3828
|
+
*/
|
|
3829
|
+
export declare class TTLCache<K, V> {
|
|
3830
|
+
private readonly ttl;
|
|
3831
|
+
private readonly checkInterval;
|
|
3832
|
+
private readonly onExpire;
|
|
3833
|
+
private map;
|
|
3834
|
+
private intervalId;
|
|
3835
|
+
constructor(ttl: number, checkInterval?: number, onExpire?: ((key: K, value: V) => void) | undefined);
|
|
3836
|
+
get size(): number;
|
|
3837
|
+
get(key: K): V | undefined;
|
|
3838
|
+
set(key: K, value: V, customTTL?: number): void;
|
|
3839
|
+
has(key: K): boolean;
|
|
3840
|
+
delete(key: K): boolean;
|
|
3841
|
+
clear(): void;
|
|
3842
|
+
/**
|
|
3843
|
+
* Manually trigger cleanup of expired entries.
|
|
3844
|
+
*/
|
|
3845
|
+
cleanup(): void;
|
|
3846
|
+
/**
|
|
3847
|
+
* Get remaining TTL for a key in milliseconds.
|
|
3848
|
+
* This method gives undefined if key doesn't exist or is expired.
|
|
3849
|
+
*/
|
|
3850
|
+
getTTL(key: K): number | undefined;
|
|
3851
|
+
/**
|
|
3852
|
+
* Update TTL for an existing key.
|
|
3853
|
+
*/
|
|
3854
|
+
touch(key: K, customTTL?: number): boolean;
|
|
3855
|
+
private startExpirationCheck;
|
|
3856
|
+
/**
|
|
3857
|
+
* Stop the background expiration checker and clean up.
|
|
3858
|
+
*/
|
|
3859
|
+
dispose(): void;
|
|
3860
|
+
keys(): IterableIterator<K>;
|
|
3861
|
+
values(): IterableIterator<V>;
|
|
3862
|
+
entries(): IterableIterator<[K, V]>;
|
|
3863
|
+
}
|
|
3864
|
+
/**
|
|
3865
|
+
* Factory function to make a TTL cache.
|
|
3866
|
+
*/
|
|
3867
|
+
export declare function ttlCache<K, V>(ttl: number, options?: {
|
|
3868
|
+
checkInterval?: number;
|
|
3869
|
+
onExpire?: (key: K, value: V) => void;
|
|
3870
|
+
}): TTLCache<K, V>;
|
|
3871
|
+
/**
|
|
3872
|
+
* Options for making an AsyncQueue.
|
|
3873
|
+
*/
|
|
3874
|
+
export interface AsyncQueueOptions {
|
|
3875
|
+
/**
|
|
3876
|
+
* Maximum capacity. Infinity for unbounded queue.
|
|
3877
|
+
* @default Infinity
|
|
3878
|
+
*/
|
|
3879
|
+
capacity?: number;
|
|
3880
|
+
/**
|
|
3881
|
+
* Timeout for put/take operations in milliseconds.
|
|
3882
|
+
* @default undefined (no timeout)
|
|
3883
|
+
*/
|
|
3884
|
+
timeout?: number;
|
|
3885
|
+
}
|
|
3886
|
+
/**
|
|
3887
|
+
* AsyncQueue<T> - Thread-safe async queue with backpressure.
|
|
3888
|
+
*
|
|
3889
|
+
* Features:
|
|
3890
|
+
* - Blocking put() when full (backpressure)
|
|
3891
|
+
* - Blocking take() when empty
|
|
3892
|
+
* - AsyncIterable support
|
|
3893
|
+
* - Graceful closing
|
|
3894
|
+
* - Timeouts
|
|
3895
|
+
*
|
|
3896
|
+
* Perfect for:
|
|
3897
|
+
* - Producer-consumer patterns
|
|
3898
|
+
* - Rate limiting
|
|
3899
|
+
* - Task queues
|
|
3900
|
+
* - Stream processing
|
|
3901
|
+
*/
|
|
3902
|
+
export declare class AsyncQueue<T> implements AsyncQueue$1<T> {
|
|
3903
|
+
private buffer;
|
|
3904
|
+
private putters;
|
|
3905
|
+
private takers;
|
|
3906
|
+
/** The calls of `peek` that wait: they see the next element, but they do not take it. */
|
|
3907
|
+
private peekers;
|
|
3908
|
+
private _isClosed;
|
|
3909
|
+
private readonly capacity;
|
|
3910
|
+
private readonly defaultTimeout?;
|
|
3911
|
+
constructor(options?: AsyncQueueOptions);
|
|
3912
|
+
get size(): number;
|
|
3913
|
+
get isEmpty(): boolean;
|
|
3914
|
+
get isFull(): boolean;
|
|
3915
|
+
get isClosed(): boolean;
|
|
3916
|
+
get remainingCapacity(): number;
|
|
3917
|
+
/**
|
|
3918
|
+
* This method adds an element to the queue.
|
|
3919
|
+
* If the queue is full, waits until space is available. With a capacity of 0, it waits until
|
|
3920
|
+
* a taker takes the element (a rendezvous).
|
|
3921
|
+
*
|
|
3922
|
+
* @throws Error if queue is closed
|
|
3923
|
+
* @throws Error if timeout expires
|
|
3924
|
+
*/
|
|
3925
|
+
put(element: T, timeout?: number): Promise<void>;
|
|
3926
|
+
/**
|
|
3927
|
+
* Removes and gives an element from the queue.
|
|
3928
|
+
* If the queue is empty, waits until an element is available.
|
|
3929
|
+
*
|
|
3930
|
+
* @throws Error if queue is closed and empty
|
|
3931
|
+
* @throws Error if timeout expires
|
|
3932
|
+
*/
|
|
3933
|
+
take(timeout?: number): Promise<T>;
|
|
3934
|
+
/**
|
|
3935
|
+
* This method waits for the next element, as `take` does, but it gives the element to
|
|
3936
|
+
* `onElement` and the closure of the queue to `onClosed`. These calls are synchronous: they
|
|
3937
|
+
* occur in the call that adds the element or closes the queue. The method has no timeout.
|
|
3938
|
+
*
|
|
3939
|
+
* The function that it gives withdraws the wait. A withdrawn wait gets no element, so no
|
|
3940
|
+
* element goes to a taker that left. `select` races its cases with this method.
|
|
3941
|
+
*
|
|
3942
|
+
* @returns A function that withdraws the wait. It gives true when the wait had no element yet.
|
|
3943
|
+
*/
|
|
3944
|
+
waitTake(onElement: (element: T) => void, onClosed: (error: Error) => void): () => boolean;
|
|
3945
|
+
/**
|
|
3946
|
+
* This method tries to add an element without waiting.
|
|
3947
|
+
* This method gives false if queue is full (with a capacity of 0: if no taker waits).
|
|
3948
|
+
*/
|
|
3949
|
+
tryPut(element: T): boolean;
|
|
3950
|
+
/**
|
|
3951
|
+
* This method tries to remove an element without waiting.
|
|
3952
|
+
* This method gives undefined if queue is empty.
|
|
3953
|
+
*/
|
|
3954
|
+
tryTake(): T | undefined;
|
|
3955
|
+
/** This method gives the element to a waiting taker, or puts it in the buffer when there is space. */
|
|
3956
|
+
private tryPutNow;
|
|
3957
|
+
/** Each waiting peek gets the element, which stays where it is. */
|
|
3958
|
+
private wakePeekers;
|
|
3959
|
+
/**
|
|
3960
|
+
* This method takes the next element: from the buffer (then a waiting putter moves its element into the
|
|
3961
|
+
* buffer), or directly from a waiting putter (a capacity of 0). (Before, the direct path
|
|
3962
|
+
* released the putter but did not give its element to the taker, so the taker waited forever:
|
|
3963
|
+
* BUGS-2026-07 C11.)
|
|
3964
|
+
*/
|
|
3965
|
+
private takeNow;
|
|
3966
|
+
/**
|
|
3967
|
+
* This method gives the next element without removing it.
|
|
3968
|
+
* Waits if queue is empty.
|
|
3969
|
+
*/
|
|
3970
|
+
peek(): Promise<T>;
|
|
3971
|
+
/**
|
|
3972
|
+
* This method gives the next element without waiting.
|
|
3973
|
+
* This method gives undefined if queue is empty.
|
|
3974
|
+
*/
|
|
3975
|
+
tryPeek(): T | undefined;
|
|
3976
|
+
/**
|
|
3977
|
+
* This method closes the queue. No more elements can be added.
|
|
3978
|
+
* Existing elements can still be consumed.
|
|
3979
|
+
*/
|
|
3980
|
+
close(): void;
|
|
3981
|
+
/**
|
|
3982
|
+
* This method drains all elements into an array.
|
|
3983
|
+
* This method gives immediately with current elements.
|
|
3984
|
+
*/
|
|
3985
|
+
drain(): T[];
|
|
3986
|
+
/**
|
|
3987
|
+
* Async iterator that yields elements as they become available.
|
|
3988
|
+
* Stops when queue is closed and empty.
|
|
3989
|
+
*/
|
|
3990
|
+
[Symbol.asyncIterator](): AsyncIterator<T>;
|
|
3991
|
+
/** When the buffer has space, the first waiting putter moves its element into it. */
|
|
3992
|
+
private admitPutter;
|
|
3993
|
+
private clearTimeout;
|
|
3994
|
+
/**
|
|
3995
|
+
* This method gives statistics about the queue.
|
|
3996
|
+
*/
|
|
3997
|
+
getStats(): {
|
|
3998
|
+
size: number;
|
|
3999
|
+
capacity: number;
|
|
4000
|
+
isEmpty: boolean;
|
|
4001
|
+
isFull: boolean;
|
|
4002
|
+
isClosed: boolean;
|
|
4003
|
+
waitingPutters: number;
|
|
4004
|
+
waitingTakers: number;
|
|
4005
|
+
remainingCapacity: number;
|
|
4006
|
+
};
|
|
4007
|
+
toString(): string;
|
|
4008
|
+
}
|
|
4009
|
+
/**
|
|
4010
|
+
* Factory function to make an AsyncQueue.
|
|
4011
|
+
*
|
|
4012
|
+
* @example
|
|
4013
|
+
* const queue = asyncQueue<number>({ capacity: 100 })
|
|
4014
|
+
* await queue.put(42)
|
|
4015
|
+
* const value = await queue.take()
|
|
4016
|
+
*/
|
|
4017
|
+
export declare function asyncQueue<T>(options?: AsyncQueueOptions): AsyncQueue<T>;
|
|
4018
|
+
/**
|
|
4019
|
+
* Channel<T> - Bidirectional communication channel.
|
|
4020
|
+
*
|
|
4021
|
+
* The channels of Go are the model. This class gives send/receive operations
|
|
4022
|
+
* that can block until the other side is ready.
|
|
4023
|
+
*/
|
|
4024
|
+
export declare class Channel<T> {
|
|
4025
|
+
private queue;
|
|
4026
|
+
private _isClosed;
|
|
4027
|
+
constructor(bufferSize?: number);
|
|
4028
|
+
/**
|
|
4029
|
+
* This method sends a value through the channel.
|
|
4030
|
+
* Blocks if channel buffer is full.
|
|
4031
|
+
*
|
|
4032
|
+
* @throws Error if channel is closed
|
|
4033
|
+
*/
|
|
4034
|
+
send(value: T): Promise<void>;
|
|
4035
|
+
/**
|
|
4036
|
+
* This method receives a value from the channel.
|
|
4037
|
+
* Blocks if no value is available.
|
|
4038
|
+
*
|
|
4039
|
+
* @throws Error if channel is closed and empty
|
|
4040
|
+
*/
|
|
4041
|
+
receive(): Promise<T>;
|
|
4042
|
+
/**
|
|
4043
|
+
* This method waits for one value, as `receive` does, and gives it to `onValue`. When the
|
|
4044
|
+
* channel is closed and empty, it calls `onClosed`. These calls are synchronous: they occur in
|
|
4045
|
+
* the call that sends the value or closes the channel.
|
|
4046
|
+
*
|
|
4047
|
+
* The function that it gives withdraws the wait, so no later value goes to it.
|
|
4048
|
+
*
|
|
4049
|
+
* @returns A function that withdraws the wait. It gives true when the wait had no value yet.
|
|
4050
|
+
*/
|
|
4051
|
+
waitReceive(onValue: (value: T) => void, onClosed: (error: Error) => void): () => boolean;
|
|
4052
|
+
/**
|
|
4053
|
+
* This method tries to send without blocking.
|
|
4054
|
+
* This method gives false if channel is full.
|
|
4055
|
+
*/
|
|
4056
|
+
trySend(value: T): boolean;
|
|
4057
|
+
/**
|
|
4058
|
+
* This method tries to receive without blocking.
|
|
4059
|
+
* This method gives undefined if no value is available.
|
|
4060
|
+
*/
|
|
4061
|
+
tryReceive(): T | undefined;
|
|
4062
|
+
/**
|
|
4063
|
+
* This method closes the channel. No more values can be sent.
|
|
4064
|
+
* Receivers can still drain remaining values.
|
|
4065
|
+
*/
|
|
4066
|
+
close(): void;
|
|
4067
|
+
get isClosed(): boolean;
|
|
4068
|
+
get size(): number;
|
|
4069
|
+
/**
|
|
4070
|
+
* This method makes a select case for this channel.
|
|
4071
|
+
* The select() function uses this value for multiplexing.
|
|
4072
|
+
*/
|
|
4073
|
+
case<R>(handler: (value: T) => R): SelectCase<R>;
|
|
4074
|
+
/**
|
|
4075
|
+
* Async iteration over channel values.
|
|
4076
|
+
* Stops when channel is closed and empty.
|
|
4077
|
+
*/
|
|
4078
|
+
[Symbol.asyncIterator](): AsyncIterator<T>;
|
|
4079
|
+
}
|
|
4080
|
+
/**
|
|
4081
|
+
* SelectCase represents a case in a select statement.
|
|
4082
|
+
*/
|
|
4083
|
+
export interface SelectCase<R> {
|
|
4084
|
+
channel: Channel<any>;
|
|
4085
|
+
handler: (value: any) => R;
|
|
4086
|
+
tryReceive: () => any;
|
|
4087
|
+
receive: () => Promise<any>;
|
|
4088
|
+
/**
|
|
4089
|
+
* A wait for one value that `select` can withdraw: `Channel.waitReceive`. A case without it
|
|
4090
|
+
* uses the `waitReceive` method of its channel.
|
|
4091
|
+
*/
|
|
4092
|
+
wait?: (onValue: (value: any) => void, onClosed: (error: Error) => void) => () => boolean;
|
|
4093
|
+
}
|
|
4094
|
+
/**
|
|
4095
|
+
* Selects from multiple channels (like Go's select statement).
|
|
4096
|
+
*
|
|
4097
|
+
* This function waits for the first channel to have a value available,
|
|
4098
|
+
* then calls its handler and gives the result.
|
|
4099
|
+
*
|
|
4100
|
+
* The first case with a value at once wins. The cases that lose keep no receive: a value that
|
|
4101
|
+
* comes later stays in its channel for the next receiver. When the channel of a case is closed
|
|
4102
|
+
* and empty, the given promise rejects.
|
|
4103
|
+
*
|
|
4104
|
+
* @example
|
|
4105
|
+
* const result = await select(
|
|
4106
|
+
* ch1.case((v) => `ch1: ${v}`),
|
|
4107
|
+
* ch2.case((v) => `ch2: ${v}`),
|
|
4108
|
+
* timeout(1000).case(() => 'timeout')
|
|
4109
|
+
* )
|
|
4110
|
+
*/
|
|
4111
|
+
export declare function select<R>(...cases: SelectCase<R>[]): Promise<R>;
|
|
4112
|
+
/**
|
|
4113
|
+
* This function makes a timeout channel that sends after the specified duration.
|
|
4114
|
+
*
|
|
4115
|
+
* @example
|
|
4116
|
+
* const result = await select(
|
|
4117
|
+
* dataChannel.case((v) => v),
|
|
4118
|
+
* timeout(5000).case(() => 'timeout!')
|
|
4119
|
+
* )
|
|
4120
|
+
*/
|
|
4121
|
+
export declare function timeout(ms: number): Channel<void>;
|
|
4122
|
+
/**
|
|
4123
|
+
* This function makes a ticker channel that sends at regular intervals.
|
|
4124
|
+
*
|
|
4125
|
+
* @example
|
|
4126
|
+
* const tick = ticker(1000) // Every second
|
|
4127
|
+
* for await (const t of tick) {
|
|
4128
|
+
* console.log('Tick:', t)
|
|
4129
|
+
* }
|
|
4130
|
+
*/
|
|
4131
|
+
export declare function ticker(ms: number): Channel<number> & {
|
|
4132
|
+
stop: () => void;
|
|
4133
|
+
};
|
|
4134
|
+
/**
|
|
4135
|
+
* Pipeline: pipes values from input channel through transform to output channel.
|
|
4136
|
+
*
|
|
4137
|
+
* @example
|
|
4138
|
+
* const input = channel<number>()
|
|
4139
|
+
* const output = pipeline(input, (x) => x * 2)
|
|
4140
|
+
*
|
|
4141
|
+
* await input.send(5)
|
|
4142
|
+
* const result = await output.receive() // 10
|
|
4143
|
+
*/
|
|
4144
|
+
export declare function pipeline<T, U>(input: Channel<T>, transform: (value: T) => U | Promise<U>, bufferSize?: number): Channel<U>;
|
|
4145
|
+
/**
|
|
4146
|
+
* Fan-out: distributes values from one input channel to multiple output channels.
|
|
4147
|
+
*
|
|
4148
|
+
* @example
|
|
4149
|
+
* const input = channel<number>()
|
|
4150
|
+
* const [out1, out2, out3] = fanOut(input, 3)
|
|
4151
|
+
*
|
|
4152
|
+
* // Each output gets a copy of every input value
|
|
4153
|
+
*/
|
|
4154
|
+
export declare function fanOut<T>(input: Channel<T>, count: number, bufferSize?: number): Channel<T>[];
|
|
4155
|
+
/**
|
|
4156
|
+
* Fan-in: merges multiple input channels into one output channel.
|
|
4157
|
+
*
|
|
4158
|
+
* @example
|
|
4159
|
+
* const ch1 = channel<number>()
|
|
4160
|
+
* const ch2 = channel<number>()
|
|
4161
|
+
* const merged = fanIn(ch1, ch2)
|
|
4162
|
+
*
|
|
4163
|
+
* // merged receives values from both ch1 and ch2
|
|
4164
|
+
*/
|
|
4165
|
+
export declare function fanIn<T>(...inputs: Channel<T>[]): Channel<T>;
|
|
4166
|
+
/**
|
|
4167
|
+
* Merge: like fanIn but closes output when first input closes.
|
|
4168
|
+
*
|
|
4169
|
+
* It takes one value at a time, from the inputs in turn. It keeps no receive on an input while
|
|
4170
|
+
* it waits for the reader of the output. Thus each value of each input reaches the output one
|
|
4171
|
+
* time, in the order of its input, or stays in its input.
|
|
4172
|
+
*/
|
|
4173
|
+
export declare function merge<T>(...inputs: Channel<T>[]): Channel<T>;
|
|
4174
|
+
/**
|
|
4175
|
+
* Buffered channel with specified capacity.
|
|
4176
|
+
*/
|
|
4177
|
+
export declare function buffered<T>(capacity: number): Channel<T>;
|
|
4178
|
+
/**
|
|
4179
|
+
* Unbuffered channel (rendezvous point).
|
|
4180
|
+
* Sends block until receiver is ready.
|
|
4181
|
+
*/
|
|
4182
|
+
export declare function unbuffered<T>(): Channel<T>;
|
|
4183
|
+
/**
|
|
4184
|
+
* Factory function to make a channel.
|
|
4185
|
+
*
|
|
4186
|
+
* @example
|
|
4187
|
+
* const ch = channel<number>()
|
|
4188
|
+
* const bufferedCh = channel<string>(10)
|
|
4189
|
+
*/
|
|
4190
|
+
export declare function channel<T>(bufferSize?: number): Channel<T>;
|
|
4191
|
+
/**
|
|
4192
|
+
* WorkerPool: distributes work across multiple workers.
|
|
4193
|
+
*
|
|
4194
|
+
* @example
|
|
4195
|
+
* const pool = workerPool(
|
|
4196
|
+
* jobChannel,
|
|
4197
|
+
* async (job) => await processJob(job),
|
|
4198
|
+
* { workers: 4 }
|
|
4199
|
+
* )
|
|
4200
|
+
*
|
|
4201
|
+
* // 4 workers concurrently process jobs from jobChannel
|
|
4202
|
+
*/
|
|
4203
|
+
export declare function workerPool<T, R>(jobs: Channel<T>, worker: (job: T) => Promise<R>, options?: {
|
|
4204
|
+
workers: number;
|
|
4205
|
+
bufferSize?: number;
|
|
4206
|
+
}): Channel<R>;
|
|
4207
|
+
/**
|
|
4208
|
+
* asSettled - The results of running promises, in the order that they settle.
|
|
4209
|
+
*
|
|
4210
|
+
* @example
|
|
4211
|
+
* const pages = urls.map((url) => fetch(url));
|
|
4212
|
+
* for await (const result of asSettled(pages)) {
|
|
4213
|
+
* if (result.status === "fulfilled") show(result.index, result.value);
|
|
4214
|
+
* else report(result.index, result.reason);
|
|
4215
|
+
* }
|
|
4216
|
+
*/
|
|
4217
|
+
/** One result of `asSettled`: the index of its promise in the input, and how the promise settled. */
|
|
4218
|
+
export type Settled<T> = {
|
|
4219
|
+
readonly index: number;
|
|
4220
|
+
} & PromiseSettledResult<T>;
|
|
4221
|
+
/**
|
|
4222
|
+
* This function gives the result of each promise when it settles, in settle order, not in input
|
|
4223
|
+
* order. The promises run already: this function starts nothing. Each result has the index of its
|
|
4224
|
+
* promise in the input. A rejected promise gives `status: "rejected"` and its reason: the
|
|
4225
|
+
* generator does not throw it. When the consumer stops early, the other promises continue, and
|
|
4226
|
+
* their rejections are not reported as unhandled.
|
|
4227
|
+
*/
|
|
4228
|
+
export declare function asSettled<T>(promises: Iterable<Promise<T>>): AsyncGenerator<Settled<T>, void, undefined>;
|
|
4229
|
+
/**
|
|
4230
|
+
* Functional operations for iterables (lazy sync transforms).
|
|
4231
|
+
*
|
|
4232
|
+
* This module gives Stream-like operations with lazy evaluation.
|
|
4233
|
+
* All operations return generators for composability.
|
|
4234
|
+
*
|
|
4235
|
+
* @example
|
|
4236
|
+
* const result = pipe(
|
|
4237
|
+
* [1, 2, 3, 4, 5],
|
|
4238
|
+
* map(x => x * 2),
|
|
4239
|
+
* filter(x => x > 5),
|
|
4240
|
+
* take(2),
|
|
4241
|
+
* toArray
|
|
4242
|
+
* ) // [6, 8]
|
|
4243
|
+
*/
|
|
4244
|
+
/**
|
|
4245
|
+
* This function maps each element through a function.
|
|
4246
|
+
*
|
|
4247
|
+
* @example
|
|
4248
|
+
* const doubled = map([1, 2, 3], x => x * 2) // [2, 4, 6]
|
|
4249
|
+
*/
|
|
4250
|
+
export declare function map<T, U>(iterable: Iterable<T>, fn: (value: T, index: number) => U): Generator<U>;
|
|
4251
|
+
/**
|
|
4252
|
+
* This function filters elements based on a predicate.
|
|
4253
|
+
*/
|
|
4254
|
+
export declare function filter<T>(iterable: Iterable<T>, predicate: (value: T, index: number) => boolean): Generator<T>;
|
|
4255
|
+
/**
|
|
4256
|
+
* This function flatMaps each element to an iterable and flattens.
|
|
4257
|
+
*/
|
|
4258
|
+
export declare function flatMap<T, U>(iterable: Iterable<T>, fn: (value: T, index: number) => Iterable<U>): Generator<U>;
|
|
4259
|
+
/**
|
|
4260
|
+
* This function flattens a nested iterable.
|
|
4261
|
+
*/
|
|
4262
|
+
export declare function flatten<T>(iterable: Iterable<Iterable<T>>): Generator<T>;
|
|
4263
|
+
/**
|
|
4264
|
+
* This function takes the first n elements.
|
|
4265
|
+
*/
|
|
4266
|
+
export declare function take<T>(iterable: Iterable<T>, n: number): Generator<T>;
|
|
4267
|
+
/**
|
|
4268
|
+
* This function skips the first n elements.
|
|
4269
|
+
*/
|
|
4270
|
+
export declare function skip<T>(iterable: Iterable<T>, n: number): Generator<T>;
|
|
4271
|
+
/**
|
|
4272
|
+
* This function takes elements while predicate is true.
|
|
4273
|
+
*/
|
|
4274
|
+
export declare function takeWhile<T>(iterable: Iterable<T>, predicate: (value: T) => boolean): Generator<T>;
|
|
4275
|
+
/**
|
|
4276
|
+
* This function skips elements while predicate is true.
|
|
4277
|
+
*/
|
|
4278
|
+
export declare function skipWhile<T>(iterable: Iterable<T>, predicate: (value: T) => boolean): Generator<T>;
|
|
4279
|
+
/**
|
|
4280
|
+
* Concatenates multiple iterables.
|
|
4281
|
+
*/
|
|
4282
|
+
export declare function concat<T>(...iterables: Iterable<T>[]): Generator<T>;
|
|
4283
|
+
/**
|
|
4284
|
+
* Zips multiple iterables together.
|
|
4285
|
+
*/
|
|
4286
|
+
export declare function zip<T extends any[]>(...iterables: { [K in keyof T]: Iterable<T[K]>; }): Generator<T>;
|
|
4287
|
+
/**
|
|
4288
|
+
* Zips with index.
|
|
4289
|
+
*/
|
|
4290
|
+
export declare function enumerate<T>(iterable: Iterable<T>): Generator<[number, T]>;
|
|
4291
|
+
/**
|
|
4292
|
+
* Chunks elements into arrays of specified size.
|
|
4293
|
+
*/
|
|
4294
|
+
export declare function chunk<T>(iterable: Iterable<T>, size: number): Generator<T[]>;
|
|
4295
|
+
/**
|
|
4296
|
+
* This function makes a sliding window of specified size.
|
|
4297
|
+
*/
|
|
4298
|
+
export declare function window<T>(iterable: Iterable<T>, size: number): Generator<T[]>;
|
|
4299
|
+
/**
|
|
4300
|
+
* Partitions elements into two arrays based on predicate.
|
|
4301
|
+
*/
|
|
4302
|
+
export declare function partition<T>(iterable: Iterable<T>, predicate: (value: T) => boolean): [T[], T[]];
|
|
4303
|
+
/**
|
|
4304
|
+
* This function reduces iterable to a single value.
|
|
4305
|
+
*/
|
|
4306
|
+
export declare function reduce<T, U>(iterable: Iterable<T>, fn: (acc: U, value: T, index: number) => U, initial: U): U;
|
|
4307
|
+
/**
|
|
4308
|
+
* Scans (like reduce but emits intermediate results).
|
|
4309
|
+
*/
|
|
4310
|
+
export declare function scan<T, U>(iterable: Iterable<T>, fn: (acc: U, value: T) => U, initial: U): Generator<U>;
|
|
4311
|
+
/**
|
|
4312
|
+
* This function sorts elements (materializes the iterable).
|
|
4313
|
+
*/
|
|
4314
|
+
export declare function sort<T>(iterable: Iterable<T>, compare?: (a: T, b: T) => number): Generator<T>;
|
|
4315
|
+
/**
|
|
4316
|
+
* Reverses elements (materializes the iterable).
|
|
4317
|
+
*/
|
|
4318
|
+
export declare function reverse<T>(iterable: Iterable<T>): Generator<T>;
|
|
4319
|
+
/**
|
|
4320
|
+
* This function removes duplicate elements (by reference or key function).
|
|
4321
|
+
*/
|
|
4322
|
+
export declare function distinct<T, K = T>(iterable: Iterable<T>, keyFn?: (value: T) => K): Generator<T>;
|
|
4323
|
+
/**
|
|
4324
|
+
* This function removes consecutive duplicates.
|
|
4325
|
+
*/
|
|
4326
|
+
export declare function distinctConsecutive<T, K = T>(iterable: Iterable<T>, keyFn?: (value: T) => K): Generator<T>;
|
|
4327
|
+
/**
|
|
4328
|
+
* This function does a side effect for each element. The stream does not change.
|
|
4329
|
+
*/
|
|
4330
|
+
export declare function tap<T>(iterable: Iterable<T>, fn: (value: T, index: number) => void): Generator<T>;
|
|
4331
|
+
/**
|
|
4332
|
+
* This function gives true if any element matches the predicate.
|
|
4333
|
+
*/
|
|
4334
|
+
export declare function some<T>(iterable: Iterable<T>, predicate: (value: T) => boolean): boolean;
|
|
4335
|
+
/**
|
|
4336
|
+
* This function gives true if all elements match the predicate.
|
|
4337
|
+
*/
|
|
4338
|
+
export declare function every<T>(iterable: Iterable<T>, predicate: (value: T) => boolean): boolean;
|
|
4339
|
+
/**
|
|
4340
|
+
* This function gives true if no elements match the predicate.
|
|
4341
|
+
*/
|
|
4342
|
+
export declare function none<T>(iterable: Iterable<T>, predicate: (value: T) => boolean): boolean;
|
|
4343
|
+
/**
|
|
4344
|
+
* This function finds the first element matching the predicate.
|
|
4345
|
+
*/
|
|
4346
|
+
export declare function find<T>(iterable: Iterable<T>, predicate: (value: T) => boolean): T | undefined;
|
|
4347
|
+
/**
|
|
4348
|
+
* This function counts elements (optionally matching a predicate).
|
|
4349
|
+
*/
|
|
4350
|
+
export declare function count<T>(iterable: Iterable<T>, predicate?: (value: T) => boolean): number;
|
|
4351
|
+
/**
|
|
4352
|
+
* This function gives the minimum element.
|
|
4353
|
+
*/
|
|
4354
|
+
export declare function min<T>(iterable: Iterable<T>, compare?: (a: T, b: T) => number): T | undefined;
|
|
4355
|
+
/**
|
|
4356
|
+
* This function gives the maximum element.
|
|
4357
|
+
*/
|
|
4358
|
+
export declare function max<T>(iterable: Iterable<T>, compare?: (a: T, b: T) => number): T | undefined;
|
|
4359
|
+
/**
|
|
4360
|
+
* This function gives the sum of elements.
|
|
4361
|
+
*/
|
|
4362
|
+
export declare function sum(iterable: Iterable<number>): number;
|
|
4363
|
+
/**
|
|
4364
|
+
* This function gives the average of elements.
|
|
4365
|
+
*/
|
|
4366
|
+
export declare function average(iterable: Iterable<number>): number | undefined;
|
|
4367
|
+
/**
|
|
4368
|
+
* This function collects elements into an array.
|
|
4369
|
+
*/
|
|
4370
|
+
declare function toArray$1<T>(iterable: Iterable<T>): T[];
|
|
4371
|
+
/**
|
|
4372
|
+
* This function collects elements into a Set.
|
|
4373
|
+
*/
|
|
4374
|
+
declare function toSet$1<T>(iterable: Iterable<T>): Set<T>;
|
|
4375
|
+
/**
|
|
4376
|
+
* This function collects elements into a Map.
|
|
4377
|
+
*/
|
|
4378
|
+
declare function toMap$1<T, K, V>(iterable: Iterable<T>, keyFn: (value: T) => K, valueFn: (value: T) => V): Map<K, V>;
|
|
4379
|
+
/**
|
|
4380
|
+
* This function pipes a value through a series of transformations.
|
|
4381
|
+
*
|
|
4382
|
+
* @example
|
|
4383
|
+
* const result = pipe(
|
|
4384
|
+
* [1, 2, 3, 4, 5],
|
|
4385
|
+
* (it) => map(it, x => x * 2),
|
|
4386
|
+
* (it) => filter(it, x => x > 5),
|
|
4387
|
+
* (it) => take(it, 2),
|
|
4388
|
+
* toArray
|
|
4389
|
+
* ) // [6, 8]
|
|
4390
|
+
*/
|
|
4391
|
+
export declare function pipe<T>(value: T, ...fns: Array<(value: any) => any>): any;
|
|
4392
|
+
/**
|
|
4393
|
+
* A Collector represents a mutable reduction operation.
|
|
4394
|
+
*
|
|
4395
|
+
* @template T - The type of input elements
|
|
4396
|
+
* @template A - The mutable accumulation type
|
|
4397
|
+
* @template R - The final result type
|
|
4398
|
+
*/
|
|
4399
|
+
export interface Collector<T, A, R> {
|
|
4400
|
+
/**
|
|
4401
|
+
* This value makes the initial accumulator.
|
|
4402
|
+
*/
|
|
4403
|
+
supplier: () => A;
|
|
4404
|
+
/**
|
|
4405
|
+
* This value incorporates an element into the accumulator and gives the next accumulator. A mutable
|
|
4406
|
+
* accumulator (a list, a map) gives itself. An immutable one (a count, a sum) gives the
|
|
4407
|
+
* new value. (Before, the result was ignored, so every immutable collector - counting,
|
|
4408
|
+
* summing, minBy, maxBy, reducing, first, last - given its seed: BUGS-2026-07 C12.)
|
|
4409
|
+
*/
|
|
4410
|
+
accumulator: (acc: A, element: T) => A;
|
|
4411
|
+
/**
|
|
4412
|
+
* This value combines two accumulators (for parallel streams).
|
|
4413
|
+
*/
|
|
4414
|
+
combiner?: (acc1: A, acc2: A) => A;
|
|
4415
|
+
/**
|
|
4416
|
+
* This value transforms the accumulator into the final result.
|
|
4417
|
+
*/
|
|
4418
|
+
finisher: (acc: A) => R;
|
|
4419
|
+
}
|
|
4420
|
+
/**
|
|
4421
|
+
* This function collects elements using a collector.
|
|
4422
|
+
*
|
|
4423
|
+
* @example
|
|
4424
|
+
* const list = collect([1, 2, 3], toList())
|
|
4425
|
+
* const set = collect([1, 2, 2, 3], toSet())
|
|
4426
|
+
*/
|
|
4427
|
+
export declare function collect<T, A, R>(iterable: Iterable<T>, collector: Collector<T, A, R>): R;
|
|
4428
|
+
/**
|
|
4429
|
+
* This function collects elements into an ArrayList.
|
|
4430
|
+
*/
|
|
4431
|
+
declare function toList<T>(): Collector<T, ArrayList<T>, ArrayList<T>>;
|
|
4432
|
+
/**
|
|
4433
|
+
* This function collects elements into a HashSet.
|
|
4434
|
+
*/
|
|
4435
|
+
declare function toSet<T>(): Collector<T, HashSet<T>, HashSet<T>>;
|
|
4436
|
+
/**
|
|
4437
|
+
* This function collects elements into a native Array.
|
|
4438
|
+
*/
|
|
4439
|
+
declare function toArray<T>(): Collector<T, T[], T[]>;
|
|
4440
|
+
/**
|
|
4441
|
+
* This function collects elements into a HashMap using key and value extractors.
|
|
4442
|
+
*/
|
|
4443
|
+
declare function toMap<T, K, V>(keyExtractor: (element: T) => K, valueExtractor: (element: T) => V): Collector<T, HashMap<K, V>, HashMap<K, V>>;
|
|
4444
|
+
/**
|
|
4445
|
+
* This function groups elements by a classifier function.
|
|
4446
|
+
*
|
|
4447
|
+
* @example
|
|
4448
|
+
* const byAge = collect(users, groupingBy(u => u.age))
|
|
4449
|
+
* // Map<number, ArrayList<User>>
|
|
4450
|
+
*/
|
|
4451
|
+
export declare function groupingBy<T, K>(classifier: (element: T) => K): Collector<T, HashMap<K, ArrayList<T>>, HashMap<K, ArrayList<T>>>;
|
|
4452
|
+
/**
|
|
4453
|
+
* Groups and transforms with a downstream collector.
|
|
4454
|
+
*
|
|
4455
|
+
* @example
|
|
4456
|
+
* const countByAge = collect(
|
|
4457
|
+
* users,
|
|
4458
|
+
* groupingBy(u => u.age, counting())
|
|
4459
|
+
* )
|
|
4460
|
+
* // Map<number, number>
|
|
4461
|
+
*/
|
|
4462
|
+
export declare function groupingByWith<T, K, A, D>(classifier: (element: T) => K, downstream: Collector<T, A, D>): Collector<T, HashMap<K, A>, HashMap<K, D>>;
|
|
4463
|
+
/**
|
|
4464
|
+
* Partitions elements into two groups based on a predicate.
|
|
4465
|
+
*
|
|
4466
|
+
* @example
|
|
4467
|
+
* const partitioned = collect(
|
|
4468
|
+
* numbers,
|
|
4469
|
+
* partitioningBy(n => n % 2 === 0)
|
|
4470
|
+
* )
|
|
4471
|
+
* // Map<boolean, ArrayList<number>>
|
|
4472
|
+
*/
|
|
4473
|
+
export declare function partitioningBy<T>(predicate: (element: T) => boolean): Collector<T, HashMap<boolean, ArrayList<T>>, HashMap<boolean, ArrayList<T>>>;
|
|
4474
|
+
/**
|
|
4475
|
+
* This function counts the number of elements.
|
|
4476
|
+
*/
|
|
4477
|
+
export declare function counting<T>(): Collector<T, number, number>;
|
|
4478
|
+
/**
|
|
4479
|
+
* Sums numeric elements.
|
|
4480
|
+
*/
|
|
4481
|
+
export declare function summingNumber<T>(mapper: (element: T) => number): Collector<T, number, number>;
|
|
4482
|
+
/**
|
|
4483
|
+
* Averages numeric elements.
|
|
4484
|
+
*/
|
|
4485
|
+
export declare function averagingNumber<T>(mapper: (element: T) => number): Collector<T, {
|
|
4486
|
+
sum: number;
|
|
4487
|
+
count: number;
|
|
4488
|
+
}, number>;
|
|
4489
|
+
/**
|
|
4490
|
+
* This function finds the minimum element.
|
|
4491
|
+
*/
|
|
4492
|
+
export declare function minBy<T>(comparator: (a: T, b: T) => number): Collector<T, T | undefined, T | undefined>;
|
|
4493
|
+
/**
|
|
4494
|
+
* This function finds the maximum element.
|
|
4495
|
+
*/
|
|
4496
|
+
export declare function maxBy<T>(comparator: (a: T, b: T) => number): Collector<T, T | undefined, T | undefined>;
|
|
4497
|
+
/**
|
|
4498
|
+
* Joins elements into a string.
|
|
4499
|
+
*
|
|
4500
|
+
* @example
|
|
4501
|
+
* const csv = collect(['a', 'b', 'c'], joining(', '))
|
|
4502
|
+
* // "a, b, c"
|
|
4503
|
+
*
|
|
4504
|
+
* const bracketed = collect(
|
|
4505
|
+
* ['a', 'b', 'c'],
|
|
4506
|
+
* joining(', ', '[', ']')
|
|
4507
|
+
* )
|
|
4508
|
+
* // "[a, b, c]"
|
|
4509
|
+
*/
|
|
4510
|
+
export declare function joining<T>(delimiter?: string, prefix?: string, suffix?: string): Collector<T, string[], string>;
|
|
4511
|
+
/**
|
|
4512
|
+
* This function applies a mapping function before collecting.
|
|
4513
|
+
*
|
|
4514
|
+
* @example
|
|
4515
|
+
* const names = collect(
|
|
4516
|
+
* users,
|
|
4517
|
+
* mapping(u => u.name, toList())
|
|
4518
|
+
* )
|
|
4519
|
+
*/
|
|
4520
|
+
export declare function mapping<T, U, A, R>(mapper: (element: T) => U, downstream: Collector<U, A, R>): Collector<T, A, R>;
|
|
4521
|
+
/**
|
|
4522
|
+
* This function filters elements before collecting.
|
|
4523
|
+
*/
|
|
4524
|
+
export declare function filtering<T, A, R>(predicate: (element: T) => boolean, downstream: Collector<T, A, R>): Collector<T, A, R>;
|
|
4525
|
+
/**
|
|
4526
|
+
* FlatMaps elements before collecting.
|
|
4527
|
+
*/
|
|
4528
|
+
export declare function flatMapping<T, U, A, R>(mapper: (element: T) => Iterable<U>, downstream: Collector<U, A, R>): Collector<T, A, R>;
|
|
4529
|
+
/**
|
|
4530
|
+
* General reduction collector.
|
|
4531
|
+
*/
|
|
4532
|
+
export declare function reducing<T>(identity: T, reducer: (acc: T, element: T) => T): Collector<T, T, T>;
|
|
4533
|
+
/**
|
|
4534
|
+
* Reducing with mapper.
|
|
4535
|
+
*/
|
|
4536
|
+
export declare function reducingWith<T, U>(identity: U, mapper: (element: T) => U, reducer: (acc: U, mapped: U) => U): Collector<T, U, U>;
|
|
4537
|
+
/**
|
|
4538
|
+
* This function collects with two collectors and combines results.
|
|
4539
|
+
*
|
|
4540
|
+
* @example
|
|
4541
|
+
* const stats = collect(
|
|
4542
|
+
* numbers,
|
|
4543
|
+
* teeing(
|
|
4544
|
+
* summingNumber(n => n),
|
|
4545
|
+
* counting(),
|
|
4546
|
+
* (sum, count) => ({ sum, count, avg: sum / count })
|
|
4547
|
+
* )
|
|
4548
|
+
* )
|
|
4549
|
+
*/
|
|
4550
|
+
export declare function teeing<T, A1, A2, R1, R2, R>(collector1: Collector<T, A1, R1>, collector2: Collector<T, A2, R2>, combiner: (r1: R1, r2: R2) => R): Collector<T, [A1, A2], R>;
|
|
4551
|
+
/**
|
|
4552
|
+
* This function collects the first element.
|
|
4553
|
+
*/
|
|
4554
|
+
export declare function first<T>(): Collector<T, T | undefined, T | undefined>;
|
|
4555
|
+
/**
|
|
4556
|
+
* This function collects the last element.
|
|
4557
|
+
*/
|
|
4558
|
+
export declare function last<T>(): Collector<T, T | undefined, T | undefined>;
|
|
4559
|
+
/**
|
|
4560
|
+
* This type makes summary statistics.
|
|
4561
|
+
*/
|
|
4562
|
+
export interface SummaryStatistics {
|
|
4563
|
+
count: number;
|
|
4564
|
+
sum: number;
|
|
4565
|
+
min: number;
|
|
4566
|
+
max: number;
|
|
4567
|
+
average: number;
|
|
4568
|
+
}
|
|
4569
|
+
export declare function summarizingNumber<T>(mapper: (element: T) => number): Collector<T, Partial<SummaryStatistics>, SummaryStatistics>;
|
|
4570
|
+
/**
|
|
4571
|
+
* Default equality using strict equality (===).
|
|
4572
|
+
* Works for primitives and reference equality for objects.
|
|
4573
|
+
*/
|
|
4574
|
+
export declare const defaultEq: Eq<any>;
|
|
4575
|
+
/**
|
|
4576
|
+
* Structural equality for objects (shallow comparison of properties).
|
|
4577
|
+
* This function compares own enumerable properties using ===.
|
|
4578
|
+
*/
|
|
4579
|
+
export declare const shallowEq: Eq<any>;
|
|
4580
|
+
/**
|
|
4581
|
+
* Deep structural equality (recursive comparison).
|
|
4582
|
+
* This function handles nested objects and arrays.
|
|
4583
|
+
*/
|
|
4584
|
+
export declare const deepEq: Eq<any>;
|
|
4585
|
+
/**
|
|
4586
|
+
* Equality by comparing a specific property.
|
|
4587
|
+
*
|
|
4588
|
+
* @example
|
|
4589
|
+
* const userEq = eqBy<User, 'id'>('id')
|
|
4590
|
+
* // or with accessor function:
|
|
4591
|
+
* const userEq = eqBy<User>((u) => u.id)
|
|
4592
|
+
*/
|
|
4593
|
+
export declare const eqBy: <T, K extends keyof T = keyof T>(keyOrFn: K | ((value: T) => any)) => Eq<T>;
|
|
4594
|
+
/**
|
|
4595
|
+
* Default hash function for primitives and objects.
|
|
4596
|
+
* For objects, hashes the JSON string representation.
|
|
4597
|
+
*/
|
|
4598
|
+
export declare const defaultHash: Hash<any>;
|
|
4599
|
+
/**
|
|
4600
|
+
* Hash function for strings using djb2 algorithm.
|
|
4601
|
+
*/
|
|
4602
|
+
export declare const hashString: (str: string) => number;
|
|
4603
|
+
/**
|
|
4604
|
+
* Hash function for numbers.
|
|
4605
|
+
*/
|
|
4606
|
+
export declare const hashNumber: (num: number) => number;
|
|
4607
|
+
/**
|
|
4608
|
+
* Combine multiple hash values.
|
|
4609
|
+
* Useful for hashing objects with multiple fields.
|
|
4610
|
+
*
|
|
4611
|
+
* @example
|
|
4612
|
+
* const pointHash: Hash<Point> = (p) =>
|
|
4613
|
+
* combineHashes(hashNumber(p.x), hashNumber(p.y))
|
|
4614
|
+
*/
|
|
4615
|
+
export declare const combineHashes: (...hashes: number[]) => number;
|
|
4616
|
+
/**
|
|
4617
|
+
* Make a hash function that hashes by a specific property.
|
|
4618
|
+
*
|
|
4619
|
+
* @example
|
|
4620
|
+
* const userHash = hashBy<User>((u) => u.id)
|
|
4621
|
+
*/
|
|
4622
|
+
export declare const hashBy: <T>(accessor: (value: T) => any) => Hash<T>;
|
|
4623
|
+
/**
|
|
4624
|
+
* Natural ordering comparison for primitives.
|
|
4625
|
+
* Numbers, strings, booleans, dates are compared naturally.
|
|
4626
|
+
*/
|
|
4627
|
+
export declare const defaultCompare: Compare<any>;
|
|
4628
|
+
/**
|
|
4629
|
+
* Numeric comparison (for numbers).
|
|
4630
|
+
*/
|
|
4631
|
+
export declare const numberCompare: Compare<number>;
|
|
4632
|
+
/**
|
|
4633
|
+
* String comparison using locale-aware sorting.
|
|
4634
|
+
*/
|
|
4635
|
+
export declare const stringCompare: Compare<string>;
|
|
4636
|
+
/**
|
|
4637
|
+
* Case-insensitive string comparison.
|
|
4638
|
+
*/
|
|
4639
|
+
export declare const stringCompareIgnoreCase: Compare<string>;
|
|
4640
|
+
/**
|
|
4641
|
+
* Date comparison.
|
|
4642
|
+
*/
|
|
4643
|
+
export declare const dateCompare: Compare<Date>;
|
|
4644
|
+
/**
|
|
4645
|
+
* Boolean comparison (false < true).
|
|
4646
|
+
*/
|
|
4647
|
+
export declare const booleanCompare: Compare<boolean>;
|
|
4648
|
+
/**
|
|
4649
|
+
* This function reverses a comparator.
|
|
4650
|
+
*
|
|
4651
|
+
* @example
|
|
4652
|
+
* const descending = reversed(numberCompare)
|
|
4653
|
+
*/
|
|
4654
|
+
export declare const reversed: <T>(compare: Compare<T>) => Compare<T>;
|
|
4655
|
+
/**
|
|
4656
|
+
* This function makes a comparator that compares by a specific property or function.
|
|
4657
|
+
*
|
|
4658
|
+
* @example
|
|
4659
|
+
* const byAge = comparing<Person>((p) => p.age)
|
|
4660
|
+
* const byName = comparing<Person, 'name'>('name')
|
|
4661
|
+
*/
|
|
4662
|
+
export declare const comparing: <T, K extends keyof T = keyof T>(keyOrFn: K | ((value: T) => any), compare?: Compare<any>) => Compare<T>;
|
|
4663
|
+
/**
|
|
4664
|
+
* Chains comparators. If the first comparator gives 0 (equal),
|
|
4665
|
+
* tries the next comparator, and so on.
|
|
4666
|
+
*
|
|
4667
|
+
* @example
|
|
4668
|
+
* const userCompare = thenComparing(
|
|
4669
|
+
* comparing<User>('lastName'),
|
|
4670
|
+
* comparing<User>('firstName'),
|
|
4671
|
+
* comparing<User>('age')
|
|
4672
|
+
* )
|
|
4673
|
+
*/
|
|
4674
|
+
export declare const thenComparing: <T>(...comparators: Compare<T>[]) => Compare<T>;
|
|
4675
|
+
/**
|
|
4676
|
+
* This function makes a comparator that treats null/undefined as less than all other values.
|
|
4677
|
+
*
|
|
4678
|
+
* @example
|
|
4679
|
+
* const compare = nullsFirst(numberCompare)
|
|
4680
|
+
*/
|
|
4681
|
+
export declare const nullsFirst: <T>(compare: Compare<T>) => Compare<T | null | undefined>;
|
|
4682
|
+
/**
|
|
4683
|
+
* This function makes a comparator that treats null/undefined as greater than all other values.
|
|
4684
|
+
*
|
|
4685
|
+
* @example
|
|
4686
|
+
* const compare = nullsLast(numberCompare)
|
|
4687
|
+
*/
|
|
4688
|
+
export declare const nullsLast: <T>(compare: Compare<T>) => Compare<T | null | undefined>;
|
|
4689
|
+
/**
|
|
4690
|
+
* This function makes a comparator from a key extraction function.
|
|
4691
|
+
* Elements are compared by their extracted keys.
|
|
4692
|
+
*
|
|
4693
|
+
* @example
|
|
4694
|
+
* const byLength = comparingKey<string>((s) => s.length)
|
|
4695
|
+
*/
|
|
4696
|
+
export declare const comparingKey: <T, K>(keyExtractor: (value: T) => K, keyCompare?: Compare<K>) => Compare<T>;
|
|
4697
|
+
export declare function emptyList<T>(): List<T>;
|
|
4698
|
+
export declare function emptyQueue<T>(): Queue<T>;
|
|
4699
|
+
export declare function emptyMap<K, V>(): MapLike<K, V>;
|
|
4700
|
+
/**
|
|
4701
|
+
* This function gives an immutable list containing only the specified element.
|
|
4702
|
+
*
|
|
4703
|
+
* @example
|
|
4704
|
+
* const list = singletonList(42)
|
|
4705
|
+
* list.size // 1
|
|
4706
|
+
* list.get(0) // 42
|
|
4707
|
+
* list.add(2) // Error: Cannot modify readonly collection
|
|
4708
|
+
*/
|
|
4709
|
+
export declare function singletonList<T>(element: T): List<T>;
|
|
4710
|
+
/**
|
|
4711
|
+
* This function gives an immutable map containing only the specified key-value mapping.
|
|
4712
|
+
*/
|
|
4713
|
+
export declare function singletonMap<K, V>(key: K, value: V): MapLike<K, V>;
|
|
4714
|
+
/**
|
|
4715
|
+
* This function gives an immutable list consisting of n copies of the specified element.
|
|
4716
|
+
*
|
|
4717
|
+
* @example
|
|
4718
|
+
* const list = nCopies(5, 'hello')
|
|
4719
|
+
* list.size // 5
|
|
4720
|
+
* list.get(2) // 'hello'
|
|
4721
|
+
*/
|
|
4722
|
+
export declare function nCopies<T>(n: number, element: T): List<T>;
|
|
4723
|
+
/**
|
|
4724
|
+
* This function makes a new ArrayList from the given elements.
|
|
4725
|
+
*
|
|
4726
|
+
* @example
|
|
4727
|
+
* const list = listOf(1, 2, 3, 4, 5)
|
|
4728
|
+
*/
|
|
4729
|
+
export declare function listOf<T>(...elements: T[]): ArrayList<T>;
|
|
4730
|
+
/**
|
|
4731
|
+
* This function makes a new ArrayDeque from the given elements.
|
|
4732
|
+
*/
|
|
4733
|
+
export declare function queueOf<T>(...elements: T[]): ArrayDeque<T>;
|
|
4734
|
+
/**
|
|
4735
|
+
* This function makes a new HashMap from the given key-value pairs.
|
|
4736
|
+
*
|
|
4737
|
+
* @example
|
|
4738
|
+
* const map = mapOf(['a', 1], ['b', 2], ['c', 3])
|
|
4739
|
+
*/
|
|
4740
|
+
export declare function mapOf<K, V>(...entries: [K, V][]): HashMap<K, V>;
|
|
4741
|
+
/**
|
|
4742
|
+
* This function makes a new mutable ArrayList with optional initial elements.
|
|
4743
|
+
*
|
|
4744
|
+
* @example
|
|
4745
|
+
* const list = mutableListOf(1, 2, 3)
|
|
4746
|
+
* list.add(4)
|
|
4747
|
+
*/
|
|
4748
|
+
export declare function mutableListOf<T>(...elements: T[]): ArrayList<T>;
|
|
4749
|
+
/**
|
|
4750
|
+
* This function makes a new mutable HashMap with optional initial entries.
|
|
4751
|
+
*/
|
|
4752
|
+
export declare function mutableMapOf<K, V>(...entries: [K, V][]): HashMap<K, V>;
|
|
4753
|
+
/**
|
|
4754
|
+
* This function makes an immutable List from the given elements.
|
|
4755
|
+
*
|
|
4756
|
+
* @example
|
|
4757
|
+
* const list = immutableListOf(1, 2, 3)
|
|
4758
|
+
* list.get(0) // 1
|
|
4759
|
+
* list.add(4) // Error: Cannot modify readonly collection
|
|
4760
|
+
*/
|
|
4761
|
+
export declare function immutableListOf<T>(...elements: T[]): List<T>;
|
|
4762
|
+
/**
|
|
4763
|
+
* This function makes an immutable Map from the given entries.
|
|
4764
|
+
*/
|
|
4765
|
+
export declare function immutableMapOf<K, V>(...entries: [K, V][]): MapLike<K, V>;
|
|
4766
|
+
/**
|
|
4767
|
+
* This function makes a list containing integers from start (inclusive) to end (exclusive).
|
|
4768
|
+
*
|
|
4769
|
+
* @example
|
|
4770
|
+
* const list = range(0, 5) // [0, 1, 2, 3, 4]
|
|
4771
|
+
* const list2 = range(10, 15) // [10, 11, 12, 13, 14]
|
|
4772
|
+
*/
|
|
4773
|
+
export declare function range(start: number, end: number, step?: number): ArrayList<number>;
|
|
4774
|
+
/**
|
|
4775
|
+
* This function makes a list containing integers from 0 to n (exclusive).
|
|
4776
|
+
*
|
|
4777
|
+
* @example
|
|
4778
|
+
* const list = rangeTo(5) // [0, 1, 2, 3, 4]
|
|
4779
|
+
*/
|
|
4780
|
+
export declare function rangeTo(n: number): ArrayList<number>;
|
|
4781
|
+
/**
|
|
4782
|
+
* Builder for constructing lists with a fluent API.
|
|
4783
|
+
*
|
|
4784
|
+
* @example
|
|
4785
|
+
* const list = buildList<number>((builder) => {
|
|
4786
|
+
* builder.add(1)
|
|
4787
|
+
* builder.add(2)
|
|
4788
|
+
* if (someCondition) {
|
|
4789
|
+
* builder.add(3)
|
|
4790
|
+
* }
|
|
4791
|
+
* })
|
|
4792
|
+
*/
|
|
4793
|
+
export declare function buildList<T>(builder: (list: ArrayList<T>) => void): ArrayList<T>;
|
|
4794
|
+
/**
|
|
4795
|
+
* Builder for constructing maps with a fluent API.
|
|
4796
|
+
*/
|
|
4797
|
+
export declare function buildMap<K, V>(builder: (map: HashMap<K, V>) => void): HashMap<K, V>;
|
|
4798
|
+
/**
|
|
4799
|
+
* This function converts an iterable to an ArrayList.
|
|
4800
|
+
*
|
|
4801
|
+
* @example
|
|
4802
|
+
* const set = new Set([1, 2, 3])
|
|
4803
|
+
* const list = toList(set)
|
|
4804
|
+
*/
|
|
4805
|
+
declare function toList$1<T>(iterable: Iterable<T>): ArrayList<T>;
|
|
4806
|
+
/**
|
|
4807
|
+
* This function converts an iterable to a HashMap using a key extractor function.
|
|
4808
|
+
*
|
|
4809
|
+
* @example
|
|
4810
|
+
* const users = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]
|
|
4811
|
+
* const userMap = toMap(users, u => u.id)
|
|
4812
|
+
*/
|
|
4813
|
+
declare function toMap$2<T, K>(iterable: Iterable<T>, keyExtractor: (value: T) => K): HashMap<K, T>;
|
|
4814
|
+
declare function toMap$2<T, K, V>(iterable: Iterable<T>, keyExtractor: (value: T) => K, valueExtractor: (value: T) => V): HashMap<K, V>;
|
|
4815
|
+
/**
|
|
4816
|
+
* This function groups elements by a key extractor function.
|
|
4817
|
+
*
|
|
4818
|
+
* @example
|
|
4819
|
+
* const users = [
|
|
4820
|
+
* { age: 25, name: 'Alice' },
|
|
4821
|
+
* { age: 30, name: 'Bob' },
|
|
4822
|
+
* { age: 25, name: 'Charlie' }
|
|
4823
|
+
* ]
|
|
4824
|
+
* const byAge = groupBy(users, u => u.age)
|
|
4825
|
+
* // Map { 25 => [Alice, Charlie], 30 => [Bob] }
|
|
4826
|
+
*/
|
|
4827
|
+
export declare function groupBy<T, K>(iterable: Iterable<T>, keyExtractor: (value: T) => K): HashMap<K, ArrayList<T>>;
|
|
4828
|
+
/**
|
|
4829
|
+
* Checks if an index is within valid bounds [0, size).
|
|
4830
|
+
* This function throws RangeError if out of bounds.
|
|
4831
|
+
*
|
|
4832
|
+
* @param index The index to check
|
|
4833
|
+
* @param size The size of the collection
|
|
4834
|
+
* @param collectionName Optional name for error message
|
|
4835
|
+
* @throws RangeError if index is out of bounds
|
|
4836
|
+
*/
|
|
4837
|
+
export declare function checkBounds(index: number, size: number, collectionName?: string): void;
|
|
4838
|
+
/**
|
|
4839
|
+
* Checks if an index is valid for insertion [0, size].
|
|
4840
|
+
* In this function, index === size is correct for append operations.
|
|
4841
|
+
*
|
|
4842
|
+
* @param index The index to check
|
|
4843
|
+
* @param size The size of the collection
|
|
4844
|
+
* @param collectionName Optional name for error message
|
|
4845
|
+
* @throws RangeError if index is out of bounds
|
|
4846
|
+
*/
|
|
4847
|
+
export declare function checkInsertBounds(index: number, size: number, collectionName?: string): void;
|
|
4848
|
+
/**
|
|
4849
|
+
* Checks if a range [start, end) is valid.
|
|
4850
|
+
*
|
|
4851
|
+
* @param start Start index (inclusive)
|
|
4852
|
+
* @param end End index (exclusive)
|
|
4853
|
+
* @param size The size of the collection
|
|
4854
|
+
* @param collectionName Optional name for error message
|
|
4855
|
+
* @throws RangeError if range is invalid
|
|
4856
|
+
*/
|
|
4857
|
+
export declare function checkRangeBounds(start: number, end: number, size: number, collectionName?: string): void;
|
|
4858
|
+
/**
|
|
4859
|
+
* This function finds the index of an element in an array using custom equality.
|
|
4860
|
+
*
|
|
4861
|
+
* @param array The array to search
|
|
4862
|
+
* @param element The element to find
|
|
4863
|
+
* @param eq Equality function
|
|
4864
|
+
* @returns Index of element, or -1 if not found
|
|
4865
|
+
*/
|
|
4866
|
+
export declare function findIndex<T>(array: T[], element: T, eq: Eq<T>): number;
|
|
4867
|
+
/**
|
|
4868
|
+
* This function finds the last index of an element in an array using custom equality.
|
|
4869
|
+
*
|
|
4870
|
+
* @param array The array to search
|
|
4871
|
+
* @param element The element to find
|
|
4872
|
+
* @param eq Equality function
|
|
4873
|
+
* @returns Last index of element, or -1 if not found
|
|
4874
|
+
*/
|
|
4875
|
+
export declare function findLastIndex<T>(array: T[], element: T, eq: Eq<T>): number;
|
|
4876
|
+
/**
|
|
4877
|
+
* Checks if an array contains an element using custom equality.
|
|
4878
|
+
*
|
|
4879
|
+
* @param array The array to search
|
|
4880
|
+
* @param element The element to find
|
|
4881
|
+
* @param eq Equality function
|
|
4882
|
+
* @returns true if element is found
|
|
4883
|
+
*/
|
|
4884
|
+
export declare function arrayContains<T>(array: T[], element: T, eq: Eq<T>): boolean;
|
|
4885
|
+
/**
|
|
4886
|
+
* This function removes the first occurrence of an element from an array.
|
|
4887
|
+
*
|
|
4888
|
+
* @param array The array to change
|
|
4889
|
+
* @param element The element to remove
|
|
4890
|
+
* @param eq Equality function
|
|
4891
|
+
* @returns true if element was found and removed
|
|
4892
|
+
*/
|
|
4893
|
+
export declare function arrayRemove<T>(array: T[], element: T, eq: Eq<T>): boolean;
|
|
4894
|
+
/**
|
|
4895
|
+
* This function removes all occurrences of an element from an array.
|
|
4896
|
+
*
|
|
4897
|
+
* @param array The array to change
|
|
4898
|
+
* @param element The element to remove
|
|
4899
|
+
* @param eq Equality function
|
|
4900
|
+
* @returns Number of elements removed
|
|
4901
|
+
*/
|
|
4902
|
+
export declare function arrayRemoveAll<T>(array: T[], element: T, eq: Eq<T>): number;
|
|
4903
|
+
/**
|
|
4904
|
+
* This function makes a simple iterator for an array.
|
|
4905
|
+
*
|
|
4906
|
+
* @param array The array to iterate
|
|
4907
|
+
* @returns Iterator over the array
|
|
4908
|
+
*/
|
|
4909
|
+
export declare function arrayIterator<T>(array: T[]): Iterator<T>;
|
|
4910
|
+
/**
|
|
4911
|
+
* This function makes a reversed iterator for an array.
|
|
4912
|
+
*
|
|
4913
|
+
* @param array The array to iterate
|
|
4914
|
+
* @returns Reversed iterator over the array
|
|
4915
|
+
*/
|
|
4916
|
+
export declare function reverseArrayIterator<T>(array: T[]): Iterator<T>;
|
|
4917
|
+
/**
|
|
4918
|
+
* This function makes sure that a value is not null or undefined.
|
|
4919
|
+
*
|
|
4920
|
+
* @param value The value to check
|
|
4921
|
+
* @param message Error message
|
|
4922
|
+
* @returns The non-null value
|
|
4923
|
+
* @throws Error if value is null or undefined
|
|
4924
|
+
*/
|
|
4925
|
+
export declare function requireNonNull<T>(value: T | null | undefined, message?: string): T;
|
|
4926
|
+
/**
|
|
4927
|
+
* This function makes sure that a collection is not empty.
|
|
4928
|
+
*
|
|
4929
|
+
* @param size Current size
|
|
4930
|
+
* @param collectionName Optional name for error message
|
|
4931
|
+
* @throws Error if collection is empty
|
|
4932
|
+
*/
|
|
4933
|
+
export declare function requireNonEmpty(size: number, collectionName?: string): void;
|
|
4934
|
+
/**
|
|
4935
|
+
* This function clamps a value between min and max.
|
|
4936
|
+
*
|
|
4937
|
+
* @param value The value to clamp
|
|
4938
|
+
* @param min Minimum value
|
|
4939
|
+
* @param max Maximum value
|
|
4940
|
+
* @returns Clamped value
|
|
4941
|
+
*/
|
|
4942
|
+
export declare function clamp(value: number, min: number, max: number): number;
|
|
4943
|
+
/**
|
|
4944
|
+
* This function calculates hash code for a string.
|
|
4945
|
+
* This function uses Java's String.hashCode() algorithm.
|
|
4946
|
+
*
|
|
4947
|
+
* @param str The string to hash
|
|
4948
|
+
* @returns Hash code
|
|
4949
|
+
*/
|
|
4950
|
+
export declare function stringHashCode(str: string): number;
|
|
4951
|
+
export { type BlockingQueue, type BoundedOptions, type DelayQueue, type Deque, type AsyncQueue$1 as IAsyncQueue, type PriorityQueue as IPriorityQueue, type KeyCheckOptions, type KeyedItem, type ListenerErrorHandler, type Point, PriorityQueue$1 as PriorityQueue, type Queue, type ReadonlyQueue, Set$1 as Set, type SummaryLocation, type SummaryMonoid, type SummarySearch, type SummarySpec, type TransferQueue, toArray as collectToArray, toList as collectToList, toMap as collectToMap, toSet as collectToSet, toMap$1 as iterToMap, toArray$1 as toArray, toList$1 as toList, toMap$2 as toMap, toSet$1 as toSet };
|
|
4952
|
+
//# sourceMappingURL=index.d.ts.map
|