Package ai.djl.ndarray
Class NDList
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractList<E>
-
- java.util.ArrayList<NDArray>
-
- ai.djl.ndarray.NDList
-
- All Implemented Interfaces:
BytesSupplier
,NDResource
,java.io.Serializable
,java.lang.AutoCloseable
,java.lang.Cloneable
,java.lang.Iterable<NDArray>
,java.util.Collection<NDArray>
,java.util.List<NDArray>
,java.util.RandomAccess
public class NDList extends java.util.ArrayList<NDArray> implements NDResource, BytesSupplier
AnNDList
represents a sequence ofNDArray
s with names.Each
NDArray
in this list can optionally have a name. You can use the name to look up an NDArray in the NDList.- See Also:
NDArray
, Serialized Form
-
-
Constructor Summary
Constructors Constructor Description NDList()
Constructs an empty NDList.NDList(int initialCapacity)
Constructs an empty NDList with the specified initial capacity.NDList(NDArray... arrays)
Constructs and initiates an NDList with the specifiedNDArray
s.NDList(java.util.Collection<NDArray> other)
Constructs and initiates an NDList with the specifiedNDArray
s.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description NDList
addAll(NDList other)
Appends all of the NDArrays in the specified NDList to the end of this NDList, in the order that they are returned by the specified NDList's iterator.void
attach(NDManager manager)
Attaches thisNDResource
to the specifiedNDManager
.void
close()
boolean
contains(java.lang.String name)
Returnstrue
if this NDList contains an NDArray with the specified name.static NDList
decode(NDManager manager, byte[] byteArray)
Decodes NDList from byte array.static NDList
decode(NDManager manager, java.io.InputStream is)
Decodes NDList fromInputStream
.void
detach()
Detaches theNDResource
from currentNDManager
's lifecycle.byte[]
encode()
Encodes the NDList to byte array.void
encode(java.io.OutputStream os)
Writes the encoded NDList toOutputStream
.void
encode(java.io.OutputStream os, boolean numpy)
Writes the encoded NDList toOutputStream
.NDArray
get(java.lang.String name)
Returns the first occurrence of the specified element from this NDList if it is present.byte[]
getAsBytes()
Returns thebyte[]
presentation of the object.NDManager
getManager()
Returns theNDManager
that manages this.Shape[]
getShapes()
Gets all of shapes in theNDList
.NDArray
head()
Returns the head index of the NDList.NDArray
remove(java.lang.String name)
Removes the first occurrence of the specified element from this NDList if it is present.NDArray
singletonOrThrow()
Returns the only element if this is a singleton NDList or throws an exception if multiple elements.NDList
subNDList(int fromIndex)
Returns a view of the portion of this NDList between the specified fromIndex, inclusive, and to the end.void
tempAttach(NDManager manager)
Temporarily attaches thisNDResource
to the specifiedNDManager
.java.nio.ByteBuffer
toByteBuffer()
Returns theByteBuffer
presentation of the object.NDList
toDevice(Device device, boolean copy)
java.lang.String
toString()
-
Methods inherited from class java.util.ArrayList
add, add, addAll, addAll, clear, clone, contains, ensureCapacity, equals, forEach, get, hashCode, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, removeIf, removeRange, replaceAll, retainAll, set, size, sort, spliterator, subList, toArray, toArray, trimToSize
-
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface ai.djl.ndarray.BytesSupplier
getAsObject, getAsString
-
-
-
-
Constructor Detail
-
NDList
public NDList()
Constructs an empty NDList.
-
NDList
public NDList(int initialCapacity)
Constructs an empty NDList with the specified initial capacity.- Parameters:
initialCapacity
- the initial capacity of the list- Throws:
java.lang.IllegalArgumentException
- if the specified initial capacity is negative
-
NDList
public NDList(NDArray... arrays)
Constructs and initiates an NDList with the specifiedNDArray
s.- Parameters:
arrays
- theNDArray
s
-
-
Method Detail
-
decode
public static NDList decode(NDManager manager, byte[] byteArray)
Decodes NDList from byte array.- Parameters:
manager
- manager assigned toNDArray
byteArray
- byte array to load from- Returns:
NDList
-
decode
public static NDList decode(NDManager manager, java.io.InputStream is)
Decodes NDList fromInputStream
.- Parameters:
manager
- manager assigned toNDArray
is
- input stream contains the ndlist information- Returns:
NDList
-
get
public NDArray get(java.lang.String name)
Returns the first occurrence of the specified element from this NDList if it is present.- Parameters:
name
- the name of the NDArray- Returns:
- the first occurrence
-
remove
public NDArray remove(java.lang.String name)
Removes the first occurrence of the specified element from this NDList if it is present.If this list does not contain the element, it is unchanged. More formally, removes the element with the lowest index
i
such that(o==null ? get(i)==null : o.equals(get(i)))
(if such an element exists).- Parameters:
name
- the name of the NDArray to be removed from this NDList, if present- Returns:
- the element that was removed
-
contains
public boolean contains(java.lang.String name)
Returnstrue
if this NDList contains an NDArray with the specified name.- Parameters:
name
- the name of the NDArray to be removed from this NDList, if present- Returns:
true
if this list contains the specified element
-
head
public NDArray head()
Returns the head index of the NDList.- Returns:
- the head NDArray
- Throws:
java.lang.IndexOutOfBoundsException
- if the index is out of range (index < 0 || index >= size()
)
-
singletonOrThrow
public NDArray singletonOrThrow()
Returns the only element if this is a singleton NDList or throws an exception if multiple elements.- Returns:
- the head NDArray
- Throws:
java.lang.IndexOutOfBoundsException
- if the list does not contain exactly one element
-
addAll
public NDList addAll(NDList other)
Appends all of the NDArrays in the specified NDList to the end of this NDList, in the order that they are returned by the specified NDList's iterator.- Parameters:
other
- the NDList containing NDArray to be added to this list- Returns:
- this NDList after the addition
-
subNDList
public NDList subNDList(int fromIndex)
Returns a view of the portion of this NDList between the specified fromIndex, inclusive, and to the end.- Parameters:
fromIndex
- the start index (inclusive)- Returns:
- a view of the portion of this NDList
-
getManager
public NDManager getManager()
Returns theNDManager
that manages this.- Specified by:
getManager
in interfaceNDResource
- Returns:
- the
NDManager
that manages this.
-
attach
public void attach(NDManager manager)
Attaches thisNDResource
to the specifiedNDManager
.Attached resource will be closed when the
NDManager
is closed.- Specified by:
attach
in interfaceNDResource
- Parameters:
manager
- theNDManager
to be attached to
-
tempAttach
public void tempAttach(NDManager manager)
Temporarily attaches thisNDResource
to the specifiedNDManager
.Attached resource will be returned to the original manager when the
NDManager
is closed.- Specified by:
tempAttach
in interfaceNDResource
- Parameters:
manager
- theNDManager
to be attached to
-
detach
public void detach()
Detaches theNDResource
from currentNDManager
's lifecycle.This becomes un-managed and it is the user's responsibility to close this. Failure to close the resource might cause your machine to run out of native memory.
- Specified by:
detach
in interfaceNDResource
- See Also:
NDManager
-
encode
public byte[] encode()
Encodes the NDList to byte array.- Returns:
- the byte array
-
encode
public void encode(java.io.OutputStream os) throws java.io.IOException
Writes the encoded NDList toOutputStream
.- Parameters:
os
- theOutputStream
to be written to- Throws:
java.io.IOException
- if failed on IO operation
-
encode
public void encode(java.io.OutputStream os, boolean numpy) throws java.io.IOException
Writes the encoded NDList toOutputStream
.- Parameters:
os
- theOutputStream
to be written tonumpy
- encode in npz format if true- Throws:
java.io.IOException
- if failed on IO operation
-
getAsBytes
public byte[] getAsBytes()
Returns thebyte[]
presentation of the object.- Specified by:
getAsBytes
in interfaceBytesSupplier
- Returns:
- the
byte[]
presentation of the object
-
toByteBuffer
public java.nio.ByteBuffer toByteBuffer()
Returns theByteBuffer
presentation of the object.- Specified by:
toByteBuffer
in interfaceBytesSupplier
- Returns:
- the
ByteBuffer
presentation of the object
-
getShapes
public Shape[] getShapes()
Gets all of shapes in theNDList
.- Returns:
- shapes in
NDList
-
close
public void close()
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfaceNDResource
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.util.AbstractCollection<NDArray>
-
-