memory_box.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. * @project Project
  3. * @brief 这是一个map, key为steady_lock, 存储数据最长保存一段时间, 也存在数量限制.
  4. * @author lz
  5. * @data 2023/6/24
  6. **/
  7. #pragma once
  8. #include <iostream>
  9. #include <chrono>
  10. #include <map>
  11. #include <cmath>
  12. template<typename structT>
  13. class MemoryBox {
  14. public:
  15. using time_Map = std::map<std::chrono::steady_clock::time_point, structT>;
  16. public:
  17. explicit MemoryBox(size_t st = 20, double durability = 3) {
  18. map_max_size = st;
  19. map_durability = durability;
  20. }
  21. Error_manager save(const structT &t) {
  22. if (m_map_T.size() > map_max_size) {
  23. m_map_T.erase(m_map_T.begin());
  24. }
  25. m_map_T.insert(std::pair<std::chrono::steady_clock::time_point, structT>(std::chrono::steady_clock::now(), t));
  26. update();
  27. return Error_code::SUCCESS;
  28. }
  29. Error_manager get(time_Map &t) {
  30. update();
  31. t = m_map_T;
  32. return Error_code::SUCCESS;
  33. }
  34. time_Map get() {
  35. update();
  36. return m_map_T;
  37. }
  38. Error_manager getLast(structT &t) {
  39. update();
  40. if (m_map_T.empty()) {
  41. return Error_code::FAILED;
  42. }
  43. t = m_map_T.end()->second;
  44. return Error_code::SUCCESS;
  45. }
  46. size_t size() {
  47. return m_map_T.size();
  48. }
  49. private:
  50. void update() {
  51. if (m_map_T.empty()) {
  52. return;
  53. }
  54. std::chrono::duration<double> diff = std::chrono::steady_clock::now() - m_map_T.begin()->first;
  55. while (diff.count() > map_durability) {
  56. m_map_T.erase(m_map_T.begin());
  57. if (m_map_T.size() > 0) {
  58. diff = std::chrono::steady_clock::now() - m_map_T.begin()->first;
  59. } else {
  60. return;
  61. }
  62. }
  63. }
  64. protected:
  65. std::map<std::chrono::steady_clock::time_point, structT> m_map_T;
  66. size_t map_max_size;
  67. double map_durability;
  68. private:
  69. };
  70. class stableValue : public MemoryBox<double> {
  71. public:
  72. // 方差求值
  73. double value() {
  74. double value = 0;
  75. if (m_map_T.size() > 1) {
  76. double av = 0;
  77. for (auto &t: m_map_T) {
  78. av += t.second;
  79. }
  80. av = av / m_map_T.size();
  81. printf("-----> av: %f\n", av);
  82. double var = 0;
  83. for (auto &t: m_map_T) {
  84. var += (t.second - av) * (t.second - av);
  85. }
  86. var = sqrt(var / m_map_T.size());
  87. printf("-----> var: %f\n", var);
  88. size_t num = 0;
  89. for (auto &t: m_map_T) {
  90. if (fabs(t.second - av) < var) {
  91. value += t.second;
  92. num++;
  93. }
  94. }
  95. printf("-----> value sum: %f, num: %ld\n", value, num);
  96. value = value / num;
  97. } else if (m_map_T.size() == 1) {
  98. value = m_map_T.begin()->second;
  99. } else {
  100. value = 0;
  101. }
  102. return value;
  103. }
  104. private:
  105. };
  106. #define stable_value(value_, t_map_, member_) { \
  107. value_ = 0 \
  108. if (t_map_.size() > 1) { \
  109. double av = 0; \
  110. for (auto &t: t_map_) { \
  111. av += t.second.member_; \
  112. } \
  113. av = av / t_map_.size(); \
  114. double var = 0; \
  115. for (auto &t: t_map_) { \
  116. var += (t.second.member_ - av) * (t.second.member_ - av); \
  117. } \
  118. var = sqrt(var / t_map_.size()); \
  119. size_t num = 0; \
  120. for (auto &t: t_map_) { \
  121. if (t.second.member_ - av < var) { \
  122. value_ += t.second.member_; \
  123. num++; \
  124. } \
  125. } \
  126. value_ = value_ / num; \
  127. } else if (t_map_.size() == 1) { \
  128. value_ = t_map_.begin()->second.member_; \
  129. } else { \
  130. value_ = 0; \
  131. } \
  132. }