1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
module A = BatArray
module Fp = Fingerprint
module L = BatList
module Log = Dolog.Log
type t = int array array
let distinct_rands rng n bound =
let rec loop acc count =
if count = n then
acc
else
let cand = Random.State.int rng bound in
if List.mem cand acc then
loop acc count
else
loop (cand :: acc) (count + 1) in
loop [] 0
let init n k m =
let res = Array.make_matrix n k 0 in
let rng = Random.State.make [|3141596|] in
for i = 0 to n - 1 do
let rands = distinct_rands rng k m in
L.iteri (fun j rand ->
res.(i).(j) <- rand
) rands
done;
let collisions = ref 0 in
let sorted = A.copy res in
A.sort compare sorted;
for i = 1 to n - 1 do
if sorted.(i - 1) = sorted.(i) then
incr collisions;
done;
(if !collisions > 0 then
Log.warn "Bloom.init(%d,%d,%d): %d collisions" n k m !collisions
);
(n, k, m, res)
let encode (_n, k, m, mappings) fp =
let kvs = Fp.key_value_pairs fp in
let res = A.create m 0 in
L.iter (fun (key, value) ->
let output_indexes = mappings.(key) in
for i = 0 to k - 1 do
let j = output_indexes.(i) in
res.(j) <- res.(j) + value
done
) kvs;
res