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

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

Introduction

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

Prototype

Set<E> elementSet();

Source Link

Document

Returns the set of distinct elements contained in this multiset.

Usage

From source file:com.github.fhirschmann.clozegen.lib.util.MultisetUtils.java

/**
 * Returns a limited list of all (distinct) elements of a multiset ordered
 * by their counts.//from ww  w .j a  v a 2 s  . com
 *
 * @param <E> the type of the multiset elements
 * @param multiset the multiset to work on
 * @param limit the maximum number of elements to return
 * @return a limited list of elements ordered by their count
 */
public static <E> List<E> sortedElementList(final Multiset<E> multiset, final int limit) {
    final List<E> list = Lists.newLinkedList();
    final LinkedHashMultiset<E> sms = sortMultiSet(multiset);
    int newlimit = limit;

    if (newlimit > multiset.elementSet().size()) {
        throw new IllegalArgumentException("The multiset does not contain that many keys.");
    } else if (newlimit == -1) {
        newlimit = multiset.elementSet().size();
    }

    final Iterator<E> it = sms.iterator();

    E next;
    while (list.size() < newlimit) {
        next = it.next();
        if (!list.contains(next)) {
            list.add(next);
        }
    }
    return list;
}

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  . j a  va2  s  . co  m*/
    return "[" + Joiner.on(",").join(ss) + "]";
}

From source file:org.caleydo.view.domino.internal.NodeSelections.java

public static Set<Block> getFullBlocks(Set<NodeGroup> selection) {
    if (selection.isEmpty())
        return Collections.emptySet();
    Set<Node> nodes = getFullNodes(selection);
    if (nodes.isEmpty())
        return Collections.emptySet();
    Multiset<Block> blocks = HashMultiset.create();
    for (Node node : nodes) {
        Block n = node.getBlock();/*from w  ww .  ja  v a  2s . co m*/
        blocks.add(n);
    }
    for (Iterator<Block> it = blocks.elementSet().iterator(); it.hasNext();) {
        Block block = it.next();
        if (block.nodeCount() != blocks.count(block)) {
            it.remove();// not all groups
        }
    }
    return blocks.elementSet();
}

From source file:org.apache.aurora.scheduler.async.preemptor.PendingTaskProcessor.java

/**
 * Creates execution sequence for pending task groups by interleaving their unique occurrences.
 * For example: {G1, G1, G1, G2, G2} will be converted into {G1, G2, G1, G2, G1}.
 *
 * @param groups Multiset of task groups.
 * @return A task group execution sequence.
 *//*from w  ww .  j  a v a 2  s .c  o  m*/
private static List<TaskGroupKey> getPreemptionSequence(Multiset<TaskGroupKey> groups) {
    Multiset<TaskGroupKey> mutableGroups = HashMultiset.create(groups);
    List<TaskGroupKey> instructions = Lists.newLinkedList();
    Set<TaskGroupKey> keys = ImmutableSet.copyOf(groups.elementSet());
    while (!mutableGroups.isEmpty()) {
        for (TaskGroupKey key : keys) {
            if (mutableGroups.contains(key)) {
                instructions.add(key);
                mutableGroups.remove(key);
            }
        }
    }

    return instructions;
}

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");
        }/*from www.j  a v a  2 s  .  co 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);
    }/*from w  w  w. j  a  v a 2  s . co m*/
    if (normalize) {
        return v.assign(Functions.div(v.norm(2)));
    } else {
        return v;
    }
}

From source file:org.caleydo.view.domino.internal.NodeSelections.java

private static Set<Node> getNodes(Set<NodeGroup> selection, boolean checkFull) {
    if (selection.isEmpty())
        return Collections.emptySet();
    Multiset<Node> nodes = HashMultiset.create();
    for (NodeGroup group : selection) {
        Node n = group.getNode();
        nodes.add(n);/* ww  w .j ava  2s  .co  m*/
    }
    if (checkFull) {
        for (Iterator<Node> it = nodes.elementSet().iterator(); it.hasNext();) {
            Node node = it.next();
            final int expected = node.groupCount();
            if (expected != nodes.count(node)) {
                it.remove();// not all groups
            }
        }
    }
    return nodes.elementSet();
}

From source file:org.caleydo.view.domino.internal.toolbar.NodeTools.java

/**
 * @param actives//www .  jav a  2  s .c o m
 * @return
 */
private static <T> T mostFrequent(Multiset<T> sets) {
    if (sets.isEmpty())
        return null;
    Set<T> elems = sets.elementSet();
    T maxV = elems.iterator().next();
    int max = sets.count(maxV);
    for (T elem : elems) {
        int c = sets.count(elem);
        if (c > max) {
            max = c;
            maxV = elem;
        }
    }
    return maxV;
}

From source file:com.google.idea.blaze.java.sync.source.SourceDirectoryCalculator.java

@Nullable
private static <T> T pickMostFrequentlyOccurring(Multiset<T> set, String prefer) {
    T best = null;//  ww w.j a v a  2  s  .co m
    int bestCount = 0;

    for (T candidate : set.elementSet()) {
        int candidateCount = set.count(candidate);
        if (candidateCount > bestCount || (candidateCount == bestCount && candidate.equals(prefer))) {
            best = candidate;
            bestCount = candidateCount;
        }
    }
    return best;
}

From source file:org.apache.aurora.scheduler.preemptor.PendingTaskProcessor.java

/**
 * Creates execution sequence for pending task groups by interleaving batches of requested size of
 * their occurrences. For example: {G1, G1, G1, G2, G2} with batch size of 2 task per group will
 * be converted into {G1, G1, G2, G2, G1}.
 *
 * @param groups Multiset of task groups.
 * @param batchSize The batch size of tasks from each group to sequence together.
 * @return A task group execution sequence.
 */// w ww  .j a va 2 s.co  m
@VisibleForTesting
static List<TaskGroupKey> getPreemptionSequence(Multiset<TaskGroupKey> groups, int batchSize) {

    Preconditions.checkArgument(batchSize > 0, "batchSize should be positive.");

    Multiset<TaskGroupKey> mutableGroups = HashMultiset.create(groups);
    List<TaskGroupKey> instructions = Lists.newLinkedList();
    Set<TaskGroupKey> keys = ImmutableSet.copyOf(groups.elementSet());
    while (!mutableGroups.isEmpty()) {
        for (TaskGroupKey key : keys) {
            if (mutableGroups.contains(key)) {
                int elementCount = mutableGroups.remove(key, batchSize);
                int removedCount = Math.min(elementCount, batchSize);
                instructions.addAll(Collections.nCopies(removedCount, key));
            }
        }
    }

    return instructions;
}