in CCVector, use invalid_arg, and document it

This commit is contained in:
Simon Cruanes 2021-10-20 10:47:06 -04:00
parent 7288045828
commit 541d716d5c
No known key found for this signature in database
GPG key ID: 4AC01D0849AA62B6
2 changed files with 7 additions and 4 deletions

View file

@ -129,7 +129,7 @@ let grow_with_ v ~filler:x =
) else ( ) else (
let n = Array.length v.vec in let n = Array.length v.vec in
let size = min (2 * n + 3) Sys.max_array_length in let size = min (2 * n + 3) Sys.max_array_length in
if size = n then failwith "vec: can't grow any further"; if size = n then invalid_arg "vec: can't grow any further";
resize_ v size v.vec.(0) resize_ v size v.vec.(0)
) )
@ -139,7 +139,7 @@ let grow_with_ v ~filler:x =
behave well *) behave well *)
let ensure_assuming_not_empty_ v ~size = let ensure_assuming_not_empty_ v ~size =
if size > Sys.max_array_length if size > Sys.max_array_length
then failwith "vec.ensure: size too big" then invalid_arg "vec.ensure: size too big"
else ( else (
let n = ref (max 8 (Array.length v.vec)) in let n = ref (max 8 (Array.length v.vec)) in
while !n < size do n := min Sys.max_array_length (2* !n) done; while !n < size do n := min Sys.max_array_length (2* !n) done;

View file

@ -66,12 +66,15 @@ val ensure_with : init:'a -> ('a, rw) t -> int -> unit
This does not affect [length v]. This does not affect [length v].
@param init if [capacity v = 0], used to enforce the type of the vector @param init if [capacity v = 0], used to enforce the type of the vector
(see {!create_with}). (see {!create_with}).
@raise Invalid_arg if the size is not suitable (negative, or too big for OCaml arrays)
@since 0.14 *) @since 0.14 *)
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 and [init] Just a hint, will not be enforced if the vector is empty and [init]
is not provided. *) is not provided.
@raise Invalid_arg if the size is not suitable (negative, or too big for OCaml arrays)
*)
val is_empty : ('a, _) t -> bool val is_empty : ('a, _) t -> bool
(** Is the vector empty? *) (** Is the vector empty? *)
@ -114,7 +117,7 @@ val pop : ('a, rw) t -> 'a option
(** Remove last element, or [None]. *) (** Remove last element, or [None]. *)
val pop_exn : ('a, rw) t -> 'a val pop_exn : ('a, rw) t -> 'a
(** Remove last element, or raise a Failure if empty. (** Remove last element, or raise an exception if empty.
@raise Empty on an empty vector. *) @raise Empty on an empty vector. *)
val top : ('a, _) t -> 'a option val top : ('a, _) t -> 'a option