Module Hector.Int

type element = int

This module offers an implementation of vectors. A vector is a mutable data structure, which stores a sequence of values.

type vector

The type of a vector.

type t = vector

A synonym for the type of a vector.

type length = int

An integer value of type length represents the length of a sequence. For example, it can be the length of an array, the length of a vector, or the length of a segment of an array of vector. A length is nonnegative.

type capacity = int

An integer value of type capacity represents the capacity of a vector. A capacity is nonnegative.

type index = int

An integer value of type index represents an index into a sequence of elements.

Creating

val create : unit -> vector

create() creates a new vector of length 0 and capacity 0.

val make : length -> element -> vector

make n x creates a new vector of length n and capacity n and initializes this vector by storing the value x everywhere. n must be a valid capacity.

val init : length -> (index -> element) -> vector

init n f creates a new vector of length n and capacity n and initializes it, from left to right, by computing and storing the value f i at index i. n must be a valid capacity.

val copy : vector -> vector

copy v creates a new vector whose length and capacity are length v and initializes it with a copy of the data stored in the vector v.

val sub : vector -> index -> length -> vector

sub v i n produces a new vector whose elements are the elements of the vector segment determined by vector v, index i, and length n. i and n must describe a valid segment of the vector v.

val concat : vector list -> vector

concat vs produces a new vector whose sequence of elements is the concatenation of the sequences of elements of the vectors in the list vs.

Reading and writing

val get : vector -> index -> element

get v i fetches the element that lies at index i in the vector v. i must be comprised in the semi-open interval [0, length v).

val set : vector -> index -> element -> unit

set v i x overwrites the element that lies at index i in the vector v with the value x. i must be comprised in the semi-open interval [0, length v).

val top : vector -> element

If the vector v is nonempty, top v returns its last element. If the vector v is empty, top v raises Not_found.

val get_last : vector -> element

get_last is a synonym for top.

val top_opt : vector -> element option

If the vector v is nonempty, top_opt v returns its last element. If the vector v is empty, top_opt v returns None.

val find_last : vector -> element option

find_last is a synonym for top_opt.

val fill : vector -> index -> length -> element -> unit

fill v i n x writes the value x into every slot of the vector segment determined by vector v, index i, and length n. i and n must describe a valid segment of the vector v.

val blit : vector -> index -> vector -> index -> length -> unit

blit v i v' i' n copies data from the vector segment determined by vector v, index i, and length n into the vector segment determined by vector v', index i', and length n. It works correctly even if the source and destination segments overlap. i and n must describe a valid segment of the vector v. i' and n must describe a valid segment of the vector v'.

Unsafe access

val unsafe_get : vector -> index -> element

unsafe_get v i fetches the element that lies at index i in the vector v. i must be comprised in the semi-open interval [0, length v). No bounds check is performed. If the index i is out of bounds, memory safety can be compromised. Use at your own risk!

val unsafe_set : vector -> index -> element -> unit

unsafe_set v i x overwrites the element that lies at index i in the vector v with the value x. i must be comprised in the semi-open interval [0, length v). No bounds check is performed. If the index i is out of bounds, memory safety can be compromised. Use at your own risk!

val unsafe_borrow : vector -> element array

unsafe_borrow v returns the data array that is part of the internal representation of the vector v. The length of this data array is at least length v, and can be greater than length v. Beyond this guarantee, the length of this data array is unspecified; it is not necessarily the capacity of the vector. As long as the vector v is not modified by other means, the segment of the data array delimited by the semi-open interval [0, length v) can be safely read and written.

Pushing

val push : vector -> element -> unit

push v x extends the vector v with the element x. The length of the vector v is increased by one. If necessary, the capacity of the vector v is increased.

val add_last : vector -> element -> unit

add_last is a synonym for push.

val push_array : vector -> element array -> unit

push_array v a extends the vector v with the elements of the array a. The length of the vector v is increased by the length of the array a. If necessary, the capacity of the vector v is increased.

val append_array : vector -> element array -> unit

append_array is a synonym for push_array.

val push_array_segment : vector -> element array -> index -> length -> unit

push_array_segment v a i n extends the vector v with the elements of the array segment determined by array a, index i, and length n. The length of the vector v is increased by n. If necessary, the capacity of the vector v is increased. i and n must describe a valid segment of the array a.

val append_array_segment : vector -> element array -> index -> length -> unit

append_array_segment is a synonym for push_array_segment.

val push_vector : vector -> vector -> unit

push_vector v v' extends the vector v with the elements of the vector v'. The length of the vector v is increased by the length of the array v'. If necessary, the capacity of the vector v is increased.

val append : vector -> vector -> unit

append is a synonym for push_vector.

val push_list : vector -> element list -> unit

push_list v xs extends the vector v with the elements of the list xs. The length of the vector v is increased by the length of the list xs. If necessary, the capacity of the vector v is increased.

val append_list : vector -> element list -> unit

append_list is a synonym for push_list.

val push_seq : vector -> element Seq.t -> unit

push_seq v xs extends the vector v with the elements of the sequence xs. The length of the vector v is increased by the length of the sequence xs. If necessary, the capacity of the vector v is increased. The sequence xs is demanded just once.

val append_seq : vector -> element Seq.t -> unit

append_seq is a synonym for push_seq.

val push_iter : vector -> ((element -> unit) -> 'c -> unit) -> 'c -> unit

push_iter v iter c pushes each element of the collection c in turn onto the vector v. The function iter is used to iterate over the elements of c. In other words, push_iter v iter c is equivalent to iter (push v) c.

val append_iter : vector -> ((element -> unit) -> 'c -> unit) -> 'c -> unit

append_iter is a synonym for push_iter.

Popping

val pop : vector -> element

If the vector v is nonempty, pop v removes and returns its last element. The length of the vector is decreased by one; its capacity is unchanged. If the vector v is empty, pop v raises Not_found.

val pop_last : vector -> element

pop_last is a synonym for pop.

val pop_opt : vector -> element option

If the vector v is nonempty, pop_opt v removes and returns its last element. The length of the vector is decreased by one; its capacity is unchanged. If the vector v is empty, pop_opt v returns None.

val pop_last_opt : vector -> element option

pop_last_opt is a synonym for pop_opt.

val drop : vector -> unit

If the vector v is nonempty, drop v removes its last element. If the vector v is empty, drop v has no effect. drop v is equivalent to if is_empty v then () else ignore (pop v).

val remove_last : vector -> unit

remove_last is a synonym for drop.

Iterating

While iteration is ongoing, the vector must not be modified. For example, in iter f v, the function f is not allowed to modify the vector v. This rule applies to all iteration functions.

val iter : (element -> unit) -> vector -> unit

iter f v applies the function f in turn, from left to right, to each element x of the vector v.

val iter_down : (element -> unit) -> vector -> unit

iter_down f v applies the function f in turn, from right to left, to each element x of the vector v.

val iteri : (int -> element -> unit) -> vector -> unit

iteri f v applies the function f in turn, from left to right, to each index i and corresponding element x of the vector v.

val fold_left : ('s -> element -> 's) -> 's -> vector -> 's

fold_left f s v applies the function f in turn, from left to right, to each element x of the vector v. A state, whose initial value is s, is threaded through this sequence of function invocations, and is eventually returned.

val fold_right : (element -> 's -> 's) -> vector -> 's -> 's

fold_right f v s applies the function f in turn, from right to left, to each element x of the vector v. A state, whose initial value is s, is threaded through this sequence of function invocations, and is eventually returned.

Transforming

While transformation is ongoing, the vector must not be modified. For example, in map f v, the function f is not allowed to modify the vector v. This rule applies to all transformation functions.

val map : (element -> element) -> vector -> vector

map f v applies the function f in turn, from left to right, to each element x of the vector v, and constructs a new vector of the results of these calls.

val mapi : (index -> element -> element) -> vector -> vector

mapi f v applies the function f in turn, from left to right, to each index i and corresponding element x of the vector v, and constructs a new vector of the results of these calls.

val filter : (element -> bool) -> vector -> vector

filter f v applies the function f in turn, from left to right, to each element x of the vector v, and constructs a new vector containing just the elements x such that f x returned true.

val filter_map : (element -> element option) -> vector -> vector

filter_map f v applies the function f in turn, from left to right, to each element x of the vector v, and constructs a new vector containing just the values y such that f x returned Some y.

Searching

While searching is in progress, the vector must not be modified. For example, in exists f v, the function f is not allowed to modify the vector v. This rule applies to all search functions.

val exists : (element -> bool) -> vector -> bool

exists f v determines whether at least one element x of the vector v satisfies the predicate f. The vector is scanned from left to right.

val for_all : (element -> bool) -> vector -> bool

for_all f v determines whether all elements x of the vector v satisfy the predicate f. The vector is scanned from left to right.

val find : (element -> bool) -> vector -> int

find f v finds the leftmost element x of the vector v such that f x is true, and returns its index. If no such element exists, then find f v raises Not_found.

Comparing and sorting

While comparison is in progress, the vector must not be modified. For example, in equal eq v v', the function eq is not allowed to modify the vector v. This rule applies to all comparison functions.

val equal : (element -> element -> bool) -> vector -> vector -> bool

Provided eq is an equality on elements, equal eq is the pointwise equality of vectors. In other words, equal eq v v' determines whether the sequences of elements of the vectors v and v' are pointwise equal, using the function eq to test whether two elements are equal.

val compare : (element -> element -> int) -> vector -> vector -> int

Provided cmp is a preorder on elements, compare cmp is the lexicographic preorder on vectors. In other words, compare cmp v v' compares the sequences of elements of the vectors v and v', according to the lexicographic preorder, and using the function cmp to compare two elements.

Caution: compare behaves like List.compare, not like Dynarray.compare. Dynarray.compare implements a preorder on vectors that is not is the lexicographic preorder.

val sort : (element -> element -> int) -> vector -> unit

sort cmp v sorts the vector v, in place, according to the preorder cmp.

sort is currently a synonym for stable_sort.

val stable_sort : (element -> element -> int) -> vector -> unit

stable_sort cmp v sorts the vector v, in place, according to the preorder cmp. This is a stable sort: if two elements are equivalent according to cmp then their relative order in the sequence is preserved. This is a merge sort algorithm; it is the same algorithm as in Array.stable_sort.

val fast_sort : (element -> element -> int) -> vector -> unit

fast_sort cmp v sorts the vector v, in place, according to the preorder cmp.

fast_sort is currently a synonym for stable_sort.

Converting

val of_array : element array -> vector

of_array a returns a new vector whose elements are the elements of the array a. The length and capacity of the new vector are the length of the array a.

val of_list : element list -> vector

of_list xs returns a new vector whose elements are the elements of the list xs. The length and capacity of the new vector are the length of the list xs.

val of_seq : element Seq.t -> vector

of_seq xs returns a new vector whose elements are the elements of the sequence xs. The length and capacity of the new vector are the length of the sequence xs.

val to_array : vector -> element array

to_array v creates a new array whose elements are the elements of the vector v. The length of the new array is the length of the vector v.

val to_list : vector -> element list

to_list v creates a new list whose elements are the elements of the vector v. The length of the new list is the length of the vector v.

val to_seq : vector -> element Seq.t

to_seq v creates a sequence whose elements are the elements of the vector v. The length of this sequence is the length of the vector v. This sequence can be demanded as many times as one wishes. However, this sequence is valid only as long as the vector v is not modified. As soon as v is modified, this sequence must no longer be used.

val to_seq_rev : vector -> element Seq.t

to_seq_rev v creates a sequence whose elements are the elements of the vector v, in reverse order. The length of this sequence is the length of the vector v. This sequence can be demanded as many times as one wishes. However, this sequence is valid only as long as the vector v is not modified. As soon as v is modified, this sequence must no longer be used.

Showing

val show : (element -> string) -> vector -> string

show f v returns a textual representation of the contents of the vector v. The user-supplied function f is used to obtain a textual representation of each element.

Length

val length : vector -> length

length v is the (logical) length of the vector v.

val is_empty : vector -> bool

s_empty v is equivalent to length v = 0.

val truncate : vector -> length -> unit

If n is less than length v, then truncate v n sets the length of the vector v to n. Otherwise, nothing happens. In either case, the capacity of the vector v is unchanged. This is a constant-time operation.

val clear : vector -> unit

clear v is equivalent to truncate v 0. The length of the vector v becomes zero. Its capacity is unchanged.

Capacity

val reset : vector -> unit

reset v sets the length and the capacity of the vector v to zero.

val ensure_capacity : vector -> capacity -> unit

ensure_capacity v c ensures that the capacity of the vector v is at least c. If necessary, the capacity of the vector v is increased.

val ensure_extra_capacity : vector -> capacity -> unit

ensure_extra_capacity v delta ensures that the capacity of the vector v is at least length v + delta. If necessary, the capacity of the vector v is increased. The increment delta must be nonnegative.

val fit_capacity : vector -> unit

fit_capacity v ensures that the capacity of the vector v matches its length. If necessary, the capacity of the vector v is decreased.

val set_capacity : vector -> capacity -> unit

set_capacity v c ensures that the capacity of the vector v is exactly c. If c is less than length v, then the vector v is truncated: some elements are lost. Otherwise, the elements of the vector v are preserved, and its capacity is decreased or increased as necessary.

The Stack API

module Stack : sig ... end

This module offers the same API as the standard library module Stdlib.Stack, but is implemented using vectors.