add CCVector.shrink_to_fit to limit memory usage

This commit is contained in:
Simon Cruanes 2019-11-19 14:46:33 -06:00
parent 1239960c42
commit 858616606b
2 changed files with 19 additions and 1 deletions

View file

@ -453,6 +453,21 @@ let shrink v n =
)
*)
let shrink_to_fit v : unit =
if v.size = 0 then (
v.vec <- [| |]
) else if v.size < Array.length v.vec then (
v.vec <- Array.sub v.vec 0 v.size
)
(*$QR
(gen Q.small_int) (fun v ->
let v' = copy v in
shrink_to_fit v;
to_list v = to_list v'
)
*)
let sort' cmp v =
(* possibly copy array (to avoid junk at its end), then sort the array *)
let a =

View file

@ -1,4 +1,3 @@
(* This file is free software, part of containers. See file "license" for more details. *)
(** {1 Growable, mutable vector} *)
@ -124,6 +123,10 @@ val shrink : ('a, rw) t -> int -> unit
(** Shrink to the given size (remove elements above this size).
Does nothing if the parameter is bigger than the current size. *)
val shrink_to_fit : ('a, _) t -> unit
(** Shrink internal array to fit the size of the vector
@since NEXT_RELEASE *)
val member : eq:('a -> 'a -> bool) -> 'a -> ('a, _) t -> bool
(** Is the element a member of the vector? *)