GenArrayIter.IterThe Iter module is an extention of the BigArray.Genarray module to complete missing functionalities : iteration, mapping and folding over Genarrays. To make these operations possible, an increment/decrement function is needed to iterate over multi-dimensional indexes of the array. There are many ways to iterate over multi-dimension spaces, but here two versions are provided : incr and incr_last (with their corresponding decr and decr_last versions).
The module features the following functions :
iter f a applies the function f in turn to each element of the array a. It is equivalent to f a.(0); f a.(1); ... ; f a.(length a - 1); ()
Same as iter except that the function f is applied to the index of the array as first argument, and to the element itself as second argument. iteri f a applies the function f in turn to each element of the array a and the index i : f i a.(i) for all possibles values of the index i.
NB: the index is incremented using the function Iter.incr
val map :
('a -> 'b) ->
('b, 'k2) Stdlib.Bigarray.kind ->
('a, 'k1, 'layout) Stdlib.Bigarray.Genarray.t ->
('b, 'k2, 'layout) Stdlib.Bigarray.Genarray.tmap f k a applies the map function f in turn to each element of the array a of type 'a and outputs another array b, elements of which have type 'b.
Same as map but modifies the array inplace instead of creating a new one.
val mapi :
(int Stdlib.Array.t -> 'a -> 'b) ->
('b, 'k2) Stdlib.Bigarray.kind ->
('a, 'k1, 'layout) Stdlib.Bigarray.Genarray.t ->
('b, 'k2, 'layout) Stdlib.Bigarray.Genarray.tSame as map except that the function f is applied to the index of the array as the first argument, and to the element itself as second argument. mapi f k a applies the function f in turn to each element of the array a and the index i : f i a.(i) for all possibles values of the index i. The output is a new array.
NB : the function incr is used to iterate over all elements of the array a
val mapi_inplace :
(int Stdlib.Array.t -> 'a -> 'a) ->
('a, 'b, 'c) Stdlib.Bigarray.Genarray.t ->
unitA combination of mapi and map_inplace : mapi_inplace f a applies f in turn to each index i (as first argument) and element of the array a (as second argument).
NB: the function incr is used to iterate over all elements of the array a
val fold_left :
('acc -> 'a -> 'acc) ->
'acc ->
('a, 'b, 'layout) Stdlib.Bigarray.Genarray.t ->
'accfold_left f init a computes f (f (... (f init a.(0)) ... ) a.(n-1)) where n is the length of the array a.
NB : the function incr is used to iterate over all elements of the array a
val fold_right :
('a -> 'acc -> 'acc) ->
('a, 'b, 'layout) Stdlib.Bigarray.Genarray.t ->
'acc ->
'accfold_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.
NB : the function decr is used to iterate over all elements of the array a
Given an index i(i_0,i_1, ..., i_{n-1}) of dimension d(d_0, d_1, ..., d_{n-1}) such that \forall k \in [0;n-1] with 0 \leqslant i_k < d_k, the function increments the index i according to the following recursive algorithm:
i_0 < n_0 - 1 then i_0 = i_0 + 1i_0 = 0 and i_1 is incremented by one according the same alogrithm as i_0.So for an index i of dimensions d(2,2,3) in the space \{0,1\}\times\{0,1\}\times\{0,1,2\}, applying incr to i=(0,0,0) several time will give :
i=(0,0,0)i=(1,0,0)i=(0,1,0)i=(1,1,0)i=(0,0,1)i=(1,0,1)i=(0,1,1)i=(1,1,1)i=(0,0,2)i=(1,0,2)i=(0,1,2)i=(1,1,2)Same as incr but instead of incrementing the first dimension (i_0) of the index i(i_0, ..., i_{n-1}), increments the last dimension first (i_{n-1})