-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcycle.zig
66 lines (55 loc) · 2.17 KB
/
cycle.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const zignite = @import("../zignite.zig");
const expect = @import("std").testing.expect;
const ProducerType = @import("../producer/producer_type.zig").ProducerType;
test "cycle" {
{
try expect(zignite.empty(i32).cycle().isEmpty());
}
{
const a = try zignite.range(i32, 1, 2).cycle().take(10).toBoundedArray(10);
try expect(a.get(0) == 1);
try expect(a.get(1) == 2);
try expect(a.get(2) == 1);
try expect(a.get(3) == 2);
try expect(a.get(4) == 1);
try expect(a.get(5) == 2);
try expect(a.get(6) == 1);
try expect(a.get(7) == 2);
try expect(a.get(8) == 1);
try expect(a.get(9) == 2);
// ...
}
}
pub fn Cycle(comptime S: type, comptime T: type, comptime pd_next: ProducerType(S, T).Next, comptime pd_deinit: ProducerType(S, T).Deinit) type {
return struct {
const Pd = ProducerType(S, T);
reset_state: Pd.State,
action: Pd.Action,
is_empty: bool,
pub const Type = ProducerType(@This(), T);
pub inline fn init(state: Pd.State) Type.State {
return _init(state, Pd.Action._continue(state), true);
}
pub fn next(event: Type.Event) Type.Action {
const PdA = Pd.Action;
const r = event.reset_state;
const a = event.action;
const i = event.is_empty;
const s = a.state;
return switch (a.tag) {
._continue => Type.Action._continue(_init(r, pd_n(s), i)),
._yield => |v| Type.Action._yield(_init(r, PdA._continue(s), false), v),
._break => if (i) Type.Action._break(event) else Type.Action._continue(_init(r, PdA._continue(r), i)),
};
}
pub fn deinit(state: Type.State) void {
defer pd_deinit(state.action.state);
}
inline fn _init(reset_state: Pd.State, action: Pd.Action, is_empty: bool) Type.State {
return .{ .reset_state = reset_state, .action = action, .is_empty = is_empty };
}
inline fn pd_n(event: Pd.Event) Pd.Action {
return @call(.{ .modifier = .always_inline }, pd_next, .{event});
}
};
}