FastElementSequence

public final class FastElementSequence

A sequence recycling the same NdArray instance when iterating its elements

Public Constructors

FastElementSequence(AbstractNdArray<T, U> ndArray, int dimensionIdx, U element, DataBufferWindow<?> elementWindow)

Public Methods

NdArraySequence<U>
asSlices()
Returns each element as a new slice.
void
forEachIndexed(BiConsumer<long[], U> consumer)
Iterator<U>

Inherited Methods

boolean
equals(Object arg0)
final Class<?>
getClass()
int
hashCode()
final void
notify()
final void
notifyAll()
String
toString()
final void
wait(long arg0, int arg1)
final void
wait(long arg0)
final void
wait()
org.tensorflow.ndarray.NdArraySequence
abstract NdArraySequence<U extends NdArray<T>>
asSlices()
Returns each element as a new slice.
abstract void
forEachIndexed(BiConsumer<long[], U extends NdArray<T>> consumer)
Visit each elements of this iteration and their respective coordinates.
abstract void
forEach(Consumer<? super T> arg0)
abstract Iterator<U extends NdArray<T>>
iterator()
abstract Spliterator<U extends NdArray<T>>
spliterator()

Public Constructors

public FastElementSequence (AbstractNdArray<T, U> ndArray, int dimensionIdx, U element, DataBufferWindow<?> elementWindow)

Public Methods

public NdArraySequence<U> 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 void forEachIndexed (BiConsumer<long[], U> consumer)

public Iterator<U> iterator ()