Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file ufind.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160(* -------------------------------------------------------------------- *)(* Copyright (C), The EasyCrypt Team *)(*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*)(* -------------------------------------------------------------------- *)moduletypeItem=sigtypetvalequal:t->t->boolvalcompare:t->t->intend(* -------------------------------------------------------------------- *)moduletypeData=sigtypedatatypeeffectsvalnoeffects:effectsvalfresh:unit->datavalunion:data->data->data*effectsend(* -------------------------------------------------------------------- *)moduletypeS=sigtypeitemtypedatatypeeffectstypetvalinitial:tvalfind:item->t->itemvalsame:item->item->t->boolvaldata:item->t->dataoptionvalset:item->data->t->t*effectsvalunion:?prio:[`Left|`Right]->item->item->t->t*effectsend(* -------------------------------------------------------------------- *)moduleMake(I:Item)(D:Data)=structtypeitem=I.ttypedata=D.datatypeeffects=D.effectstypelink=|Rootofint*data|LinkofitemmoduleM=Map.Make(I)typet={mutableforest:linkM.t;}(* ------------------------------------------------------------------ *)letinitial={forest=M.empty;}(* ------------------------------------------------------------------ *)letxfind=letrecfollow(pitem:item)(item:item)(uf:t)=matchM.finditemuf.forestwith|Root(w,data)->(item,w,Somedata)|Linknitem->let(nitem,_,_)asaout=followitemnitemufinuf.forest<-M.addpitem(Linknitem)uf.forest;aout|exceptionNot_found->assertfalseinfun(item:item)(uf:t)->matchM.find_optitemuf.forestwith|None->(item,0,None)|Some(Root(w,data))->(item,w,Somedata)|Some(Linknext)->followitemnextuf(* ------------------------------------------------------------------ *)letfind(item:item)(uf:t)=let(item,_,_)=xfinditemufinitem(* ------------------------------------------------------------------ *)letsame(item1:item)(item2:item)(uf:t)=I.equal(finditem1uf)(finditem2uf)(* ------------------------------------------------------------------ *)letdata(item:item)(uf:t)=let(_,_,data)=xfinditemufindata(* ------------------------------------------------------------------ *)letset(item:item)(data:data)(uf:t)=let(item,w,olddata)=xfinditemufinletdata,effects=matcholddatawith|None->data,D.noeffects|Someolddata->D.uniondataolddatainletuf={forest=M.additem(Root(w,data))uf.forest;}in(uf,effects)(* ------------------------------------------------------------------ *)letunion?prio(item1:item)(item2:item)(uf:t)=let(item1,w1,data1)=xfinditem1ufand(item2,w2,data2)=xfinditem2ufinifI.equalitem1item2then(uf,D.noeffects)elseletdata1=matchdata1withNone->D.fresh()|Somex->xinletdata2=matchdata2withNone->D.fresh()|Somex->xinlet(data,effects)=D.uniondata1data2inletroot=Root(w1+w2,data)inletprio=matchpriowith|None->ifw1>=w2then`Leftelse`Right|Someprio->prioinlet(link1,link2)=matchpriowith|`Left->(root,Linkitem1)|`Right->(Linkitem2,root)inletuf={forest=M.additem1link1(M.additem2link2uf.forest);}in(uf,effects)end(* -------------------------------------------------------------------- *)moduletypeUS=sigtypeitemtypetvalinitial:tvalfind:item->t->itemvalunion:?prio:[`Left|`Right]->item->item->t->tvalsame:item->item->t->boolend