From b2e098421427953d8550f94398e05ad36c3aef2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Sun, 17 Dec 2017 18:35:26 +0100 Subject: [PATCH] Improve memory footprint in filter classes --- code/espurna/filters/BaseFilter.h | 14 ++++++-------- code/espurna/filters/MedianFilter.h | 20 ++++++++++---------- code/espurna/filters/MovingAverageFilter.h | 6 +++--- 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/code/espurna/filters/BaseFilter.h b/code/espurna/filters/BaseFilter.h index e86dac37..d11e99d8 100644 --- a/code/espurna/filters/BaseFilter.h +++ b/code/espurna/filters/BaseFilter.h @@ -11,29 +11,27 @@ class BaseFilter { public: BaseFilter() { - _data = new std::vector(); } ~BaseFilter() { - if (_data) delete _data; } virtual void add(double value) { - _data->push_back(value); + _data.push_back(value); } virtual unsigned char count() { - return _data->size(); + return _data.size(); } virtual void reset() { - _data->clear(); + _data.clear(); } virtual double max() { double max = 0; - for (unsigned char i = 1; i < _data->size(); i++) { - if (max < _data->at(i)) max = _data->at(i); + for (unsigned char i = 1; i < _data.size(); i++) { + if (max < _data.at(i)) max = _data.at(i); } return max; } @@ -44,6 +42,6 @@ class BaseFilter { protected: - std::vector *_data; + std::vector _data; }; diff --git a/code/espurna/filters/MedianFilter.h b/code/espurna/filters/MedianFilter.h index 298de511..e85e3793 100644 --- a/code/espurna/filters/MedianFilter.h +++ b/code/espurna/filters/MedianFilter.h @@ -11,8 +11,8 @@ class MedianFilter : public BaseFilter { public: virtual void reset() { - double last = _data->empty() ? 0 : _data->back(); - _data->clear(); + double last = _data.empty() ? 0 : _data.back(); + _data.clear(); add(last); } @@ -20,13 +20,13 @@ class MedianFilter : public BaseFilter { double sum = 0; - if (_data->size() > 2) { + if (_data.size() > 2) { - for (unsigned char i = 1; i <= _data->size() - 2; i++) { + for (unsigned char i = 1; i <= _data.size() - 2; i++) { - double previous = _data->at(i-1); - double current = _data->at(i); - double next = _data->at(i+1); + double previous = _data.at(i-1); + double current = _data.at(i); + double next = _data.at(i+1); if (previous > current) std::swap(previous, current); if (current > next) std::swap(current, next); @@ -36,11 +36,11 @@ class MedianFilter : public BaseFilter { } - sum /= (_data->size() - 2); + sum /= (_data.size() - 2); - } else if (_data->size() > 0) { + } else if (_data.size() > 0) { - sum = _data->front(); + sum = _data.front(); } diff --git a/code/espurna/filters/MovingAverageFilter.h b/code/espurna/filters/MovingAverageFilter.h index a5614164..f6eb40d3 100644 --- a/code/espurna/filters/MovingAverageFilter.h +++ b/code/espurna/filters/MovingAverageFilter.h @@ -14,13 +14,13 @@ class MovingAverageFilter : public BaseFilter { MovingAverageFilter(unsigned char size) { _size = size; for (unsigned char i=0; ipush_back(0); + _data.push_back(0); } } virtual void add(double value) { - _sum = _sum + value - _data->at(_pointer); - _data->at(_pointer) = value; + _sum = _sum + value - _data.at(_pointer); + _data.at(_pointer) = value; _pointer = (_pointer + 1) % _size; }