iterator.hpp 1.9 KB

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