-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfibbonaci_counter.cppm
More file actions
57 lines (45 loc) · 1.42 KB
/
Copy pathfibbonaci_counter.cppm
File metadata and controls
57 lines (45 loc) · 1.42 KB
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
export module fibbonaci_counter;
import <algorithm>;
import <cstddef>;
import <cstdint>;
import <functional>;
import <vector>;
import counter;
export class Fibbonaci_counter : public Counter {
private:
static constexpr std::vector<std::uint64_t> fibb_gen() {
std::uint64_t a = 1, b = 1;
std::vector<std::uint64_t> res = {b};
while (true) {
// check if a+b<=UINT64_MAX
if (UINT64_MAX - b >= a) {
a = a + b;
std::swap(a, b);
res.push_back(b);
} else {
return res;
}
}
}
const inline static std::vector<std::uint64_t> fibbonaci{fibb_gen()};
std::size_t next_fib = 0; // next fibbonaci index.
virtual void perform_inner_ticks(std::uint64_t ticks,
const std::function<void(std::uint64_t)>
&report_ov) noexcept override final {
std::uint64_t initial_ticks = ticks;
while (next_fib != fibbonaci.size() && ticks != 0) {
// add ticks up to next fibbonaci number.
std::uint64_t how_many =
std::min(ticks, fibbonaci[next_fib] - inner_value);
ticks -= how_many;
inner_value += how_many;
if (inner_value == fibbonaci[next_fib]) {
++next_fib;
report_ov(initial_ticks - ticks);
}
}
inner_value+=std::min(ticks,UINT64_MAX-inner_value);
}
public:
Fibbonaci_counter(std::uint64_t ignored2) noexcept : Counter(ignored2) {}
};