Merge two homogeneous pre-sorted iterable inputs. - Java Data Structure

Java examples for Data Structure:Sort

Description

Merge two homogeneous pre-sorted iterable inputs.

Demo Code

/*//  w w w .  j  a va  2  s .c o  m
 * The MIT License
 *
 * Copyright 2013 RBC1B.
 *
 * 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.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Main{
    /**
     * Merge two homogeneous pre-sorted iterable inputs.
     * @param <T> Input type
     * @param left Left input
     * @param right Right input
     * @param comparator Comparator to compare inputs
     * @param callback Callback for each merge output
     */
    public static <T> void merge(Iterable<T> left, Iterable<T> right,
            final Comparator<T> comparator, Callback<T, T> callback) {
        merge(left, right, new Compare<T, T>() {
            @Override
            public int compare(T leftValue, T rightValue) {
                return comparator.compare(leftValue, rightValue);
            }
        }, callback);
    }
    /**
     * Merge two heterogenous pre-sorted iterable inputs.
     * @param <U> Left input type
     * @param <V> Right input type
     * @param left Left input
     * @param right Right input
     * @param comparator Comparator to compare inputs
     * @param callback Callback for each merge output
     */
    public static <U, V> void merge(Iterable<U> left, Iterable<V> right,
            Compare<U, V> comparator, Callback<U, V> callback) {
        Cursor<U> cursor1 = new IteratorCursor<U>(left.iterator());
        Cursor<V> cursor2 = new IteratorCursor<V>(right.iterator());

        while (cursor1.hasValue() && cursor2.hasValue()) {
            U value1 = cursor1.value();
            V value2 = cursor2.value();
            int n = comparator.compare(value1, value2);

            if (n == 0) {
                callback.output(value1, value2);
                cursor1.advance();
                cursor2.advance();
            } else if (n < 0) {
                callback.output(value1, null);
                cursor1.advance();
            } else {
                callback.output(null, value2);
                cursor2.advance();
            }
        }

        while (cursor1.hasValue()) {
            callback.output(cursor1.value(), null);
            cursor1.advance();
        }

        while (cursor2.hasValue()) {
            callback.output(null, cursor2.value());
            cursor2.advance();
        }
    }
}

Related Tutorials