Interface PArray
- All Superinterfaces:
Array
- All Known Subinterfaces:
BitArray
,ByteArray
,CharArray
,DoubleArray
,FloatArray
,IntArray
,LongArray
,MutableBitArray
,MutableByteArray
,MutableCharArray
,MutableDoubleArray
,MutableFloatArray
,MutableIntArray
,MutableLongArray
,MutablePArray
,MutablePFixedArray
,MutablePFloatingArray
,MutablePIntegerArray
,MutablePNumberArray
,MutableShortArray
,PFixedArray
,PFloatingArray
,PIntegerArray
,PNumberArray
,ShortArray
,UpdatableBitArray
,UpdatableByteArray
,UpdatableCharArray
,UpdatableDoubleArray
,UpdatableFloatArray
,UpdatableIntArray
,UpdatableLongArray
,UpdatablePArray
,UpdatablePFixedArray
,UpdatablePFloatingArray
,UpdatablePIntegerArray
,UpdatablePNumberArray
,UpdatableShortArray
- All Known Implementing Classes:
AbstractBitArray
,AbstractByteArray
,AbstractCharArray
,AbstractDoubleArray
,AbstractFloatArray
,AbstractIntArray
,AbstractLongArray
,AbstractShortArray
,AbstractUpdatableBitArray
,AbstractUpdatableByteArray
,AbstractUpdatableCharArray
,AbstractUpdatableDoubleArray
,AbstractUpdatableFloatArray
,AbstractUpdatableIntArray
,AbstractUpdatableLongArray
,AbstractUpdatableShortArray
AlgART array of primitive elements (boolean, char, byte, short, int, long, float or double), read-only access.
Any class implementing this interface must implement one of
BitArray
, CharArray
,
ByteArray
, ShortArray
,
IntArray
, LongArray
,
FloatArray
, DoubleArray
subinterfaces.
- Author:
- Daniel Alievsky
-
Method Summary
Modifier and TypeMethodDescriptionstatic UpdatablePArray
Equivalent toSimpleMemoryModel.asUpdatablePArray
(array).Returns an immutable view of this array.Returns a trusted immutable view of this array.long
Return the number of memory bits occupied by every element of this array.double
getDouble
(long index) Returns the element #index converted to double: (double)(value&0xFF) for byte value, (double)(value&0xFFFF) for short value, (double)value for int, long, float, double, char values, or value?1.0:0.0 for boolean values.long
indexOf
(long lowIndex, long highIndex, double value) boolean
Returns true if all elements of this array are zero (false for boolean[] array, (char)0 for char[]).default byte[]
jaByte()
Equivalent to the following expression: thisObject instanceof ByteArray a ? a.ja()
:Arrays.toByteJavaArray
(thisObject).default double[]
jaDouble()
Equivalent to the following expression: thisObject instanceof DoubleArray a ? a.ja()
:Arrays.toDoubleJavaArray
(thisObject).default float[]
jaFloat()
Equivalent to the following expression: thisObject instanceof FloatArray a ? a.ja()
:Arrays.toFloatJavaArray
(thisObject).default int[]
jaInt()
Equivalent to the following expression: thisObject instanceof IntArray a ? a.ja()
:Arrays.toIntJavaArray
(thisObject).default long[]
jaLong()
Equivalent to the following expression: thisObject instanceof LongArray a ? a.ja()
:Arrays.toLongJavaArray
(thisObject).default short[]
jaShort()
Equivalent to the following expression: thisObject instanceof ShortArray a ? a.ja()
:Arrays.toShortJavaArray
(thisObject).long
lastIndexOf
(long lowIndex, long highIndex, double value) Returns the maximal index k, so that highIndex>k>=max(lowIndex,0) andgetDouble
(k)==value, or -1 if there is no such array element.matrix
(long... dim) Equivalent tomatrix
(thisArray, dim).double
maxPossibleValue
(double valueForFloatingPoint) Returns 1 forBitArray
, 0xFF forByteArray
, 0xFFFF forCharArray
andShortArray
, Integer.MAX_VALUE forIntArray
, Long.MAX_VALUE forLongArray
, valueForFloatingPoint forFloatArray
andDoubleArray
.double
minPossibleValue
(double valueForFloatingPoint) Returns 0 forBitArray
,ByteArray
,CharArray
andShortArray
, Integer.MIN_VALUE forIntArray
, Long.MIN_VALUE forLongArray
, valueForFloatingPoint forFloatArray
andDoubleArray
.mutableClone
(MemoryModel memoryModel) Returns a mutable resizable copy of this array.Class<? extends MutablePArray>
Returns the canonical resizable AlgART type of arrays with the same element types: the class of one of 9 basic interfaces, describing all kinds of resizable AlgART arrays for 8 primitive and any non-primitive element types.type()
Returns the canonical AlgART type of this array: the class of one of 9 basic interfaces, describing all kinds of AlgART arrays for 8 primitive and any non-primitive element types.updatableClone
(MemoryModel memoryModel) Returns an unresizable updatable copy of this array.Class<? extends UpdatablePArray>
Returns the canonical updatable AlgART type of arrays with the same element types: the class of one of 9 basic interfaces, describing all kinds of updatable AlgART arrays for 8 primitive and any non-primitive element types.Methods inherited from interface net.algart.arrays.Array
asCopyOnNextWrite, buffer, buffer, buffer, buffer, byteOrder, capacity, checkUnallowedMutation, elementType, equals, flushResources, flushResources, freeResources, freeResources, getData, getData, getElement, hashCode, isCopyOnNextWrite, isEmpty, isImmutable, isLazy, isNew, isNewReadOnlyView, isUnresizable, ja, length, length32, loadResources, newJavaArray, shallowClone, subArr, subArray, toString
-
Method Details
-
bitsPerElement
long bitsPerElement()Return the number of memory bits occupied by every element of this array. The amount of memory used by the array can be estimated asArray.capacity()
*bitsPerElement()/8 bytes (when the array capacity is large enough).If the number of occupied bits is not defined (for example, may depend on JVM implementation), this method returns -1.
For implementations from this package, this method returns:
- 1 for
BitArray
(the value ofArrays.BITS_PER_BIT
constant), - 16 for
CharArray
(the value ofArrays.BITS_PER_CHAR
constant), - 8 for
ByteArray
(the value ofArrays.BITS_PER_BYTE
constant), - 16 for
ShortArray
(the value ofArrays.BITS_PER_SHORT
constant), - 32 for
IntArray
(the value ofArrays.BITS_PER_INT
constant), - 64 for
LongArray
(the value ofArrays.BITS_PER_LONG
constant), - 32 for
FloatArray
(the value ofArrays.BITS_PER_FLOAT
constant), - 64 for
DoubleArray
(the value ofArrays.BITS_PER_DOUBLE
constant).
(-1 result is never returned by implementations from this package.)
Please keep in mind that the real amount of occupied memory, theoretically, can differ from the value returned by this method. For example, some JVM, theoretically, may store byte elements of byte[] array in 32-bit memory words. In this case, this method will return invalid result for byte arrays created by the
simple memory model
. However: we guarantee the results of this method are always correct for arrays created by thebuffer memory model
andlarge memory model
.There is a guarantee that this method works very quickly (usually it just returns a constant or a value of some private field).
- Returns:
- the number of bytes occupied by every element of this array, or -1 if it cannot be determined.
- See Also:
- 1 for
-
minPossibleValue
double minPossibleValue(double valueForFloatingPoint) Returns 0 forBitArray
,ByteArray
,CharArray
andShortArray
, Integer.MIN_VALUE forIntArray
, Long.MIN_VALUE forLongArray
, valueForFloatingPoint forFloatArray
andDoubleArray
. Forfixed-point arrays
it is the minimal possible value, that can stored in elements of this array (byte and short elements are interpreted as unsigned). This method is equivalent tominPossibleValue
(thisArray.getClass(), valueForFloatingPoint).- Parameters:
valueForFloatingPoint
- the value returned for floating-point array type.- Returns:
- the minimal possible value, that can stored in elements of this array, if it is a fixed-point array, or the argument for floating-point arrays.
- See Also:
-
maxPossibleValue
double maxPossibleValue(double valueForFloatingPoint) Returns 1 forBitArray
, 0xFF forByteArray
, 0xFFFF forCharArray
andShortArray
, Integer.MAX_VALUE forIntArray
, Long.MAX_VALUE forLongArray
, valueForFloatingPoint forFloatArray
andDoubleArray
. Forfixed-point arrays
it is the maximal possible value, that can stored in elements of this array (byte and short elements are interpreted as unsigned). This method is equivalent tomaxPossibleValue
(thisArray.getClass(), valueForFloatingPoint).- Parameters:
valueForFloatingPoint
- the value returned for floating-point array type.- Returns:
- the maximal possible value, that can stored in elements of this array, if it is a fixed-point array, or the argument for floating-point arrays.
- See Also:
-
getDouble
double getDouble(long index) Returns the element #index converted to double: (double)(value&0xFF) for byte value, (double)(value&0xFFFF) for short value, (double)value for int, long, float, double, char values, or value?1.0:0.0 for boolean values. Please note that this method returns unsigned values for byte and short arrays. Returned value contains full information stored in the element, excepting the case of very large long elements.- Parameters:
index
- index of element to get.- Returns:
- the element at the specified position in this array.
- Throws:
IndexOutOfBoundsException
- if index out of range 0..length()-1.- See Also:
-
indexOf
long indexOf(long lowIndex, long highIndex, double value) Returns the minimal index k, so that lowIndex<=k<min(highIndex,thisArray.length()
) andgetDouble
(k)==value, or -1 if there is no such array element.In particular, if lowIndex>=thisArray.
length()
} or lowIndex>=highIndex, this method returns -1, and if lowIndex<0, the result is the same as if lowIndex==0.You may specify lowIndex=0 and highIndex=Long.MAX_VALUE to search through all array elements.
- Parameters:
lowIndex
- the low index in the array for search (inclusive).highIndex
- the high index in the array for search (exclusive).value
- the value to be found.- Returns:
- the index of the first occurrence of this value in this array in range lowIndex<=index<highIndex, or -1 if this value does not occur in this range.
-
lastIndexOf
long lastIndexOf(long lowIndex, long highIndex, double value) Returns the maximal index k, so that highIndex>k>=max(lowIndex,0) andgetDouble
(k)==value, or -1 if there is no such array element.In particular, if highIndex<=0 or highIndex<=lowIndex, this method returns -1, and if highIndex>=thisArray.
length()
, the result is the same as if highIndex==thisArray.length()
.You may specify lowIndex=0 and highIndex=Long.MAX_VALUE to search through all array elements.
- Parameters:
lowIndex
- the low index in the array for search (inclusive).highIndex
- the high index in the array for search (exclusive).value
- the value to be found.- Returns:
- the index of the last occurrence of this value in this array in range lowIndex<=index<highIndex, or -1 if this value does not occur in this range.
-
isZeroFilled
boolean isZeroFilled()Returns true if all elements of this array are zero (false for boolean[] array, (char)0 for char[]). Returns false if at least one of elements of this array is non-zero.For arrays of floating-point types (
PFloatingArray
), this method considers that +0.0==-0.0: both values are considered to be zero.If the
length
of this array is 0 (the array is empty), returns true.This method usually requires some time for execution, because it checks all array elements.
- Returns:
- true if and only if all elements of this array are zero, or if this array is empty.
-
type
Description copied from interface:Array
Returns the canonical AlgART type of this array: the class of one of 9 basic interfaces, describing all kinds of AlgART arrays for 8 primitive and any non-primitive element types. More precisely, returns:BitArray
.class, if this object is an instance ofBitArray
,CharArray
.class, if this object is an instance ofCharArray
,ByteArray
.class, if this object is an instance ofByteArray
,ShortArray
.class, if this object is an instance ofShortArray
,IntArray
.class, if this object is an instance ofIntArray
,LongArray
.class, if this object is an instance ofLongArray
,FloatArray
.class, if this object is an instance ofFloatArray
,DoubleArray
.class, if this object is an instance ofDoubleArray
,ObjectArray
.class, if this object is an instance ofObjectArray
.
There is a guarantee that this method works very quickly (usually it just returns a constant value).
-
updatableType
Class<? extends UpdatablePArray> updatableType()Description copied from interface:Array
Returns the canonical updatable AlgART type of arrays with the same element types: the class of one of 9 basic interfaces, describing all kinds of updatable AlgART arrays for 8 primitive and any non-primitive element types. More precisely, returns:UpdatableBitArray
.class, if this object is an instance ofBitArray
,UpdatableCharArray
.class, if this object is an instance ofCharArray
,UpdatableByteArray
.class, if this object is an instance ofByteArray
,UpdatableShortArray
.class, if this object is an instance ofShortArray
,UpdatableIntArray
.class, if this object is an instance ofIntArray
,UpdatableLongArray
.class, if this object is an instance ofLongArray
,UpdatableFloatArray
.class, if this object is an instance ofFloatArray
,UpdatableDoubleArray
.class, if this object is an instance ofDoubleArray
,UpdatableObjectArray
.class, if this object is an instance ofObjectArray
.
There is a guarantee that this method works very quickly (usually it just returns a constant value).
- Specified by:
updatableType
in interfaceArray
- Returns:
- canonical AlgART type of an updatable array of the same kind.
-
mutableType
Class<? extends MutablePArray> mutableType()Description copied from interface:Array
Returns the canonical resizable AlgART type of arrays with the same element types: the class of one of 9 basic interfaces, describing all kinds of resizable AlgART arrays for 8 primitive and any non-primitive element types. More precisely, returns:MutableBitArray
.class, if this object is an instance ofBitArray
,MutableCharArray
.class, if this object is an instance ofCharArray
,MutableByteArray
.class, if this object is an instance ofByteArray
,MutableShortArray
.class, if this object is an instance ofShortArray
,MutableIntArray
.class, if this object is an instance ofIntArray
,MutableLongArray
.class, if this object is an instance ofLongArray
,MutableFloatArray
.class, if this object is an instance ofFloatArray
,MutableDoubleArray
.class, if this object is an instance ofDoubleArray
,MutableObjectArray
.class, if this object is an instance ofObjectArray
.
There is a guarantee that this method works very quickly (usually it just returns a constant value).
- Specified by:
mutableType
in interfaceArray
- Returns:
- canonical AlgART type of a resizable array of the same kind.
-
asImmutable
PArray asImmutable()Description copied from interface:Array
Returns an immutable view of this array. If this array is already immutable (i.e.Array.isImmutable()
is true), returns a reference to this object.A array is considered to be immutable, if there are no ways to modify its content or state with help of this instance. In particular, immutable arrays never implement
UpdatableArray
orDirectAccessible
interfaces. Moreover, any third-party implementation of Array interface must return an instance of a class, which has no methods or fields allowing to change this instance.Query operations on the returned array "read through" to this array. The returned view is also unresizable (see
UpdatableArray.asUnresizable()
).The returned view (when it is not a reference to this object) contains the same elements as this array, but independent length, start offset, capacity, copy-on-next-write and possible other information about array characteristics besides its elements, as for
Array.shallowClone()
method. If modifications of this array characteristics lead to reallocation of the internal storage, then the returned array ceases to be a view of this array. The only possible reasons for reallocation are the following: callingMutableArray.length(long)
,MutableArray.ensureCapacity(long)
orMutableArray.trim()
methods for this array, or any modification of this or returned array in a case when this array iscopy-on-next-write
.By default, the array factories (
memory models
) create mutable arrays, but they can be converted to immutable by this method.Note:
Array.isNew()
method, called for the result of this method, always returns false — because it does not implementUpdatableArray
.Also note:
Array.isNewReadOnlyView()
method, called for the result of this method, always returns the same value asArray.isNewReadOnlyView()
for this object. Really,- it this object is immutable (
Array.isImmutable()
==true), then it is obvious (this method just returns a reference to this array); - it this object is not immutable (
Array.isImmutable()
==false), then, according to the contract toArray.isNewReadOnlyView()
method,Array.isNewReadOnlyView()
must return false for this array (in other caseArray.isImmutable()
would return true) and it also must return false for the returned array (because it is a view of another array and not an original view of external data — see the beginning of the comment toArray.isNewReadOnlyView()
).
- Specified by:
asImmutable
in interfaceArray
- Returns:
- an immutable view of this array (or a reference to this array if it is already immutable).
- See Also:
- it this object is immutable (
-
asTrustedImmutable
PArray asTrustedImmutable()Description copied from interface:Array
Returns a trusted immutable view of this array. If this array is already trusted immutable, returns a reference to this object.A array is considered to be "trusted" immutable, if it potentially can change its elements, but the Java code working with this array promises that it will not change them. The returned instance never implements
UpdatableArray
, but may implementDirectAccessible
, that allow quick access to its elements. As forusual immutable view
, query operations on the returned array "read through" to this array.The only standard way allowing to change elements of returned array is using
DirectAccessible.javaArray()
method, in a case when the array is backed by an accessible array. But the Java code, processing the trusted immutable array, must use this method only for quick reading elements and not try to change them. If, despite the promise, the elements of the trusted immutable array will be changed, theUnallowedMutationError
may be thrown by the call ofArray.checkUnallowedMutation()
method.In some implementations — for example, if
DirectAccessible
interface is not supported by this array — this method may return the same result asArray.asImmutable()
.The returned view is always unresizable.
The returned view (when it is not a reference to this object) contains the same elements as this array, but independent length, start offset, capacity, copy-on-next-write and possible other information about array characteristics besides its elements, as for
Array.shallowClone()
method. If modifications of this array characteristics lead to reallocation of the internal storage, then the returned array ceases to be a view of this array. The only possible reasons for reallocation are the following: callingMutableArray.length(long)
,MutableArray.ensureCapacity(long)
orMutableArray.trim()
methods for this array, or any modification of this or returned array in a case when this array iscopy-on-next-write
.Trusted immutable view is a compromise between absolute safety, provided by
usual immutable view
, and maximal efficiency, achieved while using the original non-protected array. Please see the package description to learn more about possible usage of this method.- Specified by:
asTrustedImmutable
in interfaceArray
- Returns:
- a trusted immutable view of this array (or a reference to this array if it is already trusted immutable).
- See Also:
-
mutableClone
Description copied from interface:Array
Returns a mutable resizable copy of this array. This method is equivalent to the following code:memoryModel.
newArray
(thisArray).copy
(thisArray);Please note: this method is a good choice for cloning little arrays (thousands, maybe millions elements). If you clone large arrays by this method, the user, in particular, has no ways to view the progress of copying or to interrupt copying. To clone large arrays, we recommend the following code:
MutableArray clone = memoryModel.
newArray
(thisArray);Arrays.copy
(someContext, clone, a);- Specified by:
mutableClone
in interfaceArray
- Parameters:
memoryModel
- the memory model, used for allocation a new copy of this array.- Returns:
- a mutable copy of this array.
- See Also:
-
updatableClone
Description copied from interface:Array
Returns an unresizable updatable copy of this array. This method is equivalent to the following code:memoryModel.
newUnresizableArray
(thisArray).copy
(thisArray);Please note: this method is a good choice for cloning little arrays (thousands, maybe millions elements). If you clone large arrays by this method, the user, in particular, has no ways to view the progress of copying or to interrupt copying. To clone large arrays, we recommend the following code:
UpdatableArray clone = memoryModel.
newUnresizableArray
(thisArray);Arrays.copy
(someContext, clone, a);- Specified by:
updatableClone
in interfaceArray
- Parameters:
memoryModel
- the memory model, used for allocation a new copy of this array.- Returns:
- an updatable copy of this array.
- See Also:
-
matrix
Description copied from interface:Array
Equivalent tomatrix
(thisArray, dim). -
jaByte
default byte[] jaByte()Equivalent to the following expression: thisObject instanceof ByteArray a ? a.ja()
:Arrays.toByteJavaArray
(thisObject).This method may be used instead of
Arrays.toByteJavaArray(PArray)
, if you need maximally quick access to this data in a form of byte[] array and there is high probability that this AlgART array is awrapper
for standard byte[] array.Be careful: this method is potentially unsafe! The main purpose of this method is to quickly access array data for reading. But it also allows you to modify this data, and the result of such modification is unpredictable: this may change the original AlgART array, but may also not change. Typically you should not attempt to modify the Java array returned by this method; this helps to avoid difficult bugs.
- Returns:
- Java array containing all the elements in this array, cast to byte type according to AlgART rules.
- Throws:
TooLargeArrayException
- if the array length is greater than Integer.MAX_VALUE.
-
jaShort
default short[] jaShort()Equivalent to the following expression: thisObject instanceof ShortArray a ? a.ja()
:Arrays.toShortJavaArray
(thisObject).This method may be used instead of
Arrays.toShortJavaArray(PArray)
, if you need maximally quick access to this data in a form of short[] array and there is high probability that this AlgART array is awrapper
for standard short[] array.Be careful: this method is potentially unsafe! The main purpose of this method is to quickly access array data for reading. But it also allows you to modify this data, and the result of such modification is unpredictable: this may change the original AlgART array, but may also not change. Typically you should not attempt to modify the Java array returned by this method; this helps to avoid difficult bugs.
- Returns:
- Java array containing all the elements in this array, cast to short type according to AlgART rules.
- Throws:
TooLargeArrayException
- if the array length is greater than Integer.MAX_VALUE.
-
jaInt
default int[] jaInt()Equivalent to the following expression: thisObject instanceof IntArray a ? a.ja()
:Arrays.toIntJavaArray
(thisObject).This method may be used instead of
Arrays.toIntJavaArray(PArray)
, if you need maximally quick access to this data in a form of int[] array and there is high probability that this AlgART array is awrapper
for standard int[] array.Be careful: this method is potentially unsafe! The main purpose of this method is to quickly access array data for reading. But it also allows you to modify this data, and the result of such modification is unpredictable: this may change the original AlgART array, but may also not change. Typically you should not attempt to modify the Java array returned by this method; this helps to avoid difficult bugs.
- Returns:
- Java array containing all the elements in this array, cast to int type according to AlgART rules.
- Throws:
TooLargeArrayException
- if the array length is greater than Integer.MAX_VALUE.
-
jaLong
default long[] jaLong()Equivalent to the following expression: thisObject instanceof LongArray a ? a.ja()
:Arrays.toLongJavaArray
(thisObject).This method may be used instead of
Arrays.toLongJavaArray(PArray)
, if you need maximally quick access to this data in a form of long[] array and there is high probability that this AlgART array is awrapper
for standard long[] array.Be careful: this method is potentially unsafe! The main purpose of this method is to quickly access array data for reading. But it also allows you to modify this data, and the result of such modification is unpredictable: this may change the original AlgART array, but may also not change. Typically you should not attempt to modify the Java array returned by this method; this helps to avoid difficult bugs.
- Returns:
- Java array containing all the elements in this array, cast to long type according to AlgART rules.
- Throws:
TooLargeArrayException
- if the array length is greater than Integer.MAX_VALUE.
-
jaFloat
default float[] jaFloat()Equivalent to the following expression: thisObject instanceof FloatArray a ? a.ja()
:Arrays.toFloatJavaArray
(thisObject).This method may be used instead of
Arrays.toFloatJavaArray(PArray)
, if you need maximally quick access to this data in a form of float[] array and there is high probability that this AlgART array is awrapper
for standard float[] array.Be careful: this method is potentially unsafe! The main purpose of this method is to quickly access array data for reading. But it also allows you to modify this data, and the result of such modification is unpredictable: this may change the original AlgART array, but may also not change. Typically you should not attempt to modify the Java array returned by this method; this helps to avoid difficult bugs.
- Returns:
- Java array containing all the elements in this array, cast to float type according to AlgART rules.
- Throws:
TooLargeArrayException
- if the array length is greater than Integer.MAX_VALUE.
-
jaDouble
default double[] jaDouble()Equivalent to the following expression: thisObject instanceof DoubleArray a ? a.ja()
:Arrays.toDoubleJavaArray
(thisObject).This method may be used instead of
Arrays.toDoubleJavaArray(PArray)
, if you need maximally quick access to this data in a form of double[] array and there is high probability that this AlgART array is awrapper
for standard double[] array.Be careful: this method is potentially unsafe! The main purpose of this method is to quickly access array data for reading. But it also allows you to modify this data, and the result of such modification is unpredictable: this may change the original AlgART array, but may also not change. Typically you should not attempt to modify the Java array returned by this method; this helps to avoid difficult bugs.
- Returns:
- Java array containing all the elements in this array, cast to double type according to AlgART rules.
- Throws:
TooLargeArrayException
- if the array length is greater than Integer.MAX_VALUE.
-
as
Equivalent toSimpleMemoryModel.asUpdatablePArray
(array).- Parameters:
array
- the source Java array.- Returns:
- an unresizable AlgART array backed by the specified Java array.
- Throws:
NullPointerException
- if array argument is null.IllegalArgumentException
- if array argument is not an array, or boolean[] array, or Objects[] array.
-