• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
7 #ifndef BOOST_HISTOGRAM_AXIS_ITERATOR_HPP
8 #define BOOST_HISTOGRAM_AXIS_ITERATOR_HPP
9 
10 #include <boost/histogram/axis/interval_view.hpp>
11 #include <boost/histogram/detail/iterator_adaptor.hpp>
12 #include <iterator>
13 
14 namespace boost {
15 namespace histogram {
16 namespace axis {
17 
18 template <class Axis>
19 class iterator : public detail::iterator_adaptor<iterator<Axis>, index_type,
20                                                  decltype(std::declval<Axis>().bin(0))> {
21 public:
22   /// Make iterator from axis and index.
iterator(const Axis & axis,index_type idx)23   iterator(const Axis& axis, index_type idx)
24       : iterator::iterator_adaptor_(idx), axis_(axis) {}
25 
26   /// Return current bin object.
operator *() const27   decltype(auto) operator*() const { return axis_.bin(this->base()); }
28 
29 private:
30   const Axis& axis_;
31 };
32 
33 /// Uses CRTP to inject iterator logic into Derived.
34 template <class Derived>
35 class iterator_mixin {
36 public:
37   using const_iterator = iterator<Derived>;
38   using const_reverse_iterator = std::reverse_iterator<const_iterator>;
39 
40   /// Bin iterator to beginning of the axis (read-only).
begin() const41   const_iterator begin() const noexcept {
42     return const_iterator(*static_cast<const Derived*>(this), 0);
43   }
44 
45   /// Bin iterator to the end of the axis (read-only).
end() const46   const_iterator end() const noexcept {
47     return const_iterator(*static_cast<const Derived*>(this),
48                           static_cast<const Derived*>(this)->size());
49   }
50 
51   /// Reverse bin iterator to the last entry of the axis (read-only).
rbegin() const52   const_reverse_iterator rbegin() const noexcept {
53     return std::make_reverse_iterator(end());
54   }
55 
56   /// Reverse bin iterator to the end (read-only).
rend() const57   const_reverse_iterator rend() const noexcept {
58     return std::make_reverse_iterator(begin());
59   }
60 };
61 
62 } // namespace axis
63 } // namespace histogram
64 } // namespace boost
65 
66 #endif
67