123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197(*
* PMap - Polymorphic maps
* Copyright (C) 1996-2003 Xavier Leroy, Nicolas Cannasse, Markus Mottl
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version,
* with the special exception on linking described in file LICENSE.
*
* This library 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. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*)type('k,'v)map=|Empty|Nodeof('k,'v)map*'k*'v*('k,'v)map*inttype('k,'v)t={cmp:'k->'k->int;map:('k,'v)map;}letheight=function|Node (_,_,_,_,h)->h|Empty->0letmakelkvr=Node(l,k,v,r,max(heightl)(heightr)+1)letballkvr=lethl=heightlinlet hr=heightrinif hl>hr+2thenmatchlwith|Node(ll,lk,lv,lr,_)->ifheightll>=heightlrthenmakelllklv (makelrkvr)else(matchlrwith|Node(lrl,lrk,lrv,lrr,_)->make(makelllklvlrl)lrklrv(makelrr kvr)|Empty->assertfalse)|Empty->assertfalseelseifhr>hl+2thenmatchrwith|Node(rl,rk,rv,rr,_)->ifheightrr>=heightrlthenmake(makelkvrl)rkrvrrelse(matchrlwith|Node(rll,rlk,rlv,rlr,_)->make(makelkvrll)rlkrlv(makerlr rkrvrr)|Empty ->assertfalse)|Empty->assertfalseelseNode(l,k,v,r,maxhlhr+1)letrecmin_binding=function|Node(Empty,k,v,_,_)->k,v|Node(l,_,_,_,_)->min_bindingl|Empty->raiseNot_foundletrecremove_min_binding=function|Node (Empty,_,_,r,_)->r|Node(l,k,v,r,_)->bal(remove_min_bindingl)kvr|Empty->invalid_arg"PMap.remove_min_binding"letmerget1t2=matcht1,t2with|Empty,_->t2|_,Empty->t1|_->letk,v=min_bindingt2inbalt1kv(remove_min_bindingt2)letcreatecmp={cmp=cmp;map =Empty}let empty={cmp=compare;map=Empty}letis_emptyx=x.map=Emptyletaddxd{cmp=cmp;map=map}=letrecloop=function|Node(l,k,v,r,h)->letc=cmpxkinifc=0thenNode(l,x,d,r,h)elseifc<0thenletnl=looplinbalnlkvrelseletnr=looprinballkvnr|Empty->Node(Empty,x,d,Empty,1)in{cmp=cmp;map=loopmap}letfindx{cmp =cmp;map=map}=letrecloop=function|Node(l,k,v,r,_)->letc=cmpxkinifc<0thenlooplelseifc>0thenlooprelsev|Empty->raiseNot_foundinloop mapletremovex{cmp =cmp;map=map}=letrecloop=function|Node(l,k,v,r,_)->letc=cmpxkinifc=0thenmergelrelseifc<0thenbal(loopl)kvrelseballkv(loopr)|Empty->Emptyin{cmp=cmp;map=loopmap}letmemx{cmp =cmp;map=map}=letrecloop=function|Node(l,k,v,r,_)->letc=cmpxkinc=0||loop(ifc<0thenlelser)|Empty->falseinloopmapletexists=memletiterf{map=map}=letrecloop=function|Empty->()|Node(l,k,v,r,_)->loopl;fkv;looprinloopmapletmapf{cmp=cmp;map=map}=letrecloop=function|Empty->Empty|Node(l,k,v,r,h)->letl=looplinletr=looprinNode(l,k,fv,r,h)in{cmp=cmp;map=loopmap}letmapif{cmp =cmp;map=map}=letrecloop=function|Empty->Empty|Node(l,k,v,r,h)->letl=looplinletr=looprinNode(l,k,fkv,r,h)in{cmp=cmp;map=loopmap}letfoldf{cmp =cmp;map=map}acc=letrecloopacc=function|Empty->acc|Node(l,k,v,r,_)->loop(fv(loopaccl))rinloop accmapletfoldif{cmp =cmp;map=map}acc=letrecloopacc=function|Empty->acc|Node(l,k,v,r,_)->loop(fkv(loopaccl))rinloop accmapletrecenumm=letrecmakel=letl=reflinletrecnext()=match!lwith|[]->raiseEnum.No_more_elements|Empty::tl->l:=tl;next()|Node (m1,key,data,m2,h)::tl->l:=m1 ::m2::tl;(key,data)inletcount()=letn=ref0inletr=!lintrywhiletruedoignore(next());incr ndone;assertfalsewithEnum.No_more_elements->l:=r;!ninletclone()=make!linEnum.make~next~count~cloneinmake [m.map]letuncurry_add (k,v)m=addkvmletof_enum?(cmp=compare)e=Enum.folduncurry_add (create cmp)e