mirror of
https://codeberg.org/kaasboteram/zig-iterating.git
synced 2025-12-15 09:43:14 +01:00
Unify Range and RangeInclusive
This commit is contained in:
parent
ff1d6b8734
commit
8df72a0c79
1 changed files with 8 additions and 21 deletions
29
src/root.zig
29
src/root.zig
|
|
@ -19,11 +19,11 @@ pub fn iter(comptime T: type) type {
|
|||
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 } };
|
||||
}
|
||||
|
||||
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 } };
|
||||
}
|
||||
|
||||
|
|
@ -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 {
|
||||
current: T,
|
||||
end: T,
|
||||
|
|
@ -354,24 +356,9 @@ pub fn Range(comptime T: type) type {
|
|||
const Self = @This();
|
||||
|
||||
pub fn next(self: *Self) ?Item {
|
||||
if (self.current >= self.end) return null;
|
||||
defer self.current += 1;
|
||||
return self.current;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
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;
|
||||
if (self.current > self.end or
|
||||
(end_type == .exclusive and self.current == self.end))
|
||||
return null;
|
||||
defer self.current += 1;
|
||||
return self.current;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue