public static <T> TreeMultiset<T> getPailContents(Pail<T> pail) { TreeMultiset contains = TreeMultiset.create(); for (T obj : pail) { contains.add(obj); } return contains; }
/** Test a TreeMultiset with a comparator that can return 0 when comparing unequal values. */ public void testDegenerateComparator() throws Exception { TreeMultiset<String> ms = TreeMultiset.create(DEGENERATE_COMPARATOR); ms.add("foo"); ms.add("a"); ms.add("bar"); ms.add("b"); ms.add("c"); assertEquals(2, ms.count("bar")); assertEquals(3, ms.count("b")); Multiset<String> ms2 = TreeMultiset.create(DEGENERATE_COMPARATOR); ms2.add("cat", 2); ms2.add("x", 3); assertEquals(ms, ms2); assertEquals(ms2, ms); SortedSet<String> elementSet = ms.elementSet(); assertEquals("a", elementSet.first()); assertEquals("foo", elementSet.last()); assertEquals(DEGENERATE_COMPARATOR, elementSet.comparator()); }
public static <T> void assertPailContents(Pail<T> pail, T... objects) { TreeMultiset contains = getPailContents(pail); TreeMultiset other = TreeMultiset.create(); for (T obj : objects) { other.add(obj); } Assert.assertEquals(failureString(other, contains), other, contains); }
/** * Returns a collection of multiset entries representing the counts of the distinct elements, in * sorted order. Does not check for null. */ public static <E> Collection<Multiset.Entry<E>> sortedCounts( Comparator<? super E> comparator, Iterable<E> elements) { if (elements instanceof Multiset) { Multiset<E> multiset = (Multiset<E>) elements; if (hasSameComparator(comparator, elements)) { return multiset.entrySet(); } List<Multiset.Entry<E>> entries = Lists.newArrayList(multiset.entrySet()); Collections.sort( entries, Ordering.from(comparator) .onResultOf( new Function<Multiset.Entry<E>, E>() { @Override public E apply(Entry<E> entry) { return entry.getElement(); } })); return entries; } else if (elements instanceof Set) { // known distinct Collection<E> sortedElements; if (hasSameComparator(comparator, elements)) { sortedElements = (Collection<E>) elements; } else { List<E> list = Lists.newArrayList(elements); Collections.sort(list, comparator); sortedElements = list; } return singletonEntries(sortedElements); } else if (hasSameComparator(comparator, elements)) { E current = null; int currentCount = 0; List<Multiset.Entry<E>> sortedEntries = Lists.newArrayList(); for (E e : elements) { if (currentCount > 0) { if (comparator.compare(current, e) == 0) { currentCount++; } else { sortedEntries.add(Multisets.immutableEntry(current, currentCount)); current = e; currentCount = 1; } } else { current = e; currentCount = 1; } } if (currentCount > 0) { sortedEntries.add(Multisets.immutableEntry(current, currentCount)); } return sortedEntries; } TreeMultiset<E> multiset = TreeMultiset.create(comparator); Iterables.addAll(multiset, elements); return multiset.entrySet(); }
public static void assertPailContents(Pail pail, List objects) { TreeMultiset contains = getPailContents(pail); TreeMultiset other = TreeMultiset.create(); for (Object obj : objects) { other.add(obj); } for (Object o : contains) {} Assert.assertEquals(failureString(other, contains), other, contains); }
@Override public void addKeepAliveTimeout(SelectableChannel channel, Timeout timeout) { logger.debug("added keep-alive timeout: {}", timeout); DecoratedTimeout oldTimeout = index.get(channel); if (oldTimeout != null) { keepAliveTimeouts.remove(oldTimeout); } DecoratedTimeout decorated = new DecoratedTimeout(channel, timeout); keepAliveTimeouts.add(decorated); index.put(channel, decorated); }
public void testElementSetSortedSetMethods() { TreeMultiset<String> ms = TreeMultiset.create(); ms.add("c", 1); ms.add("a", 3); ms.add("b", 2); SortedSet<String> elementSet = ms.elementSet(); assertEquals("a", elementSet.first()); assertEquals("c", elementSet.last()); assertEquals(Ordering.natural(), elementSet.comparator()); ASSERT.that(elementSet.headSet("b")).has().exactly("a").inOrder(); ASSERT.that(elementSet.tailSet("b")).has().exactly("b", "c").inOrder(); ASSERT.that(elementSet.subSet("a", "c")).has().exactly("a", "b").inOrder(); }
public void testCreate() { TreeMultiset<String> multiset = TreeMultiset.create(); multiset.add("foo", 2); multiset.add("bar"); assertEquals(3, multiset.size()); assertEquals(2, multiset.count("foo")); assertEquals(Ordering.natural(), multiset.comparator()); assertEquals("[bar, foo x 2]", multiset.toString()); }
public void testCreateWithComparator() { Multiset<String> multiset = TreeMultiset.create(Collections.reverseOrder()); multiset.add("foo", 2); multiset.add("bar"); assertEquals(3, multiset.size()); assertEquals(2, multiset.count("foo")); assertEquals("[foo x 2, bar]", multiset.toString()); }
public void testToString() { Multiset<String> ms = TreeMultiset.create(); ms.add("a", 3); ms.add("c", 1); ms.add("b", 2); assertEquals("[a x 3, b x 2, c]", ms.toString()); }
public void testSubMultisetSize() { TreeMultiset<String> ms = TreeMultiset.create(); ms.add("a", Integer.MAX_VALUE); ms.add("b", Integer.MAX_VALUE); ms.add("c", 3); assertEquals(Integer.MAX_VALUE, ms.count("a")); assertEquals(Integer.MAX_VALUE, ms.count("b")); assertEquals(3, ms.count("c")); assertEquals(Integer.MAX_VALUE, ms.headMultiset("c", CLOSED).size()); assertEquals(Integer.MAX_VALUE, ms.headMultiset("b", CLOSED).size()); assertEquals(Integer.MAX_VALUE, ms.headMultiset("a", CLOSED).size()); assertEquals(3, ms.tailMultiset("c", CLOSED).size()); assertEquals(Integer.MAX_VALUE, ms.tailMultiset("b", CLOSED).size()); assertEquals(Integer.MAX_VALUE, ms.tailMultiset("a", CLOSED).size()); }
/** * Returns an immutable sorted multiset containing the given elements sorted by the given {@code * Comparator}. This method iterates over {@code elements} at most once. * * <p>Despite the method name, this method attempts to avoid actually copying the data when it is * safe to do so. The exact circumstances under which a copy will or will not be performed are * undocumented and subject to change. * * @throws NullPointerException if {@code comparator} or any of {@code elements} is null */ public static <E> ImmutableSortedMultiset<E> copyOf( Comparator<? super E> comparator, Iterable<? extends E> elements) { if (elements instanceof ImmutableSortedMultiset) { @SuppressWarnings("unchecked") // immutable collections are always safe for covariant casts ImmutableSortedMultiset<E> multiset = (ImmutableSortedMultiset<E>) elements; if (comparator.equals(multiset.comparator())) { if (multiset.isPartialView()) { return copyOfSortedEntries(comparator, multiset.entrySet().asList()); } else { return multiset; } } } elements = Lists.newArrayList(elements); // defensive copy TreeMultiset<E> sortedCopy = TreeMultiset.create(checkNotNull(comparator)); Iterables.addAll(sortedCopy, elements); return copyOfSortedEntries(comparator, sortedCopy.entrySet()); }
@BeforeExperiment void setUp() { hashMultiset = HashMultiset.create(size); linkedHashMultiset = LinkedHashMultiset.create(size); treeMultiset = TreeMultiset.create(); Random random = new Random(); int sizeRemaining = size; // TODO(kevinb): generate better test contents for multisets for (int i = 0; sizeRemaining > 0; i++) { // The JVM will return interned values for small ints. Integer value = random.nextInt(1000) + 128; int count = Math.min(random.nextInt(10) + 1, sizeRemaining); sizeRemaining -= count; hashMultiset.add(value, count); linkedHashMultiset.add(value, count); treeMultiset.add(value, count); } // TODO(kevinb): convert to assert once benchmark tests enable asserts by default Preconditions.checkState(hashMultiset.size() == size); }
public void testNullAcceptingComparator() throws Exception { Comparator<String> comparator = Ordering.<String>natural().nullsFirst(); TreeMultiset<String> ms = TreeMultiset.create(comparator); ms.add("b"); ms.add(null); ms.add("a"); ms.add("b"); ms.add(null, 2); ASSERT.that(ms).has().exactly(null, null, null, "a", "b", "b").inOrder(); assertEquals(3, ms.count(null)); SortedSet<String> elementSet = ms.elementSet(); assertEquals(null, elementSet.first()); assertEquals("b", elementSet.last()); assertEquals(comparator, elementSet.comparator()); }
public void testElementSetSubsetRetainAll() { TreeMultiset<String> ms = TreeMultiset.create(); ms.add("a", 1); ms.add("b", 3); ms.add("c", 2); ms.add("d", 1); ms.add("e", 3); ms.add("f", 2); SortedSet<String> elementSet = ms.elementSet(); ASSERT.that(elementSet).has().exactly("a", "b", "c", "d", "e", "f").inOrder(); SortedSet<String> subset = elementSet.subSet("b", "f"); ASSERT.that(subset).has().exactly("b", "c", "d", "e").inOrder(); assertTrue(subset.retainAll(Arrays.asList("a", "c"))); ASSERT.that(elementSet).has().exactly("a", "c", "f").inOrder(); ASSERT.that(subset).has().exactly("c").inOrder(); assertEquals(5, ms.size()); }
public void testElementSetSubsetClear() { TreeMultiset<String> ms = TreeMultiset.create(); ms.add("a", 1); ms.add("b", 3); ms.add("c", 2); ms.add("d", 1); ms.add("e", 3); ms.add("f", 2); SortedSet<String> elementSet = ms.elementSet(); ASSERT.that(elementSet).has().exactly("a", "b", "c", "d", "e", "f").inOrder(); SortedSet<String> subset = elementSet.subSet("b", "f"); ASSERT.that(subset).has().exactly("b", "c", "d", "e").inOrder(); subset.clear(); ASSERT.that(elementSet).has().exactly("a", "f").inOrder(); ASSERT.that(subset).isEmpty(); assertEquals(3, ms.size()); }
private static class LanguageDistributionCounter implements Counter<LanguageDistributionCounter> { private final Multiset<String> multiset = TreeMultiset.create(); @Override public void aggregate(LanguageDistributionCounter counter) { multiset.addAll(counter.multiset); } @Override public void initialize(CounterInitializationContext context) { if (context.getLeaf().getType() == Component.Type.FILE) { initializeForFile(context); } initializeForOtherLeaf(context); } private void initializeForFile(CounterInitializationContext context) { String language = context.getLeaf().getFileAttributes().getLanguageKey(); Optional<Measure> ncloc = context.getMeasure(CoreMetrics.NCLOC_KEY); if (ncloc.isPresent()) { multiset.add(language == null ? UNKNOWN_LANGUAGE_KEY : language, ncloc.get().getIntValue()); } } private void initializeForOtherLeaf(CounterInitializationContext context) { Optional<Measure> measure = context.getMeasure(NCLOC_LANGUAGE_DISTRIBUTION_KEY); if (measure.isPresent()) { Map<String, Integer> parse = KeyValueFormat.parse( measure.get().getData(), newStringConverter(), newIntegerConverter()); for (Map.Entry<String, Integer> entry : parse.entrySet()) { multiset.add(entry.getKey(), entry.getValue()); } } } }
private long executeKeepAliveTimeouts() { // makes a defensive copy to avoid (1) CME (new timeouts are added this iteration) and (2) IO // starvation. TreeMultiset<DecoratedTimeout> defensive = TreeMultiset.create(keepAliveTimeouts); Iterator<DecoratedTimeout> iter = defensive.iterator(); final long now = System.currentTimeMillis(); while (iter.hasNext()) { DecoratedTimeout candidate = iter.next(); if (candidate.timeout.getTimeout() > now) { break; } candidate.timeout.getCallback().onCallback(); index.remove(candidate.channel); iter.remove(); keepAliveTimeouts.remove(candidate); logger.debug("Keep-alive timeout triggered: {}", candidate.timeout); } return keepAliveTimeouts.isEmpty() ? Long.MAX_VALUE : Math.max(1, keepAliveTimeouts.iterator().next().timeout.getTimeout() - now); }
public void testCustomComparator() throws Exception { Comparator<String> comparator = new Comparator<String>() { @Override public int compare(String o1, String o2) { return o2.compareTo(o1); } }; TreeMultiset<String> ms = TreeMultiset.create(comparator); ms.add("b"); ms.add("c"); ms.add("a"); ms.add("b"); ms.add("d"); ASSERT.that(ms).has().exactly("d", "c", "b", "b", "a").inOrder(); SortedSet<String> elementSet = ms.elementSet(); assertEquals("d", elementSet.first()); assertEquals("a", elementSet.last()); assertEquals(comparator, elementSet.comparator()); }
private void recomputeMultiset() { this.distinctElements = 1 + TreeMultiset.distinctElements(left) + TreeMultiset.distinctElements(right); this.totalCount = elemCount + totalCount(left) + totalCount(right); }
/** Create an empty sample. */ public Sample() { this.list = TreeMultiset.create(); }
/** * Returns a collection of multiset entries representing the counts of the distinct elements, in * sorted order. Does not check for null. */ public static <E> Collection<Multiset.Entry<E>> sortedCounts( Comparator<? super E> comparator, Iterator<E> elements) { TreeMultiset<E> multiset = TreeMultiset.create(comparator); Iterators.addAll(multiset, elements); return multiset.entrySet(); }
public String handleCase2(InputData in) { /** * The idea is we put the levels in 2 sets. * * <p>One is ordered by least set 2 star prereq first ; breaking ties by higher 1 star. * * <p>TODO why 1 star sort? */ Ordering<Level> set2Ordering = Ordering.from( new Comparator<Level>() { @Override public int compare(Level o1, Level o2) { return ComparisonChain.start() .compare(o1.twoStarPrereq, o2.twoStarPrereq) .compare(o2.oneStarPrereq, o1.oneStarPrereq) .result(); } }); TreeMultiset<Level> set2star = TreeMultiset.create(set2Ordering); /** * If we need to play a 1 star, play the one with the highest 2 star req. This is because we may * be able to play a lower 2 star req in 1 round later on. */ TreeMultiset<Level> set1star = TreeMultiset.create( new Comparator<Level>() { @Override public int compare(Level o1, Level o2) { return ComparisonChain.start() .compare(o1.oneStarPrereq, o2.oneStarPrereq) .compare(o2.twoStarPrereq, o1.twoStarPrereq) .result(); } }); set1star.addAll(in.levels); set2star.addAll(in.levels); int score = 0; int count = 0; while (!set2star.isEmpty()) { Level nextSet2 = set2star.firstEntry().getElement(); if (nextSet2.twoStarPrereq <= score) { ++count; if (set1star.contains(nextSet2)) { score++; set1star.remove(nextSet2); } set2star.remove(nextSet2); score++; continue; } List<Level> set1Choices = new ArrayList<>(set1star.headMultiset(new Level(score, 0), BoundType.CLOSED)); Collections.sort(set1Choices, set2Ordering); if (nextSet2.twoStarPrereq > score) { // Need to take from set1 with the highest possible round 2 score if (set1Choices.isEmpty()) { return String.format("Case #%d: Too Bad", in.testCase); } ++count; score += 1; Level round1 = set1Choices.remove(set1Choices.size() - 1); set1star.remove(round1); continue; } } // in.levelMin.subList() return String.format("Case #%d: %s", in.testCase, count); }
public void testCreateFromIterable() { Multiset<String> multiset = TreeMultiset.create(Arrays.asList("foo", "bar", "foo")); assertEquals(3, multiset.size()); assertEquals(2, multiset.count("foo")); assertEquals("[bar, foo x 2]", multiset.toString()); }
public class JMXDebuggableTimeoutManager implements TimeoutManager, TimeoutManagerMXBean { private final Logger logger = LoggerFactory.getLogger(JMXDebuggableTimeoutManager.class); private final TreeSet<Timeout> timeouts = Sets.newTreeSet(new TimeoutComparator()); private final TreeMultiset<DecoratedTimeout> keepAliveTimeouts = TreeMultiset.create(); private final Map<SelectableChannel, DecoratedTimeout> index = Maps.newHashMap(); private static final AtomicInteger sequence = new AtomicInteger(1); public JMXDebuggableTimeoutManager() { // instance initialization block MXBeanUtil.registerMXBean( this, "TimeoutManager", this.getClass().getSimpleName() + "-" + sequence.incrementAndGet()); } @Override public void addKeepAliveTimeout(SelectableChannel channel, Timeout timeout) { logger.debug("added keep-alive timeout: {}", timeout); DecoratedTimeout oldTimeout = index.get(channel); if (oldTimeout != null) { keepAliveTimeouts.remove(oldTimeout); } DecoratedTimeout decorated = new DecoratedTimeout(channel, timeout); keepAliveTimeouts.add(decorated); index.put(channel, decorated); } @Override public void addTimeout(Timeout timeout) { logger.debug("added generic timeout: {}", timeout); timeouts.add(timeout); } @Override public boolean hasKeepAliveTimeout(SelectableChannel channel) { return index.containsKey(channel); } @Override public long execute() { return Math.min(executeKeepAliveTimeouts(), executeTimeouts()); } private long executeKeepAliveTimeouts() { // makes a defensive copy to avoid (1) CME (new timeouts are added this iteration) and (2) IO // starvation. TreeMultiset<DecoratedTimeout> defensive = TreeMultiset.create(keepAliveTimeouts); Iterator<DecoratedTimeout> iter = defensive.iterator(); final long now = System.currentTimeMillis(); while (iter.hasNext()) { DecoratedTimeout candidate = iter.next(); if (candidate.timeout.getTimeout() > now) { break; } candidate.timeout.getCallback().onCallback(); index.remove(candidate.channel); iter.remove(); keepAliveTimeouts.remove(candidate); logger.debug("Keep-alive timeout triggered: {}", candidate.timeout); } return keepAliveTimeouts.isEmpty() ? Long.MAX_VALUE : Math.max(1, keepAliveTimeouts.iterator().next().timeout.getTimeout() - now); } private long executeTimeouts() { // makes a defensive copy to avoid (1) CME (new timeouts are added this iteration) and (2) IO // starvation. TreeSet<Timeout> defensive = new TreeSet<Timeout>(timeouts); /*Sets.newTreeSet(timeouts);*/ Iterator<Timeout> iter = defensive.iterator(); final long now = System.currentTimeMillis(); while (iter.hasNext()) { Timeout candidate = iter.next(); if (candidate.getTimeout() > now) { break; } candidate.getCallback().onCallback(); iter.remove(); timeouts.remove(candidate); logger.debug("Timeout triggered: {}", candidate); } return timeouts.isEmpty() ? Long.MAX_VALUE : Math.max(1, timeouts.iterator().next().getTimeout() - now); } // implements TimoutMXBean @Override public int getNumberOfKeepAliveTimeouts() { return index.size(); } @Override public int getNumberOfTimeouts() { return keepAliveTimeouts.size() + timeouts.size(); } private class DecoratedTimeout implements Comparable<DecoratedTimeout> { public final SelectableChannel channel; public final Timeout timeout; public DecoratedTimeout(SelectableChannel channel, Timeout timeout) { this.channel = channel; this.timeout = timeout; } @Override public int compareTo(DecoratedTimeout that) { long diff = timeout.getTimeout() - that.timeout.getTimeout(); if (diff < 0) { return -1; } else if (diff > 0) { return 1; } if (channel != null && that.channel != null) { return channel.hashCode() - that.channel.hashCode(); } else if (channel == null && that.channel != null) { return -1; } else if (channel != null && that.channel == null) { return -1; } else { return 0; } } } private class TimeoutComparator implements Comparator<Timeout> { @Override public int compare(Timeout lhs, Timeout rhs) { if (lhs == rhs) { return 0; } long diff = lhs.getTimeout() - rhs.getTimeout(); if (diff <= 0) { return -1; } return 1; /// else if (diff > 0) { } } }
/** * Creates a new builder. The returned builder is equivalent to the builder generated by {@link * ImmutableSortedMultiset#orderedBy(Comparator)}. */ public Builder(Comparator<? super E> comparator) { super(TreeMultiset.<E>create(checkNotNull(comparator))); }
@Override public int getNumberOfTimeouts() { return keepAliveTimeouts.size() + timeouts.size(); }