Permalink
Browse files

[feature] CpsServerLib: parallel iter and map on lists

  • Loading branch information...
1 parent cb74acd commit 0fbf161466cd67f231a191c11f066dbc78713e00 Louis Gesbert committed Jul 8, 2011
Showing with 32 additions and 0 deletions.
  1. +25 −0 qmlcps/qmlCpsServerLib.ml
  2. +7 −0 qmlcps/qmlCpsServerLib.mli
View
@@ -617,6 +617,31 @@ let rec fold_list f acc l k = match l with
| [] -> acc |> k
| hd::tl -> f acc hd @> ccont_ml k @> fun acc -> fold_list f acc tl @> k
+let rec iter_list f l k =
+ let n = ref 0 in
+ let k =
+ ccont_ml k (fun () -> decr n; if !n == 0 then () |> k)
+ in
+ List.iter
+ (fun x -> incr n; Scheduler.push Scheduler.default (fun () -> f x @> k))
+ l
+
+let rec map_list f l k =
+ let n = ref 0 in
+ let results = ref [||] in
+ let ki =
+ fun i ->
+ ccont_ml k
+ (fun x ->
+ !results.(i) <- x;
+ decr n;
+ if !n > 0 then () else Array.to_list !results |> k)
+ in
+ List.iter
+ (fun x -> let k = ki !n in incr n; Scheduler.push Scheduler.default (fun () -> f x @> k))
+ l;
+ results := Array.make !n (Obj.magic ())
+
let fold_array f acc arr k =
let s = Array.length arr in
let rec aux acc i k =
@@ -282,7 +282,14 @@ end
List.fold is a fun exercise, it's less efficient. *)
val fold_list : ('acc -> 'a -> 'acc continuation -> unit) -> 'acc -> 'a list -> 'acc continuation -> unit
+(** Maps the given cps function on the elements of the list, in parallel *)
+val map_list : ('a -> 'b continuation -> unit) -> 'a list -> 'b list continuation -> unit
+
+(** Iters the given cps function on the elements of the list, in parallel *)
+val iter_list : ('a -> unit continuation -> unit) -> 'a list -> unit continuation -> unit
+
val fold_array : ('acc -> 'a -> 'acc continuation -> unit) -> 'acc -> 'a array -> 'acc continuation -> unit
+
(*
(*
{6 Tasks}

0 comments on commit 0fbf161

Please sign in to comment.