From 1b777d58491555c679957edec3762a8826af890b Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Fri, 5 Dec 2025 07:44:48 +0100 Subject: [PATCH] types.Range() --- src/tools/types.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/tools/types.py b/src/tools/types.py index 6fa50a5..877b0f5 100644 --- a/src/tools/types.py +++ b/src/tools/types.py @@ -59,3 +59,32 @@ class List(list): this_window.append(self[i * size + j]) yield this_window + + +class Range: + def __init__(self, start: int, end: int) -> None: + self.start = start + self.end = end + + def overlaps(self, other: Range) -> bool: + return other.start <= self.start <= other.end or other.start <= self.end <= other.end or (other.start <= self.start and other.end >= self.end) or (self.start <= other.start and self.end >= other.end) + + def merge(self, other: Range) -> Range: + if not self.overlaps(other): + raise ValueError("Ranges do not overlap") + + return Range(min(self.start, other.start), max(self.end, other.end)) + + def __contains__(self, item: int) -> bool: + return self.start <= item <= self.end + + def __len__(self) -> int: + return self.end - self.start + 1 + + def __iter__(self) -> Iterable[int]: + for x in range(self.start, self.end + 1): + yield x + + def __reversed__(self) -> Iterable[int]: + for x in range(self.end, self.start - 1, -1): + yield x \ No newline at end of file