From 27eb874523fa74ee275757ec651523b8854aa947 Mon Sep 17 00:00:00 2001 From: Fardale Date: Sat, 14 Dec 2019 20:14:10 +0100 Subject: [PATCH] CCVector: resize_ take the elt as argument It is safer to have the check on the size of the underlying array close to access of the first element. --- src/core/CCVector.ml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/core/CCVector.ml b/src/core/CCVector.ml index 04ed2895..09d03729 100644 --- a/src/core/CCVector.ml +++ b/src/core/CCVector.ml @@ -83,11 +83,11 @@ let init n f = { let array_is_empty_ v = Array.length v.vec = 0 -(* assuming the underlying array isn't empty, resize it *) -let resize_ v newcapacity = +(* resize the underlying array using x to temporarily fill the array *) +let resize_ v newcapacity x = assert (newcapacity >= v.size); assert (not (array_is_empty_ v)); - let new_vec = Array.make newcapacity v.vec.(0) in + let new_vec = Array.make newcapacity x in Array.blit v.vec 0 new_vec 0 v.size; fill_with_junk_ new_vec v.size (newcapacity-v.size); v.vec <- new_vec; @@ -131,7 +131,7 @@ let grow_with_ v ~filler:x = let n = Array.length v.vec in let size = min (2 * n + 3) Sys.max_array_length in if size = n then failwith "vec: can't grow any further"; - resize_ v size + resize_ v size v.vec.(0) ) (* v is not empty; ensure it has at least [size] slots. @@ -144,7 +144,7 @@ let ensure_assuming_not_empty_ v ~size = else ( let n = ref (max 8 (Array.length v.vec)) in while !n < size do n := min Sys.max_array_length (2* !n) done; - resize_ v !n + resize_ v !n v.vec.(0) ) let ensure_with ~init v size =