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