Module Indexing.Vector

The submodule Vector allows safely manipulating indices into a vector.

type ('n, 'a) t = ('n, 'a) vector
val as_array : (_, 'a) t -> 'a array

as_array v exposes a view of the vector v as an ordinary array. This is a safe cast. This operation does nothing at runtime.

val length : ('n, 'a) t -> 'n cardinal

length is analogous to Array.length, but returns a cardinal instead of an ordinary integer.

val get : ('n, 'a) t -> 'n index -> 'a

get is Array.get, but expects an index instead of an ordinary integer. This guarantees that the index is within bounds.

val set : ('n, 'a) t -> 'n index -> 'a -> unit

set is Array.set, but expects an index instead of an ordinary integer. This guarantees that the index is within bounds.

val set_cons : ('n, 'a list) t -> 'n index -> 'a -> unit

set_cons t i x is short for set t i (x :: get t i).

val empty : (Empty.n, _) t

empty is the empty vector.

val make : 'n cardinal -> 'a -> ('n, 'a) t

make is analogous to Array.make. Invoking make n x fixes the cardinal n.

val make' : 'n cardinal -> (unit -> 'a) -> ('n, 'a) t

make' n f is roughly analogous to make n (f()), but removes the need to exhibit a value of type 'a when n is zero. The function call f() takes place only if n is greater than zero. It takes place at most once. Invoking make' n f fixes the cardinal n.

val init : 'n cardinal -> ('n index -> 'a) -> ('n, 'a) t

init is analogous to Array.init. Invoking init n f fixes the cardinal n.

val map : ('a -> 'b) -> ('n, 'a) t -> ('n, 'b) t

map is Array.map.

val mapi : ('n index -> 'a -> 'b) -> ('n, 'a) t -> ('n, 'b) t

mapi is Array.mapi.

val copy : ('n, 'a) t -> ('n, 'a) t

copy is Array.copy.

val iter : ('a -> unit) -> ('n, 'a) t -> unit

iter is Array.iter.

val iteri : ('n index -> 'a -> unit) -> ('n, 'a) t -> unit

iteri is Array.iteri.

val iter2 : ('a -> 'b -> unit) -> ('n, 'a) t -> ('n, 'b) t -> unit

iter2 is Array.iter2.

val fold_left : ('a -> 'b -> 'a) -> 'a -> (_, 'b) t -> 'a

fold_left is Array.fold_left.

val fold_right : ('b -> 'a -> 'a) -> (_, 'b) t -> 'a -> 'a

fold_right is Array.fold_right.

val fold_left2 : ('a -> 'b -> 'c -> 'a) -> 'a -> ('n, 'b) t -> ('n, 'c) t -> 'a

fold_left2 f accu v1 v2 folds the function f with initial accumulator accu simultaneously over the vectors v1 and v2. The elements of the vectors are processed left to right.

val fold_right2 : ('b -> 'c -> 'a -> 'a) -> ('n, 'b) t -> ('n, 'c) t -> 'a -> 'a

fold_right2 f v1 v2 accu folds the function f with initial accumulator accu simultaneously over the vectors v1 and v2. The elements of the vectors are processed right to left.

val to_list : (_, 'a) t -> 'a list

to_list is Array.to_list.

val sort : ('a -> 'a -> int) -> ('n, 'a) t -> unit

sort is Array.sort.

val invert : 'n cardinal -> ('m, 'n index) t -> ('n, 'm index option) t

Suppose v is a vector that maps indices in the range [0,m) to indices in the range [0,n), and suppose that v represents an injective function. Then, invert n v returns a vector that represents the inverse function, a partial function of [0,n) into [0,m).

val of_array : 'n cardinal -> 'a array -> ('n, 'a) t

of_array n a checks that the cardinal n is equal to the length of the array a and converts the array a to a vector. If the cardinal n is not yet fixed, then, as a side effect of this call, it becomes fixed.

  • raises Invalid_argument

    if the cardinal and array length are not equal.

module type V = sig ... end

The module type V is a module-level analogue of the type vector.

module Of_array (A : sig ... end) : V with type a = A.a

The functor Of_array converts an ordinary array to a vector. No runtime check is involved. The type component n of the result serves as a fresh type-level identifier for the length of this vector.