]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/software/components/extlib/hExtlib.ml
Naif version of the union find
[helm.git] / helm / software / components / extlib / hExtlib.ml
index b2092c5b35be4b91db19529f1449f818f7555868..13dd2f2665da9fa8f8aed93c542d18b20ebf2c15 100644 (file)
@@ -148,6 +148,14 @@ let list_mapi f l =
      aux 0 l
 ;;
 
+let list_mapi_acc f a l =
+  let rec aux k a res = function
+    | [] -> a, List.rev res
+    | h::tl -> let a,h = f h k a in aux (k+1) a (h::res) tl
+  in
+   aux 0 a [] l
+;;
+
 let list_index p =
  let rec aux n =
   function
@@ -203,6 +211,19 @@ let sharing_map f l =
   if !unchanged then l else l1
 ;;
         
+let sharing_map_acc f acc l =
+  let unchanged = ref true in
+  let final_acc = ref acc in
+  let rec aux b acc = function
+    | [] as t -> unchanged := b; final_acc := acc; t
+    | he::tl ->
+        let acc, he1 = f acc he in
+        he1 :: aux (b && he1 == he) acc tl
+  in
+  let l1 = aux true acc l in
+  !final_acc, if !unchanged then l else l1
+;;
+
 let rec list_uniq ?(eq=(=)) = function 
   | [] -> []
   | h::[] -> [h]
@@ -217,6 +238,30 @@ let rec filter_map f =
       | None -> filter_map f tl
       | Some v -> v :: filter_map f tl)
 
+let filter_map_acc f acc l =
+  let acc, res = 
+   List.fold_left
+    (fun (acc, res) t ->
+       match f acc t with
+       | None -> acc, res
+       | Some (acc, x) -> acc, x::res)
+    (acc,[]) l
+  in
+   acc, List.rev res
+;;
+
+let filter_map_monad f acc l =
+  let acc, res = 
+   List.fold_left
+    (fun (acc, res) t ->
+       match f acc t with
+       | acc, None -> acc, res
+       | acc, Some x -> acc, x::res)
+    (acc,[]) l
+  in
+   acc, List.rev res
+;;
+
 let list_rev_map_filter f l =
    let rec aux a = function
       | []       -> a
@@ -286,6 +331,49 @@ let rec list_assoc_all a = function
    | _ :: tl                 -> list_assoc_all a tl
 ;;
 
+let rec rm_assoc n = function
+  | [] -> assert false
+  | (x,i)::tl when n=x -> i,tl
+  | p::tl -> let i,tl = rm_assoc n tl in i,p::tl
+;;
+
+(* naif implementation of the union-find merge operation
+   canonicals maps elements to canonicals
+   elements maps canonicals to the classes *)
+let merge canonicals elements n m =
+  let cn,canonicals = rm_assoc n canonicals in
+  let cm,canonicals = rm_assoc m canonicals in
+  let ln,elements = rm_assoc cn elements in
+  let lm,elements = rm_assoc cm elements in
+  let canonicals = 
+    (n,cm)::(m,cm)::List.map 
+      (fun ((x,xc) as p)  -> 
+        if xc = cn then (x,cm) else p) canonicals
+  in 
+  let elements = (cm,ln@lm)::elements 
+  in
+    canonicals,elements
+;;
+
+(* f x gives the direct dependencies of x;
+   x must not belong to (f x) and (f x) must
+   be a subset of l *)
+let clusters f l =
+  let canonicals = List.map (fun x -> (x,x)) l in
+  let elements = List.map (fun x -> (x,[x])) l in
+  let _,elements = 
+    List.fold_left 
+     (fun (canonicals,elements) x ->
+       let dep = f x in
+        List.fold_left 
+          (fun (canonicals,elements) d ->
+             merge canonicals elements d x) 
+          (canonicals,elements) dep)
+     (canonicals,elements) l
+  in
+    List.map snd elements
+;;
+
 (** {2 File predicates} *)
 
 let is_dir fname =