functionalj.stream.intstream.IntStreamPlusWithMapGroup Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of functionalj-core Show documentation
Show all versions of functionalj-core Show documentation
The module for FunctionalJ Core.
// ============================================================================
// Copyright (c) 2017-2021 Nawapunth Manusitthipol (NawaMan - http://nawaman.net).
// ----------------------------------------------------------------------------
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// ============================================================================
package functionalj.stream.intstream;
import java.util.Spliterators;
import java.util.function.Consumer;
import java.util.function.DoubleConsumer;
import java.util.function.IntBinaryOperator;
import java.util.function.IntConsumer;
import java.util.function.LongConsumer;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
import java.util.stream.StreamSupport;
import functionalj.function.Func1;
import functionalj.function.Func4;
import functionalj.function.IntIntBiFunction;
import functionalj.function.IntIntToDoubleFunctionPrimitive;
import functionalj.function.IntIntToLongFunctionPrimitive;
import functionalj.stream.StreamPlus;
import functionalj.stream.doublestream.DoubleStreamPlus;
import functionalj.stream.longstream.LongStreamPlus;
import functionalj.tuple.IntIntTuple;
import lombok.val;
class IntStreamPlusWithMapGroupHelper {
static StreamPlus mapGroup(
IntStreamPlus streamPlus,
int count,
Func4, Void> processNormal,
Func4, Void> processTail) {
val splitr = streamPlus.spliterator();
val spliterator = new Spliterators.AbstractSpliterator(splitr.estimateSize(), 0) {
int[] array = new int[count*10];
int start = 0;
int end = 0;
boolean used = false;
@Override
public boolean tryAdvance(Consumer super TARGET> consumer) {
IntConsumer action = elem -> {
array[end] = elem;
end++;
int length = end - start;
if (length >= count) {
processNormal.apply(array, start, end, consumer);
used = true;
start++;
}
if (end >= array.length) {
System.arraycopy(array, start, array, 0, length - 1);
start = 0;
end = length - 1;
}
};
boolean hasNext = splitr.tryAdvance(action);
if (!hasNext && !used && (processTail != null)) {
processTail.apply(array, start, end, consumer);
}
return hasNext;
}
};
return StreamPlus.from(StreamSupport.stream(spliterator, false));
}
static IntStreamPlus mapGroupToInt(
IntStreamPlus streamPlus,
int count,
Func4 processNormal,
Func4 processTail) {
val splitr = streamPlus.spliterator();
val spliterator = new Spliterators.AbstractIntSpliterator(splitr.estimateSize(), 0) {
int[] array = new int[count*10];
int start = 0;
int end = 0;
boolean used = false;
@Override
public boolean tryAdvance(IntConsumer consumer) {
IntConsumer action = elem -> {
array[end] = elem;
end++;
int length = end - start;
if (length >= count) {
processNormal.apply(array, start, end, consumer);
used = true;
start++;
}
if (end >= array.length) {
System.arraycopy(array, start, array, 0, length - 1);
start = 0;
end = length - 1;
}
};
boolean hasNext = splitr.tryAdvance(action);
if (!hasNext && !used && (processTail != null)) {
processTail.apply(array, start, end, consumer);
}
return hasNext;
}
};
return IntStreamPlus.from(StreamSupport.intStream(spliterator, false));
}
static LongStreamPlus mapGroupToLong(
IntStreamPlus streamPlus,
int count,
Func4 processNormal,
Func4 processTail) {
val splitr = streamPlus.spliterator();
val spliterator = new Spliterators.AbstractLongSpliterator(splitr.estimateSize(), 0) {
int[] array = new int[count*10];
int start = 0;
int end = 0;
boolean used = false;
@Override
public boolean tryAdvance(LongConsumer consumer) {
IntConsumer action = elem -> {
array[end] = elem;
end++;
int length = end - start;
if (length >= count) {
processNormal.apply(array, start, end, consumer);
used = true;
start++;
}
if (end >= array.length) {
System.arraycopy(array, start, array, 0, length - 1);
start = 0;
end = length - 1;
}
};
boolean hasNext = splitr.tryAdvance(action);
if (hasNext && !used) {
hasNext = splitr.tryAdvance(action);
}
if (!hasNext && !used && (processTail != null)) {
processTail.apply(array, start, end, consumer);
}
return hasNext;
}
};
return LongStreamPlus.from(StreamSupport.longStream(spliterator, false));
}
static DoubleStreamPlus mapGroupToDouble(
IntStreamPlus streamPlus,
int count,
Func4 processNormal,
Func4 processTail) {
val splitr = streamPlus.spliterator();
val spliterator = new Spliterators.AbstractDoubleSpliterator(splitr.estimateSize(), 0) {
int[] array = new int[count*10];
int start = 0;
int end = 0;
boolean used = false;
@Override
public boolean tryAdvance(DoubleConsumer consumer) {
IntConsumer action = elem -> {
array[end] = elem;
end++;
int length = end - start;
if (length >= count) {
processNormal.apply(array, start, end, consumer);
used = true;
start++;
}
if (end >= array.length) {
System.arraycopy(array, start, array, 0, length - 1);
start = 0;
end = length - 1;
}
};
boolean hasNext = splitr.tryAdvance(action);
if (hasNext && !used) {
hasNext = splitr.tryAdvance(action);
}
if (!hasNext && !used && (processTail != null)) {
processTail.apply(array, start, end, consumer);
}
return hasNext;
}
};
return DoubleStreamPlus.from(StreamSupport.doubleStream(spliterator, false));
}
}
public interface IntStreamPlusWithMapGroup {
public IntStreamPlus intStreamPlus();
/** Create a stream whose value is the combination between the previous value and the current value of this stream. */
public default IntStreamPlus mapTwo(IntBinaryOperator combinator) {
return IntStreamPlusWithMapGroupHelper.mapGroupToInt(
intStreamPlus(), 2,
(array, start, end, consumer) -> {
val prev = array[start];
val curr = array[start + 1];
val value = combinator.applyAsInt(prev, curr);
consumer.accept(value);
return (Void)null;
},
null);
}
/** Create a stream whose value is the combination between the previous value and the current value of this stream. */
public default IntStreamPlus mapGroup(int count, ToIntFunction combinator) {
return IntStreamPlusWithMapGroupHelper.mapGroupToInt(
intStreamPlus(), count,
(array, start, end, consumer) -> {
val iterator = new ArrayBackedIntIteratorPlus(array, start, count);
val streamPlus = new ArrayBackedIntStreamPlus(iterator);
val value = combinator.applyAsInt(streamPlus);
consumer.accept(value);
return (Void)null;
},
(array, start, end, consumer) -> {
val iterator = new ArrayBackedIntIteratorPlus(array, start, end - start);
val streamPlus = new ArrayBackedIntStreamPlus(iterator);
val value = combinator.applyAsInt(streamPlus);
consumer.accept(value);
return (Void)null;
});
}
// == Object ==
/** @return the stream of each previous value and each current value. */
public default StreamPlus mapTwoToObj() {
return IntStreamPlusWithMapGroupHelper.mapGroup(
intStreamPlus(), 2,
(array, start, end, consumer) -> {
val prev = array[start];
val curr = array[start + 1];
consumer.accept(IntIntTuple.of(prev, curr));
return (Void)null;
},
null);
}
/** Create a stream whose value is the combination between the previous value and the current value of this stream. */
public default StreamPlus mapTwoToObj(IntIntBiFunction combinator) {
return IntStreamPlusWithMapGroupHelper.mapGroup(
intStreamPlus(), 2,
(array, start, end, consumer) -> {
val prev = array[start];
val curr = array[start + 1];
val value = combinator.apply(prev, curr);
consumer.accept(value);
return (Void)null;
},
null);
}
/** @return the stream of each previous value and each current value. */
public default StreamPlus mapGroupToObj(int count) {
return IntStreamPlusWithMapGroupHelper.mapGroup(
intStreamPlus(), count,
(array, start, end, consumer) -> {
val iterator = new ArrayBackedIntIteratorPlus(array, start, count);
val streamPlus = new ArrayBackedIntStreamPlus(iterator);
consumer.accept(streamPlus);
return (Void)null;
},
(array, start, end, consumer) -> {
val iterator = new ArrayBackedIntIteratorPlus(array, start, end - start);
val streamPlus = new ArrayBackedIntStreamPlus(iterator);
consumer.accept(streamPlus);
return (Void)null;
});
}
/** @return the stream of each previous value and each current value. */
public default StreamPlus mapGroupToObj(int count, Func1 combinator) {
return mapGroupToObj(count)
.map(combinator);
}
//== Int ==
/** Create a stream whose value is the combination between the previous value and the current value of this stream. */
public default IntStreamPlus mapTwoToInt(IntBinaryOperator combinator) {
return IntStreamPlusWithMapGroupHelper.mapGroupToInt(
intStreamPlus(), 2,
(array, start, end, consumer) -> {
val prev = array[start];
val curr = array[start + 1];
val value = combinator.applyAsInt(prev, curr);
consumer.accept(value);
return (Void)null;
},
null);
}
/** Create a stream whose value is the combination between the previous value and the current value of this stream. */
public default IntStreamPlus mapGroupToInt(int count, ToIntFunction combinator) {
return IntStreamPlusWithMapGroupHelper.mapGroupToInt(
intStreamPlus(), count,
(array, start, end, consumer) -> {
val iterator = new ArrayBackedIntIteratorPlus(array, start, count);
val streamPlus = new ArrayBackedIntStreamPlus(iterator);
val value = combinator.applyAsInt(streamPlus);
consumer.accept(value);
return (Void)null;
},
(array, start, end, consumer) -> {
val iterator = new ArrayBackedIntIteratorPlus(array, start, end - start);
val streamPlus = new ArrayBackedIntStreamPlus(iterator);
val value = combinator.applyAsInt(streamPlus);
consumer.accept(value);
return (Void)null;
});
}
//== Long ==
/** Create a stream whose value is the combination between the previous value and the current value of this stream. */
public default LongStreamPlus mapTwoToLong(IntIntToLongFunctionPrimitive combinator) {
return IntStreamPlusWithMapGroupHelper.mapGroupToLong(
intStreamPlus(), 2,
(array, start, end, consumer) -> {
val prev = array[start];
val curr = array[start + 1];
val value = combinator.applyAsIntAndInt(prev, curr);
consumer.accept(value);
return (Void)null;
},
null);
}
/** Create a stream whose value is the combination between the previous value and the current value of this stream. */
public default LongStreamPlus mapGroupToLong(int count, ToLongFunction combinator) {
return IntStreamPlusWithMapGroupHelper.mapGroupToLong(
intStreamPlus(), count,
(array, start, end, consumer) -> {
val iterator = new ArrayBackedIntIteratorPlus(array, start, count);
val streamPlus = new ArrayBackedIntStreamPlus(iterator);
val value = combinator.applyAsLong(streamPlus);
consumer.accept(value);
return (Void)null;
},
(array, start, end, consumer) -> {
val iterator = new ArrayBackedIntIteratorPlus(array, start, end - start);
val streamPlus = new ArrayBackedIntStreamPlus(iterator);
val value = combinator.applyAsLong(streamPlus);
consumer.accept(value);
return (Void)null;
});
}
//== Double ==
/** Create a stream whose value is the combination between the previous value and the current value of this stream. */
public default DoubleStreamPlus mapTwoToDouble(IntIntToDoubleFunctionPrimitive combinator) {
return IntStreamPlusWithMapGroupHelper.mapGroupToDouble(
intStreamPlus(), 2,
(array, start, end, consumer) -> {
val prev = array[start];
val curr = array[start + 1];
val value = combinator.applyAsDouble(prev, curr);
consumer.accept(value);
return (Void)null;
},
null);
}
/** Create a stream whose value is the combination between the previous value and the current value of this stream. */
public default DoubleStreamPlus mapGroupToDouble(int count, ToDoubleFunction combinator) {
return IntStreamPlusWithMapGroupHelper.mapGroupToDouble(
intStreamPlus(), count,
(array, start, end, consumer) -> {
val iterator = new ArrayBackedIntIteratorPlus(array, start, count);
val streamPlus = new ArrayBackedIntStreamPlus(iterator);
val value = combinator.applyAsDouble(streamPlus);
consumer.accept(value);
return (Void)null;
},
(array, start, end, consumer) -> {
val iterator = new ArrayBackedIntIteratorPlus(array, start, end - start);
val streamPlus = new ArrayBackedIntStreamPlus(iterator);
val value = combinator.applyAsDouble(streamPlus);
consumer.accept(value);
return (Void)null;
});
}
}