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.
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); } }; }
Modifiers | Name | Description |
---|---|---|
class |
ParallelLongArray.AsList |
|
class |
ParallelLongArray.ListIter |
|
static class |
ParallelLongArray.ParallelLongArrayIterator |
|
static interface |
ParallelLongArray.SummaryStatistics |
Summary statistics for a possibly bounded, filtered, and/or mapped ParallelLongArray. |
Modifiers | Name | Description |
---|---|---|
ParallelLongArray.AsList |
listView |
Constructor and description |
---|
protected ParallelLongArray
(java.util.concurrent.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. |
ParallelLongArray
(java.util.concurrent.ForkJoinPool executor, long[] array) Trusted internal version of protected constructor. |
Type | Name 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 |
appendElement(long e) |
void |
apply(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, 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, java.util.concurrent.ForkJoinPool executor) Creates a new ParallelLongArray using the given executor and an array of the given size. |
static ParallelLongArray |
createEmpty(int size, java.util.concurrent.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 ParallelLongArray.asList operations. |
static ParallelLongArray |
createFromCopy(long[] source, java.util.concurrent.ForkJoinPool executor) Creates a new ParallelLongArray using the given executor and initially holding copies of the given source elements. |
static ParallelLongArray |
createFromCopy(int size, long[] source, java.util.concurrent.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 |
createUsingHandoff(long[] handoff, java.util.concurrent.ForkJoinPool executor) Creates a new ParallelLongArray initially using the given array and executor. |
ParallelLongArray |
cumulate(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 java.util.concurrent.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. |
java.util.concurrent.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. |
void |
insertElementAt(int index, long e) |
void |
insertSlotsAt(int index, int len) Makes len slots available at index. |
java.util.Iterator<java.lang.Long> |
iterator() Returns an iterator stepping through each element of the array up to the current limit. |
long |
max(LongComparator comparator) Returns the maximum element, or Long.MIN_VALUE if empty. |
long |
max() Returns the maximum element, or Long.MIN_VALUE if empty. |
long |
min(LongComparator comparator) Returns the minimum element, or Long.MAX_VALUE if empty. |
long |
min() Returns the minimum element, or Long.MAX_VALUE if empty. |
long |
precumulate(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(LongReducer reducer, long base) Returns reduction of elements. |
ParallelLongArray |
removeAll(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. |
void |
removeSlotAt(int index) |
void |
removeSlotsAt(int fromIndex, int toIndex) |
void |
replaceElementsParallelLongArrayWith(long[] a) |
ParallelLongArray |
replaceWithGeneratedValue(LongGenerator generator) Replaces elements with the results of applying the given generator. |
ParallelLongArray |
replaceWithMappedIndex(IntToLong op) Replaces elements with the results of applying the given op to their indices. |
ParallelLongArray |
replaceWithMappedIndex(IntAndLongToLong op) Replaces elements with the results of applying the given mapping to each index and current element value. |
ParallelLongArray |
replaceWithMapping(LongOp op) Replaces elements with the results of applying the given op to their current values. |
ParallelLongArray |
replaceWithMapping(BinaryLongOp combiner, ParallelLongArrayWithLongMapping other) Replaces elements with results of applying op(thisElement, otherElement) . |
ParallelLongArray |
replaceWithMapping(BinaryLongOp combiner, long[] other) Replaces elements with results of applying op(thisElement, otherElement) . |
ParallelLongArray |
replaceWithValue(long value) Replaces elements with the given value. |
void |
resizeArray(int newCap) |
int |
seqIndexOf(long target) |
int |
seqLastIndexOf(long target) |
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(LongComparator comparator) Sorts the array. |
ParallelLongArray |
sort() Sorts the array, assuming all elements are Comparable. |
long |
sum() Returns the sum of elements. |
SummaryStatistics |
summary(LongComparator comparator) Returns summary statistics, using the given comparator to locate minimum and maximum elements. |
SummaryStatistics |
summary() Returns summary statistics, using natural comparator. |
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(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 |
withFilter(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 |
withIndexedFilter(IntAndLongPredicate selector) Returns an operation prefix that causes a method to operate only on elements for which the given indexed selector returns true. |
ParallelLongArrayWithMapping<U> |
withIndexedMapping(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. |
ParallelLongArrayWithDoubleMapping |
withIndexedMapping(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(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. |
ParallelLongArrayWithMapping<U> |
withMapping(LongToObject<? extends U> op) Returns an operation prefix that causes a method to operate on mapped elements of the array using the given op. |
ParallelLongArrayWithLongMapping |
withMapping(LongOp op) Returns an operation prefix that causes a method to operate on mapped elements of the array using the given op. |
ParallelLongArrayWithDoubleMapping |
withMapping(LongToDouble op) Returns an operation prefix that causes a method to operate on mapped elements of the array using the given op. |
ParallelLongArrayWithMapping<W> |
withMapping(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. |
ParallelLongArrayWithMapping<V> |
withMapping(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. |
ParallelLongArrayWithMapping<V> |
withMapping(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. |
ParallelLongArrayWithDoubleMapping |
withMapping(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. |
ParallelLongArrayWithDoubleMapping |
withMapping(LongAndDoubleToDouble 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(LongAndLongToDouble combiner, ParallelLongArrayWithLongMapping other) Returns an operation prefix that causes a method to operate on binary mappings of this array and the other array. |
ParallelLongArrayWithLongMapping |
withMapping(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. |
ParallelLongArrayWithLongMapping |
withMapping(LongAndDoubleToLong 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(BinaryLongOp combiner, ParallelLongArrayWithLongMapping other) Returns an operation prefix that causes a method to operate on binary mappings of this array and the other array. |
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, createEmpty, createUsingHandoff or createFromCopy.
executor
- the executorarray
- the arraylimit
- the upper bound limitTrusted internal version of protected constructor.
Equivalent to asList().addAll
but specialized for array
arguments and likely to be more efficient.
other
- the elements to addAppends all (possibly bounded, filtered, or mapped) elements of the given ParallelDoubleArray, resizing and/or reallocating this array if necessary.
other
- the elements to addReturns a new ParallelLongArray holding all elements.
Returns a new ParallelLongArray containing only the unique elements of this array (that is, without any duplicates).
Applies the given procedure to elements.
procedure
- the procedureReturns a view of this ParallelLongArray as a List. This List has the same structural and performance characteristics as java.util.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.
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.
target
- the element to search forAssuming 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.
target
- the element to search forcomparator
- the comparatorCreates a new ParallelLongArray using the given executor and an array of the given size.
size
- the array sizeexecutor
- the executorCreates 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 ParallelLongArray.asList operations.
size
- the array sizeexecutor
- the executorCreates a new ParallelLongArray using the given executor and initially holding copies of the given source elements.
source
- the source of initial elementsexecutor
- the executorCreates 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.
source
- the source of initial elementssize
- the array sizeexecutor
- the executorCreates 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.
handoff
- the arrayexecutor
- the executor 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
).
reducer
- the reducerbase
- the result for an empty arrayReplaces each element with the running sum.
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 element of the array at the given index.
i
- the indexReturns the underlying array used for computations.
Returns the executor used for computations.
Returns true if all elements at the same relative positions of this and other array are equal.
other
- the other arrayReturns the index of some element equal to given target, or -1 if not present.
target
- the element to search forMakes len slots available at index.
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 the maximum element, or Long.MIN_VALUE if empty.
comparator
- the comparatorReturns the maximum element, or Long.MIN_VALUE if empty.
Returns the minimum element, or Long.MAX_VALUE if empty.
comparator
- the comparatorReturns the minimum element, or Long.MAX_VALUE if empty.
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
).
reducer
- the reducerbase
- the result for an empty arrayReplaces each element with its prefix sum.
Returns reduction of elements.
reducer
- the reducerbase
- the result for an empty arrayRemoves from the array all elements for which the given selector holds.
selector
- the selectorRemoves 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.
Replaces elements with the results of applying the given
generator. For example, to fill the array with uniform random
values, use
replaceWithGeneratedValue(Ops.longRandom())
.
generator
- the generatorReplaces elements with the results of applying the given op to their indices.
op
- the opReplaces elements with the results of applying the given mapping to each index and current element value.
op
- the opReplaces elements with the results of applying the given op to their current values.
op
- the op Replaces elements with results of applying
op(thisElement, otherElement)
.
other
- the other arraycombiner
- the combiner Replaces elements with results of applying
op(thisElement, otherElement)
.
other
- the other arraycombiner
- the combinerReplaces elements with the given value.
value
- the valueSets the element of the array at the given index to the given value.
i
- the indexx
- the valueEnsures 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.
newLimit
- the new upper boundReturns the effective size of the underlying array. The effective size is the current limit, if used (see setLimit), or the length of the array otherwise.
Sorts the array. Unlike Arrays.sort, this sort does not guarantee that elements with equal keys maintain their relative position in the array.
comparator
- the comparator to useSorts 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 the sum of elements.
Returns summary statistics, using the given comparator to locate minimum and maximum elements.
comparator
- the comparator to use for
locating minimum and maximum elementsReturns summary statistics, using natural comparator.
Equivalent to asList().toString()
.
Returns an operation prefix that causes a method to operate only on the elements of the array between firstIndex (inclusive) and upperBound (exclusive).
firstIndex
- the lower bound (inclusive)upperBound
- the upper bound (exclusive)Returns an operation prefix that causes a method to operate only on the elements of the array for which the given selector returns true.
selector
- the selectorReturns an operation prefix that causes a method to operate only on elements for which the given binary selector returns true.
selector
- the selectorReturns an operation prefix that causes a method to operate only on elements for which the given indexed selector returns true.
selector
- the selectorReturns 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.
mapper
- the mapperReturns 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.
mapper
- the mapperReturns 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.
mapper
- the mapperReturns an operation prefix that causes a method to operate on mapped elements of the array using the given op.
op
- the opReturns an operation prefix that causes a method to operate on mapped elements of the array using the given op.
op
- the opReturns an operation prefix that causes a method to operate on mapped elements of the array using the given op.
op
- the opReturns an operation prefix that causes a method to operate on binary mappings of this array and the other array.
combiner
- the combinerother
- the other arrayReturns an operation prefix that causes a method to operate on binary mappings of this array and the other array.
combiner
- the combinerother
- the other arrayReturns an operation prefix that causes a method to operate on binary mappings of this array and the other array.
combiner
- the combinerother
- the other arrayReturns an operation prefix that causes a method to operate on binary mappings of this array and the other array.
combiner
- the combinerother
- the other arrayReturns an operation prefix that causes a method to operate on binary mappings of this array and the other array.
combiner
- the combinerother
- the other arrayReturns an operation prefix that causes a method to operate on binary mappings of this array and the other array.
combiner
- the combinerother
- the other arrayReturns an operation prefix that causes a method to operate on binary mappings of this array and the other array.
combiner
- the combinerother
- the other arrayReturns an operation prefix that causes a method to operate on binary mappings of this array and the other array.
combiner
- the combinerother
- the other arrayReturns an operation prefix that causes a method to operate on binary mappings of this array and the other array.
combiner
- the combinerother
- the other arrayCopyright © 2008–2014 Václav Pech. All Rights Reserved.