46 lines
1.3 KiB
OCaml
46 lines
1.3 KiB
OCaml
|
(*Q1*)
|
||
|
let h_exemple s = (Char.code (Char.uppercase_ascii s.[0])) - 65;;
|
||
|
h_exemple "z";;
|
||
|
type ('a, 'b) dict = {
|
||
|
hache: 'a -> int;
|
||
|
table_hachage: ('a *'b) list array;
|
||
|
largeur: int};;
|
||
|
(*Q2*)
|
||
|
let creer_dict_vide n hache = {
|
||
|
hache=hache;
|
||
|
table_hachage=(Array.make n []);
|
||
|
largeur=n};;
|
||
|
|
||
|
let d_exemple = creer_dict_vide 25 h_exemple;;
|
||
|
(*Q3*)
|
||
|
|
||
|
let contient cle d =
|
||
|
let h = d.hache cle in
|
||
|
List.mem cle (List.map fst d.table_hachage.(h));;
|
||
|
contient "blablab" d_exemple;;
|
||
|
let inserer cle enregistrement d =
|
||
|
if contient cle d then failwith "Déjà dans le dictionnaire" else ();
|
||
|
let h = d.hache cle in
|
||
|
d.table_hachage.(h) <- ((cle, enregistrement)::d.table_hachage.(h));;
|
||
|
|
||
|
let valeur cle d =
|
||
|
let rec aux acc = match acc with
|
||
|
|[] -> raise Not_found
|
||
|
|(c, enregistrement)::t -> if c=cle then enregistrement else aux t in
|
||
|
aux d.table_hachage.(d.hache cle);;
|
||
|
|
||
|
let supprimer cle d =
|
||
|
let h = d.hache cle in
|
||
|
let rec aux acc li = match li with
|
||
|
| [] -> raise Not_found
|
||
|
| (c, e)::t -> if c=cle then acc@t else aux ((c,e)::acc) t in
|
||
|
d.table_hachage.(h) <- aux [] d.table_hachage.(h);;
|
||
|
let modifier cle enregistrement d =
|
||
|
supprimer cle d;
|
||
|
inserer cle enregistrement d;;
|
||
|
|
||
|
inserer "rire" "voilà" d_exemple;;
|
||
|
valeur "mais" d_exemple;;
|
||
|
supprimer "rigolo" d_exemple;;
|
||
|
modifier "blablab" "truc" d_exemple;;
|
||
|
d_exemple;;
|