jon.recoil.org

Module Stdlib_stable.IarrayLabelsSource

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.

Sourcetype (+'a : any mod separable) t = 'a iarray

An alias for the type of immutable arrays.

Sourceval length : ('a : any mod separable). 'a iarray @ local -> int @@ portable

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

Sourceval get : ('a : any mod separable). 'a iarray -> int -> 'a @@ portable

get a n returns the element number n of immutable 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.

Sourceval (.:()) : ('a : any mod separable). 'a iarray -> int -> 'a @@ portable

A synonym for get.

Sourceval init : ('a : value_or_null mod separable). int -> f:(int -> 'a) @ local -> 'a iarray @@ portable

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.

  • 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.

Sourceval init_local : ('a : value_or_null mod separable). int -> f:(int -> 'a @ local) @ local -> 'a iarray @ local @@ portable

The locally-allocating version of init.

Sourceval append : ('a : value_or_null mod separable). 'a iarray -> 'a iarray -> 'a iarray @@ portable

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

Sourceval append_local : ('a : value_or_null mod separable). 'a iarray @ local -> 'a iarray @ local -> 'a iarray @ local @@ portable

The locally-allocating version of append.

Sourceval concat : ('a : value_or_null mod separable). 'a iarray list -> 'a iarray @@ portable

Same as append, but concatenates a list of immutable arrays.

Sourceval concat_local : ('a : value_or_null mod separable). 'a iarray list @ local -> 'a iarray @ local @@ portable

The locally-allocating version of concat.

Sourceval sub : ('a : value_or_null mod separable). 'a iarray -> pos:int -> len:int -> 'a iarray @@ portable

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.

  • 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.

Sourceval sub_local : ('a : value_or_null mod separable). 'a iarray @ local -> pos:int -> len:int -> 'a iarray @ local @@ portable

The locally-allocating version of sub.

Sourceval to_list : ('a : value_or_null mod separable). 'a iarray -> 'a list @@ portable

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

Sourceval to_list_local : ('a : value_or_null mod separable). 'a iarray @ local -> 'a list @ local @@ portable

The locally-allocating version of .

Sourceval of_list : ('a : value_or_null mod separable). 'a list -> 'a iarray @@ portable

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

  • raises Invalid_argument

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

Sourceval of_list_local : ('a : value_or_null mod separable). 'a list @ local -> 'a iarray @ local @@ portable

The locally-allocating version of of_list.

Converting to and from mutable arrays

Sourceval to_array : ('a : value_or_null mod separable). 'a iarray -> 'a array @@ portable

to_array a returns a mutable copy of the immutable array a; that is, a fresh (mutable) array containing the same elements as a

Sourceval of_array : ('a : value_or_null mod separable). 'a array -> 'a iarray @@ portable

of_array ma returns an immutable copy of the mutable array ma; that is, a fresh immutable array containing the same elements as ma

Iterators

Sourceval iter : ('a : value_or_null mod separable). f:('a -> unit) @ local -> 'a iarray -> unit @@ portable

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); ().

Sourceval iter_local : ('a : value_or_null mod separable). f:('a @ local -> unit) @ local -> 'a iarray @ local -> unit @@ portable

The locally-scoped version of iter.

Sourceval iteri : ('a : value_or_null mod separable). f:(int -> ('a -> unit) @ local) @ local -> 'a iarray -> unit @@ portable

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

Sourceval iteri_local : ('a : value_or_null mod separable). f: (int -> ('a @ local -> unit) @ local) @ local -> 'a iarray @ local -> unit @@ portable

The locally-scoped version of iteri.

Sourceval map : ('a : value_or_null mod separable) ('b : value_or_null mod separable). f: ('a -> 'b) @ local -> 'a iarray -> 'b iarray @@ portable

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

Sourceval map_local : ('a : value_or_null mod separable) ('b : value_or_null mod separable). f: ('a @ local -> 'b @ local) @ local -> 'a iarray @ local -> 'b iarray @ local @@ portable

The locally-scoped and locally-allocating version of map.

Sourceval map_local_input : ('a : value_or_null mod separable) ('b : value_or_null mod separable). f: ('a @ local -> 'b) @ local -> 'a iarray @ local -> 'b iarray @@ portable

The locally-constrained but globally-allocating version of map.

Sourceval map_local_output : ('a : value_or_null mod separable) ('b : value_or_null mod separable). f: ('a -> 'b @ local) @ local -> 'a iarray -> 'b iarray @ local @@ portable

The locally-allocating but global-input version of map.

Sourceval mapi : ('a : value_or_null mod separable) ('b : value_or_null mod separable). f: (int -> ('a -> 'b) @ local) @ local -> 'a iarray -> 'b iarray @@ portable

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

Sourceval mapi_local : ('a : value_or_null mod separable) ('b : value_or_null mod separable). f: (int -> ('a @ local -> 'b @ local) @ local) @ local -> 'a iarray @ local -> 'b iarray @ local @@ portable

The locally-scoped and locally-allocating version of mapi.

Sourceval mapi_local_input : ('a : value_or_null mod separable) ('b : value_or_null mod separable). f: (int -> ('a @ local -> 'b) @ local) @ local -> 'a iarray @ local -> 'b iarray @@ portable

The locally-constrained but globally-allocating version of mapi.

Sourceval mapi_local_output : ('a : value_or_null mod separable) ('b : value_or_null mod separable). f: (int -> ('a -> 'b @ local) @ local) @ local -> 'a iarray -> 'b iarray @ local @@ portable

The locally-allocating but global-input version of mapi.

Sourceval fold_left : ('a : value_or_null) ('b : value_or_null mod separable). f: ('a -> ('b -> 'a) @ local) @ local -> init:'a -> 'b iarray -> 'a @@ portable

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

Sourceval fold_left_local : ('a : value_or_null) ('b : value_or_null mod separable). f: ('a @ local -> 'b @ local -> 'a @ local) @ local -> init:'a @ local -> 'b iarray @ local -> 'a @ local @@ portable

The locally-constrained and locally-allocating version of fold_left.

WARNING: This function consumes O(n) extra stack space, as every intermediate accumulator will be left on the local stack!

Sourceval fold_left_local_input : ('a : value_or_null) ('b : value_or_null mod separable). f: ('a -> ('b @ local -> 'a) @ local) @ local -> init:'a -> 'b iarray @ local -> 'a @@ portable

The locally-constrained but globally-allocating version of fold_left.

Sourceval fold_left_local_output : ('a : value_or_null) ('b : value_or_null mod separable). f: ('a @ local -> 'b -> 'a @ local) @ local -> init:'a @ local -> 'b iarray -> 'a @ local @@ portable

The locally-allocating but global-input version of fold_left.

WARNING: This function consumes O(n) extra stack space, as every intermediate accumulator will be left on the local stack!

Sourceval fold_left_map : ('a : value_or_null) ('b : value_or_null mod separable) ('c : value_or_null mod separable). f: ('a -> ('b -> 'a * 'c) @ local) @ local -> init:'a -> 'b iarray -> 'a * 'c iarray @@ portable

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

Sourceval fold_left_map_local : ('a : value_or_null) ('b : value_or_null mod separable) ('c : value_or_null mod separable). f: ('a @ local -> 'b @ local -> ('a * 'c) @ local) @ local -> init:'a @ local -> 'b iarray @ local -> ('a * 'c iarray) @ local @@ portable

The locally-constrained and locally-allocating version of fold_left.

WARNING: This function consumes O(n) extra stack space, as every intermediate accumulator will be left on the local stack!

Sourceval fold_left_map_local_input : ('a : value_or_null) ('b : value_or_null mod separable) ('c : value_or_null mod separable). f: ('a -> ('b @ local -> 'a * 'c) @ local) @ local -> init:'a -> 'b iarray @ local -> 'a * 'c iarray @@ portable

The locally-constrained but globally-allocating version of fold_left.

Sourceval fold_left_map_local_output : ('a : value_or_null) ('b : value_or_null mod separable) ('c : value_or_null mod separable). f: ('a @ local -> 'b -> ('a * 'c) @ local) @ local -> init:'a @ local -> 'b iarray -> ('a * 'c iarray) @ local @@ portable

The locally-allocating but global-input version of fold_left.

WARNING: This function consumes O(n) extra stack space, as every intermediate accumulator will be left on the local stack!

Sourceval fold_right : ('a : value_or_null) ('b : value_or_null mod separable). f: ('b -> ('a -> 'a) @ local) @ local -> 'b iarray -> init:'a -> 'a @@ portable

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

Sourceval fold_right_local : ('a : value_or_null) ('b : value_or_null mod separable). f: ('b @ local -> 'a @ local -> 'a @ local) @ local -> 'b iarray @ local -> init:'a @ local -> 'a @ local @@ portable

The locally-constrained and locally-allocating version of fold_right.

WARNING: This function consumes O(n) extra stack space, as every intermediate accumulator will be left on the local stack!

Sourceval fold_right_local_input : ('a : value_or_null) ('b : value_or_null mod separable). f: ('b @ local -> 'a -> 'a) @ local -> 'b iarray @ local -> init:'a -> 'a @@ portable

The locally-constrained but globally-allocating version of fold_right.

Sourceval fold_right_local_output : ('a : value_or_null) ('b : value_or_null mod separable). f: ('b -> ('a @ local -> 'a @ local) @ local) @ local -> 'b iarray -> init:'a @ local -> 'a @ local @@ portable

The locally-allocating but global-input version of fold_right.

WARNING: This function consumes O(n) extra stack space, as every intermediate accumulator will be left on the local stack!

Iterators on two arrays

Sourceval iter2 : ('a : value_or_null mod separable) ('b : value_or_null mod separable). f: ('a -> ('b -> unit) @ local) @ local -> 'a iarray -> 'b iarray -> unit @@ portable

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

Sourceval iter2_local : ('a : value_or_null mod separable) ('b : value_or_null mod separable). f: ('a @ local -> 'b @ local -> unit) @ local -> 'a iarray @ local -> 'b iarray @ local -> unit @@ portable

The locally-scoped version of iter2.

Sourceval iter2_local_first : ('a : value_or_null mod separable) ('b : value_or_null mod separable). f: ('a @ local -> 'b -> unit) @ local -> 'a iarray @ local -> 'b iarray -> unit @@ portable

The first-biased partly-locally-scoped version of iter2.

Sourceval iter2_local_second : ('a : value_or_null mod separable) ('b : value_or_null mod separable). f: ('a -> ('b @ local -> unit) @ local) @ local -> 'a iarray -> 'b iarray @ local -> unit @@ portable

The second-biased partly-locally-scoped version of iter2.

Sourceval map2 : ('a : value_or_null mod separable) ('b : value_or_null mod separable) ('c : value_or_null mod separable). f: ('a -> ('b -> 'c) @ local) @ local -> 'a iarray -> 'b iarray -> 'c iarray @@ portable

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 a.:(0) b.:(0); ...; f a.:(length a - 1) b.:(length b - 1)|].

Sourceval map2_local : ('a : value_or_null mod separable) ('b : value_or_null mod separable) ('c : value_or_null mod separable). f: ('a @ local -> 'b @ local -> 'c @ local) @ local -> 'a iarray @ local -> 'b iarray @ local -> 'c iarray @ local @@ portable

The locally-scoped and locally-allocating version of map2.

Sourceval map2_local_inputs : ('a : value_or_null mod separable) ('b : value_or_null mod separable) ('c : value_or_null mod separable). f: ('a @ local -> 'b @ local -> 'c) @ local -> 'a iarray @ local -> 'b iarray @ local -> 'c iarray @@ portable

The locally-scoped but globally-allocating version of map2.

Sourceval map2_local_output : ('a : value_or_null mod separable) ('b : value_or_null mod separable) ('c : value_or_null mod separable). f: ('a -> ('b -> 'c @ local) @ local) @ local -> 'a iarray -> 'b iarray -> 'c iarray @ local @@ portable

The locally-allocating but global-inputs version of map2.

Sourceval map2_local_first_input : ('a : value_or_null mod separable) ('b : value_or_null mod separable) ('c : value_or_null mod separable). f: ('a @ local -> 'b -> 'c) @ local -> 'a iarray @ local -> 'b iarray -> 'c iarray @@ portable

The first-biased partly-locally-scoped but globally-allocating version of map2.

Sourceval map2_local_second_input : ('a : value_or_null mod separable) ('b : value_or_null mod separable) ('c : value_or_null mod separable). f: ('a -> ('b @ local -> 'c) @ local) @ local -> 'a iarray -> 'b iarray @ local -> 'c iarray @@ portable

The second-biased partly-locally-scoped but globally-allocating version of map2.

Sourceval map2_local_first_input_and_output : ('a : value_or_null mod separable) ('b : value_or_null mod separable) ('c : value_or_null mod separable). f: ('a @ local -> 'b -> 'c @ local) @ local -> 'a iarray @ local -> 'b iarray -> 'c iarray @ local @@ portable

The locally-allocating and first-biased partly-locally-scoped version of map2.

Sourceval map2_local_second_input_and_output : ('a : value_or_null mod separable) ('b : value_or_null mod separable) ('c : value_or_null mod separable). f: ('a -> ('b @ local -> 'c @ local) @ local) @ local -> 'a iarray -> 'b iarray @ local -> 'c iarray @ local @@ portable

The locally-allocating and second-biased partly-locally-scoped version of map2.

Array scanning

Sourceval for_all : ('a : value_or_null mod separable). f:('a -> bool) @ local -> 'a iarray -> bool @@ portable

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).

Sourceval for_all_local : ('a : value_or_null mod separable). f:('a @ local -> bool) @ local -> 'a iarray @ local -> bool @@ portable

The locally-scoped version of for_all.

Sourceval exists : ('a : value_or_null mod separable). f:('a -> bool) @ local -> 'a iarray -> bool @@ portable

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).

Sourceval exists_local : ('a : value_or_null mod separable). f:('a @ local -> bool) @ local -> 'a iarray @ local -> bool @@ portable

The locally-scoped version of exists.

Sourceval for_all2 : ('a : value_or_null mod separable) ('b : value_or_null mod separable). f: ('a -> ('b -> bool) @ local) @ local -> 'a iarray -> 'b iarray -> bool @@ portable

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

Sourceval for_all2_local : ('a : value_or_null mod separable) ('b : value_or_null mod separable). f: ('a @ local -> 'b @ local -> bool) @ local -> 'a iarray @ local -> 'b iarray @ local -> bool @@ portable

The locally-scoped version of for_all2.

Sourceval for_all2_local_first : ('a : value_or_null mod separable) ('b : value_or_null mod separable). f: ('a @ local -> 'b -> bool) @ local -> 'a iarray @ local -> 'b iarray -> bool @@ portable

The first-biased partly-locally-scoped version of for_all2.

Sourceval for_all2_local_second : ('a : value_or_null mod separable) ('b : value_or_null mod separable). f: ('a -> ('b @ local -> bool) @ local) @ local -> 'a iarray -> 'b iarray @ local -> bool @@ portable

The second-biased partly-locally-scoped version of for_all2.

Sourceval exists2 : ('a : value_or_null mod separable) ('b : value_or_null mod separable). f: ('a -> ('b -> bool) @ local) @ local -> 'a iarray -> 'b iarray -> bool @@ portable

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

Sourceval exists2_local : ('a : value_or_null mod separable) ('b : value_or_null mod separable). f: ('a @ local -> 'b @ local -> bool) @ local -> 'a iarray @ local -> 'b iarray @ local -> bool @@ portable

The locally-scoped version of exists2.

Sourceval exists2_local_first : ('a : value_or_null mod separable) ('b : value_or_null mod separable). f: ('a @ local -> 'b -> bool) @ local -> 'a iarray @ local -> 'b iarray -> bool @@ portable

The first-biased partly-locally-scoped version of exists2.

Sourceval exists2_local_second : ('a : value_or_null mod separable) ('b : value_or_null mod separable). f: ('a -> ('b @ local -> bool) @ local) @ local -> 'a iarray -> 'b iarray @ local -> bool @@ portable

The second-biased partly-locally-scoped version of exists2.

Sourceval mem : ('a : value_or_null mod separable). 'a @ local -> set:'a iarray @ local -> bool @@ portable

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).

Sourceval memq : ('a : value_or_null mod separable). 'a @ local -> set:'a iarray @ local -> bool @@ portable

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

Sourceval find_opt : ('a : value_or_null mod separable). f:('a -> bool) @ local -> 'a iarray -> 'a option @@ portable

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.

Sourceval find_opt_local : ('a : value_or_null mod separable). f:('a @ local -> bool) @ local -> 'a iarray @ local -> 'a option @ local @@ portable

The locally-constrained and locally-allocating version of .

Sourceval find_map : ('a : value_or_null mod separable) ('b : value_or_null). f: ('a -> 'b option) @ local -> 'a iarray -> 'b option @@ portable

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.

Sourceval find_map_local : ('a : value_or_null mod separable) ('b : value_or_null). f: ('a @ local -> 'b option @ local) @ local -> 'a iarray @ local -> 'b option @ local @@ portable

The locally-constrained and locally-allocating version of find_map.

Sourceval find_map_local_input : ('a : value_or_null mod separable) ('b : value_or_null). f: ('a @ local -> 'b option) @ local -> 'a iarray @ local -> 'b option @@ portable

The locally-constrained but globally-allocating version of find_map.

Sourceval find_map_local_output : ('a : value_or_null mod separable) ('b : value_or_null). f: ('a -> 'b option @ local) @ local -> 'a iarray -> 'b option @ local @@ portable

The locally-allocating but global-input version of find_map.

Arrays of pairs

Sourceval split : ('a : value_or_null mod separable) ('b : value_or_null mod separable). ('a * 'b) iarray -> 'a iarray * 'b iarray @@ portable

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

Sourceval split_local : ('a : value_or_null mod separable) ('b : value_or_null mod separable). ('a * 'b) iarray @ local -> ('a iarray * 'b iarray) @ local @@ portable

The locally-allocating version of split.

Sourceval combine : ('a : value_or_null mod separable) ('b : value_or_null mod separable). 'a iarray -> 'b iarray -> ('a * 'b) iarray @@ portable

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

Sourceval combine_local : ('a : value_or_null mod separable) ('b : value_or_null mod separable). 'a iarray @ local -> 'b iarray @ local -> ('a * 'b) iarray @ local @@ portable

The locally-allocating version of combine.

Sorting

Sourceval sort : ('a : value_or_null mod separable). cmp:('a -> 'a -> int) -> 'a iarray -> 'a iarray @@ portable

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 a'.:(i) a'.:(j) >= 0 if and only if i >= j
Sourceval stable_sort : ('a : value_or_null mod separable). cmp:('a -> 'a -> int) -> 'a iarray -> 'a iarray @@ portable

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.

Sourceval fast_sort : ('a : value_or_null mod separable). cmp:('a -> 'a -> int) -> 'a iarray -> 'a iarray @@ portable

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

Iterators

Sourceval to_seq : ('a : value_or_null mod separable). 'a iarray -> 'a Seq.t @@ portable

Iterate on the immutable array, in increasing order.

Sourceval to_seqi : ('a : value_or_null mod separable). 'a iarray -> (int * 'a) Seq.t @@ portable

Iterate on the immutable array, in increasing order, yielding indices along elements.

Sourceval of_seq : ('a : value_or_null mod separable). 'a Seq.t -> 'a iarray @@ portable

Create an immutable array from the generator