/** * This method merges n time series into one according to the following strategy: * The timestamps are merged; the resulting sequence is the union of the given ones. * The value is the list of all input values at that timestamp * * @param input an iterable of time series to be merged * @param <T> the time type * @param <V> type of input values * @return iterator of merged pairs of (time, list of values)) */ public static <T extends Comparable<T>, V> Iterator<Pair<T, List<V>>> merge(Iterable<Iterator<Pair<T, V>>> input) { return new TimeSeriesMerge<>(input); }
@Override public Pair<T, List<V>> next() { // step one: determine nextIterator based on heads int i = nextIterator(); if (i < 0) { throw new NoSuchElementException(); } // step two: update values and determine result Pair<T, V> aux = heads.get(i); T nextStep = aux.getFirst(); values.set(i, aux.getSecond()); List<V> vs = new ArrayList<>(values); // step three: update heads Iterator<Pair<T, V>> t = iterators.get(i); heads.set(i, t.hasNext() ? t.next() : null); // now, everything is ready for the next call // of either hasNext or next return new Pair<>(nextStep, vs); } }
@Override public Pair<T, List<V>> next() { // step one: determine nextIterator based on heads int i = nextIterator(); if (i < 0) { throw new NoSuchElementException(); } // step two: update values and determine result Pair<T, V> aux = heads.get(i); T nextStep = aux.getFirst(); values.set(i, aux.getSecond()); List<V> vs = new ArrayList<>(values); // step three: update heads Iterator<Pair<T, V>> t = iterators.get(i); heads.set(i, t.hasNext() ? t.next() : null); // now, everything is ready for the next call // of either hasNext or next return new Pair<>(nextStep, vs); } }
/** * This method merges n time series into one according to the following strategy: * The timestamps are merged; the resulting sequence is the union of the given ones. * The value is the list of all input values at that timestamp * * @param input an iterable of time series to be merged * @param <T> the time type * @param <V> type of input values * @return iterator of merged pairs of (time, list of values)) */ public static <T extends Comparable<T>, V> Iterator<Pair<T, List<V>>> merge(Iterable<Iterator<Pair<T, V>>> input) { return new TimeSeriesMerge<>(input); }
/** * @param ts a time series * @param <T> the time type * @param <V> the value type * @return a new time series unioning all time stamps given, * the value being the list of all values valid at that time */ public static <T extends Comparable<T>, V> TimeSeries<T, List<V>> merge(Iterable<TimeSeries<T, V>> ts) { Iterator<Pair<T, List<V>>> aux = new TimeSeriesMerge<>(map(asIterator(ts), Iterable::iterator)); return new TimeSeries<>(aux); }
/** * @param ts a time series * @param <T> the time type * @param <V> the value type * @return a new time series unioning all time stamps given, * the value being the list of all values valid at that time */ public static <T extends Comparable<T>, V> TimeSeries<T, List<V>> merge(Iterable<TimeSeries<T, V>> ts) { Iterator<Pair<T, List<V>>> aux = new TimeSeriesMerge<>(map(asIterator(ts), Iterable::iterator)); return new TimeSeries<>(aux); }
/** * @param ts a time series * @param <T> the time type * @param <V> the value type * @param op an operator reducing the value list * @return a new time series unioning all time stamps given, * the value being the result of the reducing by op (e.g. min, max, avg) */ public static <T extends Comparable<T>, V> TimeSeries<T, V> merge(Iterable<TimeSeries<T, V>> ts, BinaryOperator<V> op) { BinaryOperator<V> wop = weakBinaryOperator(op); Iterator<Pair<T, List<V>>> aux1 = new TimeSeriesMerge<>(map(asIterator(ts), Iterable::iterator)); Iterator<Pair<T, V>> aux2 = map(aux1, (Pair<T, List<V>> p) -> pairOf(p.getFirst(), reduce(asIterator(p.getSecond()), wop))); return new TimeSeries<>(aux2); }
/** * @param ts a time series * @param <T> the time type * @param <V> the value type * @param op an operator reducing the value list * @return a new time series unioning all time stamps given, * the value being the result of the reducing by op (e.g. min, max, avg) */ public static <T extends Comparable<T>, V> TimeSeries<T, V> merge(Iterable<TimeSeries<T, V>> ts, BinaryOperator<V> op) { BinaryOperator<V> wop = weakBinaryOperator(op); Iterator<Pair<T, List<V>>> aux1 = new TimeSeriesMerge<>(map(asIterator(ts), Iterable::iterator)); Iterator<Pair<T, V>> aux2 = map(aux1, (Pair<T, List<V>> p) -> pairOf(p.getFirst(), reduce(asIterator(p.getSecond()), wop))); return new TimeSeries<>(aux2); }