category.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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/fwd.hpp>
  14. #include <boost/throw_exception.hpp>
  15. #include <stdexcept>
  16. #include <string>
  17. #include <type_traits>
  18. #include <utility>
  19. #include <vector>
  20. namespace boost {
  21. namespace histogram {
  22. namespace axis {
  23. /**
  24. Maps at a set of unique values to bin indices.
  25. The axis maps a set of values to bins, following the order of arguments in the
  26. constructor. The optional overflow bin for this axis counts input values that
  27. are not part of the set. Binning has O(N) complexity, but with a very small
  28. factor. For small N (the typical use case) it beats other kinds of lookup.
  29. @tparam Value input value type, must be equal-comparable.
  30. @tparam MetaData type to store meta data.
  31. @tparam Options see boost::histogram::axis::option.
  32. @tparam Allocator allocator to use for dynamic memory management.
  33. The options `underflow` and `circular` are not allowed. The options `growth`
  34. and `overflow` are mutually exclusive.
  35. */
  36. template <class Value, class MetaData, class Options, class Allocator>
  37. class category : public iterator_mixin<category<Value, MetaData, Options, Allocator>>,
  38. public metadata_base<MetaData> {
  39. using value_type = Value;
  40. using metadata_type = typename metadata_base<MetaData>::metadata_type;
  41. using options_type = detail::replace_default<Options, option::overflow_t>;
  42. using allocator_type = Allocator;
  43. using vector_type = std::vector<value_type, allocator_type>;
  44. static_assert(!options_type::test(option::underflow),
  45. "category axis cannot have underflow");
  46. static_assert(!options_type::test(option::circular),
  47. "category axis cannot be circular");
  48. static_assert(!(options_type::test(option::growth) &&
  49. options_type::test(option::overflow)),
  50. "growing category axis cannot have entries in overflow bin");
  51. public:
  52. constexpr category() = default;
  53. explicit category(allocator_type alloc) : vec_(alloc) {}
  54. /** Construct from iterator range of unique values.
  55. *
  56. * \param begin begin of category range of unique values.
  57. * \param end end of category range of unique values.
  58. * \param meta description of the axis.
  59. * \param alloc allocator instance to use.
  60. */
  61. template <class It, class = detail::requires_iterator<It>>
  62. category(It begin, It end, metadata_type meta = {}, allocator_type alloc = {})
  63. : metadata_base<MetaData>(std::move(meta)), vec_(alloc) {
  64. if (std::distance(begin, end) < 0)
  65. BOOST_THROW_EXCEPTION(
  66. std::invalid_argument("end must be reachable by incrementing begin"));
  67. vec_.reserve(std::distance(begin, end));
  68. while (begin != end) vec_.emplace_back(*begin++);
  69. }
  70. /** Construct axis from iterable sequence of unique values.
  71. *
  72. * \param iterable sequence of unique values.
  73. * \param meta description of the axis.
  74. * \param alloc allocator instance to use.
  75. */
  76. template <class C, class = detail::requires_iterable<C>>
  77. category(const C& iterable, metadata_type meta = {}, allocator_type alloc = {})
  78. : category(std::begin(iterable), std::end(iterable), std::move(meta),
  79. std::move(alloc)) {}
  80. /** Construct axis from an initializer list of unique values.
  81. *
  82. * \param list `std::initializer_list` of unique values.
  83. * \param meta description of the axis.
  84. * \param alloc allocator instance to use.
  85. */
  86. template <class U>
  87. category(std::initializer_list<U> list, metadata_type meta = {},
  88. allocator_type alloc = {})
  89. : category(list.begin(), list.end(), std::move(meta), std::move(alloc)) {}
  90. /// Return index for value argument.
  91. index_type index(const value_type& x) const noexcept {
  92. const auto beg = vec_.begin();
  93. const auto end = vec_.end();
  94. return static_cast<index_type>(std::distance(beg, std::find(beg, end, x)));
  95. }
  96. /// Returns index and shift (if axis has grown) for the passed argument.
  97. auto update(const value_type& x) {
  98. const auto i = index(x);
  99. if (i < size()) return std::make_pair(i, 0);
  100. vec_.emplace_back(x);
  101. return std::make_pair(i, -1);
  102. }
  103. /// Return value for index argument.
  104. /// Throws `std::out_of_range` if the index is out of bounds.
  105. auto value(index_type idx) const
  106. -> std::conditional_t<std::is_scalar<value_type>::value, value_type,
  107. const value_type&> {
  108. if (idx < 0 || idx >= size())
  109. BOOST_THROW_EXCEPTION(std::out_of_range("category index out of range"));
  110. return vec_[idx];
  111. }
  112. /// Return value for index argument.
  113. decltype(auto) bin(index_type idx) const noexcept { return value(idx); }
  114. /// Returns the number of bins, without over- or underflow.
  115. index_type size() const noexcept { return static_cast<index_type>(vec_.size()); }
  116. /// Returns the options.
  117. static constexpr unsigned options() noexcept { return options_type::value; }
  118. /// Whether the axis is inclusive (see axis::traits::is_inclusive).
  119. static constexpr bool inclusive() noexcept {
  120. return options() & (option::overflow | option::growth);
  121. }
  122. template <class V, class M, class O, class A>
  123. bool operator==(const category<V, M, O, A>& o) const noexcept {
  124. const auto& a = vec_;
  125. const auto& b = o.vec_;
  126. return std::equal(a.begin(), a.end(), b.begin(), b.end()) &&
  127. metadata_base<MetaData>::operator==(o);
  128. }
  129. template <class V, class M, class O, class A>
  130. bool operator!=(const category<V, M, O, A>& o) const noexcept {
  131. return !operator==(o);
  132. }
  133. auto get_allocator() const { return vec_.get_allocator(); }
  134. template <class Archive>
  135. void serialize(Archive& ar, unsigned /* version */) {
  136. ar& make_nvp("seq", vec_);
  137. ar& make_nvp("meta", this->metadata());
  138. }
  139. private:
  140. vector_type vec_;
  141. template <class V, class M, class O, class A>
  142. friend class category;
  143. };
  144. #if __cpp_deduction_guides >= 201606
  145. template <class T>
  146. category(std::initializer_list<T>)
  147. ->category<detail::replace_cstring<std::decay_t<T>>, null_type>;
  148. template <class T, class M>
  149. category(std::initializer_list<T>, M)
  150. ->category<detail::replace_cstring<std::decay_t<T>>,
  151. detail::replace_cstring<std::decay_t<M>>>;
  152. #endif
  153. } // namespace axis
  154. } // namespace histogram
  155. } // namespace boost
  156. #endif