libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
windowedstat.h
Go to the documentation of this file.
1
8#pragma once
9
10
11#include <promeki/config.h>
12#if PROMEKI_ENABLE_CORE
13#include <functional>
14
15#include <promeki/function.h>
16#include <promeki/namespace.h>
17#include <promeki/datastream.h>
18#include <promeki/error.h>
19#include <promeki/list.h>
20#include <promeki/result.h>
21#include <promeki/string.h>
22
23PROMEKI_NAMESPACE_BEGIN
24
25class Variant;
26
64class WindowedStat {
65 public:
66 PROMEKI_DATATYPE(WindowedStat, DataTypeWindowedStat, 1)
67
68
69 using Samples = ::promeki::List<double>;
70
82 struct Stats {
84 int count = 0;
86 int capacity = 0;
88 double min = 0.0;
90 double max = 0.0;
92 double sum = 0.0;
94 double average = 0.0;
96 double stddev = 0.0;
97 };
98
110 using ValueFormatter = Function<String(double)>;
111
113 WindowedStat() = default;
114
122 explicit WindowedStat(int capacity);
123
125 int capacity() const { return _capacity; }
126
136 void setCapacity(int capacity);
137
139 int count() const;
140
142 bool isEmpty() const { return count() == 0; }
143
145 bool isFull() const { return _full; }
146
152 void push(double value);
153
179 bool push(const Variant &v);
180
182 void clear();
183
184 // ------------------------------------------------------------
185 // Statistics
186 // ------------------------------------------------------------
187
200 Stats stats() const;
201
203 double min() const;
204
206 double max() const;
207
209 double average() const;
210
212 double sum() const;
213
223 double stddev() const;
224
226 int sampleCount() const { return count(); }
227
235 Samples values() const;
236
237 // ------------------------------------------------------------
238 // String / Variant round-trip
239 // ------------------------------------------------------------
240
262 String toFormattedString(const ValueFormatter &formatter = ValueFormatter()) const;
263
277 String toString() const;
278
290 static Result<WindowedStat> fromString(const String &s);
291
293 bool operator==(const WindowedStat &other) const;
294
296 bool operator!=(const WindowedStat &other) const { return !(*this == other); }
297
309 Error writeToStream(DataStream &s) const;
310
315 template <uint32_t V> static Result<WindowedStat> readFromStream(DataStream &s);
316
317 private:
318 Samples _samples;
319 int _capacity = 0;
320 int _head = 0;
321 bool _full = false;
322};
323
324PROMEKI_NAMESPACE_END
325
326#endif // PROMEKI_ENABLE_CORE