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>, int,
20 decltype(std::declval<Axis>().bin(0))> {
22 /// Make iterator from axis and index.
23 iterator(const Axis& axis, int idx) : iterator::iterator_adaptor_(idx), axis_(axis) {}
25 /// Return current bin object.
26 decltype(auto) operator*() const { return axis_.bin(this->base()); }
32 /// Uses CRTP to inject iterator logic into Derived.
33 template <typename Derived>
34 class iterator_mixin {
36 using const_iterator = iterator<Derived>;
37 using const_reverse_iterator = std::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);
44 /// Bin iterator to the end of the axis (read-only).
45 const_iterator end() const noexcept {
46 return const_iterator(*static_cast<const Derived*>(this),
47 static_cast<const Derived*>(this)->size());
50 /// Reverse bin iterator to the last entry of the axis (read-only).
51 const_reverse_iterator rbegin() const noexcept {
52 return std::make_reverse_iterator(end());
55 /// Reverse bin iterator to the end (read-only).
56 const_reverse_iterator rend() const noexcept {
57 return std::make_reverse_iterator(begin());
62 } // namespace histogram