NdArraySequence

public interface NdArraySequence
Known Indirect Subclasses

A sequence of elements of an N-dimensional array.

An NdArraySequence is used to traverse an NdArray in a given dimension and visit each of its elements. For example, given a n x m matrix on the [x, y] axes, elements are iterated in the following order:

x 0 y 0 , x 0 y 1 , ..., x 0 y m-1 , x 1 y 0 , x 1 y 1 , ..., x n-1 y m-1

Public Methods

abstract NdArraySequence <T>
asSlices ()
Returns each element as a new slice.
abstract void
forEachIndexed (BiConsumer<long[], T> consumer)
Visit each elements of this iteration and their respective coordinates.

Inherited Methods

Public Methods

public abstract NdArraySequence <T> asSlices ()

Returns each element as a new slice.

Unlike conventional Java collections, elements of a NdArraySequence are transient, i.e. new NdArray instances are allocated for each iteration. To improve performance, the same instance can be recycled to view all elements of this sequence, using a DataBufferWindow .

In some cases though, it might be preferable to disable such optimizations to ensure that each element returned is a new slice of the original array. For example, if one or more elements visited must live beyond the scope of the sequence iteration, asSlices() makes sure that all elements returned by the sequence are unique instances.

final List<IntNdArray> vectors = new ArrayList<>();
     IntNdArray matrix = NdArrays.ofInts(Shape.of(6, 6));
     ndArray.elements(0).forEach(e -> vectors::add);  // Not safe, as `e` might always be the same recycled instance
     ndArray.elements(0).asSlices().forEach(e -> vectors::add);  // Safe, each `e` is a distinct NdArray instance
 

Returns
  • a sequence that returns each elements iterated as a new slice

public abstract void forEachIndexed (BiConsumer<long[], T> consumer)

Visit each elements of this iteration and their respective coordinates.

Important: the consumer method should not keep a reference to the coordinates as they might be mutable and reused during the iteration to improve performance.

Parameters
consumer method to invoke for each elements