Skip to content

Commit bf79b09

Browse files
committed
rolling min and max
1 parent ef28b2b commit bf79b09

File tree

6 files changed

+368
-0
lines changed

6 files changed

+368
-0
lines changed

doc/accumulators.qbk

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2077,6 +2077,99 @@ The rolling count is the current number of elements in the rolling window.
20772077

20782078
[endsect]
20792079

2080+
[section:rolling_max rolling_max]
2081+
2082+
The rolling sum is the sum of the last /N/ samples.
2083+
2084+
[variablelist
2085+
[[Result Type] [``_sample_type_``]]
2086+
[[Depends On] [`sorted_rolling_window`]]
2087+
[[Variants] [['none]]]
2088+
[[Initialization Parameters] [`tag::rolling_window::window_size`]]
2089+
[[Accumulator Parameters] [['none]]]
2090+
[[Extractor Parameters] [['none]]]
2091+
[[Accumulator Complexity] [O(log N), where N is the window size]]
2092+
[[Extractor Complexity] [O(1)]]
2093+
]
2094+
2095+
[*Header]
2096+
[def _ROLLING_MAX_HPP_ [headerref boost/accumulators/statistics/rolling_max.hpp]]
2097+
2098+
#include <_ROLLING_MAX_HPP_>
2099+
2100+
[*Example]
2101+
2102+
accumulator_set<int, stats<tag::rolling_max> > acc(tag::rolling_window::window_size = 3);
2103+
2104+
acc(1);
2105+
BOOST_CHECK_EQUAL(1, rolling_max(acc));
2106+
2107+
acc(2);
2108+
BOOST_CHECK_EQUAL(2, rolling_max(acc));
2109+
2110+
acc(3);
2111+
BOOST_CHECK_EQUAL(3, rolling_max(acc));
2112+
2113+
acc(1);
2114+
BOOST_CHECK_EQUAL(3, rolling_max(acc));
2115+
2116+
acc(-1);
2117+
BOOST_CHECK_EQUAL(1, rolling_max(acc));
2118+
2119+
acc(0);
2120+
BOOST_CHECK_EQUAL(1, rolling_max(acc));
2121+
2122+
[*See also]
2123+
2124+
* [classref boost::accumulators::impl::rolling_max_impl [^rolling_max_impl]]
2125+
2126+
[endsect]
2127+
2128+
[section:rolling_min rolling_min]
2129+
2130+
The rolling sum is the sum of the last /N/ samples.
2131+
2132+
[variablelist
2133+
[[Result Type] [``_sample_type_``]]
2134+
[[Depends On] [`sorted_rolling_window`]]
2135+
[[Variants] [['none]]]
2136+
[[Initialization Parameters] [`tag::rolling_window::window_size`]]
2137+
[[Accumulator Parameters] [['none]]]
2138+
[[Extractor Parameters] [['none]]]
2139+
[[Accumulator Complexity] [O(log N), where N is the window size]]
2140+
[[Extractor Complexity] [O(1)]]
2141+
]
2142+
2143+
[*Header]
2144+
[def _ROLLING_MIN_HPP_ [headerref boost/accumulators/statistics/rolling_min.hpp]]
2145+
2146+
#include <_ROLLING_MIN_HPP_>
2147+
2148+
[*Example]
2149+
2150+
accumulator_set<int, stats<tag::rolling_min> > acc(tag::rolling_window::window_size = 3);
2151+
2152+
acc(1);
2153+
BOOST_CHECK_EQUAL(1, rolling_min(acc));
2154+
2155+
acc(2);
2156+
BOOST_CHECK_EQUAL(1, rolling_min(acc));
2157+
2158+
acc(3);
2159+
BOOST_CHECK_EQUAL(1, rolling_min(acc));
2160+
2161+
acc(4);
2162+
BOOST_CHECK_EQUAL(2, rolling_min(acc));
2163+
2164+
acc(-1);
2165+
BOOST_CHECK_EQUAL(-1, rolling_min(acc));
2166+
2167+
[*See also]
2168+
2169+
* [classref boost::accumulators::impl::rolling_min_impl [^rolling_min_impl]]
2170+
2171+
[endsect]
2172+
20802173
[section:rolling_sum rolling_sum]
20812174

20822175
The rolling sum is the sum of the last /N/ samples.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
///////////////////////////////////////////////////////////////////////////////
2+
// rolling_max.hpp
3+
//
4+
// Copyright 2018 Quentin Chateau. Distributed under the Boost
5+
// Software License, Version 1.0. (See accompanying file
6+
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7+
8+
#ifndef BOOST_ACCUMULATORS_STATISTICS_ROLLING_MAX_HPP_QC_20_12_2018
9+
#define BOOST_ACCUMULATORS_STATISTICS_ROLLING_MAX_HPP_QC_20_12_2018
10+
11+
#include <boost/accumulators/framework/accumulator_base.hpp>
12+
#include <boost/accumulators/framework/extractor.hpp>
13+
#include <boost/accumulators/framework/parameters/sample.hpp>
14+
#include <boost/accumulators/framework/depends_on.hpp>
15+
#include <boost/accumulators/statistics_fwd.hpp>
16+
#include <boost/accumulators/statistics/sorted_rolling_window.hpp>
17+
18+
namespace boost { namespace accumulators
19+
{
20+
21+
namespace impl
22+
{
23+
///////////////////////////////////////////////////////////////////////////////
24+
// rolling_max_impl
25+
template<typename Sample>
26+
struct rolling_max_impl
27+
: accumulator_base
28+
{
29+
// for boost::result_of
30+
typedef Sample result_type;
31+
32+
rolling_max_impl(dont_care)
33+
{
34+
}
35+
36+
template<typename Args>
37+
result_type result(Args const &args) const
38+
{
39+
if (sorted_rolling_window(args).empty())
40+
{
41+
return numeric::as_min(Sample());
42+
}
43+
return sorted_rolling_window(args).back();
44+
}
45+
};
46+
47+
} // namespace impl
48+
49+
///////////////////////////////////////////////////////////////////////////////
50+
// tag::rolling_max
51+
//
52+
namespace tag
53+
{
54+
struct rolling_max
55+
: depends_on< sorted_rolling_window >
56+
{
57+
/// INTERNAL ONLY
58+
///
59+
typedef accumulators::impl::rolling_max_impl<mpl::_1> impl;
60+
};
61+
}
62+
63+
///////////////////////////////////////////////////////////////////////////////
64+
// extract::rolling_max
65+
//
66+
namespace extract
67+
{
68+
extractor<tag::rolling_max> const rolling_max = {};
69+
70+
BOOST_ACCUMULATORS_IGNORE_GLOBAL(rolling_max)
71+
}
72+
73+
using extract::rolling_max;
74+
75+
}} // namespace boost::accumulators
76+
77+
#endif
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
///////////////////////////////////////////////////////////////////////////////
2+
// rolling_min.hpp
3+
//
4+
// Copyright 2018 Quentin Chateau. Distributed under the Boost
5+
// Software License, Version 1.0. (See accompanying file
6+
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7+
8+
#ifndef BOOST_ACCUMULATORS_STATISTICS_ROLLING_MIN_HPP_QC_20_12_2018
9+
#define BOOST_ACCUMULATORS_STATISTICS_ROLLING_MIN_HPP_QC_20_12_2018
10+
11+
#include <boost/accumulators/framework/accumulator_base.hpp>
12+
#include <boost/accumulators/framework/extractor.hpp>
13+
#include <boost/accumulators/framework/parameters/sample.hpp>
14+
#include <boost/accumulators/framework/depends_on.hpp>
15+
#include <boost/accumulators/statistics_fwd.hpp>
16+
#include <boost/accumulators/statistics/sorted_rolling_window.hpp>
17+
18+
namespace boost { namespace accumulators
19+
{
20+
21+
namespace impl
22+
{
23+
///////////////////////////////////////////////////////////////////////////////
24+
// rolling_min_impl
25+
template<typename Sample>
26+
struct rolling_min_impl
27+
: accumulator_base
28+
{
29+
// for boost::result_of
30+
typedef Sample result_type;
31+
32+
rolling_min_impl(dont_care)
33+
{
34+
}
35+
36+
template<typename Args>
37+
result_type result(Args const &args) const
38+
{
39+
if (sorted_rolling_window(args).empty())
40+
{
41+
return numeric::as_max(Sample());
42+
}
43+
return sorted_rolling_window(args).front();
44+
}
45+
};
46+
47+
} // namespace impl
48+
49+
///////////////////////////////////////////////////////////////////////////////
50+
// tag::rolling_min
51+
//
52+
namespace tag
53+
{
54+
struct rolling_min
55+
: depends_on< sorted_rolling_window >
56+
{
57+
/// INTERNAL ONLY
58+
///
59+
typedef accumulators::impl::rolling_min_impl<mpl::_1> impl;
60+
};
61+
}
62+
63+
///////////////////////////////////////////////////////////////////////////////
64+
// extract::rolling_min
65+
//
66+
namespace extract
67+
{
68+
extractor<tag::rolling_min> const rolling_min = {};
69+
70+
BOOST_ACCUMULATORS_IGNORE_GLOBAL(rolling_min)
71+
}
72+
73+
using extract::rolling_min;
74+
75+
}} // namespace boost::accumulators
76+
77+
#endif

test/Jamfile.v2

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ test-suite "accumulators"
4747
[ run reference.cpp ]
4848
[ run rolling_count.cpp ]
4949
[ run rolling_sum.cpp ]
50+
[ run rolling_max.cpp ]
5051
[ run rolling_mean.cpp ]
52+
[ run rolling_min.cpp ]
5153
[ run skewness.cpp ]
5254
[ run sorted_rolling_window.cpp ]
5355
[ run sum.cpp ]

test/rolling_max.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// (C) Copyright Quentin Chateau 2018.
2+
// Use, modification and distribution are subject to the
3+
// Boost Software License, Version 1.0. (See accompanying file
4+
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5+
6+
#include <boost/test/unit_test.hpp>
7+
#include <boost/container/set.hpp>
8+
#include <boost/accumulators/accumulators.hpp>
9+
#include <boost/accumulators/statistics/stats.hpp>
10+
#include <boost/accumulators/statistics/rolling_max.hpp>
11+
12+
using namespace boost;
13+
using namespace unit_test;
14+
using namespace accumulators;
15+
using namespace container;
16+
17+
///////////////////////////////////////////////////////////////////////////////
18+
// test_stat
19+
//
20+
void test_stat()
21+
{
22+
accumulator_set<int, stats<tag::rolling_max> > acc(tag::rolling_window::window_size = 3);
23+
24+
BOOST_CHECK_EQUAL(numeric::as_min(int()), rolling_max(acc));
25+
26+
acc(1);
27+
BOOST_CHECK_EQUAL(1, rolling_max(acc));
28+
29+
acc(3);
30+
BOOST_CHECK_EQUAL(3, rolling_max(acc));
31+
32+
acc(5);
33+
BOOST_CHECK_EQUAL(5, rolling_max(acc));
34+
35+
acc(7);
36+
BOOST_CHECK_EQUAL(7, rolling_max(acc));
37+
38+
acc(6);
39+
BOOST_CHECK_EQUAL(7, rolling_max(acc));
40+
41+
acc(1);
42+
BOOST_CHECK_EQUAL(7, rolling_max(acc));
43+
44+
acc(3);
45+
BOOST_CHECK_EQUAL(6, rolling_max(acc));
46+
47+
acc(2);
48+
BOOST_CHECK_EQUAL(3, rolling_max(acc));
49+
}
50+
51+
///////////////////////////////////////////////////////////////////////////////
52+
// init_unit_test_suite
53+
//
54+
test_suite* init_unit_test_suite( int argc, char* argv[] )
55+
{
56+
test_suite *test = BOOST_TEST_SUITE("rolling max test");
57+
58+
test->add(BOOST_TEST_CASE(&test_stat));
59+
60+
return test;
61+
}

test/rolling_min.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// (C) Copyright Quentin Chateau 2018.
2+
// Use, modification and distribution are subject to the
3+
// Boost Software License, Version 1.0. (See accompanying file
4+
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5+
6+
#include <boost/test/unit_test.hpp>
7+
#include <boost/container/set.hpp>
8+
#include <boost/accumulators/accumulators.hpp>
9+
#include <boost/accumulators/statistics/stats.hpp>
10+
#include <boost/accumulators/statistics/rolling_min.hpp>
11+
12+
using namespace boost;
13+
using namespace unit_test;
14+
using namespace accumulators;
15+
using namespace container;
16+
17+
///////////////////////////////////////////////////////////////////////////////
18+
// test_stat
19+
//
20+
void test_stat()
21+
{
22+
accumulator_set<int, stats<tag::rolling_min> > acc(tag::rolling_window::window_size = 3);
23+
24+
BOOST_CHECK_EQUAL(numeric::as_max(int()), rolling_min(acc));
25+
26+
acc(1);
27+
BOOST_CHECK_EQUAL(1, rolling_min(acc));
28+
29+
acc(3);
30+
BOOST_CHECK_EQUAL(1, rolling_min(acc));
31+
32+
acc(5);
33+
BOOST_CHECK_EQUAL(1, rolling_min(acc));
34+
35+
acc(7);
36+
BOOST_CHECK_EQUAL(3, rolling_min(acc));
37+
38+
acc(6);
39+
BOOST_CHECK_EQUAL(5, rolling_min(acc));
40+
41+
acc(7);
42+
BOOST_CHECK_EQUAL(6, rolling_min(acc));
43+
44+
acc(1);
45+
BOOST_CHECK_EQUAL(1, rolling_min(acc));\
46+
}
47+
48+
///////////////////////////////////////////////////////////////////////////////
49+
// init_unit_test_suite
50+
//
51+
test_suite* init_unit_test_suite( int argc, char* argv[] )
52+
{
53+
test_suite *test = BOOST_TEST_SUITE("rolling min test");
54+
55+
test->add(BOOST_TEST_CASE(&test_stat));
56+
57+
return test;
58+
}

0 commit comments

Comments
 (0)