Java Code Examples for com.google.common.collect.Multiset#count()

The following examples show how to use com.google.common.collect.Multiset#count() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MultisetExample.java    From levelup-java-examples with Apache License 2.0 7 votes vote down vote up
@Test
public void multiset_example () {
	
	Multiset<String> camouflage = HashMultiset.create();
	camouflage.add("Realtree APG");
	camouflage.add("Realtree Hardwoods HD");
	camouflage.add("Realtree APG");
	camouflage.add("Realtree Hardwoods Green HD");
	camouflage.add("Mossy Oak New Break-Up");
	camouflage.add("Realtree APG");
	
	logger.info(camouflage);

	int numberOfRealTrees = camouflage.count("Realtree APG");
	
	assertEquals(3, numberOfRealTrees);
}
 
Example 2
Source File: Rebanker.java    From easyccg with MIT License 6 votes vote down vote up
private void writeCatList(Multiset<Category> cats, File outputFile) throws IOException {
  Multiset<Category> catsNoPPorPRfeatures = HashMultiset.create();
  for (Category cat : cats) {
    catsNoPPorPRfeatures.add(cat.dropPPandPRfeatures());
  }
  FileWriter fw = new FileWriter(outputFile.getAbsoluteFile());
  BufferedWriter bw = new BufferedWriter(fw);
  
  
  int categories = 0;
  for (Category type : Multisets.copyHighestCountFirst(cats).elementSet()) {
    if (catsNoPPorPRfeatures.count(type.dropPPandPRfeatures()) >= 10) {
      bw.write(type.toString());
      bw.newLine();
      categories++;
    }
  }
  System.out.println("Number of cats occurring 10 times: " + categories);
  
  
  bw.flush();
  bw.close();
  

}
 
Example 3
Source File: Transaction.java    From sequence-mining with GNU General Public License v3.0 6 votes vote down vote up
/** 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;
}
 
Example 4
Source File: TimeGraphViewTest.java    From tracecompass with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public boolean test() throws Exception {
    ImageHelper image = ImageHelper.grabImage(fRect);
    if (image == null) {
        fFailureMessage = "Grabbed image is null";
        return false;
    }
    Multiset<RGB> histogram = image.getHistogram();
    for (RGB rgb : fRgbs) {
        if (histogram.count(rgb) <= 0) {
            fFailureMessage = "Color not found: " + rgb;
            return false;
        }
    }
    return true;
}
 
Example 5
Source File: CutoffsDictionary.java    From EasySRL with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isFrequent(final Category category, final int argumentNumber, final SRLLabel label) {
	if (label == SRLFrame.NONE) {
		return true;
	}
	final Multiset<SRLLabel> countForCategory = categoryToArgumentToSRLs.get(category.withoutAnnotation(),
			argumentNumber);
	return countForCategory != null && countForCategory.size() >= minSlotSRL
			&& countForCategory.count(label) >= minSlotRole;
}
 
Example 6
Source File: SyntheticResultJsonService.java    From glowroot with Apache License 2.0 5 votes vote down vote up
private ImmutableList<SyntheticMonitor> getAllSyntheticMonitors(String agentRollupId,
        long from, long to) throws Exception {
    Map<String, String> syntheticMonitorIds =
            syntheticResultRepository.getSyntheticMonitorIds(agentRollupId, from, to);
    Multiset<String> multiset = HashMultiset.create();
    multiset.addAll(syntheticMonitorIds.values());
    List<SyntheticMonitor> syntheticMonitors = Lists.newArrayList();
    for (Map.Entry<String, String> entry : syntheticMonitorIds.entrySet()) {
        String id = entry.getKey();
        String display = entry.getValue();
        if (multiset.count(entry.getValue()) > 1) {
            display += " (" + id + ")";
        }
        syntheticMonitors.add(ImmutableSyntheticMonitor.of(id, display));
    }
    if (to > clock.currentTimeMillis()) {
        // so that new synthetic monitors will show up right away
        List<SyntheticMonitorConfig> configs =
                configRepository.getSyntheticMonitorConfigs(agentRollupId);
        for (SyntheticMonitorConfig config : configs) {
            if (!syntheticMonitorIds.containsKey(config.getId())) {
                syntheticMonitors.add(ImmutableSyntheticMonitor.of(config.getId(),
                        MoreConfigDefaults.getDisplayOrDefault(config)));
            }
        }
    }
    return new SyntheticMonitorOrdering().immutableSortedCopy(syntheticMonitors);
}
 
Example 7
Source File: DiscreteElementwiseConditionalDistribution.java    From tassal with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public double getMLProbability(final A element, final B given) {
	if (table.containsKey(given)) {
		final Multiset<A> elements = table.get(given);
		return ((double) elements.count(element)) / elements.size();
	} else {
		return 1;
	}
}
 
Example 8
Source File: App1.java    From pyramid with Apache License 2.0 5 votes vote down vote up
static boolean interesting(Multiset<Ngram> allNgrams, Ngram candidate, int count){
    for (int slop = 0;slop<candidate.getSlop();slop++){
        Ngram toCheck = new Ngram();
        toCheck.setInOrder(candidate.isInOrder());
        toCheck.setField(candidate.getField());
        toCheck.setNgram(candidate.getNgram());
        toCheck.setSlop(slop);
        toCheck.setName(candidate.getName());
        if (allNgrams.count(toCheck)==count){
            return false;
        }
    }
    return true;
}
 
Example 9
Source File: MinRegionsResourcePlacementStrategy.java    From bboxdb with Apache License 2.0 5 votes vote down vote up
@Override
protected double calculateUsageFactor(Multiset<BBoxDBInstance> systemUsage,
		BBoxDBInstance distributedInstance) {
	
	final int usageCount = systemUsage.count(distributedInstance);
	
	if(usageCount == 0) {
		return 0;
	}
	
	// Lower utilization is preferred by the algorithm
	return (1.0 / usageCount);
}
 
Example 10
Source File: CoreUtilizationPlacementStrategy.java    From bboxdb with Apache License 2.0 5 votes vote down vote up
/**
 * Calculate the cpu core / instance usage factor
 * @param systemUsage
 * @param distributedInstance
 * @return
 */
protected double calculateUsageFactor(final Multiset<BBoxDBInstance> systemUsage,
		final BBoxDBInstance distributedInstance) {
	
	final int count = systemUsage.count(distributedInstance);
	
	if(count == 0) {
		logger.error("Got an invalid count");
		return 0;
	}
	
	return distributedInstance.getCpuCores() / count;
}
 
Example 11
Source File: StorageUtilizationPlacementStrategy.java    From bboxdb with Apache License 2.0 5 votes vote down vote up
/**
 * Calculate the storages / instance usage factor
 * @param systemUsage
 * @param distributedInstance
 * @return
 */
protected double calculateUsageFactor(final Multiset<BBoxDBInstance> systemUsage,
		final BBoxDBInstance distributedInstance) {
	
	final int count = systemUsage.count(distributedInstance);
	
	if(count == 0) {
		logger.error("Got an invalid count");
		return 0;
	}
	
	return distributedInstance.getNumberOfStorages() / count;
}
 
Example 12
Source File: MemoryUtilizationFreePlacementStrategy.java    From bboxdb with Apache License 2.0 5 votes vote down vote up
/**
 * Calculate the memory / instance usage factor
 * @param systemUsage
 * @param distributedInstance
 * @return
 */
protected double calculateUsageFactor(final Multiset<BBoxDBInstance> systemUsage,
		final BBoxDBInstance distributedInstance) {
	
	final int count = systemUsage.count(distributedInstance);
	
	if(count == 0) {
		logger.error("Got an invalid count");
		return 0;
	}
	
	return distributedInstance.getMemory() / count;
}
 
Example 13
Source File: SourceDirectoryCalculator.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Nullable
private static <T> T pickMostFrequentlyOccurring(Multiset<T> set, String prefer) {
  T best = null;
  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;
}
 
Example 14
Source File: CountElementsInList.java    From levelup-java-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void number_of_occurences_in_list_guava () {
	
	List<String> seussCountActivities = Lists.newArrayList(
			"findow", "Balloons", "Elephants", "Boom Bands", 
			"findow", "Hakken-Kraks", "Hakken-Kraks", 
			"Hakken-Kraks", "Elephants");
	
	Multiset<String> seussCount = HashMultiset.create(seussCountActivities);
	int numberOfElephants = seussCount.count("Elephants");
	
	logger.info(seussCount);

	assertEquals(2, numberOfElephants);
}
 
Example 15
Source File: ObservableMultisetWrapper.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
@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;
}
 
Example 16
Source File: TestRotating.java    From bidder with Apache License 2.0 5 votes vote down vote up
void testStrategyFixed() throws Exception {
    HttpPostGet http = new HttpPostGet();
    String bid = Charset.defaultCharset()
            .decode(ByteBuffer.wrap(Files.readAllBytes(Paths.get("./SampleBids/rotating.txt")))).toString();
    Multiset<String> mm =  HashMultiset.create();

    LookingGlass.remove("apples");

    try {
        String s = null;
        for (int i=0;i<10;i++) {
            try {
                s = http.sendPost("http://" + Config.testHost + "/rtb/bids/c1x", bid, 100000, 100000);
            } catch (Exception error) {
                fail("Can't connect to test host: " + Config.testHost);
            }
            assertNotNull(s);
            String crid = extractCrid(s);
            mm.add(crid);
        }
        int count = mm.count("c1");
        assertNotEquals(count,10);
        System.out.println("XX = " + count);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.toString());

    }
}
 
Example 17
Source File: BucketBalancer.java    From presto with Apache License 2.0 4 votes vote down vote up
private static Multimap<String, BucketAssignment> computeAssignmentChanges(ClusterState clusterState)
{
    Multimap<String, BucketAssignment> sourceToAllocationChanges = HashMultimap.create();

    Map<String, Long> allocationBytes = new HashMap<>(clusterState.getAssignedBytes());
    Set<String> activeNodes = clusterState.getActiveNodes();

    for (Distribution distribution : clusterState.getDistributionAssignments().keySet()) {
        // number of buckets in this distribution assigned to a node
        Multiset<String> allocationCounts = HashMultiset.create();
        Collection<BucketAssignment> distributionAssignments = clusterState.getDistributionAssignments().get(distribution);
        distributionAssignments.stream()
                .map(BucketAssignment::getNodeIdentifier)
                .forEach(allocationCounts::add);

        int currentMin = allocationBytes.keySet().stream()
                .mapToInt(allocationCounts::count)
                .min()
                .getAsInt();
        int currentMax = allocationBytes.keySet().stream()
                .mapToInt(allocationCounts::count)
                .max()
                .getAsInt();

        int numBuckets = distributionAssignments.size();
        int targetMin = (int) Math.floor((numBuckets * 1.0) / clusterState.getActiveNodes().size());
        int targetMax = (int) Math.ceil((numBuckets * 1.0) / clusterState.getActiveNodes().size());

        log.info("Distribution %s: Current bucket skew: min %s, max %s. Target bucket skew: min %s, max %s", distribution.getId(), currentMin, currentMax, targetMin, targetMax);

        for (String source : ImmutableSet.copyOf(allocationCounts)) {
            List<BucketAssignment> existingAssignments = distributionAssignments.stream()
                    .filter(assignment -> assignment.getNodeIdentifier().equals(source))
                    .collect(toList());

            for (BucketAssignment existingAssignment : existingAssignments) {
                if (activeNodes.contains(source) && allocationCounts.count(source) <= targetMin) {
                    break;
                }

                // identify nodes with bucket counts lower than the computed target, and greedily select from this set based on projected disk utilization.
                // greediness means that this may produce decidedly non-optimal results if one looks at the global distribution of buckets->nodes.
                // also, this assumes that nodes in a cluster have identical storage capacity
                String target = activeNodes.stream()
                        .filter(candidate -> !candidate.equals(source) && allocationCounts.count(candidate) < targetMax)
                        .sorted(comparingInt(allocationCounts::count))
                        .min(Comparator.comparingDouble(allocationBytes::get))
                        .orElseThrow(() -> new VerifyException("unable to find target for rebalancing"));

                long bucketSize = clusterState.getDistributionBucketSize().get(distribution);

                // only move bucket if it reduces imbalance
                if (activeNodes.contains(source) && (allocationCounts.count(source) == targetMax && allocationCounts.count(target) == targetMin)) {
                    break;
                }

                allocationCounts.remove(source);
                allocationCounts.add(target);
                allocationBytes.compute(source, (k, v) -> v - bucketSize);
                allocationBytes.compute(target, (k, v) -> v + bucketSize);

                sourceToAllocationChanges.put(
                        existingAssignment.getNodeIdentifier(),
                        new BucketAssignment(existingAssignment.getDistributionId(), existingAssignment.getBucketNumber(), target));
            }
        }
    }

    return sourceToAllocationChanges;
}
 
Example 18
Source File: RebalancingHelper.java    From Singularity with Apache License 2.0 4 votes vote down vote up
public List<SingularityTaskId> rebalanceRacks(
  SingularityRequest request,
  List<SingularityTaskId> remainingActiveTasks,
  Optional<String> user
) {
  List<SingularityTaskId> extraCleanedTasks = new ArrayList<>();
  int activeRacksWithCapacityCount = slaveAndRackManager.getActiveRacksWithCapacityCount();
  double perRack = request.getInstancesSafe() / (double) activeRacksWithCapacityCount;

  Multiset<String> countPerRack = HashMultiset.create();
  for (SingularityTaskId taskId : remainingActiveTasks) {
    countPerRack.add(taskId.getRackId());
    LOG.info(
      "{} - {} - {} - {}",
      countPerRack,
      perRack,
      extraCleanedTasks.size(),
      taskId
    );
    if (
      countPerRack.count(taskId.getRackId()) > perRack &&
      extraCleanedTasks.size() < activeRacksWithCapacityCount / 2 &&
      taskId.getInstanceNo() > 1
    ) {
      extraCleanedTasks.add(taskId);
      LOG.info("Cleaning up task {} to evenly distribute tasks among racks", taskId);
      taskManager.createTaskCleanup(
        new SingularityTaskCleanup(
          user,
          TaskCleanupType.REBALANCE_RACKS,
          System.currentTimeMillis(),
          taskId,
          Optional.empty(),
          Optional.empty(),
          Optional.empty()
        )
      );
    }
  }
  return extraCleanedTasks;
}
 
Example 19
Source File: MultisetExpression.java    From gef with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public int count(Object element) {
	final Multiset<E> multiset = get();
	return (multiset == null) ? EMPTY_MULTISET.count(element)
			: multiset.count(element);
}
 
Example 20
Source File: CaseArgumentAnalyser.java    From JGiven with Apache License 2.0 4 votes vote down vote up
/**
 * Finds for each JoinedArgs set the best fitting name.
 * <p>
 * First it is tried to find a name from the explicitParameterNames list, by comparing the argument values
 * with the explicit case argument values. If no matching value can be found, the name of the argument is taken.
 */
private List<String> findArgumentNames( List<List<JoinedArgs>> joinedArgs, List<List<String>> explicitParameterValues,
        List<String> explicitParameterNames ) {
    List<String> argumentNames = Lists.newArrayListWithExpectedSize( joinedArgs.get( 0 ).size() );
    Multiset<String> paramNames = TreeMultiset.create();

    arguments:
    for( int iArg = 0; iArg < joinedArgs.get( 0 ).size(); iArg++ ) {
        parameters:
        for( int iParam = 0; iParam < explicitParameterNames.size(); iParam++ ) {
            String paramName = explicitParameterNames.get( iParam );

            boolean formattedValueMatches = true;
            boolean valueMatches = true;
            for( int iCase = 0; iCase < joinedArgs.size(); iCase++ ) {
                JoinedArgs args = joinedArgs.get( iCase ).get( iArg );

                String parameterValue = explicitParameterValues.get( iCase ).get( iParam );

                String formattedValue = args.words.get( 0 ).getFormattedValue();
                if( !formattedValue.equals( parameterValue ) ) {
                    formattedValueMatches = false;
                }

                String value = args.words.get( 0 ).getValue();
                if( !value.equals( parameterValue ) ) {
                    valueMatches = false;
                }

                if( !formattedValueMatches && !valueMatches ) {
                    continue parameters;
                }
            }

            // on this point either all formatted values match or all values match (or both)
            argumentNames.add( paramName );
            paramNames.add( paramName );
            continue arguments;
        }

        argumentNames.add( null );
    }

    Set<String> usedNames = Sets.newHashSet();
    for( int iArg = 0; iArg < joinedArgs.get( 0 ).size(); iArg++ ) {
        String name = argumentNames.get( iArg );
        if( name == null || paramNames.count( name ) > 1 ) {
            String origName = getArgumentName( joinedArgs, iArg );
            name = findFreeName( usedNames, origName );
            argumentNames.set( iArg, name );
        }
        usedNames.add( name );

    }

    return argumentNames;
}