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

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

Introduction

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

Prototype

int count(@Nullable Object element);

Source Link

Document

Returns the number of occurrences of an element in this multiset (the count of the element).

Usage

From source file:org.javafunk.funk.Sets.java

public static <T> Set<T> symmetricDifference(Iterable<? extends Iterable<? extends T>> iterables) {
    final Multiset<T> unionMultiset = Multisets.concatenate(iterables);
    return asSet(filter(unionMultiset, new Predicate<T>() {
        public boolean evaluate(T element) {
            return isOdd(unionMultiset.count(element));
        }/*from  w w w. j  a  v  a2s.co  m*/

        private boolean isOdd(Integer value) {
            return value % 2 == 1;
        }
    }));
}

From source file:org.apache.drill.plan.ParsePlan.java

private static void validate(Plan r) throws ValidationException {
    int errors = 0;
    Formatter errorMessages = new Formatter();

    // make sure that each output is assigned only once
    Multiset<Integer> counts = HashMultiset.create();
    int line = 1;
    for (Op op : r.getStatements()) {
        for (Arg assignment : op.getOutputs()) {
            int slot = ((Arg.Symbol) assignment).getSlot();
            counts.add(slot);/*from  w  w  w .ja v  a 2 s . c o m*/
            if (counts.count(slot) != 1) {
                errorMessages.format("Output symbol %%%d used more than once in statement %d\n", slot, line);
                errors++;
            }
        }
        line++;
    }

    // make sure that each input is defined at least once
    line = 1;
    for (Op op : r.getStatements()) {
        for (Arg reference : op.getInputs()) {
            if (reference instanceof Arg.Symbol) {
                int slot = ((Arg.Symbol) reference).getSlot();
                if (counts.count(slot) <= 0) {
                    errorMessages.format("Undefined reference to %%%d in statement %d\n", slot, line);
                    errors++;
                }
            }
        }
        line++;
    }

    if (errors > 0) {
        throw new ValidationException(errorMessages.toString());
    }
}

From source file:org.sonar.api.measures.MultisetDistributionFormat.java

static String format(Multiset countBag) {
    StringBuilder sb = new StringBuilder();
    boolean first = true;
    for (Object obj : countBag.elementSet()) {
        if (!first) {
            sb.append(KeyValueFormat.PAIR_SEPARATOR);
        }//from   w w w  .  jav  a  2  s  .co  m
        sb.append(obj.toString());
        sb.append(KeyValueFormat.FIELD_SEPARATOR);
        // -1 allows to include zero values
        sb.append(countBag.count(obj) - 1);
        first = false;
    }
    return sb.toString();
}

From source file:org.apache.lucene.benchmark.quality.mc.IntrinsicEvaluator.java

static void prune(Map<String, Multiset<String>> map, int minTF) {

    for (Map.Entry<String, Multiset<String>> entry : map.entrySet()) {
        Multiset<String> set = entry.getValue();

        Iterator<String> iterator = set.iterator();

        while (iterator.hasNext()) {

            String string = iterator.next();
            int count = set.count(string);

            if (count < minTF)
                iterator.remove();/*from w  w  w .ja v a  2  s.co  m*/

        }
    }
}

From source file:com.jeffreybosboom.lyne.Solver.java

/**
 * Returns the paths through the given solved puzzle, one per color, or null
 * if the solution paths are unsatisfying.
 * @param puzzle a solved puzzle/*from   w  w  w.ja  v a  2s .  c  om*/
 * @return the solution paths, one per color, or null
 */
private static Set<List<Node>> solutionPaths(Puzzle puzzle) {
    puzzle.getClass();
    checkArgument(puzzle.edges().allMatch(a -> puzzle.possibilities(a.first, a.second).size() == 1));
    ImmutableSet.Builder<List<Node>> pathsBuilder = ImmutableSet.builder();
    for (Iterator<Pair<Node, Node>> it = puzzle.terminals().iterator(); it.hasNext();) {
        Pair<Node, Node> pair = it.next();
        List<Node> path = new ArrayList<>();
        path.add(pair.first);
        path = findPath(puzzle, path, pair.second, new HashSet<>());
        if (path == null)
            return null;
        pathsBuilder.add(path);
    }
    ImmutableSet<List<Node>> paths = pathsBuilder.build();
    Multiset<Node> counts = HashMultiset.create();
    paths.stream().forEachOrdered(counts::addAll);
    //ensure each node appears enough times over all the paths
    //TODO: we check colored node appearances in findPath, so this could be
    //just octagons?
    if (!puzzle.nodes().allMatch(n -> counts.count(n) == (n.desiredEdges() + 1) / 2))
        return null;
    return paths;
}

From source file:com.googlecode.blaisemath.graph.mod.layout.StaticSpringLayout.java

private static String nicer(Multiset set) {
    List<String> ss = Lists.newArrayList();
    for (Object el : Sets.newTreeSet(set.elementSet())) {
        ss.add(el + ":" + set.count(el));
    }/*from ww w.ja  va2  s  .  co m*/
    return "[" + Joiner.on(",").join(ss) + "]";
}

From source file:org.javafunk.funk.Multisets.java

public static <T> Multiset<T> union(Iterable<? extends Iterable<? extends T>> iterables) {
    Multiset<T> unionMultiset = multisetFrom(first(iterables).get());
    for (Iterable<? extends T> iterable : rest(iterables)) {
        Multiset<T> currentMultiset = multisetFrom(iterable);
        for (T element : currentMultiset.elementSet()) {
            int numberInUnionMultiset = unionMultiset.count(element);
            int numberInCurrentMultiset = currentMultiset.count(element);
            if (numberInUnionMultiset < numberInCurrentMultiset) {
                unionMultiset.setCount(element, numberInCurrentMultiset);
            }/* w  w w. ja  v  a  2s .  co  m*/
        }
    }
    return unionMultiset;
}

From source file:org.sonar.graph.FeedbackCycle.java

public static List<FeedbackCycle> buildFeedbackCycles(Set<Cycle> cycles) {
    Multiset<Edge> edgesBag = createBagWithAllEdgesOfCycles(cycles);

    List<FeedbackCycle> feedbackCycles = new ArrayList<FeedbackCycle>();
    for (Cycle cycle : cycles) {
        FeedbackCycle feedbackCycle = new FeedbackCycle(cycle);
        int totalOccurrences = 0;
        for (Edge edge : cycle.getEdges()) {
            FeedbackEdge feedbackEdge = new FeedbackEdge(edge, edgesBag.count(edge));
            feedbackCycle.add(feedbackEdge);
            totalOccurrences += feedbackEdge.getOccurences();
        }//www  .  j  a v a 2s  .c o  m
        feedbackCycle.setTotalOccurrencesOfEdgesInCycle(totalOccurrences);
        Collections.sort(feedbackCycle.orderedFeedbackEdges);
        feedbackCycles.add(feedbackCycle);
    }
    Collections.sort(feedbackCycles);

    return feedbackCycles;
}

From source file:i5.las2peer.services.recommender.librec.util.Strings.java

public static <T> String toString(Collection<T> ts) {

    if (ts instanceof Multiset<?>) {

        StringBuilder sb = new StringBuilder();
        Multiset<T> es = (Multiset<T>) ts;

        for (T e : es.elementSet()) {
            int count = es.count(e);
            sb.append(e + ", " + count + "\n");
        }//ww  w  .  j av  a2 s  .c o m

        return sb.toString();
    }

    return toString(ts, ",");
}

From source file:org.apache.mahout.knn.tools.Vectorize20NewsGroups.java

static Vector vectorize(Multiset<String> doc, CorpusWeighting w, boolean normalize, int dimension) {
    Vector v = new RandomAccessSparseVector(dimension);
    FeatureVectorEncoder encoder = new StaticWordValueEncoder("text");
    for (String word : doc.elementSet()) {
        encoder.addToVector(word, w.weight(word, doc.count(word)), v);
    }// ww w.j  a  v a2  s.co m
    if (normalize) {
        return v.assign(Functions.div(v.norm(2)));
    } else {
        return v;
    }
}