Operations on immutable arrays. This module mirrors the API of Array, but omits functions that assume mutability; in addition to obviously mutating functions, it omits copy along with the functions make, create_float, and make_matrix that produce all-constant arrays. The exception is the sorting functions, which are given a copying API to replace the in-place one.
init n f returns a fresh immutable 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 to the integers 0 to n-1.
Same as append, but concatenates a list of immutable arrays.
Sourceval sub : 'a iarray->pos:int ->len:int ->'a iarray
sub a ~pos ~len returns a fresh immutable array of length len, containing the elements number pos to pos + len - 1 of immutable array a. This creates a copy of the selected portion of the immutable array.
eq [|a1; ...; an|] [|b1; ..; bm|] holds when the two input immutable arrays have the same length, and for each pair of elements ai, bi at the same position we have eq ai bi.
Sourceval compare : ('a->'a-> int)->'a iarray->'a iarray-> int
Provided the function cmp defines a preorder on elements, compare cmp a b compares first a and b by their length, and then, if equal, by their elements according to the lexicographic preorder.
For more details on comparison functions, see Iarray.sort.
map f a applies function f to all the elements of a, and builds an immutable array with the results returned by f: [| f (get a 0); f (get a 1); ...; f (get a (length a - 1)) |].
map2 f a b applies function f to all the elements of a and b, and builds an immutable array with the results returned by f: [| f (get a 0) (get b 0); ...; f (get a (length a - 1)) (get b (length b - 1))|].
for_all f [|a1; ...; an|] checks if all elements of the immutable array satisfy the predicate f. That is, it returns (f a1) && (f a2) && ... && (f an).
exists f [|a1; ...; an|] checks if at least one element of the immutable array satisfies the predicate f. That is, it returns (f a1) || (f a2) || ... || (f an).
find_opt f a returns the first element of the immutable array a that satisfies the predicate f, or None if there is no value that satisfies f in the array a.
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.
Sort an immutable 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. The result of calling sort is a fresh immutable array containing the same elements as the original sorted in increasing order. Other than this fresh array, 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 immutable 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
The result of sort, which we'll call a', contains the same elements as a, reordered in such a way that for all i and j valid indices of a (or equivalently, of a'):
cmp (get a' i) (get a' j) >= 0 if and only if i >= j
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 immutable array. It is usually faster than the current implementation of sort.