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 <chrono>
12 #include <vector>
13 #include <algorithm>
14 #include <numeric>
15 #include <iostream>
16 
17 
18 template<typename T>
19 class recordValue {
20 
21 public:
22  recordValue(std::chrono::system_clock::duration observTime, std::chrono::system_clock::duration upDateTime)
23  : stepSize(
24  static_cast<uint64_t>(observTime / upDateTime)), firstTime(true) {
25  if(stepSize == 0) {
26  throw std::runtime_error("stepsize is 0 -- not allowed: observTime: "
27  + std::to_string(observTime.count())
28  + " upDateTime: "
29  + std::to_string(upDateTime.count()));
30  }
31  this->recordContainer.resize(stepSize);
32  };
33 
34  explicit recordValue(uint64_t stepSize_) : stepSize(stepSize_), firstTime(true) {
35  if(stepSize_ == 0) {
36  throw std::runtime_error("stepsize is 0 -- not allowed!");
37  }
38  this->recordContainer.resize(stepSize);
39  };
40 
41  recordValue() = delete;
42 
43  void addRecord(const T &rec) {
44  this->recordContainer.push_back(rec);
45  if (this->firstTime) {
46  std::fill(this->recordContainer.begin(), this->recordContainer.end(), rec);
47  this->firstTime = false;
48  }
49  if (this->recordContainer.size() >= this->stepSize) {
50  this->recordContainer.erase(this->recordContainer.begin());
51  }
52  }
53 
54  T getMinRecord() const {
55  return static_cast<T>(*std::min_element(this->recordContainer.begin(), this->recordContainer.end()));
56  }
57 
58  T getMaxRecord() const {
59  return static_cast<T>(*std::max_element(this->recordContainer.begin(), this->recordContainer.end()));
60  }
61 
62  T getAverageRecord() const {
63  return static_cast<T>(std::accumulate(this->recordContainer.begin(), this->recordContainer.end(), 0.0) /
64  this->stepSize);
65  }
66 
67  std::vector<T> getRecordContainer() const {
68  return recordContainer;
69  }
70 
71  ~recordValue() = default;
72 
73 private:
74 
75  std::vector<T> recordContainer;
76  uint64_t stepSize;
77  bool firstTime;
78 };
79 
80 
recordValue
Definition: record_value.hpp:19
recordValue::addRecord
void addRecord(const T &rec)
Definition: record_value.hpp:43
recordValue::recordValue
recordValue()=delete
recordValue::recordValue
recordValue(uint64_t stepSize_)
Definition: record_value.hpp:34
recordValue::getMaxRecord
T getMaxRecord() const
Definition: record_value.hpp:58
recordValue::getMinRecord
T getMinRecord() const
Definition: record_value.hpp:54
recordValue::getAverageRecord
T getAverageRecord() const
Definition: record_value.hpp:62
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
recordValue::~recordValue
~recordValue()=default
recordValue::getRecordContainer
std::vector< T > getRecordContainer() const
Definition: record_value.hpp:67
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
recordValue::recordValue
recordValue(std::chrono::system_clock::duration observTime, std::chrono::system_clock::duration upDateTime)
Definition: record_value.hpp:22