Class SimpleMemoryModel

java.lang.Object
net.algart.arrays.AbstractMemoryModel
net.algart.arrays.SimpleMemoryModel
All Implemented Interfaces:
MemoryModel

public final class SimpleMemoryModel extends AbstractMemoryModel

The simplest memory model, based on usual Java arrays. It means that AlgART array of elements of some type contains an underlying Java array type[], and any access to AlgART array elements is translated to corresponding access to the underlying Java array. The only exception is bit arrays, where the bits are packed into long[] Java array, as specified in PackedBitArrays class.

This memory model supports all possible element types. The maximal theoretical limit for length and capacity of AlgART arrays, supported by this memory model, is 237-64 for bit arrays and 231-1 for all other element types. The real limit is essentially less, usually 2 GB in 64-bit JVM or ~1.0-1.5 GB in 32-bit JVM.

This memory model may be not effective enough for very large arrays of non-primitive elements (inheritors of Object class): it stores them as array of pointers, alike standard ArrayList. For effective storing arrays of objects, we recommend using the combined memory model.

All arrays created by this memory model, besides bit arrays and immutable views of the arrays, implement DirectAccessible interface, DirectAccessible.hasJavaArray() returns true for such arrays, and DirectAccessible.javaArrayOffset() returns 0 for them. Moreover, if this array is created as unresizable using newUnresizableArray(Class, long) method or some equivalent way, including newUnresizableXxxArray or newMatrix methods, then you can be sure that its length (returned by Array.length() and DirectAccessible.javaArrayLength() methods) is equal to the actual length of the Java array, returned by DirectAccessible.javaArray() method; Arrays.isJavaArrayWrapper(Array) returns true for such arrays.

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) methods: these methods do nothing.

In the arrays of objects (non-primitive) created by this memory model:

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 Details

    • getInstance

      public static SimpleMemoryModel getInstance()
      Returns an instance of this memory model. This method always returns the same object.
      Returns:
      an instance of this memory model.
      See Also:
    • newEmptyArray

      public MutableArray newEmptyArray(Class<?> elementType)
      Description copied from interface: MemoryModel
      Allocates an empty resizable array with the specified element type and a little initial capacity. It is equivalent to newEmptyArray(elementType, n), where n is some little value.

      Example of usage:

           MutableFloatArray a = (MutableFloatArray)memoryModel.newEmptyArray(float.class);
       
      Specified by:
      newEmptyArray in interface MemoryModel
      Specified by:
      newEmptyArray in class AbstractMemoryModel
      Parameters:
      elementType - the type of array elements.
      Returns:
      created AlgART array.
      See Also:
    • newEmptyArray

      public MutableArray newEmptyArray(Class<?> elementType, long initialCapacity)
      Description copied from interface: MemoryModel
      Allocates 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, DoubleArray or CharArray. 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 MutableObjectArray interface.

      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, UnsupportedElementTypeException will be thrown.

      Some too large array capacities may be not supported by this memory model. For example, SimpleMemoryModel does not support arrays larger than 0x7FFFFFFF (Integer.MAX_VALUE) elements.

      Example of usage:

           MutableFloatArray a = (MutableFloatArray)memoryModel.newEmptyArray(float.class, 10000);
       
      Specified by:
      newEmptyArray in interface MemoryModel
      Specified by:
      newEmptyArray in class AbstractMemoryModel
      Parameters:
      elementType - the type of array elements.
      initialCapacity - the initial capacity of the array.
      Returns:
      created AlgART array.
      See Also:
    • newArray

      public MutableArray newArray(Class<?> elementType, long initialLength)
      Description copied from interface: MemoryModel
      Allocates 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:
      newArray in interface MemoryModel
      Specified by:
      newArray in class AbstractMemoryModel
      Parameters:
      elementType - the type of array elements.
      initialLength - the initial length and capacity of the array.
      Returns:
      created AlgART array.
      See Also:
    • newUnresizableArray

      public UpdatableArray newUnresizableArray(Class<?> elementType, long length)
      Description copied from interface: MemoryModel
      Allocates 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 (or newUnresizableBitArray, 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 the large memory model every resizable array is stored in the file consisting of integer number of blocks per DataFileModel.recommendedBankSize(true) bytes.

      Specified by:
      newUnresizableArray in interface MemoryModel
      Specified by:
      newUnresizableArray in class AbstractMemoryModel
      Parameters:
      elementType - the type of array elements.
      length - the length and capacity of the array.
      Returns:
      created unresizable AlgART array.
      See Also:
    • valueOf

      public UpdatableArray valueOf(Object array, int offset, int count)
      Description copied from interface: MemoryModel
      Allocates an unresizable AlgART array containing count elements of the specified Java array: array[offset], array[offset + 1], ..., array[offset + count - 1].

      The returned AlgART array will be "safe" in the sense that no references to the passed Java array are maintained by it. In other words, this method must always allocate a new AlgART array.

      This method is equivalent to the following expression: newUnresizableArray(elementType, count).setData(0, array, offset, count), where elementType is the type of array elements (array.getClass().getComponentType()).

      Specified by:
      valueOf in interface MemoryModel
      Overrides:
      valueOf in class AbstractMemoryModel
      Parameters:
      array - the source Java array with elements of constructed AlgART array.
      offset - starting position in the source Java array.
      count - the length of returned AlgART array.
      Returns:
      created unresizable AlgART array.
    • valueOf

      public UpdatableArray valueOf(Object array)
      Description copied from interface: MemoryModel
      Allocates an unresizable AlgART array containing all elements of the specified Java array: array[0], array[1], ..., array[array.length - 1].

      The returned AlgART array will be "safe" in the sense that no references to the passed Java array are maintained by it. In other words, this method must always allocate a new array.

      This method is equivalent to the following expression: newUnresizableArray(elementType, len).setData(0, array), where elementType is the type of array elements (array.getClass().getComponentType()) and len is the length of the passed Java array.

      Specified by:
      valueOf in interface MemoryModel
      Overrides:
      valueOf in class AbstractMemoryModel
      Parameters:
      array - the source Java array with elements of constructed AlgART array.
      Returns:
      created unresizable AlgART array.
    • valueOf

      public UpdatableBitArray valueOf(boolean[] array, int offset, int count)
      Description copied from interface: MemoryModel
      Equivalent to (UpdatableBitArray)valueOf((Object)array, offset, count).
      Specified by:
      valueOf in interface MemoryModel
      Overrides:
      valueOf in class AbstractMemoryModel
      Parameters:
      array - the source Java array with elements of constructed AlgART array.
      offset - starting position in the source Java array.
      count - the length of returned AlgART array.
      Returns:
      created unresizable AlgART array.
    • valueOf

      public UpdatableBitArray valueOf(boolean[] array)
      Description copied from interface: MemoryModel
      Equivalent to (UpdatableBitArray)valueOf((Object)array).
      Specified by:
      valueOf in interface MemoryModel
      Overrides:
      valueOf in class AbstractMemoryModel
      Parameters:
      array - the source Java array with elements of constructed AlgART array.
      Returns:
      created unresizable AlgART array.
    • valueOf

      public UpdatableCharArray valueOf(char[] array, int offset, int count)
      Description copied from interface: MemoryModel
      Equivalent to (UpdatableCharArray)valueOf((Object)array, offset, count).
      Specified by:
      valueOf in interface MemoryModel
      Overrides:
      valueOf in class AbstractMemoryModel
      Parameters:
      array - the source Java array with elements of constructed AlgART array.
      offset - starting position in the source Java array.
      count - the length of returned AlgART array.
      Returns:
      created unresizable AlgART array.
    • valueOf

      public UpdatableCharArray valueOf(char[] array)
      Description copied from interface: MemoryModel
      Equivalent to (UpdatableCharArray)valueOf((Object)array).
      Specified by:
      valueOf in interface MemoryModel
      Overrides:
      valueOf in class AbstractMemoryModel
      Parameters:
      array - the source Java array with elements of constructed AlgART array.
      Returns:
      created unresizable AlgART array.
    • valueOf

      public UpdatableByteArray valueOf(byte[] array, int offset, int count)
      Description copied from interface: MemoryModel
      Equivalent to (UpdatableByteArray)valueOf((Object)array, offset, count).
      Specified by:
      valueOf in interface MemoryModel
      Overrides:
      valueOf in class AbstractMemoryModel
      Parameters:
      array - the source Java array with elements of constructed AlgART array.
      offset - starting position in the source Java array.
      count - the length of returned AlgART array.
      Returns:
      created unresizable AlgART array.
    • valueOf

      public UpdatableByteArray valueOf(byte[] array)
      Description copied from interface: MemoryModel
      Equivalent to (UpdatableByteArray)valueOf((Object)array).
      Specified by:
      valueOf in interface MemoryModel
      Overrides:
      valueOf in class AbstractMemoryModel
      Parameters:
      array - the source Java array with elements of constructed AlgART array.
      Returns:
      created unresizable AlgART array.
    • valueOf

      public UpdatableShortArray valueOf(short[] array, int offset, int count)
      Description copied from interface: MemoryModel
      Equivalent to (UpdatableShortArray)valueOf((Object)array, offset, count).
      Specified by:
      valueOf in interface MemoryModel
      Overrides:
      valueOf in class AbstractMemoryModel
      Parameters:
      array - the source Java array with elements of constructed AlgART array.
      offset - starting position in the source Java array.
      count - the length of returned AlgART array.
      Returns:
      created unresizable AlgART array.
    • valueOf

      public UpdatableShortArray valueOf(short[] array)
      Description copied from interface: MemoryModel
      Equivalent to (UpdatableShortArray)valueOf((Object)array).
      Specified by:
      valueOf in interface MemoryModel
      Overrides:
      valueOf in class AbstractMemoryModel
      Parameters:
      array - the source Java array with elements of constructed AlgART array.
      Returns:
      created unresizable AlgART array.
    • valueOf

      public UpdatableIntArray valueOf(int[] array, int offset, int count)
      Description copied from interface: MemoryModel
      Equivalent to (UpdatableIntArray)valueOf((Object)array, offset, count).
      Specified by:
      valueOf in interface MemoryModel
      Overrides:
      valueOf in class AbstractMemoryModel
      Parameters:
      array - the source Java array with elements of constructed AlgART array.
      offset - starting position in the source Java array.
      count - the length of returned AlgART array.
      Returns:
      created unresizable AlgART array.
    • valueOf

      public UpdatableIntArray valueOf(int[] array)
      Description copied from interface: MemoryModel
      Equivalent to (UpdatableIntArray)valueOf((Object)array).
      Specified by:
      valueOf in interface MemoryModel
      Overrides:
      valueOf in class AbstractMemoryModel
      Parameters:
      array - the source Java array with elements of constructed AlgART array.
      Returns:
      created unresizable AlgART array.
    • valueOf

      public UpdatableLongArray valueOf(long[] array, int offset, int count)
      Description copied from interface: MemoryModel
      Equivalent to (UpdatableLongArray)valueOf((Object)array, offset, count).
      Specified by:
      valueOf in interface MemoryModel
      Overrides:
      valueOf in class AbstractMemoryModel
      Parameters:
      array - the source Java array with elements of constructed AlgART array.
      offset - starting position in the source Java array.
      count - the length of returned AlgART array.
      Returns:
      created unresizable AlgART array.
    • valueOf

      public UpdatableLongArray valueOf(long[] array)
      Description copied from interface: MemoryModel
      Equivalent to (UpdatableLongArray)valueOf((Object)array).
      Specified by:
      valueOf in interface MemoryModel
      Overrides:
      valueOf in class AbstractMemoryModel
      Parameters:
      array - the source Java array with elements of constructed AlgART array.
      Returns:
      created unresizable AlgART array.
    • valueOf

      public UpdatableFloatArray valueOf(float[] array, int offset, int count)
      Description copied from interface: MemoryModel
      Equivalent to (UpdatableFloatArray)valueOf((Object)array, offset, count).
      Specified by:
      valueOf in interface MemoryModel
      Overrides:
      valueOf in class AbstractMemoryModel
      Parameters:
      array - the source Java array with elements of constructed AlgART array.
      offset - starting position in the source Java array.
      count - the length of returned AlgART array.
      Returns:
      created unresizable AlgART array.
    • valueOf

      public UpdatableFloatArray valueOf(float[] array)
      Description copied from interface: MemoryModel
      Equivalent to (UpdatableFloatArray)valueOf((Object)array).
      Specified by:
      valueOf in interface MemoryModel
      Overrides:
      valueOf in class AbstractMemoryModel
      Parameters:
      array - the source Java array with elements of constructed AlgART array.
      Returns:
      created unresizable AlgART array.
    • valueOf

      public UpdatableDoubleArray valueOf(double[] array, int offset, int count)
      Description copied from interface: MemoryModel
      Equivalent to (UpdatableDoubleArray)valueOf((Object)array, offset, count).
      Specified by:
      valueOf in interface MemoryModel
      Overrides:
      valueOf in class AbstractMemoryModel
      Parameters:
      array - the source Java array with elements of constructed AlgART array.
      offset - starting position in the source Java array.
      count - the length of returned AlgART array.
      Returns:
      created unresizable AlgART array.
    • valueOf

      public UpdatableDoubleArray valueOf(double[] array)
      Description copied from interface: MemoryModel
      Equivalent to (UpdatableDoubleArray)valueOf((Object)array).
      Specified by:
      valueOf in interface MemoryModel
      Overrides:
      valueOf in class AbstractMemoryModel
      Parameters:
      array - the source Java array with elements of constructed AlgART array.
      Returns:
      created unresizable AlgART array.
    • isElementTypeSupported

      public boolean isElementTypeSupported(Class<?> elementType)
      Description copied from interface: MemoryModel
      Returns 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 throw UnsupportedElementTypeException. The result is not defined for void.class.
      Specified by:
      isElementTypeSupported in interface MemoryModel
      Specified by:
      isElementTypeSupported in class AbstractMemoryModel
      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: MemoryModel
      Returns true if this memory model can create arrays with all primitive element types: boolean, char, byte, short, int, long, float, double.
      Specified by:
      areAllPrimitiveElementTypesSupported in interface MemoryModel
      Specified by:
      areAllPrimitiveElementTypesSupported in class AbstractMemoryModel
      Returns:
      true if this memory model supports all primitive element types.
      See Also:
    • areAllElementTypesSupported

      public boolean areAllElementTypesSupported()
      Description copied from interface: MemoryModel
      Returns true if this memory model can create arrays with all element types. This package offers only one such memory model: SimpleMemoryModel.
      Specified by:
      areAllElementTypesSupported in interface MemoryModel
      Specified by:
      areAllElementTypesSupported in class AbstractMemoryModel
      Returns:
      true if this memory model supports element types.
      See Also:
    • maxSupportedLength

      public long maxSupportedLength(Class<?> elementType)
      This implementation returns Integer.MAX_VALUE == 231-1 for all element types besides boolean.class, or some large value (depending on implementation) for boolean.class.

      In current implementation, returns 237-64 for boolean.class, because bits are stored in long values.

      Specified by:
      maxSupportedLength in interface MemoryModel
      Specified by:
      maxSupportedLength in class AbstractMemoryModel
      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

      public boolean isCreatedBy(Array array)
      Description copied from interface: MemoryModel
      Returns true if the passed array was created by this (or identical) memory model.

      For SimpleMemoryModel and BufferMemoryModel, "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 the data file factory.

      For CombinedMemoryModel, "identical" means that the memory model is also combined and was created with the same instance of the combiner.

      Returns false if the passed argument is null.

      Specified by:
      isCreatedBy in interface MemoryModel
      Specified by:
      isCreatedBy in class AbstractMemoryModel
      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.
    • isSimpleArray

      public static boolean isSimpleArray(Array array)
      Returns true if the passed instance is an array created by 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 created by the simple memory model.
    • toString

      public String toString()
      Returns a brief string description of this memory model.

      The result of this method may depend on implementation.

      Overrides:
      toString in class Object
      Returns:
      a brief string description of this object.
    • asUpdatableArray

      public static UpdatableArray asUpdatableArray(Object array)
      Returns a wrapper: an unresizable AlgART array backed by the specified Java array, excluding a case of boolean[] array. For boolean elements (BitArray) please use the method asUpdatableBitArray(long[], long).

      The result AlgART array contains all elements the passed Java array: array[0], array[1], ..., array[array.length - 1], and changes in the returned array "write through" to array argument. The length and capacity of the returned array are equal to array.length.

      For arrays, created by this method, Arrays.isJavaArrayWrapper(Array) method returns true.

      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.
    • asUpdatablePArray

      public static UpdatablePArray asUpdatablePArray(Object array)
      Analog of asUpdatableArray(Object) with the only difference, that this method does not work with Java array of objects.
      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.
    • asUpdatableBitArray

      public static UpdatableBitArray asUpdatableBitArray(long[] packedBitArray, long length)
      Returns an unresizable AlgART bit array backed by the specified long[]>array according the packing rules, describing in PackedBitArrays class. Changes in the returned array "write through" to array argument. The length and capacity of the returned array are equal to the specified length argument.
      Parameters:
      packedBitArray - the source long[]> array.
      length - the length of the returned bit array.
      Returns:
      an unresizable AlgART bit array backed by the specified Java array.
      Throws:
      NullPointerException - if array argument is null.
      IllegalArgumentException - if length<0 or if the passed array is too short to store length bits (i.e. if array.length < (length+63)/64).
    • asUpdatableCharArray

      public static UpdatableCharArray asUpdatableCharArray(char[] array)
      Equivalent to (UpdatableCharArray)asUpdatableArray((Object)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.
    • asUpdatableByteArray

      public static UpdatableByteArray asUpdatableByteArray(byte[] array)
      Equivalent to (UpdatableByteArray)asUpdatableArray((Object)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.
    • asUpdatableShortArray

      public static UpdatableShortArray asUpdatableShortArray(short[] array)
      Equivalent to (UpdatableShortArray)asUpdatableArray((Object)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.
    • asUpdatableIntArray

      public static UpdatableIntArray asUpdatableIntArray(int[] array)
      Equivalent to (UpdatableIntArray)asUpdatableArray((Object)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.
    • asUpdatableLongArray

      public static UpdatableLongArray asUpdatableLongArray(long[] array)
      Equivalent to (UpdatableLongArray)asUpdatableArray((Object)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.
    • asUpdatableFloatArray

      public static UpdatableFloatArray asUpdatableFloatArray(float[] array)
      Equivalent to (UpdatableFloatArray)asUpdatableArray((Object)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.
    • asUpdatableDoubleArray

      public static UpdatableDoubleArray asUpdatableDoubleArray(double[] array)
      Equivalent to (UpdatableDoubleArray)asUpdatableArray((Object)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.
    • asUpdatableObjectArray

      public static <E> UpdatableObjectArray<E> asUpdatableObjectArray(E[] array)
      Equivalent to (UpdatableObjectArray)asUpdatableArray((Object)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.
    • asMatrix

      public static Matrix<UpdatablePArray> asMatrix(Object array, long... dim)
      Equivalent to Matrices.matrix(asUpdatablePArray(array), dim).

      Note that this method cannot be used with Object[] array. If you still need to return a matrix of objects, you may use Matrices.matrix method together with asUpdatableArray(Object).

      Parameters:
      array - the source Java array.
      dim - the matrix dimensions.
      Returns:
      a matrix backed by the specified Java array with the specified dimensions.
      Throws:
      NullPointerException - if array or dim argument is null.
      IllegalArgumentException - if array argument is not an array, or boolean[] array, or array of objects, or if the number of dimensions is 0 (empty dim Java array), or if some of the dimensions are negative.
      SizeMismatchException - if the product of all dimensions is not equal to the array length.
      TooLargeArrayException - if the product of all dimensions is greater than Long.MAX_VALUE.