mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
small updates of Vector;
removed the capacity field in Vector
This commit is contained in:
parent
ef08010c6f
commit
3b6722d1c4
2 changed files with 14 additions and 15 deletions
23
vector.ml
23
vector.ml
|
|
@ -28,41 +28,38 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|||
(** a vector of 'a. *)
|
||||
type 'a t = {
|
||||
mutable size : int;
|
||||
mutable capacity : int;
|
||||
mutable vec : 'a array;
|
||||
}
|
||||
|
||||
let create i =
|
||||
assert (i >= 0);
|
||||
{ size = 0;
|
||||
capacity = i;
|
||||
vec = if i = 0 then [||] else Array.create i (Obj.magic None);
|
||||
}
|
||||
|
||||
(** resize the underlying array so that it can contains the
|
||||
given number of elements *)
|
||||
let resize v newcapacity =
|
||||
if newcapacity <= v.capacity
|
||||
let newcapacity = min newcapacity Sys.max_array_length in
|
||||
if newcapacity <= Array.length v.vec
|
||||
then () (* already big enough *)
|
||||
else begin
|
||||
assert (newcapacity >= v.size);
|
||||
let new_vec = Array.create newcapacity (Obj.magic None) in
|
||||
Array.blit v.vec 0 new_vec 0 v.size;
|
||||
v.vec <- new_vec;
|
||||
v.capacity <- newcapacity
|
||||
end
|
||||
|
||||
let clear v =
|
||||
v.size <- 0;
|
||||
if v.capacity > 1000 (* shrink if too large *)
|
||||
then (v.capacity <- 10;
|
||||
v.vec <- Array.create 10 (Obj.magic None))
|
||||
if Array.length v.vec > 1024 (* shrink if too large *)
|
||||
then (v.vec <- Array.create 10 (Obj.magic None))
|
||||
|
||||
let is_empty v = v.size = 0
|
||||
|
||||
let push v x =
|
||||
(if v.capacity = v.size
|
||||
then resize v (2 * v.capacity));
|
||||
(if Array.length v.vec = v.size
|
||||
then resize v (2 * v.size));
|
||||
v.vec.(v.size) <- x;
|
||||
v.size <- v.size + 1
|
||||
|
||||
|
|
@ -92,11 +89,11 @@ let copy v =
|
|||
let shrink v n =
|
||||
if n > v.size then failwith "cannot shrink to bigger size" else v.size <- n
|
||||
|
||||
let member ?(cmp=(=)) v x =
|
||||
let member ?(eq=(=)) v x =
|
||||
let n = v.size in
|
||||
let rec check i =
|
||||
if i = n then false
|
||||
else if cmp x v.vec.(i) then true
|
||||
else if eq x v.vec.(i) then true
|
||||
else check (i+1)
|
||||
in check 0
|
||||
|
||||
|
|
@ -177,11 +174,11 @@ let find v p =
|
|||
in check 0
|
||||
|
||||
let get v i =
|
||||
(if i < 0 || i >= v.size then failwith "wrong index for vector");
|
||||
(if i < 0 || i >= v.size then failwith "Vector.get");
|
||||
v.vec.(i)
|
||||
|
||||
let set v i x =
|
||||
(if i < 0 || i >= v.size then failwith "wrong index for vector");
|
||||
(if i < 0 || i >= v.size then failwith "Vector.set");
|
||||
v.vec.(i) <- x
|
||||
|
||||
let size v = v.size
|
||||
|
|
|
|||
|
|
@ -55,7 +55,7 @@ val copy : 'a t -> 'a t
|
|||
val shrink : 'a t -> int -> unit
|
||||
(** shrink to the given size (remove elements above this size) *)
|
||||
|
||||
val member : ?cmp:('a -> 'a -> bool) -> 'a t -> 'a -> bool
|
||||
val member : ?eq:('a -> 'a -> bool) -> 'a t -> 'a -> bool
|
||||
(** is the element a member of the vector? *)
|
||||
|
||||
val sort : ?cmp:('a -> 'a -> int) -> 'a t -> unit
|
||||
|
|
@ -98,7 +98,9 @@ val size : 'a t -> int
|
|||
(** number of elements in vector *)
|
||||
|
||||
val unsafe_get_array : 'a t -> 'a array
|
||||
(** Access the underlying *shared* array (do not modify!) *)
|
||||
(** Access the underlying *shared* array (do not modify!).
|
||||
[unsafe_get_array v] is longer than [size v], but elements at higher
|
||||
index than [size v] are undefined (do not access!). *)
|
||||
|
||||
val of_seq : ?init:'a t -> 'a Sequence.t -> 'a t
|
||||
val to_seq : 'a t -> 'a Sequence.t
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue