jsr166y.forkjoin
Class ParallelLongArray

java.lang.Object
  extended by jsr166y.forkjoin.ParallelLongArray

public class ParallelLongArray
extends java.lang.Object

An array of longs supporting parallel operations. This class provides methods supporting the same operations as ParallelArray, but specialized for scalar longs. It additionally provides a few methods specific to numerical values.


Nested Class Summary
static interface ParallelLongArray.SummaryStatistics
          Summary statistics for a possibly bounded, filtered, and/or mapped ParallelLongArray.
static class ParallelLongArray.WithBounds
          A restriction of parallel array operations to apply only within a given range of indices.
static class ParallelLongArray.WithDoubleMapping
          A modifier for parallel array operations to apply to mappings of elements to doubles, not to the elements themselves
static class ParallelLongArray.WithFilter
          A restriction of parallel array operations to apply only to elements for which a selector returns true
static class ParallelLongArray.WithLongMapping
          A modifier for parallel array operations to apply to mappings of elements to longs, not to the elements themselves
static class ParallelLongArray.WithMapping<U>
          A modifier for parallel array operations to apply to mappings of elements, not to the elements themselves
 
Constructor Summary
protected ParallelLongArray(ForkJoinExecutor executor, long[] array, int limit)
          Constructor for use by subclasses to create a new ParallelLongArray using the given executor, and initially using the supplied array, with effective size bound by the given limit.
 
Method Summary
 void addAll(long[] other)
          Equivalent to AsList.addAll but specialized for array arguments and likely to be more efficient.
 void addAll(ParallelLongArray.WithBounds other)
          Equivalent to AsList.addAll but specialized for ParallelLongArray arguments and likely to be more efficient.
 void addAll(ParallelLongArray other)
          Equivalent to AsList.addAll but specialized for ParallelLongArray arguments and likely to be more efficient.
 ParallelLongArray all()
          Returns a new ParallelLongArray holding all elements
 ParallelLongArray allUniqueElements()
          Returns a new ParallelLongArray containing only the unique elements of this array (that is, without any duplicates).
 void apply(Ops.LongProcedure procedure)
          Applies the given procedure to elements
 java.util.List<java.lang.Long> asList()
          Returns a view of this ParallelLongArray as a List.
 int binarySearch(long target)
          Assuming this array is sorted, returns the index of an element equal to given target, or -1 if not present.
 int binarySearch(long target, Ops.LongComparator 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.
 ParallelLongArray combine(long[] other, Ops.LongReducer combiner)
          Returns a ParallelLongArray containing results of applying combine(thisElement, otherElement) for each element.
<U,V> ParallelLongArray
combine(ParallelLongArray.WithBounds other, Ops.LongReducer combiner)
          Returns a ParallelLongArray containing results of applying combine(thisElement, otherElement) for each element.
<U,V> ParallelLongArray
combine(ParallelLongArray other, Ops.LongReducer combiner)
          Returns a ParallelLongArray containing results of applying combine(thisElement, otherElement) for each element.
static ParallelLongArray create(int size, ForkJoinExecutor executor)
          Creates a new ParallelLongArray using the given executor and an array of the given size
static ParallelLongArray createEmpty(int size, ForkJoinExecutor executor)
          Creates a new ParallelLongArray 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.
static ParallelLongArray createFromCopy(int size, long[] source, ForkJoinExecutor executor)
          Creates a new ParallelLongArray using an array of the given size, initially holding copies of the given source truncated or padded with zeros to obtain the specified length.
static ParallelLongArray createFromCopy(long[] source, ForkJoinExecutor executor)
          Creates a new ParallelLongArray using the given executor and initially holding copies of the given source elements.
static ParallelLongArray createUsingHandoff(long[] handoff, ForkJoinExecutor executor)
          Creates a new ParallelLongArray initially using the given array and executor.
 void cumulate(Ops.LongReducer reducer, long base)
          Replaces each element with the running cumulation of applying the given reducer.
 void cumulateSum()
          Replaces each element with the running sum
static ForkJoinExecutor defaultExecutor()
          Returns a common default executor for use in ParallelArrays.
 long get(int i)
          Returns the element of the array at the given index
 long[] getArray()
          Returns the underlying array used for computations
 ForkJoinExecutor getExecutor()
          Returns the executor used for computations
 int indexOf(long target)
          Returns the index of some element equal to given target, or -1 if not present
 java.util.Iterator<java.lang.Long> iterator()
          Returns an iterator stepping through each element of the array up to the current limit.
 long max()
          Returns the maximum element, or Long.MIN_VALUE if empty
 long max(Ops.LongComparator comparator)
          Returns the maximum element, or Long.MIN_VALUE if empty
 long min()
          Returns the minimum element, or Long.MAX_VALUE if empty,
 long min(Ops.LongComparator comparator)
          Returns the minimum element, or Long.MAX_VALUE if empty
 long precumulate(Ops.LongReducer reducer, long base)
          Replaces each element with the cumulation of applying the given reducer to all previous values, and returns the total reduction.
 long precumulateSum()
          Replaces each element with its prefix sum
 long reduce(Ops.LongReducer reducer, long base)
          Returns reduction of elements
 void removeConsecutiveDuplicates()
          Removes consecutive elements that are equal, shifting others leftward, and possibly decreasing size.
 void replaceWithCombination(long[] other, Ops.LongReducer combiner)
          Replaces elements with results of applying combine(thisElement, otherElement)
 void replaceWithCombination(ParallelLongArray.WithBounds other, Ops.LongReducer combiner)
          Replaces elements with results of applying combine(thisElement, otherElement)
 void replaceWithCombination(ParallelLongArray other, Ops.LongReducer combiner)
          Replaces elements with results of applying combine(thisElement, otherElement)
 void replaceWithGeneratedValue(Ops.LongGenerator generator)
          Replaces elements with the results of applying the given generator.
 void replaceWithMappedIndex(Ops.MapperFromIntToLong mapper)
          Replaces elements with the results of applying the given mapper to their indices.
 void replaceWithTransform(Ops.LongMapper mapper)
          Replaces elements with the results of applying the given mapper to their current values.
 void replaceWithValue(long value)
          Replaces elements with the given value.
 void set(int i, long x)
          Sets the element of the array at the given index to the given value
 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.
 int size()
          Returns the effective size of the underlying array.
 void sort()
          Sorts the array, assuming all elements are Comparable.
 void sort(Ops.LongComparator comparator)
          Sorts the array.
 long sum()
          Returns the sum of elements
 ParallelLongArray.SummaryStatistics summary()
          Returns summary statistics, using natural comparator
 ParallelLongArray.SummaryStatistics summary(Ops.LongComparator comparator)
          Returns summary statistics, using the given comparator to locate minimum and maximum elements.
 java.lang.String toString()
          Equivalent to asList().toString()
 ParallelLongArray.WithBounds 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).
 ParallelLongArray.WithFilter withFilter(Ops.LongPredicate 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
 ParallelLongArray.WithLongMapping withMapping(Ops.LongMapper mapper)
          Returns an operation prefix that causes a method to operate on mapped elements of the array using the given mapper.
<U> ParallelLongArray.WithMapping<U>
withMapping(Ops.MapperFromLong<? extends U> mapper)
          Returns an operation prefix that causes a method to operate on mapped elements of the array using the given mapper.
 ParallelLongArray.WithDoubleMapping withMapping(Ops.MapperFromLongToDouble mapper)
          Returns an operation prefix that causes a method to operate on mapped elements of the array using the given mapper.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ParallelLongArray

protected ParallelLongArray(ForkJoinExecutor executor,
                            long[] array,
                            int limit)
Constructor for use by subclasses to create a new ParallelLongArray using the given executor, and initially using the supplied array, with effective size bound by the given limit. This constructor is designed to enable extensions via subclassing. To create a ParallelLongArray, use create(int, jsr166y.forkjoin.ForkJoinExecutor), createEmpty(int, jsr166y.forkjoin.ForkJoinExecutor), createUsingHandoff(long[], jsr166y.forkjoin.ForkJoinExecutor) or createFromCopy(long[], jsr166y.forkjoin.ForkJoinExecutor).

Parameters:
executor - the executor
array - the array
limit - the upper bound limit
Method Detail

defaultExecutor

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

Returns:
the executor

create

public static ParallelLongArray create(int size,
                                       ForkJoinExecutor executor)
Creates a new ParallelLongArray using the given executor and an array of the given size

Parameters:
size - the array size
executor - the executor

createUsingHandoff

public static ParallelLongArray createUsingHandoff(long[] handoff,
                                                   ForkJoinExecutor executor)
Creates a new ParallelLongArray initially using the given array and executor. In general, the handed off array should not be used for other purposes once constructing this ParallelLongArray. 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 ParallelLongArray createFromCopy(long[] source,
                                               ForkJoinExecutor executor)
Creates a new ParallelLongArray 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 ParallelLongArray createFromCopy(int size,
                                               long[] source,
                                               ForkJoinExecutor executor)
Creates a new ParallelLongArray 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 ParallelLongArray createEmpty(int size,
                                            ForkJoinExecutor executor)
Creates a new ParallelLongArray 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 ForkJoinExecutor getExecutor()
Returns the executor used for computations

Returns:
the executor

apply

public void apply(Ops.LongProcedure procedure)
Applies the given procedure to elements

Parameters:
procedure - the procedure

reduce

public long reduce(Ops.LongReducer reducer,
                   long base)
Returns reduction of elements

Parameters:
reducer - the reducer
base - the result for an empty array
Returns:
reduction

all

public ParallelLongArray all()
Returns a new ParallelLongArray holding all elements

Returns:
a new ParallelLongArray holding all elements

combine

public ParallelLongArray combine(long[] other,
                                 Ops.LongReducer combiner)
Returns a ParallelLongArray containing results of applying combine(thisElement, otherElement) for each element.

Parameters:
other - the other array
combiner - the combiner
Returns:
the array of mappings
Throws:
java.lang.ArrayIndexOutOfBoundsException - if other array is shorter than this array.

combine

public <U,V> ParallelLongArray combine(ParallelLongArray other,
                                       Ops.LongReducer combiner)
Returns a ParallelLongArray containing results of applying combine(thisElement, otherElement) for each element.

Parameters:
other - the other array
combiner - the combiner
Returns:
the array of mappings
Throws:
java.lang.ArrayIndexOutOfBoundsException - if other array is not the same length as this array.

combine

public <U,V> ParallelLongArray combine(ParallelLongArray.WithBounds other,
                                       Ops.LongReducer combiner)
Returns a ParallelLongArray containing results of applying combine(thisElement, otherElement) for each element.

Parameters:
other - the other array segment
combiner - the combiner
Returns:
the array of mappings
Throws:
java.lang.ArrayIndexOutOfBoundsException - if other segment is shorter than this array.

replaceWithTransform

public void replaceWithTransform(Ops.LongMapper mapper)
Replaces elements with the results of applying the given mapper to their current values.

Parameters:
mapper - the mapper

replaceWithMappedIndex

public void replaceWithMappedIndex(Ops.MapperFromIntToLong mapper)
Replaces elements with the results of applying the given mapper to their indices.

Parameters:
mapper - the mapper

replaceWithGeneratedValue

public void replaceWithGeneratedValue(Ops.LongGenerator generator)
Replaces elements with the results of applying the given generator. For example, to fill the array with uniform random values, use replaceWithGeneratedValue(Ops.longRandom())

Parameters:
generator - the generator

replaceWithValue

public void replaceWithValue(long value)
Replaces elements with the given value.

Parameters:
value - the value

replaceWithCombination

public void replaceWithCombination(ParallelLongArray other,
                                   Ops.LongReducer combiner)
Replaces elements with results of applying combine(thisElement, otherElement)

Parameters:
other - the other array
combiner - the combiner
Throws:
java.lang.ArrayIndexOutOfBoundsException - if other array has fewer elements than this array.

replaceWithCombination

public void replaceWithCombination(long[] other,
                                   Ops.LongReducer combiner)
Replaces elements with results of applying combine(thisElement, otherElement)

Parameters:
other - the other array
combiner - the combiner
Throws:
java.lang.ArrayIndexOutOfBoundsException - if other array has fewer elements than this array.

replaceWithCombination

public void replaceWithCombination(ParallelLongArray.WithBounds other,
                                   Ops.LongReducer combiner)
Replaces elements with results of applying combine(thisElement, otherElement)

Parameters:
other - the other array segment
combiner - the combiner
Throws:
java.lang.ArrayIndexOutOfBoundsException - if other segment has fewer elements.than this array,

indexOf

public int indexOf(long 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(long 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(long target,
                        Ops.LongComparator 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

summary

public ParallelLongArray.SummaryStatistics summary(Ops.LongComparator comparator)
Returns summary statistics, using the given comparator to locate minimum and maximum elements.

Parameters:
comparator - the comparator to use for locating minimum and maximum elements
Returns:
the summary.

summary

public ParallelLongArray.SummaryStatistics summary()
Returns summary statistics, using natural comparator

Returns:
the summary.

min

public long min(Ops.LongComparator comparator)
Returns the minimum element, or Long.MAX_VALUE if empty

Parameters:
comparator - the comparator
Returns:
minimum element, or Long.MAX_VALUE if empty

min

public long min()
Returns the minimum element, or Long.MAX_VALUE if empty,

Returns:
minimum element, or Long.MAX_VALUE if empty

max

public long max(Ops.LongComparator comparator)
Returns the maximum element, or Long.MIN_VALUE if empty

Parameters:
comparator - the comparator
Returns:
maximum element, or Long.MIN_VALUE if empty

max

public long max()
Returns the maximum element, or Long.MIN_VALUE if empty

Returns:
maximum element, or Long.MIN_VALUE if empty

cumulate

public void cumulate(Ops.LongReducer reducer,
                     long 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

precumulate

public long precumulate(Ops.LongReducer reducer,
                        long 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 void sort(Ops.LongComparator 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

sort

public void 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.

Throws:
java.lang.ClassCastException - if any element is not Comparable.

removeConsecutiveDuplicates

public void 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 ParallelLongArray contains a set of unique elements.


allUniqueElements

public ParallelLongArray allUniqueElements()
Returns a new ParallelLongArray containing only the unique elements of this array (that is, without any duplicates).

Returns:
the new ParallelLongArray

sum

public long sum()
Returns the sum of elements

Returns:
the sum of elements

cumulateSum

public void cumulateSum()
Replaces each element with the running sum


precumulateSum

public long precumulateSum()
Replaces each element with its prefix sum

Returns:
the total sum

withBounds

public ParallelLongArray.WithBounds 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 ParallelLongArray.WithFilter withFilter(Ops.LongPredicate 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

withMapping

public <U> ParallelLongArray.WithMapping<U> withMapping(Ops.MapperFromLong<? extends U> mapper)
Returns an operation prefix that causes a method to operate on mapped elements of the array using the given mapper.

Parameters:
mapper - the mapper
Returns:
operation prefix

withMapping

public ParallelLongArray.WithLongMapping withMapping(Ops.LongMapper mapper)
Returns an operation prefix that causes a method to operate on mapped elements of the array using the given mapper.

Parameters:
mapper - the mapper
Returns:
operation prefix

withMapping

public ParallelLongArray.WithDoubleMapping withMapping(Ops.MapperFromLongToDouble mapper)
Returns an operation prefix that causes a method to operate on mapped elements of the array using the given mapper.

Parameters:
mapper - the mapper
Returns:
operation prefix

iterator

public java.util.Iterator<java.lang.Long> 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.Long> asList()
Returns a view of this ParallelLongArray 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 ParallelLongArray. 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.

Returns:
the effective size of array

getArray

public long[] getArray()
Returns the underlying array used for computations

Returns:
the array

get

public long 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,
                long 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

addAll

public void addAll(long[] other)
Equivalent to AsList.addAll but specialized for array arguments and likely to be more efficient.

Parameters:
other - the elements to add

addAll

public void addAll(ParallelLongArray other)
Equivalent to AsList.addAll but specialized for ParallelLongArray arguments and likely to be more efficient.

Parameters:
other - the elements to add

addAll

public void addAll(ParallelLongArray.WithBounds other)
Equivalent to AsList.addAll but specialized for ParallelLongArray arguments and likely to be more efficient.

Parameters:
other - the elements to add

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.