Return a new Interval with the intersection of the two intervals,
        i.e.  all elements that are in both self and other.

        :param Interval other: Interval to intersect with
        :rtype: Interval
493534
def intersection(self,other):
        
        if self.bounds[0] < other.bounds[0]:
            i1,i2 = self,other
        else:
            i2,i1 = self,other

        if self.is_disjoint(other):
            return Interval((1,0),(True,True))

        bounds = [None,None]
        included = [None,None]
        #sets are not disjoint, so i2.bounds[0] in i1:
        bounds[0] = i2.bounds[0]
        included[0] = i2.included[0]

        if i2.bounds[1] in i1:
            bounds[1] = i2.bounds[1]
            included[1] = i2.included[1]
        else:
            bounds[1] = i1.bounds[1]
            included[1] = i1.included[1]

        return Interval(bounds,included)
1204393
def difference(self, other):
        
        #NOTE: This is a work around
        # Python 3 return as the result of set.intersection a new set instance.
        # Python 2 however returns as a the result a ClusterShell.RangeSet.RangeSet instance.
        # ORIGINAL CODE: return self._wrap_set_op(set.difference, other)
        copy = self.copy()
        copy.difference_update(other)
        return copy
1087380
def union(self, other):
        
        return Interval(min(self.low, other.low), max(self.high, other.high))
856915
def intersection(self, other):
    

    # we only deal with the case of two lists here
    length1 = len(self)
    length2 = len(other)

    # initialize list of output segments
    ostart = -1
    outlist = []
    iseg2 = -1
    start2 = -1
    stop2 = -1

    for seg1 in self:
      start1 = seg1.start()
      stop1 = seg1.end()
      id = seg1.id()

      # loop over segments from the second list which overlap this segment
      while start2 < stop1:
        if stop2 > start1:
          # these overlap

          # find the overlapping range
          if start1 < start2:
            ostart = start2
          else:
            ostart = start1
          if stop1 > stop2:
            ostop = stop2
          else:
            ostop = stop1

          x = ScienceSegment(tuple([id, ostart, ostop, ostop-ostart]))
          outlist.append(x)

          if stop2 > stop1:
            break

        # step forward
        iseg2 += 1
        if iseg2 < len(other):
          seg2 = other[iseg2]
          start2 = seg2.start()
          stop2 = seg2.end()
        else:
          # pseudo-segment in the far future
          start2 = 2000000000
          stop2 = 2000000000

    # save the intersection and return the length
    self.__sci_segs = outlist
    return len(self)
359350
def intersection(other)
      if other.is_a?(RelativeComplement)
        # A ∩ ¬B = ¬B ∩ A
        other.intersection(self)
      elsif other.is_a?(AbstractSet)
        if other.is_a?(RelativeSet) and size > other.size
          # For efficiency, iterate the smaller of the two sets: A ∩ B = B ∩ A
          other.intersection(self)
        elsif other.empty?
          # A ∩ ∅ = ∅
          NullSet.build
        else
          hash = @hash.clone.delete_if{|o,_| not other.include?(o) }

          if hash.empty?
            NullSet.build
          else
            RelativeSet.new(hash)
          end
        end
      else
        intersection(Cantor.build(other))
      end
    end
1296061
private int compareStarts(Interval<T> other){
		if (start == null && other.start == null)
			return 0;
		if (start == null)
			return -1;
		if (other.start == null)
			return 1;
		int compare = start.compareTo(other.start);
		if (compare != 0)
			return compare;
		if (isStartInclusive ^ other.isStartInclusive)
			return isStartInclusive ? -1 : 1;
		return 0;
	}
1244564
def next_interval(self, interval):
        
        index = np.where(self.intervals == interval)
        if index[0][0] + 1 < len(self.intervals):
            return self.intervals[index[0][0] + 1]
        else:
            raise IndexError("Ran out of intervals!")
1116625
def gap_subset(self, interval: Interval,
                   flexibility: int = 2) -> "IntervalList":
        
        return self.gaps().subset(interval, flexibility)
441296
public Range intersectionWithTouch(Range other) {
        if (!intersectsWithOrTouches(other))
            return null;

        return new Range(Math.max(lower, other.lower), Math.min(upper, other.upper), reversed && other.reversed);
    }
989921
def _bigger(interval1, interval2):
        
        if interval2.cardinality > interval1.cardinality:
            return interval2.copy()
        return interval1.copy()
882799
def is_contradictory(self, other):
        
        other = IntervalCell.coerce(other)
        assert other.low <= other.high, "Low must be <= high"
        if max(other.low, self.low) <= min(other.high, self.high):
            return False
        else:
            return True
31251
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);
  }
1211987
def intersection(other)
      if other.is_a?(RelativeComplement)
        # ¬A ∩ ¬B = ¬(A ∪ B)
        complement.union(other.complement).complement
      else
        # ¬A ∩ B = B ∖ A
        Cantor.build(other).difference(complement)
      end
    end
496115
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;
    }
  }
1128455
def get_overlaps(self) -> "IntervalList":
        
        overlaps = IntervalList()
        for i in range(len(self.intervals)):
            for j in range(i + 1, len(self.intervals)):
                first = self.intervals[i]
                second = self.intervals[j]
                ol = first.intersection(second)
                if ol is not None:
                    overlaps.add(ol)
        return overlaps
605565
def intersection(self, other):
        
        #NOTE: This is a work around 
        # Python 3 return as the result of set.intersection a new set instance.
        # Python 2 however returns as a the result a ClusterShell.RangeSet.RangeSet instance.
        # ORIGINAL CODE: return self._wrap_set_op(set.intersection, other)
        copy = self.copy()
        copy.intersection_update(other)
        return copy
245256
def interval_overlap(a, b, x, y):
    """"""
    if b <= x or a >= y:
        return 0
    elif x <= a <= y:
        return min(b, y) - a
    elif x <= b <= y:
        return b - max(a, x)
    elif a >= x and b <= y:
        return b - a
    else:
        assert False