Mojo struct
Interval
struct Interval[T: IntervalElement]
A half-open interval [start, end) that represents a range of values.
The interval includes the start value but excludes the end value.
Parameters
- T (
IntervalElement
): The type of the interval bounds.
Fields
- start (
T
): The inclusive start of the interval. - end (
T
): The exclusive end of the interval.
Implemented traits
AnyType
,
Boolable
,
CollectionElement
,
Copyable
,
Movable
,
UnknownDestructibility
,
Writable
Methods
__init__
__init__(out self, start: T, end: T)
Initialize an interval with start and end values.
Args:
- start (
T
): The starting value of the interval. - end (
T
): The ending value of the interval. Must be greater than or equal to start.
__init__(out self, interval: Tuple[T, T], /)
Initialize an interval with a tuple of start and end values.
Args:
- interval (
Tuple[T, T]
): A tuple containing the start and end values.
__copyinit__
__copyinit__(out self, existing: Self, /)
Create a new instance of the interval by copying the values from an existing one.
Args:
- existing (
Self
): The interval to copy values from.
__moveinit__
__moveinit__(out self, owned existing: Self, /)
Create a new instance of the interval by moving the values from an existing one.
Args:
- existing (
Self
): The interval to move values from.
__bool__
__bool__(self) -> Bool
Returns whether this interval is empty.
Returns:
True if the interval is not empty (start < end), False otherwise.
__lt__
__lt__(self, other: Self) -> Bool
Returns whether this interval is less than another interval.
Args:
- other (
Self
): The interval to compare with.
Returns:
True if this interval's start is less than the other interval's start.
__le__
__le__(self, other: Self) -> Bool
Returns whether this interval is less than or equal to another interval.
Args:
- other (
Self
): The interval to compare with.
Returns:
True if this interval's start is less than or equal to the other interval's start.
__eq__
__eq__(self, other: Self) -> Bool
Returns whether this interval equals another interval.
Args:
- other (
Self
): The interval to compare with.
Returns:
True if both intervals have the same start and end values.
__ne__
__ne__(self, other: Self) -> Bool
Returns whether this interval is not equal to another interval.
Args:
- other (
Self
): The interval to compare with.
Returns:
True if the intervals are not equal, False if they are equal.
__gt__
__gt__(self, other: Self) -> Bool
Returns whether this interval is greater than another interval.
Args:
- other (
Self
): The interval to compare with.
Returns:
True if this interval's end is greater than the other interval's end.
__ge__
__ge__(self, other: Self) -> Bool
Returns whether this interval is greater than or equal to another interval.
Args:
- other (
Self
): The interval to compare with.
Returns:
True if this interval's end is greater than or equal to the other interval's end.
__contains__
__contains__(self, other: T) -> Bool
Returns whether a value is contained within this interval.
Args:
- other (
T
): The value to check.
Returns:
True if the value is within the interval bounds, False otherwise.
__contains__(self, other: Self) -> Bool
Returns whether another interval is fully contained within this interval.
Args:
- other (
Self
): The interval to check.
Returns:
True if the other interval is fully contained within this interval, False otherwise.
overlaps
overlaps(self, other: Self) -> Bool
Returns whether this interval overlaps with another interval.
Args:
- other (
Self
): The interval to check for overlap with.
Returns:
True if the intervals overlap, False otherwise.
union
union(self, other: Self) -> Self
Returns the union of this interval and another interval.
Args:
- other (
Self
): The interval to union with.
Returns:
The union of this interval and the other interval.
intersection
intersection(self, other: Self) -> Self
Returns the intersection of this interval and another interval.
Args:
- other (
Self
): The interval to intersect with.
Returns:
The intersection of this interval and the other interval.
__len__
__len__(self) -> Int
Returns the length of this interval.
Returns:
The difference between end and start values as an integer.
write_to
write_to[W: Writer](self, mut writer: W)
Writes this interval to a writer in the format '(start, end)'.
Parameters:
- W (
Writer
): The writer type that implements the Writer trait.
Args:
- writer (
W
): The writer to write the interval to.
__str__
__str__(self) -> String
Returns a string representation of this interval.
Returns:
A string in the format '(start, end)' representing this interval.
__repr__
__repr__(self) -> String
Returns a string representation of this interval suitable for debugging.
Returns:
A string in the format '(start, end)' representing this interval.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!