From 1fd2f374ff33ce2804f2c62f1354bda5e91bdada Mon Sep 17 00:00:00 2001 From: Adrien Guatto Date: Thu, 30 Sep 2010 19:24:41 +0200 Subject: [PATCH] New misc functions, renamed make_list_compare to list_compare. New functions fold_right_i and option_compare. --- compiler/minils/transformations/callgraph.ml | 2 +- compiler/utilities/misc.ml | 14 +++++++++++++- compiler/utilities/misc.mli | 10 ++++++---- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/compiler/minils/transformations/callgraph.ml b/compiler/minils/transformations/callgraph.ml index dbadb6f..46fe579 100644 --- a/compiler/minils/transformations/callgraph.ml +++ b/compiler/minils/transformations/callgraph.ml @@ -52,7 +52,7 @@ struct (** two instances are equal if the desc of keys are equal *) let compare_instances = let compare se1 se2 = compare se1.se_desc se2.se_desc in - Misc.make_list_compare compare + Misc.list_compare compare module S = (** Instances set *) Set.Make( diff --git a/compiler/utilities/misc.ml b/compiler/utilities/misc.ml index e157257..206790b 100644 --- a/compiler/utilities/misc.ml +++ b/compiler/utilities/misc.ml @@ -65,7 +65,7 @@ let rec split_last = function let remove x l = List.filter (fun y -> x <> y) l -let make_list_compare c l1 l2 = +let list_compare c l1 l2 = let rec aux l1 l2 = match (l1, l2) with | (h1::t1, h2::t2) -> let result = c h1 h2 in @@ -75,6 +75,12 @@ let make_list_compare c l1 l2 = | ([], _ ) -> -1 in aux l1 l2 +let option_compare f ox1 ox2 = match ox1, ox2 with + | None, None -> 0 + | Some x1, Some x2 -> f x1 x2 + | None, _ -> -1 + | _, None -> 1 + let is_empty = function | [] -> true | _ -> false @@ -144,6 +150,12 @@ let mapi3 f l1 l2 l3 = in aux 0 l1 l2 l3 +let fold_righti f l acc = + let rec aux i l acc = match l with + | [] -> acc + | h :: l -> f i h (aux (i + 1) l acc) in + aux 0 l acc + (* Functions to decompose a list into a tuple *) let _arity_error i l = Format.eprintf "Internal compiler error: \ diff --git a/compiler/utilities/misc.mli b/compiler/utilities/misc.mli index 711959b..a332b87 100644 --- a/compiler/utilities/misc.mli +++ b/compiler/utilities/misc.mli @@ -49,9 +49,11 @@ val memd_assoc : 'b -> ('a * 'b) list -> bool (** Same as List.assoc but searching for a data and returning the key. *) val assocd : 'b -> ('a * 'b) list -> 'a -(** [make_compare c] generates the lexicographical compare function on lists - induced by [c] *) -val make_list_compare : ('a -> 'a -> int) -> 'a list -> 'a list -> int +(** [list_compare c l1 l2] compares the lists [l1] and [l2] according to + lexicographical order induced by [c]. *) +val list_compare : ('a -> 'a -> int) -> 'a list -> 'a list -> int + +val option_compare : ('a -> 'a -> int) -> 'a option -> 'a option -> int (** Mapfold *) val mapfold: ('a -> 'b -> 'c * 'a) -> 'a -> 'b list -> 'c list * 'a @@ -65,6 +67,7 @@ val mapi: (int -> 'a -> 'b) -> 'a list -> 'b list val mapi2: (int -> 'a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list val mapi3: (int -> 'a -> 'b -> 'c -> 'd) -> 'a list -> 'b list -> 'c list -> 'd list +val fold_righti : (int -> 'a -> 'b -> 'b) -> 'a list -> 'b -> 'b (** Functions to decompose a list into a tuple *) val assert_empty : 'a list -> unit @@ -73,4 +76,3 @@ val assert_1min : 'a list -> 'a * 'a list val assert_2 : 'a list -> 'a * 'a val assert_2min : 'a list -> 'a * 'a * 'a list val assert_3 : 'a list -> 'a * 'a * 'a -