Source file sampler.ml
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
module type Mass = sig
type t
val encoding : t Data_encoding.t
val zero : t
val of_int : int -> t
val mul : t -> t -> t
val add : t -> t -> t
val sub : t -> t -> t
val ( = ) : t -> t -> bool
val ( <= ) : t -> t -> bool
val ( < ) : t -> t -> bool
end
module type S = sig
type mass
type 'a t
val create : ('a * mass) list -> 'a t
val sample : 'a t -> (int_bound:int -> mass_bound:mass -> int * mass) -> 'a
val encoding : 'a Data_encoding.t -> 'a t Data_encoding.t
end
module Make (Mass : Mass) : S with type mass = Mass.t = struct
type mass = Mass.t
type 'a t = {
total : Mass.t;
support : 'a FallbackArray.t;
p : Mass.t FallbackArray.t;
alias : int FallbackArray.t;
}
let rec init_loop total p alias small large =
match (small, large) with
| ([], _) -> List.iter (fun (_, i) -> FallbackArray.set p i total) large
| (_, []) ->
List.iter (fun (_, i) -> FallbackArray.set p i total) small
| ((qi, i) :: small', (qj, j) :: large') ->
FallbackArray.set p i qi ;
FallbackArray.set alias i j ;
let qj' = Mass.sub (Mass.add qi qj) total in
if Mass.(qj' < total) then
init_loop total p alias ((qj', j) :: small') large'
else init_loop total p alias small' ((qj', j) :: large')
let support :
fallback:'a -> length:int -> ('a * Mass.t) list -> 'a FallbackArray.t =
fun ~fallback ~length measure ->
let a = FallbackArray.make length fallback in
List.iteri (fun i (elt, _) -> FallbackArray.set a i elt) measure ;
a
let check_and_cleanup measure =
let (total, measure) =
List.fold_left
(fun ((total, m) as acc) ((_, p) as point) ->
if Mass.(zero < p) then (Mass.add total p, point :: m)
else if Mass.(p < zero) then invalid_arg "create"
else
acc)
(Mass.zero, [])
measure
in
match measure with
| [] -> invalid_arg "create"
| (fallback, _) :: _ -> (fallback, total, measure)
let create (measure : ('a * Mass.t) list) =
let (fallback, total, measure) = check_and_cleanup measure in
let length = List.length measure in
let n = Mass.of_int length in
let (_, small, large) =
List.fold_left
(fun (i, small, large) (_, p) ->
let q = Mass.mul p n in
if Mass.(q < total) then (i + 1, (q, i) :: small, large)
else (i + 1, small, (q, i) :: large))
(0, [], [])
measure
in
let support = support ~fallback ~length measure in
let p = FallbackArray.make length total in
let alias = FallbackArray.make length (-1) in
init_loop total p alias small large ;
{total; support; p; alias}
let sample {total; support; p; alias} draw_i_elt =
let n = FallbackArray.length support in
let (i, elt) = draw_i_elt ~int_bound:n ~mass_bound:total in
let p = FallbackArray.get p i in
if Mass.(elt < p) then FallbackArray.get support i
else
let j = FallbackArray.get alias i in
assert (Compare.Int.(j >= 0)) ;
FallbackArray.get support j
let array_encoding : 'a Data_encoding.t -> 'a FallbackArray.t Data_encoding.t
=
fun venc ->
let open Data_encoding in
conv
(fun array ->
let length = FallbackArray.length array in
let fallback = FallbackArray.fallback array in
let elements =
List.rev (FallbackArray.fold (fun acc elt -> elt :: acc) array [])
in
(length, fallback, elements))
(fun (length, fallback, elements) ->
let array = FallbackArray.make length fallback in
List.iteri (fun i elt -> FallbackArray.set array i elt) elements ;
array)
(obj3
(req "length" int31)
(req "fallback" venc)
(req "elements" (list venc)))
let mass_array_encoding = array_encoding Mass.encoding
let int_array_encoding = array_encoding Data_encoding.int31
let encoding enc =
let open Data_encoding in
conv
(fun {total; support; p; alias} -> (total, support, p, alias))
(fun (total, support, p, alias) -> {total; support; p; alias})
(obj4
(req "total" Mass.encoding)
(req "support" (array_encoding enc))
(req "p" mass_array_encoding)
(req "alias" int_array_encoding))
end
module Internal_for_tests = struct
module Make = Make
end
module Mass : Mass with type t = int64 = struct
type t = int64
let encoding = Data_encoding.int64
let zero = 0L
let of_int = Int64.of_int
let mul = Int64.mul
let add = Int64.add
let sub = Int64.sub
let ( = ) = Compare.Int64.( = )
let ( <= ) = Compare.Int64.( <= )
let ( < ) = Compare.Int64.( < )
end
include Make (Mass)