-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paths2.ml
More file actions
56 lines (46 loc) · 1 KB
/
s2.ml
File metadata and controls
56 lines (46 loc) · 1 KB
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
open S
module S2 = (struct
type t = { mutable n: int;
mutable dense: int array;
mutable sparse: int array;}
let create n p =
{ n = 0;
dense = Array.make p 0;
sparse = Array.make p 0;
}
let add2_sample t x =
let n = t.n in
t.dense.(n) <- x;
t.sparse.(x) <- n;
t.n <- (n+1)
let in_sample t x =
let rsi = t.sparse.(x) in
let ok = rsi < t.n in
ok && (t.dense.(rsi) = x)
let iter t f =
let n = t.n in
let rec loop i =
if i = n then ()
else
let x = t.dense.(i) in
let () = f x in
loop (i+1)
in
loop 0
let clear_sample t = t.n <- 0
let fill_sample sample n p =
let rec loop i =
if i = 0
then ()
else
let rec find_new () =
let x = R.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
end : S)