123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152(*pad: same than for Setb, module Make(Ord: OrderedType) = struct *)(***********************************************************************)(* *)(* Objective Caml *)(* *)(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)(* *)(* Copyright 1996 Institut National de Recherche en Informatique et *)(* en Automatique. All rights reserved. This file is distributed *)(* under the terms of the GNU Library General Public License, with *)(* the special exception on linking described in file ../LICENSE. *)(* *)(***********************************************************************)(* map.ml 1.15 2004/04/23 10:01:33 xleroy Exp *)(*
type key = Ord.t
type 'a t =
Empty
| Node of 'a t * key * 'a * 'a t * int
*)type('key,'v)t=Empty|Nodeof('key,'v)t*'key*'v*('key,'v)t*intletempty=Emptyletis_empty=functionEmpty->true|_->falseletheight=functionEmpty->0|Node(_,_,_,_,h)->hletcreatelxdr=lethl=heightlandhr=heightrinNode(l,x,d,r,(ifhl>=hrthenhl+1elsehr+1))letballxdr=lethl=matchlwithEmpty->0|Node(_,_,_,_,h)->hinlethr=matchrwithEmpty->0|Node(_,_,_,_,h)->hinifhl>hr+2thenbeginmatchlwithEmpty->invalid_arg"Map.bal"|Node(ll,lv,ld,lr,_)->ifheightll>=heightlrthencreatelllvld(createlrxdr)elsebeginmatchlrwithEmpty->invalid_arg"Map.bal"|Node(lrl,lrv,lrd,lrr,_)->create(createlllvldlrl)lrvlrd(createlrrxdr)endendelseifhr>hl+2thenbeginmatchrwithEmpty->invalid_arg"Map.bal"|Node(rl,rv,rd,rr,_)->ifheightrr>=heightrlthencreate(createlxdrl)rvrdrrelsebeginmatchrlwithEmpty->invalid_arg"Map.bal"|Node(rll,rlv,rld,rlr,_)->create(createlxdrll)rlvrld(createrlrrvrdrr)endendelseNode(l,x,d,r,(ifhl>=hrthenhl+1elsehr+1))letrecaddxdata=functionEmpty->Node(Empty,x,data,Empty,1)|Node(l,v,d,r,h)->letc=comparexvinifc=0thenNode(l,x,data,r,h)elseifc<0thenbal(addxdatal)vdrelseballvd(addxdatar)letrecfindx=functionEmpty->raiseNot_found|Node(l,v,d,r,_)->letc=comparexvinifc=0thendelsefindx(ifc<0thenlelser)letrecmemx=functionEmpty->false|Node(l,v,d,r,_)->letc=comparexvinc=0||memx(ifc<0thenlelser)letrecmin_binding=functionEmpty->raiseNot_found|Node(Empty,x,d,r,_)->(x,d)|Node(l,x,d,r,_)->min_bindinglletrecremove_min_binding=functionEmpty->invalid_arg"Map.remove_min_elt"|Node(Empty,x,d,r,_)->r|Node(l,x,d,r,_)->bal(remove_min_bindingl)xdrletmerget1t2=match(t1,t2)with(Empty,t)->t|(t,Empty)->t|(_,_)->let(x,d)=min_bindingt2inbalt1xd(remove_min_bindingt2)letrecremovex=functionEmpty->Empty|Node(l,v,d,r,h)->letc=comparexvinifc=0thenmergelrelseifc<0thenbal(removexl)vdrelseballvd(removexr)letreciterf=functionEmpty->()|Node(l,v,d,r,_)->iterfl;fvd;iterfrletrecmapf=functionEmpty->Empty|Node(l,v,d,r,h)->Node(mapfl,v,fd,mapfr,h)letrecmapif=functionEmpty->Empty|Node(l,v,d,r,h)->Node(mapifl,v,fvd,mapifr,h)letrecfoldfmaccu=matchmwithEmpty->accu|Node(l,v,d,r,_)->foldfl(fvd(foldfraccu))(* addons pad *)letof_listxs=List.fold_left(funacc(k,v)->addkvacc)emptyxsletto_listt=fold(funkvacc->(k,v)::acc)t[]