com.landawn.abacus.util.stream.ParallelArrayFloatStream Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of abacus-android Show documentation
Show all versions of abacus-android Show documentation
A general programming library in Java
/*
* Copyright (C) 2016 HaiYang Li
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package com.landawn.abacus.util.stream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import com.landawn.abacus.util.ContinuableFuture;
import com.landawn.abacus.util.FloatSummaryStatistics;
import com.landawn.abacus.util.Holder;
import com.landawn.abacus.util.MutableBoolean;
import com.landawn.abacus.util.MutableInt;
import com.landawn.abacus.util.N;
import com.landawn.abacus.util.Nth;
import com.landawn.abacus.util.OptionalDouble;
import com.landawn.abacus.util.OptionalFloat;
import com.landawn.abacus.util.Pair;
import com.landawn.abacus.util.Try;
import com.landawn.abacus.util.function.BiConsumer;
import com.landawn.abacus.util.function.BinaryOperator;
import com.landawn.abacus.util.function.Consumer;
import com.landawn.abacus.util.function.FloatBiFunction;
import com.landawn.abacus.util.function.FloatBinaryOperator;
import com.landawn.abacus.util.function.FloatConsumer;
import com.landawn.abacus.util.function.FloatFunction;
import com.landawn.abacus.util.function.FloatPredicate;
import com.landawn.abacus.util.function.FloatToDoubleFunction;
import com.landawn.abacus.util.function.FloatToIntFunction;
import com.landawn.abacus.util.function.FloatToLongFunction;
import com.landawn.abacus.util.function.FloatTriFunction;
import com.landawn.abacus.util.function.FloatUnaryOperator;
import com.landawn.abacus.util.function.Function;
import com.landawn.abacus.util.function.ObjFloatConsumer;
import com.landawn.abacus.util.function.Predicate;
import com.landawn.abacus.util.function.Supplier;
import com.landawn.abacus.util.function.ToDoubleFunction;
import com.landawn.abacus.util.function.ToFloatFunction;
import com.landawn.abacus.util.function.ToIntFunction;
import com.landawn.abacus.util.function.ToLongFunction;
/**
* This class is a sequential, stateful and immutable stream implementation.
*
* @since 0.8
*
* @author Haiyang Li
*/
final class ParallelArrayFloatStream extends ArrayFloatStream {
private final int maxThreadNum;
private final Splitor splitor;
private volatile ArrayFloatStream sequential;
private volatile Stream boxed;
ParallelArrayFloatStream(final float[] values, final int fromIndex, final int toIndex, final boolean sorted, int maxThreadNum, Splitor splitor,
final Collection closeHandlers) {
super(values, fromIndex, toIndex, sorted, closeHandlers);
this.maxThreadNum = checkMaxThreadNum(maxThreadNum);
this.splitor = splitor == null ? DEFAULT_SPLITOR : splitor;
}
@Override
public FloatStream filter(final FloatPredicate predicate) {
if (maxThreadNum <= 1 || toIndex - fromIndex <= 1) {
return super.filter(predicate);
}
final Stream stream = boxed().filter(new Predicate() {
@Override
public boolean test(Float value) {
return predicate.test(value);
}
});
return new ParallelIteratorFloatStream(stream, false, maxThreadNum, splitor, closeHandlers);
}
@Override
public FloatStream takeWhile(final FloatPredicate predicate) {
if (maxThreadNum <= 1 || toIndex - fromIndex <= 1) {
return super.takeWhile(predicate);
}
final Stream stream = boxed().takeWhile(new Predicate() {
@Override
public boolean test(Float value) {
return predicate.test(value);
}
});
return new ParallelIteratorFloatStream(stream, false, maxThreadNum, splitor, closeHandlers);
}
@Override
public FloatStream dropWhile(final FloatPredicate predicate) {
if (maxThreadNum <= 1 || toIndex - fromIndex <= 1) {
return super.dropWhile(predicate);
}
final Stream stream = boxed().dropWhile(new Predicate() {
@Override
public boolean test(Float value) {
return predicate.test(value);
}
});
return new ParallelIteratorFloatStream(stream, false, maxThreadNum, splitor, closeHandlers);
}
@Override
public FloatStream map(final FloatUnaryOperator mapper) {
if (maxThreadNum <= 1 || toIndex - fromIndex <= 1) {
return super.map(mapper);
}
final FloatStream stream = boxed().mapToFloat(new ToFloatFunction() {
@Override
public float applyAsFloat(Float value) {
return mapper.applyAsFloat(value);
}
});
return new ParallelIteratorFloatStream(stream, false, maxThreadNum, splitor, closeHandlers);
}
@Override
public IntStream mapToInt(final FloatToIntFunction mapper) {
if (maxThreadNum <= 1 || toIndex - fromIndex <= 1) {
return super.mapToInt(mapper);
}
final IntStream stream = boxed().mapToInt(new ToIntFunction() {
@Override
public int applyAsInt(Float value) {
return mapper.applyAsInt(value);
}
});
return new ParallelIteratorIntStream(stream, false, maxThreadNum, splitor, closeHandlers);
}
@Override
public LongStream mapToLong(final FloatToLongFunction mapper) {
if (maxThreadNum <= 1 || toIndex - fromIndex <= 1) {
return super.mapToLong(mapper);
}
final LongStream stream = boxed().mapToLong(new ToLongFunction() {
@Override
public long applyAsLong(Float value) {
return mapper.applyAsLong(value);
}
});
return new ParallelIteratorLongStream(stream, false, maxThreadNum, splitor, closeHandlers);
}
@Override
public DoubleStream mapToDouble(final FloatToDoubleFunction mapper) {
if (maxThreadNum <= 1 || toIndex - fromIndex <= 1) {
return super.mapToDouble(mapper);
}
final DoubleStream stream = boxed().mapToDouble(new ToDoubleFunction() {
@Override
public double applyAsDouble(Float value) {
return mapper.applyAsDouble(value);
}
});
return new ParallelIteratorDoubleStream(stream, false, maxThreadNum, splitor, closeHandlers);
}
@Override
public Stream mapToObj(final FloatFunction extends U> mapper) {
if (maxThreadNum <= 1 || toIndex - fromIndex <= 1) {
return super.mapToObj(mapper);
}
return boxed().map(new Function() {
@Override
public U apply(Float value) {
return mapper.apply(value);
}
});
}
@Override
public FloatStream flatMap(final FloatFunction extends FloatStream> mapper) {
if (maxThreadNum <= 1 || toIndex - fromIndex <= 1) {
return new ParallelIteratorFloatStream(sequential().flatMap(mapper), false, maxThreadNum, splitor, null);
}
final FloatStream stream = boxed().flatMapToFloat(new Function() {
@Override
public FloatStream apply(Float value) {
return mapper.apply(value);
}
});
return new ParallelIteratorFloatStream(stream, false, maxThreadNum, splitor, null);
}
@Override
public IntStream flatMapToInt(final FloatFunction extends IntStream> mapper) {
if (maxThreadNum <= 1 || toIndex - fromIndex <= 1) {
return new ParallelIteratorIntStream(sequential().flatMapToInt(mapper), false, maxThreadNum, splitor, null);
}
final IntStream stream = boxed().flatMapToInt(new Function() {
@Override
public IntStream apply(Float value) {
return mapper.apply(value);
}
});
return new ParallelIteratorIntStream(stream, false, maxThreadNum, splitor, null);
}
@Override
public LongStream flatMapToLong(final FloatFunction extends LongStream> mapper) {
if (maxThreadNum <= 1 || toIndex - fromIndex <= 1) {
return new ParallelIteratorLongStream(sequential().flatMapToLong(mapper), false, maxThreadNum, splitor, null);
}
final LongStream stream = boxed().flatMapToLong(new Function() {
@Override
public LongStream apply(Float value) {
return mapper.apply(value);
}
});
return new ParallelIteratorLongStream(stream, false, maxThreadNum, splitor, null);
}
@Override
public DoubleStream flatMapToDouble(final FloatFunction extends DoubleStream> mapper) {
if (maxThreadNum <= 1 || toIndex - fromIndex <= 1) {
return new ParallelIteratorDoubleStream(sequential().flatMapToDouble(mapper), false, maxThreadNum, splitor, null);
}
final DoubleStream stream = boxed().flatMapToDouble(new Function() {
@Override
public DoubleStream apply(Float value) {
return mapper.apply(value);
}
});
return new ParallelIteratorDoubleStream(stream, false, maxThreadNum, splitor, null);
}
@Override
public Stream flatMapToObj(final FloatFunction extends Stream> mapper) {
if (maxThreadNum <= 1 || toIndex - fromIndex <= 1) {
return new ParallelIteratorStream<>(sequential().flatMapToObj(mapper), false, null, maxThreadNum, splitor, null);
}
return boxed().flatMap(new Function>() {
@Override
public Stream apply(Float value) {
return mapper.apply(value);
}
});
}
@Override
public FloatStream peek(final FloatConsumer action) {
if (maxThreadNum <= 1 || toIndex - fromIndex <= 1) {
return super.peek(action);
}
final FloatStream stream = boxed().peek(new Consumer() {
@Override
public void accept(Float t) {
action.accept(t);
}
}).sequential().mapToFloat(ToFloatFunction.UNBOX);
return new ParallelIteratorFloatStream(stream, false, maxThreadNum, splitor, closeHandlers);
}
@Override
public void forEach(final Try.FloatConsumer action) throws E {
if (maxThreadNum <= 1 || toIndex - fromIndex <= 1) {
super.forEach(action);
return;
}
final int threadNum = N.min(maxThreadNum, (toIndex - fromIndex));
final List> futureList = new ArrayList<>(threadNum);
final Holder eHolder = new Holder<>();
if (splitor == Splitor.ARRAY) {
final int sliceSize = (toIndex - fromIndex) / threadNum + ((toIndex - fromIndex) % threadNum == 0 ? 0 : 1);
for (int i = 0; i < threadNum; i++) {
final int sliceIndex = i;
futureList.add(asyncExecutor.execute(new Runnable() {
@Override
public void run() {
int cursor = fromIndex + sliceIndex * sliceSize;
final int to = toIndex - cursor > sliceSize ? cursor + sliceSize : toIndex;
try {
while (cursor < to && eHolder.value() == null) {
action.accept(elements[cursor++]);
}
} catch (Exception e) {
setError(eHolder, e);
}
}
}));
}
} else {
final MutableInt cursor = MutableInt.of(fromIndex);
for (int i = 0; i < threadNum; i++) {
futureList.add(asyncExecutor.execute(new Runnable() {
@Override
public void run() {
float next = 0;
try {
while (eHolder.value() == null) {
synchronized (elements) {
if (cursor.intValue() < toIndex) {
next = elements[cursor.getAndIncrement()];
} else {
break;
}
}
action.accept(next);
}
} catch (Exception e) {
setError(eHolder, e);
}
}
}));
}
}
complette(futureList, eHolder, (E) null);
}
@Override
public > M toMap(final FloatFunction extends K> keyExtractor, final FloatFunction extends V> valueMapper,
final BinaryOperator mergeFunction, final Supplier mapFactory) {
if (maxThreadNum <= 1 || toIndex - fromIndex <= 1) {
return super.toMap(keyExtractor, valueMapper, mergeFunction, mapFactory);
}
final Function super Float, ? extends K> keyExtractor2 = new Function() {
@Override
public K apply(Float value) {
return keyExtractor.apply(value);
}
};
final Function super Float, ? extends V> valueMapper2 = new Function() {
@Override
public V apply(Float value) {
return valueMapper.apply(value);
}
};
return boxed().toMap(keyExtractor2, valueMapper2, mergeFunction, mapFactory);
}
@Override
public > M toMap(final FloatFunction extends K> classifier, final Collector downstream,
final Supplier mapFactory) {
if (maxThreadNum <= 1 || toIndex - fromIndex <= 1) {
return super.toMap(classifier, downstream, mapFactory);
}
final Function super Float, ? extends K> classifier2 = new Function() {
@Override
public K apply(Float value) {
return classifier.apply(value);
}
};
return boxed().toMap(classifier2, downstream, mapFactory);
}
@Override
public float reduce(final float identity, final FloatBinaryOperator op) {
if (maxThreadNum <= 1 || toIndex - fromIndex <= 1) {
return super.reduce(identity, op);
}
final int threadNum = N.min(maxThreadNum, (toIndex - fromIndex));
final List> futureList = new ArrayList<>(threadNum);
final Holder eHolder = new Holder<>();
if (splitor == Splitor.ARRAY) {
final int sliceSize = (toIndex - fromIndex) / threadNum + ((toIndex - fromIndex) % threadNum == 0 ? 0 : 1);
for (int i = 0; i < threadNum; i++) {
final int sliceIndex = i;
futureList.add(asyncExecutor.execute(new Callable() {
@Override
public Float call() {
int cursor = fromIndex + sliceIndex * sliceSize;
final int to = toIndex - cursor > sliceSize ? cursor + sliceSize : toIndex;
float result = identity;
try {
while (cursor < to && eHolder.value() == null) {
result = op.applyAsFloat(result, elements[cursor++]);
}
} catch (Exception e) {
setError(eHolder, e);
}
return result;
}
}));
}
} else {
final MutableInt cursor = MutableInt.of(fromIndex);
for (int i = 0; i < threadNum; i++) {
futureList.add(asyncExecutor.execute(new Callable() {
@Override
public Float call() {
float result = identity;
float next = 0;
try {
while (eHolder.value() == null) {
synchronized (elements) {
if (cursor.intValue() < toIndex) {
next = elements[cursor.getAndIncrement()];
} else {
break;
}
}
result = op.applyAsFloat(result, next);
}
} catch (Exception e) {
setError(eHolder, e);
}
return result;
}
}));
}
}
if (eHolder.value() != null) {
throw N.toRuntimeException(eHolder.value());
}
Float result = null;
try {
for (ContinuableFuture future : futureList) {
if (result == null) {
result = future.get();
} else {
result = op.applyAsFloat(result, future.get());
}
}
} catch (InterruptedException | ExecutionException e) {
throw N.toRuntimeException(e);
}
return result == null ? identity : result;
}
@Override
public OptionalFloat reduce(final FloatBinaryOperator accumulator) {
if (maxThreadNum <= 1 || toIndex - fromIndex <= 1) {
return super.reduce(accumulator);
}
final int threadNum = N.min(maxThreadNum, (toIndex - fromIndex));
final List> futureList = new ArrayList<>(threadNum);
final Holder eHolder = new Holder<>();
if (splitor == Splitor.ARRAY) {
final int sliceSize = (toIndex - fromIndex) / threadNum + ((toIndex - fromIndex) % threadNum == 0 ? 0 : 1);
for (int i = 0; i < threadNum; i++) {
final int sliceIndex = i;
futureList.add(asyncExecutor.execute(new Callable() {
@Override
public Float call() {
int cursor = fromIndex + sliceIndex * sliceSize;
final int to = toIndex - cursor > sliceSize ? cursor + sliceSize : toIndex;
if (cursor >= to) {
return null;
}
float result = elements[cursor++];
try {
while (cursor < to && eHolder.value() == null) {
result = accumulator.applyAsFloat(result, elements[cursor++]);
}
} catch (Exception e) {
setError(eHolder, e);
}
return result;
}
}));
}
} else {
final MutableInt cursor = MutableInt.of(fromIndex);
for (int i = 0; i < threadNum; i++) {
futureList.add(asyncExecutor.execute(new Callable() {
@Override
public Float call() {
float result = 0;
synchronized (elements) {
if (cursor.intValue() < toIndex) {
result = elements[cursor.getAndIncrement()];
} else {
return null;
}
}
float next = 0;
try {
while (eHolder.value() == null) {
synchronized (elements) {
if (cursor.intValue() < toIndex) {
next = elements[cursor.getAndIncrement()];
} else {
break;
}
}
result = accumulator.applyAsFloat(result, next);
}
} catch (Exception e) {
setError(eHolder, e);
}
return result;
}
}));
}
}
if (eHolder.value() != null) {
throw N.toRuntimeException(eHolder.value());
}
Float result = null;
try {
for (ContinuableFuture future : futureList) {
final Float tmp = future.get();
if (tmp == null) {
continue;
} else if (result == null) {
result = tmp;
} else {
result = accumulator.applyAsFloat(result, tmp);
}
}
} catch (InterruptedException | ExecutionException e) {
throw N.toRuntimeException(e);
}
return result == null ? OptionalFloat.empty() : OptionalFloat.of(result);
}
@Override
public R collect(final Supplier supplier, final ObjFloatConsumer accumulator, final BiConsumer combiner) {
if (maxThreadNum <= 1 || toIndex - fromIndex <= 1) {
return super.collect(supplier, accumulator, combiner);
}
final int threadNum = N.min(maxThreadNum, (toIndex - fromIndex));
final List> futureList = new ArrayList<>(threadNum);
final Holder eHolder = new Holder<>();
if (splitor == Splitor.ARRAY) {
final int sliceSize = (toIndex - fromIndex) / threadNum + ((toIndex - fromIndex) % threadNum == 0 ? 0 : 1);
for (int i = 0; i < threadNum; i++) {
final int sliceIndex = i;
futureList.add(asyncExecutor.execute(new Callable() {
@Override
public R call() {
int cursor = fromIndex + sliceIndex * sliceSize;
final int to = toIndex - cursor > sliceSize ? cursor + sliceSize : toIndex;
final R container = supplier.get();
try {
while (cursor < to && eHolder.value() == null) {
accumulator.accept(container, elements[cursor++]);
}
} catch (Exception e) {
setError(eHolder, e);
}
return container;
}
}));
}
} else {
final MutableInt cursor = MutableInt.of(fromIndex);
for (int i = 0; i < threadNum; i++) {
futureList.add(asyncExecutor.execute(new Callable() {
@Override
public R call() {
final R container = supplier.get();
float next = 0;
try {
while (eHolder.value() == null) {
synchronized (elements) {
if (cursor.intValue() < toIndex) {
next = elements[cursor.getAndIncrement()];
} else {
break;
}
}
accumulator.accept(container, next);
}
} catch (Exception e) {
setError(eHolder, e);
}
return container;
}
}));
}
}
if (eHolder.value() != null) {
throw N.toRuntimeException(eHolder.value());
}
R container = (R) NONE;
try {
for (ContinuableFuture future : futureList) {
if (container == NONE) {
container = future.get();
} else {
combiner.accept(container, future.get());
}
}
} catch (InterruptedException | ExecutionException e) {
throw N.toRuntimeException(e);
}
return container == NONE ? supplier.get() : container;
}
@Override
public OptionalFloat min() {
if (fromIndex == toIndex) {
return OptionalFloat.empty();
} else if (sorted) {
return OptionalFloat.of(elements[fromIndex]);
} else if (maxThreadNum <= 1 || toIndex - fromIndex <= 1) {
return OptionalFloat.of(N.min(elements, fromIndex, toIndex));
}
final int threadNum = N.min(maxThreadNum, (toIndex - fromIndex));
final List> futureList = new ArrayList<>(threadNum);
final int sliceSize = (toIndex - fromIndex) / threadNum + ((toIndex - fromIndex) % threadNum == 0 ? 0 : 1);
for (int i = 0; i < threadNum; i++) {
final int sliceIndex = i;
futureList.add(asyncExecutor.execute(new Callable() {
@Override
public Float call() {
int cursor = fromIndex + sliceIndex * sliceSize;
final int to = toIndex - cursor > sliceSize ? cursor + sliceSize : toIndex;
return cursor >= to ? null : N.min(elements, cursor, to);
}
}));
}
Float candidate = null;
try {
for (ContinuableFuture future : futureList) {
final Float tmp = future.get();
if (tmp == null) {
continue;
} else if (candidate == null || N.compare(tmp.floatValue(), candidate.floatValue()) < 0) {
candidate = tmp;
}
}
} catch (InterruptedException | ExecutionException e) {
throw N.toRuntimeException(e);
}
return candidate == null ? OptionalFloat.empty() : OptionalFloat.of(candidate);
}
@Override
public OptionalFloat max() {
if (fromIndex == toIndex) {
return OptionalFloat.empty();
} else if (sorted) {
return OptionalFloat.of(elements[toIndex - 1]);
} else if (maxThreadNum <= 1 || toIndex - fromIndex <= 1) {
return OptionalFloat.of(N.max(elements, fromIndex, toIndex));
}
final int threadNum = N.min(maxThreadNum, (toIndex - fromIndex));
final List> futureList = new ArrayList<>(threadNum);
final int sliceSize = (toIndex - fromIndex) / threadNum + ((toIndex - fromIndex) % threadNum == 0 ? 0 : 1);
for (int i = 0; i < threadNum; i++) {
final int sliceIndex = i;
futureList.add(asyncExecutor.execute(new Callable() {
@Override
public Float call() {
int cursor = fromIndex + sliceIndex * sliceSize;
final int to = toIndex - cursor > sliceSize ? cursor + sliceSize : toIndex;
return cursor >= to ? null : N.max(elements, cursor, to);
}
}));
}
Float candidate = null;
try {
for (ContinuableFuture future : futureList) {
final Float tmp = future.get();
if (tmp == null) {
continue;
} else if (candidate == null || N.compare(tmp.floatValue(), candidate.floatValue()) > 0) {
candidate = tmp;
}
}
} catch (InterruptedException | ExecutionException e) {
throw N.toRuntimeException(e);
}
return candidate == null ? OptionalFloat.empty() : OptionalFloat.of(candidate);
}
@Override
public double sum() {
if (fromIndex == toIndex) {
return 0d;
} else if (maxThreadNum <= 1 || toIndex - fromIndex <= 1) {
return super.sum();
}
final Supplier supplier = new Supplier() {
@Override
public double[] get() {
return new double[3];
}
};
final ObjFloatConsumer accumulator = new ObjFloatConsumer() {
@Override
public void accept(double[] ll, float f) {
Collectors.sumWithCompensation(ll, f);
ll[2] += f;
}
};
final BiConsumer combiner = new BiConsumer() {
@Override
public void accept(double[] ll, double[] rr) {
Collectors.sumWithCompensation(ll, rr[0]);
Collectors.sumWithCompensation(ll, rr[1]);
ll[2] += rr[2];
}
};
final int threadNum = N.min(maxThreadNum, (toIndex - fromIndex));
final List> futureList = new ArrayList<>(threadNum);
final int sliceSize = (toIndex - fromIndex) / threadNum + ((toIndex - fromIndex) % threadNum == 0 ? 0 : 1);
for (int i = 0; i < threadNum; i++) {
final int sliceIndex = i;
futureList.add(asyncExecutor.execute(new Callable() {
@Override
public double[] call() {
int cursor = fromIndex + sliceIndex * sliceSize;
final int to = toIndex - cursor > sliceSize ? cursor + sliceSize : toIndex;
double[] container = supplier.get();
while (cursor < to) {
accumulator.accept(container, elements[cursor++]);
}
return container;
}
}));
}
double[] summation = null;
try {
for (ContinuableFuture future : futureList) {
final double[] tmp = future.get();
if (summation == null) {
summation = tmp;
} else {
combiner.accept(summation, tmp);
}
}
} catch (InterruptedException | ExecutionException e) {
throw N.toRuntimeException(e);
}
return Collectors.computeFinalSum(summation);
}
@Override
public OptionalDouble average() {
if (fromIndex == toIndex) {
return OptionalDouble.empty();
} else if (maxThreadNum <= 1 || toIndex - fromIndex <= 1) {
return super.average();
}
final Supplier supplier = new Supplier() {
@Override
public double[] get() {
return new double[4];
}
};
final ObjFloatConsumer accumulator = new ObjFloatConsumer() {
@Override
public void accept(double[] ll, float f) {
ll[2]++;
Collectors.sumWithCompensation(ll, f);
ll[3] += f;
}
};
final BiConsumer combiner = new BiConsumer() {
@Override
public void accept(double[] ll, double[] rr) {
Collectors.sumWithCompensation(ll, rr[0]);
Collectors.sumWithCompensation(ll, rr[1]);
ll[2] += rr[2];
ll[3] += rr[3];
}
};
final int threadNum = N.min(maxThreadNum, (toIndex - fromIndex));
final List> futureList = new ArrayList<>(threadNum);
final int sliceSize = (toIndex - fromIndex) / threadNum + ((toIndex - fromIndex) % threadNum == 0 ? 0 : 1);
for (int i = 0; i < threadNum; i++) {
final int sliceIndex = i;
futureList.add(asyncExecutor.execute(new Callable() {
@Override
public double[] call() {
int cursor = fromIndex + sliceIndex * sliceSize;
final int to = toIndex - cursor > sliceSize ? cursor + sliceSize : toIndex;
double[] container = supplier.get();
while (cursor < to) {
accumulator.accept(container, elements[cursor++]);
}
return container;
}
}));
}
double[] avg = null;
try {
for (ContinuableFuture future : futureList) {
final double[] tmp = future.get();
if (avg == null) {
avg = tmp;
} else {
combiner.accept(avg, tmp);
}
}
} catch (InterruptedException | ExecutionException e) {
throw N.toRuntimeException(e);
}
return avg[2] > 0 ? OptionalDouble.of(Collectors.computeFinalSum(avg) / avg[2]) : OptionalDouble.empty();
}
@Override
public FloatSummaryStatistics summarize() {
if (fromIndex == toIndex) {
return new FloatSummaryStatistics();
} else if (maxThreadNum <= 1 || toIndex - fromIndex <= 1) {
return super.summarize();
}
final int threadNum = N.min(maxThreadNum, (toIndex - fromIndex));
final List> futureList = new ArrayList<>(threadNum);
final int sliceSize = (toIndex - fromIndex) / threadNum + ((toIndex - fromIndex) % threadNum == 0 ? 0 : 1);
for (int i = 0; i < threadNum; i++) {
final int sliceIndex = i;
futureList.add(asyncExecutor.execute(new Callable() {
@Override
public FloatSummaryStatistics call() {
int cursor = fromIndex + sliceIndex * sliceSize;
final int to = toIndex - cursor > sliceSize ? cursor + sliceSize : toIndex;
final FloatSummaryStatistics result = new FloatSummaryStatistics();
for (int i = cursor; i < to; i++) {
result.accept(elements[i]);
}
return result;
}
}));
}
FloatSummaryStatistics result = null;
try {
for (ContinuableFuture future : futureList) {
final FloatSummaryStatistics tmp = future.get();
if (tmp == null) {
continue;
} else if (result == null) {
result = tmp;
} else {
result.combine(tmp);
}
}
} catch (InterruptedException | ExecutionException e) {
throw N.toRuntimeException(e);
}
return result;
}
@Override
public boolean anyMatch(final Try.FloatPredicate predicate) throws E {
if (maxThreadNum <= 1 || toIndex - fromIndex <= 1) {
return super.anyMatch(predicate);
}
final int threadNum = N.min(maxThreadNum, (toIndex - fromIndex));
final List> futureList = new ArrayList<>(threadNum);
final Holder eHolder = new Holder<>();
final MutableBoolean result = MutableBoolean.of(false);
if (splitor == Splitor.ARRAY) {
final int sliceSize = (toIndex - fromIndex) / threadNum + ((toIndex - fromIndex) % threadNum == 0 ? 0 : 1);
for (int i = 0; i < threadNum; i++) {
final int sliceIndex = i;
futureList.add(asyncExecutor.execute(new Runnable() {
@Override
public void run() {
int cursor = fromIndex + sliceIndex * sliceSize;
final int to = toIndex - cursor > sliceSize ? cursor + sliceSize : toIndex;
try {
while (cursor < to && result.isFalse() && eHolder.value() == null) {
if (predicate.test(elements[cursor++])) {
result.setTrue();
break;
}
}
} catch (Exception e) {
setError(eHolder, e);
}
}
}));
}
} else {
final MutableInt cursor = MutableInt.of(fromIndex);
for (int i = 0; i < threadNum; i++) {
futureList.add(asyncExecutor.execute(new Runnable() {
@Override
public void run() {
float next = 0;
try {
while (result.isFalse() && eHolder.value() == null) {
synchronized (elements) {
if (cursor.intValue() < toIndex) {
next = elements[cursor.getAndIncrement()];
} else {
break;
}
}
if (predicate.test(next)) {
result.setTrue();
break;
}
}
} catch (Exception e) {
setError(eHolder, e);
}
}
}));
}
}
complette(futureList, eHolder, (E) null);
return result.value();
}
@Override
public boolean allMatch(final Try.FloatPredicate predicate) throws E {
if (maxThreadNum <= 1 || toIndex - fromIndex <= 1) {
return super.allMatch(predicate);
}
final int threadNum = N.min(maxThreadNum, (toIndex - fromIndex));
final List> futureList = new ArrayList<>(threadNum);
final Holder eHolder = new Holder<>();
final MutableBoolean result = MutableBoolean.of(true);
if (splitor == Splitor.ARRAY) {
final int sliceSize = (toIndex - fromIndex) / threadNum + ((toIndex - fromIndex) % threadNum == 0 ? 0 : 1);
for (int i = 0; i < threadNum; i++) {
final int sliceIndex = i;
futureList.add(asyncExecutor.execute(new Runnable() {
@Override
public void run() {
int cursor = fromIndex + sliceIndex * sliceSize;
final int to = toIndex - cursor > sliceSize ? cursor + sliceSize : toIndex;
try {
while (cursor < to && result.isTrue() && eHolder.value() == null) {
if (predicate.test(elements[cursor++]) == false) {
result.setFalse();
break;
}
}
} catch (Exception e) {
setError(eHolder, e);
}
}
}));
}
} else {
final MutableInt cursor = MutableInt.of(fromIndex);
for (int i = 0; i < threadNum; i++) {
futureList.add(asyncExecutor.execute(new Runnable() {
@Override
public void run() {
float next = 0;
try {
while (result.isTrue() && eHolder.value() == null) {
synchronized (elements) {
if (cursor.intValue() < toIndex) {
next = elements[cursor.getAndIncrement()];
} else {
break;
}
}
if (predicate.test(next) == false) {
result.setFalse();
break;
}
}
} catch (Exception e) {
setError(eHolder, e);
}
}
}));
}
}
complette(futureList, eHolder, (E) null);
return result.value();
}
@Override
public boolean noneMatch(final Try.FloatPredicate predicate) throws E {
if (maxThreadNum <= 1 || toIndex - fromIndex <= 1) {
return super.noneMatch(predicate);
}
final int threadNum = N.min(maxThreadNum, (toIndex - fromIndex));
final List> futureList = new ArrayList<>(threadNum);
final Holder eHolder = new Holder<>();
final MutableBoolean result = MutableBoolean.of(true);
if (splitor == Splitor.ARRAY) {
final int sliceSize = (toIndex - fromIndex) / threadNum + ((toIndex - fromIndex) % threadNum == 0 ? 0 : 1);
for (int i = 0; i < threadNum; i++) {
final int sliceIndex = i;
futureList.add(asyncExecutor.execute(new Runnable() {
@Override
public void run() {
int cursor = fromIndex + sliceIndex * sliceSize;
final int to = toIndex - cursor > sliceSize ? cursor + sliceSize : toIndex;
try {
while (cursor < to && result.isTrue() && eHolder.value() == null) {
if (predicate.test(elements[cursor++])) {
result.setFalse();
break;
}
}
} catch (Exception e) {
setError(eHolder, e);
}
}
}));
}
} else {
final MutableInt cursor = MutableInt.of(fromIndex);
for (int i = 0; i < threadNum; i++) {
futureList.add(asyncExecutor.execute(new Runnable() {
@Override
public void run() {
float next = 0;
try {
while (result.isTrue() && eHolder.value() == null) {
synchronized (elements) {
if (cursor.intValue() < toIndex) {
next = elements[cursor.getAndIncrement()];
} else {
break;
}
}
if (predicate.test(next)) {
result.setFalse();
break;
}
}
} catch (Exception e) {
setError(eHolder, e);
}
}
}));
}
}
complette(futureList, eHolder, (E) null);
return result.value();
}
@Override
public OptionalFloat findFirst(final Try.FloatPredicate predicate) throws E {
if (maxThreadNum <= 1 || toIndex - fromIndex <= 1) {
return super.findFirst(predicate);
}
final int threadNum = N.min(maxThreadNum, (toIndex - fromIndex));
final List> futureList = new ArrayList<>(threadNum);
final Holder eHolder = new Holder<>();
final Holder> resultHolder = new Holder<>();
if (splitor == Splitor.ARRAY) {
final int sliceSize = (toIndex - fromIndex) / threadNum + ((toIndex - fromIndex) % threadNum == 0 ? 0 : 1);
for (int i = 0; i < threadNum; i++) {
final int sliceIndex = i;
futureList.add(asyncExecutor.execute(new Runnable() {
@Override
public void run() {
int cursor = fromIndex + sliceIndex * sliceSize;
final int to = toIndex - cursor > sliceSize ? cursor + sliceSize : toIndex;
final Pair pair = new Pair<>();
try {
while (cursor < to && (resultHolder.value() == null || cursor < resultHolder.value().left) && eHolder.value() == null) {
pair.left = cursor;
pair.right = elements[cursor++];
if (predicate.test(pair.right)) {
synchronized (resultHolder) {
if (resultHolder.value() == null || pair.left < resultHolder.value().left) {
resultHolder.setValue(pair.copy());
}
}
break;
}
}
} catch (Exception e) {
setError(eHolder, e);
}
}
}));
}
} else {
final MutableInt cursor = MutableInt.of(fromIndex);
for (int i = 0; i < threadNum; i++) {
futureList.add(asyncExecutor.execute(new Runnable() {
@Override
public void run() {
final Pair pair = new Pair<>();
try {
while (resultHolder.value() == null && eHolder.value() == null) {
synchronized (elements) {
if (cursor.intValue() < toIndex) {
pair.left = cursor.intValue();
pair.right = elements[cursor.getAndIncrement()];
} else {
break;
}
}
if (predicate.test(pair.right)) {
synchronized (resultHolder) {
if (resultHolder.value() == null || pair.left < resultHolder.value().left) {
resultHolder.setValue(pair.copy());
}
}
break;
}
}
} catch (Exception e) {
setError(eHolder, e);
}
}
}));
}
}
complette(futureList, eHolder, (E) null);
return resultHolder.value() == null ? OptionalFloat.empty() : OptionalFloat.of(resultHolder.value().right);
}
@Override
public OptionalFloat findLast(final Try.FloatPredicate predicate) throws E {
if (maxThreadNum <= 1 || toIndex - fromIndex <= 1) {
return super.findLast(predicate);
}
final int threadNum = N.min(maxThreadNum, (toIndex - fromIndex));
final List> futureList = new ArrayList<>(threadNum);
final Holder eHolder = new Holder<>();
final Holder> resultHolder = new Holder<>();
if (splitor == Splitor.ARRAY) {
final int sliceSize = (toIndex - fromIndex) / threadNum + ((toIndex - fromIndex) % threadNum == 0 ? 0 : 1);
for (int i = 0; i < threadNum; i++) {
final int sliceIndex = i;
futureList.add(asyncExecutor.execute(new Runnable() {
@Override
public void run() {
final int from = fromIndex + sliceIndex * sliceSize;
int cursor = toIndex - from > sliceSize ? from + sliceSize : toIndex;
final Pair pair = new Pair<>();
try {
while (cursor > from && (resultHolder.value() == null || cursor > resultHolder.value().left) && eHolder.value() == null) {
pair.left = cursor;
pair.right = elements[--cursor];
if (predicate.test(pair.right)) {
synchronized (resultHolder) {
if (resultHolder.value() == null || pair.left > resultHolder.value().left) {
resultHolder.setValue(pair.copy());
}
}
break;
}
}
} catch (Exception e) {
setError(eHolder, e);
}
}
}));
}
} else {
final MutableInt cursor = MutableInt.of(toIndex);
for (int i = 0; i < threadNum; i++) {
futureList.add(asyncExecutor.execute(new Runnable() {
@Override
public void run() {
final Pair pair = new Pair<>();
try {
while (resultHolder.value() == null && eHolder.value() == null) {
synchronized (elements) {
if (cursor.intValue() > fromIndex) {
pair.left = cursor.intValue();
pair.right = elements[cursor.decrementAndGet()];
} else {
break;
}
}
if (predicate.test(pair.right)) {
synchronized (resultHolder) {
if (resultHolder.value() == null || pair.left > resultHolder.value().left) {
resultHolder.setValue(pair.copy());
}
}
break;
}
}
} catch (Exception e) {
setError(eHolder, e);
}
}
}));
}
}
complette(futureList, eHolder, (E) null);
return resultHolder.value() == null ? OptionalFloat.empty() : OptionalFloat.of(resultHolder.value().right);
}
@Override
public OptionalFloat findAny(final Try.FloatPredicate predicate) throws E {
if (maxThreadNum <= 1 || toIndex - fromIndex <= 1) {
return super.findAny(predicate);
}
final int threadNum = N.min(maxThreadNum, (toIndex - fromIndex));
final List> futureList = new ArrayList<>(threadNum);
final Holder eHolder = new Holder<>();
final Holder
© 2015 - 2025 Weber Informatics LLC | Privacy Policy