123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867(*
* Splay -- splay trees
* Copyright (C) 2011 Batteries Included Development Team
*
* 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
*)moduleList=structincludeListincludeBatListendmoduleEnum=BatEnumtype'abst=Empty|Nodeof'abst*'a*'abstletsize =letreccounttrk=matchtrwith|Empty->k0|Node(l,_,r)->countl(funm->countr(funn->k(1+m+n)))infuntr->counttr(funn->n)letbst_appendlr=letreccat=function|Empty->r|Node(l,x,r)->Node(l,x,catr)incatltype'astep=|Leftof'a*'abst|Rightof'abst*'atype'acursor=Cof 'asteplist*'abstletrec top' cxt=matchcxwith|[]->t|(Left(p,pr)::cx)->top'cx (Node(t,p,pr))|(Right(pl,p)::cx)->top'cx(Node(pl,p,t))lettop(C(cx,t))=top'cxtletreccsplay'cxlr=matchcxwith|[]->(l,r)|[Left(p,pr)]->(l,Node(r,p,pr))|[Right(pl,px)]->(Node(pl,px,l),r)|(Left(px,pr)::Left(ppx,ppr):: cx)->(* zig zig *)letr=Node(r,px,Node(pr,ppx,ppr))incsplay'cxlr|(Left(px,pr)::Right(ppl,ppx)::cx)->(* zig zag *)letl=Node(ppl,ppx,l)inletr=Node(r,px,pr)incsplay'cx lr|(Right(pl,px)::Right(ppl,ppx)::cx)->(* zig zig *)letl=Node(Node(ppl,ppx,pl),px,l)incsplay' cxlr|(Right(pl,px)::Left(ppx,ppr):: cx)->(* zig zag *)let l=Node(pl,px,l)inletr=Node(r,ppx,ppr)incsplay'cxlrletcsplay=function|C(cx,Node(l,x,r))->letl',r'=csplay'cxlrinNode(l',x,r')|_->raiseNot_foundletreccfind?(cx=[])~sel=function|Empty->C(cx,Empty)|Node(l,x,r)asnode->letsx=selxinifsx=0thenC(cx,node)elseifsx<0then cfind~cx:(Left (x,r)::cx)~sellelsecfind~cx:(Right(l,x)::cx)~selr(* Asplay tree isa binary tree that is dynamically balanced: when
a key is accessed, the tree is rebalanced (by an internal mutation) so
that the next accesses to the same or neighbouring keys are very fast.
Despite the use of a mutation for rebalancing, the structure is
observably pure/persistent, as the mutation does not change the set
of elements.
*)moduleStrongRef:sigtype+##V>=4.12##!'atvalref:'a->'atvalget:'at->'aval set:'at->'a->unitend=struct(* Didactic implementation note : why that ugly Obj.magic below?
What does StrongRef bring compared to the usual ('a ref) type?
We want splay tree to respect the Map interface, which whose map
type is covariant (type (+'a) t). OCaml checks the internal
definition to verify that the internal datatype is consistent
with the variance annotation. Using a reference in the
implementation of BatSplay would make the compiler reject the
implementation, because reference types must be invariant.
Following is an explanation of covariance and reference
invariance, feel free to skip it if you already know.
The idea of covariance for data structure is the following : if
you have an ('a list), and a type 'b which is less specific than
'a (a subtype, eg. with OCaml polymorphic variants or object
types), you can at any type pretend that your list is
a ('b list): if all 'a can be used as 'b, then all ('a list) can
be used as ('b list).
# type a = < f1 : int; f2 : float >;;
# type b = < f1 : int >;;
# let t : a = object method f1 = 1 method f2 = 2. end;;
val t : a = <obj>
# (t :> b);;
- : b = <obj>
# ([t] :> b list);;
- : b list = [<obj>]
But this is not true for ('a list ref), or else I may locally
consider it a ('b list) and mutate it to add an element of type
'b in it, then observe it at type ('a list ref) again. This is
unsound because the added 'b element won't behave correctly as
a 'a.
# let tref = ref [t];;
# (tref :> b list ref);;
Error: Type a list ref is not a subtype of b list ref
Type a = < f1 : int; f2 : float > is not compatible with type
b = < f1 : int >
The second object type has no method f2
Imagine I think I know better, and break the type safety.
# let forced_tref = (Obj.magic tref : b list ref);;
Then I can add a element of type b to the list :
# forced_tref := object method f1 = 1 end :: !forced_tref;;
But this is unsound as I can now look at tref again, at type
(a list ref).
# !tref;;
- : a list = [<obj>; <obj>]
# (List.hd !tref)#f2;;
Segmentation fault
So in general, reference types cannot be safely subtyped (note
that Java has had a blatant flaw in its type system for years, as
mutable Arrays were covariant). If we used a `ref` in the
internal definition of BatSplay.t, the typer would reject the
module (the interface claims its covariant, while it's
invariant).
Said otherwise, covariance of a type (+'a t) allows situations
where a single value may have several distinct types
simultaneously:
- the empty list [] is both an (int list) and a (float list)
(distinct types here come from instantiations of the polymorphic
'a list, generalized by the (relaxed) value restrict)
- if a is a subtype of b, then all (a list) (even non-empty)
are simultaneously of type (b list)
Mutating such values is unsound in the general case, if the
result of the mutation is a value that is not valid for some of
those simultaneous types (adding a float in a ('a list ref) makes
it invalid as an (int list ref)).
In our case however, the mutations that actually happen (that are
confined in the internal implementation of BatSplay) are soundly
compatible with subtyping or polymorphic instantiation. Indeed,
rebalancing never adds any element to the splay tree, it only
reorders the element that were already there. In particular,
sharing values between two different types (either through
subtyping (cast) or polymorphic instantiation
(relaxed value restriction)) is correct even if mutations happens
on those shared value.. However, we must be careful to ensure
that all rebalancings keep the set of elements of the splay tree
unchanged (dropping elements would be ok-ish, but adding new
elements would be unsound).
We use the dirty Obj magic to create a type of "strong
references" that are mutable yet covariant. Note that the
mutations are confined to the "top" of the structure, the
balanced tree itself is purely functional. Note that we must be
careful (in the internal implementation) to allocate a new strong
reference (with StrongRef.ref) each time we want to build a tree
with a different set of elements than the one we started with.
PS : No list reference were harmed during the implementation of
this module.
*)type'at={ref:'a}type'amut={mutablemut_ref:'a}letref(x:'a)=(Obj.magic{mut_ref=x}:'at)letgetr=r.refletset(r:'at)v=(Obj.magicr:'amut).mut_ref<-vendmoduleMap (Ord:BatInterfaces.OrderedType)=struct(*$inject
module TestMap = Splay.Map (Int)
*)(*$< TestMap *)typekey=Ord.ttype'amap=(key*'a)bsttype'at='amapStrongRef.tletsget =StrongRef.getletsref=StrongRef.refletempty=srefEmptyletis_emptym=lettr=sgetmintr=Empty(* let kcmp (j, _) (k, _) = Ord.compare j k*)letkselj(k,_)=Ord.comparejkletsingleton'kv=Node(Empty,(k,v),Empty)letsingletonkv=sref(singleton'kv)letaddkvtr=lettr=sgettrinsrefbegincsplaybeginmatchcfind~sel:(kselk)trwith|C(cx,Node(l,(k,_),r))->C(cx,Node(l,(k,v),r))|C(cx,Empty)->C(cx,singleton'kv)endendletmodifykfntr=let tr=sgettrinsrefbegincsplaybeginmatchcfind~sel:(kselk)trwith|C(cx,Node(l,(k,v),r))->C(cx,Node(l,(k,fnv),r))|C(_cx,Empty)->raiseNot_foundendendletmodify_defdefkfntr=let tr=sgettrinsrefbegincsplaybeginmatchcfind~sel:(kselk)trwith|C(cx,Node(l,(k,v),r))->C(cx,Node(l,(k,fnv),r))|C(cx,Empty)->C(cx,singleton'k(fndef))endendletmodify_optkfntr=let tr=sgettrinsrefbegintrymatchcfind~sel:(kselk)trwith|C(cx,Node(l,(k,v),r))->beginmatchfn(Somev)with|Somev'->csplay(C(cx,Node(l,(k,v'),r)))|None->bst_appendlrend|C(cx,Empty)->matchfnNonewith|Somev->csplay(C(cx,singleton'kv))|None->raiseExitwithExit->trendletrebalancemtr=StrongRef.setmtrletfindkm=lettr=sgetminlettr=csplay(cfind~sel:(kselk)tr)inmatchtrwith|Node(_,(_,v),_)->rebalancemtr;v|_->raiseNot_foundletfind_optkm=trySome(findkm)withNot_found->Noneletfind_defaultdefkm=tryfindkmwithNot_found->defletrecfind_first_helper_foundfkvmap=function|Node(l,(k,v),r)->iffkthenfind_first_helper_foundf(k,v)maplelse find_first_helper_foundfkvmapr|Empty ->(* dummy find to rebalance the tree *)ignore(find(fstkv)map);kvletfind_firstf(map:'at)=letrecloop_notfoundf=function|Node(l,(k,v),r)->iffkthenfind_first_helper_foundf(k,v)maplelseloop_notfoundfr|Empty->raiseNot_foundinloop_notfoundf(sgetmap)letfind_first_opt fmap=letrecloop_notfoundf=function|Node(l,(k,v),r)->iffkthenSome(find_first_helper_foundf(k,v)mapl)elseloop_notfoundfr|Empty->Noneinloop_notfoundf(sgetmap)letrec find_last_helper_foundfkvmap=function|Node(l,(k,v),r)->iffkthenfind_last_helper_foundf(k,v)maprelse find_last_helper_foundfkvmapl|Empty ->(* dummy find to rebalance the tree *)ignore(find(fstkv)map);kvletfind_lastf(map:'at)=letrecloop_notfoundf=function|Node(l,(k,v),r)->iffkthenfind_last_helper_foundf(k,v)maprelseloop_notfoundfl|Empty->raiseNot_foundinloop_notfoundf(sgetmap)letfind_last_opt fmap=letrecloop_notfoundf=function|Node(l,(k,v),r)->iffkthenSome(find_last_helper_foundf(k,v)mapr)elseloop_notfoundfl|Empty->Noneinloop_notfoundf(sgetmap)(*$T find_first
(empty |> add 1 11 |> add 2 12 |> add 3 13 |> find_first (fun x -> x >= 0)) = ((1, 11))
(empty |> add 1 11 |> add 2 12 |> add 3 13 |> find_first (fun x -> x >= 1)) = ((1, 11))
(empty |> add 1 11 |> add 2 12 |> add 3 13 |> find_first (fun x -> x >= 2)) = ((2, 12))
(empty |> add 1 11 |> add 2 12 |> add 3 13 |> find_first (fun x -> x >= 3)) = ((3, 13))
try ignore(empty |> add 1 11 |> add 2 12 |> add 3 13 |> find_first (fun x -> x >= 4)); false with Not_found -> true
try ignore(empty |> find_first (fun x -> x >= 3)); false with Not_found -> true
*)(*$T find_first_opt
(empty |> add 1 11 |> add 2 12 |> add 3 13 |> find_first_opt (fun x -> x >= 0)) = (Some (1, 11))
(empty |> add 1 11 |> add 2 12 |> add 3 13 |> find_first_opt (fun x -> x >= 1)) = (Some (1, 11))
(empty |> add 1 11 |> add 2 12 |> add 3 13 |> find_first_opt (fun x -> x >= 2)) = (Some (2, 12))
(empty |> add 1 11 |> add 2 12 |> add 3 13 |> find_first_opt (fun x -> x >= 3)) = (Some (3, 13))
(empty |> add 1 11 |> add 2 12 |> add 3 13 |> find_first_opt (fun x -> x >= 4)) = (None)
(empty |> find_first_opt (fun x -> x >= 3)) = (None)
*)(*$T find_last
(empty |> add 1 11 |> add 2 12 |> add 3 13 |> find_last (fun x -> x <= 1)) = (1, 11)
(empty |> add 1 11 |> add 2 12 |> add 3 13 |> find_last (fun x -> x <= 2)) = (2, 12)
(empty |> add 1 11 |> add 2 12 |> add 3 13 |> find_last (fun x -> x <= 3)) = (3, 13)
(empty |> add 1 11 |> add 2 12 |> add 3 13 |> find_last (fun x -> x <= 4)) = (3, 13)
try ignore(empty |> add 1 11 |> add 2 12 |> add 3 13 |> find_last (fun x -> x <= 0)); false with Not_found -> true
try ignore(empty |> find_last (fun x -> x <= 3)); false with Not_found -> true
*)(*$T find_last_opt
(empty |> add 1 11 |> add 2 12 |> add 3 13 |> find_last_opt (fun x -> x <= 0)) = None
(empty |> add 1 11 |> add 2 12 |> add 3 13 |> find_last_opt (fun x -> x <= 1)) = Some (1, 11)
(empty |> add 1 11 |> add 2 12 |> add 3 13 |> find_last_opt (fun x -> x <= 2)) = Some (2, 12)
(empty |> add 1 11 |> add 2 12 |> add 3 13 |> find_last_opt (fun x -> x <= 3)) = Some (3, 13)
(empty |> add 1 11 |> add 2 12 |> add 3 13 |> find_last_opt (fun x -> x <= 4)) = Some (3, 13)
(empty |> find_last_opt (fun x -> x <= 3)) = None
*)letcchangefn(C(cx,t))=C(cx,fnt)letremovektr=lettr=sgettrinletreplace=function|Empty->Empty|Node(l,_,r)->bst_appendlrinlettr=top(cchangereplace(cfind~sel:(kselk)tr))insref trletremove_exnktr=lettr=sgettrinletreplace=function|Empty->raiseNot_found|Node(l,_,r)->bst_appendlrinlettr=top(cchangereplace(cfind~sel:(kselk)tr))insref tr(*$T remove_exn
try remove_exn 1 empty |> ignore ; false with Not_found -> true
*)letupdatek1k2v2tr=ifOrd.comparek1k2<>0thenaddk2v2(removek1tr)elselettr=sgettrinsref begincsplaybeginmatchcfind~sel:(kselk1)trwith|C(cx,Node(l,_kv,r))->C(cx,Node(l,(k2,v2),r))|C(_cx,Empty)->raiseNot_foundendendletupdate_stdlibkfm=matchf(find_optkm)with|Somex->addkxm|None->removekmletmemkm=tryignore(findkm);truewithNot_found->falseletiterfntr=let tr=sgettrinletrecvisit=function|Empty->()|Node(l,(k,v),r)->visitl;fnkv;visitrinvisittrletfoldfntracc=lettr=sgettrinletrecvisitacc=function|Empty->acc|Node (l,(k,v),r)->letacc=visitacc linletacc=fnkvacc invisitaccrinvisitacctrletmin_bindingtr=let tr=sgettrinletrecbfind=function|Node(Empty,kv,_)->kv|Node(l,_,_)->bfindl|Empty->raiseNot_foundinbfindtrletmin_binding_opttr=lettr=sgettrinletrecbfind=function|Node(Empty,kv,_)->Somekv|Node(l,_,_)->bfindl|Empty->Noneinbfindtrletchoose=min_binding(*$= choose
(empty |> add 0 1 |> add 1 1 |> choose) \
(empty |> add 1 1 |> add 0 1 |> choose)
*)(*$T choose
try ignore (choose empty) ; false with Not_found -> true
*)letchoose_opt=min_binding_optletanytr=matchsgettrwith|Empty->raiseNot_found|Node(_,kv,_)->kv(*$T any
try ignore (any empty) ; false with Not_found -> true
*)letpop_min_bindingtr=letmini=ref(choosetr)inletrecbfind=function|Node(Empty,kv,r)->mini:=kv;r|Node(l,kv,r)->Node(bfindl,kv,r)|Empty->assert(false)(* choose already raises Not_found on empty map *)in(!mini,sref(bfind (sgettr)))letmax_bindingtr=let tr=sgettrinletrecbfind=function|Node(_,kv,Empty)->kv|Node(_,_,r)->bfindr|Empty->raiseNot_foundinbfindtrletmax_binding_opttr=lettr=sgettrinletrecbfind=function|Node(_,kv,Empty)->Somekv|Node(_,_,r)->bfindr|Empty->Noneinbfindtrletpop_max_bindingtr=letmaxi=ref(choosetr)inletrecbfind=function|Node(l,kv,Empty)->maxi:=kv;l|Node(l,kv,r)->Node(l,kv,bfindr)|Empty->assert(false)(* choose already raises Not_found on empty map *)in(!maxi,sref(bfind (sgettr)))letfilter_map(f:key-> 'a->'boption):'at->'bt=letrecvisittcont=matchtwith|Empty->contEmpty|Node(l,(k,v),r)->visitlbeginfunl->letw=fkvinvisitrbeginfunr->matchwwith|None->cont(bst_appendlr)|Somew->cont(Node(l,(k,w),r))endendinfunm->visit(sgetm)srefletfiltervft=filter_map(fun_v->iffvthenSomevelseNone)tletfilterft=filter_map(funkv->iffkvthenSomevelseNone)tletmapft=filter_map(fun_v->Some(fv))tletmapift=filter_map(funkv->Some(fkv))tletpartition(p:key-> 'a->bool):'at->'at*'at=letrecvisittcont=matchtwith|Empty->contEmptyEmpty|Node(l,((k,v)askv),r)->visitlbeginfunl1l2->letb=pkvinvisitrbeginfunr1r2->ifbthencont(Node(l1,kv,r1))(bst_appendl2r2)elsecont(bst_appendl1r1)(Node(l2,kv,r2))endendinfunm->visit(sgetm)(funt1t2->sreft1,sreft2)type'aenumeration=|End|Moreofkey*'a*(key *'a)bst*'aenumerationletcount_enum =letreccountk=function|End->k|More(_,_,tr,en)->count(1+k+size tr)eninfunen->count0enlet reccons_enumme=matchmwith|Empty->e|Node(l,(k,v),r)->cons_enuml(More(k,v,r,e))letrecrev_cons_enumme=matchmwith|Empty->e|Node(l,(k,v),r)->rev_cons_enumr(More(k,v,l,e))letreccons_enum_fromk2me=matchmwith|Empty->e|Node(l,(k,v),r)->ifOrd.comparek2k<=0then cons_enum_fromk2l(More(k,v,r,e))elsecons_enum_fromk2reletcomparecmptr1tr2=lettr1,tr2=sgettr1,sgettr2inletrecauxe1e2=match(e1,e2)with|(End,End)->0|(End,_)->-1|(_,End)->1|(More(v1,d1,r1,e1),More(v2,d2,r2,e2))->letc=Ord.comparev1v2inifc<>0thencelseletc=cmpd1d2inifc<>0thencelseaux(cons_enumr1e1)(cons_enumr2e2)inaux(cons_enumtr1End)(cons_enumtr2End)let equalcmptr1tr2=lettr1,tr2=sgettr1,sgettr2inletrecauxe1e2=match(e1,e2)with(End,End)->true|(End,_)->false|(_,End)->false|(More(v1,d1,r1,e1),More(v2,d2,r2,e2))->Ord.comparev1v2=0&&cmpd1d2&&aux(cons_enumr1e1)(cons_enumr2e2)inaux(cons_enumtr1End)(cons_enumtr2End)let recenum_bstcfnen=let cur=refeninletnext()=match!curwith|End->raiseEnum.No_more_elements|More(k,v,r,e)->cur:=cfnre;(k,v)inletcount()=count_enum!curinletclone()=enum_bstcfn!curinEnum.make~next~count~cloneletenumtr=enum_bstcons_enum(cons_enum(sgettr)End)letbackwardstr=enum_bstrev_cons_enum(rev_cons_enum(sgettr)End)let keysm=Enum.mapfst(enumm)letvaluesm=Enum.mapsnd(enumm)letof_enume=Enum.fold beginfunacc(k,v)->addkvaccendempty eletto_listm=List.of_enum (enumm)letof_listl=of_enum(List.enuml)letadd_to_listxdatam=letadd=functionNone->Some[data]|Somel->Some(data::l)inupdate_stdlibxaddmletcustom_print~first~last~sepkvproutm=Enum.print~first~last~sep(funout(k,v)->kvproutkv)out(enumm)letprint?(first="{\n")?(last="}\n")?(sep=",\n")?(kvsep=": ")kprvproutm=custom_print ~first~last~sep(funoutkv->BatPrintf.fprintfout"%a%s%a"kprkkvsepvprv)outmletprint_as_listkprvproutm=print ~first:"["~last:"]"~sep:"; "~kvsep:", "kprvproutmmoduleLabels=structletadd~key~datat=addkeydatatletiter~ft=iter (funkeydata->f~key~data)tletmap~ft=mapftletmapi~ft=mapi (funkeydata->f~key~data)tletfold~ft~init=fold (funkeydataacc->f~key~dataacc)tinitletcompare~cmpab=comparecmpabletequal~cmpab=equalcmpabletfilterv~f=filtervfletfilter~f=filter fendmoduleExceptionless=structletfindkm=find_optkmletchoosem=trySome(choosem)withNot_found->Noneletanym=trySome(anym)withNot_found->NoneendmoduleInfix=structlet(-->)mk=findkmlet(<--)m(k,v)=addkvmendletbindingsm=List.of_enum (enumm)letexist_boolbfm=tryiter(funkv->iffkv=bthenraiseExit)m;falsewithExit->trueletexistsfm=exist_bool truefmletfor_allfm=not(exist_boolfalsefm)letcardinalm=fold(fun_k_v->succ)m0let splitkm=lettr=sgetminletC(cx,center)=cfind~sel:(kselk)trinmatchcenterwith|Empty->letl,r=csplay'cxEmptyEmptyin(srefl,None,srefr)|Node(l,x,r)->letl',r'=csplay'cxlrin(* we rebalance as in 'find' *)rebalancem(Node(l',x,r'));(srefl',Some(sndx),srefr')letmergefm1m2=(* The implementation is a bit long, but has the important
property of applying `f` in increasing key order. *)(* we will iterate on both enumerations in increasing order simultaneously *)lete1=enumm1inlete2=enumm2in(* we will push the results in increasing order from left to
right; the result will be very unbalanced, but this will be
corrected by the rebalancing at the first lookup in the splay
tree. *)letmaybe_pushacckmaybe_v1maybe_v2=match fkmaybe_v1maybe_v2with|None->acc|Some v->Node(acc,(k,v),Empty)inletpush1acc(k,v1)=maybe_pushacck(Somev1)Noneinletpush2acc(k,v2)=maybe_pushacckNone(Somev2)in(* we iterate simultaneously on both inputs, in increasing
order of keys. There are four different "states" to consider :
- we have no idea of the inputs :
none_known
- we know the next (key, value) pair of e1, and that e2 is empty :
only_e1 (k1, v1)
- we know the next (key, value) pair of e2, and that e1 is empty :
only_e2 (k2, v2)
- we know the next (key, value) pair of both e1 and e2 :
both_known (k1, v1) (k2, v2)
*)letrecnone_knownacc=match Enum.peeke1,Enum.peeke2with|None,None->acc|None,Somekv2->Enum.junke2;only_e2 acckv2|Some kv1,None->Enum.junke1;only_e1 acckv1|Some kv1,Somekv2->Enum.junke1;Enum.junke2;both_known acckv1kv2andonly_e1 acckv1=Enum.foldpush1(push1acckv1)e1and only_e2acckv2=Enum.foldpush2(push2acckv2)e2and both_knownacc((k1,v1)askv1)((k2,v2)askv2)=let cmp=Ord.comparek1k2inif cmp<0thenbeginletacc=push1acc kv1inmatchEnum.peeke1with|None->only_e2acckv2|Somekv1'->Enum.junke1;both_knownacckv1'kv2endelseifcmp>0thenbeginletacc=push2acc kv2inmatchEnum.peeke2with|None->only_e1acckv1|Somekv2'->Enum.junke2;both_knownacckv1kv2'endelsebeginletacc=maybe_pushacck1(Some v1)(Somev2)innone_knownaccendinsref(none_knownEmpty)letpopm=matchsgetmwith|Empty->raiseNot_found|Node(l,kv,r)->kv,sref(bst_appendlr)letadd_seqsm=BatSeq.fold_left(funm(k,v)->addkvm)msletof_seqs=add_seq semptyletrecseq_of_iterm()=matchmwith|End->BatSeq.Nil|More(k,v,r,e)->BatSeq.Cons((k,v),seq_of_iter(cons_enumre))letto_seqm=seq_of_iter (cons_enum(sgetm)End)letto_rev_seqm=seq_of_iter (rev_cons_enum(sgetm)End)letto_seq_fromkm=seq_of_iter(cons_enum_fromk(sgetm)End)letunionfm1m2=fold(funkvm->matchfind_optkmwith|Somev1->(matchfkvv1with|Somevmerged->addkvmerged m|None->removekm)|None->addkvm)m1m2letextractktr=lettr=sgettrin(* the reference here is a tad ugly but allows to reuse `cfind`
without fuss *)letmaybe_v=refNoneinletreplace=function|Empty->Empty|Node(l,(_,v),r)->maybe_v:=Somev;bst_appendlrinlettr=top(cchangereplace(cfind~sel:(kselk)tr))in(* like in the `remove` case, we don't bother rebalancing *)match!maybe_vwith|None->raiseNot_found|Somev->v,sreftr(*$>*)end