Example usage for com.google.common.collect RangeSet subRangeSet

List of usage examples for com.google.common.collect RangeSet subRangeSet

Introduction

In this page you can find the example usage for com.google.common.collect RangeSet subRangeSet.

Prototype

RangeSet<C> subRangeSet(Range<C> view);

Source Link

Document

Returns a view of the intersection of this RangeSet with the specified range.

Usage

From source file:io.druid.sql.calcite.filtration.RangeSets.java

/**
 * Intersects a set of ranges, or returns null if the set is empty.
 *///from   w  w  w. ja  v  a  2 s .  c  o  m
public static <T extends Comparable<T>> RangeSet<T> intersectRanges(final Iterable<Range<T>> ranges) {
    RangeSet<T> rangeSet = null;
    for (final Range<T> range : ranges) {
        if (rangeSet == null) {
            rangeSet = TreeRangeSet.create();
            rangeSet.add(range);
        } else {
            rangeSet = TreeRangeSet.create(rangeSet.subRangeSet(range));
        }
    }
    return rangeSet;
}

From source file:com.google.googlejavaformat.java.Formatter.java

/**
 * Converts zero-indexed, [closed, open) line ranges in the given source file to character ranges.
 *//*  w  w  w .j a  v  a  2  s  .c  om*/
public static RangeSet<Integer> lineRangesToCharRanges(String input, RangeSet<Integer> lineRanges) {
    List<Integer> lines = new ArrayList<>();
    Iterators.addAll(lines, Newlines.lineOffsetIterator(input));
    lines.add(input.length() + 1);

    final RangeSet<Integer> characterRanges = TreeRangeSet.create();
    for (Range<Integer> lineRange : lineRanges.subRangeSet(Range.closedOpen(0, lines.size() - 1)).asRanges()) {
        int lineStart = lines.get(lineRange.lowerEndpoint());
        // Exclude the trailing newline. This isn't strictly necessary, but handling blank lines
        // as empty ranges is convenient.
        int lineEnd = lines.get(lineRange.upperEndpoint()) - 1;
        Range<Integer> range = Range.closedOpen(lineStart, lineEnd);
        characterRanges.add(range);
    }
    return characterRanges;
}

From source file:com.pingcap.tikv.predicates.RangeBuilder.java

/**
 * Turn CNF filters into range//w w  w .jav  a  2s  .  c  o m
 *
 * @param accessConditions filters in CNF list
 * @param type index column type
 * @return access ranges
 */
@SuppressWarnings("unchecked")
static List<Range> exprToRanges(List<TiExpr> accessConditions, DataType type) {
    if (accessConditions == null || accessConditions.size() == 0) {
        return ImmutableList.of();
    }
    RangeSet ranges = TreeRangeSet.create();
    ranges.add(Range.all());
    for (TiExpr ac : accessConditions) {
        NormalizedCondition cond = AccessConditionNormalizer.normalize(ac);
        TiConstant constVal = cond.constantVals.get(0);
        Comparable<?> comparableVal = Comparables.wrap(constVal.getValue());
        TiExpr expr = cond.condition;

        if (expr instanceof GreaterThan) {
            ranges = ranges.subRangeSet(Range.greaterThan(comparableVal));
        } else if (expr instanceof GreaterEqual) {
            ranges = ranges.subRangeSet(Range.atLeast(comparableVal));
        } else if (expr instanceof LessThan) {
            ranges = ranges.subRangeSet(Range.lessThan(comparableVal));
        } else if (expr instanceof LessEqual) {
            ranges = ranges.subRangeSet(Range.atMost(comparableVal));
        } else if (expr instanceof Equal) {
            ranges = ranges.subRangeSet(Range.singleton(comparableVal));
        } else if (expr instanceof NotEqual) {
            RangeSet left = ranges.subRangeSet(Range.lessThan(comparableVal));
            RangeSet right = ranges.subRangeSet(Range.greaterThan(comparableVal));
            ranges = TreeRangeSet.create(left);
            ranges.addAll(right);
        } else {
            throw new TiClientInternalException(
                    "Unsupported conversion to Range " + expr.getClass().getSimpleName());
        }
    }
    return ImmutableList.copyOf(ranges.asRanges());
}

From source file:org.eclipse.fx.ui.controls.styledtext.internal.VerticalLineFlow.java

public void update(RangeSet<Integer> r) {
    prepareNodes(r.subRangeSet(this.visibleLines.get())
            .subRangeSet(Range.closedOpen(Integer.valueOf(0), Integer.valueOf(this.numberOfLines.get()))));
}

From source file:org.eclipse.fx.ui.controls.styledtext.internal.VerticalLineFlow.java

public RangeSet<Integer> getVisibleLines() {
    RangeSet<Integer> visibleLines = TreeRangeSet.create();
    visibleLines.add(this.visibleLines.get());
    return visibleLines
            .subRangeSet(Range.closedOpen(Integer.valueOf(0), Integer.valueOf(this.numberOfLines.get())));
}

From source file:org.apache.druid.timeline.partition.SingleDimensionShardSpec.java

@Override
public boolean possibleInDomain(Map<String, RangeSet<String>> domain) {
    RangeSet<String> rangeSet = domain.get(dimension);
    if (rangeSet == null) {
        return true;
    }//w ww.j  a  va  2  s . co m
    return !rangeSet.subRangeSet(getRange()).isEmpty();
}

From source file:io.horizondb.model.core.predicates.AndPredicate.java

/**
 * {@inheritDoc}/*  ww  w.j  a v a 2 s.co m*/
 */
@Override
public RangeSet<Field> getTimestampRanges() {

    RangeSet<Field> leftRanges = this.left.getTimestampRanges();
    RangeSet<Field> rightRanges = this.right.getTimestampRanges();

    Builder<Field> builder = ImmutableRangeSet.builder();

    for (Range<Field> range : leftRanges.asRanges()) {

        builder.addAll(rightRanges.subRangeSet(range));
    }

    return builder.build();
}

From source file:org.eclipse.fx.ui.controls.styledtext.internal.VFlow.java

private void triggerUpdate(RangeSet<Integer> range) {
    Range<Integer> all = Range.closedOpen(Integer.valueOf(0), Integer.valueOf(getNumberOfLines()));
    RangeSet<Integer> onlyExisting = range.subRangeSet(all);
    RangeSet<Integer> onlyVisible = onlyExisting.subRangeSet(getVisible());

    onlyVisible.asRanges().stream().flatMapToInt(VFlow::toIntStream).forEach(index -> updateNode(index));
}

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 .  ja  v a2s . 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:eu.trentorise.opendata.semtext.SemText.java

/**
 * Returns a copy of the this SemText without terms intersecting provided
 * ranges.//w w w  .  j a v a2  s . c o  m
 *
 */
// note: Current version is inefficient, tried RangeMap.remove, but it only removes matching subsegments! 
public SemText deleteTerms(Iterable<Range<Integer>> deletionRanges) {
    checkNotNull(deletionRanges);

    ImmutableList.Builder<Sentence> sentencesB = ImmutableList.builder();

    RangeSet<Integer> rangeSet = TreeRangeSet.create();

    for (Range r : deletionRanges) {
        rangeSet.add(r);
    }

    for (Sentence sentence : sentences) {
        ImmutableList.Builder<Term> termsB = ImmutableList.builder();
        for (Term term : sentence.getTerms()) {
            RangeSet<Integer> intersection = rangeSet.subRangeSet(SemTexts.spanToRange(term));
            if (intersection.isEmpty()) {
                termsB.add(term);
            }

        }
        sentencesB.add(sentence.withTerms(termsB.build()));
    }

    return this.withSentences(sentencesB.build());
}