Java List Join joinLists(List A, List B, double epsilon)

Here you can find the source of joinLists(List A, List B, double epsilon)

Description

Join together 2 ordered lists of doubles into a single, ordered list without duplicates.

License

MIT License

Parameter

Parameter Description
lists The lists to be joined
A The first list
B The second list
epsilon The difference within which two doubles are considered equal

Return

The conjoined list

Declaration

public static List<Double> joinLists(List<Double> A, List<Double> B, double epsilon) 

Method Source Code

//package com.java2s;
/**//from  w w  w  . j  a va  2 s .c  om
 * Copyright (c) 2013 Oculus Info Inc.
 * http://www.oculusinfo.com/
 *
 * Released under the 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.
 */

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**
     * Join together 2 ordered lists of doubles into a single, ordered list
     * without duplicates.
     * 
     * @param lists The lists to be joined
     * 
     * @param A The first list
     * @param B The second list
     * @param epsilon The difference within which two doubles are considered
     *            equal
     * @return The conjoined list
     */
    public static List<Double> joinLists(List<Double> A, List<Double> B, double epsilon) {
        List<Double> C = new ArrayList<Double>();
        int nA = 0;
        int NA = A.size();
        double a = A.get(nA);

        int nB = 0;
        int NB = B.size();
        double b = B.get(nB);

        while (nA < NA || nB < NB) {
            if (equal(a, b, epsilon)) {
                // A bit silly, since they are already shown equal, but just in
                // case of large epsilon, it'd be nice to be sure
                C.add((a + b) / 2);
                while (nA < NA && equal(a, A.get(nA), epsilon))
                    ++nA;
                a = (nA < NA ? A.get(nA) : Double.MAX_VALUE);
                while (nB < NB && equal(b, B.get(nB), epsilon))
                    ++nB;
                b = (nB < NB ? B.get(nB) : Double.MAX_VALUE);
            } else if (a < b) {
                C.add(a);
                ++nA;
                a = (nA < NA ? A.get(nA) : Double.MAX_VALUE);
            } else {
                C.add(b);
                ++nB;
                b = (nB < NB ? B.get(nB) : Double.MAX_VALUE);
            }
        }

        return C;
    }

    private static boolean equal(double a, double b, double epsilon) {
        return Math.abs(a - b) < epsilon;
    }
}

Related

  1. joinList(List list, String glue)
  2. joinList(List list, String separator)
  3. joinList(List parts, String sep)
  4. joinList(String separater, List list)
  5. joinLists( final List> lists)
  6. joinLists(List... list)
  7. joinListToString(List lsStr, String sSeparator)
  8. joinLong(List lst, String prefix)
  9. joinNiceString(List strings)