Package extra166y

Class ParallelDoubleArray


  • public class ParallelDoubleArray
    extends ParallelDoubleArrayWithBounds
    An array of doubles supporting parallel operations. This class provides methods supporting the same operations as ParallelArray, but specialized for scalar doubles. It additionally provides a few methods specific to numerical values.
    • Method Detail

      • defaultExecutor

        public static jsr166y.ForkJoinPool defaultExecutor()
        Returns a common default executor for use in ParallelArrays. This executor arranges enough parallelism to use most, but not necessarily all, of the available processors on this system.
        Returns:
        the executor
      • create

        public static ParallelDoubleArray create​(int size,
                                                 jsr166y.ForkJoinPool executor)
        Creates a new ParallelDoubleArray using the given executor and an array of the given size
        Parameters:
        size - the array size
        executor - the executor
      • createUsingHandoff

        public static ParallelDoubleArray createUsingHandoff​(double[] handoff,
                                                             jsr166y.ForkJoinPool executor)
        Creates a new ParallelDoubleArray initially using the given array and executor. In general, the handed off array should not be used for other purposes once constructing this ParallelDoubleArray. The given array may be internally replaced by another array in the course of methods that add or remove elements.
        Parameters:
        handoff - the array
        executor - the executor
      • createFromCopy

        public static ParallelDoubleArray createFromCopy​(double[] source,
                                                         jsr166y.ForkJoinPool executor)
        Creates a new ParallelDoubleArray using the given executor and initially holding copies of the given source elements.
        Parameters:
        source - the source of initial elements
        executor - the executor
      • createFromCopy

        public static ParallelDoubleArray createFromCopy​(int size,
                                                         double[] source,
                                                         jsr166y.ForkJoinPool executor)
        Creates a new ParallelDoubleArray using an array of the given size, initially holding copies of the given source truncated or padded with zeros to obtain the specified length.
        Parameters:
        source - the source of initial elements
        size - the array size
        executor - the executor
      • createEmpty

        public static ParallelDoubleArray createEmpty​(int size,
                                                      jsr166y.ForkJoinPool executor)
        Creates a new ParallelDoubleArray using the given executor and an array of the given size, but with an initial effective size of zero, enabling incremental insertion via asList() operations.
        Parameters:
        size - the array size
        executor - the executor
      • getExecutor

        public jsr166y.ForkJoinPool getExecutor()
        Returns the executor used for computations
        Returns:
        the executor
      • replaceWithMapping

        public ParallelDoubleArray replaceWithMapping​(Ops.BinaryDoubleOp combiner,
                                                      double[] other)
        Replaces elements with results of applying op(thisElement, otherElement)
        Overrides:
        replaceWithMapping in class ParallelDoubleArrayWithFilter
        Parameters:
        other - the other array
        combiner - the combiner
        Returns:
        this (to simplify use in expressions)
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - if other array has fewer elements than this array.
      • indexOf

        public int indexOf​(double target)
        Returns the index of some element equal to given target, or -1 if not present
        Parameters:
        target - the element to search for
        Returns:
        the index or -1 if not present
      • binarySearch

        public int binarySearch​(double target)
        Assuming this array is sorted, returns the index of an element equal to given target, or -1 if not present. If the array is not sorted, the results are undefined.
        Parameters:
        target - the element to search for
        Returns:
        the index or -1 if not present
      • binarySearch

        public int binarySearch​(double target,
                                Ops.DoubleComparator comparator)
        Assuming this array is sorted with respect to the given comparator, returns the index of an element equal to given target, or -1 if not present. If the array is not sorted, the results are undefined.
        Parameters:
        target - the element to search for
        comparator - the comparator
        Returns:
        the index or -1 if not present
      • min

        public double min()
        Returns the minimum element, or Double.MAX_VALUE if empty,
        Overrides:
        min in class ParallelDoubleArrayWithDoubleMapping
        Returns:
        minimum element, or Double.MAX_VALUE if empty
      • max

        public double max()
        Returns the maximum element, or -Double.MAX_VALUE if empty
        Overrides:
        max in class ParallelDoubleArrayWithDoubleMapping
        Returns:
        maximum element, or -Double.MAX_VALUE if empty
      • cumulate

        public ParallelDoubleArray cumulate​(Ops.DoubleReducer reducer,
                                            double base)
        Replaces each element with the running cumulation of applying the given reducer. For example, if the contents are the numbers 1, 2, 3, and the reducer operation adds numbers, then after invocation of this method, the contents would be 1, 3, 6 (that is, 1, 1+2, 1+2+3);
        Parameters:
        reducer - the reducer
        base - the result for an empty array
        Returns:
        this (to simplify use in expressions)
      • precumulate

        public double precumulate​(Ops.DoubleReducer reducer,
                                  double base)
        Replaces each element with the cumulation of applying the given reducer to all previous values, and returns the total reduction. For example, if the contents are the numbers 1, 2, 3, and the reducer operation adds numbers, then after invocation of this method, the contents would be 0, 1, 3 (that is, 0, 0+1, 0+1+2, and the return value would be 6 (that is, 1+2+3);
        Parameters:
        reducer - the reducer
        base - the result for an empty array
        Returns:
        the total reduction
      • sort

        public ParallelDoubleArray sort​(Ops.DoubleComparator comparator)
        Sorts the array. Unlike Arrays.sort, this sort does not guarantee that elements with equal keys maintain their relative position in the array.
        Parameters:
        comparator - the comparator to use
        Returns:
        this (to simplify use in expressions)
      • sort

        public ParallelDoubleArray sort()
        Sorts the array, assuming all elements are Comparable. Unlike Arrays.sort, this sort does not guarantee that elements with equal keys maintain their relative position in the array.
        Returns:
        this (to simplify use in expressions)
        Throws:
        java.lang.ClassCastException - if any element is not Comparable.
      • removeConsecutiveDuplicates

        public ParallelDoubleArray removeConsecutiveDuplicates()
        Removes consecutive elements that are equal, shifting others leftward, and possibly decreasing size. This method may be used after sorting to ensure that this ParallelDoubleArray contains a set of unique elements.
        Returns:
        this (to simplify use in expressions)
      • addAll

        public ParallelDoubleArray addAll​(double[] other)
        Equivalent to asList().addAll but specialized for array arguments and likely to be more efficient.
        Parameters:
        other - the elements to add
        Returns:
        this (to simplify use in expressions)
      • addAll

        public ParallelDoubleArray addAll​(ParallelDoubleArrayWithDoubleMapping other)
        Appends all (possibly bounded, filtered, or mapped) elements of the given ParallelDoubleArray, resizing and/or reallocating this array if necessary.
        Parameters:
        other - the elements to add
        Returns:
        this (to simplify use in expressions)
      • removeAll

        public ParallelDoubleArray removeAll​(Ops.DoublePredicate selector)
        Removes from the array all elements for which the given selector holds.
        Parameters:
        selector - the selector
        Returns:
        this (to simplify use in expressions)
      • cumulateSum

        public ParallelDoubleArray cumulateSum()
        Replaces each element with the running sum
        Returns:
        this (to simplify use in expressions)
      • precumulateSum

        public double precumulateSum()
        Replaces each element with its prefix sum
        Returns:
        the total sum
      • withBounds

        public ParallelDoubleArrayWithBounds withBounds​(int firstIndex,
                                                        int upperBound)
        Returns an operation prefix that causes a method to operate only on the elements of the array between firstIndex (inclusive) and upperBound (exclusive).
        Parameters:
        firstIndex - the lower bound (inclusive)
        upperBound - the upper bound (exclusive)
        Returns:
        operation prefix
      • withFilter

        public ParallelDoubleArrayWithFilter withFilter​(Ops.DoublePredicate selector)
        Returns an operation prefix that causes a method to operate only on the elements of the array for which the given selector returns true
        Parameters:
        selector - the selector
        Returns:
        operation prefix
      • withIndexedFilter

        public ParallelDoubleArrayWithFilter withIndexedFilter​(Ops.IntAndDoublePredicate selector)
        Returns an operation prefix that causes a method to operate only on elements for which the given indexed selector returns true
        Parameters:
        selector - the selector
        Returns:
        operation prefix
      • withMapping

        public <U> ParallelDoubleArrayWithMapping<U> withMapping​(Ops.DoubleToObject<? extends U> op)
        Returns an operation prefix that causes a method to operate on mapped elements of the array using the given op.
        Parameters:
        op - the op
        Returns:
        operation prefix
      • withMapping

        public ParallelDoubleArrayWithDoubleMapping withMapping​(Ops.DoubleOp op)
        Returns an operation prefix that causes a method to operate on mapped elements of the array using the given op.
        Parameters:
        op - the op
        Returns:
        operation prefix
      • withMapping

        public ParallelDoubleArrayWithLongMapping withMapping​(Ops.DoubleToLong op)
        Returns an operation prefix that causes a method to operate on mapped elements of the array using the given op.
        Parameters:
        op - the op
        Returns:
        operation prefix
      • withIndexedMapping

        public <U> ParallelDoubleArrayWithMapping<U> withIndexedMapping​(Ops.IntAndDoubleToObject<? extends U> mapper)
        Returns an operation prefix that causes a method to operate on mappings of this array using the given mapper that accepts as arguments an element's current index and value, and produces a new value.
        Parameters:
        mapper - the mapper
        Returns:
        operation prefix
      • withIndexedMapping

        public ParallelDoubleArrayWithDoubleMapping withIndexedMapping​(Ops.IntAndDoubleToDouble mapper)
        Returns an operation prefix that causes a method to operate on mappings of this array using the given mapper that accepts as arguments an element's current index and value, and produces a new value.
        Parameters:
        mapper - the mapper
        Returns:
        operation prefix
      • withIndexedMapping

        public ParallelDoubleArrayWithLongMapping withIndexedMapping​(Ops.IntAndDoubleToLong mapper)
        Returns an operation prefix that causes a method to operate on mappings of this array using the given mapper that accepts as arguments an element's current index and value, and produces a new value.
        Parameters:
        mapper - the mapper
        Returns:
        operation prefix
      • iterator

        public java.util.Iterator<java.lang.Double> iterator()
        Returns an iterator stepping through each element of the array up to the current limit. This iterator does not support the remove operation. However, a full ListIterator supporting add, remove, and set operations is available via asList().
        Returns:
        an iterator stepping through each element.
      • asList

        public java.util.List<java.lang.Double> asList()
        Returns a view of this ParallelDoubleArray as a List. This List has the same structural and performance characteristics as ArrayList, and may be used to modify, replace or extend the bounds of the array underlying this ParallelDoubleArray. The methods supported by this list view are not in general implemented as parallel operations. This list is also not itself thread-safe. In particular, performing list updates while other parallel operations are in progress has undefined (and surely undesired) effects.
        Returns:
        a list view
      • size

        public int size()
        Returns the effective size of the underlying array. The effective size is the current limit, if used (see setLimit(int)), or the length of the array otherwise.
        Overrides:
        size in class AbstractParallelAnyArray
        Returns:
        the effective size of array
      • getArray

        public double[] getArray()
        Returns the underlying array used for computations
        Returns:
        the array
      • get

        public double get​(int i)
        Returns the element of the array at the given index
        Parameters:
        i - the index
        Returns:
        the element of the array at the given index
      • set

        public void set​(int i,
                        double x)
        Sets the element of the array at the given index to the given value
        Parameters:
        i - the index
        x - the value
      • toString

        public java.lang.String toString()
        Equivalent to asList().toString()
        Overrides:
        toString in class java.lang.Object
        Returns:
        a string representation
      • setLimit

        public final void setLimit​(int newLimit)
        Ensures that the underlying array can be accessed up to the given upper bound, reallocating and copying the underlying array to expand if necessary. Or, if the given limit is less than the length of the underlying array, causes computations to ignore elements past the given limit.
        Parameters:
        newLimit - the new upper bound
        Throws:
        java.lang.IllegalArgumentException - if newLimit less than zero.