add Array.append

This commit is contained in:
Simon Cruanes 2016-12-07 16:20:13 +01:00
parent 85d0ff8f3f
commit cdb4ba5dbc
2 changed files with 23 additions and 0 deletions

View file

@ -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

View file

@ -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. *)