@libp2p/utils 5.3.2 → 5.4.0-4ad63bb79

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 (62) hide show
  1. package/dist/src/abort-options.d.ts +7 -0
  2. package/dist/src/abort-options.d.ts.map +1 -0
  3. package/dist/src/abort-options.js +14 -0
  4. package/dist/src/abort-options.js.map +1 -0
  5. package/dist/src/adaptive-timeout.d.ts +35 -0
  6. package/dist/src/adaptive-timeout.d.ts.map +1 -0
  7. package/dist/src/adaptive-timeout.js +63 -0
  8. package/dist/src/adaptive-timeout.js.map +1 -0
  9. package/dist/src/close.d.ts +21 -0
  10. package/dist/src/close.d.ts.map +1 -0
  11. package/dist/src/close.js +49 -0
  12. package/dist/src/close.js.map +1 -0
  13. package/dist/src/filters/bloom-filter.d.ts +34 -0
  14. package/dist/src/filters/bloom-filter.d.ts.map +1 -0
  15. package/dist/src/filters/bloom-filter.js +113 -0
  16. package/dist/src/filters/bloom-filter.js.map +1 -0
  17. package/dist/src/filters/bucket.d.ts +10 -0
  18. package/dist/src/filters/bucket.d.ts.map +1 -0
  19. package/dist/src/filters/bucket.js +53 -0
  20. package/dist/src/filters/bucket.js.map +1 -0
  21. package/dist/src/filters/cuckoo-filter.d.ts +41 -0
  22. package/dist/src/filters/cuckoo-filter.d.ts.map +1 -0
  23. package/dist/src/filters/cuckoo-filter.js +134 -0
  24. package/dist/src/filters/cuckoo-filter.js.map +1 -0
  25. package/dist/src/filters/fingerprint.d.ts +11 -0
  26. package/dist/src/filters/fingerprint.d.ts.map +1 -0
  27. package/dist/src/filters/fingerprint.js +34 -0
  28. package/dist/src/filters/fingerprint.js.map +1 -0
  29. package/dist/src/filters/hashes.d.ts +8 -0
  30. package/dist/src/filters/hashes.d.ts.map +1 -0
  31. package/dist/src/filters/hashes.js +29 -0
  32. package/dist/src/filters/hashes.js.map +1 -0
  33. package/dist/src/filters/index.d.ts +9 -0
  34. package/dist/src/filters/index.d.ts.map +1 -0
  35. package/dist/src/filters/index.js +4 -0
  36. package/dist/src/filters/index.js.map +1 -0
  37. package/dist/src/filters/scalable-cuckoo-filter.d.ts +24 -0
  38. package/dist/src/filters/scalable-cuckoo-filter.d.ts.map +1 -0
  39. package/dist/src/filters/scalable-cuckoo-filter.js +87 -0
  40. package/dist/src/filters/scalable-cuckoo-filter.js.map +1 -0
  41. package/dist/src/filters/utils.d.ts +2 -0
  42. package/dist/src/filters/utils.d.ts.map +1 -0
  43. package/dist/src/filters/utils.js +4 -0
  44. package/dist/src/filters/utils.js.map +1 -0
  45. package/dist/src/moving-average.d.ts +18 -0
  46. package/dist/src/moving-average.d.ts.map +1 -0
  47. package/dist/src/moving-average.js +43 -0
  48. package/dist/src/moving-average.js.map +1 -0
  49. package/package.json +32 -7
  50. package/src/abort-options.ts +20 -0
  51. package/src/adaptive-timeout.ts +94 -0
  52. package/src/close.ts +65 -0
  53. package/src/filters/bloom-filter.ts +142 -0
  54. package/src/filters/bucket.ts +64 -0
  55. package/src/filters/cuckoo-filter.ts +197 -0
  56. package/src/filters/fingerprint.ts +44 -0
  57. package/src/filters/hashes.ts +38 -0
  58. package/src/filters/index.ts +9 -0
  59. package/src/filters/scalable-cuckoo-filter.ts +111 -0
  60. package/src/filters/utils.ts +3 -0
  61. package/src/moving-average.ts +45 -0
  62. package/dist/typedoc-urls.json +0 -76
@@ -0,0 +1,142 @@
1
+ // ported from xxbloom - https://github.com/ceejbot/xxbloom/blob/master/LICENSE
2
+ import { randomBytes } from '@libp2p/crypto'
3
+ import mur from 'murmurhash3js-revisited'
4
+ import { Uint8ArrayList } from 'uint8arraylist'
5
+ import { alloc } from 'uint8arrays/alloc'
6
+ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
7
+ import type { Filter } from './index.js'
8
+
9
+ const LN2_SQUARED = Math.LN2 * Math.LN2
10
+
11
+ export interface BloomFilterOptions {
12
+ seeds?: number[]
13
+ hashes?: number
14
+ bits?: number
15
+ }
16
+
17
+ export class BloomFilter implements Filter {
18
+ public readonly seeds: number[]
19
+ public readonly bits: number
20
+ public buffer: Uint8Array
21
+
22
+ constructor (options: BloomFilterOptions = {}) {
23
+ if (options.seeds != null) {
24
+ this.seeds = options.seeds
25
+ } else {
26
+ this.seeds = generateSeeds(options.hashes ?? 8)
27
+ }
28
+
29
+ this.bits = options.bits ?? 1024
30
+ this.buffer = alloc(Math.ceil(this.bits / 8))
31
+ }
32
+
33
+ /**
34
+ * Add an item to the filter
35
+ */
36
+ add (item: Uint8Array | string): void {
37
+ if (typeof item === 'string') {
38
+ item = uint8ArrayFromString(item)
39
+ }
40
+
41
+ for (let i = 0; i < this.seeds.length; i++) {
42
+ const hash = mur.x86.hash32(item, this.seeds[i])
43
+ const bit = hash % this.bits
44
+
45
+ this.setbit(bit)
46
+ }
47
+ }
48
+
49
+ /**
50
+ * Test if the filter has an item. If it returns false it definitely does not
51
+ * have the item. If it returns true, it probably has the item but there's
52
+ * an `errorRate` chance it doesn't.
53
+ */
54
+ has (item: Uint8Array | string): boolean {
55
+ if (typeof item === 'string') {
56
+ item = uint8ArrayFromString(item)
57
+ }
58
+
59
+ for (let i = 0; i < this.seeds.length; i++) {
60
+ const hash = mur.x86.hash32(item, this.seeds[i])
61
+ const bit = hash % this.bits
62
+
63
+ const isSet = this.getbit(bit)
64
+
65
+ if (!isSet) {
66
+ return false
67
+ }
68
+ }
69
+
70
+ return true
71
+ }
72
+
73
+ /**
74
+ * Reset the filter
75
+ */
76
+ clear (): void {
77
+ this.buffer.fill(0)
78
+ }
79
+
80
+ setbit (bit: number): void {
81
+ let pos = 0
82
+ let shift = bit
83
+ while (shift > 7) {
84
+ pos++
85
+ shift -= 8
86
+ }
87
+
88
+ let bitfield = this.buffer[pos]
89
+ bitfield |= (0x1 << shift)
90
+ this.buffer[pos] = bitfield
91
+ }
92
+
93
+ getbit (bit: number): boolean {
94
+ let pos = 0
95
+ let shift = bit
96
+ while (shift > 7) {
97
+ pos++
98
+ shift -= 8
99
+ }
100
+
101
+ const bitfield = this.buffer[pos]
102
+ return (bitfield & (0x1 << shift)) !== 0
103
+ }
104
+ }
105
+
106
+ /**
107
+ * Create a `BloomFilter` with the smallest `bits` and `hashes` value for the
108
+ * specified item count and error rate.
109
+ */
110
+ export function createBloomFilter (itemcount: number, errorRate: number = 0.005): Filter {
111
+ const opts = optimize(itemcount, errorRate)
112
+ return new BloomFilter(opts)
113
+ }
114
+
115
+ function optimize (itemCount: number, errorRate: number = 0.005): { bits: number, hashes: number } {
116
+ const bits = Math.round(-1 * itemCount * Math.log(errorRate) / LN2_SQUARED)
117
+ const hashes = Math.round((bits / itemCount) * Math.LN2)
118
+
119
+ return { bits, hashes }
120
+ }
121
+
122
+ function generateSeeds (count: number): number[] {
123
+ let buf: Uint8ArrayList
124
+ let j: number
125
+ const seeds = []
126
+
127
+ for (let i = 0; i < count; i++) {
128
+ buf = new Uint8ArrayList(randomBytes(4))
129
+ seeds[i] = buf.getUint32(0, true)
130
+
131
+ // Make sure we don't end up with two identical seeds,
132
+ // which is unlikely but possible.
133
+ for (j = 0; j < i; j++) {
134
+ if (seeds[i] === seeds[j]) {
135
+ i--
136
+ break
137
+ }
138
+ }
139
+ }
140
+
141
+ return seeds
142
+ }
@@ -0,0 +1,64 @@
1
+ import { Fingerprint } from './fingerprint.js'
2
+ import { getRandomInt } from './utils.js'
3
+
4
+ export class Bucket {
5
+ private readonly contents: Array<Fingerprint | null>
6
+
7
+ constructor (size: number) {
8
+ this.contents = new Array(size).fill(null)
9
+ }
10
+
11
+ has (fingerprint: Fingerprint): boolean {
12
+ if (!(fingerprint instanceof Fingerprint)) {
13
+ throw new TypeError('Invalid Fingerprint')
14
+ }
15
+
16
+ return this.contents.some((fp) => {
17
+ return fingerprint.equals(fp)
18
+ })
19
+ }
20
+
21
+ add (fingerprint: Fingerprint): boolean {
22
+ if (!(fingerprint instanceof Fingerprint)) {
23
+ throw new TypeError('Invalid Fingerprint')
24
+ }
25
+
26
+ for (let i = 0; i < this.contents.length; i++) {
27
+ if (this.contents[i] == null) {
28
+ this.contents[i] = fingerprint
29
+ return true
30
+ }
31
+ }
32
+
33
+ return true
34
+ }
35
+
36
+ swap (fingerprint: Fingerprint): Fingerprint | null {
37
+ if (!(fingerprint instanceof Fingerprint)) {
38
+ throw new TypeError('Invalid Fingerprint')
39
+ }
40
+
41
+ const i = getRandomInt(0, this.contents.length - 1)
42
+ const current = this.contents[i]
43
+ this.contents[i] = fingerprint
44
+
45
+ return current
46
+ }
47
+
48
+ remove (fingerprint: Fingerprint): boolean {
49
+ if (!(fingerprint instanceof Fingerprint)) {
50
+ throw new TypeError('Invalid Fingerprint')
51
+ }
52
+
53
+ const found = this.contents.findIndex((fp) => {
54
+ return fingerprint.equals(fp)
55
+ })
56
+
57
+ if (found > -1) {
58
+ this.contents[found] = null
59
+ return true
60
+ } else {
61
+ return false
62
+ }
63
+ }
64
+ }
@@ -0,0 +1,197 @@
1
+ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
2
+ import { Bucket } from './bucket.js'
3
+ import { Fingerprint, MAX_FINGERPRINT_SIZE } from './fingerprint.js'
4
+ import { fnv1a, type Hash } from './hashes.js'
5
+ import { getRandomInt } from './utils.js'
6
+ import type { Filter } from './index.js'
7
+
8
+ const maxCuckooCount = 500
9
+
10
+ export interface CuckooFilterInit {
11
+ /**
12
+ * How many items the filter is expected to contain
13
+ */
14
+ filterSize: number
15
+
16
+ /**
17
+ * How many items to put in each bucket
18
+ */
19
+ bucketSize?: number
20
+
21
+ /**
22
+ * How many bytes the fingerprint is expected to be
23
+ */
24
+ fingerprintSize?: number
25
+
26
+ /**
27
+ * A non-cryptographic hash implementation
28
+ */
29
+ hash?: Hash
30
+
31
+ /**
32
+ * A number used to seed the hash
33
+ */
34
+ seed?: number
35
+ }
36
+
37
+ export class CuckooFilter implements Filter {
38
+ private readonly bucketSize: number
39
+ private readonly filterSize: number
40
+ private readonly fingerprintSize: number
41
+ private readonly buckets: Bucket[]
42
+ public count: number
43
+ private readonly hash: Hash
44
+ private readonly seed: number
45
+
46
+ constructor (init: CuckooFilterInit) {
47
+ this.filterSize = init.filterSize
48
+ this.bucketSize = init.bucketSize ?? 4
49
+ this.fingerprintSize = init.fingerprintSize ?? 2
50
+ this.count = 0
51
+ this.buckets = []
52
+ this.hash = init.hash ?? fnv1a
53
+ this.seed = init.seed ?? getRandomInt(0, Math.pow(2, 10))
54
+ }
55
+
56
+ add (item: Uint8Array | string): boolean {
57
+ if (typeof item === 'string') {
58
+ item = uint8ArrayFromString(item)
59
+ }
60
+
61
+ const fingerprint = new Fingerprint(item, this.hash, this.seed, this.fingerprintSize)
62
+ const j = this.hash.hash(item, this.seed) % this.filterSize
63
+ const k = (j ^ fingerprint.hash()) % this.filterSize
64
+
65
+ if (this.buckets[j] == null) {
66
+ this.buckets[j] = new Bucket(this.bucketSize)
67
+ }
68
+
69
+ if (this.buckets[k] == null) {
70
+ this.buckets[k] = new Bucket(this.bucketSize)
71
+ }
72
+
73
+ if (this.buckets[j].add(fingerprint) || this.buckets[k].add(fingerprint)) {
74
+ this.count++
75
+ return true
76
+ }
77
+
78
+ const rand = [j, k]
79
+ let i = rand[getRandomInt(0, rand.length - 1)]
80
+
81
+ if (this.buckets[i] == null) {
82
+ this.buckets[i] = new Bucket(this.bucketSize)
83
+ }
84
+
85
+ for (let n = 0; n < maxCuckooCount; n++) {
86
+ const swapped = this.buckets[i].swap(fingerprint)
87
+
88
+ if (swapped == null) {
89
+ continue
90
+ }
91
+
92
+ i = (i ^ swapped.hash()) % this.filterSize
93
+
94
+ if (this.buckets[i] == null) {
95
+ this.buckets[i] = new Bucket(this.bucketSize)
96
+ }
97
+
98
+ if (this.buckets[i].add(swapped)) {
99
+ this.count++
100
+
101
+ return true
102
+ } else {
103
+ continue
104
+ }
105
+ }
106
+
107
+ return false
108
+ }
109
+
110
+ has (item: Uint8Array | string): boolean {
111
+ if (typeof item === 'string') {
112
+ item = uint8ArrayFromString(item)
113
+ }
114
+
115
+ const fingerprint = new Fingerprint(item, this.hash, this.seed, this.fingerprintSize)
116
+ const j = this.hash.hash(item, this.seed) % this.filterSize
117
+ const inJ = this.buckets[j]?.has(fingerprint) ?? false
118
+
119
+ if (inJ) {
120
+ return inJ
121
+ }
122
+
123
+ const k = (j ^ fingerprint.hash()) % this.filterSize
124
+
125
+ return this.buckets[k]?.has(fingerprint) ?? false
126
+ }
127
+
128
+ remove (item: Uint8Array | string): boolean {
129
+ if (typeof item === 'string') {
130
+ item = uint8ArrayFromString(item)
131
+ }
132
+
133
+ const fingerprint = new Fingerprint(item, this.hash, this.seed, this.fingerprintSize)
134
+ const j = this.hash.hash(item, this.seed) % this.filterSize
135
+ const inJ = this.buckets[j]?.remove(fingerprint) ?? false
136
+
137
+ if (inJ) {
138
+ this.count--
139
+ return inJ
140
+ }
141
+
142
+ const k = (j ^ fingerprint.hash()) % this.filterSize
143
+ const inK = this.buckets[k]?.remove(fingerprint) ?? false
144
+
145
+ if (inK) {
146
+ this.count--
147
+ }
148
+
149
+ return inK
150
+ }
151
+
152
+ get reliable (): boolean {
153
+ return Math.floor(100 * (this.count / this.filterSize)) <= 95
154
+ }
155
+ }
156
+
157
+ // max load constants, defined in the cuckoo paper
158
+ const MAX_LOAD = {
159
+ 1: 0.5,
160
+ 2: 0.84,
161
+ 4: 0.95,
162
+ 8: 0.98
163
+ }
164
+
165
+ function calculateBucketSize (errorRate: number = 0.001): 2 | 4 | 8 {
166
+ if (errorRate > 0.002) {
167
+ return 2
168
+ }
169
+
170
+ if (errorRate > 0.00001) {
171
+ return 4
172
+ }
173
+
174
+ return 8
175
+ }
176
+
177
+ export function optimize (maxItems: number, errorRate: number = 0.001): CuckooFilterInit {
178
+ // https://www.eecs.harvard.edu/~michaelm/postscripts/cuckoo-conext2014.pdf
179
+ // Section 5.1 Optimal Bucket Size
180
+ const bucketSize = calculateBucketSize(errorRate)
181
+ const load = MAX_LOAD[bucketSize]
182
+
183
+ // https://stackoverflow.com/questions/57555236/how-to-size-a-cuckoo-filter/57617208#57617208
184
+ const filterSize = Math.round(maxItems / load)
185
+ const fingerprintSize = Math.min(Math.ceil(Math.log(filterSize / bucketSize)) + 2, MAX_FINGERPRINT_SIZE)
186
+
187
+ return {
188
+ filterSize,
189
+ bucketSize,
190
+ fingerprintSize
191
+ }
192
+ }
193
+
194
+ export function createCuckooFilter (maxItems: number, errorRate: number = 0.005): Filter {
195
+ const opts = optimize(maxItems, errorRate)
196
+ return new CuckooFilter(opts)
197
+ }
@@ -0,0 +1,44 @@
1
+ import { alloc as uint8ArrayAlloc } from 'uint8arrays/alloc'
2
+ import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
3
+ import type { Hash } from './hashes'
4
+
5
+ export const MAX_FINGERPRINT_SIZE = 64
6
+
7
+ export class Fingerprint {
8
+ private readonly fp: Uint8Array
9
+ private readonly h: Hash
10
+ private readonly seed: number
11
+
12
+ constructor (buf: Uint8Array, hash: Hash, seed: number, fingerprintSize: number = 2) {
13
+ if (fingerprintSize > MAX_FINGERPRINT_SIZE) {
14
+ throw new TypeError('Invalid Fingerprint Size')
15
+ }
16
+
17
+ const fnv = hash.hashV(buf, seed)
18
+ const fp = uint8ArrayAlloc(fingerprintSize)
19
+
20
+ for (let i = 0; i < fp.length; i++) {
21
+ fp[i] = fnv[i]
22
+ }
23
+
24
+ if (fp.length === 0) {
25
+ fp[0] = 7
26
+ }
27
+
28
+ this.fp = fp
29
+ this.h = hash
30
+ this.seed = seed
31
+ }
32
+
33
+ hash (): number {
34
+ return this.h.hash(this.fp, this.seed)
35
+ }
36
+
37
+ equals (other?: any): boolean {
38
+ if (!(other?.fp instanceof Uint8Array)) {
39
+ return false
40
+ }
41
+
42
+ return uint8ArrayEquals(this.fp, other.fp)
43
+ }
44
+ }
@@ -0,0 +1,38 @@
1
+ import fnv1aHash from '@sindresorhus/fnv1a'
2
+ import mur from 'murmurhash3js-revisited'
3
+ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
4
+
5
+ export interface Hash {
6
+ hash(input: Uint8Array, seed: number): number
7
+ hashV(input: Uint8Array, seed: number): Uint8Array
8
+ }
9
+
10
+ export const murmur3: Hash = {
11
+ hash: (input, seed) => {
12
+ return mur.x86.hash32(input, seed)
13
+ },
14
+ hashV: (input, seed) => {
15
+ return numberToBuffer(murmur3.hash(input, seed))
16
+ }
17
+ }
18
+
19
+ export const fnv1a: Hash = {
20
+ hash: (input) => {
21
+ return Number(fnv1aHash(input, {
22
+ size: 32
23
+ }))
24
+ },
25
+ hashV: (input, seed) => {
26
+ return numberToBuffer(fnv1a.hash(input, seed))
27
+ }
28
+ }
29
+
30
+ export function numberToBuffer (num: bigint | number): Uint8Array {
31
+ let hex = num.toString(16)
32
+
33
+ if (hex.length % 2 === 1) {
34
+ hex = `0${hex}`
35
+ }
36
+
37
+ return uint8ArrayFromString(hex, 'base16')
38
+ }
@@ -0,0 +1,9 @@
1
+ export { BloomFilter, createBloomFilter, type BloomFilterOptions } from './bloom-filter.js'
2
+ export { CuckooFilter, createCuckooFilter, type CuckooFilterInit } from './cuckoo-filter.js'
3
+ export { ScalableCuckooFilter, createScalableCuckooFilter, type ScalableCuckooFilterInit } from './scalable-cuckoo-filter.js'
4
+
5
+ export interface Filter {
6
+ add(item: Uint8Array | string): void
7
+ has(item: Uint8Array | string): boolean
8
+ remove?(buf: Uint8Array | string): boolean
9
+ }
@@ -0,0 +1,111 @@
1
+ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
2
+ import { CuckooFilter, optimize, type CuckooFilterInit } from './cuckoo-filter.js'
3
+ import { fnv1a, type Hash } from './hashes.js'
4
+ import { getRandomInt } from './utils.js'
5
+ import type { Filter } from './index.js'
6
+
7
+ export interface ScalableCuckooFilterInit extends CuckooFilterInit {
8
+ /**
9
+ * A number to multiply maxItems by when adding new sub-filters
10
+ */
11
+ scale?: number
12
+ }
13
+
14
+ export class ScalableCuckooFilter implements Filter {
15
+ private readonly filterSize: number
16
+ private readonly bucketSize: number
17
+ private readonly fingerprintSize: number
18
+ private readonly scale: number
19
+ private readonly filterSeries: CuckooFilter[]
20
+ private readonly hash: Hash
21
+ private readonly seed: number
22
+
23
+ constructor (init: ScalableCuckooFilterInit) {
24
+ this.bucketSize = init.bucketSize ?? 4
25
+ this.filterSize = init.filterSize ?? (1 << 18) / this.bucketSize
26
+ this.fingerprintSize = init.fingerprintSize ?? 2
27
+ this.scale = init.scale ?? 2
28
+ this.hash = init.hash ?? fnv1a
29
+ this.seed = init.seed ?? getRandomInt(0, Math.pow(2, 10))
30
+ this.filterSeries = [
31
+ new CuckooFilter({
32
+ filterSize: this.filterSize,
33
+ bucketSize: this.bucketSize,
34
+ fingerprintSize: this.fingerprintSize,
35
+ hash: this.hash,
36
+ seed: this.seed
37
+ })
38
+ ]
39
+ }
40
+
41
+ add (item: Uint8Array | string): boolean {
42
+ if (typeof item === 'string') {
43
+ item = uint8ArrayFromString(item)
44
+ }
45
+
46
+ if (this.has(item)) {
47
+ return true
48
+ }
49
+
50
+ let current = this.filterSeries.find((cuckoo) => {
51
+ return cuckoo.reliable
52
+ })
53
+
54
+ if (current == null) {
55
+ const curSize = this.filterSize * Math.pow(this.scale, this.filterSeries.length)
56
+
57
+ current = new CuckooFilter({
58
+ filterSize: curSize,
59
+ bucketSize: this.bucketSize,
60
+ fingerprintSize: this.fingerprintSize,
61
+ hash: this.hash,
62
+ seed: this.seed
63
+ })
64
+
65
+ this.filterSeries.push(current)
66
+ }
67
+
68
+ return current.add(item)
69
+ }
70
+
71
+ has (item: Uint8Array | string): boolean {
72
+ if (typeof item === 'string') {
73
+ item = uint8ArrayFromString(item)
74
+ }
75
+
76
+ for (let i = 0; i < this.filterSeries.length; i++) {
77
+ if (this.filterSeries[i].has(item)) {
78
+ return true
79
+ }
80
+ }
81
+
82
+ return false
83
+ }
84
+
85
+ remove (item: Uint8Array | string): boolean {
86
+ if (typeof item === 'string') {
87
+ item = uint8ArrayFromString(item)
88
+ }
89
+
90
+ for (let i = 0; i < this.filterSeries.length; i++) {
91
+ if (this.filterSeries[i].remove(item)) {
92
+ return true
93
+ }
94
+ }
95
+
96
+ return false
97
+ }
98
+
99
+ get count (): number {
100
+ return this.filterSeries.reduce((acc, curr) => {
101
+ return acc + curr.count
102
+ }, 0)
103
+ }
104
+ }
105
+
106
+ export function createScalableCuckooFilter (maxItems: number, errorRate: number = 0.001, options?: Pick<ScalableCuckooFilterInit, 'hash' | 'seed' | 'scale'>): Filter {
107
+ return new ScalableCuckooFilter({
108
+ ...optimize(maxItems, errorRate),
109
+ ...(options ?? {})
110
+ })
111
+ }
@@ -0,0 +1,3 @@
1
+ export function getRandomInt (min: number, max: number): number {
2
+ return Math.floor(Math.random() * (max - min)) + min
3
+ }
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Implements exponential moving average. Ported from `moving-average`.
3
+ *
4
+ * @see https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average
5
+ * @see https://www.npmjs.com/package/moving-average
6
+ */
7
+ export class MovingAverage {
8
+ public movingAverage: number
9
+ public variance: number
10
+ public deviation: number
11
+ public forecast: number
12
+ private readonly timespan: number
13
+ private previousTime?: number
14
+
15
+ constructor (timespan: number) {
16
+ this.timespan = timespan
17
+ this.movingAverage = 0
18
+ this.variance = 0
19
+ this.deviation = 0
20
+ this.forecast = 0
21
+ }
22
+
23
+ alpha (t: number, pt: number): number {
24
+ return 1 - (Math.exp(-(t - pt) / this.timespan))
25
+ }
26
+
27
+ push (value: number, time: number = Date.now()): void {
28
+ if (this.previousTime != null) {
29
+ // calculate moving average
30
+ const a = this.alpha(time, this.previousTime)
31
+ const diff = value - this.movingAverage
32
+ const incr = a * diff
33
+ this.movingAverage = a * value + (1 - a) * this.movingAverage
34
+ // calculate variance & deviation
35
+ this.variance = (1 - a) * (this.variance + diff * incr)
36
+ this.deviation = Math.sqrt(this.variance)
37
+ // calculate forecast
38
+ this.forecast = this.movingAverage + a * diff
39
+ } else {
40
+ this.movingAverage = value
41
+ }
42
+
43
+ this.previousTime = time
44
+ }
45
+ }