All Downloads are FREE. Search and download functionalities are using the official Maven repository.

functionalj.stream.doublestream.DoubleStreamPlusWithMapGroup Maven / Gradle / Ivy

There is a newer version: 1.0.17
Show newest version
// ============================================================================
// 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.doublestream;

import java.util.Spliterators;
import java.util.function.Consumer;
import java.util.function.DoubleBinaryOperator;
import java.util.function.DoubleConsumer;
import java.util.function.IntConsumer;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;
import java.util.stream.StreamSupport;

import functionalj.function.DoubleDoubleFunction;
import functionalj.function.DoubleDoubleToIntFunctionPrimitive;
import functionalj.function.Func1;
import functionalj.function.Func4;
import functionalj.stream.StreamPlus;
import functionalj.stream.intstream.IntStreamPlus;
import functionalj.tuple.DoubleDoubleTuple;
import lombok.val;


class DoubleStreamPlusWithMapGroupHelper {
    
    static  StreamPlus mapGroup(
            DoubleStreamPlus streamPlus,
            int count,
            Func4, Void> processNormal,
            Func4, Void> processTail) {
        val splitr = streamPlus.spliterator();
        val spliterator = new Spliterators.AbstractSpliterator(splitr.estimateSize(), 0) {
            double[] array = new double[count*10];
            int      start = 0;
            int      end   = 0;
            boolean  used  = false;
            @Override
            public boolean tryAdvance(Consumer consumer) {
                DoubleConsumer 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(
            DoubleStreamPlus streamPlus,
            int count,
            Func4 processNormal,
            Func4 processTail) {
        val splitr = streamPlus.spliterator();
        val spliterator = new Spliterators.AbstractIntSpliterator(splitr.estimateSize(), 0) {
            double[] array = new double[count*10];
            int      start = 0;
            int      end   = 0;
            boolean  used  = false;
            @Override
            public boolean tryAdvance(IntConsumer consumer) {
                DoubleConsumer 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  DoubleStreamPlus mapGroupToDouble(
            DoubleStreamPlus streamPlus,
            int count,
            Func4 processNormal,
            Func4 processTail) {
        val splitr = streamPlus.spliterator();
        val spliterator = new Spliterators.AbstractDoubleSpliterator(splitr.estimateSize(), 0) {
            double[] array = new double[count*10];
            int      start = 0;
            int      end   = 0;
            boolean  used  = false;
            @Override
            public boolean tryAdvance(DoubleConsumer consumer) {
                DoubleConsumer 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 DoubleStreamPlusWithMapGroup {
    
    public DoubleStreamPlus doubleStreamPlus();
    
    
    /** Create a stream whose value is the combination between the previous value and the current value of this stream. */
    public default DoubleStreamPlus mapTwo(DoubleBinaryOperator combinator) {
        return DoubleStreamPlusWithMapGroupHelper.mapGroupToDouble(
                doubleStreamPlus(), 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 mapGroup(int count, ToDoubleFunction combinator) {
        return DoubleStreamPlusWithMapGroupHelper.mapGroupToDouble(
                doubleStreamPlus(), count, 
                (array, start, end, consumer) -> {
                    val iterator   = new ArrayBackedDoubleIteratorPlus(array, start, count);
                    val streamPlus = new ArrayBackedDoubleStreamPlus(iterator);
                    val value      = combinator.applyAsDouble(streamPlus);
                    consumer.accept(value);
                    return (Void)null;
                },
                (array, start, end, consumer) -> {
                    val iterator   = new ArrayBackedDoubleIteratorPlus(array, start, end - start);
                    val streamPlus = new ArrayBackedDoubleStreamPlus(iterator);
                    val value      = combinator.applyAsDouble(streamPlus);
                    consumer.accept(value);
                    return (Void)null;
                });
    }
    
    // == Object ==
    
    /** @return  the stream of  each previous value and each current value. */
    public default StreamPlus mapTwoToObj() {
        return DoubleStreamPlusWithMapGroupHelper.mapGroup(
                doubleStreamPlus(), 2, 
                (array, start, end, consumer) -> {
                    val prev  = array[start];
                    val curr  = array[start + 1];
                    consumer.accept(DoubleDoubleTuple.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 mapGroupToObj(DoubleDoubleFunction combinator) {
        return DoubleStreamPlusWithMapGroupHelper.mapGroup(
                doubleStreamPlus(), 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 DoubleStreamPlusWithMapGroupHelper.mapGroup(
                doubleStreamPlus(), count, 
                (array, start, end, consumer) -> {
                    val iterator   = new ArrayBackedDoubleIteratorPlus(array, start, count);
                    val streamPlus = new ArrayBackedDoubleStreamPlus(iterator);
                    consumer.accept(streamPlus);
                    return (Void)null;
                },
                (array, start, end, consumer) -> {
                    val iterator   = new ArrayBackedDoubleIteratorPlus(array, start, end - start);
                    val streamPlus = new ArrayBackedDoubleStreamPlus(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(DoubleDoubleToIntFunctionPrimitive combinator) {
        return DoubleStreamPlusWithMapGroupHelper.mapGroupToInt(
                doubleStreamPlus(), 2, 
                (array, start, end, consumer) -> {
                    val prev  = array[start];
                    val curr  = array[start + 1];
                    val value = combinator.applyAsDoubleAndDouble(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 DoubleStreamPlusWithMapGroupHelper.mapGroupToInt(
                doubleStreamPlus(), count, 
                (array, start, end, consumer) -> {
                    val iterator   = new ArrayBackedDoubleIteratorPlus(array, start, count);
                    val streamPlus = new ArrayBackedDoubleStreamPlus(iterator);
                    val value      = combinator.applyAsInt(streamPlus);
                    consumer.accept(value);
                    return (Void)null;
                },
                (array, start, end, consumer) -> {
                    val iterator   = new ArrayBackedDoubleIteratorPlus(array, start, end - start);
                    val streamPlus = new ArrayBackedDoubleStreamPlus(iterator);
                    val value      = combinator.applyAsInt(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(DoubleBinaryOperator combinator) {
        return DoubleStreamPlusWithMapGroupHelper.mapGroupToDouble(
                doubleStreamPlus(), 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 DoubleStreamPlusWithMapGroupHelper.mapGroupToDouble(
                doubleStreamPlus(), count, 
                (array, start, end, consumer) -> {
                    val iterator   = new ArrayBackedDoubleIteratorPlus(array, start, count);
                    val streamPlus = new ArrayBackedDoubleStreamPlus(iterator);
                    val value      = combinator.applyAsDouble(streamPlus);
                    consumer.accept(value);
                    return (Void)null;
                },
                (array, start, end, consumer) -> {
                    val iterator   = new ArrayBackedDoubleIteratorPlus(array, start, end - start);
                    val streamPlus = new ArrayBackedDoubleStreamPlus(iterator);
                    val value      = combinator.applyAsDouble(streamPlus);
                    consumer.accept(value);
                    return (Void)null;
                });
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy