record_value.hpp
Go to the documentation of this file.
1/**
2 * @author: Daniel Fuchs
3 * @contact: fuxeysolutions@gmail.com
4 *
5 * distributed under the MIT License (MIT).
6 * Copyright (c) Daniel Fuchs
7 *
8 */
9#pragma once
10
11#include <algorithm>
12#include <chrono>
13#include <iostream>
14#include <numeric>
15#include <vector>
16
17template <typename T>
19{
20
21public:
22 recordValue(std::chrono::system_clock::duration observTime,
23 std::chrono::system_clock::duration upDateTime) :
24 stepSize(static_cast<uint64_t>(observTime / upDateTime)), firstTime(true)
25 {
26 if (stepSize == 0)
27 {
28 throw std::runtime_error(
29 "stepsize is 0 -- not allowed: observTime: " + std::to_string(observTime.count()) +
30 " upDateTime: " + std::to_string(upDateTime.count()));
31 }
32 this->recordContainer.resize(stepSize);
33 };
34
35 explicit recordValue(uint64_t stepSize_) : stepSize(stepSize_), firstTime(true)
36 {
37 if (stepSize_ == 0)
38 {
39 throw std::runtime_error("stepsize is 0 -- not allowed!");
40 }
41 this->recordContainer.resize(stepSize);
42 };
43
44 recordValue() = delete;
45
46 void
47 addRecord(const T& rec)
48 {
49 this->recordContainer.push_back(rec);
50 if (this->firstTime)
51 {
52 std::fill(this->recordContainer.begin(), this->recordContainer.end(), rec);
53 this->firstTime = false;
54 }
55 if (this->recordContainer.size() >= this->stepSize)
56 {
57 this->recordContainer.erase(this->recordContainer.begin());
58 }
59 }
60
61 T
63 {
64 return static_cast<T>(
65 *std::min_element(this->recordContainer.begin(), this->recordContainer.end()));
66 }
67
68 T
70 {
71 return static_cast<T>(
72 *std::max_element(this->recordContainer.begin(), this->recordContainer.end()));
73 }
74
75 T
77 {
78 return static_cast<T>(
79 std::accumulate(this->recordContainer.begin(), this->recordContainer.end(), 0.0) /
80 this->stepSize);
81 }
82
83 std::vector<T>
85 {
86 return recordContainer;
87 }
88
89 ~recordValue() = default;
90
91private:
92 std::vector<T> recordContainer;
93 uint64_t stepSize;
94 bool firstTime;
95};
T getMinRecord() const
void addRecord(const T &rec)
recordValue()=delete
std::vector< T > getRecordContainer() const
T getMaxRecord() const
recordValue(std::chrono::system_clock::duration observTime, std::chrono::system_clock::duration upDateTime)
~recordValue()=default
T getAverageRecord() const
recordValue(uint64_t stepSize_)