Skip to content

Commit 5cf9cdb

Browse files
lthlschambartGbury
authored
flambda-backend: Better results from meet (#406)
Co-authored-by: Pierre Chambart <[email protected]> Co-authored-by: Guillaume Bury <[email protected]>
1 parent cf21f2b commit 5cf9cdb

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

utils/misc.ml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,15 @@ module Stdlib = struct
233233
else loop (succ i) in
234234
loop 0
235235

236+
let fold_left2 f x a1 a2 =
237+
if Array.length a1 <> Array.length a2
238+
then invalid_arg "Misc.Stdlib.Array.fold_left2";
239+
let r = ref x in
240+
for i = 0 to Array.length a1 - 1 do
241+
r := f !r (Array.unsafe_get a1 i) (Array.unsafe_get a2 i)
242+
done;
243+
!r
244+
236245
let for_alli p a =
237246
let n = Array.length a in
238247
let rec loop i =
@@ -260,6 +269,17 @@ module Stdlib = struct
260269
false
261270
in
262271
loop 0
272+
273+
let map_sharing f a =
274+
let same = ref true in
275+
let f' x =
276+
let x' = f x in
277+
if x != x' then
278+
same := false;
279+
x'
280+
in
281+
let a' = (Array.map [@inlined hint]) f' a in
282+
if !same then a else a'
263283
end
264284

265285
module String = struct

utils/misc.mli

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,14 @@ module Stdlib : sig
192192
val exists2 : ('a -> 'b -> bool) -> 'a array -> 'b array -> bool
193193
(** Same as [Array.exists2] from the standard library. *)
194194

195+
val fold_left2 :
196+
('acc -> 'a -> 'b -> 'acc) -> 'acc -> 'a array -> 'b array -> 'acc
197+
(** [fold_left2 f init [|a1; ...; an|] [|b1; ...; bn|]] is
198+
[f (... (f (f init a1 b1) a2 b2) ...) an bn].
199+
@raise Invalid_argument if the two arrays are determined
200+
to have different lengths.
201+
*)
202+
195203
val for_alli : (int -> 'a -> bool) -> 'a array -> bool
196204
(** Same as [Array.for_all] from the standard library, but the
197205
function is applied with the index of the element as first argument,
@@ -202,6 +210,10 @@ module Stdlib : sig
202210
val equal : ('a -> 'a -> bool) -> 'a array -> 'a array -> bool
203211
(** Compare two arrays for equality, using the supplied predicate for
204212
element equality *)
213+
214+
val map_sharing : ('a -> 'a) -> 'a array -> 'a array
215+
(** [map_sharing f a] is [map f a]. If for all elements of the array
216+
[f e == e] then [map_sharing f a == a] *)
205217
end
206218

207219
(** {2 Extensions to the String module} *)

0 commit comments

Comments
 (0)