IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: Nightly
For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).

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​

Fields​

  • ​start (T): The inclusive start of the interval.
  • ​end (T): The exclusive end of the interval.

Implemented traits​

AnyType, Boolable, Copyable, Deinitable, Equatable, ImplicitlyCopyable, Movable, Sized, Writable

Methods​

__init__​

def __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.

def __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.

def __init__(out self, *, copy: Self)

Create a new instance of the interval by copying the values from an existing one.

Args:

  • ​copy (Self): The interval to copy values from.

__bool__​

def __bool__(self) -> Bool

Returns whether this interval is empty.

Returns:

Bool: True if the interval is not empty (start < end), False otherwise.

__lt__​

def __lt__(self, other: Self) -> Bool

Returns whether this interval is less than another interval.

Args:

  • ​other (Self): The interval to compare with.

Returns:

Bool: True if this interval's start is less than the other interval's start.

__le__​

def __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:

Bool: True if this interval's start is less than or equal to the other interval's start.

__gt__​

def __gt__(self, other: Self) -> Bool

Returns whether this interval is greater than another interval.

Args:

  • ​other (Self): The interval to compare with.

Returns:

Bool: True if this interval's end is greater than the other interval's end.

__ge__​

def __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:

Bool: True if this interval's end is greater than or equal to the other interval's end.

__contains__​

def __contains__(self, other: T) -> Bool

Returns whether a value is contained within this interval.

Args:

  • ​other (T): The value to check.

Returns:

Bool: True if the value is within the interval bounds, False otherwise.

def __contains__(self, other: Self) -> Bool

Returns whether another interval is fully contained within this interval.

Args:

  • ​other (Self): The interval to check.

Returns:

Bool: True if the other interval is fully contained within this interval, False otherwise.

overlaps​

def overlaps(self, other: Self) -> Bool

Returns whether this interval overlaps with another interval.

Args:

  • ​other (Self): The interval to check for overlap with.

Returns:

Bool: True if the intervals overlap, False otherwise.

union​

def union(self, other: Self) -> Self

Returns the union of this interval and another interval.

Args:

  • ​other (Self): The interval to union with.

Returns:

Self: The union of this interval and the other interval.

intersection​

def intersection(self, other: Self) -> Self

Returns the intersection of this interval and another interval.

Args:

  • ​other (Self): The interval to intersect with.

Returns:

Self: The intersection of this interval and the other interval.

__len__​

def __len__(self) -> Int

Returns the length of this interval.

Returns:

Int: The difference between end and start values as an integer.

write_to​

def write_to(self, mut writer: T)

Writes this interval to a writer in the format '(start, end)'.

Args:

  • ​writer (T): The writer to write the interval to.