java.lang.Object
uk.ac.manchester.tornado.api.types.arrays.TornadoNativeArray
uk.ac.manchester.tornado.api.types.arrays.ShortArray
ShortArray relies on preview features of the Java platform:
ShortArrayrefers to one or more preview APIs:MemorySegment.
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.-
Field Summary
Fields inherited from class uk.ac.manchester.tornado.api.types.arrays.TornadoNativeArray
ARRAY_HEADER -
Constructor Summary
ConstructorsConstructorDescriptionShortArray(int numberOfElements) Constructs a new instance of theShortArraythat will store a user-specified number of elements.ShortArray(ShortArray... arrays) Constructs a newShortArrayinstance by concatenating the contents of the given array ofShortArrayinstances. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Sets all the values of theShortArrayinstance to zero.static ShortArrayconcat(ShortArray... arrays) Concatenates multipleShortArrayinstances into a singleShortArray.static ShortArrayfromArray(short[] values) Creates a new instance of theShortArrayclass from an on-heap short array.static ShortArrayfromElements(short... values) Creates a new instance of theShortArrayclass from a set of short values.static ShortArrayfromSegment(MemorySegmentPREVIEW segment) static ShortArrayfromSegmentShallow(MemorySegmentPREVIEW segment) Creates a new instance of theShortArrayclass by wrapping an existingMemorySegmentPREVIEW without copying its contents.static ShortArrayfromShortBuffer(ShortBuffer buffer) Creates a new instance of theShortArrayclass from aShortBuffer.shortget(int index) Gets the short value stored at the specified index of theShortArrayinstance.intlongReturns the number of bytes of theMemorySegmentPREVIEW that is associated with theShortArrayinstance, excluding the header bytes.longReturns the total number of bytes that theMemorySegmentPREVIEW, associated with theShortArrayinstance, occupies.intgetSize()Returns the number of short elements stored in theShortArrayinstance.voidinit(short value) Initializes all the elements of theShortArrayinstance with a specified value.static voidinitialize(ShortArray array, short value) Factory method to initialize aShortArray.voidset(int index, short value) Sets the short value at a specified index of theShortArrayinstance.slice(int offset, int length) Extracts a slice of elements from a givenShortArray, creating a newShortArrayinstance.short[]Converts the short data from off-heap to on-heap, by copying the values of aShortArrayinstance into a new on-heap array.
-
Constructor Details
-
ShortArray
public ShortArray(int numberOfElements) Constructs a new instance of theShortArraythat will store a user-specified number of elements.- Parameters:
numberOfElements- The number of elements in the array.
-
ShortArray
Constructs a newShortArrayinstance by concatenating the contents of the given array ofShortArrayinstances.- Parameters:
arrays- An array ofShortArrayinstances to be concatenated into the new instance.
-
-
Method Details
-
fromArray
Creates a new instance of theShortArrayclass from an on-heap short array.- Parameters:
values- The on-heap short array to create the instance from.- Returns:
- A new
ShortArrayinstance, initialized with values of the on-heap short array.
-
fromElements
Creates a new instance of theShortArrayclass from a set of short values.- Parameters:
values- The short values to initialize the array with.- Returns:
- A new
ShortArrayinstance, initialized with the given values.
-
fromSegment
- Parameters:
segment- TheMemorySegmentPREVIEW containing the off-heap short data.- Returns:
- A new
ShortArrayinstance, initialized with the segment data.
-
fromSegmentShallow
Creates a new instance of theShortArrayclass by wrapping an existingMemorySegmentPREVIEW without copying its contents.- Parameters:
segment- TheMemorySegmentPREVIEW containing *both* the off-heap short *header* and *data*.- Returns:
- A new
ShortArrayinstance that wraps the given segment.
-
fromShortBuffer
Creates a new instance of theShortArrayclass from aShortBuffer.- Parameters:
buffer- TheShortBuffercontaining the short data.- Returns:
- A new
ShortArrayinstance, initialized with the buffer data.
-
toHeapArray
public short[] toHeapArray()Converts the short data from off-heap to on-heap, by copying the values of aShortArrayinstance into a new on-heap array.- Returns:
- A new on-heap short array, initialized with the values stored in the
ShortArrayinstance.
-
set
public void set(int index, short value) Sets the short value at a specified index of theShortArrayinstance.- 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 theShortArrayinstance.- Parameters:
index- The index of which to retrieve the short value.- Returns:
-
clear
public void clear()Sets all the values of theShortArrayinstance to zero.- Specified by:
clearin classTornadoNativeArray
-
getElementSize
public int getElementSize()- Specified by:
getElementSizein classTornadoNativeArray
-
init
public void init(short value) Initializes all the elements of theShortArrayinstance with a specified value.- Parameters:
value- The short value to initialize theShortArrayinstance with.
-
getSize
public int getSize()Returns the number of short elements stored in theShortArrayinstance.- Specified by:
getSizein classTornadoNativeArray- Returns:
-
getSegment
- Specified by:
getSegmentin classTornadoNativeArray- Returns:
- The
MemorySegmentPREVIEW associated with theShortArrayinstance.
-
getSegmentWithHeader
- Specified by:
getSegmentWithHeaderin classTornadoNativeArray- Returns:
- The
MemorySegmentPREVIEW associated with theShortArrayinstance.
-
getNumBytesOfSegmentWithHeader
public long getNumBytesOfSegmentWithHeader()Returns the total number of bytes that theMemorySegmentPREVIEW, associated with theShortArrayinstance, occupies.- Specified by:
getNumBytesOfSegmentWithHeaderin classTornadoNativeArray- Returns:
- The total number of bytes of the
MemorySegmentPREVIEW.
-
getNumBytesOfSegment
public long getNumBytesOfSegment()Returns the number of bytes of theMemorySegmentPREVIEW that is associated with theShortArrayinstance, excluding the header bytes.- Specified by:
getNumBytesOfSegmentin classTornadoNativeArray- Returns:
- The number of bytes of the raw data in the
MemorySegmentPREVIEW.
-
initialize
Factory method to initialize aShortArray. This method can be invoked from a Task-Graph.- Parameters:
array- Input Array.value- The float value to initialize theShortArrayinstance with.
-
concat
Concatenates multipleShortArrayinstances into a singleShortArray.- Parameters:
arrays- Variable number ofShortArrayobjects to be concatenated.- Returns:
- A new
ShortArrayinstance containing all the elements of the input arrays, concatenated in the order they were provided.
-
slice
Extracts a slice of elements from a givenShortArray, creating a newShortArrayinstance.- 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
ShortArrayinstance representing the specified slice of the original array. - Throws:
IllegalArgumentException- if the specified slice is out of the bounds of the original array.
-
ShortArraywhen preview features are enabled.