From 57b8e68cb57a320e99574ba70a5f3548dc85c824 Mon Sep 17 00:00:00 2001 From: Francois Berenger Date: Fri, 18 Dec 2020 13:34:28 +0900 Subject: [PATCH] BatArray.remove_at --- src/batArray.mliv | 6 ++++++ src/batArray.mlv | 16 ++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/batArray.mliv b/src/batArray.mliv index ab8309f12..b58d67045 100644 --- a/src/batArray.mliv +++ b/src/batArray.mliv @@ -558,6 +558,12 @@ val insert : 'a array -> 'a -> int -> 'a array @raise Invalid_argument if [i < 0 || i > Array.length xs]. *) +val remove_at : int -> 'a array -> 'a array +(** [remove_at i a] returns the array [a] without the element at index [i]. + + @raise Invalid_argument if [i] is outside of [a] bounds. + @since NEXT_RELEASE *) + (** {6 Boilerplate code}*) val print : ?first:string -> ?last:string -> ?sep:string -> diff --git a/src/batArray.mlv b/src/batArray.mlv index f1fcbadc1..9a9290cfe 100644 --- a/src/batArray.mlv +++ b/src/batArray.mlv @@ -822,6 +822,22 @@ let insert xs x i = with Invalid_argument _ -> true *) +let remove_at i src = + let x = src.(i) in (* keep the bound check in there *) + let n = length src in + let dst = make (n - 1) x in + blit src 0 dst 0 i; + blit src (i + 1) dst i (n - i - 1); + dst +(*$T remove_at + try remove_at 0 [||] = [|1|] \ + with Invalid_argument _ -> true + remove_at 0 [|1;2;3|] = [|2;3|] + remove_at 1 [|1;2;3|] = [|1;3|] + remove_at 2 [|1;2;3|] = [|1;2|] + try remove_at 3 [|1;2;3|] = [|1|] \ + with Invalid_argument _ -> true +*) (* helper function; only works for arrays of equal length *) let eq_elements eq_elt a1 a2 = for_all2 eq_elt a1 a2