123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117(**************************************************************************)(* *)(* Ocamlgraph: a generic graph library for OCaml *)(* Copyright (C) 2004-2010 *)(* Sylvain Conchon, Jean-Christophe Filliatre and Julien Signoles *)(* *)(* This software is free software; you can redistribute it and/or *)(* modify it under the terms of the GNU Library General Public *)(* License version 2.1, with the special exception on linking *)(* described in file LICENSE. *)(* *)(* This software is distributed in the hope that it will be useful, *)(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *)(* *)(**************************************************************************)(* $Id$ *)moduletypeHashedOrderedType=sigtypetvalequal:t->t->boolvalhash:t->intvalcompare:t->t->intendmoduletypeS=sigtypeelttypetvalinit:eltlist->tvalfind:elt->t->eltvalunion:elt->elt->t->unitendmoduleMake(X:HashedOrderedType)=structtypeelt=X.tmoduleH=Hashtbl.Make(X)typecell={mutablec:int;data:elt;mutablefather:cell}typet=cellH.t(* a forest *)letinitl=leth=H.create997inList.iter(funx->letreccell={c=0;data=x;father=cell}inH.addhxcell)l;hletrecfind_auxcell=ifcell.father==cellthencellelseletr=find_auxcell.fatherincell.father<-r;rletfindxh=(find_aux(H.findhx)).dataletunionxyh=letrx=find_aux(H.findhx)inletry=find_aux(H.findhy)inifrx!=rythenbeginifrx.c>ry.cthenry.father<-rxelseifrx.c<ry.cthenrx.father<-ryelsebeginrx.c<-rx.c+1;ry.father<-rxendendend(*** test ***)(***
module M = Make (struct
type t = int let
hash = Hashtbl.hash
let compare = compare
let equal = (=)
end)
open Printf
let saisir s =
printf "%s = " s; flush stdout;
let x = read_int () in
x
let h = M.init [0;1;2;3;4;5;6;7;8;9]
let () = if not !Sys.interactive then
while true do
printf "1) find\n2) union\n";
match read_int () with
1 -> begin
let x = saisir "x" in
printf "%d\n" (M.find x h)
end
| 2 -> begin
let x, y = saisir "x", saisir "y" in
M.union x y h
end
| _ -> ()
done
***)