1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798(* This file is part of Lwt, released under the MIT license. See LICENSE.md for
details, or visit https://github.com/ocsigen/lwt/blob/master/LICENSE.md. *)moduletypeOrderedType=sigtypetvalcompare:t->t->intendmoduletypeS=sigtypeelttypetvalempty:tvalis_empty:t->boolvaladd:elt->t->tvalunion:t->t->tvalfind_min:t->eltvallookup_min:t->eltoptionvalremove_min:t->tvalsize:t->intendmoduleMake(Ord:OrderedType):(Swithtypeelt=Ord.t)=structtypeelt=Ord.ttypet=treelistandtree=Nodeofelt*int*treelistletroot(Node(x,_,_))=xletrank(Node(_,r,_))=rletlink(Node(x1,r1,c1)ast1)(Node(x2,r2,c2)ast2)=letc=Ord.comparex1x2inifc<=0thenNode(x1,r1+1,t2::c1)elseNode(x2,r2+1,t1::c2)letrecinst=function[]->[t]|(t'::_)astswhenrankt<rankt'->t::ts|t'::ts->ins(linktt')tsletempty=[]letis_emptyts=ts=[]letaddxts=ins(Node(x,0,[]))tsletrecuniontsts'=matchts,ts'with([],_)->ts'|(_,[])->ts|(t1::ts1,t2::ts2)->ifrankt1<rankt2thent1::unionts1(t2::ts2)elseifrankt2<rankt1thent2::union(t1::ts1)ts2elseins(linkt1t2)(unionts1ts2)letrecfind_min=function[]->raiseNot_found|[t]->roott|t::ts->letx=find_mintsinletc=Ord.compare(roott)xinifc<0thenroottelsexletreclookup_min=function|[]->None|[t]->Some(roott)|t::ts->matchlookup_mintswith|None->None|Somexasresult->letc=Ord.compare(roott)xinifc<0thenSome(roott)elseresultletrecget_min=function[]->assertfalse|[t]->(t,[])|t::ts->let(t',ts')=get_mintsinletc=Ord.compare(roott)(roott')inifc<0then(t,ts)else(t',t::ts')letremove_min=function[]->raiseNot_found|ts->let(Node(_,_,c),ts)=get_mintsinunion(List.revc)tsletrecsizel=letsizetree(Node(_,_,tl))=1+sizetlinList.fold_left(funst->s+sizetreet)0lend