Refine search
SerializedForm(Multiset<?> multiset) { int distinct = multiset.entrySet().size(); elements = new Object[distinct]; counts = new int[distinct]; int i = 0; for (Entry<?> entry : multiset.entrySet()) { elements[i] = entry.getElement(); counts[i] = entry.getCount(); i++; } }
/** * Stores the contents of a multiset in an output stream, as part of serialization. It does not * support concurrent multisets whose content may change while the method is running. * * <p>The serialized output consists of the number of distinct elements, the first element, its * count, the second element, its count, and so on. */ static <E> void writeMultiset(Multiset<E> multiset, ObjectOutputStream stream) throws IOException { int entryCount = multiset.entrySet().size(); stream.writeInt(entryCount); for (Multiset.Entry<E> entry : multiset.entrySet()) { stream.writeObject(entry.getElement()); stream.writeInt(entry.getCount()); } }
@Override Set<Entry<E>> createEntrySet() { return Sets.filter( unfiltered.entrySet(), new Predicate<Entry<E>>() { @Override public boolean apply(Entry<E> entry) { return predicate.apply(entry.getElement()); } }); }
/** * Runs the specified action for each distinct element in this multiset, and the number of * occurrences of that element. For some {@code Multiset} implementations, this may be more * efficient than iterating over the {@link #entrySet()} either explicitly or with {@code * entrySet().forEach(action)}. * * @since 21.0 */ @Beta default void forEachEntry(ObjIntConsumer<? super E> action) { checkNotNull(action); entrySet().forEach(entry -> action.accept(entry.getElement(), entry.getCount())); }
/** * {@inheritDoc} * * <p>Elements that occur multiple times in the multiset will be passed to the {@code Consumer} * correspondingly many times, though not necessarily sequentially. */ @Override default void forEach(Consumer<? super E> action) { checkNotNull(action); entrySet() .forEach( entry -> { E elem = entry.getElement(); int count = entry.getCount(); for (int i = 0; i < count; i++) { action.accept(elem); } }); }
@Override public Set<Multiset.Entry<E>> create(Object... entries) { List<Object> contents = new ArrayList<>(); Set<E> elements = new HashSet<>(); for (Object o : entries) { @SuppressWarnings("unchecked") Multiset.Entry<E> entry = (Entry<E>) o; checkArgument( elements.add(entry.getElement()), "Duplicate keys not allowed in EntrySetGenerator"); for (int i = 0; i < entry.getCount(); i++) { contents.add(entry.getElement()); } } return ((Multiset<E>) gen.create(contents.toArray())).entrySet(); }
static void serialize(SerializationStreamWriter writer, Multiset<?> instance) throws SerializationException { int entryCount = instance.entrySet().size(); writer.writeInt(entryCount); for (Multiset.Entry<?> entry : instance.entrySet()) { writer.writeObject(entry.getElement()); writer.writeInt(entry.getCount()); } }
/** * Stores the contents of a multiset in an output stream, as part of serialization. It does not * support concurrent multisets whose content may change while the method is running. * * <p>The serialized output consists of the number of distinct elements, the first element, its * count, the second element, its count, and so on. */ static <E> void writeMultiset(Multiset<E> multiset, ObjectOutputStream stream) throws IOException { int entryCount = multiset.entrySet().size(); stream.writeInt(entryCount); for (Multiset.Entry<E> entry : multiset.entrySet()) { stream.writeObject(entry.getElement()); stream.writeInt(entry.getCount()); } }
SerializedForm(Multiset<?> multiset) { int distinct = multiset.entrySet().size(); elements = new Object[distinct]; counts = new int[distinct]; int i = 0; for (Entry<?> entry : multiset.entrySet()) { elements[i] = entry.getElement(); counts[i] = entry.getCount(); i++; } }
/** An implementation of {@link Multiset#equals}. */ static boolean equalsImpl(Multiset<?> multiset, @Nullable Object object) { if (object == multiset) { return true; } if (object instanceof Multiset) { Multiset<?> that = (Multiset<?>) object; /* * We can't simply check whether the entry sets are equal, since that * approach fails when a TreeMultiset has a comparator that returns 0 * when passed unequal elements. */ if (multiset.size() != that.size() || multiset.entrySet().size() != that.entrySet().size()) { return false; } for (Entry<?> entry : that.entrySet()) { if (multiset.count(entry.getElement()) != entry.getCount()) { return false; } } return true; } return false; }
/** * Stores the contents of a multiset in an output stream, as part of serialization. It does not * support concurrent multisets whose content may change while the method is running. * * <p>The serialized output consists of the number of distinct elements, the first element, its * count, the second element, its count, and so on. */ static <E> void writeMultiset(Multiset<E> multiset, ObjectOutputStream stream) throws IOException { int entryCount = multiset.entrySet().size(); stream.writeInt(entryCount); for (Multiset.Entry<E> entry : multiset.entrySet()) { stream.writeObject(entry.getElement()); stream.writeInt(entry.getCount()); } }
/** Delegate implementation which cares about the element type. */ private static <E> boolean retainOccurrencesImpl( Multiset<E> multisetToModify, Multiset<?> occurrencesToRetain) { checkNotNull(multisetToModify); checkNotNull(occurrencesToRetain); // Avoiding ConcurrentModificationExceptions is tricky. Iterator<Entry<E>> entryIterator = multisetToModify.entrySet().iterator(); boolean changed = false; while (entryIterator.hasNext()) { Entry<E> entry = entryIterator.next(); int retainCount = occurrencesToRetain.count(entry.getElement()); if (retainCount == 0) { entryIterator.remove(); changed = true; } else if (retainCount < entry.getCount()) { multisetToModify.setCount(entry.getElement(), retainCount); changed = true; } } return changed; }
public void summarizeBuilderAnnotations() { List<GraphBuilderAnnotation> gbas = this.graphBuilderAnnotations; Multiset<Class<? extends GraphBuilderAnnotation>> classes = HashMultiset.create(); LOG.info("Summary (number of each type of annotation):"); for (GraphBuilderAnnotation gba : gbas) classes.add(gba.getClass()); for (Multiset.Entry<Class<? extends GraphBuilderAnnotation>> e : classes.entrySet()) { String name = e.getElement().getSimpleName(); int count = e.getCount(); LOG.info(" {} - {}", name, count); } }
/** * Returns {@code true} if {@code subMultiset.count(o) <= superMultiset.count(o)} for all {@code * o}. * * @since 10.0 */ @CanIgnoreReturnValue public static boolean containsOccurrences(Multiset<?> superMultiset, Multiset<?> subMultiset) { checkNotNull(superMultiset); checkNotNull(subMultiset); for (Entry<?> entry : subMultiset.entrySet()) { int superCount = superMultiset.count(entry.getElement()); if (superCount < entry.getCount()) { return false; } } return true; }
/** Returns true if {@code atLeastM} of the expressions in the given column are the same kind. */ private static boolean expressionsAreParallel( List<List<ExpressionTree>> rows, int column, int atLeastM) { Multiset<Tree.Kind> nodeTypes = HashMultiset.create(); for (List<? extends ExpressionTree> row : rows) { if (column >= row.size()) { continue; } nodeTypes.add(row.get(column).getKind()); } for (Multiset.Entry<Tree.Kind> nodeType : nodeTypes.entrySet()) { if (nodeType.getCount() >= atLeastM) { return true; } } return false; }
/** * Runs the specified action for each distinct element in this multiset, and the number of * occurrences of that element. For some {@code Multiset} implementations, this may be more * efficient than iterating over the {@link #entrySet()} either explicitly or with {@code * entrySet().forEach(action)}. * * @since 21.0 */ @Beta default void forEachEntry(ObjIntConsumer<? super E> action) { checkNotNull(action); entrySet().forEach(entry -> action.accept(entry.getElement(), entry.getCount())); }
/** * {@inheritDoc} * * <p>Elements that occur multiple times in the multiset will be passed to the {@code Consumer} * correspondingly many times, though not necessarily sequentially. */ @Override default void forEach(Consumer<? super E> action) { checkNotNull(action); entrySet() .forEach( entry -> { E elem = entry.getElement(); int count = entry.getCount(); for (int i = 0; i < count; i++) { action.accept(elem); } }); }
Iterator<? extends Entry<?>> entryIterator = multisetToModify.entrySet().iterator(); while (entryIterator.hasNext()) { Entry<?> entry = entryIterator.next(); int removeCount = occurrencesToRemove.count(entry.getElement()); if (removeCount >= entry.getCount()) { entryIterator.remove(); changed = true; } else if (removeCount > 0) { multisetToModify.remove(entry.getElement(), removeCount); changed = true;