increment - increment a multi-dimensional index
idx.increment(shp)
The increment() operator treats the 1D vector
idx as a multi-dimensional index into an array of shape
shp. It increments the index to the "next" position
in row-major order (the rightmost dimension increments fastest).
If a dimension exceeds its bound in shp, it wraps to zero and the dimension to its left is incremented, similar to an odometer.
The increment operator is especially useful
when used in combination with the "flattened" indexing
operator #[ ] and the stride operator.
A new PackInt vector containing the
incremented index, or nil if the index has rolled
over (i.e., it was already at the last possible position).
ArgCheck if the number of arguments is
incorrect.
ShapeCheck if idx or
shp are not 1D vectors.
RangeCheck
if the length of idx does not match the length of shp.
arr = "abcdef".reshape(2,3)
arr
abc
def
shp = arr.shape()
idx = (0).reshape(arr.rank()) // Start with [0,0]
strd = arr.stride() // Needed to compute offset
do {
var n = idx.inner(`+,`*,strd); // Sum of products of idx and stride
"arr[", idx, "] == ", arr#[n], '\n'; // Note: flattened index operator
idx = idx.increment(shp); // Increment to next index in shape
} while (idx != nil)
arr[0 0] == a
arr[0 1] == b
arr[0 2] == c
arr[1 0] == d
arr[1 1] == e
arr[1 2] == f