boolean.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright Hans Dembinski 2020
  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_BOOLEAN_HPP
  7. #define BOOST_HISTOGRAM_AXIS_BOOLEAN_HPP
  8. #include <boost/core/nvp.hpp>
  9. #include <boost/histogram/axis/iterator.hpp>
  10. #include <boost/histogram/axis/metadata_base.hpp>
  11. #include <boost/histogram/axis/option.hpp>
  12. #include <boost/histogram/detail/relaxed_equal.hpp>
  13. #include <boost/histogram/detail/replace_type.hpp>
  14. #include <boost/histogram/fwd.hpp>
  15. #include <string>
  16. namespace boost {
  17. namespace histogram {
  18. namespace axis {
  19. /**
  20. Discrete axis for boolean data.
  21. Binning is a pass-though operation with zero cost, making this the
  22. fastest possible axis. The axis has no internal state apart from the
  23. optional metadata state. The axis has no under- and overflow bins.
  24. It cannot grow and cannot be reduced.
  25. @tparam MetaData type to store meta data.
  26. */
  27. template <class MetaData>
  28. class boolean : public iterator_mixin<boolean<MetaData>>,
  29. public metadata_base_t<MetaData> {
  30. using value_type = bool;
  31. using metadata_base = metadata_base_t<MetaData>;
  32. using metadata_type = typename metadata_base::metadata_type;
  33. public:
  34. /** Construct a boolean axis.
  35. @param meta description of the axis.
  36. The constructor is nothrow if meta is nothrow move constructible.
  37. */
  38. explicit boolean(metadata_type meta = {}) noexcept(
  39. std::is_nothrow_move_constructible<metadata_type>::value)
  40. : metadata_base(std::move(meta)) {}
  41. /// Return index for value argument.
  42. index_type index(value_type x) const noexcept { return static_cast<index_type>(x); }
  43. /// Return value for index argument.
  44. value_type value(index_type i) const noexcept { return static_cast<value_type>(i); }
  45. /// Return bin for index argument.
  46. value_type bin(index_type i) const noexcept { return value(i); }
  47. /// Returns the number of bins, without over- or underflow.
  48. index_type size() const noexcept { return 2; }
  49. /// Whether the axis is inclusive (see axis::traits::is_inclusive).
  50. static constexpr bool inclusive() noexcept { return true; }
  51. /// Returns the options.
  52. static constexpr unsigned options() noexcept { return option::none_t::value; }
  53. template <class M>
  54. bool operator==(const boolean<M>& o) const noexcept {
  55. return detail::relaxed_equal{}(this->metadata(), o.metadata());
  56. }
  57. template <class M>
  58. bool operator!=(const boolean<M>& o) const noexcept {
  59. return !operator==(o);
  60. }
  61. template <class Archive>
  62. void serialize(Archive& ar, unsigned /* version */) {
  63. ar& make_nvp("meta", this->metadata());
  64. }
  65. private:
  66. template <class M>
  67. friend class boolean;
  68. };
  69. #if __cpp_deduction_guides >= 201606
  70. boolean()->boolean<null_type>;
  71. template <class M>
  72. boolean(M) -> boolean<detail::replace_type<std::decay_t<M>, const char*, std::string>>;
  73. #endif
  74. } // namespace axis
  75. } // namespace histogram
  76. } // namespace boost
  77. #endif // BOOST_HISTOGRAM_AXIS_BOOLEAN_HPP