1 ///////////////////////////////////////////////////////////////////////////////
4 // Copyright 2005 Eric Niebler, Michael Gauckler. 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)
8 #ifndef BOOST_ACCUMULATORS_STATISTICS_TAIL_HPP_EAN_28_10_2005
9 #define BOOST_ACCUMULATORS_STATISTICS_TAIL_HPP_EAN_28_10_2005
13 #include <boost/assert.hpp>
14 #include <boost/range.hpp>
15 #include <boost/mpl/if.hpp>
16 #include <boost/mpl/or.hpp>
17 #include <boost/mpl/placeholders.hpp>
18 #include <boost/parameter/keyword.hpp>
19 #include <boost/iterator/reverse_iterator.hpp>
20 #include <boost/iterator/permutation_iterator.hpp>
21 #include <boost/accumulators/accumulators_fwd.hpp>
22 #include <boost/accumulators/framework/accumulator_base.hpp>
23 #include <boost/accumulators/framework/extractor.hpp>
24 #include <boost/accumulators/numeric/functional.hpp>
25 #include <boost/accumulators/framework/parameters/sample.hpp>
26 #include <boost/accumulators/framework/depends_on.hpp>
27 #include <boost/accumulators/statistics_fwd.hpp>
29 namespace boost { namespace accumulators
31 ///////////////////////////////////////////////////////////////////////////////
32 // cache_size named parameters
33 BOOST_PARAMETER_NESTED_KEYWORD(tag, right_tail_cache_size, cache_size)
34 BOOST_PARAMETER_NESTED_KEYWORD(tag, left_tail_cache_size, cache_size)
36 BOOST_ACCUMULATORS_IGNORE_GLOBAL(right_tail_cache_size)
37 BOOST_ACCUMULATORS_IGNORE_GLOBAL(left_tail_cache_size)
41 ///////////////////////////////////////////////////////////////////////////////
45 template<typename ElementIterator, typename IndexIterator>
48 typedef boost::iterator_range<
49 boost::reverse_iterator<boost::permutation_iterator<ElementIterator, IndexIterator> >
53 ///////////////////////////////////////////////////////////////////////////////
57 template<typename ElementIterator, typename IndexIterator>
58 typename tail_range<ElementIterator, IndexIterator>::type
59 make_tail_range(ElementIterator elem_begin, IndexIterator index_begin, IndexIterator index_end)
61 return boost::make_iterator_range(
62 boost::make_reverse_iterator(
63 boost::make_permutation_iterator(elem_begin, index_end)
65 , boost::make_reverse_iterator(
66 boost::make_permutation_iterator(elem_begin, index_begin)
71 ///////////////////////////////////////////////////////////////////////////////
72 // stat_assign_visitor
75 template<typename Args>
76 struct stat_assign_visitor
78 stat_assign_visitor(Args const &a, std::size_t i)
84 template<typename Stat>
85 void operator ()(Stat &stat) const
87 stat.assign(this->args, this->index);
91 stat_assign_visitor &operator =(stat_assign_visitor const &);
96 ///////////////////////////////////////////////////////////////////////////////
100 template<typename Args>
101 inline stat_assign_visitor<Args> const stat_assign(Args const &args, std::size_t index)
103 return stat_assign_visitor<Args>(args, index);
106 ///////////////////////////////////////////////////////////////////////////////
107 // is_tail_variate_feature
110 template<typename Stat, typename LeftRight>
111 struct is_tail_variate_feature
118 template<typename VariateType, typename VariateTag, typename LeftRight>
119 struct is_tail_variate_feature<tag::tail_variate<VariateType, VariateTag, LeftRight>, LeftRight>
126 template<typename LeftRight>
127 struct is_tail_variate_feature<tag::tail_weights<LeftRight>, LeftRight>
132 } // namespace detail
136 ///////////////////////////////////////////////////////////////////////////////
138 template<typename Sample, typename LeftRight>
142 // LeftRight must be either right or left
144 mpl::or_<is_same<LeftRight, right>, is_same<LeftRight, left> >
149 is_same<LeftRight, right>
150 , numeric::functional::greater<Sample const, Sample const>
151 , numeric::functional::less<Sample const, Sample const>
155 // for boost::result_of
156 typedef typename detail::tail_range<
157 typename std::vector<Sample>::const_iterator
158 , std::vector<std::size_t>::iterator
161 template<typename Args>
162 tail_impl(Args const &args)
165 , samples(args[tag::tail<LeftRight>::cache_size], args[sample | Sample()])
167 this->indices.reserve(this->samples.size());
170 tail_impl(tail_impl const &that)
171 : is_sorted(that.is_sorted)
172 , indices(that.indices)
173 , samples(that.samples)
175 this->indices.reserve(this->samples.size());
178 // This just stores the heap and the samples.
179 // In operator()() below, if we are adding a new sample
180 // to the sample cache, we force all the
181 // tail_variates to update also. (It's not
182 // good enough to wait for the accumulator_set to do it
183 // for us because then information about whether a sample
184 // was stored and where is lost, and would need to be
185 // queried at runtime, which would be slow.) This is
186 // implemented as a filtered visitation over the stats,
187 // which we can access because args[accumulator] gives us
190 template<typename Args>
191 void operator ()(Args const &args)
193 if(this->indices.size() < this->samples.size())
195 this->indices.push_back(this->indices.size());
196 this->assign(args, this->indices.back());
198 else if(predicate_type()(args[sample], this->samples[this->indices[0]]))
200 std::pop_heap(this->indices.begin(), this->indices.end(), indirect_cmp(this->samples));
201 this->assign(args, this->indices.back());
205 result_type result(dont_care) const
209 // Must use the same predicate here as in push_heap/pop_heap above.
210 std::sort_heap(this->indices.begin(), this->indices.end(), indirect_cmp(this->samples));
211 // sort_heap puts elements in reverse order. Calling std::reverse
212 // turns the sorted sequence back into a valid heap.
213 std::reverse(this->indices.begin(), this->indices.end());
214 this->is_sorted = true;
217 return detail::make_tail_range(
218 this->samples.begin()
219 , this->indices.begin()
220 , this->indices.end()
226 struct is_tail_variate
230 : detail::is_tail_variate_feature<
231 typename detail::feature_tag<T>::type
237 template<typename Args>
238 void assign(Args const &args, std::size_t index)
240 BOOST_ASSERT(index < this->samples.size());
241 this->samples[index] = args[sample];
242 std::push_heap(this->indices.begin(), this->indices.end(), indirect_cmp(this->samples));
243 this->is_sorted = false;
244 // Tell the tail variates to store their values also
245 args[accumulator].template visit_if<is_tail_variate>(detail::stat_assign(args, index));
248 ///////////////////////////////////////////////////////////////////////////////
251 : std::binary_function<std::size_t, std::size_t, bool>
253 indirect_cmp(std::vector<Sample> const &s)
258 bool operator ()(std::size_t left, std::size_t right) const
260 return predicate_type()(this->samples[left], this->samples[right]);
264 indirect_cmp &operator =(indirect_cmp const &);
265 std::vector<Sample> const &samples;
268 mutable bool is_sorted;
269 mutable std::vector<std::size_t> indices;
270 std::vector<Sample> samples;
275 // TODO The templatized tag::tail below should inherit from the correct named parameter.
276 // The following lines provide a workaround, but there must be a better way of doing this.
278 struct tail_cache_size_named_arg
282 struct tail_cache_size_named_arg<left>
283 : tag::left_tail_cache_size
287 struct tail_cache_size_named_arg<right>
288 : tag::right_tail_cache_size
292 ///////////////////////////////////////////////////////////////////////////////
297 template<typename LeftRight>
300 , tail_cache_size_named_arg<LeftRight>
304 typedef accumulators::impl::tail_impl<mpl::_1, LeftRight> impl;
306 #ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
307 /// tag::tail<LeftRight>::cache_size named parameter
308 static boost::parameter::keyword<tail_cache_size_named_arg<LeftRight> > const cache_size;
318 ///////////////////////////////////////////////////////////////////////////////
323 extractor<tag::abstract_tail> const tail = {};
325 BOOST_ACCUMULATORS_IGNORE_GLOBAL(tail)
330 template<typename LeftRight>
331 struct feature_of<tag::tail<LeftRight> >
332 : feature_of<tag::abstract_tail>
336 }} // namespace boost::accumulators