category.hpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // Copyright 2015-2018 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_CATEGORY_HPP
  7. #define BOOST_HISTOGRAM_AXIS_CATEGORY_HPP
  8. #include <algorithm>
  9. #include <boost/core/nvp.hpp>
  10. #include <boost/histogram/axis/iterator.hpp>
  11. #include <boost/histogram/axis/metadata_base.hpp>
  12. #include <boost/histogram/axis/option.hpp>
  13. #include <boost/histogram/detail/detect.hpp>
  14. #include <boost/histogram/detail/relaxed_equal.hpp>
  15. #include <boost/histogram/fwd.hpp>
  16. #include <boost/throw_exception.hpp>
  17. #include <stdexcept>
  18. #include <string>
  19. #include <type_traits>
  20. #include <utility>
  21. #include <vector>
  22. namespace boost {
  23. namespace histogram {
  24. namespace axis {
  25. /** Maps at a set of unique values to bin indices.
  26. The axis maps a set of values to bins, following the order of arguments in the
  27. constructor. The optional overflow bin for this axis counts input values that
  28. are not part of the set. Binning has O(N) complexity, but with a very small
  29. factor. For small N (the typical use case) it beats other kinds of lookup.
  30. @tparam Value input value type, must be equal-comparable.
  31. @tparam MetaData type to store meta data.
  32. @tparam Options see boost::histogram::axis::option.
  33. @tparam Allocator allocator to use for dynamic memory management.
  34. The options `underflow` and `circular` are not allowed. The options `growth`
  35. and `overflow` are mutually exclusive.
  36. */
  37. template <class Value, class MetaData, class Options, class Allocator>
  38. class category : public iterator_mixin<category<Value, MetaData, Options, Allocator>>,
  39. public metadata_base_t<MetaData> {
  40. // these must be private, so that they are not automatically inherited
  41. using value_type = Value;
  42. using metadata_base = metadata_base_t<MetaData>;
  43. using metadata_type = typename metadata_base::metadata_type;
  44. using options_type = detail::replace_default<Options, option::overflow_t>;
  45. using allocator_type = Allocator;
  46. using vector_type = std::vector<value_type, allocator_type>;
  47. public:
  48. constexpr category() = default;
  49. explicit category(allocator_type alloc) : vec_(alloc) {}
  50. /** Construct from forward iterator range of unique values.
  51. @param begin begin of category range of unique values.
  52. @param end end of category range of unique values.
  53. @param meta description of the axis (optional).
  54. @param options see boost::histogram::axis::option (optional).
  55. @param alloc allocator instance to use (optional).
  56. The constructor throws `std::invalid_argument` if iterator range is invalid. If the
  57. range contains duplicated values, the behavior of the axis is undefined.
  58. The arguments meta and alloc are passed by value. If you move either of them into the
  59. axis and the constructor throws, their values are lost. Do not move if you cannot
  60. guarantee that the bin description is not valid.
  61. */
  62. template <class It, class = detail::requires_iterator<It>>
  63. category(It begin, It end, metadata_type meta = {}, options_type options = {},
  64. allocator_type alloc = {})
  65. : metadata_base(std::move(meta)), vec_(alloc) {
  66. // static_asserts were moved here from class scope to satisfy deduction in gcc>=11
  67. static_assert(!options.test(option::underflow),
  68. "category axis cannot have underflow");
  69. static_assert(!options.test(option::circular), "category axis cannot be circular");
  70. static_assert(!(options.test(option::growth) && options.test(option::overflow)),
  71. "growing category axis cannot have entries in overflow bin");
  72. if (std::distance(begin, end) < 0)
  73. BOOST_THROW_EXCEPTION(
  74. std::invalid_argument("end must be reachable by incrementing begin"));
  75. vec_.reserve(std::distance(begin, end));
  76. while (begin != end) vec_.emplace_back(*begin++);
  77. }
  78. // kept for backward compatibility; requires_allocator is a workaround for deduction
  79. // guides in gcc>=11
  80. template <class It, class A, class = detail::requires_iterator<It>,
  81. class = detail::requires_allocator<A>>
  82. category(It begin, It end, metadata_type meta, A alloc)
  83. : category(begin, end, std::move(meta), {}, std::move(alloc)) {}
  84. /** Construct axis from iterable sequence of unique values.
  85. @param iterable sequence of unique values.
  86. @param meta description of the axis.
  87. @param options see boost::histogram::axis::option (optional).
  88. @param alloc allocator instance to use.
  89. */
  90. template <class C, class = detail::requires_iterable<C>>
  91. category(const C& iterable, metadata_type meta = {}, options_type options = {},
  92. allocator_type alloc = {})
  93. : category(std::begin(iterable), std::end(iterable), std::move(meta), options,
  94. std::move(alloc)) {}
  95. // kept for backward compatibility; requires_allocator is a workaround for deduction
  96. // guides in gcc>=11
  97. template <class C, class A, class = detail::requires_iterable<C>,
  98. class = detail::requires_allocator<A>>
  99. category(const C& iterable, metadata_type meta, A alloc)
  100. : category(std::begin(iterable), std::end(iterable), std::move(meta), {},
  101. std::move(alloc)) {}
  102. /** Construct axis from an initializer list of unique values.
  103. @param list `std::initializer_list` of unique values.
  104. @param meta description of the axis.
  105. @param options see boost::histogram::axis::option (optional).
  106. @param alloc allocator instance to use.
  107. */
  108. template <class U>
  109. category(std::initializer_list<U> list, metadata_type meta = {},
  110. options_type options = {}, allocator_type alloc = {})
  111. : category(list.begin(), list.end(), std::move(meta), options, std::move(alloc)) {}
  112. // kept for backward compatibility; requires_allocator is a workaround for deduction
  113. // guides in gcc>=11
  114. template <class U, class A, class = detail::requires_allocator<A>>
  115. category(std::initializer_list<U> list, metadata_type meta, A alloc)
  116. : category(list.begin(), list.end(), std::move(meta), {}, std::move(alloc)) {}
  117. /// Constructor used by algorithm::reduce to shrink and rebin (not for users).
  118. category(const category& src, index_type begin, index_type end, unsigned merge)
  119. // LCOV_EXCL_START: gcc-8 is missing the delegated ctor for no reason
  120. : category(src.vec_.begin() + begin, src.vec_.begin() + end, src.metadata(), {},
  121. src.get_allocator())
  122. // LCOV_EXCL_STOP
  123. {
  124. if (merge > 1)
  125. BOOST_THROW_EXCEPTION(std::invalid_argument("cannot merge bins for category axis"));
  126. }
  127. /// Return index for value argument.
  128. index_type index(const value_type& x) const noexcept {
  129. const auto beg = vec_.begin();
  130. const auto end = vec_.end();
  131. return static_cast<index_type>(std::distance(beg, std::find(beg, end, x)));
  132. }
  133. /// Returns index and shift (if axis has grown) for the passed argument.
  134. std::pair<index_type, index_type> update(const value_type& x) {
  135. const auto i = index(x);
  136. if (i < size()) return {i, 0};
  137. vec_.emplace_back(x);
  138. return {i, -1};
  139. }
  140. /// Return value for index argument.
  141. /// Throws `std::out_of_range` if the index is out of bounds.
  142. auto value(index_type idx) const
  143. -> std::conditional_t<std::is_scalar<value_type>::value, value_type,
  144. const value_type&> {
  145. if (idx < 0 || idx >= size())
  146. BOOST_THROW_EXCEPTION(std::out_of_range("category index out of range"));
  147. return vec_[idx];
  148. }
  149. /// Return value for index argument; alias for value(...).
  150. decltype(auto) bin(index_type idx) const { return value(idx); }
  151. /// Returns the number of bins, without over- or underflow.
  152. index_type size() const noexcept { return static_cast<index_type>(vec_.size()); }
  153. /// Returns the options.
  154. static constexpr unsigned options() noexcept { return options_type::value; }
  155. /// Whether the axis is inclusive (see axis::traits::is_inclusive).
  156. static constexpr bool inclusive() noexcept {
  157. return options() & (option::overflow | option::growth);
  158. }
  159. /// Indicate that the axis is not ordered.
  160. static constexpr bool ordered() noexcept { return false; }
  161. template <class V, class M, class O, class A>
  162. bool operator==(const category<V, M, O, A>& o) const noexcept {
  163. const auto& a = vec_;
  164. const auto& b = o.vec_;
  165. return std::equal(a.begin(), a.end(), b.begin(), b.end(), detail::relaxed_equal{}) &&
  166. detail::relaxed_equal{}(this->metadata(), o.metadata());
  167. }
  168. template <class V, class M, class O, class A>
  169. bool operator!=(const category<V, M, O, A>& o) const noexcept {
  170. return !operator==(o);
  171. }
  172. allocator_type get_allocator() const { return vec_.get_allocator(); }
  173. template <class Archive>
  174. void serialize(Archive& ar, unsigned /* version */) {
  175. ar& make_nvp("seq", vec_);
  176. ar& make_nvp("meta", this->metadata());
  177. }
  178. private:
  179. vector_type vec_;
  180. template <class V, class M, class O, class A>
  181. friend class category;
  182. };
  183. #if __cpp_deduction_guides >= 201606
  184. template <class T>
  185. category(std::initializer_list<T>)
  186. -> category<detail::replace_cstring<std::decay_t<T>>, null_type>;
  187. template <class T, class M>
  188. category(std::initializer_list<T>, M)
  189. -> category<detail::replace_cstring<std::decay_t<T>>,
  190. detail::replace_cstring<std::decay_t<M>>>;
  191. template <class T, class M, unsigned B>
  192. category(std::initializer_list<T>, M, const option::bitset<B>&)
  193. -> category<detail::replace_cstring<std::decay_t<T>>,
  194. detail::replace_cstring<std::decay_t<M>>, option::bitset<B>>;
  195. #endif
  196. } // namespace axis
  197. } // namespace histogram
  198. } // namespace boost
  199. #endif