Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file ufind.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222(* -------------------------------------------------------------------- *)(* 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=sigtypedatatypeeffectsvaldefault:datavalisvoid:data->boolvalnoeffects:effectsvalunion:data->data->data*effectsend(* -------------------------------------------------------------------- *)moduletypeS=sigtypeitemtypedatatypeeffectstypetvalinitial:tvalfind:item->t->itemvalsame:item->item->t->boolvaldata:item->t->datavalset:item->data->t->tvalisset:item->t->boolvalunion:item->item->t->t*effectsvaldomain:t->itemlistvalclosed:t->boolvalopened:t->intend(* -------------------------------------------------------------------- *)moduleMake(I:Item)(D:Data)=structtypeitem=I.ttypedata=D.datatypeeffects=D.effectstypelink=|Rootofint*data|LinkofitemmoduleM=Map.Make(I)typet={mutableforest:linkM.t;(*---*)nvoids:int;}(* ------------------------------------------------------------------ *)letint_of_bool=functiontrue->1|false->0(* ------------------------------------------------------------------ *)letinitial={forest=M.empty;nvoids=0;}(* ------------------------------------------------------------------ *)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)=xfinditemufinmatchdatawithNone->D.default|Somedata->data(* ------------------------------------------------------------------ *)letset(item:item)(data:data)(uf:t)=let(item,w,olddata)=xfinditemufinletolddata=matcholddatawithNone->D.default|Someod->odin{forest=M.additem(Root(w,data))uf.forest;nvoids=uf.nvoids-(int_of_bool(D.isvoidolddata))+(int_of_bool(D.isvoiddata));}(* ------------------------------------------------------------------ *)letisset(item:item)(uf:t)=M.memitemuf.forest(* ------------------------------------------------------------------ *)letunion(item1:item)(item2:item)(uf:t)=let(item1,w1,data1)=xfinditem1ufand(item2,w2,data2)=xfinditem2ufinletdata1=matchdata1withNone->D.default|Somedata1->data1inletdata2=matchdata2withNone->D.default|Somedata2->data2inifI.equalitem1item2then(uf,D.noeffects)elselet(data,effects)=D.uniondata1data2inletroot=Root(w1+w2,data)inlet(link1,link2)=ifw1>=w2then(root,Linkitem1)else(Linkitem2,root)inletuf={forest=M.additem1link1(M.additem2link2uf.forest);nvoids=uf.nvoids-(int_of_bool(D.isvoiddata1)+int_of_bool(D.isvoiddata2))+(int_of_bool(D.isvoiddata));}in(uf,effects)(* ------------------------------------------------------------------ *)letdomain(uf:t)=List.mapfst(M.bindingsuf.forest)(* ------------------------------------------------------------------ *)letclosed(uf:t)=uf.nvoids=0(* ------------------------------------------------------------------ *)letopened(uf:t)=uf.nvoidsend(* -------------------------------------------------------------------- *)moduletypeUS=sigtypeitemtypetvalinitial:tvalfind:item->t->itemvalunion:item->item->t->tvalsame:item->item->t->boolend(* -------------------------------------------------------------------- *)moduleUMake(I:Item)=structmoduleD:Datawithtypedata=unitandtypeeffects=unit=structtypedata=unittypeeffects=unitletdefault:data=()letisvoid(_:data)=falseletnoeffects:effects=()letunion()()=((),())endmoduleUF=Make(I)(D)typeitem=I.ttypet=UF.tletinitial=UF.initialletfind(x:item)(uf:t)=UF.findxufletunion(x1:item)(x2:item)(uf:t)=fst(UF.unionx1x2uf)letsame(x1:item)(x2:item)(uf:t)=UF.samex1x2ufend