Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file UnionFindOverStore.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241(******************************************************************************)(* *)(* UnionFind *)(* *)(* François Pottier, Inria Paris *)(* *)(* Copyright Inria. All rights reserved. This file is distributed under *)(* the terms of the GNU Library General Public License version 2, with a *)(* special exception on linking, as described in the file LICENSE. *)(* *)(******************************************************************************)(* This module offers a union-find data structure based on disjoint set
forests, with path compression and linking by rank. *)openStoremoduleMake(S:STORE)=struct(* -------------------------------------------------------------------------- *)(* The rank of a vertex is the maximum length, in edges, of an uncompressed path
that leads to this vertex. In other words, the rank of [x] is the height of
the tree rooted at [x] that would exist if we did not perform path
compression. *)typerank=int(* The content of a vertex is a pointer to a parent vertex (if the vertex
has a parent) or a pair of a rank and a user value (if the vertex has no
parent, and is thus the representative vertex for this equivalence
class). *)(* In this version the code, the type ['a content] must not mutable. Indeed,
every mutation must be performed via [S.set]. *)type'acontent=|Linkof'arref|Rootofrank*'a(* The type ['a rref] represents a vertex in the union-find data structure. *)and'arref='acontentS.rref(* -------------------------------------------------------------------------- *)(* The type of stores, and the function for creating a new store, are those
of the underlying implementation [S]. *)type'astore='acontentS.storeletnew_store:unit->'astore=S.new_storeletcopy:'astore->'astore=S.copy(* -------------------------------------------------------------------------- *)(* [make s v] creates a new root of rank zero. *)letmake(s:'astore)(v:'a):'arref=S.makes(Root(0,v))(* -------------------------------------------------------------------------- *)(* [find s x] finds the representative vertex of the equivalence class of [x].
It does by following the path from [x] to the root. Path compression is
performed (on the way back) by making every vertex along the path a
direct child of the representative vertex. No rank is altered. *)letrecfind(s:'astore)(x:'arref):'arref=matchS.getsxwith|Root(_,_)->x|Linky->letz=findsyinifS.eqsyzthenzelseletlink_to_z=S.getsyinS.setsxlink_to_z;zletis_representative(s:'astore)(x:'arref):bool=matchS.getsxwith|Root_->true|Link_->false(* -------------------------------------------------------------------------- *)(* [eq s x y] determines whether the vertices [x] and [y] belong in the same
equivalence class. It does so via two calls to [find] and a physical
equality test. As a fast path, we first test whether [x] and [y] are
physically equal. *)leteq(s:'astore)(x:'arref)(y:'arref):bool=S.eqsxy||S.eqs(findsx)(findsy)(* -------------------------------------------------------------------------- *)(* [get_ s x] returns the value stored at [x]'s representative vertex. *)letget_(s:'astore)(x:'arref):'a=letx=findsxinmatchS.getsxwith|Root(_,v)->v|Link_->assertfalse(* [get s x] returns the value stored at [x]'s representative vertex. *)(* By not calling [find] immediately, we optimize the common cases where the
path out of [x] has length 0 or 1, at the expense of the general case.
Thus, we call [find] only if path compression must be performed. *)letget(s:'astore)(x:'arref):'a=matchS.getsxwith|Root(_,v)->v|Linky->matchS.getsywith|Root(_,v)->v|Link_->get_sx(* -------------------------------------------------------------------------- *)(* [set_ s x] updates the value stored at [x]'s representative vertex. *)letset_(s:'astore)(x:'arref)(v:'a):unit=letx=findsxinmatchS.getsxwith|Root(r,_)->S.setsx(Root(r,v))|Link_->assertfalse(* [set s x] updates the value stored at [x]'s representative vertex. *)(* By not calling [find] immediately, we optimize the common cases where the
path out of [x] has length 0 or 1, at the expense of the general case.
Thus, we call [find] only if path compression must be performed. *)letset(s:'astore)(x:'arref)(v:'a):unit=matchS.getsxwith|Root(r,_)->S.setsx(Root(r,v))|Linky->matchS.getsywith|Root(r,_)->S.setsy(Root(r,v))|Link_->set_sxv(* -------------------------------------------------------------------------- *)(* [union s x y] merges the equivalence classes of [x] and [y] by installing a
link from one root vertex to the other. *)(* Linking is by rank: the smaller-ranked vertex is made to point to the
larger. If the two vertices have the same rank, then an arbitrary choice
is made, and the rank of the new root is incremented by one. *)letunion(s:'astore)(x:'arref)(y:'arref):'arref=letx=findsxandy=findsyinifS.eqsxythenxelsematchS.getsx,S.getsywith|Root(rx,vx),Root(ry,_)->ifrx<rythenbeginS.setsx(Linky);yendelseifrx>rythenbeginS.setsy(Linkx);xendelsebeginS.setsy(Linkx);S.setsx(Root(rx+1,vx));xend|Root_,Link_|Link_,Root_|Link_,Link_->assertfalse(* -------------------------------------------------------------------------- *)(* [merge] is analogous to [union], but invokes a user-specified function [f]
to compute the new value [v] associated with the equivalence class. *)(* The function [f] must not affect the union-find data structure by making
re-entrant calls to [set], [union], or [merge]. There are two reasons for
this. First, [f] may be invoked at a time when the invariant of the data
structure is temporarily violated: in the third branch below, the rank of
[x] has not yet been increased when [f] is invoked. Second, more seriously,
if [f] could call, say, [union], then that could change a [Root] into a
[Link], so the write that follows the call to [f] might change a [Link]
back into a [Root], something that does not make any sense. Also, if [f]
could call [set], then the write that follows the call to [f] might undo
the effect of this [set] operation; this also does not make sense. *)(* The tests [if v != vy then ...] and [if v != vx then ...] are intended to
save an allocation and a write when possible. *)(* We invoke [f] before performing any update, so that if [f] fails
(by raising an exception), the state is unaffected. *)letmerges(f:'a->'a->'a)(x:'arref)(y:'arref):'arref=letx=findsxandy=findsyinifS.eqsxythenxelsematchS.getsx,S.getsywith|Root(rx,vx),Root(ry,vy)->letv=fvxvyinifrx<rythenbeginS.setsx(Linky);ifv!=vythenS.setsy(Root(ry,v));yendelseifrx>rythenbeginS.setsy(Linkx);ifv!=vxthenS.setsx(Root(rx,v));xendelsebeginS.setsy(Linkx);S.setsx(Root(rx+1,v));xend|Root_,Link_|Link_,Root_|Link_,Link_->assertfalseend