/** * Create an interval with the specified endpoints, reordering them as needed, * using the specified flags * @param a one of the endpoints * @param b the other endpoint * @param flags flags characterizing the interval * @param <E> type of the interval endpoints * @return Interval with endpoints re-ordered as needed */ public static <E extends Comparable<E>> Interval<E> toValidInterval(E a, E b, int flags) { int comp = a.compareTo(b); if (comp <= 0) { return new Interval<>(a, b, flags); } else { return new Interval<>(b, a, flags); } }
/** * Return set of flags indicating possible relationships between * this interval and another interval. * * @param other Interval with which to compare with * @return flags indicating possible relationship between this interval and the other interval */ public int getRelationFlags(Interval<E> other) { if (other == null) return 0; int flags = 0; int comp11 = this.first.compareTo(other.first()); // 3 choices flags |= toRelFlags(comp11, REL_FLAGS_SS_SHIFT); int comp22 = this.second.compareTo(other.second()); // 3 choices flags |= toRelFlags(comp22, REL_FLAGS_EE_SHIFT); int comp12 = this.first.compareTo(other.second()); // 3 choices flags |= toRelFlags(comp12, REL_FLAGS_SE_SHIFT); int comp21 = this.second.compareTo(other.first()); // 3 choices flags |= toRelFlags(comp21, REL_FLAGS_ES_SHIFT); flags = addIntervalRelationFlags(flags, false); return flags; }
/** * Checks whether this interval is comparable with another interval * comes before or after * @param other interval to compare with */ public boolean isIntervalComparable(Interval<E> other) { int flags = getRelationFlags(other); if (checkMultipleBitSet(flags & REL_FLAGS_INTERVAL_UNKNOWN)) { return false; } return checkFlagSet(flags, REL_FLAGS_INTERVAL_BEFORE) || checkFlagSet(flags, REL_FLAGS_INTERVAL_AFTER); }
public boolean contains(Interval<E> other) { boolean containsOtherBegin = (other.includesBegin())? contains(other.getBegin()): containsOpen(other.getBegin()); boolean containsOtherEnd = (other.includesEnd())? contains(other.getEnd()): containsOpen(other.getEnd()); return (containsOtherBegin && containsOtherEnd); }
/** * Check whether this interval overlaps with the other interval. * (I.e. the intersect would not be null.) * * @param other interval to compare with * @return true if this interval overlaps the other interval */ public boolean overlaps(Interval<E> other) { if (other == null) return false; int comp12 = this.first.compareTo(other.second()); int comp21 = this.second.compareTo(other.first()); if (comp12 > 0 || comp21 < 0) { return false; } else { if (comp12 == 0) { if (!this.includesBegin() || !other.includesEnd()) { return false; } } if (comp21 == 0) { if (!this.includesEnd() || !other.includesBegin()) { return false; } } return true; } }
protected boolean extractAnnotation(List<? extends CoreMap> source, CoreMapAggregator aggregator) { annotation = aggregator.merge(source, chunkOffsets.getBegin(), chunkOffsets.getEnd()); charOffsets = Interval.toInterval(annotation.get(CoreAnnotations.CharacterOffsetBeginAnnotation.class), annotation.get(CoreAnnotations.CharacterOffsetEndAnnotation.class), Interval.INTERVAL_OPEN_END); tokenOffsets = Interval.toInterval(annotation.get(CoreAnnotations.TokenBeginAnnotation.class), annotation.get(CoreAnnotations.TokenEndAnnotation.class), Interval.INTERVAL_OPEN_END); text = annotation.get(CoreAnnotations.TextAnnotation.class); extractFunc.annotate(this, source.subList(chunkOffsets.getBegin(), chunkOffsets.getEnd())); return true; }
public static List<? extends CoreMap> replaceMerged(List<? extends CoreMap> list, List<? extends MatchedExpression> matchedExprs) { if (matchedExprs == null) return list; matchedExprs.sort(EXPR_TOKEN_OFFSET_COMPARATOR); List<CoreMap> merged = new ArrayList<>(list.size()); // Approximate size int last = 0; for (MatchedExpression expr:matchedExprs) { int start = expr.chunkOffsets.first(); int end = expr.chunkOffsets.second(); if (start >= last) { merged.addAll(list.subList(last,start)); CoreMap m = expr.getAnnotation(); merged.add(m); last = end; } } // Add rest of elements if (last < list.size()) { merged.addAll(list.subList(last, list.size())); } return merged; }
public static <E extends Comparable<E>, T extends HasInterval<E>> boolean overlaps(TreeNode<E,T> node, Interval<E> target) { Stack<TreeNode<E,T>> todo = new Stack<>(); todo.push(node); while (!todo.isEmpty()) { TreeNode<E,T> n = todo.pop(); // Don't search nodes that don't exist if (n == null || n.isEmpty()) continue; // If target is to the right of the rightmost point of any interval // in this node and all children, there won't be any matches. if (target.first.compareTo(n.maxEnd) > 0) continue; // Check this node if (n.value.getInterval().overlaps(target)) { return true; } // Search left children if (n.left != null) { todo.add(n.left); } // If target is to the left of the start of this interval, // then it can't be in any child to the right. if (target.second.compareTo(n.value.getInterval().first()) < 0) { continue; } if (n.right != null) { todo.add(n.right); } } return false; }
protected static int addIntervalRelationFlags(int flags, boolean checkFuzzy) { int f11 = extractRelationSubflags(flags, REL_FLAGS_SS_SHIFT); int f22 = extractRelationSubflags(flags, REL_FLAGS_EE_SHIFT); int f12 = extractRelationSubflags(flags, REL_FLAGS_SE_SHIFT); int f21 = extractRelationSubflags(flags, REL_FLAGS_ES_SHIFT); if (checkFuzzy) { boolean isFuzzy = checkMultipleBitSet(f11) || checkMultipleBitSet(f12) || checkMultipleBitSet(f21) || checkMultipleBitSet(f22); if (isFuzzy) { flags |= REL_FLAGS_INTERVAL_FUZZY;
/** * Checks whether the point p is contained inside this interval. * * @param p point to check * @return True if the point p is contained withing the interval, false otherwise */ public boolean contains(E p) { // Check that the start point is before p boolean check1 = (includesBegin())? (first.compareTo(p) <= 0):(first.compareTo(p) < 0); // Check that the end point is after p boolean check2 = (includesEnd())? (second.compareTo(p) >= 0):(second.compareTo(p) > 0); return (check1 && check2); }
E leftMax = (node.left != null)? node.left.maxEnd:null; E rightMax = (node.right != null)? node.right.maxEnd:null; E maxEnd = node.value.getInterval().getEnd(); if (leftMax != null && leftMax.compareTo(maxEnd) > 0) { maxEnd = leftMax; if (node.value.getInterval().compareTo(n.parent.value.getInterval()) > 0) { throw new IllegalStateException("node is not on the correct side!!!"); if (node.value.getInterval().compareTo(n.parent.value.getInterval()) <= 0) { throw new IllegalStateException("node is not on the correct side!!!");
node.maxEnd = Interval.max(node.left.maxEnd, node.right.maxEnd); TreeNode<E,T> origRight = node.right; node.right = node.left.right; if (target.getInterval().compareTo(node.value.getInterval()) <= 0) { node.maxEnd = Interval.max(node.maxEnd, node.left.maxEnd); node.size--; node.maxEnd = Interval.max(node.maxEnd, node.right.maxEnd); node.size--;
@Override public int compare(MatchedExpression e1, MatchedExpression e2) { Interval.RelType rel = e1.tokenOffsets.getRelation(e2.tokenOffsets); if (rel.equals(Interval.RelType.CONTAIN)) { return 1; } else if (rel.equals(Interval.RelType.INSIDE)) { return -1; } else { return (e1.tokenOffsets.compareTo(e2.tokenOffsets)); } } };
private void adjustUpwards(TreeNode<E,T> node, TreeNode<E,T> stopAt) { TreeNode<E,T> n = node; while (n != null && n != stopAt) { int leftSize = (n.left != null)? n.left.size:0; int rightSize = (n.right != null)? n.right.size:0; n.maxEnd = n.value.getInterval().getEnd(); if (n.left != null) { n.maxEnd = Interval.max(n.maxEnd, n.left.maxEnd); } if (n.right != null) { n.maxEnd = Interval.max(n.maxEnd, n.right.maxEnd); } n.size = leftSize + 1 + rightSize; if (n == n.parent) { throw new IllegalStateException("node is same as parent!!!"); } n = n.parent; } }
@Override public int compare(MatchedExpression e1, MatchedExpression e2) { return (e1.tokenOffsets.compareTo(e2.tokenOffsets)); } };
if (target.second.compareTo(n.value.getInterval().first()) <= 0) { continue;
public boolean contains(Interval<E> other) { boolean containsOtherBegin = (other.includesBegin())? contains(other.getBegin()): containsOpen(other.getBegin()); boolean containsOtherEnd = (other.includesEnd())? contains(other.getEnd()): containsOpen(other.getEnd()); return (containsOtherBegin && containsOtherEnd); }