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 
17 template <typename T>
19 {
20 
21 public:
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
62  getMinRecord() const
63  {
64  return static_cast<T>(
65  *std::min_element(this->recordContainer.begin(), this->recordContainer.end()));
66  }
67 
68  T
69  getMaxRecord() const
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 
91 private:
92  std::vector<T> recordContainer;
93  uint64_t stepSize;
94  bool firstTime;
95 };
recordValue
Definition: record_value.hpp:18
recordValue::addRecord
void addRecord(const T &rec)
Definition: record_value.hpp:47
recordValue::recordValue
recordValue()=delete
recordValue::recordValue
recordValue(uint64_t stepSize_)
Definition: record_value.hpp:35
recordValue::getMaxRecord
T getMaxRecord() const
Definition: record_value.hpp:69
recordValue::getMinRecord
T getMinRecord() const
Definition: record_value.hpp:62
recordValue::getAverageRecord
T getAverageRecord() const
Definition: record_value.hpp:76
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:41
recordValue::~recordValue
~recordValue()=default
recordValue::getRecordContainer
std::vector< T > getRecordContainer() const
Definition: record_value.hpp:84
T
float T
Definition: UnscentedKalmanFilterTest.cpp:38
recordValue::recordValue
recordValue(std::chrono::system_clock::duration observTime, std::chrono::system_clock::duration upDateTime)
Definition: record_value.hpp:22