Tiebreak ordering of ranges with the same lower bound using upper bound.
[pyutils.git] / src / pyutils / collectionz / interval_tree.py
index d4b33cbe62dea02385b1381924429ff2ebb613f7..a8278a2dc8ea835a501951e3abddb9727d405930 100644 (file)
@@ -22,6 +22,18 @@ class NumericRange(object):
     helper methods on it."""
 
     def __init__(self, low: Numeric, high: Numeric):
+        """Creates a NumericRange.
+
+        Args:
+            low: the lowest point in the range (inclusive).
+            high: the highest point in the range (inclusive).
+
+        .. warning::
+
+            If low > high this code swaps the parameters and keeps the range
+            rather than raising.
+
+        """
         if low > high:
             temp: Numeric = low
             low = high
@@ -31,15 +43,30 @@ class NumericRange(object):
         self.highest_in_subtree: Numeric = high
 
     def __lt__(self, other: NumericRange) -> bool:
-        return self.low < other.low
+        """
+        Returns:
+            True is this range is less than (lower low) other, else False.
+        """
+        if self.low != other.low:
+            return self.low < other.low
+        else:
+            return self.high < other.high
 
     @overrides
     def __eq__(self, other: object) -> bool:
+        """
+        Returns:
+            True if this is the same range as other, else False.
+        """
         if not isinstance(other, NumericRange):
             return False
         return self.low == other.low and self.high == other.high
 
     def overlaps_with(self, other: NumericRange) -> bool:
+        """
+        Returns:
+            True if this NumericRange overlaps with other, else False.
+        """
         return self.low <= other.high and self.high >= other.low
 
     def __repr__(self) -> str: