Module tornado.api

Class ShortArray

java.lang.Object
uk.ac.manchester.tornado.api.types.arrays.TornadoNativeArray
uk.ac.manchester.tornado.api.types.arrays.ShortArray

public final class ShortArray extends TornadoNativeArray
ShortArray relies on preview features of the Java platform:
Programs can only use ShortArray when preview features are enabled.
Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.
This class represents an array of shorts stored in native memory. The short data is stored in a MemorySegmentPREVIEW, which represents a contiguous region of off-heap memory. The class also encapsulates methods for setting and getting short values, for initializing the short array, and for converting the array to and from different representations.
  • Constructor Details

    • ShortArray

      public ShortArray(int numberOfElements)
      Constructs a new instance of the ShortArray that will store a user-specified number of elements.
      Parameters:
      numberOfElements - The number of elements in the array.
    • ShortArray

      public ShortArray(ShortArray... arrays)
      Constructs a new ShortArray instance by concatenating the contents of the given array of ShortArray instances.
      Parameters:
      arrays - An array of ShortArray instances to be concatenated into the new instance.
  • Method Details

    • fromArray

      public static ShortArray fromArray(short[] values)
      Creates a new instance of the ShortArray class from an on-heap short array.
      Parameters:
      values - The on-heap short array to create the instance from.
      Returns:
      A new ShortArray instance, initialized with values of the on-heap short array.
    • fromElements

      public static ShortArray fromElements(short... values)
      Creates a new instance of the ShortArray class from a set of short values.
      Parameters:
      values - The short values to initialize the array with.
      Returns:
      A new ShortArray instance, initialized with the given values.
    • fromSegment

      public static ShortArray fromSegment(MemorySegmentPREVIEW segment)
      Creates a new instance of the ShortArray class from a MemorySegmentPREVIEW.
      Parameters:
      segment - The MemorySegmentPREVIEW containing the off-heap short data.
      Returns:
      A new ShortArray instance, initialized with the segment data.
    • fromSegmentShallow

      public static ShortArray fromSegmentShallow(MemorySegmentPREVIEW segment)
      Creates a new instance of the ShortArray class by wrapping an existing MemorySegmentPREVIEW without copying its contents.
      Parameters:
      segment - The MemorySegmentPREVIEW containing *both* the off-heap short *header* and *data*.
      Returns:
      A new ShortArray instance that wraps the given segment.
    • fromShortBuffer

      public static ShortArray fromShortBuffer(ShortBuffer buffer)
      Creates a new instance of the ShortArray class from a ShortBuffer.
      Parameters:
      buffer - The ShortBuffer containing the short data.
      Returns:
      A new ShortArray instance, initialized with the buffer data.
    • toHeapArray

      public short[] toHeapArray()
      Converts the short data from off-heap to on-heap, by copying the values of a ShortArray instance into a new on-heap array.
      Returns:
      A new on-heap short array, initialized with the values stored in the ShortArray instance.
    • set

      public void set(int index, short value)
      Sets the short value at a specified index of the ShortArray instance.
      Parameters:
      index - The index at which to set the short value.
      value - The short value to store at the specified index.
    • get

      public short get(int index)
      Gets the short value stored at the specified index of the ShortArray instance.
      Parameters:
      index - The index of which to retrieve the short value.
      Returns:
    • clear

      public void clear()
      Sets all the values of the ShortArray instance to zero.
      Specified by:
      clear in class TornadoNativeArray
    • getElementSize

      public int getElementSize()
      Specified by:
      getElementSize in class TornadoNativeArray
    • init

      public void init(short value)
      Initializes all the elements of the ShortArray instance with a specified value.
      Parameters:
      value - The short value to initialize the ShortArray instance with.
    • getSize

      public int getSize()
      Returns the number of short elements stored in the ShortArray instance.
      Specified by:
      getSize in class TornadoNativeArray
      Returns:
    • getSegment

      public MemorySegmentPREVIEW getSegment()
      Returns the underlying MemorySegmentPREVIEW of the ShortArray instance.
      Specified by:
      getSegment in class TornadoNativeArray
      Returns:
      The MemorySegmentPREVIEW associated with the ShortArray instance.
    • getSegmentWithHeader

      public MemorySegmentPREVIEW getSegmentWithHeader()
      Returns the underlying MemorySegmentPREVIEW of the ShortArray instance, including the header.
      Specified by:
      getSegmentWithHeader in class TornadoNativeArray
      Returns:
      The MemorySegmentPREVIEW associated with the ShortArray instance.
    • getNumBytesOfSegmentWithHeader

      public long getNumBytesOfSegmentWithHeader()
      Returns the total number of bytes that the MemorySegmentPREVIEW, associated with the ShortArray instance, occupies.
      Specified by:
      getNumBytesOfSegmentWithHeader in class TornadoNativeArray
      Returns:
      The total number of bytes of the MemorySegmentPREVIEW.
    • getNumBytesOfSegment

      public long getNumBytesOfSegment()
      Returns the number of bytes of the MemorySegmentPREVIEW that is associated with the ShortArray instance, excluding the header bytes.
      Specified by:
      getNumBytesOfSegment in class TornadoNativeArray
      Returns:
      The number of bytes of the raw data in the MemorySegmentPREVIEW.
    • initialize

      public static void initialize(ShortArray array, short value)
      Factory method to initialize a ShortArray. This method can be invoked from a Task-Graph.
      Parameters:
      array - Input Array.
      value - The float value to initialize the ShortArray instance with.
    • concat

      public static ShortArray concat(ShortArray... arrays)
      Concatenates multiple ShortArray instances into a single ShortArray.
      Parameters:
      arrays - Variable number of ShortArray objects to be concatenated.
      Returns:
      A new ShortArray instance containing all the elements of the input arrays, concatenated in the order they were provided.
    • slice

      public ShortArray slice(int offset, int length)
      Extracts a slice of elements from a given ShortArray, creating a new ShortArray instance.
      Parameters:
      offset - The starting index from which to begin the slice, inclusive.
      length - The number of elements to include in the slice.
      Returns:
      A new ShortArray instance representing the specified slice of the original array.
      Throws:
      IllegalArgumentException - if the specified slice is out of the bounds of the original array.