More docs.
[pyutils.git] / src / pyutils / collectionz / interval_tree.py
index 92c975d35a277f8a2f266c2ce221b3401e8a7c60..aee838585b098887e7b3202ca48478453978311e 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
@@ -73,9 +85,16 @@ class AugmentedIntervalTree(bst.BinarySearchTree):
                 new_highest_candidates.append(parent.right.value.highest_in_subtree)
             parent.value.highest_in_subtree = max(new_highest_candidates)
 
-    def find_one_overlap(self, x: NumericRange):
+    def find_one_overlap(self, to_find: NumericRange) -> Optional[NumericRange]:
         """Identify and return one overlapping node from the tree.
 
+        Args:
+            to_find: the interval with which to find an overlap.
+
+        Returns:
+            An overlapping range from the tree or None if no such
+            ranges exist in the tree at present.
+
         >>> tree = AugmentedIntervalTree()
         >>> tree.insert(NumericRange(20, 24))
         >>> tree.insert(NumericRange(18, 22))
@@ -90,8 +109,9 @@ class AugmentedIntervalTree(bst.BinarySearchTree):
         >>> tree.insert(NumericRange(21, 27))
         >>> tree.find_one_overlap(NumericRange(6, 7))
         1..30
+
         """
-        return self._find_one_overlap(self.root, x)
+        return self._find_one_overlap(self.root, to_find)
 
     def _find_one_overlap(
         self, root: bst.Node, x: NumericRange
@@ -110,8 +130,18 @@ class AugmentedIntervalTree(bst.BinarySearchTree):
             return self._find_one_overlap(root.right, x)
         return None
 
-    def find_all_overlaps(self, x: NumericRange):
-        """Yields ranges previously added to the tree that x overlaps with.
+    def find_all_overlaps(
+        self, to_find: NumericRange
+    ) -> Generator[NumericRange, None, None]:
+        """Yields ranges previously added to the tree that overlaps with
+        to_find argument.
+
+        Args:
+            to_find: the interval with which to find all overlaps.
+
+        Returns:
+            A (potentially empty) sequence of all ranges in the tree
+            that overlap with the argument.
 
         >>> tree = AugmentedIntervalTree()
         >>> tree.insert(NumericRange(20, 24))
@@ -140,10 +170,11 @@ class AugmentedIntervalTree(bst.BinarySearchTree):
         18..22
         16..28
         21..27
+
         """
         if self.root is None:
             return
-        yield from self._find_all_overlaps(self.root, x)
+        yield from self._find_all_overlaps(self.root, to_find)
 
     def _find_all_overlaps(
         self, root: bst.Node, x: NumericRange