-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcobtmrleanmill.hpp
167 lines (145 loc) · 4.76 KB
/
cobtmrleanmill.hpp
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* Compile time bound timer lean mill. For documentation, see README.md.
* (C) Srdjan Veljkovic
* License: MIT (see LICENSE)
*/
#if !defined(INC_COBTMRLEANMILL)
#define INC_COBTMRLEANMILL
#include "cobarray.hpp"
#include "cobmatrix.hpp"
template <class ID, int N, class U, class NULIFY, unsigned LVL = 5, unsigned DIM = 64>
class cobtmrleanmill {
static_assert(N > 0, "Mill must have some timers");
static_assert(LVL * DIM > 0, "Mill must have some spokes");
static_assert(LVL * DIM < N, "Too many spokes");
static constexpr auto spokedim = N / (LVL * DIM) + 1;
using level = cobint<0, LVL - 1>;
using lvlindex = cobint<0, DIM - 1>;
using spoke = cobarray<ID, spokedim>;
using spokepos = cobint<0, spokedim>;
spoke timers[LVL][DIM];
cobmatrix<unsigned, LVL, DIM> active;
cobarray<lvlindex, LVL> next;
public:
using tmrID = ID;
cobtmrleanmill()
{
active.fill(0);
next.fill(cobic<0>);
}
static constexpr U max_duration{ 1 << (DIM * cobhlp::log2(LVL)) };
struct index {
level lvl;
lvlindex ispoke;
spokepos idx;
constexpr bool valid() const { return idx != idx.greatest(); }
};
constexpr index spoke_from_duration(U duration)
{
level lvl;
unsigned d = duration.count();
do {
lvlindex idx;
if (idx.be(d)) {
d = (d + next.get(lvl).get()) % (idx.greatest().get() + 1);
idx.be(d);
return { lvl, idx, cobic<0> };
}
d = d / DIM;
} while (lvl.advance());
return { lvl, cobic<0>, spokepos::greatest() };
}
constexpr index start(ID id, U duration)
{
index i = spoke_from_duration(duration);
if (i.valid()) {
auto& spoke = timers[i.lvl.get()][i.ispoke.get()];
auto pos = active.get({ i.lvl, i.ispoke });
if (spoke.maybe_set(pos, id)) {
active.set({ i.lvl, i.ispoke }, pos + 1);
}
}
return i;
}
int stop(ID id)
{
level lvl;
do {
lvlindex ispoke;
do {
for (unsigned i = 0; i < active.get({ lvl, ispoke }); ++i) {
NULIFY nlf;
auto p = timers[lvl.get()][ispoke.get()].begin() + i;
if (*p == id) {
nlf(*p);
return 0;
}
}
} while (ispoke.advance());
} while (lvl.advance());
return -1;
}
void stop(index it)
{
if (it.valid()) {
NULIFY nlf;
auto p = timers[it.lvl.get()][it.ispoke.get()].begin() + it.idx.get();
nlf(*p);
}
}
template <template <class> class V> constexpr V<ID> stop_first()
{
level lvl;
do {
auto ispoke = next.get(lvl);
do {
if (active.get({ lvl, ispoke }) > 0) {
NULIFY nlf;
auto p = timers[lvl.get()][ispoke.get()].begin();
ID rslt = *p;
nlf(*p);
return rslt;
}
if (!ispoke.advance()) {
auto next_lvl = lvl;
if (next_lvl.advance()) {
auto nxt_lvl_idx = next.get(next_lvl);
if (active.get({ next_lvl, nxt_lvl_idx }) > 0) {
auto& nxtlst =
timers[next_lvl.get()][nxt_lvl_idx.get()];
NULIFY nlf;
auto p = nxtlst.begin();
ID rslt = *p;
if (nlf(*p)) {
return rslt;
}
}
}
ispoke = cobic<0>;
}
} while (ispoke != next.get(lvl));
} while (lvl.advance());
return {};
}
template <class F> void process_expired(U elapsed, F f)
{
for (U i{ 0 }; i < elapsed; ++i) {
level lvl;
do {
auto ispoke = next.get(lvl);
auto p = timers[lvl.get()][ispoke.get()].begin();
for (unsigned j = 0; j < active.get({ lvl, ispoke }); ++j) {
f(*p);
++p;
}
active.set({ lvl, ispoke }, 0);
if (ispoke.advance()) {
next.set(lvl, ispoke);
break;
}
next.set(lvl, cobic<0>);
} while (lvl.advance());
}
}
// std::optional<ID> expired(U proteklo) {}
};
#endif // define INC_COBTMRLEANWHEEL