public class ParallelLongArray extends ParallelLongArrayWithBounds
ParallelArray
, but specialized for scalar longs. It additionally
provides a few methods specific to numerical values.
Sample usages. Here is a complete (although naive) prime filter program:
import java.math.BigInteger; import jsr166y.*; import static extra166y.Ops.*; import static extra166y.ParallelLongArray.*; public class Sieve { public static void main(String[] args) { int n = Integer.parseInt(args[0]); // create array of divisors ParallelLongArray a = create(n-1, defaultExecutor()); a.replaceWithMappedIndex(add2); int i = 0; long p = 2; while (p * p < n) { // repeatedly filter a = a.withFilter(notDivisibleBy(p)).all(); p = a.get(++i); } System.out.printf("sieve(%d) = %s%n", n, a); // check result if (!a.withFilter(notProbablePrime).isEmpty()) throw new Error(); } static IntToLong add2 = new IntToLong() { public long op(int i) { return i + 2; } }; static LongPredicate notDivisibleBy(final long p) { return new LongPredicate() { public boolean op(long n) { return n <= p || (n % p) != 0; } }; } static LongPredicate notProbablePrime = new LongPredicate() { private static final int CERTAINTY = 8; public boolean op(long n) { return !BigInteger.valueOf(n).isProbablePrime(CERTAINTY); } }; }
Modifier and Type | Class and Description |
---|---|
static interface |
ParallelLongArray.SummaryStatistics
Summary statistics for a possibly bounded, filtered, and/or
mapped ParallelLongArray.
|
Modifier | Constructor and Description |
---|---|
protected |
ParallelLongArray(ForkJoinPool 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.
|
Modifier and Type | Method and Description |
---|---|
ParallelLongArray |
addAll(long[] other)
Equivalent to asList().addAll but specialized for array
arguments and likely to be more efficient.
|
ParallelLongArray |
addAll(ParallelLongArrayWithLongMapping other)
Appends all (possibly bounded, filtered, or mapped) elements of
the given ParallelDoubleArray, resizing and/or reallocating this
array if necessary.
|
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.
|
static ParallelLongArray |
create(int size,
ForkJoinPool executor)
Creates a new ParallelLongArray using the given executor and
an array of the given size
|
static ParallelLongArray |
createEmpty(int size,
ForkJoinPool 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,
ForkJoinPool 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,
ForkJoinPool executor)
Creates a new ParallelLongArray using the given executor and
initially holding copies of the given
source elements.
|
static ParallelLongArray |
createUsingHandoff(long[] handoff,
ForkJoinPool executor)
Creates a new ParallelLongArray initially using the given array and
executor.
|
ParallelLongArray |
cumulate(Ops.LongReducer reducer,
long base)
Replaces each element with the running cumulation of applying
the given reducer.
|
ParallelLongArray |
cumulateSum()
Replaces each element with the running sum
|
static ForkJoinPool |
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
|
ForkJoinPool |
getExecutor()
Returns the executor used for computations
|
boolean |
hasAllEqualElements(ParallelLongArrayWithLongMapping other)
Returns true if all elements at the same relative positions
of this and other array are equal.
|
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
|
ParallelLongArray |
removeAll(Ops.LongPredicate selector)
Removes from the array all elements for which the given
selector holds.
|
ParallelLongArray |
removeConsecutiveDuplicates()
Removes consecutive elements that are equal,
shifting others leftward, and possibly decreasing size.
|
ParallelLongArray |
replaceWithGeneratedValue(Ops.LongGenerator generator)
Replaces elements with the results of applying the given
generator.
|
ParallelLongArray |
replaceWithMappedIndex(Ops.IntAndLongToLong op)
Replaces elements with the results of applying the given
mapping to each index and current element value
|
ParallelLongArray |
replaceWithMappedIndex(Ops.IntToLong op)
Replaces elements with the results of applying the given
op to their indices.
|
ParallelLongArray |
replaceWithMapping(Ops.BinaryLongOp combiner,
long[] other)
Replaces elements with results of applying
op(thisElement, otherElement)
|
ParallelLongArray |
replaceWithMapping(Ops.BinaryLongOp combiner,
ParallelLongArrayWithLongMapping other)
Replaces elements with results of applying
op(thisElement, otherElement)
|
ParallelLongArray |
replaceWithMapping(Ops.LongOp op)
Replaces elements with the results of applying the given op
to their current values.
|
ParallelLongArray |
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.
|
ParallelLongArray |
sort()
Sorts the array, assuming all elements are Comparable.
|
ParallelLongArray |
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()
|
ParallelLongArrayWithBounds |
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).
|
ParallelLongArrayWithFilter |
withFilter(Ops.BinaryLongPredicate selector,
ParallelLongArrayWithLongMapping other)
Returns an operation prefix that causes a method to operate
only on elements for which the given binary selector returns
true
|
ParallelLongArrayWithFilter |
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
|
ParallelLongArrayWithFilter |
withIndexedFilter(Ops.IntAndLongPredicate selector)
Returns an operation prefix that causes a method to operate
only on elements for which the given indexed selector returns
true
|
ParallelLongArrayWithDoubleMapping |
withIndexedMapping(Ops.IntAndLongToDouble 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.
|
ParallelLongArrayWithLongMapping |
withIndexedMapping(Ops.IntAndLongToLong 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.
|
<U> ParallelLongArrayWithMapping<U> |
withIndexedMapping(Ops.IntAndLongToObject<? 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.
|
ParallelLongArrayWithLongMapping |
withMapping(Ops.BinaryLongOp combiner,
ParallelLongArrayWithLongMapping other)
Returns an operation prefix that causes a method to operate
on binary mappings of this array and the other array.
|
ParallelLongArrayWithDoubleMapping |
withMapping(Ops.LongAndDoubleToDouble combiner,
ParallelDoubleArrayWithDoubleMapping other)
Returns an operation prefix that causes a method to operate
on binary mappings of this array and the other array.
|
ParallelLongArrayWithLongMapping |
withMapping(Ops.LongAndDoubleToLong combiner,
ParallelDoubleArrayWithDoubleMapping other)
Returns an operation prefix that causes a method to operate
on binary mappings of this array and the other array.
|
<V> ParallelLongArrayWithMapping<V> |
withMapping(Ops.LongAndDoubleToObject<? extends V> combiner,
ParallelDoubleArrayWithDoubleMapping other)
Returns an operation prefix that causes a method to operate
on binary mappings of this array and the other array.
|
ParallelLongArrayWithDoubleMapping |
withMapping(Ops.LongAndLongToDouble combiner,
ParallelLongArrayWithLongMapping other)
Returns an operation prefix that causes a method to operate
on binary mappings of this array and the other array.
|
<V> ParallelLongArrayWithMapping<V> |
withMapping(Ops.LongAndLongToObject<? extends V> combiner,
ParallelLongArrayWithLongMapping other)
Returns an operation prefix that causes a method to operate
on binary mappings of this array and the other array.
|
<V,W> ParallelLongArrayWithDoubleMapping |
withMapping(Ops.LongAndObjectToDouble<? super V> combiner,
ParallelArrayWithMapping<W,V> other)
Returns an operation prefix that causes a method to operate
on binary mappings of this array and the other array.
|
<V,W> ParallelLongArrayWithLongMapping |
withMapping(Ops.LongAndObjectToLong<? super V> combiner,
ParallelArrayWithMapping<W,V> other)
Returns an operation prefix that causes a method to operate
on binary mappings of this array and the other array.
|
<V,W,X> ParallelLongArrayWithMapping<W> |
withMapping(Ops.LongAndObjectToObject<? super V,? extends W> combiner,
ParallelArrayWithMapping<X,V> other)
Returns an operation prefix that causes a method to operate
on binary mappings of this array and the other array.
|
ParallelLongArrayWithLongMapping |
withMapping(Ops.LongOp op)
Returns an operation prefix that causes a method to operate
on mapped elements of the array using the given op.
|
ParallelLongArrayWithDoubleMapping |
withMapping(Ops.LongToDouble op)
Returns an operation prefix that causes a method to operate
on mapped elements of the array using the given op.
|
<U> ParallelLongArrayWithMapping<U> |
withMapping(Ops.LongToObject<? extends U> op)
Returns an operation prefix that causes a method to operate
on mapped elements of the array using the given op.
|
sequentially
anyIndex, isEmpty
protected ParallelLongArray(ForkJoinPool executor, long[] array, int limit)
create(int, ForkJoinPool)
,
createEmpty(int, ForkJoinPool)
, createUsingHandoff(long[], ForkJoinPool)
or createFromCopy(long[], ForkJoinPool)
.executor
- the executorarray
- the arraylimit
- the upper bound limitpublic static ForkJoinPool defaultExecutor()
public static ParallelLongArray create(int size, ForkJoinPool executor)
size
- the array sizeexecutor
- the executorpublic static ParallelLongArray createUsingHandoff(long[] handoff, ForkJoinPool executor)
handoff
- the arrayexecutor
- the executorpublic static ParallelLongArray createFromCopy(long[] source, ForkJoinPool executor)
source
- the source of initial elementsexecutor
- the executorpublic static ParallelLongArray createFromCopy(int size, long[] source, ForkJoinPool executor)
source
- the source of initial elementssize
- the array sizeexecutor
- the executorpublic static ParallelLongArray createEmpty(int size, ForkJoinPool executor)
asList()
operations.size
- the array sizeexecutor
- the executorpublic ForkJoinPool getExecutor()
public void apply(Ops.LongProcedure procedure)
apply
in class ParallelLongArrayWithLongMapping
procedure
- the procedurepublic long reduce(Ops.LongReducer reducer, long base)
reduce
in class ParallelLongArrayWithLongMapping
reducer
- the reducerbase
- the result for an empty arraypublic ParallelLongArray all()
all
in class ParallelLongArrayWithLongMapping
public ParallelLongArray replaceWithMapping(Ops.LongOp op)
replaceWithMapping
in class ParallelLongArrayWithFilter
op
- the oppublic ParallelLongArray replaceWithMappedIndex(Ops.IntToLong op)
replaceWithMappedIndex
in class ParallelLongArrayWithFilter
op
- the oppublic ParallelLongArray replaceWithMappedIndex(Ops.IntAndLongToLong op)
replaceWithMappedIndex
in class ParallelLongArrayWithFilter
op
- the oppublic ParallelLongArray replaceWithGeneratedValue(Ops.LongGenerator generator)
replaceWithGeneratedValue
in class ParallelLongArrayWithFilter
generator
- the generatorpublic ParallelLongArray replaceWithValue(long value)
replaceWithValue
in class ParallelLongArrayWithFilter
value
- the valuepublic ParallelLongArray replaceWithMapping(Ops.BinaryLongOp combiner, ParallelLongArrayWithLongMapping other)
replaceWithMapping
in class ParallelLongArrayWithFilter
other
- the other arraycombiner
- the combinerpublic ParallelLongArray replaceWithMapping(Ops.BinaryLongOp combiner, long[] other)
replaceWithMapping
in class ParallelLongArrayWithFilter
other
- the other arraycombiner
- the combinerjava.lang.ArrayIndexOutOfBoundsException
- if other array has
fewer elements than this array.public int indexOf(long target)
target
- the element to search forpublic int binarySearch(long target)
target
- the element to search forpublic int binarySearch(long target, Ops.LongComparator comparator)
target
- the element to search forcomparator
- the comparatorpublic ParallelLongArray.SummaryStatistics summary(Ops.LongComparator comparator)
summary
in class ParallelLongArrayWithLongMapping
comparator
- the comparator to use for
locating minimum and maximum elementspublic ParallelLongArray.SummaryStatistics summary()
summary
in class ParallelLongArrayWithLongMapping
public long min(Ops.LongComparator comparator)
min
in class ParallelLongArrayWithLongMapping
comparator
- the comparatorpublic long min()
min
in class ParallelLongArrayWithLongMapping
public long max(Ops.LongComparator comparator)
max
in class ParallelLongArrayWithLongMapping
comparator
- the comparatorpublic long max()
max
in class ParallelLongArrayWithLongMapping
public ParallelLongArray cumulate(Ops.LongReducer reducer, long base)
reducer
- the reducerbase
- the result for an empty arraypublic long precumulate(Ops.LongReducer reducer, long base)
reducer
- the reducerbase
- the result for an empty arraypublic ParallelLongArray sort(Ops.LongComparator comparator)
comparator
- the comparator to usepublic ParallelLongArray sort()
java.lang.ClassCastException
- if any element is not Comparable.public ParallelLongArray removeConsecutiveDuplicates()
public ParallelLongArray addAll(long[] other)
other
- the elements to addpublic ParallelLongArray addAll(ParallelLongArrayWithLongMapping other)
other
- the elements to addpublic ParallelLongArray allUniqueElements()
allUniqueElements
in class ParallelLongArrayWithFilter
public ParallelLongArray removeAll(Ops.LongPredicate selector)
selector
- the selectorpublic boolean hasAllEqualElements(ParallelLongArrayWithLongMapping other)
hasAllEqualElements
in class ParallelLongArrayWithFilter
other
- the other arraypublic long sum()
sum
in class ParallelLongArrayWithLongMapping
public ParallelLongArray cumulateSum()
public long precumulateSum()
public ParallelLongArrayWithBounds withBounds(int firstIndex, int upperBound)
firstIndex
- the lower bound (inclusive)upperBound
- the upper bound (exclusive)public ParallelLongArrayWithFilter withFilter(Ops.LongPredicate selector)
selector
- the selectorpublic ParallelLongArrayWithFilter withFilter(Ops.BinaryLongPredicate selector, ParallelLongArrayWithLongMapping other)
withFilter
in class ParallelLongArrayWithFilter
selector
- the selectorpublic ParallelLongArrayWithFilter withIndexedFilter(Ops.IntAndLongPredicate selector)
selector
- the selectorpublic <U> ParallelLongArrayWithMapping<U> withMapping(Ops.LongToObject<? extends U> op)
op
- the oppublic ParallelLongArrayWithLongMapping withMapping(Ops.LongOp op)
op
- the oppublic ParallelLongArrayWithDoubleMapping withMapping(Ops.LongToDouble op)
op
- the oppublic <V,W,X> ParallelLongArrayWithMapping<W> withMapping(Ops.LongAndObjectToObject<? super V,? extends W> combiner, ParallelArrayWithMapping<X,V> other)
withMapping
in class ParallelLongArrayWithLongMapping
combiner
- the combinerother
- the other arrayjava.lang.IllegalArgumentException
- if other array is a
filtered view (all filters must precede all mappings).public <V> ParallelLongArrayWithMapping<V> withMapping(Ops.LongAndDoubleToObject<? extends V> combiner, ParallelDoubleArrayWithDoubleMapping other)
withMapping
in class ParallelLongArrayWithLongMapping
combiner
- the combinerother
- the other arrayjava.lang.IllegalArgumentException
- if other array is a
filtered view (all filters must precede all mappings).public <V> ParallelLongArrayWithMapping<V> withMapping(Ops.LongAndLongToObject<? extends V> combiner, ParallelLongArrayWithLongMapping other)
withMapping
in class ParallelLongArrayWithLongMapping
combiner
- the combinerother
- the other arrayjava.lang.IllegalArgumentException
- if other array is a
filtered view (all filters must precede all mappings).public <V,W> ParallelLongArrayWithDoubleMapping withMapping(Ops.LongAndObjectToDouble<? super V> combiner, ParallelArrayWithMapping<W,V> other)
withMapping
in class ParallelLongArrayWithLongMapping
combiner
- the combinerother
- the other arrayjava.lang.IllegalArgumentException
- if other array is a
filtered view (all filters must precede all mappings).public ParallelLongArrayWithDoubleMapping withMapping(Ops.LongAndDoubleToDouble combiner, ParallelDoubleArrayWithDoubleMapping other)
withMapping
in class ParallelLongArrayWithLongMapping
combiner
- the combinerother
- the other arrayjava.lang.IllegalArgumentException
- if other array is a
filtered view (all filters must precede all mappings).public ParallelLongArrayWithDoubleMapping withMapping(Ops.LongAndLongToDouble combiner, ParallelLongArrayWithLongMapping other)
withMapping
in class ParallelLongArrayWithLongMapping
combiner
- the combinerother
- the other arrayjava.lang.IllegalArgumentException
- if other array is a
filtered view (all filters must precede all mappings).public <V,W> ParallelLongArrayWithLongMapping withMapping(Ops.LongAndObjectToLong<? super V> combiner, ParallelArrayWithMapping<W,V> other)
withMapping
in class ParallelLongArrayWithLongMapping
combiner
- the combinerother
- the other arrayjava.lang.IllegalArgumentException
- if other array is a
filtered view (all filters must precede all mappings).public ParallelLongArrayWithLongMapping withMapping(Ops.LongAndDoubleToLong combiner, ParallelDoubleArrayWithDoubleMapping other)
withMapping
in class ParallelLongArrayWithLongMapping
combiner
- the combinerother
- the other arrayjava.lang.IllegalArgumentException
- if other array is a
filtered view (all filters must precede all mappings).public ParallelLongArrayWithLongMapping withMapping(Ops.BinaryLongOp combiner, ParallelLongArrayWithLongMapping other)
withMapping
in class ParallelLongArrayWithLongMapping
combiner
- the combinerother
- the other arrayjava.lang.IllegalArgumentException
- if other array is a
filtered view (all filters must precede all mappings).public <U> ParallelLongArrayWithMapping<U> withIndexedMapping(Ops.IntAndLongToObject<? extends U> mapper)
mapper
- the mapperpublic ParallelLongArrayWithDoubleMapping withIndexedMapping(Ops.IntAndLongToDouble mapper)
mapper
- the mapperpublic ParallelLongArrayWithLongMapping withIndexedMapping(Ops.IntAndLongToLong mapper)
mapper
- the mapperpublic java.util.Iterator<java.lang.Long> iterator()
asList()
.public java.util.List<java.lang.Long> asList()
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.public int size()
setLimit(int)
), or the length of the array otherwise.size
in class AbstractParallelAnyArray
public long[] getArray()
public long get(int i)
i
- the indexpublic void set(int i, long x)
i
- the indexx
- the valuepublic java.lang.String toString()
toString
in class java.lang.Object
public final void setLimit(int newLimit)
newLimit
- the new upper boundjava.lang.IllegalArgumentException
- if newLimit less than zero.