@ac-kit/data 0.3.3 → 0.5.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/CHANGELOG.md +106 -0
- package/README.md +36 -3
- package/dist/bit/bit-set.d.ts.map +1 -1
- package/dist/bit/bit-set.js +3 -3
- package/dist/bit/bit-set.js.map +1 -1
- package/dist/bit/bit-vector.js +2 -2
- package/dist/bit/bit-vector.js.map +1 -1
- package/dist/cache/_lru-store.d.ts +4 -0
- package/dist/cache/_lru-store.d.ts.map +1 -1
- package/dist/cache/_lru-store.js +16 -1
- package/dist/cache/_lru-store.js.map +1 -1
- package/dist/cache/lru-map.d.ts +16 -0
- package/dist/cache/lru-map.d.ts.map +1 -1
- package/dist/cache/lru-map.js +20 -0
- package/dist/cache/lru-map.js.map +1 -1
- package/dist/cache/ttl-map.d.ts.map +1 -1
- package/dist/cache/ttl-map.js +3 -3
- package/dist/cache/ttl-map.js.map +1 -1
- package/dist/collection/_bounded.d.ts.map +1 -1
- package/dist/collection/_bounded.js +2 -1
- package/dist/collection/_bounded.js.map +1 -1
- package/dist/collection/_capacity-semaphore.d.ts.map +1 -1
- package/dist/collection/_capacity-semaphore.js +4 -2
- package/dist/collection/_capacity-semaphore.js.map +1 -1
- package/dist/collection/concurrent-modification-error.d.ts +11 -0
- package/dist/collection/concurrent-modification-error.d.ts.map +1 -0
- package/dist/collection/concurrent-modification-error.js +14 -0
- package/dist/collection/concurrent-modification-error.js.map +1 -0
- package/dist/collection/ibounded.d.ts +6 -1
- package/dist/collection/ibounded.d.ts.map +1 -1
- package/dist/collection/ibounded.js +7 -2
- package/dist/collection/ibounded.js.map +1 -1
- package/dist/disjoint-set/static-disjoint-set.js.map +1 -1
- package/dist/graph/directed-graph.js.map +1 -1
- package/dist/graph/graph.js.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/multiset/multi-set.d.ts +1 -1
- package/dist/multiset/multi-set.d.ts.map +1 -1
- package/dist/multiset/multi-set.js +2 -1
- package/dist/multiset/multi-set.js.map +1 -1
- package/dist/priority-queue/priority-queue.js.map +1 -1
- package/dist/probabilistic/bloom-filter.js +3 -3
- package/dist/probabilistic/bloom-filter.js.map +1 -1
- package/dist/probabilistic/count-min-sketch.d.ts.map +1 -1
- package/dist/probabilistic/count-min-sketch.js +4 -3
- package/dist/probabilistic/count-min-sketch.js.map +1 -1
- package/dist/probabilistic/cuckoo-filter.d.ts.map +1 -1
- package/dist/probabilistic/cuckoo-filter.js +3 -2
- package/dist/probabilistic/cuckoo-filter.js.map +1 -1
- package/dist/probabilistic/hyper-log-log.d.ts.map +1 -1
- package/dist/probabilistic/hyper-log-log.js +2 -1
- package/dist/probabilistic/hyper-log-log.js.map +1 -1
- package/dist/probabilistic/top-k.d.ts.map +1 -1
- package/dist/probabilistic/top-k.js +2 -1
- package/dist/probabilistic/top-k.js.map +1 -1
- package/dist/queue/iweight-bounded-queue.d.ts +290 -0
- package/dist/queue/iweight-bounded-queue.d.ts.map +1 -0
- package/dist/queue/iweight-bounded-queue.js +19 -0
- package/dist/queue/iweight-bounded-queue.js.map +1 -0
- package/dist/queue/weight-bounded-queue.d.ts +83 -0
- package/dist/queue/weight-bounded-queue.d.ts.map +1 -0
- package/dist/queue/weight-bounded-queue.js +429 -0
- package/dist/queue/weight-bounded-queue.js.map +1 -0
- package/dist/similarity/vp-tree.d.ts +8 -13
- package/dist/similarity/vp-tree.d.ts.map +1 -1
- package/dist/similarity/vp-tree.js +6 -2
- package/dist/similarity/vp-tree.js.map +1 -1
- package/dist/storage/ring-vector.d.ts.map +1 -1
- package/dist/storage/ring-vector.js +3 -3
- package/dist/storage/ring-vector.js.map +1 -1
- package/dist/storage/segmented-ring-vector.d.ts.map +1 -1
- package/dist/storage/segmented-ring-vector.js +8 -13
- package/dist/storage/segmented-ring-vector.js.map +1 -1
- package/dist/storage/typed-column-ring.d.ts +132 -0
- package/dist/storage/typed-column-ring.d.ts.map +1 -0
- package/dist/storage/typed-column-ring.js +218 -0
- package/dist/storage/typed-column-ring.js.map +1 -0
- package/dist/storage/typed-vector.d.ts.map +1 -1
- package/dist/storage/typed-vector.js +3 -2
- package/dist/storage/typed-vector.js.map +1 -1
- package/dist/substring/generalized-suffix-array.js.map +1 -1
- package/dist/tree/binary-search-tree.js.map +1 -1
- package/package.json +4 -3
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
import type { DefinedValue, Predicate } from "@ac-kit/core";
|
|
2
|
+
import { CollectionCapacityExceededError } from "../collection/ibounded.js";
|
|
3
|
+
import { IQueue } from "./iqueue.js";
|
|
4
|
+
/**
|
|
5
|
+
* What a weight-bounded queue does with an item heavier than its whole weight
|
|
6
|
+
* limit, which no amount of draining makes room for.
|
|
7
|
+
*
|
|
8
|
+
* - `"reject"` never admits it: `tryEnqueue` answers `"oversized"`, so a memory
|
|
9
|
+
* budget is never exceeded.
|
|
10
|
+
* - `"alone"` admits it only into an otherwise empty queue (nothing queued,
|
|
11
|
+
* nothing reserved), and admits nothing beside it until it has left, so a
|
|
12
|
+
* producer that must move every item through the queue cannot stall on one.
|
|
13
|
+
* While it is held the occupied weight exceeds `maxWeight`, by that item
|
|
14
|
+
* alone.
|
|
15
|
+
*/
|
|
16
|
+
export type OversizedItemPolicy = "reject" | "alone";
|
|
17
|
+
/**
|
|
18
|
+
* The answer of `IWeightBoundedQueue.tryEnqueue` and `tryEnqueueAll`.
|
|
19
|
+
*
|
|
20
|
+
* - `"enqueued"`: everything was added.
|
|
21
|
+
* - `"full"`: nothing was added, and it would be admitted into an empty queue
|
|
22
|
+
* with no reservation — retry once room is freed.
|
|
23
|
+
* - `"oversized"`: nothing was added, and it would not be admitted even into an
|
|
24
|
+
* empty queue: more items than `maxCount`, or more weight than `maxWeight`
|
|
25
|
+
* other than one item under the `"alone"` policy. Retrying cannot succeed.
|
|
26
|
+
*/
|
|
27
|
+
export type WeightBoundedEnqueueOutcome = "enqueued" | "full" | "oversized";
|
|
28
|
+
/** Constructor options of a weight-bounded queue. */
|
|
29
|
+
export type WeightBoundedQueueOptions = {
|
|
30
|
+
/**
|
|
31
|
+
* The most items the queue may hold, reservations included: a non-negative
|
|
32
|
+
* safe integer. Default: no count limit.
|
|
33
|
+
*/
|
|
34
|
+
readonly maxCount?: number;
|
|
35
|
+
/** Default `"reject"`. */
|
|
36
|
+
readonly oversizedItemPolicy?: OversizedItemPolicy;
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Error thrown by `IWeightBoundedQueue.enqueue` and `enqueueAll` when the
|
|
40
|
+
* weight limit refuses the insertion; the count limit throws the base
|
|
41
|
+
* {@link CollectionCapacityExceededError}.
|
|
42
|
+
*/
|
|
43
|
+
export declare class WeightCapacityExceededError extends CollectionCapacityExceededError {
|
|
44
|
+
/** The queue's weight limit. */
|
|
45
|
+
readonly maxWeight: number;
|
|
46
|
+
/** The weight the insertion asked for. */
|
|
47
|
+
readonly requestedWeight: number;
|
|
48
|
+
constructor(maxWeight: number, requestedWeight: number);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* A queued entry of a weight-bounded queue: a handle that removes this one
|
|
52
|
+
* entry in `O(1)`, wherever it is in the queue.
|
|
53
|
+
*
|
|
54
|
+
* @template T The type of elements in the queue.
|
|
55
|
+
*/
|
|
56
|
+
export interface IWeightBoundedQueueEntry<T extends DefinedValue> {
|
|
57
|
+
/** The queued item. */
|
|
58
|
+
readonly item: T;
|
|
59
|
+
/** The item's weight, read once from `weightOf` when it was enqueued. */
|
|
60
|
+
readonly weight: number;
|
|
61
|
+
/** Whether the entry is still in the queue. */
|
|
62
|
+
readonly isQueued: boolean;
|
|
63
|
+
/**
|
|
64
|
+
* Removes the entry from the queue, freeing its count and weight.
|
|
65
|
+
*
|
|
66
|
+
* Complexity: `O(1)`.
|
|
67
|
+
*
|
|
68
|
+
* @returns `true` if it was queued, `false` if it had already left.
|
|
69
|
+
*/
|
|
70
|
+
remove(): boolean;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* An item taken off the front of a weight-bounded queue whose count and weight
|
|
74
|
+
* still occupy the queue's limits — the item in flight while a consumer
|
|
75
|
+
* delivers it.
|
|
76
|
+
*
|
|
77
|
+
* End it with exactly one of `release` (done, free the room) or `requeue`
|
|
78
|
+
* (failed, put it back); `[Symbol.dispose]` releases, so `using` ends it on
|
|
79
|
+
* every path.
|
|
80
|
+
*
|
|
81
|
+
* @template T The type of elements in the queue.
|
|
82
|
+
*/
|
|
83
|
+
export interface IWeightBoundedQueueReservation<T extends DefinedValue> extends Disposable {
|
|
84
|
+
/** The reserved item. */
|
|
85
|
+
readonly item: T;
|
|
86
|
+
/** The item's weight, read once from `weightOf` when it was enqueued. */
|
|
87
|
+
readonly weight: number;
|
|
88
|
+
/** Whether the reservation is still held. */
|
|
89
|
+
readonly isReserved: boolean;
|
|
90
|
+
/**
|
|
91
|
+
* Ends the reservation and frees its count and weight.
|
|
92
|
+
*
|
|
93
|
+
* Complexity: `O(1)`.
|
|
94
|
+
*
|
|
95
|
+
* @returns `true` if it was held, `false` if it had already ended.
|
|
96
|
+
*/
|
|
97
|
+
release(): boolean;
|
|
98
|
+
/**
|
|
99
|
+
* Ends the reservation by putting the item back in the queue, at the place
|
|
100
|
+
* its enqueue order gives it — ahead of everything enqueued after it, so a
|
|
101
|
+
* failed delivery is retried first. It always fits: its room was held.
|
|
102
|
+
*
|
|
103
|
+
* Complexity: `O(1 + r)`, where `r` is the number of requeued entries
|
|
104
|
+
* enqueued before it that are still queued ahead of it.
|
|
105
|
+
*
|
|
106
|
+
* @returns `true` if it was held, `false` if it had already ended.
|
|
107
|
+
*/
|
|
108
|
+
requeue(): boolean;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* A FIFO queue bounded by the total weight of what it holds — bytes, tokens,
|
|
112
|
+
* cost units — and optionally by a count, with selective removal and
|
|
113
|
+
* reservation of in-flight items.
|
|
114
|
+
*
|
|
115
|
+
* **Weights.** `weightOf(item)` is called once per item, when it is enqueued,
|
|
116
|
+
* and must return a non-negative safe integer; the queue keeps that value, so
|
|
117
|
+
* an item whose weight later changes does not corrupt the totals. Integer
|
|
118
|
+
* weights keep every total exact (a sum of floats drifts under removal); a
|
|
119
|
+
* fractional measure is scaled to an integer unit first, as Guava's and
|
|
120
|
+
* Caffeine's weighers do.
|
|
121
|
+
*
|
|
122
|
+
* **Limits.** Reserved items occupy the limits like queued ones. An insertion
|
|
123
|
+
* of `k` items of total weight `w` is admitted when `count() + reservedCount() +
|
|
124
|
+
* k ≤ maxCount` and `weight() + reservedWeight() + w ≤ maxWeight` — both limits
|
|
125
|
+
* are inclusive. An item heavier than `maxWeight` follows the
|
|
126
|
+
* {@link OversizedItemPolicy}. A batch is all or nothing.
|
|
127
|
+
*
|
|
128
|
+
* **Order.** Entries always stand in the order they were first enqueued:
|
|
129
|
+
* `dequeue` and `reserve` take the oldest, and a requeued reservation returns
|
|
130
|
+
* to its own place.
|
|
131
|
+
*
|
|
132
|
+
* **Traversal.** Iteration runs front to back over queued items only
|
|
133
|
+
* (reservations are not in the queue). It is fail-fast: the next step throws
|
|
134
|
+
* `ConcurrentModificationError` after any change to the queued entries, except
|
|
135
|
+
* the removal of the entry it last yielded (through its handle, `dequeue`,
|
|
136
|
+
* `reserve` or `removeFirst`). Releasing a reservation is not a change to the
|
|
137
|
+
* queued entries. A predicate passed to `find`, `findEntry`, `removeFirst` or
|
|
138
|
+
* `removeWhere` must not change the queue either; if it does, the search throws
|
|
139
|
+
* the same error.
|
|
140
|
+
*
|
|
141
|
+
* @template T The type of elements in the queue.
|
|
142
|
+
*/
|
|
143
|
+
export interface IWeightBoundedQueue<T extends DefinedValue = DefinedValue> extends IQueue<T> {
|
|
144
|
+
/** The inclusive weight limit, a non-negative safe integer. */
|
|
145
|
+
readonly maxWeight: number;
|
|
146
|
+
/** The inclusive count limit, `Infinity` when there is none. */
|
|
147
|
+
readonly maxCount: number;
|
|
148
|
+
/** What happens to an item heavier than `maxWeight`. */
|
|
149
|
+
readonly oversizedItemPolicy: OversizedItemPolicy;
|
|
150
|
+
/**
|
|
151
|
+
* The number of queued items, reservations excluded.
|
|
152
|
+
*
|
|
153
|
+
* Complexity: `O(1)`.
|
|
154
|
+
*/
|
|
155
|
+
count(): number;
|
|
156
|
+
/**
|
|
157
|
+
* The total weight of the queued items, reservations excluded.
|
|
158
|
+
*
|
|
159
|
+
* Complexity: `O(1)`.
|
|
160
|
+
*/
|
|
161
|
+
weight(): number;
|
|
162
|
+
/**
|
|
163
|
+
* The number of reservations held.
|
|
164
|
+
*
|
|
165
|
+
* Complexity: `O(1)`.
|
|
166
|
+
*/
|
|
167
|
+
reservedCount(): number;
|
|
168
|
+
/**
|
|
169
|
+
* The total weight of the reservations held.
|
|
170
|
+
*
|
|
171
|
+
* Complexity: `O(1)`.
|
|
172
|
+
*/
|
|
173
|
+
reservedWeight(): number;
|
|
174
|
+
/**
|
|
175
|
+
* Adds an item to the rear of the queue if the limits admit it.
|
|
176
|
+
*
|
|
177
|
+
* Complexity: `O(1)`, plus one call to `weightOf`.
|
|
178
|
+
*
|
|
179
|
+
* @throws {RangeError} If `weightOf` returns anything but a non-negative safe
|
|
180
|
+
* integer — a fault, not an outcome.
|
|
181
|
+
*/
|
|
182
|
+
tryEnqueue(item: T): WeightBoundedEnqueueOutcome;
|
|
183
|
+
/**
|
|
184
|
+
* Adds every item to the rear, in array order, if the limits admit the whole
|
|
185
|
+
* batch; otherwise adds nothing. An empty batch is `"enqueued"`.
|
|
186
|
+
*
|
|
187
|
+
* Complexity: `O(k)` for `k` items, plus one call to `weightOf` each.
|
|
188
|
+
*
|
|
189
|
+
* @throws {RangeError} As `tryEnqueue`, before anything is added.
|
|
190
|
+
*/
|
|
191
|
+
tryEnqueueAll(items: readonly T[]): WeightBoundedEnqueueOutcome;
|
|
192
|
+
/**
|
|
193
|
+
* Same as `IQueue.enqueue`, but throws when the limits refuse the item.
|
|
194
|
+
*
|
|
195
|
+
* Complexity: as `tryEnqueue`.
|
|
196
|
+
*
|
|
197
|
+
* @throws {CollectionCapacityExceededError} When the count limit refuses
|
|
198
|
+
* it.
|
|
199
|
+
* @throws {WeightCapacityExceededError} When the weight limit refuses it.
|
|
200
|
+
* @throws {RangeError} As `tryEnqueue`.
|
|
201
|
+
*/
|
|
202
|
+
enqueue(item: T): void;
|
|
203
|
+
/**
|
|
204
|
+
* Same as `IQueue.enqueueAll`, but throws when the limits refuse the batch.
|
|
205
|
+
* Nothing is added when it throws.
|
|
206
|
+
*
|
|
207
|
+
* Complexity: as `tryEnqueueAll`.
|
|
208
|
+
*
|
|
209
|
+
* @throws {CollectionCapacityExceededError} When the count limit refuses
|
|
210
|
+
* it.
|
|
211
|
+
* @throws {WeightCapacityExceededError} When the weight limit refuses it.
|
|
212
|
+
* @throws {RangeError} As `tryEnqueue`.
|
|
213
|
+
*/
|
|
214
|
+
enqueueAll(items: readonly T[]): void;
|
|
215
|
+
/**
|
|
216
|
+
* Removes the front item and frees its count and weight.
|
|
217
|
+
*
|
|
218
|
+
* Complexity: `O(1)`.
|
|
219
|
+
*/
|
|
220
|
+
dequeue(): T | undefined;
|
|
221
|
+
/**
|
|
222
|
+
* The front item, left in place.
|
|
223
|
+
*
|
|
224
|
+
* Complexity: `O(1)`.
|
|
225
|
+
*/
|
|
226
|
+
front(): T | undefined;
|
|
227
|
+
/**
|
|
228
|
+
* Takes the front item off the queue while its count and weight keep
|
|
229
|
+
* occupying the limits, until the reservation ends. Any number of
|
|
230
|
+
* reservations may be held at once.
|
|
231
|
+
*
|
|
232
|
+
* Complexity: `O(1)`.
|
|
233
|
+
*
|
|
234
|
+
* @returns The reservation, or `undefined` if the queue is empty.
|
|
235
|
+
*/
|
|
236
|
+
reserve(): IWeightBoundedQueueReservation<T> | undefined;
|
|
237
|
+
/**
|
|
238
|
+
* The queued entries, front to back, as removable handles. Fail-fast, as the
|
|
239
|
+
* queue's iterator.
|
|
240
|
+
*
|
|
241
|
+
* Complexity: `O(1)` per step.
|
|
242
|
+
*/
|
|
243
|
+
entries(): IterableIterator<IWeightBoundedQueueEntry<T>>;
|
|
244
|
+
/**
|
|
245
|
+
* The frontmost queued item matching `condition`.
|
|
246
|
+
*
|
|
247
|
+
* Complexity: `O(i)`, where `i` is the matching item's position (`O(n)` when
|
|
248
|
+
* none matches).
|
|
249
|
+
*
|
|
250
|
+
* @throws {ConcurrentModificationError} If `condition` changes the queue.
|
|
251
|
+
*/
|
|
252
|
+
find(condition: Predicate<[item: T]>): T | undefined;
|
|
253
|
+
/**
|
|
254
|
+
* The frontmost queued entry whose item matches `condition`, as a handle — to
|
|
255
|
+
* inspect it, then remove that one entry in `O(1)`.
|
|
256
|
+
*
|
|
257
|
+
* Complexity: as `find`.
|
|
258
|
+
*
|
|
259
|
+
* @throws {ConcurrentModificationError} If `condition` changes the queue.
|
|
260
|
+
*/
|
|
261
|
+
findEntry(condition: Predicate<[item: T]>): IWeightBoundedQueueEntry<T> | undefined;
|
|
262
|
+
/**
|
|
263
|
+
* Removes the frontmost queued item matching `condition`.
|
|
264
|
+
*
|
|
265
|
+
* Complexity: as `find`.
|
|
266
|
+
*
|
|
267
|
+
* @returns `true` if an item was removed.
|
|
268
|
+
* @throws {ConcurrentModificationError} If `condition` changes the queue.
|
|
269
|
+
*/
|
|
270
|
+
removeFirst(condition: Predicate<[item: T]>): boolean;
|
|
271
|
+
/**
|
|
272
|
+
* Removes every queued item matching `condition`, in one pass. Reservations
|
|
273
|
+
* are not in the queue and are never removed.
|
|
274
|
+
*
|
|
275
|
+
* Complexity: `O(n)`, one call to `condition` per queued item.
|
|
276
|
+
*
|
|
277
|
+
* @returns How many items were removed.
|
|
278
|
+
* @throws {ConcurrentModificationError} If `condition` changes the queue; the
|
|
279
|
+
* items it matched before then stay removed.
|
|
280
|
+
*/
|
|
281
|
+
removeWhere(condition: Predicate<[item: T]>): number;
|
|
282
|
+
/**
|
|
283
|
+
* Removes every queued item. Reservations are not in the queue: they stay
|
|
284
|
+
* held, and still occupy the limits until they end.
|
|
285
|
+
*
|
|
286
|
+
* Complexity: `O(n)`.
|
|
287
|
+
*/
|
|
288
|
+
clear(): void;
|
|
289
|
+
}
|
|
290
|
+
//# sourceMappingURL=iweight-bounded-queue.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"iweight-bounded-queue.d.ts","sourceRoot":"","sources":["../../src/queue/iweight-bounded-queue.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAE5D,OAAO,EAAE,+BAA+B,EAAE,MAAM,2BAA2B,CAAC;AAC5E,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC;;;;;;;;;;;GAWG;AACH,MAAM,MAAM,mBAAmB,GAAG,QAAQ,GAAG,OAAO,CAAC;AAErD;;;;;;;;;GASG;AACH,MAAM,MAAM,2BAA2B,GAAG,UAAU,GAAG,MAAM,GAAG,WAAW,CAAC;AAE5E,qDAAqD;AACrD,MAAM,MAAM,yBAAyB,GAAG;IACvC;;;OAGG;IACH,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAE3B,0BAA0B;IAC1B,QAAQ,CAAC,mBAAmB,CAAC,EAAE,mBAAmB,CAAC;CACnD,CAAC;AAEF;;;;GAIG;AACH,qBAAa,2BAA4B,SAAQ,+BAA+B;IAC/E,gCAAgC;IAChC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAE3B,0CAA0C;IAC1C,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;IAEjC,YAAY,SAAS,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,EAQrD;CACD;AAED;;;;;GAKG;AACH,MAAM,WAAW,wBAAwB,CAAC,CAAC,SAAS,YAAY;IAC/D,uBAAuB;IACvB,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC;IAEjB,yEAAyE;IACzE,QAAQ,CAAC,M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E,CAAC,GAAG,IAAI,CAAC;IAEvB;;;;;;;;;;OAUG;IACH,UAAU,CAAC,KAAK,EAAE,SAAS,CAAC,EAAE,GAAG,IAAI,CAAC;IAEtC;;;;OAIG;IACH,OAAO,IAAI,CAAC,GAAG,SAAS,CAAC;IAEzB;;;;OAIG;IACH,KAAK,IAAI,CAAC,GAAG,SAAS,CAAC;IAEvB;;;;;;;;OAQG;IACH,OAAO,IAAI,8BAA8B,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC;IAEzD;;;;;OAKG;IACH,OAAO,IAAI,gBAAgB,CAAC,wBAAwB,CAAC,CAAC,CAAC,CAAC,CAAC;IAEzD;;;;;;;OAOG;IACH,IAAI,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC;IAErD;;;;;;;OAOG;IACH,SAAS,CACR,SAAS,EAAE,SAAS,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,GAC7B,wBAAwB,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC;IAE3C;;;;;;;OAOG;IACH,WAAW,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC;IAEtD;;;;;;;;;OASG;IACH,WAAW,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC;IAErD;;;;;OAKG;IACH,KAAK,IAAI,IAAI,CAAC;CACd"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { CollectionCapacityExceededError } from "../collection/ibounded.js";
|
|
2
|
+
/**
|
|
3
|
+
* Error thrown by `IWeightBoundedQueue.enqueue` and `enqueueAll` when the
|
|
4
|
+
* weight limit refuses the insertion; the count limit throws the base
|
|
5
|
+
* {@link CollectionCapacityExceededError}.
|
|
6
|
+
*/
|
|
7
|
+
export class WeightCapacityExceededError extends CollectionCapacityExceededError {
|
|
8
|
+
/** The queue's weight limit. */
|
|
9
|
+
maxWeight;
|
|
10
|
+
/** The weight the insertion asked for. */
|
|
11
|
+
requestedWeight;
|
|
12
|
+
constructor(maxWeight, requestedWeight) {
|
|
13
|
+
super(maxWeight, `Weight capacity of ${maxWeight} exceeded: ${requestedWeight} more requested`);
|
|
14
|
+
this.name = "WeightCapacityExceededError";
|
|
15
|
+
this.maxWeight = maxWeight;
|
|
16
|
+
this.requestedWeight = requestedWeight;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=iweight-bounded-queue.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"iweight-bounded-queue.js","sourceRoot":"","sources":["../../src/queue/iweight-bounded-queue.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,+BAA+B,EAAE,MAAM,2BAA2B,CAAC;AAyC5E;;;;GAIG;AACH,MAAM,OAAO,2BAA4B,SAAQ,+BAA+B;IAC/E,gCAAgC;IACvB,SAAS,CAAS;IAE3B,0CAA0C;IACjC,eAAe,CAAS;IAEjC,YAAY,SAAiB,EAAE,eAAuB;QACrD,KAAK,CACJ,SAAS,EACT,sBAAsB,SAAS,cAAc,eAAe,iBAAiB,CAC7E,CAAC;QACF,IAAI,CAAC,IAAI,GAAG,6BAA6B,CAAC;QAC1C,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,eAAe,GAAG,eAAe,CAAC;IACxC,CAAC;CACD"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { type Callable, type DefinedValue, type Predicate } from "@ac-kit/core";
|
|
2
|
+
import { type IWeightBoundedQueue, type IWeightBoundedQueueEntry, type IWeightBoundedQueueReservation, type OversizedItemPolicy, type WeightBoundedEnqueueOutcome, type WeightBoundedQueueOptions } from "./iweight-bounded-queue.js";
|
|
3
|
+
/**
|
|
4
|
+
* A FIFO queue bounded by the total weight of its items — bytes, tokens, cost
|
|
5
|
+
* units — and optionally by their count, with selective removal and reservation
|
|
6
|
+
* of in-flight items. See {@link IWeightBoundedQueue} for the contract: integer
|
|
7
|
+
* weights read once, inclusive limits that reservations occupy, all-or-nothing
|
|
8
|
+
* batches, enqueue order kept through requeues, and fail-fast traversal.
|
|
9
|
+
*
|
|
10
|
+
* Every operation at an end, through a handle, or on a reservation is `O(1)`;
|
|
11
|
+
* predicate searches are linear. One node is allocated per enqueued item and
|
|
12
|
+
* serves as its entry handle and its reservation; nothing else is allocated per
|
|
13
|
+
* operation.
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* const outbox = new WeightBoundedQueue<Message>(
|
|
17
|
+
* 64 * 1024,
|
|
18
|
+
* (message) => message.bytes,
|
|
19
|
+
* undefined,
|
|
20
|
+
* { maxCount: 32 },
|
|
21
|
+
* );
|
|
22
|
+
*
|
|
23
|
+
* if (outbox.tryEnqueue(message) !== "enqueued") {
|
|
24
|
+
* disconnect("backpressure");
|
|
25
|
+
* }
|
|
26
|
+
*
|
|
27
|
+
* using sending = outbox.reserve(); // still counted until released
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @template T The type of elements in the queue.
|
|
31
|
+
*/
|
|
32
|
+
export declare class WeightBoundedQueue<T extends DefinedValue> implements IWeightBoundedQueue<T> {
|
|
33
|
+
private readonly weightOf;
|
|
34
|
+
readonly maxWeight: number;
|
|
35
|
+
readonly maxCount: number;
|
|
36
|
+
readonly oversizedItemPolicy: OversizedItemPolicy;
|
|
37
|
+
private readonly shared;
|
|
38
|
+
/**
|
|
39
|
+
* @param maxWeight The inclusive weight limit, a non-negative safe integer.
|
|
40
|
+
* @param weightOf The weight of an item, a non-negative safe integer, read
|
|
41
|
+
* once when it is enqueued.
|
|
42
|
+
* @param iterable Initial items, enqueued in order.
|
|
43
|
+
* @param options The count limit and the oversized item policy.
|
|
44
|
+
* @throws {RangeError} If a limit is not a non-negative safe integer.
|
|
45
|
+
* @throws {CollectionCapacityExceededError} If the initial items do not fit.
|
|
46
|
+
*/
|
|
47
|
+
constructor(maxWeight: number, weightOf: Callable<[item: T], number>, iterable?: Iterable<T>, options?: WeightBoundedQueueOptions);
|
|
48
|
+
count(): number;
|
|
49
|
+
weight(): number;
|
|
50
|
+
reservedCount(): number;
|
|
51
|
+
reservedWeight(): number;
|
|
52
|
+
tryEnqueue(item: T): WeightBoundedEnqueueOutcome;
|
|
53
|
+
tryEnqueueAll(items: readonly T[]): WeightBoundedEnqueueOutcome;
|
|
54
|
+
enqueue(item: T): void;
|
|
55
|
+
enqueueAll(items: readonly T[]): void;
|
|
56
|
+
dequeue(): T | undefined;
|
|
57
|
+
front(): T | undefined;
|
|
58
|
+
reserve(): IWeightBoundedQueueReservation<T> | undefined;
|
|
59
|
+
[Symbol.iterator](): Iterator<T>;
|
|
60
|
+
entries(): IterableIterator<IWeightBoundedQueueEntry<T>>;
|
|
61
|
+
find(condition: Predicate<[item: T]>): T | undefined;
|
|
62
|
+
findEntry(condition: Predicate<[item: T]>): IWeightBoundedQueueEntry<T> | undefined;
|
|
63
|
+
removeFirst(condition: Predicate<[item: T]>): boolean;
|
|
64
|
+
removeWhere(condition: Predicate<[item: T]>): number;
|
|
65
|
+
clear(): void;
|
|
66
|
+
private weighItem;
|
|
67
|
+
/**
|
|
68
|
+
* The batch as an unlinked chain of nodes, each weighed once — built before
|
|
69
|
+
* the admission check, so a refused batch costs its nodes and nothing else.
|
|
70
|
+
*/
|
|
71
|
+
private chain;
|
|
72
|
+
/**
|
|
73
|
+
* Sums of safe integers can round past `2^53`, but only where they already
|
|
74
|
+
* exceed `maxWeight ≤ 2^53 − 1`, so every comparison here is exact.
|
|
75
|
+
*/
|
|
76
|
+
private admission;
|
|
77
|
+
private throwUnlessAdmitted;
|
|
78
|
+
private append;
|
|
79
|
+
/** Links a chain of `count` nodes weighing `weight` in at the rear. */
|
|
80
|
+
private link;
|
|
81
|
+
private findNode;
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=weight-boun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