Class HugeDoubleArray


  • public abstract class HugeDoubleArray
    extends java.lang.Object
    A long-indexable version of a primitive double array (double[]) that can contain more than 2 bn. elements.

    It is implemented by paging of smaller double-arrays (double[][]) to support approx. 32k bn. elements. If the provided size is small enough, an optimized view of a single double[] might be used.

    • The array is of a fixed size and cannot grow or shrink dynamically.
    • The array is not optimized for sparseness and has a large memory overhead if the values written to it are very sparse.
    • The array does not support default values and returns the same default for unset values that a regular double[] does (0).

    Basic Usage

     
     AllocationTracker tracker = ...;
     long arraySize = 42L;
     HugeDoubleArray array = HugeDoubleArray.newArray(arraySize, tracker);
     array.set(13L, 37D);
     double value = array.get(13L);
     // value = 37D
     
     
    • Constructor Summary

      Constructors 
      Constructor Description
      HugeDoubleArray()  
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      abstract void addTo​(long index, double value)
      Adds (+) the existing value and the provided value at the given index and stored the result into the given index.
      int copyFromArrayIntoSlice​(Array source, long sliceStart, long sliceEnd)
      Copies data from source into this array, starting from sliceStart up until sliceEnd.
      HugeDoubleArray copyOf​(long newLength, AllocationTracker tracker)
      Creates a copy of the given array.
      abstract void copyTo​(HugeDoubleArray dest, long length)
      Copies the content of this array into the target array.
      abstract void fill​(double value)
      Assigns the specified double value to each element.
      abstract double get​(long index)  
      HugeCursor<Array> initCursor​(HugeCursor<Array> cursor)
      Resets the HugeCursor to range from index 0 until size().
      HugeCursor<Array> initCursor​(HugeCursor<Array> cursor, long start, long end)
      Resets the HugeCursor to range from index start (inclusive, the first index to be contained) until end (exclusive, the first index not to be contained).
      static long memoryEstimation​(long size)  
      static HugeDoubleArray newArray​(long size, AllocationTracker tracker)
      Creates a new array of the given size, tracking the memory requirements into the given AllocationTracker.
      abstract HugeCursor<double[]> newCursor()
      Returns a new HugeCursor for this array.
      static HugeDoubleArray of​(double... values)  
      abstract long release()
      Destroys the data, allowing the underlying storage arrays to be collected as garbage.
      abstract void set​(long index, double value)
      Sets the double value at the given index to the given value.
      abstract void setAll​(java.util.function.LongToDoubleFunction gen)
      Set all elements using the provided generator function to compute each element.
      abstract long size()
      Returns the length of this array.
      abstract long sizeOf()
      abstract java.util.stream.DoubleStream stream()  
      double[] toArray()
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • HugeDoubleArray

        public HugeDoubleArray()
    • Method Detail

      • get

        public abstract double get​(long index)
        Returns:
        the double value at the given index
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - if the index is not within size()
      • set

        public abstract void set​(long index,
                                 double value)
        Sets the double value at the given index to the given value.
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - if the index is not within size()
      • addTo

        public abstract void addTo​(long index,
                                   double value)
        Adds (+) the existing value and the provided value at the given index and stored the result into the given index. If there was no previous value, the final result is set to the provided value (x + 0 == x).
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - if the index is not within size()
      • setAll

        public abstract void setAll​(java.util.function.LongToDoubleFunction gen)
        Set all elements using the provided generator function to compute each element.

        The behavior is identical to Arrays.setAll(double[], IntToDoubleFunction).

      • fill

        public abstract void fill​(double value)
        Assigns the specified double value to each element.

        The behavior is identical to Arrays.fill(double[], double).

      • size

        public abstract long size()
        Returns the length of this array.

        If the size is greater than zero, the highest supported index is size() - 1

        The behavior is identical to calling array.length on primitive arrays.

      • sizeOf

        public abstract long sizeOf()
        Returns:
        the amount of memory used by the instance of this array, in bytes. This should be the same as returned from release() without actually releasing the array.
      • release

        public abstract long release()
        Destroys the data, allowing the underlying storage arrays to be collected as garbage. The array is unusable after calling this method and will throw NullPointerExceptions on virtually every method invocation.

        Note that the data might not immediately collectible if there are still cursors alive that reference this array. You have to HugeCursor.close() every cursor instance as well.

        The amount is not removed from the AllocationTracker that had been provided in the constructor.

        Returns:
        the amount of memory freed, in bytes.
      • stream

        public abstract java.util.stream.DoubleStream stream()
      • copyTo

        public abstract void copyTo​(HugeDoubleArray dest,
                                    long length)
        Copies the content of this array into the target array.

        The behavior is identical to System.arraycopy(Object, int, Object, int, int).

      • copyOf

        public final HugeDoubleArray copyOf​(long newLength,
                                            AllocationTracker tracker)
        Creates a copy of the given array. The behavior is identical to Arrays.copyOf(int[], int).
      • toArray

        public double[] toArray()
        Returns:
        the contents of this array as a flat java primitive array. The returned array might be shared and changes would then be reflected and visible in this array.
      • newArray

        public static HugeDoubleArray newArray​(long size,
                                               AllocationTracker tracker)
        Creates a new array of the given size, tracking the memory requirements into the given AllocationTracker. The tracker is no longer referenced, as the arrays do not dynamically change their size.
      • memoryEstimation

        public static long memoryEstimation​(long size)
      • initCursor

        public final HugeCursor<Array> initCursor​(HugeCursor<Array> cursor)
        Resets the HugeCursor to range from index 0 until size(). The returned cursor is not positioned and in an invalid state. You must call HugeCursor.next() first to position the cursor to a valid state. The returned cursor is the reference-same (==) one as the provided one. Resetting the HugeCursor of an empty array (where size() returns 0) is undefined and might result in a NullPointerException or another RuntimeException.
      • initCursor

        public final HugeCursor<Array> initCursor​(HugeCursor<Array> cursor,
                                                  long start,
                                                  long end)
        Resets the HugeCursor to range from index start (inclusive, the first index to be contained) until end (exclusive, the first index not to be contained). The returned cursor is not positioned and in an invalid state. You must call HugeCursor.next() first to position the cursor to a valid state. The returned cursor is the reference-same (==) one as the provided one. Resetting the HugeCursor of an empty array (where size() returns 0) is undefined and might result in a NullPointerException or another RuntimeException.
        See Also:
        HugeArray.initCursor(HugeCursor)
      • copyFromArrayIntoSlice

        public final int copyFromArrayIntoSlice​(Array source,
                                                long sliceStart,
                                                long sliceEnd)
        Copies data from source into this array, starting from sliceStart up until sliceEnd.
        Returns:
        the number of entries copied
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object