Skip to content

Commit

Permalink
applye reviews, fix bug, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
RadioPotin committed Oct 21, 2021
1 parent 4114f1a commit ec86b57
Showing 1 changed file with 39 additions and 30 deletions.
69 changes: 39 additions & 30 deletions src/core/CCVector.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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) (-1); 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 (-1); 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 *)
Expand Down

0 comments on commit ec86b57

Please sign in to comment.