iterator.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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/meta.hpp>
  10. #include <boost/iterator/iterator_adaptor.hpp>
  11. #include <boost/iterator/reverse_iterator.hpp>
  12. namespace boost {
  13. namespace histogram {
  14. namespace axis {
  15. template <typename Axis>
  16. class iterator
  17. : public boost::iterator_adaptor<iterator<Axis>, int,
  18. decltype(std::declval<const Axis&>().bin(0)),
  19. std::random_access_iterator_tag,
  20. decltype(std::declval<const Axis&>().bin(0)), int> {
  21. public:
  22. explicit iterator(const Axis& axis, int idx)
  23. : iterator::iterator_adaptor_(idx), axis_(axis) {}
  24. protected:
  25. bool equal(const iterator& other) const noexcept {
  26. return &axis_ == &other.axis_ && this->base() == other.base();
  27. }
  28. decltype(auto) dereference() const { return axis_.bin(this->base()); }
  29. private:
  30. const Axis& axis_;
  31. friend class boost::iterator_core_access;
  32. };
  33. /// Uses CRTP to inject iterator logic into Derived.
  34. template <typename Derived>
  35. class iterator_mixin {
  36. public:
  37. using const_iterator = iterator<Derived>;
  38. using const_reverse_iterator = boost::reverse_iterator<const_iterator>;
  39. /// Bin iterator to beginning of the axis (read-only).
  40. const_iterator begin() const noexcept {
  41. return const_iterator(*static_cast<const Derived*>(this), 0);
  42. }
  43. /// Bin iterator to the end of the axis (read-only).
  44. const_iterator end() const noexcept {
  45. return const_iterator(*static_cast<const Derived*>(this),
  46. static_cast<const Derived*>(this)->size());
  47. }
  48. /// Reverse bin iterator to the last entry of the axis (read-only).
  49. const_reverse_iterator rbegin() const noexcept {
  50. return boost::make_reverse_iterator(end());
  51. }
  52. /// Reverse bin iterator to the end (read-only).
  53. const_reverse_iterator rend() const noexcept {
  54. return boost::make_reverse_iterator(begin());
  55. }
  56. };
  57. } // namespace axis
  58. } // namespace histogram
  59. } // namespace boost
  60. #endif