Module Stdlib_stable.IarraySource
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.
An alias for the type of immutable arrays.
Return the length (number of elements) of the given immutable array.
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.
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.
val append :
('a : value_or_null mod separable). 'a iarray ->
'a iarray ->
'a iarray @@ portableappend v1 v2 returns a fresh immutable array containing the concatenation of the immutable arrays v1 and v2.
Same as append, but concatenates a list of immutable arrays.
val sub :
('a : value_or_null mod separable). 'a iarray ->
pos:int ->
len:int ->
'a iarray @@ portablesub 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.
to_list a returns the list of all the elements of a.
of_list l returns a fresh immutable array containing the elements of l.
Converting to and from mutable arrays
to_array a returns a mutable copy of the immutable array a; that is, a fresh (mutable) array containing the same elements as a
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
val iter :
('a : value_or_null mod separable). ('a -> unit) @ local ->
'a iarray ->
unit @@ portableiter f a applies function f in turn to all the elements of a. It is equivalent to f (get a 0); f (get a 1); ...; f (get a (length a - 1)); ().
val iteri :
('a : value_or_null mod separable). (int -> ('a -> unit) @ local) @ local ->
'a iarray ->
unit @@ portableSame as iter, but the function is applied to the index of the element as first argument, and the element itself as second argument.
val map :
('a : value_or_null mod separable) ('b : value_or_null mod separable).
('a ->
'b) @ local ->
'a iarray ->
'b iarray @@ portablemap 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)) |].
val mapi :
('a : value_or_null mod separable) ('b : value_or_null mod separable).
(int ->
('a ->
'b) @ local) @ local ->
'a iarray ->
'b iarray @@ portableSame as map, but the function is applied to the index of the element as first argument, and the element itself as second argument.
val fold_left :
('a : value_or_null) ('b : value_or_null mod separable). ('a ->
('b ->
'a) @ local) @ local ->
'a ->
'b iarray ->
'a @@ portablefold_left f init a computes f (... (f (f init (get a 0)) (get a 1)) ...) (get a n-1), where n is the length of the immutable array a.
val fold_left_map :
('a : value_or_null) ('b : value_or_null mod separable) ('c : value_or_null mod separable).
('a ->
('b ->
'a * 'c) @ local) @ local ->
'a ->
'b iarray ->
'a * 'c iarray @@ portableval fold_right :
('a : value_or_null) ('b : value_or_null mod separable). ('b ->
('a ->
'a) @ local) @ local ->
'b iarray ->
'a ->
'a @@ portablefold_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.
Iterators on two arrays
val iter2 :
('a : value_or_null mod separable) ('b : value_or_null mod separable).
('a ->
('b ->
unit) @ local) @ local ->
'a iarray ->
'b iarray ->
unit @@ portableiter2 f a b applies function f to all the elements of a and b.
val map2 :
('a : value_or_null mod separable) ('b : value_or_null mod separable) ('c : value_or_null mod separable).
('a ->
('b ->
'c) @ local) @ local ->
'a iarray ->
'b iarray ->
'c iarray @@ portablemap2 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))|].
Array scanning
val for_all :
('a : value_or_null mod separable). ('a -> bool) @ local ->
'a iarray ->
bool @@ portablefor_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).
val exists :
('a : value_or_null mod separable). ('a -> bool) @ local ->
'a iarray ->
bool @@ portableexists 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).
val for_all2 :
('a : value_or_null mod separable) ('b : value_or_null mod separable).
('a ->
('b ->
bool) @ local) @ local ->
'a iarray ->
'b iarray ->
bool @@ portableSame as for_all, but for a two-argument predicate.
val exists2 :
('a : value_or_null mod separable) ('b : value_or_null mod separable).
('a ->
('b ->
bool) @ local) @ local ->
'a iarray ->
'b iarray ->
bool @@ portableSame as exists, but for a two-argument predicate.
val mem :
('a : value_or_null mod separable). 'a @ local ->
'a iarray @ local ->
bool @@ portablemem 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).
val memq :
('a : value_or_null mod separable). 'a @ local ->
'a iarray @ local ->
bool @@ portableSame as mem, but uses physical equality instead of structural equality to compare list elements.
val find_opt :
('a : value_or_null mod separable). ('a -> bool) @ local ->
'a iarray ->
'a option @@ portablefind_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.
val find_map :
('a : value_or_null mod separable) ('b : value_or_null). ('a -> 'b option) @ local ->
'a iarray ->
'b option @@ portablefind_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.
Arrays of pairs
val split :
('a : value_or_null mod separable) ('b : value_or_null mod separable).
('a * 'b) iarray ->
'a iarray * 'b iarray @@ portablesplit [:(a1,b1); ...; (an,bn):] is ([:a1; ...; an:], [:b1; ...; bn:]).
val combine :
('a : value_or_null mod separable) ('b : value_or_null mod separable).
'a iarray ->
'b iarray ->
('a * 'b) iarray @@ portablecombine [:a1; ...; an:] [:b1; ...; bn:] is [:(a1,b1); ...; (an,bn):]. Raise Invalid_argument if the two immutable iarrays have different lengths.
Sorting
val sort :
('a : value_or_null mod separable). ('a -> 'a -> int) ->
'a iarray ->
'a iarray @@ portableSort 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 ifcmp y x< 0- if
cmp x y>= 0 andcmp y z>= 0 thencmp 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
val stable_sort :
('a : value_or_null mod separable). ('a -> 'a -> int) ->
'a iarray ->
'a iarray @@ portableSame 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.
val fast_sort :
('a : value_or_null mod separable). ('a -> 'a -> int) ->
'a iarray ->
'a iarray @@ portableSame as sort or stable_sort, whichever is faster on typical input.
Iterators
Iterate on the immutable array, in increasing order.
Iterate on the immutable array, in increasing order, yielding indices along elements.