-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paths0.ml
More file actions
50 lines (44 loc) · 1007 Bytes
/
s0.ml
File metadata and controls
50 lines (44 loc) · 1007 Bytes
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
open R
open S
module S0 = (struct
type t = {mutable c: int; es:int array}
let create n p = {c = 0; es = Array.make n 0}
let in_sample t x =
let rec loop i =
if i < 0 then false
else
if t.es.(i) = x
then true
else loop (i-1)
in
loop (t.c -1)
let add2_sample t x =
let i = t.c in
t.es.(i) <- x;
t.c <- i+1
let fill_sample sample n p =
let rec loop i =
if i = 0
then ()
else
let rec find_new () =
let x = random_range p in
if in_sample sample x
then find_new()
else add2_sample sample x
in
let () = find_new () in
loop (i-1)
in
loop n
let clear_sample t = t.c <- 0
let iter t f =
let rec loop i =
if i = t.c
then ()
else
let () = f t.es.(i) in
loop (i+1)
in
loop 0
end : S)