-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcobtmrwheel.hpp
96 lines (78 loc) · 2.15 KB
/
cobtmrwheel.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
/* Compile time bound timer wheel. For documentation, see README.md.
* (C) Srdjan Veljkovic
* License: MIT (see LICENSE)
*/
#if !defined(INC_COBTMRWHEEL)
#define INC_COBTMRWHEEL
#include "coblist.hpp"
template <class ID, int N, class U, unsigned SPOKES> class cobtmrwheel {
static_assert(N > 0, "List must have some capacity");
static_assert(SPOKES > 1, "Must have more than one spoke");
struct elem {
ID id;
U remaining;
elem(ID id_)
: id(id_)
{
}
elem(ID id_, U remain)
: id(id_)
, remaining(remain)
{
}
bool operator==(elem const& x) { return id == x.id; }
};
using list = cobtmrlist<ID, N / SPOKES * 2, U>;
using ispoke = cobint<0, SPOKES - 1>;
list timers[SPOKES];
ispoke next;
public:
using tmrID = ID;
struct index {
ispoke spoke;
typename list::index idx;
};
index start(ID id, U duration)
{
unsigned i = duration.count() % SPOKES;
ispoke spoke;
spoke.be(i);
return { spoke, timers[spoke.get()].start(id, duration) };
}
int stop(ID id)
{
for (auto& l : timers) {
if (l.stop(id) == 0) {
return 0;
}
}
return -1;
}
int stop(index it) { return timers[it.spoke.get()].stop(it.idx); }
template <template <class> class V> constexpr V<ID> stop_first()
{
auto it = next;
do {
auto& spoke = timers[it.get()];
auto rslt = spoke.template stop_first<V>();
if (rslt) {
return rslt;
}
if (!it.advance()) {
it = cobic<0>;
}
} while (it != next);
return {};
}
template <class F> void process_expired(U elapsed, F f)
{
for (unsigned i = 0; i < elapsed.count(); ++i) {
timers[next.get()].process_expired(U{ SPOKES }, f);
if (!next.advance()) {
next = cobic<0>;
}
}
}
// std::optional<ID> expired(U proteklo) {}
};
#endif // define INC_COBTMRWHEEL