List of usage examples for com.google.common.collect Range canonical
public Range<C> canonical(DiscreteDomain<C> domain)
From source file:org.onlab.util.ClosedOpenRange.java
/** * Creates a range from a Guava's range. * * @param range Guava's range//from w ww .ja va 2s. co m * @return this range */ public static ClosedOpenRange of(Range<Integer> range) { return new ClosedOpenRange(range.canonical(DiscreteDomain.integers()).lowerEndpoint(), range.canonical(DiscreteDomain.integers()).upperEndpoint()); }
From source file:com.google.googlejavaformat.java.SnippetFormatter.java
private static Range<Integer> offsetRange(Range<Integer> range, int offset) { range = range.canonical(DiscreteDomain.integers()); return Range.closedOpen(range.lowerEndpoint() + offset, range.upperEndpoint() + offset); }
From source file:com.github.fge.grappa.matchers.join.JoinMatcherBuilder.java
private static Range<Integer> toClosedRange(Range<Integer> range) { /*/*from ww w . ja v a2 s . c o m*/ * The canonical form will always be the same: closed on the lower bound * (if any; but here we are guaranteed that), open on the upper bound * (if any). * * All we have to do is therefore to pick the canonical representation, * pick the lower bound, and if it has an upper bound, pick it and * substract 1. */ Range<Integer> canonical = range.canonical(DiscreteDomain.integers()); int lowerBound = canonical.lowerEndpoint(); return canonical.hasUpperBound() ? Range.closed(lowerBound, canonical.upperEndpoint() - 1) : Range.atLeast(lowerBound); }
From source file:com.google.android.apps.forscience.whistlepunk.sensordb.SensorDatabaseImpl.java
/** * Gets the selection string and selectionArgs based on the tag, range and resolution tier. * * @return a pair where the first element is the selection string and the second element is the * array of selectionArgs.//from ww w . ja v a 2s. c o m */ private Pair<String, String[]> getSelectionAndArgs(String sensorTag, TimeRange range, int resolutionTier) { List<String> clauses = new ArrayList<>(); List<String> values = new ArrayList<>(); clauses.add(ScalarSensorsTable.Column.TAG + " = ?"); values.add(sensorTag); if (resolutionTier >= 0) { clauses.add(ScalarSensorsTable.Column.RESOLUTION_TIER + " = ?"); values.add(String.valueOf(resolutionTier)); } Range<Long> times = range.getTimes(); Range<Long> canonicalTimes = times.canonical(DiscreteDomain.longs()); if (canonicalTimes.hasLowerBound()) { String comparator = (canonicalTimes.lowerBoundType() == BoundType.CLOSED) ? " >= ?" : " > ?"; clauses.add(ScalarSensorsTable.Column.TIMESTAMP_MILLIS + comparator); values.add(String.valueOf(canonicalTimes.lowerEndpoint())); } if (canonicalTimes.hasUpperBound()) { String comparator = (canonicalTimes.upperBoundType() == BoundType.CLOSED) ? " =< ?" : " < ?"; clauses.add(ScalarSensorsTable.Column.TIMESTAMP_MILLIS + comparator); values.add(String.valueOf(canonicalTimes.upperEndpoint())); } return new Pair<>(Joiner.on(" AND ").join(clauses), values.toArray(new String[values.size()])); }
From source file:edu.mit.streamjit.impl.compiler2.Storage.java
/** * Returns a range spanning the indices read from this storage during an * execution of the given schedule. (Note that, as a span, not every * contained index will be read.) The returned range will be * {@link Range#canonical(com.google.common.collect.DiscreteDomain) canonical}. * The range is not cached so as to be responsive to changes in input index * functions./*from www . j a va 2 s .co m*/ * @param externalSchedule the schedule * @return a range spanning the indices read during the given schedule under * the current index functions * @see #readIndices(java.util.Map) */ public Range<Integer> readIndexSpan(Map<ActorGroup, Integer> externalSchedule) { int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE; for (Actor a : downstream()) { int maxIteration = a.group().schedule().get(a) * externalSchedule.get(a.group()) - 1; if (maxIteration >= 0) for (int input = 0; input < a.inputs().size(); ++input) if (a.inputs().get(input).equals(this) && (a.peek(input).max() > 0 || a.pop(input).max() > 0)) { min = Math.min(min, a.translateInputIndex(input, a.peeks(input, 0).first())); max = Math.max(max, a.translateInputIndex(input, a.peeks(input, maxIteration).last())); } } Range<Integer> range = (min != Integer.MAX_VALUE ? Range.closed(min, max) : Range.closedOpen(0, 0)); return range.canonical(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 iterations.//from www . j a va 2s .c o m * @param s the storage being read from * @param iterations the group iterations * @return the physical indices read */ public ImmutableSortedSet<Integer> reads(Storage s, Range<Integer> iterations) { iterations = iterations.canonical(DiscreteDomain.integers()); ImmutableSortedSet.Builder<Integer> builder = ImmutableSortedSet.naturalOrder(); for (Actor a : actors()) builder.addAll(a.reads(s, Range.closedOpen(iterations.lowerEndpoint() * schedule.get(a), iterations.upperEndpoint() * schedule.get(a)))); return builder.build(); }
From source file:com.google.googlejavaformat.java.JavaOutput.java
/** * Emit a list of {@link Replacement}s to convert from input to output. * * @return a list of {@link Replacement}s, sorted by start index, without overlaps *///w w w .j a va2s. c o m public ImmutableList<Replacement> getFormatReplacements(RangeSet<Integer> iRangeSet0) { ImmutableList.Builder<Replacement> result = ImmutableList.builder(); Map<Integer, Range<Integer>> kToJ = JavaOutput.makeKToIJ(this); // Expand the token ranges to align with re-formattable boundaries. RangeSet<Integer> breakableRanges = TreeRangeSet.create(); RangeSet<Integer> iRangeSet = iRangeSet0.subRangeSet(Range.closed(0, javaInput.getkN())); for (Range<Integer> iRange : iRangeSet.asRanges()) { Range<Integer> range = expandToBreakableRegions(iRange.canonical(DiscreteDomain.integers())); if (range.equals(EMPTY_RANGE)) { // the range contains only whitespace continue; } breakableRanges.add(range); } // Construct replacements for each reformatted region. for (Range<Integer> range : breakableRanges.asRanges()) { Input.Tok startTok = startTok(javaInput.getToken(range.lowerEndpoint())); Input.Tok endTok = endTok(javaInput.getToken(range.upperEndpoint() - 1)); // Add all output lines in the given token range to the replacement. StringBuilder replacement = new StringBuilder(); int replaceFrom = startTok.getPosition(); // Replace leading whitespace in the input with the whitespace from the formatted file while (replaceFrom > 0) { char previous = javaInput.getText().charAt(replaceFrom - 1); if (!CharMatcher.whitespace().matches(previous)) { break; } replaceFrom--; } int i = kToJ.get(startTok.getIndex()).lowerEndpoint(); // Include leading blank lines from the formatted output, unless the formatted range // starts at the beginning of the file. while (i > 0 && getLine(i - 1).isEmpty()) { i--; } // Write out the formatted range. for (; i < kToJ.get(endTok.getIndex()).upperEndpoint(); i++) { // It's possible to run out of output lines (e.g. if the input ended with // multiple trailing newlines). if (i < getLineCount()) { if (i > 0) { replacement.append(lineSeparator); } replacement.append(getLine(i)); } } int replaceTo = Math.min(endTok.getPosition() + endTok.length(), javaInput.getText().length()); // If the formatted ranged ended in the trailing trivia of the last token before EOF, // format all the way up to EOF to deal with trailing whitespace correctly. if (endTok.getIndex() == javaInput.getkN() - 1) { replaceTo = javaInput.getText().length(); } // Replace trailing whitespace in the input with the whitespace from the formatted file. // If the trailing whitespace in the input includes one or more line breaks, preserve the // whitespace after the last newline to avoid re-indenting the line following the formatted // line. int newline = -1; while (replaceTo < javaInput.getText().length()) { char next = javaInput.getText().charAt(replaceTo); if (!CharMatcher.whitespace().matches(next)) { break; } int newlineLength = Newlines.hasNewlineAt(javaInput.getText(), replaceTo); if (newlineLength != -1) { newline = replaceTo; // Skip over the entire newline; don't count the second character of \r\n as a newline. replaceTo += newlineLength; } else { replaceTo++; } } if (newline != -1) { replaceTo = newline; } if (newline == -1) { // There wasn't an existing trailing newline; add one. replacement.append(lineSeparator); } for (; i < getLineCount(); i++) { String after = getLine(i); int idx = CharMatcher.whitespace().negate().indexIn(after); if (idx == -1) { // Write out trailing empty lines from the formatted output. replacement.append(lineSeparator); } else { if (newline == -1) { // If there wasn't a trailing newline in the input, indent the next line. replacement.append(after.substring(0, idx)); } break; } } result.add(Replacement.create(replaceFrom, replaceTo, replacement.toString())); } return result.build(); }
From source file:com.google.googlejavaformat.java.JavaInput.java
public RangeSet<Integer> characterRangesToTokenRanges(Collection<Range<Integer>> characterRanges) throws FormatterException { RangeSet<Integer> tokenRangeSet = TreeRangeSet.create(); for (Range<Integer> characterRange0 : characterRanges) { Range<Integer> characterRange = characterRange0.canonical(DiscreteDomain.integers()); tokenRangeSet.add(characterRangeToTokenRange(characterRange.lowerEndpoint(), characterRange.upperEndpoint() - characterRange.lowerEndpoint())); }/* www . j av a2 s . c o m*/ return tokenRangeSet; }
From source file:edu.mit.streamjit.impl.compiler2.Storage.java
/** * Returns a set containing the indices live before the initialization * schedule; that is, the indices holding initial data. (Note that, as a * span, not every contained index will be occupied.) The returned range * will be// ww w . ja v a 2s .c om * {@link Range#canonical(com.google.common.collect.DiscreteDomain) canonical}. * The range is not cached so as to be responsive to changes in initial data * index functions. * @return a range spanning the indices holding initial data under the * current index functions * @see #initialDataIndices() */ public Range<Integer> initialDataIndexSpan() { Range<Integer> range = null; for (Pair<ImmutableList<Object>, IndexFunction> p : initialData()) for (int i = 0; i < p.first.size(); ++i) try { int x = p.second.applyAsInt(i); range = (range == null) ? Range.singleton(x) : range.span(Range.singleton(x)); } catch (Throwable ex) { throw new AssertionError("index functions should not throw", ex); } range = (range != null ? range : Range.closedOpen(0, 0)); return range.canonical(DiscreteDomain.integers()); }
From source file:edu.mit.streamjit.impl.compiler2.Storage.java
/** * Returns a range spanning the indices written in this storage during an * execution of the given schedule. (Note that, as a span, not every * contained index will be written.) The returned range will be * {@link Range#canonical(com.google.common.collect.DiscreteDomain) canonical}. * The range is not cached so as to be responsive to changes in output index * functions.//w ww . j a v a 2 s.com * @param externalSchedule the schedule * @return a range spanning the indices written during the given schedule * under the current index functions * @see #writeIndices(java.util.Map) */ public Range<Integer> writeIndexSpan(Map<ActorGroup, Integer> externalSchedule) { Range<Integer> range = null; for (Actor a : upstream()) { //just the first and last iteration int maxIteration = a.group().schedule().get(a) * externalSchedule.get(a.group()) - 1; if (maxIteration >= 0) for (int iteration : new int[] { 0, maxIteration }) { ImmutableSortedSet<Integer> writes = a.writes(this, iteration); Range<Integer> writeRange = writes.isEmpty() ? range : Range.closed(writes.first(), writes.last()); range = range == null ? writeRange : range.span(writeRange); } } range = (range != null ? range : Range.closedOpen(0, 0)); return range.canonical(DiscreteDomain.integers()); }