Skip to content

Commit 2b205d8

Browse files
authored
flambda-backend: Clean up algorithms (#611)
1 parent 524f0b4 commit 2b205d8

File tree

2 files changed

+98
-5
lines changed

2 files changed

+98
-5
lines changed

utils/strongly_connected_components.ml

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,52 @@ end = struct
107107
}
108108
end
109109

110+
module type Id = sig
111+
type t
112+
113+
module Set : sig
114+
type elt = t
115+
116+
type t
117+
118+
val empty : t
119+
120+
val add : elt -> t -> t
121+
122+
val elements : t -> elt list
123+
124+
val iter : (elt -> unit) -> t -> unit
125+
126+
val fold : (elt -> 'a -> 'a) -> t -> 'a -> 'a
127+
end
128+
129+
module Map : sig
130+
type key = t
131+
132+
type 'a t
133+
134+
val empty : _ t
135+
136+
val add : key -> 'a -> 'a t -> 'a t
137+
138+
val cardinal : _ t -> int
139+
140+
val bindings : 'a t -> (key * 'a) list
141+
142+
val find : key -> 'a t -> 'a
143+
144+
val iter : (key -> 'a -> unit) -> 'a t -> unit
145+
146+
val fold : (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b
147+
148+
val mem : key -> 'a t -> bool
149+
end
150+
151+
val print : Format.formatter -> t -> unit
152+
end
153+
110154
module type S = sig
111-
module Id : Identifiable.S
155+
module Id : Id
112156

113157
type directed_graph = Id.Set.t Id.Map.t
114158

@@ -123,7 +167,7 @@ module type S = sig
123167
val component_graph : directed_graph -> (component * int list) array
124168
end
125169

126-
module Make (Id : Identifiable.S) = struct
170+
module Make (Id : Id) = struct
127171
type directed_graph = Id.Set.t Id.Map.t
128172

129173
type component =
@@ -176,6 +220,11 @@ module Make (Id : Identifiable.S) = struct
176220
in
177221
{ back; forth }, integer_graph
178222

223+
let rec int_list_mem x xs =
224+
match xs with
225+
| [] -> false
226+
| x' :: xs -> if Int.equal x x' then true else int_list_mem x xs
227+
179228
let component_graph graph =
180229
let numbering, integer_graph = number graph in
181230
let { Kosaraju. sorted_connected_components;
@@ -186,7 +235,7 @@ module Make (Id : Identifiable.S) = struct
186235
match nodes with
187236
| [] -> assert false
188237
| [node] ->
189-
(if List.mem node integer_graph.(node)
238+
(if int_list_mem node integer_graph.(node)
190239
then Has_loop [numbering.forth.(node)]
191240
else No_loop numbering.forth.(node)),
192241
component_edges.(component)

utils/strongly_connected_components.mli

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,52 @@
2121
2222
*)
2323

24+
module type Id = sig
25+
type t
26+
27+
module Set : sig
28+
type elt = t
29+
30+
type t
31+
32+
val empty : t
33+
34+
val add : elt -> t -> t
35+
36+
val elements : t -> elt list
37+
38+
val iter : (elt -> unit) -> t -> unit
39+
40+
val fold : (elt -> 'a -> 'a) -> t -> 'a -> 'a
41+
end
42+
43+
module Map : sig
44+
type key = t
45+
46+
type 'a t
47+
48+
val empty : _ t
49+
50+
val add : key -> 'a -> 'a t -> 'a t
51+
52+
val cardinal : _ t -> int
53+
54+
val bindings : 'a t -> (key * 'a) list
55+
56+
val find : key -> 'a t -> 'a
57+
58+
val iter : (key -> 'a -> unit) -> 'a t -> unit
59+
60+
val fold : (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b
61+
62+
val mem : key -> 'a t -> bool
63+
end
64+
65+
val print : Format.formatter -> t -> unit
66+
end
67+
2468
module type S = sig
25-
module Id : Identifiable.S
69+
module Id : Id
2670

2771
type directed_graph = Id.Set.t Id.Map.t
2872
(** If (a -> set) belongs to the map, it means that there are edges
@@ -40,4 +84,4 @@ module type S = sig
4084
val component_graph : directed_graph -> (component * int list) array
4185
end
4286

43-
module Make (Id : Identifiable.S) : S with module Id := Id
87+
module Make (Id : Id) : S with module Id := Id

0 commit comments

Comments
 (0)