Permalink
Browse files

Added moving_average windowing stats

  • Loading branch information...
1 parent c1425d4 commit 9c7095ec9ccc34eea1a73c250a39024bc257ebd8 @saleyn committed Jul 13, 2014
Showing with 27 additions and 26 deletions.
  1. +12 −0 include/utxx/running_stat.hpp
  2. +15 −26 test/test_running_stat.cpp
@@ -209,6 +209,18 @@ struct basic_moving_average
size_t samples() const { return m_full ? capacity() : m_size; }
double mean() const { return m_sum / ((m_full || !m_size) ? capacity() : m_size); }
+ std::pair<T,T> minmax() const {
+ if (empty()) return std::make_pair(0, 0);
+
+ T min = std::numeric_limits<T>::max();
+ T max = std::numeric_limits<T>::min();
+ for (T const* p=m_data, *e=m_full ? m_data+capacity() : m_data+m_size; p != e; ++p) {
+ if (*p > max) max = *p;
+ if (*p < min) min = *p;
+ }
+ return std::make_pair(min, max);
+ }
+
private:
const size_t MASK;
bool m_full;
@@ -96,22 +96,37 @@ BOOST_AUTO_TEST_CASE( test_running_stat )
BOOST_REQUIRE_EQUAL(0.0, rs.deviation());
}
+namespace std
+{
+ ostream& operator<< (ostream& out, pair<int, int> const& a)
+ {
+ return out << '{' << a.first << ',' << a.second << '}';
+ }
+}
+
BOOST_AUTO_TEST_CASE( test_running_stats_moving_average )
{
basic_moving_average<int, 4> ma;
+ BOOST_REQUIRE_EQUAL(std::make_pair(0, 0), ma.minmax());
+
BOOST_REQUIRE_EQUAL(4u, ma.capacity());
ma.add(2); BOOST_REQUIRE_EQUAL(2.0, ma.mean());
ma.add(4); BOOST_REQUIRE_EQUAL(3.0, ma.mean());
+
+ BOOST_REQUIRE_EQUAL(std::make_pair(2, 4), ma.minmax());
+
ma.add(6); BOOST_REQUIRE_EQUAL(4.0, ma.mean());
ma.add(8); BOOST_REQUIRE_EQUAL(5.0, ma.mean());
+ BOOST_REQUIRE_EQUAL(std::make_pair(2, 8), ma.minmax());
BOOST_REQUIRE_EQUAL(4u, ma.samples());
ma.add(10); BOOST_REQUIRE_EQUAL(7.0, ma.mean());
ma.add(8); BOOST_REQUIRE_EQUAL(8.0, ma.mean());
+ BOOST_REQUIRE_EQUAL(std::make_pair(6,10), ma.minmax());
BOOST_REQUIRE_EQUAL(4u, ma.samples());
ma.clear();
@@ -120,29 +135,3 @@ BOOST_AUTO_TEST_CASE( test_running_stats_moving_average )
BOOST_REQUIRE_EQUAL(0.0, ma.mean());
}
-enum test_rs {
- ONE
- , TWO
- , THREE
-};
-
-BOOST_AUTO_TEST_CASE( test_running_stats )
-{
-/*
- typedef basic_running_tuple_stat<test_rs, size_t, 3> basic_rs;
-
- basic_rs::value_tuple value;
-
- value.clear();
- boost::get<ONE>(value) = 1.0;
- boost::get<TWO>(value) = 2.0;
- boost::get<THREE>(value) = 3.0;
-
- basic_rs stats;
-
- stats.clear();
- stats.add(value);
-
- std::cout << "Count: " << stats.count() << std::endl;
-*/
-}

0 comments on commit 9c7095e

Please sign in to comment.