iterator.hpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. using reference = typename iterator::iterator_adaptor_::reference;
  19. /// Make iterator from axis and index.
  20. iterator(const Axis& axis, index_type idx)
  21. : iterator::iterator_adaptor_(idx), axis_(axis) {}
  22. /// Return current bin object.
  23. reference operator*() const { return axis_.bin(this->base()); }
  24. private:
  25. const Axis& axis_;
  26. };
  27. /// Uses CRTP to inject iterator logic into Derived.
  28. template <class Derived>
  29. class iterator_mixin {
  30. public:
  31. using const_iterator = iterator<Derived>;
  32. using const_reverse_iterator = std::reverse_iterator<const_iterator>;
  33. /// Bin iterator to beginning of the axis (read-only).
  34. const_iterator begin() const noexcept {
  35. return const_iterator(*static_cast<const Derived*>(this), 0);
  36. }
  37. /// Bin iterator to the end of the axis (read-only).
  38. const_iterator end() const noexcept {
  39. return const_iterator(*static_cast<const Derived*>(this),
  40. static_cast<const Derived*>(this)->size());
  41. }
  42. /// Reverse bin iterator to the last entry of the axis (read-only).
  43. const_reverse_iterator rbegin() const noexcept {
  44. return std::make_reverse_iterator(end());
  45. }
  46. /// Reverse bin iterator to the end (read-only).
  47. const_reverse_iterator rend() const noexcept {
  48. return std::make_reverse_iterator(begin());
  49. }
  50. };
  51. } // namespace axis
  52. } // namespace histogram
  53. } // namespace boost
  54. #endif