A module for representing bounded ranges.
>>> from graphtage import bounds >>> p = bounds.Infinity(positive=True) >>> str(p) '∞' >>> str(p + p) '∞' >>> bounds.Range(0, p) Range(0, Infinity(positive=True)) >>> bounds.Range(0, 10) < bounds.Range(20, 30) True
This module provides a variety of data structures and algorithms for both representing bounds as well as operating on bounded ranges (e.g., sorting).
- class graphtage.bounds.Bounded(*args, **kwargs)¶
A protocol for objects that have bounds that can be tightened.
- __init__(*args, **kwargs)¶
- class graphtage.bounds.BoundedComparator(bounded: Bounded)¶
A comparator for
This comparator will automatically tighten the bounds of the
Boundedobject it wraps until they are either definitive or sufficiently distinct to differentiate them from another object to which it is being compared.
- __init__(bounded: Bounded)¶
Initializes this bounded comparator.
bounded – The object to wrap.
Compares the wrapped object to
other, auto-tightening their bounds if necessary.
The auto-tightening is equivalent to:
while not ( self.bounded.bounds().dominates(other.bounded.bounds()) or other.bounded.bounds().dominates(self.bounded.bounds()) ) and ( self.bounded.tighten_bounds() or other.bounded.tighten_bounds() ): pass
In the event that
otherhave identical bounds after fully tightening, the object with the smaller
The wrapped bounded object.
- class graphtage.bounds.ConstantBound(value: int | Infinity)¶
An object with constant bounds.
- __init__(value: int | Infinity)¶
Initializes the constant bounded object.
value – The constant value of the object, which will constitute both its lower and upper bound.
- bounds() Range ¶
Rangewhere both the lower and upper bounds are equal to this object’s constant value.
- class graphtage.bounds.Range(lower_bound: int | Infinity = Infinity(positive=False), upper_bound: int | Infinity = Infinity(positive=True))¶
An integer range.
- __init__(lower_bound: int | Infinity = Infinity(positive=False), upper_bound: int | Infinity = Infinity(positive=True))¶
Constructs a range.
lower_bound – The lower bound of the range (inclusive).
upper_bound – The upper bound of the range (inclusive).
ValueError – If the upper bound is less than the lower bound.
- definitive() bool ¶
Checks whether this range is definitive.
A range is definitive if both of its bounds are finite and equal to each other.
- dominates(other) bool ¶
Checks whether this range dominates another.
One range dominates another if its upper bound is less than or equal to the lower bound of the other.
This is equivalent to:
return self.upper_bound <= other.lower_bound
- property finite: bool¶
Returns whether this range is finite.
A range is finite if neither of its bounds is infinite.
- to_interval() Interval ¶
Converts this range to an
intervaltree.Intervalfor use with the Interval Tree package.
- graphtage.bounds.sort(items: Iterable[B]) Iterator[B] ¶
Sorts a sequence of bounded items.
items – Zero or more bounded objects.
An iterator over the sorted sequence of items.
- Return type:
This is equivalent to:
heap: FibonacciHeap[B, BoundedComparator] = FibonacciHeap(key=BoundedComparator) for item in items: heap.push(item) while heap: yield heap.pop()