123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302(*pad: taken from set.ml from stdlib ocaml, functor sux: module Make(Ord: OrderedType) = *)(* with some addons such as from list *)(***********************************************************************)(* *)(* 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. *)(* *)(***********************************************************************)(* set.ml 1.18.4.1 2004/11/03 21:19:49 doligez Exp *)(* Sets over ordered types *)(* pad:
type elt = Ord.t
type t = Empty | Node of t * elt * t * int
and subst all Ord.compare with just compare
*)type'eltt=Empty|Nodeof'eltt*'elt*'eltt*int(* Sets are represented by balanced binary trees (the heights of the
children differ by at most 2 *)letheight=functionEmpty->0|Node(_,_,_,h)->h(* Creates a new node with left son l, value v and right son r.
We must have all elements of l < v < all elements of r.
l and r must be balanced and | height l - height r | <= 2.
Inline expansion of height for better speed. *)letcreatelvr=lethl=matchlwithEmpty->0|Node(_,_,_,h)->hinlethr=matchrwithEmpty->0|Node(_,_,_,h)->hinNode(l,v,r,(ifhl>=hrthenhl+1elsehr+1))(* Same as create, but performs one step of rebalancing if necessary.
Assumes l and r balanced and | height l - height r | <= 3.
Inline expansion of create for better speed in the most frequent case
where no rebalancing is required. *)letballvr=lethl=matchlwithEmpty->0|Node(_,_,_,h)->hinlethr=matchrwithEmpty->0|Node(_,_,_,h)->hinifhl>hr+2thenbeginmatchlwithEmpty->invalid_arg"Set.bal"|Node(ll,lv,lr,_)->ifheightll>=heightlrthencreatelllv(createlrvr)elsebeginmatchlrwithEmpty->invalid_arg"Set.bal"|Node(lrl,lrv,lrr,_)->create(createlllvlrl)lrv(createlrrvr)endendelseifhr>hl+2thenbeginmatchrwithEmpty->invalid_arg"Set.bal"|Node(rl,rv,rr,_)->ifheightrr>=heightrlthencreate(createlvrl)rvrrelsebeginmatchrlwithEmpty->invalid_arg"Set.bal"|Node(rll,rlv,rlr,_)->create(createlvrll)rlv(createrlrrvrr)endendelseNode(l,v,r,(ifhl>=hrthenhl+1elsehr+1))(* Insertion of one element *)letrecaddx=functionEmpty->Node(Empty,x,Empty,1)|Node(l,v,r,_)ast->letc=comparexvinifc=0thentelseifc<0thenbal(addxl)vrelseballv(addxr)(* Same as create and bal, but no assumptions are made on the
relative heights of l and r. *)letrecjoinlvr=match(l,r)with(Empty,_)->addvr|(_,Empty)->addvl|(Node(ll,lv,lr,lh),Node(rl,rv,rr,rh))->iflh>rh+2thenballllv(joinlrvr)elseifrh>lh+2thenbal(joinlvrl)rvrrelsecreatelvr(* Smallest and greatest element of a set *)letrecmin_elt=functionEmpty->raiseNot_found|Node(Empty,v,r,_)->v|Node(l,v,r,_)->min_eltlletrecmax_elt=functionEmpty->raiseNot_found|Node(l,v,Empty,_)->v|Node(l,v,r,_)->max_eltr(* Remove the smallest element of the given set *)letrecremove_min_elt=functionEmpty->invalid_arg"Set.remove_min_elt"|Node(Empty,v,r,_)->r|Node(l,v,r,_)->bal(remove_min_eltl)vr(* Merge two trees l and r into one.
All elements of l must precede the elements of r.
Assume | height l - height r | <= 2. *)letmerget1t2=match(t1,t2)with(Empty,t)->t|(t,Empty)->t|(_,_)->balt1(min_eltt2)(remove_min_eltt2)(* Merge two trees l and r into one.
All elements of l must precede the elements of r.
No assumption on the heights of l and r. *)letconcatt1t2=match(t1,t2)with(Empty,t)->t|(t,Empty)->t|(_,_)->joint1(min_eltt2)(remove_min_eltt2)(* Splitting. split x s returns a triple (l, present, r) where
- l is the set of elements of s that are < x
- r is the set of elements of s that are > x
- present is false if s contains no element equal to x,
or true if s contains an element equal to x. *)letrecsplitx=functionEmpty->(Empty,false,Empty)|Node(l,v,r,_)->letc=comparexvinifc=0then(l,true,r)elseifc<0thenlet(ll,pres,rl)=splitxlin(ll,pres,joinrlvr)elselet(lr,pres,rr)=splitxrin(joinlvlr,pres,rr)(* Implementation of the set operations *)letempty=Emptyletis_empty=functionEmpty->true|_->falseletrecmemx=functionEmpty->false|Node(l,v,r,_)->letc=comparexvinc=0||memx(ifc<0thenlelser)letsingletonx=Node(Empty,x,Empty,1)letrecremovex=functionEmpty->Empty|Node(l,v,r,_)->letc=comparexvinifc=0thenmergelrelseifc<0thenbal(removexl)vrelseballv(removexr)letrecunions1s2=match(s1,s2)with(Empty,t2)->t2|(t1,Empty)->t1|(Node(l1,v1,r1,h1),Node(l2,v2,r2,h2))->ifh1>=h2thenifh2=1thenaddv2s1elsebeginlet(l2,_,r2)=splitv1s2injoin(unionl1l2)v1(unionr1r2)endelseifh1=1thenaddv1s2elsebeginlet(l1,_,r1)=splitv2s1injoin(unionl1l2)v2(unionr1r2)endletrecinters1s2=match(s1,s2)with(Empty,t2)->Empty|(t1,Empty)->Empty|(Node(l1,v1,r1,_),t2)->matchsplitv1t2with(l2,false,r2)->concat(interl1l2)(interr1r2)|(l2,true,r2)->join(interl1l2)v1(interr1r2)letrecdiffs1s2=match(s1,s2)with(Empty,t2)->Empty|(t1,Empty)->t1|(Node(l1,v1,r1,_),t2)->matchsplitv1t2with(l2,false,r2)->join(diffl1l2)v1(diffr1r2)|(l2,true,r2)->concat(diffl1l2)(diffr1r2)letreccompare_auxl1l2=match(l1,l2)with([],[])->0|([],_)->-1|(_,[])->1|(Empty::t1,Empty::t2)->compare_auxt1t2|(Node(Empty,v1,r1,_)::t1,Node(Empty,v2,r2,_)::t2)->letc=comparev1v2inifc<>0thencelsecompare_aux(r1::t1)(r2::t2)|(Node(l1,v1,r1,_)::t1,t2)->compare_aux(l1::Node(Empty,v1,r1,0)::t1)t2|(t1,Node(l2,v2,r2,_)::t2)->compare_auxt1(l2::Node(Empty,v2,r2,0)::t2)letcompares1s2=compare_aux[s1][s2]letequals1s2=compares1s2=0letrecsubsets1s2=match(s1,s2)withEmpty,_->true|_,Empty->false|Node(l1,v1,r1,_),(Node(l2,v2,r2,_)ast2)->letc=Pervasives.comparev1v2inifc=0thensubsetl1l2&&subsetr1r2elseifc<0thensubset(Node(l1,v1,Empty,0))l2&&subsetr1t2elsesubset(Node(Empty,v1,r1,0))r2&&subsetl1t2letreciterf=functionEmpty->()|Node(l,v,r,_)->iterfl;fv;iterfrletrecfoldfsaccu=matchswithEmpty->accu|Node(l,v,r,_)->foldfl(fv(foldfraccu))letrecfor_allp=functionEmpty->true|Node(l,v,r,_)->pv&&for_allpl&&for_allprletrecexistsp=functionEmpty->false|Node(l,v,r,_)->pv||existspl||existsprletfilterps=letrecfiltaccu=function|Empty->accu|Node(l,v,r,_)->filt(filt(ifpvthenaddvaccuelseaccu)l)rinfiltEmptysletpartitionps=letrecpart(t,fasaccu)=function|Empty->accu|Node(l,v,r,_)->part(part(ifpvthen(addvt,f)else(t,addvf))l)rinpart(Empty,Empty)sletreccardinal=functionEmpty->0|Node(l,v,r,_)->cardinall+1+cardinalrletrecelements_auxaccu=functionEmpty->accu|Node(l,v,r,_)->elements_aux(v::elements_auxaccur)lletelementss=elements_aux[]sletchoose=min_elt(* pad: *)let(of_list:'alist->'at)=funxs->List.fold_left(funae->addea)emptyxs