Module Stdlib.Array

Array operations.

The labeled version of this module can be used as described in the StdLabels module.

type t('a) = array('a);

An alias for the type of arrays.

let length: array('a) => int;

Return the length (number of elements) of the given array.

let get: array('a) => int => 'a;

get a n returns the element number n of array a. The first element has number 0. The last element has number length a - 1. You can also write a.(n) instead of get a n.

let set: array('a) => int => 'a => unit;

set a n x modifies array a in place, replacing element number n with x. You can also write a.(n) <- x instead of set a n x.

let make: int => 'a => array('a);

make n x returns a fresh array of length n, initialized with x. All the elements of this new array are initially physically equal to x (in the sense of the == predicate). Consequently, if x is mutable, it is shared among all elements of the array, and modifying x through one of the array entries will modify all other entries at the same time.

  • raises Invalid_argument

    if n < 0 or n > Sys.max_array_length. If the value of x is a floating-point number, then the maximum size is only Sys.max_array_length / 2.

let create_float: int => array(float);

create_float n returns a fresh float array of length n, with uninitialized data.

  • since 4.03
let init: int => (int => 'a) => array('a);

init n f returns a fresh array of length n, with element number i initialized to the result of f i. In other terms, init n f tabulates the results of f applied in order to the integers 0 to n-1.

  • raises Invalid_argument

    if n < 0 or n > Sys.max_array_length. If the return type of f is float, then the maximum size is only Sys.max_array_length / 2.

let make_matrix: int => int => 'a => array(array('a));

make_matrix dimx dimy e returns a two-dimensional array (an array of arrays) with first dimension dimx and second dimension dimy. All the elements of this new matrix are initially physically equal to e. The element (x,y) of a matrix m is accessed with the notation m.(x).(y).

  • raises Invalid_argument

    if dimx or dimy is negative or greater than Sys.max_array_length. If the value of e is a floating-point number, then the maximum size is only Sys.max_array_length / 2.

let append: array('a) => array('a) => array('a);

append v1 v2 returns a fresh array containing the concatenation of the arrays v1 and v2.

let concat: list(array('a)) => array('a);

Same as append, but concatenates a list of arrays.

let sub: array('a) => int => int => array('a);

sub a pos len returns a fresh array of length len, containing the elements number pos to pos + len - 1 of array a.

  • raises Invalid_argument

    if pos and len do not designate a valid subarray of a; that is, if pos < 0, or len < 0, or pos + len > length a.

let copy: array('a) => array('a);

copy a returns a copy of a, that is, a fresh array containing the same elements as a.

let fill: array('a) => int => int => 'a => unit;

fill a pos len x modifies the array a in place, storing x in elements number pos to pos + len - 1.

let blit: array('a) => int => array('a) => int => int => unit;

blit src src_pos dst dst_pos len copies len elements from array src, starting at element number src_pos, to array dst, starting at element number dst_pos. It works correctly even if src and dst are the same array, and the source and destination chunks overlap.

  • raises Invalid_argument

    if src_pos and len do not designate a valid subarray of src, or if dst_pos and len do not designate a valid subarray of dst.

let to_list: array('a) => list('a);

to_list a returns the list of all the elements of a.

let of_list: list('a) => array('a);

of_list l returns a fresh array containing the elements of l.

  • raises Invalid_argument

    if the length of l is greater than Sys.max_array_length.

Iterators

let iter: ('a => unit) => array('a) => unit;

iter f a applies function f in turn to all the elements of a. It is equivalent to f a.(0); f a.(1); ...; f a.(length a - 1); ().

let iteri: (int => 'a => unit) => array('a) => unit;

Same as iter, but the function is applied to the index of the element as first argument, and the element itself as second argument.

let map: ('a => 'b) => array('a) => array('b);

map f a applies function f to all the elements of a, and builds an array with the results returned by f: [| f a.(0); f a.(1); ...; f a.(length a - 1) |].

let map_inplace: ('a => 'a) => array('a) => unit;

map_inplace f a applies function f to all elements of a, and updates their values in place.

  • since 5.1
let mapi: (int => 'a => 'b) => array('a) => array('b);

Same as map, but the function is applied to the index of the element as first argument, and the element itself as second argument.

let mapi_inplace: (int => 'a => 'a) => array('a) => unit;

Same as map_inplace, but the function is applied to the index of the element as first argument, and the element itself as second argument.

  • since 5.1
let fold_left: ('acc => 'a => 'acc) => 'acc => array('a) => 'acc;

fold_left f init a computes f (... (f (f init a.(0)) a.(1)) ...) a.(n-1), where n is the length of the array a.

let fold_left_map: ('acc => 'a => ('acc, 'b)) => 'acc => array('a) => ('acc, array('b));

fold_left_map is a combination of fold_left and map that threads an accumulator through calls to f.

  • since 4.13
let fold_right: ('a => 'acc => 'acc) => array('a) => 'acc => 'acc;

fold_right f a init computes f a.(0) (f a.(1) ( ... (f a.(n-1) init) ...)), where n is the length of the array a.

Iterators on two arrays

let iter2: ('a => 'b => unit) => array('a) => array('b) => unit;

iter2 f a b applies function f to all the elements of a and b.

  • since 4.03 (4.05 in ArrayLabels)
let map2: ('a => 'b => 'c) => array('a) => array('b) => array('c);

map2 f a b applies function f to all the elements of a and b, and builds an array with the results returned by f: [| f a.(0) b.(0); ...; f a.(length a - 1) b.(length b - 1)|].

  • since 4.03 (4.05 in ArrayLabels)

Array scanning

let for_all: ('a => bool) => array('a) => bool;

for_all f [|a1; ...; an|] checks if all elements of the array satisfy the predicate f. That is, it returns (f a1) && (f a2) && ... && (f an).

  • since 4.03
let exists: ('a => bool) => array('a) => bool;

exists f [|a1; ...; an|] checks if at least one element of the array satisfies the predicate f. That is, it returns (f a1) || (f a2) || ... || (f an).

  • since 4.03
let for_all2: ('a => 'b => bool) => array('a) => array('b) => bool;

Same as for_all, but for a two-argument predicate.

  • since 4.11
let exists2: ('a => 'b => bool) => array('a) => array('b) => bool;

Same as exists, but for a two-argument predicate.

  • since 4.11
let mem: 'a => array('a) => bool;

mem a set is true if and only if a is structurally equal to an element of l (i.e. there is an x in l such that compare a x = 0).

  • since 4.03
let memq: 'a => array('a) => bool;

Same as mem, but uses physical equality instead of structural equality to compare list elements.

  • since 4.03
let find_opt: ('a => bool) => array('a) => option('a);

find_opt f a returns the first element of the array a that satisfies the predicate f, or None if there is no value that satisfies f in the array a.

  • since 4.13
let find_index: ('a => bool) => array('a) => option(int);

find_index f a returns Some i, where i is the index of the first element of the array a that satisfies f x, if there is such an element.

It returns None if there is no such element.

  • since 5.1
let find_map: ('a => option('b)) => array('a) => option('b);

find_map f a applies f to the elements of a in order, and returns the first result of the form Some v, or None if none exist.

  • since 4.13
let find_mapi: (int => 'a => option('b)) => array('a) => option('b);

Same as find_map, but the predicate is applied to the index of the element as first argument (counting from 0), and the element itself as second argument.

  • since 5.1

Arrays of pairs

let split: array(('a, 'b)) => (array('a), array('b));

split [|(a1,b1); ...; (an,bn)|] is ([|a1; ...; an|], [|b1; ...; bn|]).

  • since 4.13
let combine: array('a) => array('b) => array(('a, 'b));

combine [|a1; ...; an|] [|b1; ...; bn|] is [|(a1,b1); ...; (an,bn)|]. Raise Invalid_argument if the two arrays have different lengths.

  • since 4.13

Sorting

let sort: ('a => 'a => int) => array('a) => unit;

Sort an array in increasing order according to a comparison function. The comparison function must return 0 if its arguments compare as equal, a positive integer if the first is greater, and a negative integer if the first is smaller (see below for a complete specification). For example, Stdlib.compare is a suitable comparison function. After calling sort, the array is sorted in place in increasing order. sort is guaranteed to run in constant heap space and (at most) logarithmic stack space.

The current implementation uses Heap Sort. It runs in constant stack space.

Specification of the comparison function: Let a be the array and cmp the comparison function. The following must be true for all x, y, z in a :

  • cmp x y > 0 if and only if cmp y x < 0
  • if cmp x y >= 0 and cmp y z >= 0 then cmp x z >= 0

When sort returns, a contains the same elements as before, reordered in such a way that for all i and j valid indices of a :

  • cmp a.(i) a.(j) >= 0 if and only if i >= j
let stable_sort: ('a => 'a => int) => array('a) => unit;

Same as sort, but the sorting algorithm is stable (i.e. elements that compare equal are kept in their original order) and not guaranteed to run in constant heap space.

The current implementation uses Merge Sort. It uses a temporary array of length n/2, where n is the length of the array. It is usually faster than the current implementation of sort.

let fast_sort: ('a => 'a => int) => array('a) => unit;

Same as sort or stable_sort, whichever is faster on typical input.

Arrays and Sequences

let to_seq: array('a) => Seq.t('a);

Iterate on the array, in increasing order. Modifications of the array during iteration will be reflected in the sequence.

  • since 4.07
let to_seqi: array('a) => Seq.t((int, 'a));

Iterate on the array, in increasing order, yielding indices along elements. Modifications of the array during iteration will be reflected in the sequence.

  • since 4.07
let of_seq: Seq.t('a) => array('a);

Create an array from the generator

  • since 4.07

Arrays and concurrency safety

Care must be taken when concurrently accessing arrays from multiple domains: accessing an array will never crash a program, but unsynchronized accesses might yield surprising (non-sequentially-consistent) results.

Atomicity

Every array operation that accesses more than one array element is not atomic. This includes iteration, scanning, sorting, splitting and combining arrays.

For example, consider the following program:

let size = 100_000_000
let a = Array.make size 1
let d1 = Domain.spawn (fun () ->
   Array.iteri (fun i x -> a.(i) <- x + 1) a
)
let d2 = Domain.spawn (fun () ->
  Array.iteri (fun i x -> a.(i) <- 2 * x + 1) a
)
let () = Domain.join d1; Domain.join d2

After executing this code, each field of the array a is either 2, 3, 4 or 5. If atomicity is required, then the user must implement their own synchronization (for example, using Mutex.t).

Data races

If two domains only access disjoint parts of the array, then the observed behaviour is the equivalent to some sequential interleaving of the operations from the two domains.

A data race is said to occur when two domains access the same array element without synchronization and at least one of the accesses is a write. In the absence of data races, the observed behaviour is equivalent to some sequential interleaving of the operations from different domains.

Whenever possible, data races should be avoided by using synchronization to mediate the accesses to the array elements.

Indeed, in the presence of data races, programs will not crash but the observed behaviour may not be equivalent to any sequential interleaving of operations from different domains. Nevertheless, even in the presence of data races, a read operation will return the value of some prior write to that location (with a few exceptions for float arrays).

Float arrays

Float arrays have two supplementary caveats in the presence of data races.

First, the blit operation might copy an array byte-by-byte. Data races between such a blit operation and another operation might produce surprising values due to tearing: partial writes interleaved with other operations can create float values that would not exist with a sequential execution.

For instance, at the end of

let zeros = Array.make size 0.
let max_floats = Array.make size Float.max_float
let res = Array.copy zeros
let d1 = Domain.spawn (fun () -> Array.blit zeros 0 res 0 size)
let d2 = Domain.spawn (fun () -> Array.blit max_floats 0 res 0 size)
let () = Domain.join d1; Domain.join d2

the res array might contain values that are neither 0. nor max_float.

Second, on 32-bit architectures, getting or setting a field involves two separate memory accesses. In the presence of data races, the user may observe tearing on any operation.