5 #ifndef HERALD_AGGREGATES_H
6 #define HERALD_AGGREGATES_H
17 namespace aggregates {
20 static constexpr
int runs = 1;
25 void beginRun(
int thisRun) {
29 template <
typename ValT>
30 void map(ValT value) {
50 static constexpr
int runs = 1;
52 Mean() : count(0), run(1), sum(0.0) {}
55 void beginRun(
int thisRun) {
59 template <
typename ValT>
60 void map(ValT value) {
83 static constexpr
int runs = 1;
85 Mode() : run(1), counts() {}
88 void beginRun(
int thisRun) {
92 template <
typename ValT>
93 void map(ValT value) {
96 double dv = (double)value;
97 auto ptr = counts.find(dv);
98 if (counts.end() == ptr) {
108 int largestCount = 0;
109 for (
auto iter = counts.begin();iter != counts.end();++iter) {
110 if (iter->second > largestCount) {
111 largestCount = iter->second;
112 largest = iter->first;
124 std::map<double,int> counts;
128 static constexpr
int runs = 2;
130 Variance() : count(0), run(1), sum(0.0), mean(0.0) {}
133 void beginRun(
int thisRun) {
144 template <
typename ValT>
145 void map(ValT value) {
146 double dv = (double)value;
151 sum += (dv - mean)*(dv - mean);
160 return sum / (count - 1);
179 static constexpr
int runs = 1;
181 Median() : run(1), minNextPos(0), maxNextPos(0), minHeap(), maxHeap() {}
184 void beginRun(
int thisRun) {
188 template <
typename ValT>
189 void map(ValT value) {
193 double dv = (double)value;
194 if (minNextPos == maxNextPos) {
195 if (10 == maxNextPos) {
196 removeLeast(maxHeap,maxNextPos);
198 maxHeap[maxNextPos++] = dv;
199 if (10 == minNextPos) {
200 removeMost(minHeap,minNextPos);
202 minHeap[minNextPos++] = maxHeap[leastIndex(maxHeap,maxNextPos)];
204 if (10 == minNextPos) {
205 removeMost(minHeap,minNextPos);
207 minHeap[minNextPos++] = dv;
208 if (10 == maxNextPos) {
209 removeLeast(maxHeap,maxNextPos);
211 maxHeap[maxNextPos++] = minHeap[mostIndex(minHeap,minNextPos)];
216 if (0 == minNextPos && 0 == maxNextPos) {
219 if (minNextPos > maxNextPos) {
220 return minHeap[leastIndex(minHeap,minNextPos)];
222 return (minHeap[leastIndex(minHeap,minNextPos)] + maxHeap[mostIndex(maxHeap,maxNextPos)]) / 2.0;
236 std::array<double,10> minHeap;
237 std::array<double,10> maxHeap;
239 int leastIndex(
const std::array<double,10>& from,
const int fromNextPos)
const {
241 double least = from[0];
242 for (
int i = 1;i < fromNextPos;++i) {
243 if (from[i] < least) {
251 int mostIndex(
const std::array<double,10>& from,
const int fromNextPos)
const {
253 double most = from[0];
254 for (
int i = 1;i < fromNextPos;++i) {
255 if (from[i] > most) {
263 void removeMost(std::array<double,10>& from,
int& fromNextPos) {
264 int mostIdx = mostIndex(from,fromNextPos);
265 for (
int i = mostIdx; i < fromNextPos - 1;++i) {
266 from[i] = from[i + 1];
271 void removeLeast(std::array<double,10>& from,
int& fromNextPos) {
272 int leastIdx = leastIndex(from,fromNextPos);
273 for (
int i = leastIdx; i < fromNextPos - 1;++i) {
274 from[i] = from[i + 1];
283 static constexpr
int runs = 1;
285 Gaussian() : run(1), distribution() {}
288 void beginRun(
int thisRun) {
292 template <
typename ValT>
293 void map(ValT value) {
297 double dv = (double)value;
298 distribution.
add(dv);
302 return distribution.
mean();
307 distribution.
reset();
325 template <
typename... Aggs>
327 aggregate(Aggs... configuredAggregates) : aggregates() {
328 addAggregate<Aggs...>(configuredAggregates...);
332 template <
typename SampleListT,
333 typename SampleT =
typename std::remove_cv<typename SampleListT::value_type>::type,
334 typename ValT =
typename std::remove_cv<typename SampleT::value_type>::type,
335 std::size_t MaxSize = SampleListT::max_size
340 for (
auto& agg : me.aggregates) {
341 std::visit([&maxRuns](
auto&& arg) {
342 if (arg.runs > maxRuns) {
349 for (
int run = 1;run <= maxRuns;run++) {
350 for (
auto& agg : me.aggregates) {
351 std::visit([&run](
auto&& arg) {
357 for (
auto& v : from) {
358 for (
auto& agg : me.aggregates) {
359 std::visit([&v](
auto&& arg) {
371 template <
typename Coll,
typename Pred>
375 for (
auto& agg : me.aggregates) {
376 std::visit([&maxRuns](
auto&& arg) {
377 if (arg.runs > maxRuns) {
384 for (
int run = 1;run <= maxRuns;run++) {
385 for (
auto& agg : me.aggregates) {
386 std::visit([&run](
auto&& arg) {
392 while (!from.ended()) {
394 for (
auto& agg : me.aggregates) {
395 std::visit([&v](
auto&& arg) {
408 template <
typename ValT>
412 for (
auto& agg : me.aggregates) {
413 std::visit([&maxRuns](
auto&& arg) {
414 if (arg.runs > maxRuns) {
421 for (
int run = 1;run <= maxRuns;run++) {
422 for (
auto& agg : me.aggregates) {
423 std::visit([&run](
auto&& arg) {
429 for (
auto& v : from) {
430 for (
auto& agg : me.aggregates) {
431 std::visit([&v](
auto&& arg) {
443 template <
typename Agg>
445 Agg& retval = std::get<0>(aggregates.front());
446 for (
auto& agg : aggregates) {
448 std::visit([&retval](
auto&& arg) {
449 using T = std::decay_t<decltype(arg)>;
450 if constexpr (std::is_same_v<Agg,T>) {
459 std::vector<std::variant<Aggs...>> aggregates;
461 template <
typename Last>
462 void addAggregate(Last last) {
463 aggregates.emplace_back(std::move(last));
466 template <
typename First,
typename Second,
typename... Remaining>
467 void addAggregate(First first, Second second, Remaining... remaining) {
468 aggregates.emplace_back(std::move(first));
469 addAggregate<Second, Remaining...>(second, remaining...);
483 template <
typename... Aggs>
487 addAggregate<Aggs...>();
491 template <
typename ValT>
495 for (
auto& agg : me.aggregates) {
496 std::visit([&maxRuns](
auto&& arg) {
497 if (arg.runs > maxRuns) {
504 for (
int run = 1;run <= maxRuns;run++) {
505 for (
auto& agg : me.aggregates) {
506 std::visit([&run](
auto&& arg) {
511 for (
auto& v : from) {
512 for (
auto& agg : me.aggregates) {
513 std::visit([&v](
auto&& arg) {
524 template <
typename Agg>
527 for (
auto& agg : aggregates) {
529 std::visit([&result](
auto&& arg) {
530 using T = std::decay_t<decltype(arg)>;
531 if constexpr (std::is_same_v<Agg,T>) {
532 result = arg.reduce();
540 std::vector<std::variant<Aggs...>> aggregates;
542 template <
typename Last>
543 void addAggregate() {
544 aggregates.emplace_back(Last());
547 template <
typename First,
typename Second,
typename... Remaining>
548 void addAggregate() {
549 aggregates.emplace_back(First());
550 addAggregate<Second, Remaining...>();
Definition: distribution.h:16
void add(double x) noexcept
Add a single occurence of a value.
void reset() noexcept
Reset this instance to its initial state.
const double mean() const noexcept
return the mean
Acts as a non-global memory arena for arbitrary classes.
Definition: aggregates.h:15
Definition: aggregates.h:19
Gaussian.
Definition: aggregates.h:282
Definition: aggregates.h:49
Definition: aggregates.h:82
Definition: aggregates.h:127
A Variadic aggregation function requiring aggregations to be prior initialised (i....
Definition: aggregates.h:326
Definition: aggregates.h:484