forked from UnixJunkie/vp-tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyArray.ml
67 lines (61 loc) · 1.57 KB
/
myArray.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
(* extend the Array module *)
include Array
(* smaller array, without elt at index 'i' *)
let remove i a =
let n = length a in
assert(i >= 0 && i < n);
let res = make (n - 1) (unsafe_get a 0) in
let j = ref 0 in
for i' = 0 to n - 1 do
if i' <> i then
(unsafe_set res !j (unsafe_get a i');
incr j)
done;
res
(* <=> List.partition *)
let partition p a =
let ok, ko =
fold_right (fun x (ok_acc, ko_acc) ->
if p x then (x :: ok_acc, ko_acc)
else (ok_acc, x :: ko_acc)
) a ([], [])
in
(of_list ok, of_list ko)
(* <=> List.split *)
let split a =
let n = length a in
if n = 0 then ([||], [||])
else
let l, r = unsafe_get a 0 in
let left = make n l in
let right = make n r in
for i = 1 to n - 1 do
let l, r = unsafe_get a i in
unsafe_set left i l;
unsafe_set right i r
done;
(left, right)
(* <=> BatArray.min_max with default value in case of empty array *)
let min_max_def a def =
let n = length a in
if n = 0 then def
else
let mini = ref (unsafe_get a 0) in
let maxi = ref (unsafe_get a 0) in
for i = 1 to n - 1 do
let x = unsafe_get a i in
if x < !mini then mini := x;
if x > !maxi then maxi := x
done;
(!mini, !maxi)
(* get one bootstrap sample of 'size' using sampling with replacement *)
let bootstrap_sample rng size a =
let n = length a in
assert(n > 0);
assert(size < n);
let res = make size (unsafe_get a 0) in
for i = 0 to size - 1 do
let rand = Random.State.int rng n in
unsafe_set res i (unsafe_get a rand)
done;
res