1 // Copyright Hans Dembinski 2020
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)
7 #ifndef BOOST_HISTOGRAM_AXIS_BOOLEAN_HPP
8 #define BOOST_HISTOGRAM_AXIS_BOOLEAN_HPP
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>
24 Discrete axis for boolean data.
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.
29 It cannot grow and cannot be reduced.
31 @tparam MetaData type to store meta data.
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;
41 /** Construct a boolean axis.
43 * \param meta description of the axis.
45 explicit boolean(metadata_type meta = {}) : metadata_base(std::move(meta)) {}
47 /// Return index for value argument.
48 index_type index(value_type x) const noexcept { return static_cast<index_type>(x); }
50 /// Return value for index argument.
51 value_type value(index_type i) const noexcept { return static_cast<value_type>(i); }
53 /// Return bin for index argument.
54 value_type bin(index_type i) const noexcept { return value(i); }
56 /// Returns the number of bins, without over- or underflow.
57 index_type size() const noexcept { return 2; }
59 /// Whether the axis is inclusive (see axis::traits::is_inclusive).
60 static constexpr bool inclusive() noexcept { return true; }
62 /// Returns the options.
63 static constexpr unsigned options() noexcept { return option::none_t::value; }
66 bool operator==(const boolean<M>& o) const noexcept {
67 return detail::relaxed_equal{}(this->metadata(), o.metadata());
71 bool operator!=(const boolean<M>& o) const noexcept {
72 return !operator==(o);
75 template <class Archive>
76 void serialize(Archive& ar, unsigned /* version */) {
77 ar& make_nvp("meta", this->metadata());
85 #if __cpp_deduction_guides >= 201606
87 boolean()->boolean<null_type>;
90 boolean(M) -> boolean<detail::replace_type<std::decay_t<M>, const char*, std::string>>;
95 } // namespace histogram
98 #endif // BOOST_HISTOGRAM_AXIS_BOOLEAN_HPP