Debian Codegraph @

clib/unionfind.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 (** An imperative implementation of partitions via Union-Find *)
12
13 (** Paths are compressed imperatively at each lookup of a
14 canonical representative. Each union also modifies in-place
15 the partition structure.
16
17 Nota: For the moment we use Pervasive's comparison for
18 choosing the smallest object as representative. This could
19 be made more generic.
20 *)
21
22
23
24 module type PartitionSig = sig
25
26 (** The type of elements in the partition *)
27 type elt
28
29 (** A set structure over elements *)
30 type set
31
32 (** The type of partitions *)
33 type t
34
35 (** Initialise an empty partition *)
36 val create : unit -> t
37
38 (** Add (in place) an element in the partition, or do nothing
39 if the element is already in the partition. *)
40 val add : elt -> t -> unit
41
42 (** Find the canonical representative of an element.
43 Raise [not_found] if the element isn't known yet. *)
44 val find : elt -> t -> elt
45
46 (** Merge (in place) the equivalence classes of two elements.
47 This will add the elements in the partition if necessary. *)
48 val union : elt -> elt -> t -> unit
49
50 (** Merge (in place) the equivalence classes of many elements. *)
51 val union_set : set -> t -> unit
52
53 (** Listing the different components of the partition *)
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