summaryrefslogtreecommitdiff
path: root/lib/util.ml
diff options
context:
space:
mode:
authorGravatar Stephane Glondu <steph@glondu.net>2012-06-04 12:07:52 +0200
committerGravatar Stephane Glondu <steph@glondu.net>2012-06-04 12:07:52 +0200
commit61dc740ed1c3780cccaec00d059a28f0d31d0052 (patch)
treed88d05baf35b9b09a034233300f35a694f9fa6c2 /lib/util.ml
parent97fefe1fcca363a1317e066e7f4b99b9c1e9987b (diff)
Imported Upstream version 8.4~gamma0+really8.4beta2upstream/8.4_gamma0+really8.4beta2
Diffstat (limited to 'lib/util.ml')
-rw-r--r--lib/util.ml25
1 files changed, 24 insertions, 1 deletions
diff --git a/lib/util.ml b/lib/util.ml
index 287dd371..4f14b83a 100644
--- a/lib/util.ml
+++ b/lib/util.ml
@@ -499,6 +499,9 @@ let list_map4 f l1 l2 l3 l4 =
in
map (l1,l2,l3,l4)
+let list_map_to_array f l =
+ Array.of_list (List.map f l)
+
let rec list_smartfilter f l = match l with
[] -> l
| h::tl ->
@@ -708,6 +711,12 @@ let list_map_filter_i f =
match f i x with None -> l' | Some y -> y::l'
in aux 0
+let list_filter_along f filter l =
+ snd (list_filter2 (fun b c -> f b) (filter,l))
+
+let list_filter_with filter l =
+ list_filter_along (fun x -> x) filter l
+
let list_subset l1 l2 =
let t2 = Hashtbl.create 151 in
List.iter (fun x -> Hashtbl.add t2 x ()) l2;
@@ -741,7 +750,7 @@ let list_split_when p =
split_when_loop []
(* [list_split_by p l] splits [l] into two lists [(l1,l2)] such that elements of
- [l1] satisfy [p] and elements of [l2] do not *)
+ [l1] satisfy [p] and elements of [l2] do not; order is preserved *)
let list_split_by p =
let rec split_by_loop = function
| [] -> ([],[])
@@ -900,6 +909,14 @@ let rec list_cartesians_filter op init ll =
let rec list_drop_last = function [] -> assert false | hd :: [] -> [] | hd :: tl -> hd :: list_drop_last tl
+(* Factorize lists of pairs according to the left argument *)
+let rec list_factorize_left = function
+ | (a,b)::l ->
+ let al,l' = list_split_by (fun (a',b) -> a=a') l in
+ (a,(b::List.map snd al)) :: list_factorize_left l'
+ | [] ->
+ []
+
(* Arrays *)
let array_compare item_cmp v1 v2 =
@@ -1217,6 +1234,12 @@ let array_rev_to_list a =
if i >= Array.length a then res else tolist (i+1) (a.(i) :: res) in
tolist 0 []
+let array_filter_along f filter v =
+ Array.of_list (list_filter_along f filter (Array.to_list v))
+
+let array_filter_with filter v =
+ Array.of_list (list_filter_with filter (Array.to_list v))
+
(* Stream *)
let stream_nth n st =