下面列出了com.google.common.collect.Multiset#contains ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@SuppressWarnings("unchecked")
@Override
public boolean removeAll(Collection<?> collection) {
Multiset<E> previousContents = delegateCopy();
boolean changed = super.removeAll(collection);
if (changed) {
List<ElementarySubChange<E>> elementaryChanges = new ArrayList<>();
// collection may contain element multiple times; as we only want to
// notify once per element, we have to iterate over the set of
// unique elements
for (Object e : new HashSet<>(collection)) {
if (previousContents.contains(e)) {
// if the element was contained, its safe to cast to E in
// the following
if (previousContents.count(e) > count(e)) {
elementaryChanges.add(new ElementarySubChange<>((E) e,
previousContents.count(e), 0));
}
}
}
helper.fireValueChangedEvent(
new MultisetListenerHelper.AtomicChange<>(this,
previousContents, elementaryChanges));
}
return changed;
}
/** Calculate cached cost for structural EM-step */
private double calculateCachedCost(final Table<Sequence, Integer, Double> sequences,
final Multiset<Sequence> covering) {
double totalCost = 0;
int lenCovering = 0;
for (final Sequence seq : cachedSequences.rowKeySet()) {
if (sequences.containsRow(seq)) {
if (covering.contains(seq)) {
final int occur = covering.count(seq);
totalCost += -Math.log(sequences.get(seq, occur));
for (int m = 1; m <= occur; m++) {
totalCost += sumLogRange(lenCovering + 1, lenCovering + seq.size());
lenCovering += seq.size();
}
} else if (seq.size() == 1 && sum(cachedSequences.row(seq).values()) == 0.) {
continue; // ignore seqs used to fill incomplete coverings
} else {
totalCost += -Math.log(sequences.get(seq, 0));
}
}
}
return totalCost;
}
/**
* 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.
*/
@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;
}
/**
* Keeps the cumulated distance for all the common raw super types of the given references.
* Interfaces that are more directly implemented will get a lower total count than more general
* interfaces.
*/
protected void cumulateDistance(final List<LightweightTypeReference> references, Multimap<JvmType, LightweightTypeReference> all,
Multiset<JvmType> cumulatedDistance) {
for(LightweightTypeReference other: references) {
Multiset<JvmType> otherDistance = LinkedHashMultiset.create();
initializeDistance(other, all, otherDistance);
cumulatedDistance.retainAll(otherDistance);
for(Multiset.Entry<JvmType> typeToDistance: otherDistance.entrySet()) {
if (cumulatedDistance.contains(typeToDistance.getElement()))
cumulatedDistance.add(typeToDistance.getElement(), typeToDistance.getCount());
}
}
}
@Override
public boolean addAll(Collection<? extends E> collection) {
Multiset<E> previousContents = delegateCopy();
boolean changed = super.addAll(collection);
if (changed) {
List<ElementarySubChange<E>> elementaryChanges = new ArrayList<>();
// collection may contain element multiple times; as we only want to
// notify once per element, we have to iterate over the set of
// unique elements
for (E e : new HashSet<>(collection)) {
if (previousContents.contains(e)) {
// already contained
if (count(e) > previousContents.count(e)) {
elementaryChanges.add(new ElementarySubChange<>(e, 0,
count(e) - previousContents.count(e)));
}
} else {
// newly added
elementaryChanges
.add(new ElementarySubChange<>(e, 0, count(e)));
}
}
helper.fireValueChangedEvent(
new MultisetListenerHelper.AtomicChange<>(this,
previousContents, elementaryChanges));
}
return changed;
}
@ExpectWarning(value="GC", num=4)
public static void testMultiset(Multiset<String> ms) {
ms.contains(1);
ms.count(1);
ms.remove(1);
ms.remove(1, 2);
}
@NoWarning("GC")
public static void testMultisetOK(Multiset<String> ms) {
ms.contains("x");
ms.count("x");
ms.remove("x");
ms.remove("x", 2);
}
/**
* Taken from https://en.wikipedia.org/wiki/Topological_sorting#Depth-first_search
*
* <p>
* function visit(node n, list L)
* mark n as being "part of the current chain"
* for each node m with an edge from n to m do
* if n is not already "part of the current chain"
* if m is has not been visited yet then
* visit(m)
* else
* //A cycle should not be possible at this point
* mark n as being visited
* n is no longer "part of the current chain"
* add n to tail of L
* </p>
* @param current the next unvisited node from input
* @param result the sorted result
* @param currentChain the complete list of input
* @param lookup a lookup table to link URI's to their tripleMap objects
* @param onCycle code to handle a cycle if it occurs
* @param errorConsumer code to handle errors reported by this method (i.e. log it, throw it, do what you want)
*/
private static void sortStep(TriplesMapBuilder current, LinkedList<TriplesMapBuilder> result,
Multiset<TriplesMapBuilder> currentChain, Map<String, TriplesMapBuilder> lookup,
Set<TriplesMapBuilder> unvisited, TopologicalSorter.CycleConsumer onCycle,
Consumer<String> errorConsumer) {
// mark n as being "part of the current chain"
currentChain.add(current);
// for each node m with an edge from n to m do
for (String uriOfDependency : current.getReferencedTriplesMaps()) {
TriplesMapBuilder dependentBuilder = lookup.get(uriOfDependency);
if (dependentBuilder == null) {
errorConsumer.accept("No triplesMap with uri " + uriOfDependency + " was found");
continue;
}
// if n is already "part of the current chain"
if (currentChain.contains(dependentBuilder)) {
onCycle.handleCycle(currentChain, current, dependentBuilder);
} else {
//if m is has not been visited yet then
if (unvisited.contains(dependentBuilder)) {
// visit(m)
sortStep(dependentBuilder, result, currentChain, lookup, unvisited, onCycle, errorConsumer);
}
}
}
// mark n as being visited
unvisited.remove(current);
// n is no longer "part of the current chain"
currentChain.remove(current);
// add n to head of L
result.add(current);
}
@Override
public boolean contains(Object element) {
final Multiset<E> multiset = get();
return (multiset == null) ? EMPTY_MULTISET.contains(element)
: multiset.contains(element);
}