123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144(*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=function|Empty->true|_->falseletheight=function|Empty->0|Node(_,_,_,_,h)->hletcreatelxdr=lethl=heightlandhr=heightrinNode(l,x,d,r,ifhl>=hrthenhl+1elsehr+1)letballxdr=lethl=matchlwith|Empty->0|Node(_,_,_,_,h)->hinlethr=matchrwith|Empty->0|Node(_,_,_,_,h)->hinifhl>hr+2thenmatchlwith|Empty->invalid_arg"Map.bal"|Node(ll,lv,ld,lr,_)->(ifheightll>=heightlrthencreatelllvld(createlrxdr)elsematchlrwith|Empty->invalid_arg"Map.bal"|Node(lrl,lrv,lrd,lrr,_)->create(createlllvldlrl)lrvlrd(createlrrxdr))elseifhr>hl+2thenmatchrwith|Empty->invalid_arg"Map.bal"|Node(rl,rv,rd,rr,_)->(ifheightrr>=heightrlthencreate(createlxdrl)rvrdrrelsematchrlwith|Empty->invalid_arg"Map.bal"|Node(rll,rlv,rld,rlr,_)->create(createlxdrll)rlvrld(createrlrrvrdrr))elseNode(l,x,d,r,ifhl>=hrthenhl+1elsehr+1)letrecaddxdata=function|Empty->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=function|Empty->raiseNot_found|Node(l,v,d,r,_)->letc=comparexvinifc=0thendelsefindx(ifc<0thenlelser)letrecmemx=function|Empty->false|Node(l,v,_d,r,_)->letc=comparexvinc=0||memx(ifc<0thenlelser)letrecmin_binding=function|Empty->raiseNot_found|Node(Empty,x,d,_r,_)->(x,d)|Node(l,_x,_d,_r,_)->min_bindinglletrecremove_min_binding=function|Empty->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|_,_->letx,d=min_bindingt2inbalt1xd(remove_min_bindingt2)letrecremovex=function|Empty->Empty|Node(l,v,d,r,_h)->letc=comparexvinifc=0thenmergelrelseifc<0thenbal(removexl)vdrelseballvd(removexr)letreciterf=function|Empty->()|Node(l,v,d,r,_)->iterfl;fvd;iterfrletrecmapf=function|Empty->Empty|Node(l,v,d,r,h)->Node(mapfl,v,fd,mapfr,h)letrecmapif=function|Empty->Empty|Node(l,v,d,r,h)->Node(mapifl,v,fvd,mapifr,h)letrecfoldfmaccu=matchmwith|Empty->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[]