package patoline

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type

Dynamic arrays

A dynamic array behaves like an ordinary array, except that it can grow on demand. It is implemented as a regular OCaml array, except that any write attempt beyond the end of the array triggers the allocation of a new array, whose size is twice the size of the current array. This new array replaces the old one.

type 'a t = {
  1. mutable data : 'a array;
  2. mutable length : int;
  3. default_value : 'a;
}

Type of dynamic arrays. A dynamic array stores a defaut value of type 'a, which is used to initialize objects when expanding arrays.

val make : int -> 'a -> 'a t

Creating a new array, which initially has n elements

val get : 'a t -> int -> 'a

Retrieve the element stored at index i. Returns the array default value if index i is beyond the end of the array.

val set : 'a t -> int -> 'a -> unit

Sets the value of the element at index i, possibly resizing the array if needed.

val length : 'a t -> int

Gets the actual number of elements stored in the array, which is always smaller than the number of available slots in memory. This always corresponds to i+1 where i is the maximal index ever given to a call to set.

val append : 'a t -> 'a -> unit

Appends a new element after the last one

val empty : 'a t -> unit

Reset the dynamic length to zero, without actually freeing slots.

val slots_length : 'a t -> int

Returns the size of the actual OCaml array. This corresponds to the number of available slots which can be filled before set needs to reallocate a new array.