List of usage examples for com.google.common.collect Range closedOpen
public static <C extends Comparable<?>> Range<C> closedOpen(C lower, C upper)
From source file:sim.ExtendedGraph.java
/** * Returns the probability spread across the graph. This is used for making * for a weighted random node selection. * * * @return HashMap with each node's ID and it's probability given as a * Range object//from w ww .ja v a 2 s .c o m */ protected HashMap<String, Range<Double>> agentProbabilitySpread() { double offset = 0.0; HashMap<String, Range<Double>> map = new HashMap<String, Range<Double>>(); Iterator<ExtendedNode> it = this.getNodeIterator(); while (it.hasNext()) { ExtendedNode n = it.next(); // Probability of the node being selected double p = (double) n.getAgentCount() / (double) numAgents; // Upper bound for the range of the this node double upper = offset + p; // https://code.google.com/p/guava-libraries/wiki/RangesExplained map.put(n.getId(), Range.closedOpen(offset, upper)); // Logger.trace("{0}; Probability {1}; Offset {2}; Upper {3}", n, // p, // offset, // upper); offset = upper; } return map; }
From source file:org.opendaylight.controller.cluster.datastore.AbstractFrontendHistory.java
private TransactionSuccess<?> handleTransactionPurgeRequest(final TransactionRequest<?> request, final RequestEnvelope envelope, final long now) { final TransactionIdentifier id = request.getTarget(); final UnsignedLong ul = UnsignedLong.fromLongBits(id.getTransactionId()); if (purgedTransactions.contains(ul)) { // Retransmitted purge request: nothing to do LOG.debug("{}: transaction {} already purged", persistenceId, id); return new TransactionPurgeResponse(id, request.getSequence()); }/* w w w . j a v a 2 s . c o m*/ // We perform two lookups instead of a straight remove, because once the map becomes empty we switch it // to an ImmutableMap, which does not allow remove(). if (closedTransactions.containsKey(ul)) { tree.purgeTransaction(id, () -> { closedTransactions.remove(ul); if (closedTransactions.isEmpty()) { closedTransactions = ImmutableMap.of(); } purgedTransactions.add(Range.closedOpen(ul, UnsignedLong.ONE.plus(ul))); LOG.debug("{}: finished purging inherited transaction {}", persistenceId(), id); envelope.sendSuccess(new TransactionPurgeResponse(id, request.getSequence()), readTime() - now); }); return null; } final FrontendTransaction tx = transactions.get(id); if (tx == null) { // This should never happen because the purge callback removes the transaction and puts it into // purged transactions in one go. If it does, we warn about the situation and LOG.warn("{}: transaction {} not tracked in {}, but not present in active transactions", persistenceId, id, purgedTransactions); purgedTransactions.add(Range.closedOpen(ul, UnsignedLong.ONE.plus(ul))); return new TransactionPurgeResponse(id, request.getSequence()); } tree.purgeTransaction(id, () -> { purgedTransactions.add(Range.closedOpen(ul, UnsignedLong.ONE.plus(ul))); transactions.remove(id); LOG.debug("{}: finished purging transaction {}", persistenceId(), id); envelope.sendSuccess(new TransactionPurgeResponse(id, request.getSequence()), readTime() - now); }); return null; }
From source file:edu.mit.streamjit.impl.compiler2.Actor.java
/** * Returns the logical indices peeked or popped on the given input during * the given iterations. Note that this method may return a nonempty set * even if peeks(input) returns 0 and isPeeking() returns false. * @param input the input index/*from w w w . j ava 2 s . c o m*/ * @param iterations the iteration numbers * @return the logical indices peeked or popped on the given input during * the given iterations */ public ContiguousSet<Integer> peeks(int input, ContiguousSet<Integer> iterations) { if (iterations.isEmpty()) return ContiguousSet.create(Range.closedOpen(0, 0), DiscreteDomain.integers()); return ContiguousSet.create( Range.closedOpen(iterations.first() * pop(input).max(), (iterations.last() + 1) * pop(input).max() + excessPeeks(input)), DiscreteDomain.integers()); }
From source file:edu.mit.streamjit.impl.compiler2.ActorGroup.java
/** * Returns the physical indices read from the given storage during the given * group iteration.//from w w w. ja v a 2 s. c o m * @param s the storage being read from * @param iteration the group iteration number * @return the physical indices read */ public ImmutableSortedSet<Integer> reads(Storage s, int iteration) { ImmutableSortedSet.Builder<Integer> builder = ImmutableSortedSet.naturalOrder(); for (Actor a : actors()) builder.addAll( a.reads(s, Range.closedOpen(iteration * schedule.get(a), (iteration + 1) * schedule.get(a)))); return builder.build(); }
From source file:org.apache.aurora.scheduler.updater.JobDiff.java
/** * Creates a map of {@code instanceCount} copies of {@code config}. * * @param config Configuration to generate an instance mapping for. * @param instanceCount Number of instances to represent. * @return A map of instance IDs (from 0 to {@code instanceCount - 1}) to {@code config}. *//*from w ww. jav a 2s. co m*/ public static Map<Integer, ITaskConfig> asMap(ITaskConfig config, int instanceCount) { requireNonNull(config); Set<Integer> desiredInstances = ContiguousSet.create(Range.closedOpen(0, instanceCount), DiscreteDomain.integers()); return ImmutableMap.copyOf(Maps.asMap(desiredInstances, Functions.constant(config))); }
From source file:com.basistech.tclre.ColorMap.java
/** * subcolor - allocate a new subcolor (if necessary) to this char * Internal API that can do a range of characters; called from * {@link #subrange}./* w w w .j a v a 2 s .com*/ * * @param c The character, or first character in a range, to process. * @param rangeCount the number of characters. */ private short subcolor(int c, int rangeCount) throws RegexException { short co; /* current color of c */ short sco; /* new subcolor */ co = getcolor(c); sco = newsub(co); assert sco != Constants.COLORLESS; if (co == sco) /* already in an open subcolor */ { return co; /* rest is redundant */ } ColorDesc cd = colorDescs.get(co); cd.incrementNChars(-rangeCount); ColorDesc scd = colorDescs.get(sco); scd.incrementNChars(rangeCount); map.put(Range.closedOpen(c, c + rangeCount), sco); return sco; }
From source file:com.google.googlejavaformat.java.JavaOutput.java
/** Flush any incomplete last line, then add the EOF token into our data structures. */ void flush() {/*w w w . j a va 2s. c o m*/ String lastLine = lineBuilder.toString(); if (!CharMatcher.whitespace().matchesAllOf(lastLine)) { mutableLines.add(lastLine); } int jN = mutableLines.size(); Range<Integer> eofRange = Range.closedOpen(kN, kN + 1); while (ranges.size() < jN) { ranges.add(Formatter.EMPTY_RANGE); } ranges.add(eofRange); setLines(ImmutableList.copyOf(mutableLines)); }
From source file:org.apache.drill.exec.store.schedule.BlockMapBuilder.java
/** * Builds a mapping of block locations to file byte range *//*from w ww . j a va 2s .c o m*/ private ImmutableRangeMap<Long, BlockLocation> buildBlockMap(FileStatus status) throws IOException { final Timer.Context context = metrics.timer(BLOCK_MAP_BUILDER_TIMER).time(); BlockLocation[] blocks; ImmutableRangeMap<Long, BlockLocation> blockMap; blocks = fs.getFileBlockLocations(status, 0, status.getLen()); ImmutableRangeMap.Builder<Long, BlockLocation> blockMapBuilder = new ImmutableRangeMap.Builder<Long, BlockLocation>(); for (BlockLocation block : blocks) { long start = block.getOffset(); long end = start + block.getLength(); Range<Long> range = Range.closedOpen(start, end); blockMapBuilder = blockMapBuilder.put(range, block); } blockMap = blockMapBuilder.build(); blockMapMap.put(status.getPath(), blockMap); context.stop(); return blockMap; }
From source file:org.apache.aurora.scheduler.sla.MetricCalculator.java
@Timed("sla_stats_computation") @Override/*from ww w . j ava 2 s .co m*/ public void run() { FluentIterable<IScheduledTask> tasks = FluentIterable .from(Storage.Util.fetchTasks(storage, Query.unscoped())); List<IScheduledTask> prodTasks = tasks .filter(Predicates.compose(Predicates.and(ITaskConfig::isProduction, IS_SERVICE), Tasks::getConfig)) .toList(); List<IScheduledTask> nonProdTasks = tasks.filter(Predicates .compose(Predicates.and(Predicates.not(ITaskConfig::isProduction), IS_SERVICE), Tasks::getConfig)) .toList(); long nowMs = clock.nowMillis(); Range<Long> timeRange = Range.closedOpen(nowMs - settings.refreshRateMs, nowMs); runAlgorithms(prodTasks, settings.prodMetrics, timeRange, NAME_QUALIFIER_PROD); runAlgorithms(nonProdTasks, settings.nonProdMetrics, timeRange, NAME_QUALIFIER_NON_PROD); }
From source file:it.units.malelab.ege.ge.mapper.HierarchicalMapper.java
public Node<T> mapIteratively(BitsGenotype genotype, int[] bitUsages) throws MappingException { Node<EnhancedSymbol<T>> enhancedTree = new Node<>( new EnhancedSymbol<>(grammar.getStartingSymbol(), Range.closedOpen(0, genotype.size()))); while (true) { Node<EnhancedSymbol<T>> nodeToBeReplaced = null; for (Node<EnhancedSymbol<T>> node : enhancedTree.leafNodes()) { if (grammar.getRules().keySet().contains(node.getContent().getSymbol())) { nodeToBeReplaced = node; break; }/* w w w .jav a 2 s . c o m*/ } if (nodeToBeReplaced == null) { break; } //get genotype T symbol = nodeToBeReplaced.getContent().getSymbol(); Range<Integer> symbolRange = nodeToBeReplaced.getContent().getRange(); List<List<T>> options = grammar.getRules().get(symbol); //get option List<T> symbols; if ((symbolRange.upperEndpoint() - symbolRange.lowerEndpoint()) < options.size()) { int count = (symbolRange.upperEndpoint() - symbolRange.lowerEndpoint() > 0) ? genotype.slice(symbolRange).count() : genotype.count(); int index = shortestOptionIndexesMap.get(symbol) .get(count % shortestOptionIndexesMap.get(symbol).size()); symbols = options.get(index); } else { symbols = chooseOption(genotype, symbolRange, options); for (int i = symbolRange.lowerEndpoint(); i < symbolRange.upperEndpoint(); i++) { bitUsages[i] = bitUsages[i] + 1; } } //add children List<Range<Integer>> childRanges = getChildrenSlices(symbolRange, symbols); for (int i = 0; i < symbols.size(); i++) { Range<Integer> childRange = childRanges.get(i); if (childRanges.get(i).equals(symbolRange) && (childRange.upperEndpoint() - childRange.lowerEndpoint() > 0)) { childRange = Range.closedOpen(symbolRange.lowerEndpoint(), symbolRange.upperEndpoint() - 1); } Node<EnhancedSymbol<T>> newChild = new Node<>(new EnhancedSymbol<>(symbols.get(i), childRange)); nodeToBeReplaced.getChildren().add(newChild); } } //convert return extractFromEnhanced(enhancedTree); }