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:org.dishevelled.bio.align.Alignments.java
/** * Return the alignment mismatches in the specified alignment pair as 0-based [closed, open) ranges. * * @param alignmentPair alignment pair, must not be null * @return the alignment mismatches in the alignment pair as 0-based [closed, open) ranges *///w ww .j ava 2s . c o m public static List<Range<Long>> mismatches(final AlignmentPair alignmentPair) { checkNotNull(alignmentPair); List<Range<Long>> mismatches = new ArrayList<Range<Long>>(); int mismatchStart = -1; for (int i = 1, length = alignmentPair.length() + 1; i < length; i++) { if (isMismatchSymbol(alignmentPair.symbolAt(i))) { if (mismatchStart < 0) { mismatchStart = i; } } else { if (mismatchStart > 0) { // biojava coordinates are 1-based mismatches.add(Range.closedOpen(Long.valueOf(mismatchStart - 1L), Long.valueOf(i - 1L))); mismatchStart = -1; } } } if (mismatchStart > 0) { mismatches .add(Range.closedOpen(Long.valueOf(mismatchStart - 1L), Long.valueOf(alignmentPair.length()))); } return mismatches; }
From source file:eu.trentorise.opendata.semtext.SemTexts.java
/** * Converts provided span to a Guava Range of the [start, end) form. *///from ww w . j ava2 s. c om public static Range spanToRange(Span span) { return Range.closedOpen(span.getStart(), span.getEnd()); }
From source file:com.google.testing.pogen.parser.template.TemplateParser.java
/** * Returns a {@link Map} of names and indexes between the start and the end * tags for non-nested tags./*from www . j ava2 s. co m*/ * * @param text * the string to be parsed * @param startPattern * the regular expression of start tags to find * @param endPattern * the regular expression of end tags to find * @param groupIndex * the index of the group in matched parts to retrieve a name * @return the {@link Map} of the names and the indexes between the start * and the end tags * @throws TemplateParseException * if the specified template is in bad format where broken pairs * of start and end tags appear */ protected static Map<String, Range<Integer>> getNamedIndexRangesOfNonNestedTags(String text, Pattern startPattern, Pattern endPattern, int groupIndex) throws TemplateParseException { Map<String, Range<Integer>> templates = Maps.newHashMap(); List<StringWithIndex> starts = getMatchedStringAndIndexes(text, startPattern, groupIndex); List<Integer> ends = getMatchedIndexes(text, endPattern); // Check whether the sizes of start tags and end tags are equal if (starts.size() != ends.size()) { throw new TemplateParseException( String.format("There are broken pairs of start and end tags (#start tags: %d, #end tags: %d)", starts.size(), ends.size())); } int endIndexesIndex = 0; for (StringWithIndex nameAndIndex : starts) { int endIndex = ends.get(endIndexesIndex++); // Check whether all start tags and end tags are paired correctly if (nameAndIndex.getIndex() >= endIndex) { throw new TemplateParseException(String.format("Broken pairs of start and end tags are found.")); } templates.put(nameAndIndex.getString(), Range.closedOpen(nameAndIndex.getIndex(), endIndex)); } return templates; }
From source file:org.robotframework.red.nattable.painter.RedTableTextPainter.java
private Stream<Range<Integer>> splitRangesForWrappedLabel(final int[] map, final String text, final Range<Integer> range) { final int lower = range.lowerEndpoint(); final int upper = range.upperEndpoint() - 1; final int lowerEndpoint = map[lower] == -1 ? map[lower + 1] : map[lower]; final int upperEndpoint = map[upper] == -1 ? map[upper - 1] : map[upper]; final List<Range<Integer>> transformedRanges = new ArrayList<>(); if (lowerEndpoint <= upperEndpoint) { final Matcher matcher = NEW_LINE_PATTERN.matcher(text.substring(lowerEndpoint, upperEndpoint + 1)); int lastLower = lowerEndpoint; while (matcher.find()) { transformedRanges.add(Range.closedOpen(lastLower, matcher.start() + lowerEndpoint)); lastLower = matcher.end() + lowerEndpoint; }/* w ww.j a v a 2 s .com*/ transformedRanges.add(Range.closedOpen(lastLower, upperEndpoint + 1)); } return transformedRanges.stream(); }
From source file:it.units.malelab.ege.benchmark.mapper.MapperUtils.java
private static List<BitsGenotype> splitWeighted(BitsGenotype g, List<Double> weights, int maxN) { if (weights.isEmpty()) { return Collections.singletonList(g); }//w w w.j a v a2 s .co m if (g.size() == 0) { return Collections.nCopies(weights.size(), new BitsGenotype(0)); } double minWeight = Double.POSITIVE_INFINITY; for (double w : weights) { if ((w < minWeight) && (w > 0)) { minWeight = w; } } if (Double.isInfinite(minWeight)) { //all zero return split(g, weights.size(), maxN); } List<Integer> intWeights = new ArrayList<>(weights.size()); for (double w : weights) { intWeights.add((int) Math.max(Math.round(w / minWeight), 0d)); } List<Range<Integer>> ranges = Utils.slices(Range.closedOpen(0, g.size()), intWeights); return g.slices(ranges); }
From source file:com.google.googlejavaformat.java.RemoveUnusedImports.java
/** Construct replacements to fix unused imports. */ private static RangeMap<Integer, String> buildReplacements(String contents, JCCompilationUnit unit, Set<String> usedNames, Multimap<String, Range<Integer>> usedInJavadoc) { RangeMap<Integer, String> replacements = TreeRangeMap.create(); for (JCImport importTree : unit.getImports()) { String simpleName = getSimpleName(importTree); if (!isUnused(unit, usedNames, usedInJavadoc, importTree, simpleName)) { continue; }//from w w w. ja va 2 s . c o m // delete the import int endPosition = importTree.getEndPosition(unit.endPositions); endPosition = Math.max(CharMatcher.isNot(' ').indexIn(contents, endPosition), endPosition); String sep = Newlines.guessLineSeparator(contents); if (endPosition + sep.length() < contents.length() && contents.subSequence(endPosition, endPosition + sep.length()).toString().equals(sep)) { endPosition += sep.length(); } replacements.put(Range.closedOpen(importTree.getStartPosition(), endPosition), ""); // fully qualify any javadoc references with the same simple name as a deleted // non-static import if (!importTree.isStatic()) { for (Range<Integer> docRange : usedInJavadoc.get(simpleName)) { if (docRange == null) { continue; } String replaceWith = importTree.getQualifiedIdentifier().toString(); replacements.put(docRange, replaceWith); } } } return replacements; }
From source file:edu.mit.streamjit.impl.compiler2.Actor.java
/** * Returns the logical indices pushed to the given output during the given * iteration.// www . ja v a 2 s .c o m * @param output the output index * @param iteration the iteration number * @return the logical indices pushed to the given input during the given * iteration */ public ContiguousSet<Integer> pushes(int output, int iteration) { return ContiguousSet.create( Range.closedOpen(iteration * push(output).max(), (iteration + 1) * push(output).max()), DiscreteDomain.integers()); }
From source file:org.asoem.greyfish.utils.collect.BitString.java
/** * Create a random bit string of given {@code length} where each bit is set with probability {@code p}, and not set * with probability {@code 1-p}./*from w w w . ja v a2s . c o m*/ * * @param length the length of the bit string * @param rng the random number generator to use * @param p the probability for each bit in the new bit string to hold the value 1 * @return a new bit string */ public static BitString random(final int length, final RandomGenerator rng, final double p) { checkNotNull(rng); checkArgument(p >= 0 && p <= 1); checkArgument(length >= 0); if (length == 0) { return emptyBitSequence(); } if (p == 0.5) { return random(length, rng); // faster } final int n; if (p == 0) { n = 0; } else if (p == 1) { n = length; } else { final BinomialDistribution binomialDistribution = new BinomialDistribution(rng, length, p); n = binomialDistribution.sample(); } assert n >= 0 && n <= length : n; if (n == 0) { return zeros(length); } else if (n == length) { return ones(length); } final ContiguousSet<Integer> indexRange = ContiguousSet.create(Range.closedOpen(0, length), DiscreteDomain.integers()); final Iterable<Integer> uniqueIndexSample = Samplings.random(rng).withoutReplacement().sample(indexRange, n); if ((double) n / length < 1.0 / 32) { // < 1 bit per word? return new IndexSetString(ImmutableSortedSet.copyOf(uniqueIndexSample), length); } else { final BitSet bs = new BitSet(length); for (Integer index : uniqueIndexSample) { bs.set(index, true); } return new BitSetString(bs, length); } }
From source file:com.spotify.helios.system.SystemTestBase.java
@Before public void dockerSetup() throws Exception { final String portRange = System.getenv("DOCKER_PORT_RANGE"); final AllocatedPort allocatedPort; final int probePort; if (portRange != null) { final String[] parts = portRange.split(":", 2); dockerPortRange = Range.closedOpen(Integer.valueOf(parts[0]), Integer.valueOf(parts[1])); allocatedPort = Polling.await(LONG_WAIT_SECONDS, SECONDS, new Callable<AllocatedPort>() { @Override// ww w . j a v a 2 s.co m public AllocatedPort call() throws Exception { final int port = ThreadLocalRandom.current().nextInt(dockerPortRange.lowerEndpoint(), dockerPortRange.upperEndpoint()); return temporaryPorts.tryAcquire("docker-probe", port); } }); probePort = allocatedPort.port(); } else { dockerPortRange = temporaryPorts.localPortRange("docker", 10); probePort = dockerPortRange().lowerEndpoint(); allocatedPort = null; } try { assertDockerReachable(probePort); } finally { if (allocatedPort != null) { allocatedPort.release(); } } }
From source file:edu.mit.streamjit.impl.compiler2.ActorGroup.java
/** * Returns a void->void MethodHandle that will run this ActorGroup for the * given iterations using the given ConcreteStorage instances. * @param iterations the range of iterations to run for * @param storage the storage being used * @return a void->void method handle *//*from w ww .ja v a2 s. c o m*/ public MethodHandle specialize(Range<Integer> iterations, Map<Storage, ConcreteStorage> storage, BiFunction<MethodHandle[], WorkerActor, MethodHandle> switchFactory, int unrollFactor, ImmutableTable<Actor, Integer, IndexFunctionTransformer> inputTransformers, ImmutableTable<Actor, Integer, IndexFunctionTransformer> outputTransformers) { //TokenActors are special. assert !isTokenGroup() : actors(); Map<Actor, MethodHandle> withRWHandlesBound = bindActorsToStorage(iterations, storage, switchFactory, inputTransformers, outputTransformers); int totalIterations = iterations.upperEndpoint() - iterations.lowerEndpoint(); unrollFactor = Math.min(unrollFactor, totalIterations); int unrolls = (totalIterations / unrollFactor); int unrollEndpoint = iterations.lowerEndpoint() + unrolls * unrollFactor; MethodHandle overall = Combinators.semicolon( makeGroupLoop(Range.closedOpen(iterations.lowerEndpoint(), unrollEndpoint), unrollFactor, withRWHandlesBound), makeGroupLoop(Range.closedOpen(unrollEndpoint, iterations.upperEndpoint()), 1, withRWHandlesBound)); return overall; }