distillate 0.12.0 → 0.14.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/README.md +62 -10
- package/dist/blocked/index.cjs +1 -1
- package/dist/blocked/index.js +1 -1
- package/dist/bloom/index.cjs +3 -4
- package/dist/bloom/index.d.cts +12 -2
- package/dist/bloom/index.d.ts +12 -2
- package/dist/bloom/index.js +2 -3
- package/dist/bytes-Be7tq1F7.js +9 -0
- package/dist/bytes-D2duG1jq.cjs +14 -0
- package/dist/countmin/index.cjs +203 -0
- package/dist/countmin/index.d.cts +191 -0
- package/dist/countmin/index.d.ts +191 -0
- package/dist/countmin/index.js +191 -0
- package/dist/cuckoo/index.cjs +2 -2
- package/dist/cuckoo/index.js +2 -2
- package/dist/fuse/index.cjs +1 -1
- package/dist/fuse/index.js +1 -1
- package/dist/{hasher-3Cyn7Yg-.js → hasher-DOdmZ-LI.js} +1 -8
- package/dist/{hasher-MhY7aQ8F.cjs → hasher-DPXmP7SH.cjs} +2 -9
- package/dist/hll/index.cjs +10 -4
- package/dist/hll/index.d.cts +13 -2
- package/dist/hll/index.d.ts +13 -2
- package/dist/hll/index.js +9 -3
- package/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/dist/scalable/index.cjs +5 -6
- package/dist/scalable/index.js +2 -3
- package/dist/{bitset-QYGqxnEf.cjs → sizing-Ch0SDdza.cjs} +15 -0
- package/dist/{bitset-Cf2jwRJX.js → sizing-zxCBlyhO.js} +10 -1
- package/dist/topk/index.cjs +423 -0
- package/dist/topk/index.d.cts +207 -0
- package/dist/topk/index.d.ts +207 -0
- package/dist/topk/index.js +411 -0
- package/package.json +17 -1
- package/dist/sizing-BLzZk2zr.cjs +0 -28
- package/dist/sizing-BtUrtvF_.js +0 -17
- package/dist/sizing-wYOW27eY.d.cts +0 -22
- package/dist/sizing-wYOW27eY.d.ts +0 -22
|
@@ -0,0 +1,191 @@
|
|
|
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/countmin/countmin.d.ts
|
|
5
|
+
/** Thrown when an operation requires two sketches built with identical parameters. */
|
|
6
|
+
export declare class CountMinParamMismatchError extends Error {
|
|
7
|
+
/** Discriminates this error from other `Error`s. */
|
|
8
|
+
override readonly name = "CountMinParamMismatchError";
|
|
9
|
+
}
|
|
10
|
+
/** Thrown when an add would carry a counter past what a u32 holds. */
|
|
11
|
+
export declare class CountMinOverflowError extends RangeError {
|
|
12
|
+
/** Discriminates this error from other `Error`s. */
|
|
13
|
+
override readonly name = "CountMinOverflowError";
|
|
14
|
+
}
|
|
15
|
+
/** Options accepted alongside a sizing solve: everything but the geometry. */
|
|
16
|
+
type CountMinOptions = Omit<CountMinParams, "width" | "depth">;
|
|
17
|
+
/** Low-level Count-Min sketch parameters. */
|
|
18
|
+
interface CountMinParams {
|
|
19
|
+
/** Counters per row. */
|
|
20
|
+
width: number;
|
|
21
|
+
/** Number of rows, one probe each. */
|
|
22
|
+
depth: number;
|
|
23
|
+
/** Hash seed; defaults to `0`. */
|
|
24
|
+
seed?: number;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* A Count-Min sketch: a fixed-size frequency estimate with a tunable
|
|
28
|
+
* overestimate and no underestimate.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* const sketch = CountMinSketch.create(0.001, 0.001);
|
|
33
|
+
* sketch.add("alice");
|
|
34
|
+
* sketch.add("alice", 3);
|
|
35
|
+
* sketch.count("alice"); // 4 (or a rare overestimate)
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export declare class CountMinSketch {
|
|
39
|
+
#private;
|
|
40
|
+
/**
|
|
41
|
+
* Creates a sketch whose estimate is at most `epsilon * total` above the
|
|
42
|
+
* true count, with probability `1 - delta`.
|
|
43
|
+
*
|
|
44
|
+
* Unlike a filter, a sketch is sized by the error it targets rather than by
|
|
45
|
+
* how many keys it will see, so no key count is needed.
|
|
46
|
+
*
|
|
47
|
+
* @param epsilon - Error factor relative to the total recorded, e.g. `0.001`.
|
|
48
|
+
* @param delta - Probability the bound is exceeded, e.g. `0.001`.
|
|
49
|
+
* @param options - Optional seed.
|
|
50
|
+
* @returns A new, empty sketch.
|
|
51
|
+
*/
|
|
52
|
+
static create(epsilon: number, delta: number, options?: CountMinOptions): CountMinSketch;
|
|
53
|
+
/**
|
|
54
|
+
* Builds a sketch recording `keys`, at the target error bound. The
|
|
55
|
+
* ergonomic entry point when the stream is already in hand.
|
|
56
|
+
*
|
|
57
|
+
* A repeated key is counted once per occurrence, which is the whole point of
|
|
58
|
+
* a frequency sketch. This is the opposite of `CuckooFilter.from`, where a
|
|
59
|
+
* repeat costs a slot and is therefore dropped.
|
|
60
|
+
*
|
|
61
|
+
* @param keys - The keys to record, repeats included.
|
|
62
|
+
* @param epsilon - Error factor relative to the total recorded.
|
|
63
|
+
* @param delta - Probability the bound is exceeded.
|
|
64
|
+
* @param options - Optional seed.
|
|
65
|
+
* @returns A new sketch holding every occurrence.
|
|
66
|
+
*/
|
|
67
|
+
static from(keys: Iterable<BytesLike>, epsilon: number, delta: number, options?: CountMinOptions): CountMinSketch;
|
|
68
|
+
/**
|
|
69
|
+
* Restores a sketch from its {@link CountMinSketch.toBytes} serialization.
|
|
70
|
+
*
|
|
71
|
+
* Built from the geometry the frame stores, never from a sizing solve: the
|
|
72
|
+
* constructor takes `width` and `depth` directly, so there is nothing to
|
|
73
|
+
* re-derive and nothing to disagree about.
|
|
74
|
+
*
|
|
75
|
+
* @param bytes - The serialized sketch.
|
|
76
|
+
* @returns The reconstructed sketch.
|
|
77
|
+
*/
|
|
78
|
+
static fromBytes(bytes: Uint8Array): CountMinSketch;
|
|
79
|
+
/**
|
|
80
|
+
* Constructs a sketch from low-level {@link CountMinParams}. Prefer
|
|
81
|
+
* {@link CountMinSketch.create} unless restoring a specific geometry.
|
|
82
|
+
*/
|
|
83
|
+
constructor({ width, depth, seed }: CountMinParams);
|
|
84
|
+
/** Counters per row. */
|
|
85
|
+
get width(): number;
|
|
86
|
+
/** Number of rows, one probe each. */
|
|
87
|
+
get depth(): number;
|
|
88
|
+
/** Hash seed. */
|
|
89
|
+
get seed(): number;
|
|
90
|
+
/** Sum of every count recorded, the denominator of the error bound. */
|
|
91
|
+
get total(): number;
|
|
92
|
+
/**
|
|
93
|
+
* Error factor the geometry implements, `e / width`.
|
|
94
|
+
*
|
|
95
|
+
* Sizing rounds `width` up, so this is at or below the `epsilon` passed to
|
|
96
|
+
* {@link CountMinSketch.create}: the sketch reports what it delivers rather
|
|
97
|
+
* than what was asked for.
|
|
98
|
+
*/
|
|
99
|
+
get epsilon(): number;
|
|
100
|
+
/**
|
|
101
|
+
* Probability the error bound is exceeded, `e ** -depth`. At or below the
|
|
102
|
+
* `delta` passed to {@link CountMinSketch.create}, for the same reason.
|
|
103
|
+
*/
|
|
104
|
+
get delta(): number;
|
|
105
|
+
/**
|
|
106
|
+
* The additive error bound right now, `epsilon * total`. A count is at most
|
|
107
|
+
* this far above the truth, with probability `1 - delta`.
|
|
108
|
+
*
|
|
109
|
+
* It grows with what the sketch has recorded, so an estimate means less as
|
|
110
|
+
* the stream goes on, the way a filter's `rate()` rises as it fills.
|
|
111
|
+
*
|
|
112
|
+
* @returns The bound, `0` for an empty sketch.
|
|
113
|
+
*/
|
|
114
|
+
error(): number;
|
|
115
|
+
/**
|
|
116
|
+
* Records `count` occurrences of a key.
|
|
117
|
+
*
|
|
118
|
+
* @param key - The key to record, as a string or bytes.
|
|
119
|
+
* @param count - How many occurrences to record; defaults to `1`.
|
|
120
|
+
*/
|
|
121
|
+
add(key: BytesLike, count?: number): void;
|
|
122
|
+
/**
|
|
123
|
+
* Returns a new sketch counting everything both this one and `other` have
|
|
124
|
+
* recorded, as if one sketch had seen both streams.
|
|
125
|
+
*
|
|
126
|
+
* Both operands are left untouched.
|
|
127
|
+
*
|
|
128
|
+
* Unlike the filters, this is not idempotent: `a.union(a)` doubles every
|
|
129
|
+
* count, because counts add where bits only ever turn on.
|
|
130
|
+
*
|
|
131
|
+
* @param other - A sketch built with an identical geometry and seed.
|
|
132
|
+
* @returns A new sketch holding both streams.
|
|
133
|
+
*/
|
|
134
|
+
union(other: CountMinSketch): CountMinSketch;
|
|
135
|
+
/**
|
|
136
|
+
* Serializes the sketch to a portable little-endian byte layout.
|
|
137
|
+
*
|
|
138
|
+
* The total is not stored. Under plain increment every row sums to it, so
|
|
139
|
+
* the field would be redundant, and a reader deriving it instead gets an
|
|
140
|
+
* integrity check on the counters for free.
|
|
141
|
+
*
|
|
142
|
+
* @returns The serialized sketch, readable by {@link CountMinSketch.fromBytes}.
|
|
143
|
+
*/
|
|
144
|
+
toBytes(): Uint8Array;
|
|
145
|
+
/**
|
|
146
|
+
* Tests structural equality: `true` when `other` serializes to identical
|
|
147
|
+
* bytes, meaning identical geometry, seed and counters.
|
|
148
|
+
*
|
|
149
|
+
* @param other - The sketch to compare against.
|
|
150
|
+
* @returns `true` if the two sketches are byte-for-byte identical.
|
|
151
|
+
*/
|
|
152
|
+
equals(other: CountMinSketch): boolean;
|
|
153
|
+
/**
|
|
154
|
+
* Serializes the sketch to a JSON-friendly envelope wrapping the base64 of
|
|
155
|
+
* {@link CountMinSketch.toBytes}.
|
|
156
|
+
*
|
|
157
|
+
* @returns The envelope, readable by {@link CountMinSketch.fromJSON}.
|
|
158
|
+
*/
|
|
159
|
+
toJSON(): FilterJSON;
|
|
160
|
+
/**
|
|
161
|
+
* Restores a sketch from its {@link CountMinSketch.toJSON} envelope.
|
|
162
|
+
*
|
|
163
|
+
* @param value - The JSON envelope.
|
|
164
|
+
* @returns The reconstructed sketch.
|
|
165
|
+
*/
|
|
166
|
+
static fromJSON(value: unknown): CountMinSketch;
|
|
167
|
+
/**
|
|
168
|
+
* Estimates how many times a key was added. The estimate is never below the
|
|
169
|
+
* true count and is at most {@link CountMinSketch.error} above it.
|
|
170
|
+
*
|
|
171
|
+
* @param key - The key to estimate.
|
|
172
|
+
* @returns The estimated count, `0` for a key the sketch has not seen.
|
|
173
|
+
*/
|
|
174
|
+
count(key: BytesLike): number;
|
|
175
|
+
}
|
|
176
|
+
//#endregion
|
|
177
|
+
//#region src/countmin/sizing.d.ts
|
|
178
|
+
/** Count-Min geometry: the `CountMinParams` fields a sizing solve determines. */
|
|
179
|
+
interface CountMinSizing {
|
|
180
|
+
/** Counters per row. */
|
|
181
|
+
width: number;
|
|
182
|
+
/** Number of rows, one probe each. */
|
|
183
|
+
depth: number;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Count-Min sizing: `width` columns and `depth` rows for an estimate at most
|
|
187
|
+
* `epsilon * total` above the true count, with probability `1 - delta`.
|
|
188
|
+
*/
|
|
189
|
+
export declare function countMinSizing(epsilon: number, delta: number): CountMinSizing;
|
|
190
|
+
//#endregion
|
|
191
|
+
export { BadMagicError, ChecksumError, type CountMinOptions, type CountMinParams, type CountMinSizing, type FilterJSON, ParamError, ReservedBitsError, SerializationError, TruncatedError, UnknownHashVariantError, UnknownVersionError };
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { a as hash32x2Into, s as probeAt } from "../hasher-DOdmZ-LI.js";
|
|
2
|
+
import { a as TruncatedError, c as assertBodyLength, d as bytesEqual, f as fromJSONEnvelope, g as writeFrame, h as toJSONEnvelope, i as SerializationError, l as assertMinBodyLength, m as readHeader, n as ChecksumError, o as UnknownHashVariantError, r as ReservedBitsError, s as UnknownVersionError, t as BadMagicError, u as assertParamsPadding } from "../serialize-CMSYFym-.js";
|
|
3
|
+
import { i as assertProbability, o as assertUint32, r as assertPositiveInt, t as ParamError } from "../params-CeajSrwv.js";
|
|
4
|
+
//#region src/countmin/sizing.ts
|
|
5
|
+
function countMinSizing(epsilon, delta) {
|
|
6
|
+
assertProbability(epsilon, "epsilon");
|
|
7
|
+
assertProbability(delta, "delta");
|
|
8
|
+
return {
|
|
9
|
+
width: Math.ceil(Math.E / epsilon),
|
|
10
|
+
depth: Math.ceil(Math.log(1 / delta))
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region src/countmin/countmin.ts
|
|
15
|
+
const TYPE = 8;
|
|
16
|
+
const PARAMS_SIZE = 16;
|
|
17
|
+
const PARAMS_FIELDS_END = 12;
|
|
18
|
+
const counterBytes = (counters) => new Uint8Array(counters.buffer, counters.byteOffset, counters.byteLength);
|
|
19
|
+
function rowSum(counters, width, depth) {
|
|
20
|
+
let expected = 0;
|
|
21
|
+
for (let r = 0; r < depth; r++) {
|
|
22
|
+
let sum = 0;
|
|
23
|
+
const from = r * width;
|
|
24
|
+
for (let c = from; c < from + width; c++) sum += counters[c] ?? 0;
|
|
25
|
+
if (sum > Number.MAX_SAFE_INTEGER) throw new SerializationError(`countmin: row ${String(r)} sums to ${String(sum)}, past the safe integer range`);
|
|
26
|
+
if (r === 0) expected = sum;
|
|
27
|
+
else if (sum !== expected) throw new SerializationError(`countmin: row ${String(r)} sums to ${String(sum)}, but row 0 sums to ${String(expected)}`);
|
|
28
|
+
}
|
|
29
|
+
return expected;
|
|
30
|
+
}
|
|
31
|
+
var CountMinParamMismatchError = class extends Error {
|
|
32
|
+
name = "CountMinParamMismatchError";
|
|
33
|
+
};
|
|
34
|
+
var CountMinOverflowError = class extends RangeError {
|
|
35
|
+
name = "CountMinOverflowError";
|
|
36
|
+
};
|
|
37
|
+
var CountMinSketch = class CountMinSketch {
|
|
38
|
+
#counters;
|
|
39
|
+
#width;
|
|
40
|
+
#depth;
|
|
41
|
+
#seed;
|
|
42
|
+
#words = new Uint32Array(2);
|
|
43
|
+
#positions;
|
|
44
|
+
#total = 0;
|
|
45
|
+
static create(epsilon, delta, options = {}) {
|
|
46
|
+
return new CountMinSketch({
|
|
47
|
+
...options,
|
|
48
|
+
...countMinSizing(epsilon, delta)
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
static from(keys, epsilon, delta, options = {}) {
|
|
52
|
+
const sketch = CountMinSketch.create(epsilon, delta, options);
|
|
53
|
+
for (const key of keys) sketch.add(key);
|
|
54
|
+
return sketch;
|
|
55
|
+
}
|
|
56
|
+
static fromBytes(bytes) {
|
|
57
|
+
const { type, flags, body } = readHeader(bytes);
|
|
58
|
+
if (type !== TYPE) throw new SerializationError(`expected DSTL type ${String(TYPE)}, got ${String(type)}`);
|
|
59
|
+
if ((flags & 15) !== 0) throw new UnknownHashVariantError(`unsupported hash variant ${String(flags & 15)}`);
|
|
60
|
+
assertMinBodyLength(body.length, PARAMS_SIZE, "countmin");
|
|
61
|
+
assertParamsPadding(body, PARAMS_FIELDS_END, PARAMS_SIZE, "countmin");
|
|
62
|
+
const view = new DataView(body.buffer, body.byteOffset, body.byteLength);
|
|
63
|
+
const width = view.getUint32(0, true);
|
|
64
|
+
const depth = view.getUint32(4, true);
|
|
65
|
+
const seed = view.getUint32(8, true);
|
|
66
|
+
assertBodyLength(body.length, PARAMS_SIZE + 4 * width * depth, "countmin");
|
|
67
|
+
let sketch;
|
|
68
|
+
try {
|
|
69
|
+
sketch = new CountMinSketch({
|
|
70
|
+
width,
|
|
71
|
+
depth,
|
|
72
|
+
seed
|
|
73
|
+
});
|
|
74
|
+
} catch (err) {
|
|
75
|
+
if (err instanceof ParamError) throw new SerializationError(`countmin: ${err.message}`);
|
|
76
|
+
throw err;
|
|
77
|
+
}
|
|
78
|
+
const counters = sketch.#counters;
|
|
79
|
+
for (let i = 0; i < counters.length; i++) counters[i] = view.getUint32(PARAMS_SIZE + 4 * i, true);
|
|
80
|
+
sketch.#total = rowSum(counters, width, depth);
|
|
81
|
+
return sketch;
|
|
82
|
+
}
|
|
83
|
+
constructor({ width, depth, seed = 0 }) {
|
|
84
|
+
assertPositiveInt(width, "width");
|
|
85
|
+
assertUint32(width, "width");
|
|
86
|
+
assertPositiveInt(depth, "depth");
|
|
87
|
+
assertUint32(depth, "depth");
|
|
88
|
+
assertUint32(seed, "seed");
|
|
89
|
+
if (width * depth > 4294967295) throw new ParamError(`width ${String(width)} by depth ${String(depth)} needs ${String(width * depth)} counters, above the maximum ${String(4294967295)}`);
|
|
90
|
+
this.#counters = new Uint32Array(width * depth);
|
|
91
|
+
this.#positions = new Uint32Array(depth);
|
|
92
|
+
this.#width = width;
|
|
93
|
+
this.#depth = depth;
|
|
94
|
+
this.#seed = seed;
|
|
95
|
+
}
|
|
96
|
+
get width() {
|
|
97
|
+
return this.#width;
|
|
98
|
+
}
|
|
99
|
+
get depth() {
|
|
100
|
+
return this.#depth;
|
|
101
|
+
}
|
|
102
|
+
get seed() {
|
|
103
|
+
return this.#seed;
|
|
104
|
+
}
|
|
105
|
+
get total() {
|
|
106
|
+
return this.#total;
|
|
107
|
+
}
|
|
108
|
+
get epsilon() {
|
|
109
|
+
return Math.E / this.#width;
|
|
110
|
+
}
|
|
111
|
+
get delta() {
|
|
112
|
+
return Math.exp(-this.#depth);
|
|
113
|
+
}
|
|
114
|
+
error() {
|
|
115
|
+
return this.epsilon * this.#total;
|
|
116
|
+
}
|
|
117
|
+
#positionAt(row) {
|
|
118
|
+
return row * this.#width + probeAt(this.#words[0] ?? 0, this.#words[1] ?? 0, row, this.#width);
|
|
119
|
+
}
|
|
120
|
+
add(key, count = 1) {
|
|
121
|
+
assertPositiveInt(count, "count");
|
|
122
|
+
if (this.#total + count > Number.MAX_SAFE_INTEGER) throw new CountMinOverflowError(`adding ${String(count)} would carry the total past ${String(Number.MAX_SAFE_INTEGER)}`);
|
|
123
|
+
hash32x2Into(key, this.#seed, this.#words);
|
|
124
|
+
const headroom = 4294967295 - count;
|
|
125
|
+
for (let r = 0; r < this.#depth; r++) {
|
|
126
|
+
const at = this.#positionAt(r);
|
|
127
|
+
if ((this.#counters[at] ?? 0) > headroom) throw new CountMinOverflowError(`adding ${String(count)} would carry a counter past ${String(4294967295)}`);
|
|
128
|
+
this.#positions[r] = at;
|
|
129
|
+
}
|
|
130
|
+
for (let r = 0; r < this.#depth; r++) {
|
|
131
|
+
const at = this.#positions[r] ?? 0;
|
|
132
|
+
this.#counters[at] = (this.#counters[at] ?? 0) + count;
|
|
133
|
+
}
|
|
134
|
+
this.#total += count;
|
|
135
|
+
}
|
|
136
|
+
union(other) {
|
|
137
|
+
if (this.#width !== other.#width || this.#depth !== other.#depth || this.#seed !== other.#seed) throw new CountMinParamMismatchError("cannot union Count-Min sketches whose parameters do not match");
|
|
138
|
+
if (this.#total + other.#total > Number.MAX_SAFE_INTEGER) throw new CountMinOverflowError(`union would carry the total past ${String(Number.MAX_SAFE_INTEGER)}`);
|
|
139
|
+
const mine = this.#counters;
|
|
140
|
+
const theirs = other.#counters;
|
|
141
|
+
const merged = new CountMinSketch({
|
|
142
|
+
width: this.#width,
|
|
143
|
+
depth: this.#depth,
|
|
144
|
+
seed: this.#seed
|
|
145
|
+
});
|
|
146
|
+
const into = merged.#counters;
|
|
147
|
+
for (let i = 0; i < into.length; i++) {
|
|
148
|
+
const sum = (mine[i] ?? 0) + (theirs[i] ?? 0);
|
|
149
|
+
if (sum > 4294967295) throw new CountMinOverflowError(`union would carry a counter to ${String(sum)}, past ${String(4294967295)}`);
|
|
150
|
+
into[i] = sum;
|
|
151
|
+
}
|
|
152
|
+
merged.#total = this.#total + other.#total;
|
|
153
|
+
return merged;
|
|
154
|
+
}
|
|
155
|
+
toBytes() {
|
|
156
|
+
const counters = this.#counters;
|
|
157
|
+
return writeFrame({
|
|
158
|
+
version: 5,
|
|
159
|
+
type: TYPE,
|
|
160
|
+
flags: 0
|
|
161
|
+
}, PARAMS_SIZE, 4 * counters.length, (_, view) => {
|
|
162
|
+
view.setUint32(0, this.#width, true);
|
|
163
|
+
view.setUint32(4, this.#depth, true);
|
|
164
|
+
view.setUint32(8, this.#seed, true);
|
|
165
|
+
counters.forEach((c, i) => {
|
|
166
|
+
view.setUint32(PARAMS_SIZE + 4 * i, c, true);
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
equals(other) {
|
|
171
|
+
if (this.#width !== other.#width || this.#depth !== other.#depth || this.#seed !== other.#seed) return false;
|
|
172
|
+
return bytesEqual(counterBytes(this.#counters), counterBytes(other.#counters));
|
|
173
|
+
}
|
|
174
|
+
toJSON() {
|
|
175
|
+
return toJSONEnvelope(this.toBytes());
|
|
176
|
+
}
|
|
177
|
+
static fromJSON(value) {
|
|
178
|
+
return CountMinSketch.fromBytes(fromJSONEnvelope(value));
|
|
179
|
+
}
|
|
180
|
+
count(key) {
|
|
181
|
+
hash32x2Into(key, this.#seed, this.#words);
|
|
182
|
+
let min = Infinity;
|
|
183
|
+
for (let r = 0; r < this.#depth; r++) {
|
|
184
|
+
const at = this.#counters[this.#positionAt(r)] ?? 0;
|
|
185
|
+
if (at < min) min = at;
|
|
186
|
+
}
|
|
187
|
+
return min;
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
//#endregion
|
|
191
|
+
export { BadMagicError, ChecksumError, CountMinOverflowError, CountMinParamMismatchError, CountMinSketch, ParamError, ReservedBitsError, SerializationError, TruncatedError, UnknownHashVariantError, UnknownVersionError, countMinSizing };
|
package/dist/cuckoo/index.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_hasher = require("../hasher-
|
|
2
|
+
const require_hasher = require("../hasher-DPXmP7SH.cjs");
|
|
3
3
|
const require_serialize = require("../serialize-CHikZ4GH.cjs");
|
|
4
4
|
const require_params = require("../params-UTZbJ22c.cjs");
|
|
5
5
|
function cuckooSizing(n, epsilon) {
|
|
@@ -195,7 +195,7 @@ var CuckooFilter = class CuckooFilter {
|
|
|
195
195
|
});
|
|
196
196
|
}
|
|
197
197
|
equals(other) {
|
|
198
|
-
return this.#n === other.#n && this.#epsilon === other.#epsilon && this.#seed === other.#seed && this.#count === other.#count && require_serialize.bytesEqual(wordBytes(this.#words), wordBytes(other.#words));
|
|
198
|
+
return this.#n === other.#n && this.#epsilon === other.#epsilon && this.#seed === other.#seed && this.#count === other.#count && this.#f === other.#f && this.#buckets === other.#buckets && require_serialize.bytesEqual(wordBytes(this.#words), wordBytes(other.#words));
|
|
199
199
|
}
|
|
200
200
|
toJSON() {
|
|
201
201
|
return require_serialize.toJSONEnvelope(this.toBytes());
|
package/dist/cuckoo/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { i as hash128KeyInto, u as reduce } from "../hasher-
|
|
1
|
+
import { i as hash128KeyInto, u as reduce } from "../hasher-DOdmZ-LI.js";
|
|
2
2
|
import { a as TruncatedError, c as assertBodyLength, d as bytesEqual, f as fromJSONEnvelope, g as writeFrame, h as toJSONEnvelope, i as SerializationError, l as assertMinBodyLength, m as readHeader, n as ChecksumError, o as UnknownHashVariantError, r as ReservedBitsError, s as UnknownVersionError, t as BadMagicError, u as assertParamsPadding } from "../serialize-CMSYFym-.js";
|
|
3
3
|
import { i as assertProbability, o as assertUint32, r as assertPositiveInt, t as ParamError } from "../params-CeajSrwv.js";
|
|
4
4
|
function cuckooSizing(n, epsilon) {
|
|
@@ -194,7 +194,7 @@ var CuckooFilter = class CuckooFilter {
|
|
|
194
194
|
});
|
|
195
195
|
}
|
|
196
196
|
equals(other) {
|
|
197
|
-
return this.#n === other.#n && this.#epsilon === other.#epsilon && this.#seed === other.#seed && this.#count === other.#count && bytesEqual(wordBytes(this.#words), wordBytes(other.#words));
|
|
197
|
+
return this.#n === other.#n && this.#epsilon === other.#epsilon && this.#seed === other.#seed && this.#count === other.#count && this.#f === other.#f && this.#buckets === other.#buckets && bytesEqual(wordBytes(this.#words), wordBytes(other.#words));
|
|
198
198
|
}
|
|
199
199
|
toJSON() {
|
|
200
200
|
return toJSONEnvelope(this.toBytes());
|
package/dist/fuse/index.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_hasher = require("../hasher-
|
|
2
|
+
const require_hasher = require("../hasher-DPXmP7SH.cjs");
|
|
3
3
|
const require_serialize = require("../serialize-CHikZ4GH.cjs");
|
|
4
4
|
//#region src/fuse/fuse.ts
|
|
5
5
|
const ARITY = 3;
|
package/dist/fuse/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { i as hash128KeyInto, n as RLO, o as mul64, r as fmix64, t as RHI } from "../hasher-
|
|
1
|
+
import { i as hash128KeyInto, n as RLO, o as mul64, r as fmix64, t as RHI } from "../hasher-DOdmZ-LI.js";
|
|
2
2
|
import { a as TruncatedError, c as assertBodyLength, d as bytesEqual, f as fromJSONEnvelope, g as writeFrame, h as toJSONEnvelope, i as SerializationError, l as assertMinBodyLength, m as readHeader, n as ChecksumError, o as UnknownHashVariantError, r as ReservedBitsError, s as UnknownVersionError, t as BadMagicError } from "../serialize-CMSYFym-.js";
|
|
3
3
|
//#region src/fuse/fuse.ts
|
|
4
4
|
const ARITY = 3;
|
|
@@ -1,11 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
const encoder = new TextEncoder();
|
|
3
|
-
function normalize(input) {
|
|
4
|
-
if (typeof input === "string") return encoder.encode(input);
|
|
5
|
-
if (input instanceof Uint8Array) return input;
|
|
6
|
-
return new Uint8Array(input);
|
|
7
|
-
}
|
|
8
|
-
//#endregion
|
|
1
|
+
import { t as normalize } from "./bytes-Be7tq1F7.js";
|
|
9
2
|
//#region src/core/hasher.ts
|
|
10
3
|
let RLO = 0;
|
|
11
4
|
let RHI = 0;
|
|
@@ -1,11 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
const encoder = new TextEncoder();
|
|
3
|
-
function normalize(input) {
|
|
4
|
-
if (typeof input === "string") return encoder.encode(input);
|
|
5
|
-
if (input instanceof Uint8Array) return input;
|
|
6
|
-
return new Uint8Array(input);
|
|
7
|
-
}
|
|
8
|
-
//#endregion
|
|
1
|
+
const require_bytes = require("./bytes-D2duG1jq.cjs");
|
|
9
2
|
//#region src/core/hasher.ts
|
|
10
3
|
let RLO = 0;
|
|
11
4
|
let RHI = 0;
|
|
@@ -185,7 +178,7 @@ function encodeKey(key) {
|
|
|
185
178
|
encBytes = keyBuf;
|
|
186
179
|
return;
|
|
187
180
|
}
|
|
188
|
-
encBytes = normalize(key);
|
|
181
|
+
encBytes = require_bytes.normalize(key);
|
|
189
182
|
encLen = encBytes.length;
|
|
190
183
|
}
|
|
191
184
|
function keyToLanes(key, seed) {
|
package/dist/hll/index.cjs
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_hasher = require("../hasher-
|
|
2
|
+
const require_hasher = require("../hasher-DPXmP7SH.cjs");
|
|
3
3
|
const require_serialize = require("../serialize-CHikZ4GH.cjs");
|
|
4
4
|
const require_params = require("../params-UTZbJ22c.cjs");
|
|
5
|
-
|
|
5
|
+
function hllSizing(relativeError) {
|
|
6
|
+
require_params.assertProbability(relativeError, "relativeError");
|
|
7
|
+
const p = Math.max(4, Math.ceil(2 * Math.log2(1.04 / relativeError)));
|
|
8
|
+
if (p > 18) throw new require_params.ParamError(`relativeError ${String(relativeError)} needs precision ${String(p)}, above the maximum ${String(18)}`);
|
|
9
|
+
return { p };
|
|
10
|
+
}
|
|
11
|
+
//#endregion
|
|
6
12
|
//#region src/hll/estimate.ts
|
|
7
13
|
const ALPHA_INF = .5 / Math.LN2;
|
|
8
14
|
function sigma(x) {
|
|
@@ -171,7 +177,7 @@ var HyperLogLog = class HyperLogLog {
|
|
|
171
177
|
this.#sparse = new Int32Array(sparseCapacity(this.#registers));
|
|
172
178
|
}
|
|
173
179
|
static create(relativeError) {
|
|
174
|
-
return new HyperLogLog(
|
|
180
|
+
return new HyperLogLog(hllSizing(relativeError));
|
|
175
181
|
}
|
|
176
182
|
static from(keys, relativeError) {
|
|
177
183
|
const sketch = HyperLogLog.create(relativeError);
|
|
@@ -363,4 +369,4 @@ exports.SerializationError = require_serialize.SerializationError;
|
|
|
363
369
|
exports.TruncatedError = require_serialize.TruncatedError;
|
|
364
370
|
exports.UnknownHashVariantError = require_serialize.UnknownHashVariantError;
|
|
365
371
|
exports.UnknownVersionError = require_serialize.UnknownVersionError;
|
|
366
|
-
exports.hllSizing =
|
|
372
|
+
exports.hllSizing = hllSizing;
|
package/dist/hll/index.d.cts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { t as BytesLike } from "../bytes-DCuYtUVS.cjs";
|
|
2
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
3
|
import { t as ParamError } from "../params-DnqJBqLS.cjs";
|
|
4
|
-
import { i as hllSizing, n as HllSizing } from "../sizing-wYOW27eY.cjs";
|
|
5
4
|
//#region src/hll/hll.d.ts
|
|
6
5
|
/** Construction parameters for a {@link HyperLogLog} sketch. */
|
|
7
6
|
interface HllParams {
|
|
@@ -117,4 +116,16 @@ export declare class HyperLogLog {
|
|
|
117
116
|
count(): number;
|
|
118
117
|
}
|
|
119
118
|
//#endregion
|
|
120
|
-
|
|
119
|
+
//#region src/hll/sizing.d.ts
|
|
120
|
+
/** HyperLogLog geometry: the `HllParams` fields a sizing solve determines. */
|
|
121
|
+
interface HllSizing {
|
|
122
|
+
/** Precision: the sketch holds `2 ** p` registers. */
|
|
123
|
+
p: number;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Smallest precision whose standard error `1.04 / sqrt(2 ** p)` meets
|
|
127
|
+
* `relativeError`. Throws {@link ParamError} when even `HLL_MAX_P` cannot.
|
|
128
|
+
*/
|
|
129
|
+
export declare function hllSizing(relativeError: number): HllSizing;
|
|
130
|
+
//#endregion
|
|
131
|
+
export { BadMagicError, ChecksumError, type FilterJSON, type HllParams, type HllSizing, ParamError, ReservedBitsError, SerializationError, TruncatedError, UnknownHashVariantError, UnknownVersionError };
|
package/dist/hll/index.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { t as BytesLike } from "../bytes-DCuYtUVS.js";
|
|
2
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
3
|
import { t as ParamError } from "../params-DnqJBqLS.js";
|
|
4
|
-
import { i as hllSizing, n as HllSizing } from "../sizing-wYOW27eY.js";
|
|
5
4
|
//#region src/hll/hll.d.ts
|
|
6
5
|
/** Construction parameters for a {@link HyperLogLog} sketch. */
|
|
7
6
|
interface HllParams {
|
|
@@ -117,4 +116,16 @@ export declare class HyperLogLog {
|
|
|
117
116
|
count(): number;
|
|
118
117
|
}
|
|
119
118
|
//#endregion
|
|
120
|
-
|
|
119
|
+
//#region src/hll/sizing.d.ts
|
|
120
|
+
/** HyperLogLog geometry: the `HllParams` fields a sizing solve determines. */
|
|
121
|
+
interface HllSizing {
|
|
122
|
+
/** Precision: the sketch holds `2 ** p` registers. */
|
|
123
|
+
p: number;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Smallest precision whose standard error `1.04 / sqrt(2 ** p)` meets
|
|
127
|
+
* `relativeError`. Throws {@link ParamError} when even `HLL_MAX_P` cannot.
|
|
128
|
+
*/
|
|
129
|
+
export declare function hllSizing(relativeError: number): HllSizing;
|
|
130
|
+
//#endregion
|
|
131
|
+
export { BadMagicError, ChecksumError, type FilterJSON, type HllParams, type HllSizing, ParamError, ReservedBitsError, SerializationError, TruncatedError, UnknownHashVariantError, UnknownVersionError };
|
package/dist/hll/index.js
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
|
-
import { i as hash128KeyInto } from "../hasher-
|
|
1
|
+
import { i as hash128KeyInto } from "../hasher-DOdmZ-LI.js";
|
|
2
2
|
import { a as TruncatedError, c as assertBodyLength, f as fromJSONEnvelope, g as writeFrame, h as toJSONEnvelope, i as SerializationError, l as assertMinBodyLength, m as readHeader, n as ChecksumError, o as UnknownHashVariantError, r as ReservedBitsError, s as UnknownVersionError, t as BadMagicError, u as assertParamsPadding } from "../serialize-CMSYFym-.js";
|
|
3
|
-
import { o as assertUint32, t as ParamError } from "../params-CeajSrwv.js";
|
|
4
|
-
|
|
3
|
+
import { i as assertProbability, o as assertUint32, t as ParamError } from "../params-CeajSrwv.js";
|
|
4
|
+
function hllSizing(relativeError) {
|
|
5
|
+
assertProbability(relativeError, "relativeError");
|
|
6
|
+
const p = Math.max(4, Math.ceil(2 * Math.log2(1.04 / relativeError)));
|
|
7
|
+
if (p > 18) throw new ParamError(`relativeError ${String(relativeError)} needs precision ${String(p)}, above the maximum ${String(18)}`);
|
|
8
|
+
return { p };
|
|
9
|
+
}
|
|
10
|
+
//#endregion
|
|
5
11
|
//#region src/hll/estimate.ts
|
|
6
12
|
const ALPHA_INF = .5 / Math.LN2;
|
|
7
13
|
function sigma(x) {
|
package/dist/index.cjs
CHANGED
package/dist/index.js
CHANGED
package/dist/scalable/index.cjs
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const
|
|
3
|
-
const require_hasher = require("../hasher-
|
|
2
|
+
const require_sizing = require("../sizing-Ch0SDdza.cjs");
|
|
3
|
+
const require_hasher = require("../hasher-DPXmP7SH.cjs");
|
|
4
4
|
const require_serialize = require("../serialize-CHikZ4GH.cjs");
|
|
5
5
|
const require_params = require("../params-UTZbJ22c.cjs");
|
|
6
|
-
const require_sizing = require("../sizing-BLzZk2zr.cjs");
|
|
7
6
|
//#region src/scalable/scalable.ts
|
|
8
7
|
const MAX_BITS = 4294967295;
|
|
9
8
|
const TYPE = 6;
|
|
@@ -35,7 +34,7 @@ function readStages(body, entries, start) {
|
|
|
35
34
|
let at = start;
|
|
36
35
|
return entries.map((entry, i) => {
|
|
37
36
|
const length = Math.ceil(entry.m / 8);
|
|
38
|
-
const bits = new
|
|
37
|
+
const bits = new require_sizing.BitSet(entry.m);
|
|
39
38
|
bits.bytes.set(body.subarray(at, at + length));
|
|
40
39
|
for (let j = at + length; j < at + padded8(length); j++) if (body[j] !== 0) throw new require_serialize.SerializationError(`scalable: stage ${String(i)} bit padding is not zero`);
|
|
41
40
|
at += padded8(length);
|
|
@@ -103,7 +102,7 @@ var ScalableBloomFilter = class ScalableBloomFilter {
|
|
|
103
102
|
}
|
|
104
103
|
#open({ capacity, m, k }) {
|
|
105
104
|
const stage = {
|
|
106
|
-
bits: new
|
|
105
|
+
bits: new require_sizing.BitSet(m),
|
|
107
106
|
m,
|
|
108
107
|
k,
|
|
109
108
|
capacity,
|
|
@@ -206,7 +205,7 @@ var ScalableBloomFilter = class ScalableBloomFilter {
|
|
|
206
205
|
this.#assertMergeable(other);
|
|
207
206
|
const [longer, shorter] = this.#stages.length >= other.#stages.length ? [this.#stages, other.#stages] : [other.#stages, this.#stages];
|
|
208
207
|
const stages = longer.map((stage, i) => {
|
|
209
|
-
const bits = new
|
|
208
|
+
const bits = new require_sizing.BitSet(stage.m);
|
|
210
209
|
bits.bytes.set(stage.bits.bytes);
|
|
211
210
|
const twin = shorter[i];
|
|
212
211
|
if (twin) {
|
package/dist/scalable/index.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { i as hash128KeyInto, l as probeLanesInto, s as probeAt } from "../hasher-
|
|
1
|
+
import { n as BitSet, t as bloomSizing } from "../sizing-zxCBlyhO.js";
|
|
2
|
+
import { i as hash128KeyInto, l as probeLanesInto, s as probeAt } from "../hasher-DOdmZ-LI.js";
|
|
3
3
|
import { a as TruncatedError, c as assertBodyLength, d as bytesEqual, f as fromJSONEnvelope, g as writeFrame, h as toJSONEnvelope, i as SerializationError, l as assertMinBodyLength, m as readHeader, n as ChecksumError, o as UnknownHashVariantError, r as ReservedBitsError, s as UnknownVersionError, t as BadMagicError, u as assertParamsPadding } from "../serialize-CMSYFym-.js";
|
|
4
4
|
import { i as assertProbability, o as assertUint32, r as assertPositiveInt, t as ParamError } from "../params-CeajSrwv.js";
|
|
5
|
-
import { t as bloomSizing } from "../sizing-BtUrtvF_.js";
|
|
6
5
|
//#region src/scalable/scalable.ts
|
|
7
6
|
const MAX_BITS = 4294967295;
|
|
8
7
|
const TYPE = 6;
|
|
@@ -29,9 +29,24 @@ var BitSet = class {
|
|
|
29
29
|
}
|
|
30
30
|
};
|
|
31
31
|
//#endregion
|
|
32
|
+
//#region src/core/sizing.ts
|
|
33
|
+
function bloomSizing(n, epsilon) {
|
|
34
|
+
const m = Math.ceil(-n * Math.log(epsilon) / (Math.LN2 * Math.LN2));
|
|
35
|
+
return {
|
|
36
|
+
m,
|
|
37
|
+
k: Math.max(1, Math.round(m / n * Math.LN2))
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
//#endregion
|
|
32
41
|
Object.defineProperty(exports, "BitSet", {
|
|
33
42
|
enumerable: true,
|
|
34
43
|
get: function() {
|
|
35
44
|
return BitSet;
|
|
36
45
|
}
|
|
37
46
|
});
|
|
47
|
+
Object.defineProperty(exports, "bloomSizing", {
|
|
48
|
+
enumerable: true,
|
|
49
|
+
get: function() {
|
|
50
|
+
return bloomSizing;
|
|
51
|
+
}
|
|
52
|
+
});
|