interval_view.hpp 1.4 KB

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