-
Notifications
You must be signed in to change notification settings - Fork 0
/
array_mp.ml
executable file
·99 lines (87 loc) · 2.43 KB
/
array_mp.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
let contains elem tab =
let rec aux = function
| i when i = Array.length tab -> false
| i when tab.(i) = elem -> true
| i -> aux (i+1)
in aux 0
let max_i compare tab =
let taille = Array.length tab
in if taille = 0
then failwith "Array_mp.max_i : Tableau vide"
else
let rec aux i max i_max =
if i = taille then i_max
else let elem = tab.(i)
in if compare elem max = 1
then aux (i + 1) elem i
else aux (i + 1) max i_max
in aux 1 tab.(0) 0
let max compare tab =
if tab = [||] then failwith "Array_mp.max : Tableau vide"
else let i = max_i compare tab
in tab.(i)
let m_index_of tab elem =
let rec aux = function
| i when i = Array.length tab -> None
| i when tab.(i) = elem -> Some i
| i -> aux (i+1)
in aux 0
let index_of tab elem =
match m_index_of tab elem with
Some i -> i
| None -> failwith "Array_mp.index_of : élément non présent dans le tableau"
let l_shift tab index n =
let taille = Array.length tab in
if index >= taille then failwith "l_shift : erreur indice de départ trop grand"
else if n > index then failwith " l_shift : décalage trop grand"
else for i = index to taille - 1 do
tab.(i-n) <- tab.(i)
done
let shuffle tab =
Random.self_init ();
let taille = Array.length tab
in let tab2 = Array.make taille tab.(0)
in let rec aux tab taille =
if taille = 0 then ()
else begin
let i = Random.int taille
in tab2.(taille - 1) <- tab.(i);
aux (Array.append (Array.sub tab 0 i) (Array.sub tab (i + 1) (taille - (i + 1)))) (taille - 1)
end
in aux tab taille;
tab2
let of_string s =
let tab = Array.make (String.length s) 'a'
in let p i car = tab.(i) <- car
in String_mp.iteri p s;
tab
let display f array =
print_string "[|";
(match Array.length array with
0 -> ()
| 1 -> f array.(0)
| n ->
f array.(0);
Array.iter (fun x ->
print_char ';';
f x)
(Array.sub array 1 (n - 1)));
print_string "|]"
let dicho_member x f tab =
let length = Array.length tab in
let rec aux debut fin =
if debut > fin then
false
else
let milieu = (debut + fin) / 2
in match f tab.(milieu) x with
0 -> true
| -1 -> aux (milieu + 1) fin
| _ -> aux debut (milieu - 1)
in aux 0 length
let foldi_left f acc tab =
let rec aux i acc =
if i = Array.length tab
then acc
else aux (i + 1) (f i acc tab.(i))
in aux 0 acc