/*
 * Copyright 1997-2018 Optimatika
 *
 * 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 org.ojalgo.series.primitive;

import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;

import org.ojalgo.series.BasicSeries;

public class CoordinatedSet<K extends Comparable<? super K>> {

    public static <K extends Comparable<? super K>> CoordinatedSet<K> from(final List<? extends BasicSeries<K, ?>> uncoordinated) {

        final K first = BasicSeries.findLatestFirstKey(uncoordinated);
        final K last = BasicSeries.findEarliestLastKey(uncoordinated);

        final SortedSet<K> relevantKeys = new TreeSet<>();
        for (final BasicSeries<K, ?> individual : uncoordinated) {
            relevantKeys.addAll(individual.subMap(first, last).keySet());
        }
        relevantKeys.add(last);

        final int numberOfSeries = uncoordinated.size();
        final int numberOfKeys = relevantKeys.size();

        final PrimitiveSeries[] coordinated = new PrimitiveSeries[numberOfSeries];

        for (int s = 0; s < numberOfSeries; s++) {
            final BasicSeries<K, ?> inputSeries = uncoordinated.get(s);
            final double[] outputSeries = new double[numberOfKeys];

            double tmpVal = Double.NaN;
            double curVal = Double.NaN;

            int k = 0;
            for (final K key : relevantKeys) {
                tmpVal = inputSeries.doubleValue(key);
                if (Double.isNaN(tmpVal)) {
                    tmpVal = curVal;
                }
                outputSeries[k] = tmpVal;
                curVal = tmpVal;
                k++;
            }
            coordinated[s] = DataSeries.wrap(outputSeries);
        }

        return new CoordinatedSet<>(coordinated, first, last);
    }

    private final PrimitiveSeries[] myCoordinated;
    private final K myFirstKey;
    private final K myLastKey;

    private CoordinatedSet(final PrimitiveSeries[] coordinated, final K first, final K last) {

        super();

        myCoordinated = coordinated;
        myFirstKey = first;
        myLastKey = last;
    }

    public K getFirstKey() {
        return myFirstKey;
    }

    public K getLastKey() {
        return myLastKey;
    }

    public PrimitiveSeries getSeries(final int index) {
        return myCoordinated[index];
    }

    public int size() {
        return myCoordinated.length;
    }

}

@@ -1147,16 +1147,41 @@
 itive;%0A%0A
+import java.util.Arrays;%0A
 import j
@@ -1349,24 +1349,59 @@
 uper K%3E%3E %7B%0A%0A
+    @SuppressWarnings(%22unchecked%22)%0A
     public s
@@ -1463,22 +1463,202 @@
 K%3E from(
-final 
+BasicSeries%3CK, ?%3E... uncoordinated) %7B%0A        return CoordinatedSet.from(Arrays.asList(uncoordinated));%0A    %7D%0A%0A    public static %3CK extends Comparable%3C? super K%3E%3E CoordinatedSet%3CK%3E from(
 List%3C? e
@@ -1709,22 +1709,16 @@
         
-final 
 K first 
@@ -1774,22 +1774,16 @@
         
-final 
 K last =
@@ -1840,22 +1840,16 @@
         
-final 
 SortedSe
@@ -1898,22 +1898,16 @@
    for (
-final 
 BasicSer
@@ -2063,38 +2063,32 @@
 last);%0A%0A        
-final 
 int numberOfSeri
@@ -2122,22 +2122,16 @@
         
-final 
 int numb
@@ -2171,22 +2171,16 @@
         
-final 
 Primitiv
@@ -2304,22 +2304,16 @@
         
-final 
 BasicSer
@@ -2370,22 +2370,16 @@
         
-final 
 double%5B%5D
@@ -2542,22 +2542,16 @@
    for (
-final 
 K key : 
@@ -3113,22 +3113,16 @@
 atedSet(
-final 
 Primitiv
@@ -3143,22 +3143,16 @@
 dinated,
- final
  K first
@@ -3152,22 +3152,16 @@
 K first,
- final
  K last)
@@ -3441,22 +3441,16 @@
 tSeries(
-final 
 int inde

Update CoordinatedSet.java