Example usage for com.google.common.collect Multiset size

List of usage examples for com.google.common.collect Multiset size

Introduction

In this page you can find the example usage for com.google.common.collect Multiset size.

Prototype

int size();

Source Link

Document

Returns the number of elements in this collection.

Usage

From source file:org.simmetrics.metrics.Math.java

static <T> Multiset<T> union(Multiset<T> a, Multiset<T> b) {
    // Lager set first for performance improvement.
    // See: MathCaliper
    if (a.size() < b.size()) {
        return Multisets.union(b, a);
    }//from w ww .  jav a 2  s  .  c  o m

    return Multisets.union(a, b);
}

From source file:org.simmetrics.metrics.Math.java

static <T> Multiset<T> intersection(Multiset<T> a, Multiset<T> b) {
    // Smaller set first for performance improvement.
    // See: MathCaliper
    if (a.size() < b.size()) {
        return Multisets.intersection(a, b);
    }// w ww  .  j  a  v  a  2s .c om

    return Multisets.intersection(b, a);
}

From source file:org.apache.mahout.math.stats.LogLikelihood.java

/**
 * Compares two sets of counts to see which items are interestingly over-represented in the first
 * set./*from  ww w. j a v a  2 s  . co  m*/
 * @param a  The first counts.
 * @param b  The reference counts.
 * @param maxReturn  The maximum number of items to return.  Use maxReturn >= a.elementSet.size() to return all
 * scores above the threshold.
 * @param threshold  The minimum score for items to be returned.  Use 0 to return all items more common
 * in a than b.  Use -Double.MAX_VALUE (not Double.MIN_VALUE !) to not use a threshold.
 * @return  A list of scored items with their scores.
 */
public static <T> List<ScoredItem<T>> compareFrequencies(Multiset<T> a, Multiset<T> b, int maxReturn,
        double threshold) {
    int totalA = a.size();
    int totalB = b.size();

    Ordering<ScoredItem<T>> byScoreAscending = new Ordering<ScoredItem<T>>() {
        @Override
        public int compare(ScoredItem<T> tScoredItem, ScoredItem<T> tScoredItem1) {
            return Double.compare(tScoredItem.score, tScoredItem1.score);
        }
    };
    Queue<ScoredItem<T>> best = new PriorityQueue<ScoredItem<T>>(maxReturn + 1, byScoreAscending);

    for (T t : a.elementSet()) {
        compareAndAdd(a, b, maxReturn, threshold, totalA, totalB, best, t);
    }

    // if threshold >= 0 we only iterate through a because anything not there can't be as or more common than in b.
    if (threshold < 0) {
        for (T t : b.elementSet()) {
            // only items missing from a need be scored
            if (a.count(t) == 0) {
                compareAndAdd(a, b, maxReturn, threshold, totalA, totalB, best, t);
            }
        }
    }

    List<ScoredItem<T>> r = Lists.newArrayList(best);
    Collections.sort(r, byScoreAscending.reverse());
    return r;
}

From source file:io.ssc.trackthetrackers.analysis.stats.LogLikelihood.java

/**
 * Compares two sets of counts to see which items are interestingly over-represented in the first
 * set.//from  www  .j  a  va  2s  .  co  m
 * @param a  The first counts.
 * @param b  The reference counts.
 * @param maxReturn  The maximum number of items to return.  Use maxReturn >= a.elementSet.size() to return all
 * scores above the threshold.
 * @param threshold  The minimum score for items to be returned.  Use 0 to return all items more common
 * in a than b.  Use -Double.MAX_VALUE (not Double.MIN_VALUE !) to not use a threshold.
 * @return  A list of scored items with their scores.
 */
public static <T> List<ScoredItem<T>> compareFrequencies(Multiset<T> a, Multiset<T> b, int maxReturn,
        double threshold) {
    int totalA = a.size();
    int totalB = b.size();

    Ordering<ScoredItem<T>> byScoreAscending = new Ordering<ScoredItem<T>>() {
        @Override
        public int compare(ScoredItem<T> tScoredItem, ScoredItem<T> tScoredItem1) {
            return Double.compare(tScoredItem.score, tScoredItem1.score);
        }
    };
    Queue<ScoredItem<T>> best = new PriorityQueue<ScoredItem<T>>(maxReturn + 1, byScoreAscending);

    for (T t : a.elementSet()) {
        compareAndAdd(a, b, maxReturn, threshold, totalA, totalB, best, t);
    }

    // if threshold >= 0 we only iterate through a because anything not there can't be as or more common than in b.
    if (threshold < 0) {
        for (T t : b.elementSet()) {
            // only items missing from a need be scored
            if (a.count(t) == 0) {
                compareAndAdd(a, b, maxReturn, threshold, totalA, totalB, best, t);
            }
        }
    }

    List<ScoredItem<T>> r = new ArrayList<ScoredItem<T>>(best);
    Collections.sort(r, byScoreAscending.reverse());
    return r;
}

From source file:com.scaleunlimited.cascading.ml.LogLikelihood.java

/**
 * Compares two sets of counts to see which items are interestingly
 * over-represented in the first set./*w  w w .j  av a 2  s  . c o m*/
 * 
 * @param a
 *            The first counts.
 * @param b
 *            The reference counts.
 * @param maxReturn
 *            The maximum number of items to return. Use maxReturn >=
 *            a.elementSet.size() to return all scores above the threshold.
 * @param threshold
 *            The minimum score for items to be returned. Use 0 to return
 *            all items more common in a than b. Use -Double.MAX_VALUE (not
 *            Double.MIN_VALUE !) to not use a threshold.
 * @return A list of scored items with their scores.
 */
public static <T> List<ScoredItem<T>> compareFrequencies(Multiset<T> a, Multiset<T> b, int maxReturn,
        double threshold) {
    int totalA = a.size();
    int totalB = b.size();

    Ordering<ScoredItem<T>> byScoreAscending = new Ordering<ScoredItem<T>>() {
        @Override
        public int compare(ScoredItem<T> tScoredItem, ScoredItem<T> tScoredItem1) {
            return Double.compare(tScoredItem.score, tScoredItem1.score);
        }
    };
    Queue<ScoredItem<T>> best = new PriorityQueue<ScoredItem<T>>(maxReturn + 1, byScoreAscending);

    for (T t : a.elementSet()) {
        compareAndAdd(a, b, maxReturn, threshold, totalA, totalB, best, t);
    }

    // if threshold >= 0 we only iterate through a because anything not
    // there can't be as or more common than in b.
    if (threshold < 0) {
        for (T t : b.elementSet()) {
            // only items missing from a need be scored
            if (a.count(t) == 0) {
                compareAndAdd(a, b, maxReturn, threshold, totalA, totalB, best, t);
            }
        }
    }

    List<ScoredItem<T>> r = Lists.newArrayList(best);
    Collections.sort(r, byScoreAscending.reverse());
    return r;
}

From source file:org.onebusaway.nyc.vehicle_tracking.impl.particlefilter.ParticleFilter.java

/**
 * Low variance sampler. Follows Thrun's example in Probabilistic Robots.
 * @throws ParticleFilterException //from w  ww. j a  v  a  2  s.c  o m
 */
public static Multiset<Particle> lowVarianceSampler(Multiset<Particle> particles, double M)
        throws BadProbabilityParticleFilterException {
    Preconditions.checkArgument(particles.size() > 0);
    Preconditions.checkArgument(M > 0);

    final Multiset<Particle> resampled = HashMultiset.create((int) M);
    final double r = ParticleFactoryImpl.getLocalRng().nextDouble() / M;
    final Iterator<Particle> pIter = particles.iterator();
    Particle p = pIter.next();
    double c = p.getLogNormedWeight() - FastMath.log(particles.count(p));
    for (int m = 0; m < M; ++m) {
        final double U = FastMath.log(r + m / M);
        while (U > c && pIter.hasNext()) {
            p = pIter.next();
            c = LogMath.add(p.getLogNormedWeight() - FastMath.log(particles.count(p)), c);
        }
        resampled.add(p);
    }

    if (resampled.size() != M)
        throw new BadProbabilityParticleFilterException("low variance sampler did not return a valid sample");

    return resampled;
}

From source file:io.prestosql.tests.QueryAssertions.java

public static void assertEqualsIgnoreOrder(Iterable<?> actual, Iterable<?> expected, String message) {
    assertNotNull(actual, "actual is null");
    assertNotNull(expected, "expected is null");

    ImmutableMultiset<?> actualSet = ImmutableMultiset.copyOf(actual);
    ImmutableMultiset<?> expectedSet = ImmutableMultiset.copyOf(expected);
    if (!actualSet.equals(expectedSet)) {
        Multiset<?> unexpectedRows = Multisets.difference(actualSet, expectedSet);
        Multiset<?> missingRows = Multisets.difference(expectedSet, actualSet);
        int limit = 100;
        fail(format(/*from www .j a va 2s  .  c o  m*/
                "%snot equal\n" + "Actual rows (up to %s of %s extra rows shown, %s rows in total):\n    %s\n"
                        + "Expected rows (up to %s of %s missing rows shown, %s rows in total):\n    %s\n",
                message == null ? "" : (message + "\n"), limit, unexpectedRows.size(), actualSet.size(),
                Joiner.on("\n    ").join(Iterables.limit(unexpectedRows, limit)), limit, missingRows.size(),
                expectedSet.size(), Joiner.on("\n    ").join(Iterables.limit(missingRows, limit))));
    }
}

From source file:org.simmetrics.metrics.EuclideanDistance.java

@Override
public float compare(Multiset<T> a, Multiset<T> b) {

    if (a.isEmpty() && b.isEmpty()) {
        return 1.0f;
    }/*from  w  ww .j  ava 2  s. c  o  m*/

    float maxDistance = (float) sqrt((a.size() * a.size()) + (b.size() * b.size()));
    return 1.0f - distance(a, b) / maxDistance;
}

From source file:org.javafunk.matchbox.implementations.HasOnlyItemsInAnyOrderMatcher.java

@Override
protected boolean matchesSafely(final Iterable<E> actualItems, Description description) {
    boolean matches = true;
    final Multiset<E> actualMultiset = HashMultiset.create(actualItems);

    description.appendText("got ");

    if (actualMultiset.size() > 0) {
        description.appendValueList("", ", ", "", actualItems);
    } else {//from   www  . ja v  a2  s.c  om
        description.appendText("empty collection");
        if (expectedMultiset.size() != 0) {
            return false;
        }
    }

    if (actualMultiset.size() != expectedMultiset.size()) {
        description.appendText("\n").appendText("got collection with size ").appendValue(actualMultiset.size())
                .appendText(" rather than ").appendValue(expectedMultiset.size());
        matches = false;
    }

    if (checkForDifferences(expectedMultiset, actualMultiset, description, "expected but didn't get ")) {
        matches = false;
    }
    if (checkForDifferences(actualMultiset, expectedMultiset, description, "got but didn't expect ")) {
        matches = false;
    }

    return matches;
}

From source file:org.javafunk.matchbox.implementations.HasOnlyItemsInAnyOrderMatcher.java

private boolean checkForDifferences(Multiset<E> expectedSet, Multiset<E> actualSet, Description description,
        String message) {//from  w  w w .j av  a  2s  .c o  m
    Multiset<E> differences = HashMultiset.create(expectedSet);
    for (E item : actualSet) {
        differences.remove(item);
    }
    if (differences.size() > 0) {
        description.appendText("\n").appendText(message).appendValueList("", ", ", "", differences);
        return true;
    }
    return false;
}