Example usage for com.google.common.collect Sets intersection

List of usage examples for com.google.common.collect Sets intersection

Introduction

In this page you can find the example usage for com.google.common.collect Sets intersection.

Prototype

public static <E> SetView<E> intersection(final Set<E> set1, final Set<?> set2) 

Source Link

Document

Returns an unmodifiable view of the intersection of two sets.

Usage

From source file:edu.usc.ee599.NaiveTriangleCounting.java

public static void main(String[] args) throws Exception {

    GraphGenerator generator = new SimpleGraphGenerator("/home/charith/Downloads/Email-Enron.txt");

    generator.initialize();/*  w ww  . ja v a  2s  .c o  m*/

    long startTime = System.currentTimeMillis();

    Edge e = generator.nextEdge();
    int edgeCount = 0;
    while (e != null) {
        edgeCount++;
        if (edgeSet.contains(e)) {
            e = generator.nextEdge();
            continue;
        } else {

            if (edgeMap.get(e.source) != null && edgeMap.get(e.sink) != null) {
                Sets.SetView<Integer> setView = Sets.intersection(edgeMap.get(e.source), edgeMap.get(e.sink));

                for (int common : setView) {

                    Triangle t = new Triangle(e.source, e.sink, common);

                    if (!triangles.contains(t)) {
                        triangles.add(t);
                        count++;

                    }

                }

            }

            edgeSet.add(e);

            if (edgeMap.containsKey(e.source)) {

                edgeMap.get(e.source).add(e.sink);

            } else {
                Set<Integer> set = new HashSet<Integer>();
                set.add(e.sink);

                edgeMap.put(e.source, set);
            }

            if (edgeMap.containsKey(e.sink)) {

                edgeMap.get(e.sink).add(e.source);

            } else {
                Set<Integer> set = new HashSet<Integer>();
                set.add(e.source);

                edgeMap.put(e.sink, set);
            }

        }

        e = generator.nextEdge();
    }

    long endTime = System.currentTimeMillis();

    System.out.println(" Time Spent(ms) : " + (endTime - startTime));

    System.out.println("Rate (edges/sec):" + (double) (edgeCount * 1000) / (double) (endTime - startTime));

    System.out.println(" Triangle Count: " + count);

}

From source file:squash.tools.FakeBookingCreator.java

public static void main(String[] args) throws IOException {
    int numberOfDays = 21;
    int numberOfCourts = 5;
    int maxCourtSpan = 5;
    int numberOfSlots = 16;
    int maxSlotSpan = 3;
    int minSurnameLength = 2;
    int maxSurnameLength = 20;
    int minBookingsPerDay = 0;
    int maxBookingsPerDay = 8;
    LocalDate startDate = LocalDate.of(2016, 7, 5);

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    List<Booking> bookings = new ArrayList<>();
    for (LocalDate date = startDate; date.isBefore(startDate.plusDays(numberOfDays)); date = date.plusDays(1)) {
        int numBookings = ThreadLocalRandom.current().nextInt(minBookingsPerDay, maxBookingsPerDay + 1);
        List<Booking> daysBookings = new ArrayList<>();
        for (int bookingIndex = 0; bookingIndex < numBookings; bookingIndex++) {
            String player1 = RandomStringUtils.randomAlphabetic(1) + "." + RandomStringUtils.randomAlphabetic(
                    ThreadLocalRandom.current().nextInt(minSurnameLength, maxSurnameLength + 1));
            String player2 = RandomStringUtils.randomAlphabetic(1) + "." + RandomStringUtils.randomAlphabetic(
                    ThreadLocalRandom.current().nextInt(minSurnameLength, maxSurnameLength + 1));

            Set<ImmutablePair<Integer, Integer>> bookedCourts = new HashSet<>();
            daysBookings.forEach((booking) -> {
                addBookingToSet(booking, bookedCourts);
            });//from  ww w. j  av a 2 s.c  om

            Booking booking;
            Set<ImmutablePair<Integer, Integer>> courtsToBook = new HashSet<>();
            do {
                // Loop until we create a booking of free courts
                int court = ThreadLocalRandom.current().nextInt(1, numberOfCourts + 1);
                int courtSpan = ThreadLocalRandom.current().nextInt(1,
                        Math.min(maxCourtSpan + 1, numberOfCourts - court + 2));
                int slot = ThreadLocalRandom.current().nextInt(1, numberOfSlots + 1);
                int slotSpan = ThreadLocalRandom.current().nextInt(1,
                        Math.min(maxSlotSpan + 1, numberOfSlots - slot + 2));
                booking = new Booking(court, courtSpan, slot, slotSpan, player1 + "/" + player2);
                booking.setDate(date.format(formatter));
                courtsToBook.clear();
                addBookingToSet(booking, courtsToBook);
            } while (Boolean.valueOf(Sets.intersection(courtsToBook, bookedCourts).size() > 0));

            daysBookings.add(booking);
        }
        bookings.addAll(daysBookings);
    }

    // Encode bookings as JSON
    // Create the node factory that gives us nodes.
    JsonNodeFactory factory = new JsonNodeFactory(false);
    // Create a json factory to write the treenode as json.
    JsonFactory jsonFactory = new JsonFactory();
    ObjectNode rootNode = factory.objectNode();

    ArrayNode bookingsNode = rootNode.putArray("bookings");
    for (int i = 0; i < bookings.size(); i++) {
        Booking booking = bookings.get(i);
        ObjectNode bookingNode = factory.objectNode();
        bookingNode.put("court", booking.getCourt());
        bookingNode.put("courtSpan", booking.getCourtSpan());
        bookingNode.put("slot", booking.getSlot());
        bookingNode.put("slotSpan", booking.getSlotSpan());
        bookingNode.put("name", booking.getName());
        bookingNode.put("date", booking.getDate());
        bookingsNode.add(bookingNode);
    }
    // Add empty booking rules array - just so restore works
    rootNode.putArray("bookingRules");
    rootNode.put("clearBeforeRestore", true);

    try (JsonGenerator generator = jsonFactory.createGenerator(new File("FakeBookings.json"),
            JsonEncoding.UTF8)) {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializationInclusion(Include.NON_EMPTY);
        mapper.setSerializationInclusion(Include.NON_NULL);
        mapper.writeTree(generator, rootNode);
    }
}

From source file:th.algorithms.propinquitydynamics.utils.CalculationTable.java

public static Set<Integer> CalculateCrr(Set<Integer> Nr, Set<Integer> nnNr) {

    return Sets.intersection(Nr, nnNr);
}

From source file:th.algorithms.propinquitydynamics.utils.CalculationTable.java

public static Set<Integer> CalculateCri(Set<Integer> Nr, Set<Integer> Ni, Set<Integer> nnNr,
        Set<Integer> nnNi) {
    // (Nr ^ nnNi) + (Ni ^ nnNr) + (Ni ^ nnNi)
    return Sets.union(Sets.union(Sets.intersection(Nr, nnNi), Sets.intersection(Ni, nnNr)),
            Sets.intersection(Ni, nnNi)).copyInto(new HashSet<Integer>(20));
}

From source file:de.faustedition.reasoning.InscriptionRelations.java

public static boolean areParadigmaticallyRelated(Inscription i, Inscription j) {
    final int intersectionSize = Sets.intersection(i, j).size();
    return (intersectionSize >= (COMMON_RATIO * i.size())) && (intersectionSize >= (COMMON_RATIO * j.size()));
}

From source file:th.algorithms.propinquitydynamics.utils.CalculationTable.java

public static Set<Integer> CalculateCrd(Set<Integer> Nr, Set<Integer> Nd, Set<Integer> nnNr,
        Set<Integer> nnNd) {
    // (Nr ^ nnNd) + (Nd ^ nnNr) + (Nd ^ nnNd)
    return Sets.union(Sets.union(Sets.intersection(Nr, nnNd), Sets.intersection(Nd, nnNr)),
            Sets.intersection(Nd, nnNd)).copyInto(new HashSet<Integer>(20));
}

From source file:com.facebook.buck.util.MoreSets.java

/**
 * Returns a new and mutable set containing the intersection of the
 * two specified sets. Using the smaller of the two sets as the base for
 * finding the intersection for performance reasons.
 *///from   www .  j  a  v  a  2 s  .  c  o  m
public static <T> Set<T> intersection(Set<T> x, Set<T> y) {
    Set<T> result = new LinkedHashSet<>();
    if (x.size() > y.size()) {
        Sets.intersection(y, x).copyInto(result);
    } else {
        Sets.intersection(x, y).copyInto(result);
    }
    return result;
}

From source file:com.google.devtools.build.xcode.util.Intersection.java

/**
 * Returns the intersection of two sets.
 *//*ww w  .  ja v a2  s.  c  o  m*/
public static <T> Set<T> of(Set<T> set1, Set<T> set2) {
    return Sets.intersection(set1, set2);
}

From source file:org.eclipse.buildship.core.workspace.internal.ManagedModelMergingStrategy.java

/**
 * Calculates the updated state.//from   w w w. j  a v a  2 s.c o m
 *
 * @param current elements currently defined on the project
 * @param model elements defined in the Gradle model
 * @param managed elements managed by Buildship
 * @return the description of the updated state
 */
public static <T> Result<T> calculate(Set<T> current, Set<T> model, Set<T> managed) {
    Set<T> missing = Sets.difference(current, model);
    Set<T> removed = Sets.intersection(missing, managed);
    Set<T> notRemoved = Sets.difference(missing, removed);
    Set<T> added = Sets.difference(model, current);
    Set<T> nextElements = Sets.union(model, notRemoved);
    Set<T> nextManaged = Sets.difference(Sets.union(managed, added), removed);

    return new Result<T>(nextElements, nextManaged);
}

From source file:com.isotrol.impe3.core.impl.AbstractParameters.java

static <T> Set<T> intersect(Set<T> initial, Set<T> included) {
    if (included == null || included.isEmpty()) {
        return ImmutableSet.of();
    }// w w w . j av  a2  s . c o  m
    return Sets.intersection(initial, included);
}