Class BufferMemoryModel
- All Implemented Interfaces:
MemoryModel
The memory model, based on ByteBuffer and other buffers from
package.
It means that AlgART array of elements of some type (byte,
char, etc.) contains an underlying NIO buffer
TypeBuffer (correspondingly ByteBuffer, CharBuffer, etc.),
and any access to AlgART array elements is translated
to corresponding access to the underlying NIO buffer.
The only exception is bit arrays,
where the bits are packed into elements of LongBuffer,
as specified in PackedBitBuffers class.
Unlike the simple memory model, this model supports
primitive element types only:
BitArray, CharArray, ByteArray, ShortArray,
IntArray, LongArray, FloatArray, DoubleArray.
If you need to store objects in NIO buffers, you may use the AlgART arrays,
created by this memory model, as a storage for combined arrays,
created by CombinedMemoryModel.
The maximal theoretical limit for length and capacity of AlgART arrays, supported by this memory model, is 234-64 for bit arrays and 231/size-1 for all other element types, where size is 1,2,2,4,8,4,8 for element types byte, char, short, int, long, float, double correspondingly. In other words, the capacity of AlgART array cannot exceed 2 GB. The real limit is little less in 32-bit JVM, usually ~1.0-1.5 GB.
The NIO buffers, used by this memory model, are always
they are created by ByteBuffer.allocateDirect method with, for non-byte arrays,
the following byteBuffer.asTypeBuffer() call.
The byte order in the buffers is always
excepting the case when AlgART arrays are created via asUpdatableArray(ByteBuffer, Class)
method, when the byte order of the passed buffer is preserver.
Arrays, created by this memory model, never implement DirectAccessible interface.
However, if is absolutely necessary to provide maximal performance for this memory model,
you may use getByteBuffer(Array) method to
get a reference to the internal underlying ByteBuffer.
All arrays, created by this memory model, have empty implementation of
Array.loadResources(ArrayContext),
Array.flushResources(ArrayContext), Array.flushResources(ArrayContext, boolean) and
Array.freeResources(ArrayContext, boolean) methods:
these methods do nothing.
This class is immutable and thread-safe:
there are no ways to modify settings of its instance returned by getInstance() method.
Moreover, it is a singleton: getInstance() always returns the same object.
- Author:
- Daniel Alievsky
-
Method Summary
Modifier and TypeMethodDescriptionbooleanReturns true if this memory model can create arrays with all element types.booleanReturns true if this memory model can create arrays with all primitive element types: boolean, char, byte, short, int, long, float, double.static UpdatableArrayasUpdatableArray(ByteBuffer byteBuffer, Class<?> elementType) Returns an unresizable AlgART array backed by a duplicate of the specified ByteBuffer (more precisely, byteBuffer.duplicate().order(byteBuffer.order())), excluding a case of boolean element type.static UpdatableBitArrayasUpdatableBitArray(ByteBuffer byteBuffer, long length) Returns an unresizable AlgART bit array backed by a duplicate of the specified ByteBuffer (more precisely, byteBuffer.duplicate().order(byteBuffer.order())), according the packing rules, describing inPackedBitBuffersclass, applied to asLongBuffer() representation of the duplicate.static UpdatableByteArrayasUpdatableByteArray(ByteBuffer byteBuffer) Equivalent to (UpdatableByteArray)asUpdatableArray(byteBuffer, byte.class).static UpdatableCharArrayasUpdatableCharArray(ByteBuffer byteBuffer) Equivalent to (UpdatableCharArray)asUpdatableArray(byteBuffer, char.class).static UpdatableDoubleArrayasUpdatableDoubleArray(ByteBuffer byteBuffer) Equivalent to (UpdatableDoubleArray)asUpdatableArray(byteBuffer, double.class).static UpdatableFloatArrayasUpdatableFloatArray(ByteBuffer byteBuffer) Equivalent to (UpdatableFloatArray)asUpdatableArray(byteBuffer, float.class).static UpdatableIntArrayasUpdatableIntArray(ByteBuffer byteBuffer) Equivalent to (UpdatableIntArray)asUpdatableArray(byteBuffer, int.class).static UpdatableLongArrayasUpdatableLongArray(ByteBuffer byteBuffer) Equivalent to (UpdatableLongArray)asUpdatableArray(byteBuffer, long.class).static UpdatableShortArrayasUpdatableShortArray(ByteBuffer byteBuffer) Equivalent to (UpdatableShortArray)asUpdatableArray(byteBuffer, short.class).static longgetBufferOffset(Array bufferArray) Returns the start offset in the buffer returned bygetByteBuffer(Array)call, corresponding to the first element of the passed AlgART array.static ByteBuffergetByteBuffer(Array bufferArray) Returns the underlying ByteBuffer instance where all elements of the passed array are stored.static BufferMemoryModelReturns an instance of this memory model.static booleanisBufferArray(Array array) Returns true if the passed array was created by some instance of this memory model.booleanisCreatedBy(Array array) Returns true if the passed array was created by this (or identical) memory model.booleanisElementTypeSupported(Class<?> elementType) Returns true if this memory model can create arrays with this element type.longmaxSupportedLength(Class<?> elementType) This implementation returns 234-64 for boolean.class or 231/size-1 for all other element types, where size is 1,2,2,4,8,4,8 for element types byte, char, short, int, long, float, double correspondingly.Allocates a zero-filled resizable array with the specified element type and initial length.newEmptyArray(Class<?> elementType) Allocates an empty resizable array with the specified element type and a little initial capacity.newEmptyArray(Class<?> elementType, long initialCapacity) Allocates an empty resizable array with the specified element type and initial capacity.newUnresizableArray(Class<?> elementType, long length) Allocates a zero-filled unresizable array with the specified element type and length.toString()Returns a brief string description of this memory model.Methods inherited from class net.algart.arrays.AbstractMemoryModel
newArray, newBitArray, newBitMatrix, newByteArray, newByteMatrix, newCharArray, newCharMatrix, newDoubleArray, newDoubleMatrix, newEmptyBitArray, newEmptyBitArray, newEmptyByteArray, newEmptyByteArray, newEmptyCharArray, newEmptyCharArray, newEmptyDoubleArray, newEmptyDoubleArray, newEmptyFloatArray, newEmptyFloatArray, newEmptyIntArray, newEmptyIntArray, newEmptyLongArray, newEmptyLongArray, newEmptyObjectArray, newEmptyObjectArray, newEmptyShortArray, newEmptyShortArray, newFloatArray, newFloatMatrix, newIntArray, newIntMatrix, newLazyCopy, newLazyCopy, newLongArray, newLongMatrix, newMatrix, newMatrix, newMatrix, newObjectArray, newObjectMatrix, newShortArray, newShortMatrix, newStructuredMatrix, newUnresizableArray, newUnresizableBitArray, newUnresizableByteArray, newUnresizableCharArray, newUnresizableDoubleArray, newUnresizableFloatArray, newUnresizableIntArray, newUnresizableLazyCopy, newUnresizableLongArray, newUnresizableObjectArray, newUnresizableShortArray, valueOf, valueOf, valueOf, valueOf, valueOf, valueOf, valueOf, valueOf, valueOf, valueOf, valueOf, valueOf, valueOf, valueOf, valueOf, valueOf, valueOf, valueOf, valueOf, valueOf
-
Method Details
-
getInstance
Returns an instance of this memory model. This method always returns the same object.- Returns:
- an instance of this memory model.
-
newEmptyArray
Description copied from interface:MemoryModelAllocates an empty resizable array with the specified element type and a little initial capacity. It is equivalent tonewEmptyArray(elementType, n), where n is some little value.Example of usage:
MutableFloatArray a = (MutableFloatArray)memoryModel.newEmptyArray(float.class);
- Specified by:
newEmptyArrayin interfaceMemoryModel- Specified by:
newEmptyArrayin classAbstractMemoryModel- Parameters:
elementType- the type of array elements.- Returns:
- created AlgART array.
- See Also:
-
newEmptyArray
Description copied from interface:MemoryModelAllocates an empty resizable array with the specified element type and initial capacity.The element type can be either usual object class (as String.class), or one of the primitive types: boolean.class, byte.class, short.class, int.class, long.class, float.class, double.class, char.class. The element type cannot be void.class.
In a case of primitive types, the created array will implement the corresponding interface
BitArray,ByteArray,ShortArray,IntArray,LongArray,FloatArray,DoubleArrayorCharArray. In this case, the created array (unlike standard ArrayList) will occupy the same amount of memory as the Java array boolean[initialCapacity], byte[initialCapacity], etc.In a case of non-primitive types (Object inheritors), the created array will implement the
MutableObjectArrayinterface.Some element type may be not supported by this memory model. For example, some memory models may support only primitive types, or only one concrete type. In such a case,
UnsupportedElementTypeExceptionwill be thrown.Some too large array capacities may be not supported by this memory model. For example,
SimpleMemoryModeldoes not support arrays larger than 0x7FFFFFFF (Integer.MAX_VALUE) elements.Example of usage:
MutableFloatArray a = (MutableFloatArray)memoryModel.newEmptyArray(float.class, 10000);
- Specified by:
newEmptyArrayin interfaceMemoryModel- Specified by:
newEmptyArrayin classAbstractMemoryModel- Parameters:
elementType- the type of array elements.initialCapacity- the initial capacity of the array.- Returns:
- created AlgART array.
- See Also:
-
newArray
Description copied from interface:MemoryModelAllocates a zero-filled resizable array with the specified element type and initial length. The capacity of new array will be equal to its length.This method is equivalent to the following call:
newEmptyArray(elementType, initialLength).length(initialLength).trim().- Specified by:
newArrayin interfaceMemoryModel- Specified by:
newArrayin classAbstractMemoryModel- Parameters:
elementType- the type of array elements.initialLength- the initial length and capacity of the array.- Returns:
- created AlgART array.
- See Also:
-
newUnresizableArray
Description copied from interface:MemoryModelAllocates a zero-filled unresizable array with the specified element type and length. The capacity of new array will be equal to its length.The analogous result may be obtained the following call:
newArray(elementType, length).asUnresizable(). However, we don't recommend to use such code. If you are sure that you will not need to change the array length, please always use this method (ornewUnresizableBitArray,newUnresizableBteArray, etc.). In some memory models, creating resizable array with the given length may require much more resources that creating unresizable one. For example, in thelarge memory modelevery resizable array is stored in the file consisting of integer number of blocks perDataFileModel.recommendedBankSize(true)bytes.- Specified by:
newUnresizableArrayin interfaceMemoryModel- Specified by:
newUnresizableArrayin classAbstractMemoryModel- Parameters:
elementType- the type of array elements.length- the length and capacity of the array.- Returns:
- created unresizable AlgART array.
- See Also:
-
isElementTypeSupported
Description copied from interface:MemoryModelReturns true if this memory model can create arrays with this element type. If it does not support it, creation methods of this memory model will throwUnsupportedElementTypeException. The result is not defined for void.class.- Specified by:
isElementTypeSupportedin interfaceMemoryModel- Specified by:
isElementTypeSupportedin classAbstractMemoryModel- Parameters:
elementType- the type of array elements.- Returns:
- true if this memory model supports this element type.
-
areAllPrimitiveElementTypesSupported
public boolean areAllPrimitiveElementTypesSupported()Description copied from interface:MemoryModelReturns true if this memory model can create arrays with all primitive element types: boolean, char, byte, short, int, long, float, double.- Specified by:
areAllPrimitiveElementTypesSupportedin interfaceMemoryModel- Specified by:
areAllPrimitiveElementTypesSupportedin classAbstractMemoryModel- Returns:
- true if this memory model supports all primitive element types.
- See Also:
-
areAllElementTypesSupported
public boolean areAllElementTypesSupported()Description copied from interface:MemoryModelReturns true if this memory model can create arrays with all element types. This package offers only one such memory model:SimpleMemoryModel.- Specified by:
areAllElementTypesSupportedin interfaceMemoryModel- Specified by:
areAllElementTypesSupportedin classAbstractMemoryModel- Returns:
- true if this memory model supports element types.
- See Also:
-
maxSupportedLength
This implementation returns 234-64 for boolean.class or 231/size-1 for all other element types, where size is 1,2,2,4,8,4,8 for element types byte, char, short, int, long, float, double correspondingly.- Specified by:
maxSupportedLengthin interfaceMemoryModel- Specified by:
maxSupportedLengthin classAbstractMemoryModel- Parameters:
elementType- the type of array elements.- Returns:
- maximal possible length of arrays supported by this memory model.
- Throws:
NullPointerException- if elementType is null.
-
isCreatedBy
Description copied from interface:MemoryModelReturns true if the passed array was created by this (or identical) memory model.For
SimpleMemoryModelandBufferMemoryModel, "identical" means the same memory model.For
LargeMemoryModel, "identical" means that the memory model is also large and was created with the same instance of thedata file factory.For
CombinedMemoryModel, "identical" means that the memory model is also combined and was created with the same instance of thecombiner.Returns false if the passed argument is null.
- Specified by:
isCreatedByin interfaceMemoryModel- Specified by:
isCreatedByin classAbstractMemoryModel- Parameters:
array- the AlgART array (may be null, than the method returns false).- Returns:
- true if the passed array was created by this memory model.
-
isBufferArray
Returns true if the passed array was created by some instance of this memory model. Returns false if the passed array is null or an AlgART array created by another memory model.As this memory model is a singleton, this method is equivalent to
isCreatedBy(Array).- Parameters:
array- the checked array.- Returns:
- true if this array is an array created by the buffer memory model.
-
getByteBuffer
Returns the underlying ByteBuffer instance where all elements of the passed array are stored. More precisely, if the backed ByteBuffer is bb, this method returnsbufferArray.
where o is the byte order of the original bb, which is usually the native order. So, the returned buffer does not allow to violate immutability of the passed array, and in any case you may freely change the position and limit in the returned buffer.isImmutable()? bb.asReadOnlyBuffer().order(o) : bb.duplicate().order(o);The returned object is ByteBuffer, even if the AlgART array consists of elements of another primitive type: for example, int or float. The buffer, which elements really correspond to the elements of the passed array, may be got by the following operators (where byteBuffer is a result of this method):
- just this byteBuffer if the passed array is a
ByteArray; - byteBuffer.asCharBuffer() if the passed array is a
CharArray; - byteBuffer.asShortBuffer() if the passed array is a
ShortArray; - byteBuffer.asIntBuffer() if the passed array is a
IntArray; - byteBuffer.asLongBuffer() if the passed array is a
LongArray; - byteBuffer.asFloatBuffer() if the passed array is a
FloatArray; - byteBuffer.asDoubleBuffer() if the passed array is a
DoubleArray; - byteBuffer.asLongBuffer() if the passed array is a
BitArray; in this case, the bits are packed into long values as specified inPackedBitBuffersclass.
The ByteBuffer and the specific buffer, returned by the conversion operators, listed above, may contain extra elements besides the content of the passed AlgART array. Really, the elements of the AlgART array correspond to elements #offset..#offset+bufferArray.
Array.length()-1 of the buffer returned by the conversion operator (or the ByteBuffer in a case of a byte array), where offset isgetBufferOffset(bufferArray). Changes to this range of the returned buffer "write through" to the AlgART array. For bit arrays, this range is specified in terms ofPackedBitBuffersclass; so, all elements of the AlgART array may be get and set viagetBit(src,offset+k)andsetBit(dest,offset+k,value)methods of that class, where k=0,1,...,bufferArray.length()-1.If modifications of this AlgART array characteristics lead to reallocation of the internal storage, then the returned ByteBuffer ceases to be a view of this array. The only possible reasons for reallocation are the following: calling
MutableArray.length(long),MutableArray.ensureCapacity(long)orMutableArray.trim()methods for this array, or any modification of this array in a case when this array iscopy-on-next-write.The passed argument must be created by the buffer memory model:
isBufferArray(Array)must return true. In other case, IllegalArgumentException will be thrown.- Parameters:
bufferArray- the AlgART array created by this memory model.- Returns:
- the underlying ByteBuffer.
- Throws:
NullPointerException- if bufferArray is null.IllegalArgumentException- if bufferArray is not created by the buffer memory model.
- just this byteBuffer if the passed array is a
-
getBufferOffset
Returns the start offset in the buffer returned bygetByteBuffer(Array)call, corresponding to the first element of the passed AlgART array. The returned offset is specified in terms of array elements (bits, bytes, shorts, etc.)- Parameters:
bufferArray- the AlgART array created by this memory model.- Returns:
- the start offset in the buffer returned returned by
getByteBuffer(Array)call. - Throws:
NullPointerException- if bufferArray is null.IllegalArgumentException- if bufferArray is not created by the buffer memory model.
-
asUpdatableArray
Returns an unresizable AlgART array backed by a duplicate of the specified ByteBuffer (more precisely, byteBuffer.duplicate().order(byteBuffer.order())), excluding a case of boolean element type. For boolean elements (BitArray) please use the methodasUpdatableBitArray(ByteBuffer, long).If elementType is not byte.class, the passed byte buffer is automatically converted to a buffer of corresponding type (CharBuffer, ShortBuffer, etc.) via ((ByteBuffer)byteBuffer.duplicate().order(byteBuffer.order()).rewind()).asTypeBuffer() call, where Type is the passed element type. The result AlgART array contains all elements of the converted NIO buffer, and changes in the returned array "write through" to byteBuffer argument. The length and capacity of the returned array are equal to byteBuffer.limit()/size, where size is 1,2,2,4,8,4,8 for element types byte, char, short, int, long, float, double correspondingly.
- Parameters:
byteBuffer- the source NIO ByteBuffer.elementType- the required primitive element type.- Returns:
- an unresizable AlgART array backed by the specified byte buffer.
- Throws:
NullPointerException- if byteBuffer or elementType argument is null.IllegalArgumentException- if elementType is boolean.class or non-primitive type.
-
asUpdatableBitArray
Returns an unresizable AlgART bit array backed by a duplicate of the specified ByteBuffer (more precisely, byteBuffer.duplicate().order(byteBuffer.order())), according the packing rules, describing inPackedBitBuffersclass, applied to asLongBuffer() representation of the duplicate. Changes in the returned buffer "write through" to byteBuffer argument. The length and capacity of the returned array are equal to the specified length argument.- Parameters:
byteBuffer- the source NIO ByteBuffer.length- the length of the returned bit array.- Returns:
- an unresizable AlgART array backed by the specified byte buffer.
- Throws:
NullPointerException- if byteBuffer or elementType argument is null.IllegalArgumentException- if length<0 or if the passed byteBuffer is too short to store (length+63)/64 full 64-bit long values.
-
asUpdatableCharArray
Equivalent to (UpdatableCharArray)asUpdatableArray(byteBuffer, char.class).- Parameters:
byteBuffer- the source NIO ByteBuffer.- Returns:
- an unresizable AlgART array backed by the specified byte buffer.
- Throws:
NullPointerException- if byteBuffer argument is null.
-
asUpdatableByteArray
Equivalent to (UpdatableByteArray)asUpdatableArray(byteBuffer, byte.class).- Parameters:
byteBuffer- the source NIO ByteBuffer.- Returns:
- an unresizable AlgART array backed by the specified byte buffer.
- Throws:
NullPointerException- if byteBuffer argument is null.
-
asUpdatableShortArray
Equivalent to (UpdatableShortArray)asUpdatableArray(byteBuffer, short.class).- Parameters:
byteBuffer- the source NIO ByteBuffer.- Returns:
- an unresizable AlgART array backed by the specified byte buffer.
- Throws:
NullPointerException- if byteBuffer argument is null.
-
asUpdatableIntArray
Equivalent to (UpdatableIntArray)asUpdatableArray(byteBuffer, int.class).- Parameters:
byteBuffer- the source NIO ByteBuffer.- Returns:
- an unresizable AlgART array backed by the specified byte buffer.
- Throws:
NullPointerException- if byteBuffer argument is null.
-
asUpdatableLongArray
Equivalent to (UpdatableLongArray)asUpdatableArray(byteBuffer, long.class).- Parameters:
byteBuffer- the source NIO ByteBuffer.- Returns:
- an unresizable AlgART array backed by the specified byte buffer.
- Throws:
NullPointerException- if byteBuffer argument is null.
-
asUpdatableFloatArray
Equivalent to (UpdatableFloatArray)asUpdatableArray(byteBuffer, float.class).- Parameters:
byteBuffer- the source NIO ByteBuffer.- Returns:
- an unresizable AlgART array backed by the specified byte buffer.
- Throws:
NullPointerException- if byteBuffer argument is null.
-
asUpdatableDoubleArray
Equivalent to (UpdatableDoubleArray)asUpdatableArray(byteBuffer, double.class).- Parameters:
byteBuffer- the source NIO ByteBuffer.- Returns:
- an unresizable AlgART array backed by the specified byte buffer.
- Throws:
NullPointerException- if byteBuffer argument is null.
-
toString
Returns a brief string description of this memory model.The result of this method may depend on implementation.
-