Void Sentry
An AI-driven, real-time Sentry Turret platform leveraging asynchronous I/O and computer vision to deliver high-precision autonomous motion tracking on resource-constrained embedded hardware.
Loading...
Searching...
No Matches
windowed_counter.h
Go to the documentation of this file.
1
10#pragma once
11
12#include <cstdint>
13#include <type_traits>
14
15namespace stats
16{
18template <typename T> class WindowedCounter
19{
20 static_assert(std::is_unsigned_v<T>, "wrap-safe deltas need unsigned arithmetic");
21
22public:
24 T sample(T total)
25 {
26 const T delta = static_cast<T>(total - prev_);
27 prev_ = total;
28 return delta;
29 }
30
31private:
32 T prev_ = 0;
33};
34
36template <typename T> constexpr T window_avg(T sum, T count) { return count ? static_cast<T>(sum / count) : T{0}; }
37
39constexpr unsigned window_percent(uint64_t part, uint64_t whole)
40{
41 return whole ? static_cast<unsigned>(part * 100 / whole) : 0u;
42}
43} // namespace stats
Remembers the previous reading of a free-running unsigned counter; sample() returns the delta since.
Definition windowed_counter.h:19
T sample(T total)
Record total as the new reading and return how much it grew since the previous call (0 first).
Definition windowed_counter.h:24
constexpr unsigned window_percent(uint64_t part, uint64_t whole)
100 * part / whole as an integer percent, or 0 when whole is 0 (an empty window).
Definition windowed_counter.h:39
constexpr T window_avg(T sum, T count)
sum / count, or 0 when count is 0 (an empty window).
Definition windowed_counter.h:36