1 // Copyright 2015-2017 Hans Dembinski
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)
7 #ifndef BOOST_HISTOGRAM_AXIS_ITERATOR_HPP
8 #define BOOST_HISTOGRAM_AXIS_ITERATOR_HPP
10 #include <boost/histogram/axis/interval_view.hpp>
11 #include <boost/histogram/detail/iterator_adaptor.hpp>
19 class iterator : public detail::iterator_adaptor<iterator<Axis>, index_type,
20 decltype(std::declval<Axis>().bin(0))> {
22 /// Make iterator from axis and index.
23 iterator(const Axis& axis, index_type idx)
24 : iterator::iterator_adaptor_(idx), axis_(axis) {}
26 /// Return current bin object.
27 decltype(auto) operator*() const { return axis_.bin(this->base()); }
33 /// Uses CRTP to inject iterator logic into Derived.
34 template <class Derived>
35 class iterator_mixin {
37 using const_iterator = iterator<Derived>;
38 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
40 /// Bin iterator to beginning of the axis (read-only).
41 const_iterator begin() const noexcept {
42 return const_iterator(*static_cast<const Derived*>(this), 0);
45 /// Bin iterator to the end of the axis (read-only).
46 const_iterator end() const noexcept {
47 return const_iterator(*static_cast<const Derived*>(this),
48 static_cast<const Derived*>(this)->size());
51 /// Reverse bin iterator to the last entry of the axis (read-only).
52 const_reverse_iterator rbegin() const noexcept {
53 return std::make_reverse_iterator(end());
56 /// Reverse bin iterator to the end (read-only).
57 const_reverse_iterator rend() const noexcept {
58 return std::make_reverse_iterator(begin());
63 } // namespace histogram