tail_variate.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // tail_variate.hpp
  3. //
  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)
  7. #ifndef BOOST_STAT_STATISTICS_TAIL_VARIATE_HPP_EAN_28_10_2005
  8. #define BOOST_STAT_STATISTICS_TAIL_VARIATE_HPP_EAN_28_10_2005
  9. #include <boost/range.hpp>
  10. #include <boost/mpl/always.hpp>
  11. #include <boost/mpl/placeholders.hpp>
  12. #include <boost/iterator/reverse_iterator.hpp>
  13. #include <boost/iterator/permutation_iterator.hpp>
  14. #include <boost/accumulators/framework/accumulator_base.hpp>
  15. #include <boost/accumulators/framework/extractor.hpp>
  16. #include <boost/accumulators/framework/depends_on.hpp>
  17. #include <boost/accumulators/statistics_fwd.hpp>
  18. #include <boost/accumulators/statistics/tail.hpp>
  19. #include <boost/serialization/vector.hpp>
  20. namespace boost { namespace accumulators
  21. {
  22. namespace impl
  23. {
  24. ///////////////////////////////////////////////////////////////////////////////
  25. // tail_variate_impl
  26. template<typename VariateType, typename VariateTag, typename LeftRight>
  27. struct tail_variate_impl
  28. : accumulator_base
  29. {
  30. // for boost::result_of
  31. typedef
  32. typename detail::tail_range<
  33. typename std::vector<VariateType>::const_iterator
  34. , std::vector<std::size_t>::iterator
  35. >::type
  36. result_type;
  37. template<typename Args>
  38. tail_variate_impl(Args const &args)
  39. : variates(args[tag::tail<LeftRight>::cache_size], args[parameter::keyword<VariateTag>::get() | VariateType()])
  40. {
  41. }
  42. template<typename Args>
  43. void assign(Args const &args, std::size_t index)
  44. {
  45. this->variates[index] = args[parameter::keyword<VariateTag>::get()];
  46. }
  47. template<typename Args>
  48. result_type result(Args const &args) const
  49. {
  50. // getting the order result causes the indices vector to be sorted.
  51. extractor<tag::tail<LeftRight> > const some_tail = {};
  52. return this->do_result(some_tail(args));
  53. }
  54. private:
  55. template<typename TailRng>
  56. result_type do_result(TailRng const &rng) const
  57. {
  58. return detail::make_tail_range(
  59. this->variates.begin()
  60. , rng.end().base().base() // the index iterator
  61. , rng.begin().base().base() // (begin and end reversed because these are reverse iterators)
  62. );
  63. }
  64. // make this accumulator serializeable
  65. template<class Archive>
  66. void serialize(Archive & ar, const unsigned int file_version)
  67. {
  68. ar & variates;
  69. }
  70. private:
  71. std::vector<VariateType> variates;
  72. };
  73. } // namespace impl
  74. ///////////////////////////////////////////////////////////////////////////////
  75. // tag::tail_variate<>
  76. //
  77. namespace tag
  78. {
  79. template<typename VariateType, typename VariateTag, typename LeftRight>
  80. struct tail_variate
  81. : depends_on<tail<LeftRight> >
  82. {
  83. /// INTERNAL ONLY
  84. ///
  85. typedef mpl::always<accumulators::impl::tail_variate_impl<VariateType, VariateTag, LeftRight> > impl;
  86. };
  87. struct abstract_tail_variate
  88. : depends_on<>
  89. {
  90. };
  91. template<typename LeftRight>
  92. struct tail_weights
  93. : depends_on<tail<LeftRight> >
  94. {
  95. /// INTERNAL ONLY
  96. ///
  97. typedef accumulators::impl::tail_variate_impl<mpl::_2, tag::weight, LeftRight> impl;
  98. };
  99. struct abstract_tail_weights
  100. : depends_on<>
  101. {
  102. };
  103. }
  104. ///////////////////////////////////////////////////////////////////////////////
  105. // extract::tail_variate
  106. // extract::tail_weights
  107. //
  108. namespace extract
  109. {
  110. extractor<tag::abstract_tail_variate> const tail_variate = {};
  111. extractor<tag::abstract_tail_weights> const tail_weights = {};
  112. BOOST_ACCUMULATORS_IGNORE_GLOBAL(tail_variate)
  113. BOOST_ACCUMULATORS_IGNORE_GLOBAL(tail_weights)
  114. }
  115. using extract::tail_variate;
  116. using extract::tail_weights;
  117. template<typename VariateType, typename VariateTag, typename LeftRight>
  118. struct feature_of<tag::tail_variate<VariateType, VariateTag, LeftRight> >
  119. : feature_of<tag::abstract_tail_variate>
  120. {
  121. };
  122. template<typename LeftRight>
  123. struct feature_of<tag::tail_weights<LeftRight> >
  124. {
  125. typedef tag::abstract_tail_weights type;
  126. };
  127. }} // namespace boost::accumulators
  128. #endif