T - Type of the elementsU - Type of the NdArray with this sequencepublic final class FastElementSequence<T,U extends NdArray<T>> extends Object implements NdArraySequence<U>
NdArray instance when iterating its elements| Constructor and Description |
|---|
FastElementSequence(AbstractNdArray<T,U> ndArray,
int dimensionIdx,
U element,
DataBufferWindow<?> elementWindow) |
| Modifier and Type | Method and Description |
|---|---|
NdArraySequence<U> |
asSlices()
Returns each element as a new slice.
|
void |
forEachIndexed(BiConsumer<long[],U> consumer)
Visit each elements of this iteration and their respective coordinates.
|
Iterator<U> |
iterator() |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitforEach, spliteratorpublic FastElementSequence(AbstractNdArray<T,U> ndArray, int dimensionIdx, U element, DataBufferWindow<?> elementWindow)
public void forEachIndexed(BiConsumer<long[],U> consumer)
NdArraySequenceImportant: the consumer method should not keep a reference to the coordinates as they might be mutable and reused during the iteration to improve performance.
forEachIndexed in interface NdArraySequence<U extends NdArray<T>>consumer - method to invoke for each elementspublic NdArraySequence<U> asSlices()
NdArraySequenceUnlike 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
asSlices in interface NdArraySequence<U extends NdArray<T>>DataBufferWindowCopyright © 2015–2020. All rights reserved.