diff --git a/src/core/CCVector.ml b/src/core/CCVector.ml index 672433d30..42ae69484 100644 --- a/src/core/CCVector.ml +++ b/src/core/CCVector.ml @@ -202,43 +202,52 @@ let push v x = *) let resize_with v f size = - if size > Sys.max_array_length then - invalid_arg "vec.resize_with: size too big" - else 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); - for i = v.size to size - 1 do - Array.unsafe_set new_vec i (f i) - done; - v.vec <- new_vec; - v.size <- 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 to_list (resize_with v (fun i -> i) 5) = [0;1;2;3;4] - let v = make 1 0 in length (resize_with v (fun i -> i) 5) = 5 - - let v = make 5 0 in to_list (resize_with v (fun i -> i) 5) = [0;0;0;0;0] - let v = make 5 0 in to_list (resize_with v (fun i -> i) 6) = [0;0;0;0;0;6] - let v = make 5 0 in to_list (resize_with v (fun i -> i) -1) = [0;0;0;0;0] - let v = make 5 0 in length (resize_with v (fun i -> i) 5) = 5 + 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 = - if size > Sys.max_array_length then - invalid_arg "vec.resize_with_init: size too big" - else 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); - v.vec <- new_vec; - v.size <- 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 to_list (resize_with_init v ~init:1 5) = [1;1;1;1;1] - let v = make 1 0 in length (resize_with_init v ~init:1 5) = 5 - - let v = make 5 0 in to_list (resize_with_init v ~init:1 5) = [1;1;1;1;1] - let v = make 5 0 in to_list (resize_with_init v ~init:1 -1) = [1;1;1;1;1] - let v = make 5 0 in length (resize_with_init v ~init:1 5) = 5 + 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 *)