From d1ddeeb31f54ca5aed74d8c46a94cfdfe6a8aa5c Mon Sep 17 00:00:00 2001 From: Dario Pinto <31403378+RadioPotin@users.noreply.github.com> Date: Thu, 21 Oct 2021 16:57:23 +0200 Subject: [PATCH] add CCVector.resize_with and CCVector.resize_with_init, tests and doc (#389) add CCVector.resize_with and CCVector.resize_with_init, tests and doc --- src/core/CCVector.ml | 49 +++++++++++++++++++++++++++++++++++++++++++ src/core/CCVector.mli | 18 ++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/src/core/CCVector.ml b/src/core/CCVector.ml index 3bf6ab51..96230351 100644 --- a/src/core/CCVector.ml +++ b/src/core/CCVector.ml @@ -201,6 +201,55 @@ let push v x = let v = of_list [1;2;3] in push v 4; to_list v = [1;2;3;4] *) +let resize_with v f size = + let new_vec = + if size >= Array.length v.vec then + let new_vec = Array.make size (f 0) in + Array.blit v.vec 0 new_vec 0 (v.size - 1); + new_vec + else + v.vec + in + for i = v.size to size - 1 do + Array.unsafe_set new_vec i (f i) + done; + v.vec <- new_vec; + v.size <- size + +(*$T + let v = make 1 0 in resize_with v (fun i -> i) 5; to_list v = [0;1;2;3;4] + let v = make 1 0 in resize_with v (fun i -> i) 5; CCList.length (to_list v) = 5 + let v = create_with ~capacity:2 0 in resize_with v (fun i -> i) 5; to_list v = [0;1;2;3;4] + let v = make 5 0 in resize_with v (fun i -> i) 5; to_list v = [0;0;0;0;0] + let v = make 5 0 in resize_with v (fun i -> i) 6; to_list v = [0;0;0;0;0;6] + let v = make 5 0 in resize_with v (fun i -> i) 4; to_list v = [0;0;0;0;0] + let v = make 5 0 in resize_with v (fun i -> i) 5; CCList.length (to_list v) = 5 +*) + +let resize_with_init v ~init size = + let new_vec = + if size >= Array.length v.vec then + let new_vec = Array.make size init in + Array.blit v.vec 0 new_vec 0 (v.size - 1); + new_vec + else + v.vec + in + for i = v.size to size - 1 do + Array.unsafe_set new_vec i init + done; + v.vec <- new_vec; + v.size <- size + +(*$T + let v = make 1 0 in resize_with_init v ~init:1 5; to_list v = [1;1;1;1;1] + let v = make 1 0 in resize_with_init v ~init:1 5; CCList.length (to_list v) = 5 + let v = create_with ~capacity:2 0 in resize_with v ~init:1 5; to_list v = [1;1;1;1;1] + let v = make 5 0 in resize_with_init v ~init:1 5; to_list v = [1;1;1;1;1] + let v = make 5 0 in resize_with_init v ~init:1 4; to_list v = [1;1;1;1;1] + let v = make 5 0 in resize_with_init v ~init:1 5; CCList.length (to_list v) = 5 +*) + (** Add all elements of b to a *) let append a b = if array_is_empty_ a then ( diff --git a/src/core/CCVector.mli b/src/core/CCVector.mli index 88a9edcb..8e02ea64 100644 --- a/src/core/CCVector.mli +++ b/src/core/CCVector.mli @@ -82,6 +82,24 @@ val is_empty : ('a, _) t -> bool val push : ('a, rw) t -> 'a -> unit (** Add an element at the end of the vector. *) +val resize_with : ('a, rw) t -> (int -> 'a) -> int -> unit +(** [resize_with vec f size] resizes vector [vec] up to [size], fills vector + with calls to [f] on indexes [[vec.size-1.. size - 1]]. + The contents and size of vec are untouched if [size] is inferior or equal + to [length vec]. + @raise Invalid_argument if the size is too big + + @since NEXT_RELEASE *) + +val resize_with_init : ('a, rw) t -> init:'a -> int -> unit +(** [resize_with_init vec init size] resizes vector [vec] up to [size], + fills vector with calls to [init] on indexes [[length vec -1.. size - 1]]. + The contents and size of vec are untouched if [size] is inferior or equal + to [length vec]. + @raise Invalid_argument if the size is too big + + @since NEXT_RELEASE *) + val append : ('a, rw) t -> ('a, _) t -> unit (** [append a b] adds all elements of b to a. *)