/** * 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(); }
@Override public SampleElements<Multiset.Entry<E>> samples() { SampleElements<E> samples = gen.samples(); return new SampleElements<Multiset.Entry<E>>( Multisets.immutableEntry(samples.e0, 3), Multisets.immutableEntry(samples.e1, 4), Multisets.immutableEntry(samples.e2, 1), Multisets.immutableEntry(samples.e3, 5), Multisets.immutableEntry(samples.e4, 2)); }
public String getResultsComparison(int precision) { List<List<Object>> controlResults = controlResult.getResults(); List<List<Object>> testResults = testResult.getResults(); if (valid() || (controlResults == null) || (testResults == null)) { return ""; } Multiset<List<Object>> control = ImmutableSortedMultiset.copyOf(rowComparator(precision), controlResults); Multiset<List<Object>> test = ImmutableSortedMultiset.copyOf(rowComparator(precision), testResults); try { Iterable<ChangedRow> diff = ImmutableSortedMultiset.<ChangedRow>naturalOrder() .addAll( Iterables.transform( Multisets.difference(control, test), row -> new ChangedRow(Changed.REMOVED, row, precision))) .addAll( Iterables.transform( Multisets.difference(test, control), row -> new ChangedRow(Changed.ADDED, row, precision))) .build(); diff = Iterables.limit(diff, 100); StringBuilder sb = new StringBuilder(); sb.append(format("Control %s rows, Test %s rows%n", control.size(), test.size())); if (verboseResultsComparison) { Joiner.on("\n").appendTo(sb, diff); } else { sb.append("RESULTS DO NOT MATCH\n"); } return sb.toString(); } catch (TypesDoNotMatchException e) { return e.getMessage(); } }
/** * Adds each element of {@code elements} to the {@code ImmutableMultiset}. * * @param elements the {@code Iterable} to add to the {@code ImmutableMultiset} * @return this {@code Builder} object * @throws NullPointerException if {@code elements} is null or contains a null element */ @Override public Builder<E> addAll(Iterable<? extends E> elements) { if (elements instanceof Multiset) { Multiset<? extends E> multiset = Multisets.cast(elements); for (Entry<? extends E> entry : multiset.entrySet()) { addCopies(entry.getElement(), entry.getCount()); } } else { super.addAll(elements); } return this; }
@Test public void whenGetTopUsingMultiSet_thenCorrect() { final Multiset<String> names = HashMultiset.create(); names.add("John"); names.add("Adam", 5); names.add("Jane"); names.add("Tom", 2); final Set<String> sorted = Multisets.copyHighestCountFirst(names).elementSet(); final List<String> topTwo = Lists.newArrayList(sorted).subList(0, 2); assertEquals(2, topTwo.size()); assertEquals("Adam", topTwo.get(0)); assertEquals("Tom", topTwo.get(1)); }
/** * Returns an immutable multiset containing the given elements. * * <p>The multiset is ordered by the first occurrence of each element. For example, {@code * ImmutableMultiset.copyOf(Arrays.asList(2, 3, 1, 3))} yields a multiset with elements in the * order {@code 2, 3, 3, 1}. * * <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. * * <p><b>Note:</b> Despite what the method name suggests, if {@code elements} is an {@code * ImmutableMultiset}, no copy will actually be performed, and the given multiset itself will be * returned. * * @throws NullPointerException if any of {@code elements} is null */ public static <E> ImmutableMultiset<E> copyOf(Iterable<? extends E> elements) { if (elements instanceof ImmutableMultiset) { @SuppressWarnings("unchecked") // all supported methods are covariant ImmutableMultiset<E> result = (ImmutableMultiset<E>) elements; if (!result.isPartialView()) { return result; } } Multiset<? extends E> multiset = (elements instanceof Multiset) ? Multisets.cast(elements) : LinkedHashMultiset.create(elements); return copyOfInternal(multiset); }
@Override public String toString() { final StringBuffer buf = new StringBuffer(); for (final Entry<TSGNode, ConcurrentHashMultiset<TreeNode<TSGNode>>> rootEntry : grammar.entrySet()) { if (rootEntry.getValue().entrySet().isEmpty()) { continue; } final TSGNode root = rootEntry.getKey(); buf.append("********\n"); buf.append(treeFormat.getSymbol(root.nodeKey) + ":\n"); for (final Multiset.Entry<TreeNode<TSGNode>> tree : Multisets.copyHighestCountFirst(rootEntry.getValue()).entrySet()) { final double prob = ((double) tree.getCount()) / rootEntry.getValue().size(); buf.append("----------------------------------------\n"); if (tree.getElement() != null) { if (tree.getElement().getData() != null) { buf.append(treeToString(tree.getElement())); buf.append("_________________________________\n"); try { final TreeNode<Integer> intTree = TreeNode.create( tree.getElement().getData().nodeKey, tree.getElement().nProperties()); TSGNode.copyChildren(intTree, tree.getElement()); if (treeFormat.getSymbol(intTree.getData()).nodeType == AstNodeSymbol.MULTI_NODE) { treeFormat.printMultinode(buf, intTree); } else { buf.append(treeFormat.getCodeFromTree(intTree)); } } catch (final Throwable e) { buf.append("Cannot get AST representation of rule"); } buf.append("\n"); } else { buf.append("null"); } } else { buf.append("null"); } buf.append(">Prob " + prob + " (" + tree.getCount() + ")\n"); } } return buf.toString(); }
private void identifyDuplicates(List<ModContainer> mods) { TreeMultimap<ModContainer, File> dupsearch = TreeMultimap.create(new ModIdComparator(), Ordering.arbitrary()); for (ModContainer mc : mods) { if (mc.getSource() != null) { dupsearch.put(mc, mc.getSource()); } } ImmutableMultiset<ModContainer> duplist = Multisets.copyHighestCountFirst(dupsearch.keys()); SetMultimap<ModContainer, File> dupes = LinkedHashMultimap.create(); for (Entry<ModContainer> e : duplist.entrySet()) { if (e.getCount() > 1) { FMLLog.severe( "Found a duplicate mod %s at %s", e.getElement().getModId(), dupsearch.get(e.getElement())); dupes.putAll(e.getElement(), dupsearch.get(e.getElement())); } } if (!dupes.isEmpty()) { throw new DuplicateModsFoundException(dupes); } }
@Override public String toString() { return Multisets.immutableEntry(getElement(), getCount()).toString(); }
/** * Creates a new {@code HashMultiset} containing the specified elements. * * <p>This implementation is highly efficient when {@code elements} is itself a {@link Multiset}. * * @param elements the elements that the multiset should contain */ public static <E> HashMultiset<E> create(Iterable<? extends E> elements) { HashMultiset<E> multiset = create(Multisets.inferDistinctElements(elements)); Iterables.addAll(multiset, elements); return multiset; }
public void updateMapData(World par1World, Entity par2Entity, MapData par3MapData) { if (par1World.provider.dimensionId == par3MapData.dimension && par2Entity instanceof EntityPlayer) { int var4 = 1 << par3MapData.scale; int var5 = par3MapData.xCenter; int var6 = par3MapData.zCenter; int var7 = MathHelper.floor_double(par2Entity.posX - (double) var5) / var4 + 64; int var8 = MathHelper.floor_double(par2Entity.posZ - (double) var6) / var4 + 64; int var9 = 128 / var4; if (par1World.provider.hasNoSky) { var9 /= 2; } MapData.MapInfo var10 = par3MapData.func_82568_a((EntityPlayer) par2Entity); ++var10.field_82569_d; for (int var11 = var7 - var9 + 1; var11 < var7 + var9; ++var11) { if ((var11 & 15) == (var10.field_82569_d & 15)) { int var12 = 255; int var13 = 0; double var14 = 0.0D; for (int var16 = var8 - var9 - 1; var16 < var8 + var9; ++var16) { if (var11 >= 0 && var16 >= -1 && var11 < 128 && var16 < 128) { int var17 = var11 - var7; int var18 = var16 - var8; boolean var19 = var17 * var17 + var18 * var18 > (var9 - 2) * (var9 - 2); int var20 = (var5 / var4 + var11 - 64) * var4; int var21 = (var6 / var4 + var16 - 64) * var4; HashMultiset var22 = HashMultiset.create(); Chunk var23 = par1World.getChunkFromBlockCoords(var20, var21); if (!var23.isEmpty()) { int var24 = var20 & 15; int var25 = var21 & 15; int var26 = 0; double var27 = 0.0D; int var29; if (par1World.provider.hasNoSky) { var29 = var20 + var21 * 231871; var29 = var29 * var29 * 31287121 + var29 * 11; if ((var29 >> 20 & 1) == 0) { var22.add(Blocks.dirt.getMapColor(0), 10); } else { var22.add(Blocks.stone.getMapColor(0), 100); } var27 = 100.0D; } else { for (var29 = 0; var29 < var4; ++var29) { for (int var30 = 0; var30 < var4; ++var30) { int var31 = var23.getHeightValue(var29 + var24, var30 + var25) + 1; Block var32 = Blocks.air; int var33 = 0; if (var31 > 1) { do { --var31; var32 = var23.func_150810_a(var29 + var24, var31, var30 + var25); var33 = var23.getBlockMetadata(var29 + var24, var31, var30 + var25); } while (var32.getMapColor(var33) == MapColor.field_151660_b && var31 > 0); if (var31 > 0 && var32.getMaterial().isLiquid()) { int var34 = var31 - 1; Block var35; do { var35 = var23.func_150810_a(var29 + var24, var34--, var30 + var25); ++var26; } while (var34 > 0 && var35.getMaterial().isLiquid()); } } var27 += (double) var31 / (double) (var4 * var4); var22.add(var32.getMapColor(var33)); } } } var26 /= var4 * var4; double var36 = (var27 - var14) * 4.0D / (double) (var4 + 4) + ((double) (var11 + var16 & 1) - 0.5D) * 0.4D; byte var39 = 1; if (var36 > 0.6D) { var39 = 2; } if (var36 < -0.6D) { var39 = 0; } MapColor var38 = (MapColor) Iterables.getFirst( Multisets.copyHighestCountFirst(var22), MapColor.field_151660_b); if (var38 == MapColor.field_151662_n) { var36 = (double) var26 * 0.1D + (double) (var11 + var16 & 1) * 0.2D; var39 = 1; if (var36 < 0.5D) { var39 = 2; } if (var36 > 0.9D) { var39 = 0; } } var14 = var27; if (var16 >= 0 && var17 * var17 + var18 * var18 < var9 * var9 && (!var19 || (var11 + var16 & 1) != 0)) { byte var37 = par3MapData.colors[var11 + var16 * 128]; byte var40 = (byte) (var38.colorIndex * 4 + var39); if (var37 != var40) { if (var12 > var16) { var12 = var16; } if (var13 < var16) { var13 = var16; } par3MapData.colors[var11 + var16 * 128] = var40; } } } } } if (var12 <= var13) { par3MapData.setColumnDirty(var11, var12, var13); } } } } }
/** * Returns an unmodifiable view of the union of the patterns, functions and variables multisets. * * @see org.kframework.kil.CollectionBuiltin#baseTerms */ public Multiset<Term> baseTerms() { return Multisets.union( Multisets.union(collectionPatterns, collectionFunctions), collectionVariables); }