boolean.hpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. *
  36. * \param meta description of the axis.
  37. */
  38. explicit boolean(metadata_type meta = {}) : metadata_base(std::move(meta)) {}
  39. /// Return index for value argument.
  40. index_type index(value_type x) const noexcept { return static_cast<index_type>(x); }
  41. /// Return value for index argument.
  42. value_type value(index_type i) const noexcept { return static_cast<value_type>(i); }
  43. /// Return bin for index argument.
  44. value_type bin(index_type i) const noexcept { return value(i); }
  45. /// Returns the number of bins, without over- or underflow.
  46. index_type size() const noexcept { return 2; }
  47. /// Whether the axis is inclusive (see axis::traits::is_inclusive).
  48. static constexpr bool inclusive() noexcept { return true; }
  49. /// Returns the options.
  50. static constexpr unsigned options() noexcept { return option::none_t::value; }
  51. template <class M>
  52. bool operator==(const boolean<M>& o) const noexcept {
  53. return detail::relaxed_equal{}(this->metadata(), o.metadata());
  54. }
  55. template <class M>
  56. bool operator!=(const boolean<M>& o) const noexcept {
  57. return !operator==(o);
  58. }
  59. template <class Archive>
  60. void serialize(Archive& ar, unsigned /* version */) {
  61. ar& make_nvp("meta", this->metadata());
  62. }
  63. private:
  64. template <class M>
  65. friend class boolean;
  66. };
  67. #if __cpp_deduction_guides >= 201606
  68. boolean()->boolean<null_type>;
  69. template <class M>
  70. boolean(M) -> boolean<detail::replace_type<std::decay_t<M>, const char*, std::string>>;
  71. #endif
  72. } // namespace axis
  73. } // namespace histogram
  74. } // namespace boost
  75. #endif // BOOST_HISTOGRAM_AXIS_BOOLEAN_HPP