mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-10 13:13:56 -05:00
add CCVector.ensure_with
This commit is contained in:
parent
2c39b63945
commit
fb3ffa1bb5
2 changed files with 23 additions and 7 deletions
|
|
@ -117,13 +117,12 @@ let _grow v x =
|
||||||
_resize v size
|
_resize v size
|
||||||
)
|
)
|
||||||
|
|
||||||
(* resize so that capacity is at least size. Use a doubling-size
|
(* v is not empty; ensure it has at least [size] slots.
|
||||||
strategy so that calling many times [ensure] will
|
|
||||||
|
Use a doubling-size strategy so that calling many times [ensure] will
|
||||||
behave well *)
|
behave well *)
|
||||||
let ensure v size =
|
let ensure_not_empty_ v size =
|
||||||
if Array.length v.vec = 0
|
if size > Sys.max_array_length
|
||||||
then ()
|
|
||||||
else if size > Sys.max_array_length
|
|
||||||
then failwith "vec.ensure: size too big"
|
then failwith "vec.ensure: size too big"
|
||||||
else (
|
else (
|
||||||
let n = ref (max 16 (Array.length v.vec)) in
|
let n = ref (max 16 (Array.length v.vec)) in
|
||||||
|
|
@ -131,6 +130,16 @@ let ensure v size =
|
||||||
_resize v !n
|
_resize v !n
|
||||||
)
|
)
|
||||||
|
|
||||||
|
let ensure_with ~init v size =
|
||||||
|
if Array.length v.vec = 0
|
||||||
|
then v.vec <- Array.make size init
|
||||||
|
else ensure_not_empty_ v size
|
||||||
|
|
||||||
|
let ensure v size =
|
||||||
|
if Array.length v.vec = 0
|
||||||
|
then ()
|
||||||
|
else ensure_not_empty_ v size
|
||||||
|
|
||||||
let clear v =
|
let clear v =
|
||||||
v.size <- 0
|
v.size <- 0
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -72,9 +72,16 @@ val init : int -> (int -> 'a) -> ('a, 'mut) t
|
||||||
val clear : ('a, rw) t -> unit
|
val clear : ('a, rw) t -> unit
|
||||||
(** clear the content of the vector *)
|
(** clear the content of the vector *)
|
||||||
|
|
||||||
|
val ensure_with : init:'a -> ('a, rw) t -> int -> unit
|
||||||
|
(** Hint to the vector that it should have at least the given capacity.
|
||||||
|
@param init if [capacity v = 0], used as a filler
|
||||||
|
element for the underlying array (see {!create_with})
|
||||||
|
@since NEXT_RELEASE *)
|
||||||
|
|
||||||
val ensure : ('a, rw) t -> int -> unit
|
val ensure : ('a, rw) t -> int -> unit
|
||||||
(** Hint to the vector that it should have at least the given capacity.
|
(** Hint to the vector that it should have at least the given capacity.
|
||||||
Just a hint, will not be enforced if the vector is empty. *)
|
Just a hint, will not be enforced if the vector is empty and [init]
|
||||||
|
is not provided. *)
|
||||||
|
|
||||||
val is_empty : ('a, _) t -> bool
|
val is_empty : ('a, _) t -> bool
|
||||||
(** is the vector empty? *)
|
(** is the vector empty? *)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue