distillate 0.13.0 → 0.14.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/README.md +38 -12
  2. package/dist/blocked/index.cjs +1 -1
  3. package/dist/blocked/index.js +1 -1
  4. package/dist/bloom/index.cjs +3 -4
  5. package/dist/bloom/index.d.cts +12 -2
  6. package/dist/bloom/index.d.ts +12 -2
  7. package/dist/bloom/index.js +2 -3
  8. package/dist/bytes-Be7tq1F7.js +9 -0
  9. package/dist/bytes-D2duG1jq.cjs +14 -0
  10. package/dist/countmin/index.cjs +13 -4
  11. package/dist/countmin/index.d.cts +15 -2
  12. package/dist/countmin/index.d.ts +15 -2
  13. package/dist/countmin/index.js +12 -3
  14. package/dist/cuckoo/index.cjs +20 -9
  15. package/dist/cuckoo/index.js +20 -9
  16. package/dist/distinct-BhxDB8mW.js +61 -0
  17. package/dist/distinct-Ck5PKuCS.cjs +66 -0
  18. package/dist/fuse/index.cjs +6 -11
  19. package/dist/fuse/index.js +6 -11
  20. package/dist/{hasher-3Cyn7Yg-.js → hasher-DOdmZ-LI.js} +1 -8
  21. package/dist/{hasher-MhY7aQ8F.cjs → hasher-DPXmP7SH.cjs} +2 -9
  22. package/dist/hll/index.cjs +10 -4
  23. package/dist/hll/index.d.cts +13 -2
  24. package/dist/hll/index.d.ts +13 -2
  25. package/dist/hll/index.js +9 -3
  26. package/dist/index.cjs +1 -1
  27. package/dist/index.js +1 -1
  28. package/dist/scalable/index.cjs +5 -6
  29. package/dist/scalable/index.js +2 -3
  30. package/dist/{bitset-QYGqxnEf.cjs → sizing-Ch0SDdza.cjs} +15 -0
  31. package/dist/{bitset-Cf2jwRJX.js → sizing-zxCBlyhO.js} +10 -1
  32. package/dist/topk/index.cjs +423 -0
  33. package/dist/topk/index.d.cts +207 -0
  34. package/dist/topk/index.d.ts +207 -0
  35. package/dist/topk/index.js +411 -0
  36. package/package.json +15 -7
  37. package/dist/sizing-BmKfHf_V.d.cts +0 -34
  38. package/dist/sizing-BmKfHf_V.d.ts +0 -34
  39. package/dist/sizing-D3-_7pdP.js +0 -25
  40. package/dist/sizing-R1pwbzjC.cjs +0 -42
@@ -0,0 +1,207 @@
1
+ import { t as BytesLike } from "../bytes-DCuYtUVS.cjs";
2
+ import { a as ReservedBitsError, c as UnknownHashVariantError, l as UnknownVersionError, n as ChecksumError, o as SerializationError, r as FilterJSON, s as TruncatedError, t as BadMagicError } from "../serialize--xR6vGSW.cjs";
3
+ import { t as ParamError } from "../params-DnqJBqLS.cjs";
4
+ //#region src/topk/topk.d.ts
5
+ /**
6
+ * Thrown when an `add` or `union` would carry a stored count past
7
+ * `2^32 - 1`, or the total past `Number.MAX_SAFE_INTEGER`. Counts are `u32`,
8
+ * so a wrapped one would read back below the truth, the one outcome this
9
+ * structure rules out; a total past the safe range is one `fromBytes`
10
+ * refuses. Every sketch involved is left exactly as it was.
11
+ */
12
+ export declare class TopKOverflowError extends RangeError {
13
+ /** Discriminates this error from other `Error`s. */
14
+ override readonly name = "TopKOverflowError";
15
+ }
16
+ /** Thrown when an operation requires two sketches built with identical parameters. */
17
+ export declare class TopKParamMismatchError extends Error {
18
+ /** Discriminates this error from other `Error`s. */
19
+ override readonly name = "TopKParamMismatchError";
20
+ }
21
+ /** One of the heaviest keys, with the bracket its stored count implies. */
22
+ interface TopKEntry {
23
+ /** The key exactly as it was recorded. A copy, safe to keep or mutate. */
24
+ key: Uint8Array;
25
+ /** Upper bound, `stored + error()`. Never below the true count. */
26
+ count: number;
27
+ /** Lower bound, the stored count alone. Never above the true count. */
28
+ lowerBound: number;
29
+ }
30
+ /** Low-level Top-K sketch parameters. */
31
+ interface TopKParams {
32
+ /** Slots in the counter map; must be a power of two. */
33
+ capacity: number;
34
+ }
35
+ /**
36
+ * A Top-K sketch: Misra-Gries with reverse purge, answering which keys are
37
+ * heaviest in space fixed by the error you ask for.
38
+ *
39
+ * @example
40
+ * ```ts
41
+ * const sketch = TopK.create(0.001);
42
+ * sketch.add("alice");
43
+ * ```
44
+ */
45
+ export declare class TopK {
46
+ #private;
47
+ /**
48
+ * Creates a sketch whose estimate is at most `epsilon` of the total recorded
49
+ * above the true count.
50
+ *
51
+ * Sized by the error it targets rather than by how many keys it will see, or
52
+ * by the k you will ask for: `top(k)` takes k per call.
53
+ *
54
+ * @param epsilon - Error factor relative to the total recorded, e.g. `0.001`.
55
+ * @returns A new, empty sketch.
56
+ */
57
+ static create(epsilon: number): TopK;
58
+ /**
59
+ * Builds a sketch recording `keys`, at the target error bound. The
60
+ * ergonomic entry point when the stream is already in hand.
61
+ *
62
+ * A repeated key is counted once per occurrence, which is the whole point of
63
+ * a frequency sketch. This is the opposite of `CuckooFilter.from`, where a
64
+ * repeat costs a slot and is therefore dropped.
65
+ *
66
+ * @param keys - The keys to record, repeats included.
67
+ * @param epsilon - Error factor relative to the total recorded.
68
+ * @returns A new sketch holding every occurrence.
69
+ */
70
+ static from(keys: Iterable<BytesLike>, epsilon: number): TopK;
71
+ /**
72
+ * Restores a sketch from its {@link TopK.toBytes} serialization.
73
+ *
74
+ * @param bytes - The serialized sketch.
75
+ * @returns The reconstructed sketch.
76
+ */
77
+ static fromBytes(bytes: Uint8Array): TopK;
78
+ /**
79
+ * Constructs a sketch from low-level {@link TopKParams}. Prefer
80
+ * {@link TopK.create} unless restoring a specific geometry.
81
+ */
82
+ constructor({ capacity }: TopKParams);
83
+ /** Slots in the counter map. */
84
+ get capacity(): number;
85
+ /**
86
+ * Error factor the geometry implements, `1 / topKPurgeWidth(capacity)`.
87
+ *
88
+ * Sizing rounds the capacity up to a power of two, so this is at or below the
89
+ * `epsilon` passed to {@link TopK.create}: the sketch reports what it
90
+ * delivers rather than what was asked for.
91
+ */
92
+ get epsilon(): number;
93
+ /** Sum of every count recorded, whatever the map has since purged. */
94
+ get total(): number;
95
+ /**
96
+ * The most any estimate can exceed the truth by: everything the purges have
97
+ * subtracted so far.
98
+ *
99
+ * It is also the threshold of the one-sided guarantee. Every key whose true
100
+ * count exceeds this is still held, so nothing heavier than the error can
101
+ * have been silently dropped.
102
+ *
103
+ * @returns The accumulated purge offset, `0` before the first purge.
104
+ */
105
+ error(): number;
106
+ /**
107
+ * Records `count` occurrences of a key.
108
+ *
109
+ * @param key - The key to record, as a string or bytes.
110
+ * @param count - How many occurrences to record; defaults to `1`.
111
+ */
112
+ add(key: BytesLike, count?: number): void;
113
+ /**
114
+ * Estimates how many times a key was added.
115
+ *
116
+ * For a key the map holds this is `stored + error()`, never below the true
117
+ * count. A key the map does not hold returns `0` rather than the offset,
118
+ * since its true count may genuinely be zero.
119
+ *
120
+ * @param key - The key to estimate.
121
+ * @returns The estimated count, `0` for a key the map does not hold.
122
+ */
123
+ count(key: BytesLike): number;
124
+ /**
125
+ * The `k` heaviest keys the map holds, heaviest first.
126
+ *
127
+ * `k` is taken per call rather than fixed when the sketch was built, so one
128
+ * sketch answers top-10 and top-100 without a rebuild.
129
+ *
130
+ * Fewer than `k` entries come back when the map holds fewer, and a key
131
+ * purged away is not among them: what is guaranteed present is every key
132
+ * whose true count exceeds {@link TopK.error}.
133
+ *
134
+ * @param k - How many entries to return at most.
135
+ * @returns The heaviest entries, each with its upper and lower bound.
136
+ */
137
+ top(k: number): TopKEntry[];
138
+ /**
139
+ * Combines this sketch with another built with the same parameters.
140
+ *
141
+ * Each key's stored counts are summed, as are the offsets and totals, and
142
+ * the merged entries are purged until they fit the load limit. The result
143
+ * holds the same guarantee as its inputs, with an error of at least the sum
144
+ * of theirs.
145
+ *
146
+ * Unlike Count-Min's union, this is not the sketch one stream fed both
147
+ * inputs would have produced. Misra-Gries depends on arrival order: a
148
+ * single sketch purges as it goes, while a union purges only what the two
149
+ * inputs kept. Both are correct, and their bytes can differ.
150
+ *
151
+ * @param other - The sketch to combine with.
152
+ * @returns A new sketch; neither input changes.
153
+ */
154
+ union(other: TopK): TopK;
155
+ /**
156
+ * Serializes the sketch to a portable little-endian byte layout.
157
+ *
158
+ * Entries are written in the order `top` returns them, so the bytes depend
159
+ * only on what the map holds and never on the order it was filled in.
160
+ *
161
+ * @returns The serialized sketch, readable by {@link TopK.fromBytes}.
162
+ */
163
+ toBytes(): Uint8Array;
164
+ /**
165
+ * Tests structural equality: `true` when `other` serializes to identical
166
+ * bytes, meaning identical geometry, offset, total and entries.
167
+ *
168
+ * Entries are written in canonical order, so equal bytes mean the same
169
+ * entries are held, whatever order they arrived in.
170
+ *
171
+ * @param other - The sketch to compare against.
172
+ * @returns `true` if the two sketches are byte-for-byte identical.
173
+ */
174
+ equals(other: TopK): boolean;
175
+ /**
176
+ * Serializes the sketch to a JSON-friendly envelope wrapping the base64 of
177
+ * {@link TopK.toBytes}.
178
+ *
179
+ * @returns The envelope, readable by {@link TopK.fromJSON}.
180
+ */
181
+ toJSON(): FilterJSON;
182
+ /**
183
+ * Restores a sketch from its {@link TopK.toJSON} envelope.
184
+ *
185
+ * @param value - The JSON envelope.
186
+ * @returns The reconstructed sketch.
187
+ */
188
+ static fromJSON(value: unknown): TopK;
189
+ }
190
+ //#endregion
191
+ //#region src/topk/sizing.d.ts
192
+ /** Top-K geometry: the `TopKParams` fields a sizing solve determines. */
193
+ interface TopKSizing {
194
+ /** Slots in the counter map, a power of two. */
195
+ capacity: number;
196
+ }
197
+ /**
198
+ * Smallest power-of-two capacity whose purge width keeps the offset within
199
+ * `epsilon` of the total recorded. Throws {@link ParamError} when even
200
+ * `TOPK_MAX_CAPACITY` cannot.
201
+ *
202
+ * The bound is a function of capacity, not of k, which is why `top(k)` takes k
203
+ * per call instead of the sketch being built for one.
204
+ */
205
+ export declare function topKSizing(epsilon: number): TopKSizing;
206
+ //#endregion
207
+ export { BadMagicError, ChecksumError, type FilterJSON, ParamError, ReservedBitsError, SerializationError, type TopKEntry, type TopKParams, type TopKSizing, TruncatedError, UnknownHashVariantError, UnknownVersionError };
@@ -0,0 +1,207 @@
1
+ import { t as BytesLike } from "../bytes-DCuYtUVS.js";
2
+ import { a as ReservedBitsError, c as UnknownHashVariantError, l as UnknownVersionError, n as ChecksumError, o as SerializationError, r as FilterJSON, s as TruncatedError, t as BadMagicError } from "../serialize--xR6vGSW.js";
3
+ import { t as ParamError } from "../params-DnqJBqLS.js";
4
+ //#region src/topk/topk.d.ts
5
+ /**
6
+ * Thrown when an `add` or `union` would carry a stored count past
7
+ * `2^32 - 1`, or the total past `Number.MAX_SAFE_INTEGER`. Counts are `u32`,
8
+ * so a wrapped one would read back below the truth, the one outcome this
9
+ * structure rules out; a total past the safe range is one `fromBytes`
10
+ * refuses. Every sketch involved is left exactly as it was.
11
+ */
12
+ export declare class TopKOverflowError extends RangeError {
13
+ /** Discriminates this error from other `Error`s. */
14
+ override readonly name = "TopKOverflowError";
15
+ }
16
+ /** Thrown when an operation requires two sketches built with identical parameters. */
17
+ export declare class TopKParamMismatchError extends Error {
18
+ /** Discriminates this error from other `Error`s. */
19
+ override readonly name = "TopKParamMismatchError";
20
+ }
21
+ /** One of the heaviest keys, with the bracket its stored count implies. */
22
+ interface TopKEntry {
23
+ /** The key exactly as it was recorded. A copy, safe to keep or mutate. */
24
+ key: Uint8Array;
25
+ /** Upper bound, `stored + error()`. Never below the true count. */
26
+ count: number;
27
+ /** Lower bound, the stored count alone. Never above the true count. */
28
+ lowerBound: number;
29
+ }
30
+ /** Low-level Top-K sketch parameters. */
31
+ interface TopKParams {
32
+ /** Slots in the counter map; must be a power of two. */
33
+ capacity: number;
34
+ }
35
+ /**
36
+ * A Top-K sketch: Misra-Gries with reverse purge, answering which keys are
37
+ * heaviest in space fixed by the error you ask for.
38
+ *
39
+ * @example
40
+ * ```ts
41
+ * const sketch = TopK.create(0.001);
42
+ * sketch.add("alice");
43
+ * ```
44
+ */
45
+ export declare class TopK {
46
+ #private;
47
+ /**
48
+ * Creates a sketch whose estimate is at most `epsilon` of the total recorded
49
+ * above the true count.
50
+ *
51
+ * Sized by the error it targets rather than by how many keys it will see, or
52
+ * by the k you will ask for: `top(k)` takes k per call.
53
+ *
54
+ * @param epsilon - Error factor relative to the total recorded, e.g. `0.001`.
55
+ * @returns A new, empty sketch.
56
+ */
57
+ static create(epsilon: number): TopK;
58
+ /**
59
+ * Builds a sketch recording `keys`, at the target error bound. The
60
+ * ergonomic entry point when the stream is already in hand.
61
+ *
62
+ * A repeated key is counted once per occurrence, which is the whole point of
63
+ * a frequency sketch. This is the opposite of `CuckooFilter.from`, where a
64
+ * repeat costs a slot and is therefore dropped.
65
+ *
66
+ * @param keys - The keys to record, repeats included.
67
+ * @param epsilon - Error factor relative to the total recorded.
68
+ * @returns A new sketch holding every occurrence.
69
+ */
70
+ static from(keys: Iterable<BytesLike>, epsilon: number): TopK;
71
+ /**
72
+ * Restores a sketch from its {@link TopK.toBytes} serialization.
73
+ *
74
+ * @param bytes - The serialized sketch.
75
+ * @returns The reconstructed sketch.
76
+ */
77
+ static fromBytes(bytes: Uint8Array): TopK;
78
+ /**
79
+ * Constructs a sketch from low-level {@link TopKParams}. Prefer
80
+ * {@link TopK.create} unless restoring a specific geometry.
81
+ */
82
+ constructor({ capacity }: TopKParams);
83
+ /** Slots in the counter map. */
84
+ get capacity(): number;
85
+ /**
86
+ * Error factor the geometry implements, `1 / topKPurgeWidth(capacity)`.
87
+ *
88
+ * Sizing rounds the capacity up to a power of two, so this is at or below the
89
+ * `epsilon` passed to {@link TopK.create}: the sketch reports what it
90
+ * delivers rather than what was asked for.
91
+ */
92
+ get epsilon(): number;
93
+ /** Sum of every count recorded, whatever the map has since purged. */
94
+ get total(): number;
95
+ /**
96
+ * The most any estimate can exceed the truth by: everything the purges have
97
+ * subtracted so far.
98
+ *
99
+ * It is also the threshold of the one-sided guarantee. Every key whose true
100
+ * count exceeds this is still held, so nothing heavier than the error can
101
+ * have been silently dropped.
102
+ *
103
+ * @returns The accumulated purge offset, `0` before the first purge.
104
+ */
105
+ error(): number;
106
+ /**
107
+ * Records `count` occurrences of a key.
108
+ *
109
+ * @param key - The key to record, as a string or bytes.
110
+ * @param count - How many occurrences to record; defaults to `1`.
111
+ */
112
+ add(key: BytesLike, count?: number): void;
113
+ /**
114
+ * Estimates how many times a key was added.
115
+ *
116
+ * For a key the map holds this is `stored + error()`, never below the true
117
+ * count. A key the map does not hold returns `0` rather than the offset,
118
+ * since its true count may genuinely be zero.
119
+ *
120
+ * @param key - The key to estimate.
121
+ * @returns The estimated count, `0` for a key the map does not hold.
122
+ */
123
+ count(key: BytesLike): number;
124
+ /**
125
+ * The `k` heaviest keys the map holds, heaviest first.
126
+ *
127
+ * `k` is taken per call rather than fixed when the sketch was built, so one
128
+ * sketch answers top-10 and top-100 without a rebuild.
129
+ *
130
+ * Fewer than `k` entries come back when the map holds fewer, and a key
131
+ * purged away is not among them: what is guaranteed present is every key
132
+ * whose true count exceeds {@link TopK.error}.
133
+ *
134
+ * @param k - How many entries to return at most.
135
+ * @returns The heaviest entries, each with its upper and lower bound.
136
+ */
137
+ top(k: number): TopKEntry[];
138
+ /**
139
+ * Combines this sketch with another built with the same parameters.
140
+ *
141
+ * Each key's stored counts are summed, as are the offsets and totals, and
142
+ * the merged entries are purged until they fit the load limit. The result
143
+ * holds the same guarantee as its inputs, with an error of at least the sum
144
+ * of theirs.
145
+ *
146
+ * Unlike Count-Min's union, this is not the sketch one stream fed both
147
+ * inputs would have produced. Misra-Gries depends on arrival order: a
148
+ * single sketch purges as it goes, while a union purges only what the two
149
+ * inputs kept. Both are correct, and their bytes can differ.
150
+ *
151
+ * @param other - The sketch to combine with.
152
+ * @returns A new sketch; neither input changes.
153
+ */
154
+ union(other: TopK): TopK;
155
+ /**
156
+ * Serializes the sketch to a portable little-endian byte layout.
157
+ *
158
+ * Entries are written in the order `top` returns them, so the bytes depend
159
+ * only on what the map holds and never on the order it was filled in.
160
+ *
161
+ * @returns The serialized sketch, readable by {@link TopK.fromBytes}.
162
+ */
163
+ toBytes(): Uint8Array;
164
+ /**
165
+ * Tests structural equality: `true` when `other` serializes to identical
166
+ * bytes, meaning identical geometry, offset, total and entries.
167
+ *
168
+ * Entries are written in canonical order, so equal bytes mean the same
169
+ * entries are held, whatever order they arrived in.
170
+ *
171
+ * @param other - The sketch to compare against.
172
+ * @returns `true` if the two sketches are byte-for-byte identical.
173
+ */
174
+ equals(other: TopK): boolean;
175
+ /**
176
+ * Serializes the sketch to a JSON-friendly envelope wrapping the base64 of
177
+ * {@link TopK.toBytes}.
178
+ *
179
+ * @returns The envelope, readable by {@link TopK.fromJSON}.
180
+ */
181
+ toJSON(): FilterJSON;
182
+ /**
183
+ * Restores a sketch from its {@link TopK.toJSON} envelope.
184
+ *
185
+ * @param value - The JSON envelope.
186
+ * @returns The reconstructed sketch.
187
+ */
188
+ static fromJSON(value: unknown): TopK;
189
+ }
190
+ //#endregion
191
+ //#region src/topk/sizing.d.ts
192
+ /** Top-K geometry: the `TopKParams` fields a sizing solve determines. */
193
+ interface TopKSizing {
194
+ /** Slots in the counter map, a power of two. */
195
+ capacity: number;
196
+ }
197
+ /**
198
+ * Smallest power-of-two capacity whose purge width keeps the offset within
199
+ * `epsilon` of the total recorded. Throws {@link ParamError} when even
200
+ * `TOPK_MAX_CAPACITY` cannot.
201
+ *
202
+ * The bound is a function of capacity, not of k, which is why `top(k)` takes k
203
+ * per call instead of the sketch being built for one.
204
+ */
205
+ export declare function topKSizing(epsilon: number): TopKSizing;
206
+ //#endregion
207
+ export { BadMagicError, ChecksumError, type FilterJSON, ParamError, ReservedBitsError, SerializationError, type TopKEntry, type TopKParams, type TopKSizing, TruncatedError, UnknownHashVariantError, UnknownVersionError };