| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
module type PartitionSig = sig |
| 25 |
|
| 26 |
|
| 27 |
type elt |
| 28 |
|
| 29 |
|
| 30 |
type set |
| 31 |
|
| 32 |
|
| 33 |
type t |
| 34 |
|
| 35 |
|
| 36 |
val create : unit -> t |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
val add : elt -> t -> unit |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
val find : elt -> t -> elt |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
val union : elt -> elt -> t -> unit |
| 49 |
|
| 50 |
|
| 51 |
val union_set : set -> t -> unit |
| 52 |
|
| 53 |
|
| 54 |
val partition : t -> set list |
| 55 |
|
| 56 |
end |
| 57 |
|
| 58 |
module type SetS = |
| 59 |
sig |
| 60 |
type t |
| 61 |
type elt |
| 62 |
val singleton : elt -> t |
| 63 |
val union : t -> t -> t |
| 64 |
val choose : t -> elt |
| 65 |
val iter : (elt -> unit) -> t -> unit |
| 66 |
end |
| 67 |
|
| 68 |
module type MapS = |
| 69 |
sig |
| 70 |
type key |
| 71 |
type +'a t |
| 72 |
val empty : 'a t |
| 73 |
val find : key -> 'a t -> 'a |
| 74 |
val add : key -> 'a -> 'a t -> 'a t |
| 75 |
val mem : key -> 'a t -> bool |
| 76 |
val fold : (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b |
| 77 |
end |
| 78 |
|
| 79 |
module Make (S:SetS)(M:MapS with type key = S.elt) = struct |
| 80 |
|
| 81 |
type elt = S.elt |
| 82 |
type set = S.t |
| 83 |
|
| 84 |
type node = |
| 85 |
| Canon of set |
| 86 |
| Equiv of elt |
| 87 |
|
| 88 |
type t = node ref M.t ref |
| 89 |
|
| 90 |
let create () = ref (M.empty : node ref M.t) |
| 91 |
|
| 92 |
let fresh x p = |
| 93 |
let node = ref (Canon (S.singleton x)) in |
| 94 |
p := M.add x node !p; |
| 95 |
x, node |
| 96 |
|
| 97 |
let rec lookup x p = |
| 98 |
let node = M.find x !p in |
| 99 |
match !node with |
| 100 |
| Canon _ -> x, node |
| 101 |
| Equiv y -> |
| 102 |
let ((z,_) as res) = lookup y p in |
| 103 |
if not (z == y) then node := Equiv z; |
| 104 |
res |
| 105 |
|
| 106 |
let add x p = if not (M.mem x !p) then ignore (fresh x p) |
| 107 |
|
| 108 |
let find x p = fst (lookup x p) |
| 109 |
|
| 110 |
let canonical x p = try lookup x p with Not_found -> fresh x p |
| 111 |
|
| 112 |
let union x y p = |
| 113 |
let ((x,_) as xcan) = canonical x p in |
| 114 |
let ((y,_) as ycan) = canonical y p in |
| 115 |
if x = y then () |
| 116 |
else |
| 117 |
let xcan, ycan = if x < y then xcan, ycan else ycan, xcan in |
| 118 |
let x,xnode = xcan and y,ynode = ycan in |
| 119 |
match !xnode, !ynode with |
| 120 |
| Canon lx, Canon ly -> |
| 121 |
xnode := Canon (S.union lx ly); |
| 122 |
ynode := Equiv x; |
| 123 |
| _ -> assert false |
| 124 |
|
| 125 |
let union_set s p = |
| 126 |
try |
| 127 |
let x = S.choose s in |
| 128 |
S.iter (fun y -> union x y p) s |
| 129 |
with Not_found -> () |
| 130 |
|
| 131 |
let partition p = |
| 132 |
List.rev (M.fold |
| 133 |
(fun x node acc -> match !node with |
| 134 |
| Equiv _ -> acc |
| 135 |
| Canon lx -> lx::acc) |
| 136 |
!p []) |
| 137 |
|
| 138 |
end |