Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions doc/accumulators.qbk
Original file line number Diff line number Diff line change
Expand Up @@ -2077,6 +2077,109 @@ The rolling count is the current number of elements in the rolling window.

[endsect]

[section:rolling_max rolling_max]

The rolling max is the max of the last /N/ samples.

[variablelist
[[Result Type] [``_sample_type_``]]
[[Depends On] [`lazy_rolling_max` depends on `rolling_window` \n
`immediate_rolling_max` depends on `sorted_rolling_window`]]
[[Variants] [`lazy_rolling_max` (a.k.a. `rolling_max(lazy)`) \n
`immediate_rolling_max` (a.k.a. `rolling_max(immediate)`)]]
[[Initialization Parameters] [`tag::rolling_window::window_size`]]
[[Accumulator Parameters] [['none]]]
[[Extractor Parameters] [['none]]]
[[Accumulator Complexity] [`lazy_rolling_max` is O(1) \n
`immediate_rolling_max` is O(log N), where N is the window size]]
[[Extractor Complexity] [`lazy_rolling_max` is O(N), where N is the window size \n
`immediate_rolling_max` is O(1)]]
]

[*Header]
[def _ROLLING_MAX_HPP_ [headerref boost/accumulators/statistics/rolling_max.hpp]]

#include <_ROLLING_MAX_HPP_>

[*Example]

accumulator_set<int, stats<tag::rolling_max> > acc(tag::rolling_window::window_size = 3);

acc(1);
BOOST_CHECK_EQUAL(1, rolling_max(acc));

acc(2);
BOOST_CHECK_EQUAL(2, rolling_max(acc));

acc(3);
BOOST_CHECK_EQUAL(3, rolling_max(acc));

acc(1);
BOOST_CHECK_EQUAL(3, rolling_max(acc));

acc(-1);
BOOST_CHECK_EQUAL(1, rolling_max(acc));

acc(0);
BOOST_CHECK_EQUAL(1, rolling_max(acc));

[*See also]

* [classref boost::accumulators::impl::lazy_rolling_max_impl [^lazy_rolling_max_impl]]
* [classref boost::accumulators::impl::immediate_rolling_max_impl [^immediate_rolling_max_impl]]

[endsect]

[section:rolling_min rolling_min]

The rolling min is the min of the last /N/ samples.

[variablelist
[[Result Type] [``_sample_type_``]]
[[Depends On] [`lazy_rolling_min` depends on `rolling_window` \n
`immediate_rolling_min` depends on `sorted_rolling_window`]]
[[Variants] [`lazy_rolling_min` (a.k.a. `rolling_min(lazy)`) \n
`immediate_rolling_min` (a.k.a. `rolling_min(immediate)`)]]
[[Initialization Parameters] [`tag::rolling_window::window_size`]]
[[Accumulator Parameters] [['none]]]
[[Extractor Parameters] [['none]]]
[[Accumulator Complexity] [`lazy_rolling_min` is O(1) \n
`immediate_rolling_min` is O(log N), where N is the window size]]
[[Extractor Complexity] [`lazy_rolling_min` is O(N), where N is the window size \n
`immediate_rolling_min` is O(1)]]
]

[*Header]
[def _ROLLING_MIN_HPP_ [headerref boost/accumulators/statistics/rolling_min.hpp]]

#include <_ROLLING_MIN_HPP_>

[*Example]

accumulator_set<int, stats<tag::rolling_min> > acc(tag::rolling_window::window_size = 3);

acc(1);
BOOST_CHECK_EQUAL(1, rolling_min(acc));

acc(2);
BOOST_CHECK_EQUAL(1, rolling_min(acc));

acc(3);
BOOST_CHECK_EQUAL(1, rolling_min(acc));

acc(4);
BOOST_CHECK_EQUAL(2, rolling_min(acc));

acc(-1);
BOOST_CHECK_EQUAL(-1, rolling_min(acc));

[*See also]

* [classref boost::accumulators::impl::lazy_rolling_min_impl [^lazy_rolling_min_impl]]
* [classref boost::accumulators::impl::immediate_rolling_min_impl [^immediate_rolling_min_impl]]

[endsect]

[section:rolling_sum rolling_sum]

The rolling sum is the sum of the last /N/ samples.
Expand Down
163 changes: 163 additions & 0 deletions include/boost/accumulators/statistics/rolling_max.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
///////////////////////////////////////////////////////////////////////////////
// rolling_max.hpp
//
// Copyright 2018 Quentin Chateau. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#ifndef BOOST_ACCUMULATORS_STATISTICS_ROLLING_MAX_HPP_QC_20_12_2018
#define BOOST_ACCUMULATORS_STATISTICS_ROLLING_MAX_HPP_QC_20_12_2018

#include <boost/accumulators/framework/accumulator_base.hpp>
#include <boost/accumulators/framework/extractor.hpp>
#include <boost/accumulators/framework/parameters/sample.hpp>
#include <boost/accumulators/framework/depends_on.hpp>
#include <boost/accumulators/statistics_fwd.hpp>
#include <boost/accumulators/statistics/rolling_window.hpp>
#include <boost/accumulators/statistics/sorted_rolling_window.hpp>

namespace boost { namespace accumulators
{

namespace impl
{
///////////////////////////////////////////////////////////////////////////////
// immediate_rolling_max_impl
// max is calculated on insersion using a sorted_rolling_window
template<typename Sample>
struct immediate_rolling_max_impl
: accumulator_base
{
// for boost::result_of
typedef Sample result_type;

immediate_rolling_max_impl(dont_care)
{
}

template<typename Args>
result_type result(Args const &args) const
{
if (sorted_rolling_window(args).empty())
{
return numeric::as_min(Sample());
}
return sorted_rolling_window(args).back();
}
};

///////////////////////////////////////////////////////////////////////////////
// lazy_rolling_max_impl
// max is calculated on extraction by iterating the rolling window
template<typename Sample>
struct lazy_rolling_max_impl
: accumulator_base
{
// for boost::result_of
typedef Sample result_type;

lazy_rolling_max_impl(dont_care)
{
}

template<typename Args>
result_type result(Args const &args) const
{
if (rolling_window(args).empty())
{
return numeric::as_min(Sample());
}
return *max_element(rolling_window(args).begin(), rolling_window(args).end());
}
};

} // namespace impl

///////////////////////////////////////////////////////////////////////////////
// tag::immediate_rolling_max
// tag::lazy_rolling_max
// tag::rolling_max
//
namespace tag
{
struct immediate_rolling_max
: depends_on< sorted_rolling_window >
{
/// INTERNAL ONLY
///
typedef accumulators::impl::immediate_rolling_max_impl<mpl::_1> impl;

#ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
/// tag::rolling_window::window_size named parameter
static boost::parameter::keyword<tag::rolling_window_size> const window_size;
#endif
};

struct lazy_rolling_max
: depends_on< rolling_window >
{
/// INTERNAL ONLY
///
typedef accumulators::impl::lazy_rolling_max_impl<mpl::_1> impl;

#ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
/// tag::rolling_window::window_size named parameter
static boost::parameter::keyword<tag::rolling_window_size> const window_size;
#endif
};

// make lazy_rolling_max the default implementation
struct rolling_max : lazy_rolling_max {};
}

///////////////////////////////////////////////////////////////////////////////
// extract::rolling_max
//
namespace extract
{
extractor<tag::immediate_rolling_max> const immediate_rolling_max = {};
extractor<tag::lazy_rolling_max> const lazy_rolling_max = {};
extractor<tag::rolling_max> const rolling_max = {};

BOOST_ACCUMULATORS_IGNORE_GLOBAL(immediate_rolling_max)
BOOST_ACCUMULATORS_IGNORE_GLOBAL(lazy_rolling_max)
BOOST_ACCUMULATORS_IGNORE_GLOBAL(rolling_max)
}

using extract::immediate_rolling_max;
using extract::lazy_rolling_max;
using extract::rolling_max;

// rolling_max(lazy) -> lazy_rolling_max
template<>
struct as_feature<tag::rolling_max(lazy)>
{
typedef tag::lazy_rolling_max type;
};

// rolling_max(immediate) -> immediate_rolling_max
template<>
struct as_feature<tag::rolling_max(immediate)>
{
typedef tag::immediate_rolling_max type;
};

// for the purposes of feature-based dependency resolution,
// immediate_rolling_max provides the same feature as rolling_max
template<>
struct feature_of<tag::immediate_rolling_max>
: feature_of<tag::rolling_max>
{
};

// for the purposes of feature-based dependency resolution,
// lazy_rolling_max provides the same feature as rolling_max
template<>
struct feature_of<tag::lazy_rolling_max>
: feature_of<tag::rolling_max>
{
};

}} // namespace boost::accumulators

#endif
Loading