Boost logo

Boost :

From: Tom Brinkman (reportbase_at_[hidden])
Date: 2007-07-23 00:57:58


I've modified the "simple_moving_average_functor" according to your
suggestions of using the boost::circular_buffer container. (see
below)

I'm still unclear about how to incorporate the offsets, "start" and
"stop". It might be easier if you illustrate this by modifying the
function "operator()". I'm not shur how to proceed.

Also, what should I do with the "finalize()" function. Its required
that I implement this part of the interface, but its not needed to
calculate the moving_average. Yet, it requires me to return a value.
Which value should I return?

template<typename out_t>
struct simple_moving_average_functor
{
        double total;
        out_t out;
        boost::circular_buffer<double> buffer;
        
        simple_moving_average_functor(const out_t& out, std::size_t periods) :
                out(out), total(0), buffer(0)
        {
                //BUG: a bug in circular_buffer causes me to resize the
                //buffer here in the constructor, after initializing it to zero.
                BOOST_ASSERT(periods > 0);
                buffer.resize(periods);
        }

        template <typename value_t, typename offset_t>
        void operator ()(const value_t& value, offset_t start, offset_t stop)
        {
                if (buffer.full())
                {
                        total -= buffer[0];
                        total += value;
                        out(total/buffer.size(), start,stop);
                }
                else
                {
                        total += value;
                }

                buffer.push_back(value);
        }

        out_t const &finalize()
        {
        }
};


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk