From cdb4ba5dbc456885a008958228c3a04111ce41b1 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Wed, 7 Dec 2016 16:20:13 +0100 Subject: [PATCH] add `Array.append` --- src/core/CCArray.ml | 19 +++++++++++++++++++ src/core/CCArray.mli | 4 ++++ 2 files changed, 23 insertions(+) diff --git a/src/core/CCArray.ml b/src/core/CCArray.ml index f881489e..b58f2072 100644 --- a/src/core/CCArray.ml +++ b/src/core/CCArray.ml @@ -490,6 +490,25 @@ let filter_map f a = let filter p a = filter_map (fun x -> if p x then Some x else None) a +let append a1 a2 = + let n1 = Array.length a1 in + let n2 = Array.length a2 in + if n1=0 then a2 + else if n2=0 then a1 + else ( + let res = Array.make (n1+n2) a1.(0) in + Array.blit a1 0 res 0 n1; + Array.blit a2 0 res n1 n2; + res + ) + +(*$= & ~printer:Q.Print.(array int) + [| 1;2;3;4 |] (append [|1;2|] [| 3;4 |]) + [| 1;2;3;4 |] (append [||] [| 1;2; 3;4 |]) + [| 1;2;3;4 |] (append [| 1;2; 3;4 |] [||]) + [||] (append [||] [||]) +*) + (* append [rev a] in front of [acc] *) let rec __rev_append_list a acc i = if i = Array.length a diff --git a/src/core/CCArray.mli b/src/core/CCArray.mli index 11be5ebd..a8298c6d 100644 --- a/src/core/CCArray.mli +++ b/src/core/CCArray.mli @@ -187,6 +187,10 @@ val rev : 'a t -> 'a t (** Copy + reverse in place @since 0.20 *) +val append : 'a t -> 'a t -> 'a t +(** Append 2 arrays + @since NEXT_RELEASE *) + val filter : ('a -> bool) -> 'a t -> 'a t (** Filter elements out of the array. Only the elements satisfying the given predicate will be kept. *)