Unify Range and RangeInclusive

This commit is contained in:
Luuk Machielse 2025-12-09 17:02:32 +01:00
parent ff1d6b8734
commit 8df72a0c79

View file

@ -19,11 +19,11 @@ pub fn iter(comptime T: type) type {
try expectEqual(null, it.next()); try expectEqual(null, it.next());
} }
pub fn range(start: T, end: T) Iterator(Range(T)) { pub fn range(start: T, end: T) Iterator(Range(T, .exclusive)) {
return .{ .inner = .{ .current = start, .end = end } }; return .{ .inner = .{ .current = start, .end = end } };
} }
pub fn rangeInclusive(start: T, end: T) Iterator(RangeInclusive(T)) { pub fn rangeInclusive(start: T, end: T) Iterator(Range(T, .inclusive)) {
return .{ .inner = .{ .current = start, .end = end } }; return .{ .inner = .{ .current = start, .end = end } };
} }
@ -344,7 +344,9 @@ pub const adapters = struct {
} }
}; };
pub fn Range(comptime T: type) type { pub const RangeEndType = enum { exclusive, inclusive };
pub fn Range(comptime T: type, end_type: RangeEndType) type {
return struct { return struct {
current: T, current: T,
end: T, end: T,
@ -354,24 +356,9 @@ pub fn Range(comptime T: type) type {
const Self = @This(); const Self = @This();
pub fn next(self: *Self) ?Item { pub fn next(self: *Self) ?Item {
if (self.current >= self.end) return null; if (self.current > self.end or
defer self.current += 1; (end_type == .exclusive and self.current == self.end))
return self.current; return null;
}
};
}
pub fn RangeInclusive(comptime T: type) type {
return struct {
current: T,
end: T,
pub const Item = T;
const Self = @This();
pub fn next(self: *Self) ?Item {
if (self.current > self.end) return null;
defer self.current += 1; defer self.current += 1;
return self.current; return self.current;
} }