interval_view.hpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright 2015-2019 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_INTERVAL_VIEW_HPP
  7. #define BOOST_HISTOGRAM_AXIS_INTERVAL_VIEW_HPP
  8. #include <boost/histogram/fwd.hpp>
  9. namespace boost {
  10. namespace histogram {
  11. namespace axis {
  12. /**
  13. Lightweight bin view.
  14. Represents the current bin interval.
  15. */
  16. template <class Axis>
  17. class interval_view {
  18. public:
  19. interval_view(const Axis& axis, index_type idx) : axis_(axis), idx_(idx) {}
  20. // avoid viewing a temporary that goes out of scope
  21. interval_view(Axis&& axis, index_type idx) = delete;
  22. /// Return lower edge of bin.
  23. decltype(auto) lower() const noexcept { return axis_.value(idx_); }
  24. /// Return upper edge of bin.
  25. decltype(auto) upper() const noexcept { return axis_.value(idx_ + 1); }
  26. /// Return center of bin.
  27. decltype(auto) center() const noexcept { return axis_.value(idx_ + 0.5); }
  28. /// Return width of bin.
  29. decltype(auto) width() const noexcept { return upper() - lower(); }
  30. template <class BinType>
  31. bool operator==(const BinType& rhs) const noexcept {
  32. return lower() == rhs.lower() && upper() == rhs.upper();
  33. }
  34. template <class BinType>
  35. bool operator!=(const BinType& rhs) const noexcept {
  36. return !operator==(rhs);
  37. }
  38. private:
  39. const Axis& axis_;
  40. const index_type idx_;
  41. };
  42. } // namespace axis
  43. } // namespace histogram
  44. } // namespace boost
  45. #endif