iterator.hpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2015-2017 Hans Dembinski
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt
  5. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_HISTOGRAM_AXIS_ITERATOR_HPP
  7. #define BOOST_HISTOGRAM_AXIS_ITERATOR_HPP
  8. #include <boost/histogram/axis/interval_view.hpp>
  9. #include <boost/histogram/detail/iterator_adaptor.hpp>
  10. #include <iterator>
  11. namespace boost {
  12. namespace histogram {
  13. namespace axis {
  14. template <class Axis>
  15. class iterator : public detail::iterator_adaptor<iterator<Axis>, index_type,
  16. decltype(std::declval<Axis>().bin(0))> {
  17. public:
  18. /// Make iterator from axis and index.
  19. iterator(const Axis& axis, index_type idx)
  20. : iterator::iterator_adaptor_(idx), axis_(axis) {}
  21. /// Return current bin object.
  22. decltype(auto) operator*() const { return axis_.bin(this->base()); }
  23. private:
  24. const Axis& axis_;
  25. };
  26. /// Uses CRTP to inject iterator logic into Derived.
  27. template <class Derived>
  28. class iterator_mixin {
  29. public:
  30. using const_iterator = iterator<Derived>;
  31. using const_reverse_iterator = std::reverse_iterator<const_iterator>;
  32. /// Bin iterator to beginning of the axis (read-only).
  33. const_iterator begin() const noexcept {
  34. return const_iterator(*static_cast<const Derived*>(this), 0);
  35. }
  36. /// Bin iterator to the end of the axis (read-only).
  37. const_iterator end() const noexcept {
  38. return const_iterator(*static_cast<const Derived*>(this),
  39. static_cast<const Derived*>(this)->size());
  40. }
  41. /// Reverse bin iterator to the last entry of the axis (read-only).
  42. const_reverse_iterator rbegin() const noexcept {
  43. return std::make_reverse_iterator(end());
  44. }
  45. /// Reverse bin iterator to the end (read-only).
  46. const_reverse_iterator rend() const noexcept {
  47. return std::make_reverse_iterator(begin());
  48. }
  49. };
  50. } // namespace axis
  51. } // namespace histogram
  52. } // namespace boost
  53. #endif