Class NDList

All Implemented Interfaces:
BytesSupplier, NDResource, Serializable, AutoCloseable, Cloneable, Iterable<NDArray>, Collection<NDArray>, List<NDArray>, RandomAccess

public class NDList extends ArrayList<NDArray> implements NDResource, BytesSupplier
An NDList represents a sequence of NDArrays 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:
  • Constructor Details

    • 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:
      IllegalArgumentException - if the specified initial capacity is negative
    • NDList

      public NDList(NDArray... arrays)
      Constructs and initiates an NDList with the specified NDArrays.
      Parameters:
      arrays - the NDArrays
    • NDList

      public NDList(Collection<NDArray> other)
      Constructs and initiates an NDList with the specified NDArrays.
      Parameters:
      other - the NDArrays
  • Method Details

    • decode

      public static NDList decode(NDManager manager, byte[] byteArray)
      Decodes NDList from byte array.
      Parameters:
      manager - manager assigned to NDArray
      byteArray - byte array to load from
      Returns:
      NDList
    • decode

      public static NDList decode(NDManager manager, InputStream is)
      Decodes NDList from InputStream.
      Parameters:
      manager - manager assigned to NDArray
      is - input stream contains the ndlist information
      Returns:
      NDList
    • get

      public NDArray get(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(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&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;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(String name)
      Returns true 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:
      IndexOutOfBoundsException - if the index is out of range (index &lt; 0 || index &gt;= 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:
      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
    • subNDList

      public NDList subNDList(int fromIndex, int toIndex)
      Returns a view of the portion of this NDList between the specified fromIndex, inclusive, and toIndex, exclusive.
      Parameters:
      fromIndex - the start index (inclusive)
      toIndex - the end index (exclusive)
      Returns:
      a view of the portion of this NDList
    • toDevice

      public NDList toDevice(Device device, boolean copy)
      Converts all the NDArray in NDList to a different Device.
      Parameters:
      device - the Device to be set
      copy - set true if you want to return a copy of the underlying NDArray
      Returns:
      a new NDList with the NDArrays on specified Device
    • getManager

      public NDManager getManager()
      Returns the NDManager that manages this.
      Specified by:
      getManager in interface NDResource
      Returns:
      the NDManager that manages this.
    • getResourceNDArrays

      public List<NDArray> getResourceNDArrays()
      Returns the NDArray or NDArrays contained within this resource.
      Specified by:
      getResourceNDArrays in interface NDResource
      Returns:
      the NDArray or NDArrays contained within this resource
    • attach

      public void attach(NDManager manager)
      Attaches this NDResource to the specified NDManager.

      Attached resource will be closed when the NDManager is closed.

      Specified by:
      attach in interface NDResource
      Parameters:
      manager - the NDManager to be attached to
    • tempAttach

      public void tempAttach(NDManager manager)
      Temporarily attaches this NDResource to the specified NDManager.

      Attached resource will be returned to the original manager when the NDManager is closed.

      Specified by:
      tempAttach in interface NDResource
      Parameters:
      manager - the NDManager to be attached to
    • detach

      public void detach()
      Detaches the NDResource from current NDManager'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 interface NDResource
      See Also:
    • encode

      public byte[] encode()
      Encodes the NDList to byte array.
      Returns:
      the byte array
    • encode

      public byte[] encode(NDList.Encoding encoding)
      Encodes the NDList to byte array.
      Parameters:
      encoding - encode mode, one of ndlist/npz/safetensor format
      Returns:
      the byte array
    • encode

      public void encode(OutputStream os) throws IOException
      Writes the encoded NDList to OutputStream.
      Parameters:
      os - the OutputStream to be written to
      Throws:
      IOException - if failed on IO operation
    • encode

      public void encode(OutputStream os, NDList.Encoding encoding) throws IOException
      Writes the encoded NDList to OutputStream.
      Parameters:
      os - the OutputStream to be written to
      encoding - encode mode, one of ndlist/npz/safetensor format
      Throws:
      IOException - if failed on IO operation
    • getAsBytes

      public byte[] getAsBytes()
      Returns the byte[] presentation of the object.
      Specified by:
      getAsBytes in interface BytesSupplier
      Returns:
      the byte[] presentation of the object
    • toByteBuffer

      public ByteBuffer toByteBuffer()
      Returns the ByteBuffer presentation of the object.
      Specified by:
      toByteBuffer in interface BytesSupplier
      Returns:
      the ByteBuffer presentation of the object
    • getShapes

      public Shape[] getShapes()
      Gets all of shapes in the NDList.
      Returns:
      shapes in NDList
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface NDResource
    • toString

      public String toString()
      Overrides:
      toString in class AbstractCollection<NDArray>