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

com.cinchapi.concourse.util.TSets Maven / Gradle / Ivy

/*
 * Copyright (c) 2013-2017 Cinchapi Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.cinchapi.concourse.util;

import java.util.Comparator;
import java.util.Iterator;
import java.util.Set;
import java.util.SortedSet;

import com.google.common.base.Preconditions;
import com.google.common.collect.Sets;

/**
 * Utilities for {@link Set} objects. This class is mostly designed to offer
 * performance improvements over some of the operations in the Guava
 * {@link Sets} class.
 * 
 * @author Jeff Nelson
 */
public class TSets {

    /**
     * Return a Set that contains only the elements that are in both {@code a}
     * and {@code b}.
     * 
     * @param a
     * @param b
     * @return the intersection of the Sets
     */
    public static  Set intersection(Set a, Set b) {
        if(a instanceof SortedSet && b instanceof SortedSet) {
            return sortedIntersection((SortedSet) a, (SortedSet) b);
        }
        else {
            Set intersection = Sets.newLinkedHashSet();
            Set smaller = a.size() <= b.size() ? a : b;
            Set larger = Sets.newHashSet(a.size() > b.size() ? a : b);
            for (T element : smaller) {
                if(larger.contains(element)) {
                    intersection.add(element);
                }
            }
            return intersection;
        }
    }

    /**
     * Return a {@link Set} that contains long integers {@code from} the first
     * parameter {@code to} the last one in sequential order.
     * 
     * @param from the first number (inclusive)
     * @param to the second number (inclusive)
     * @return a {@link Set} with numbers
     */
    public static Set sequence(long from, long to) {
        Preconditions.checkArgument(from <= to);
        int size = (int) (to - from);
        Set sequence = Sets.newLinkedHashSetWithExpectedSize(size);
        for (long i = from; i <= to; i++) {
            sequence.add(i);
        }
        return sequence;
    }

    /**
     * Return a Set that contains all the elements in both {@code a} and
     * {@code b}.
     * 
     * @param a
     * @param b
     * @return the union of the Sets
     */
    public static  Set union(Set a, Set b) {
        Set union = Sets.newLinkedHashSet(a);
        union.addAll(b);
        return union;
    }

    /**
     * Perform an optimized intersection calculation, assuming that both
     * {@code a} and {@code b} are sorted sets.
     * 
     * @param a
     * @param b
     * @return the intersection of the Sets
     */
    @SuppressWarnings("unchecked")
    private static  Set sortedIntersection(SortedSet a,
            SortedSet b) {
        Set intersection = Sets.newLinkedHashSet();
        if(a.isEmpty() || b.isEmpty()) {
            return intersection;
        }
        Iterator ait = a.iterator();
        Iterator bit = b.iterator();
        Comparator comp = a.comparator();
        boolean ago = true;
        boolean bgo = true;
        T aelt = null;
        T belt = null;
        while (((ago && ait.hasNext()) || !ago)
                && ((bgo && bit.hasNext()) || !bgo)) {
            aelt = ago ? ait.next() : aelt;
            belt = bgo ? bit.next() : belt;
            int order = comp == null ? ((Comparable) aelt).compareTo(belt)
                    : comp.compare(aelt, belt);
            if(order == 0) {
                intersection.add(aelt);
                ago = true;
                bgo = true;
            }
            else if(order > 0) {
                bgo = true;
                ago = false;
            }
            else {
                ago = true;
                bgo = false;
            }
        }
        return intersection;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy