Line data Source code
1 : /** 2 : Copyright (c) 2016-2017 Roman Katuntsev <sbkarr@stappler.org> 3 : Copyright (c) 2023 Stappler LLC <admin@stappler.dev> 4 : 5 : Permission is hereby granted, free of charge, to any person obtaining a copy 6 : of this software and associated documentation files (the "Software"), to deal 7 : in the Software without restriction, including without limitation the rights 8 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 : copies of the Software, and to permit persons to whom the Software is 10 : furnished to do so, subject to the following conditions: 11 : 12 : The above copyright notice and this permission notice shall be included in 13 : all copies or substantial portions of the Software. 14 : 15 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 : THE SOFTWARE. 22 : **/ 23 : 24 : #ifndef STAPPLER_CORE_UTILS_SPMOVINGAVERAGE_H_ 25 : #define STAPPLER_CORE_UTILS_SPMOVINGAVERAGE_H_ 26 : 27 : #include "SPCommon.h" 28 : 29 : namespace STAPPLER_VERSIONIZED stappler::math { 30 : 31 : template <uint64_t Count, typename T = float> 32 : class MovingAverage { 33 : public: 34 218 : void dropValues() { 35 1048 : for (size_t i = 0; i < Count; i++) { 36 830 : _values[i] = 0; 37 : } 38 218 : } 39 19205 : void addValue(T value) { 40 19205 : _values[_current ++ % Count] = value; 41 19205 : } 42 19205 : T getAverage() const { 43 19205 : size_t c = 0; 44 19205 : T s = 0; 45 385061 : for (size_t i = 0; i < min(Count, _current); i++) { 46 365855 : s += _values[i]; 47 365856 : ++ c; 48 : } 49 19204 : return s / c; 50 : } 51 584 : T step(T value) { 52 584 : addValue(value); 53 584 : return getAverage(); 54 : } 55 : 56 : T range() { 57 : Pair<T, T> minmax(std::numeric_limits<T>::max(), std::numeric_limits<T>::min()); 58 : 59 : for (size_t i = 0; i < min(Count, _current); i++) { 60 : if (_values[i] != 0) { 61 : if (_values[i] < minmax.first) { minmax.first = _values[i]; } 62 : if (_values[i] > minmax.second) { minmax.second = _values[i]; } 63 : } 64 : } 65 : 66 : if (minmax.first <= minmax.second) { 67 : return minmax.second - minmax.first; 68 : } else { 69 : return 0; 70 : } 71 : } 72 : 73 : size_t size() const { return Count; } 74 : 75 20 : void reset(const T &value) { 76 420 : for (auto &it : _values) { 77 400 : it = value; 78 : } 79 20 : } 80 : 81 276 : MovingAverage() { 82 276 : memset(_values.data(), 0, _values.size() * sizeof(T)); 83 276 : } 84 : 85 : protected: 86 : uint64_t _current = 0; 87 : std::array<T, Count> _values; 88 : }; 89 : 90 : } 91 : 92 : #endif // STAPPLER_CORE_UTILS_SPMOVINGAVERAGE_H_