integer.hpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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_INTEGER_HPP
  7. #define BOOST_HISTOGRAM_AXIS_INTEGER_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/convert_integer.hpp>
  13. #include <boost/histogram/detail/limits.hpp>
  14. #include <boost/histogram/detail/relaxed_equal.hpp>
  15. #include <boost/histogram/detail/replace_type.hpp>
  16. #include <boost/histogram/detail/static_if.hpp>
  17. #include <boost/histogram/fwd.hpp>
  18. #include <boost/throw_exception.hpp>
  19. #include <cmath>
  20. #include <limits>
  21. #include <stdexcept>
  22. #include <string>
  23. #include <type_traits>
  24. #include <utility>
  25. namespace boost {
  26. namespace histogram {
  27. namespace axis {
  28. /**
  29. Axis for an interval of integer values with unit steps.
  30. Binning is a O(1) operation. This axis bins faster than a regular axis.
  31. @tparam Value input value type. Must be integer or floating point.
  32. @tparam MetaData type to store meta data.
  33. @tparam Options see boost::histogram::axis::option (all values allowed).
  34. */
  35. template <class Value, class MetaData, class Options>
  36. class integer : public iterator_mixin<integer<Value, MetaData, Options>>,
  37. public metadata_base_t<MetaData> {
  38. // these must be private, so that they are not automatically inherited
  39. using value_type = Value;
  40. using metadata_base = metadata_base_t<MetaData>;
  41. using metadata_type = typename metadata_base::metadata_type;
  42. using options_type =
  43. detail::replace_default<Options, decltype(option::underflow | option::overflow)>;
  44. static_assert(std::is_integral<value_type>::value ||
  45. std::is_floating_point<value_type>::value,
  46. "integer axis requires floating point or integral type");
  47. static_assert(!options_type::test(option::circular | option::growth) ||
  48. (options_type::test(option::circular) ^
  49. options_type::test(option::growth)),
  50. "circular and growth options are mutually exclusive");
  51. static_assert(std::is_floating_point<value_type>::value ||
  52. (!options_type::test(option::circular) &&
  53. !options_type::test(option::growth)) ||
  54. (!options_type::test(option::overflow) &&
  55. !options_type::test(option::underflow)),
  56. "circular or growing integer axis with integral type "
  57. "cannot have entries in underflow or overflow bins");
  58. using local_index_type = std::conditional_t<std::is_integral<value_type>::value,
  59. index_type, real_index_type>;
  60. public:
  61. constexpr integer() = default;
  62. /** Construct over semi-open integer interval [start, stop).
  63. *
  64. * \param start first integer of covered range.
  65. * \param stop one past last integer of covered range.
  66. * \param meta description of the axis.
  67. */
  68. integer(value_type start, value_type stop, metadata_type meta = {})
  69. : metadata_base(std::move(meta))
  70. , size_(static_cast<index_type>(stop - start))
  71. , min_(start) {
  72. if (!(stop >= start))
  73. BOOST_THROW_EXCEPTION(std::invalid_argument("stop >= start required"));
  74. }
  75. /// Constructor used by algorithm::reduce to shrink and rebin.
  76. integer(const integer& src, index_type begin, index_type end, unsigned merge)
  77. : integer(src.value(begin), src.value(end), src.metadata()) {
  78. if (merge > 1)
  79. BOOST_THROW_EXCEPTION(std::invalid_argument("cannot merge bins for integer axis"));
  80. if (options_type::test(option::circular) && !(begin == 0 && end == src.size()))
  81. BOOST_THROW_EXCEPTION(std::invalid_argument("cannot shrink circular axis"));
  82. }
  83. /// Return index for value argument.
  84. index_type index(value_type x) const noexcept {
  85. return index_impl(options_type::test(axis::option::circular),
  86. std::is_floating_point<value_type>{},
  87. static_cast<double>(x - min_));
  88. }
  89. /// Returns index and shift (if axis has grown) for the passed argument.
  90. auto update(value_type x) noexcept {
  91. auto impl = [this](long x) -> std::pair<index_type, index_type> {
  92. const auto i = x - min_;
  93. if (i >= 0) {
  94. const auto k = static_cast<axis::index_type>(i);
  95. if (k < size()) return {k, 0};
  96. const auto n = k - size() + 1;
  97. size_ += n;
  98. return {k, -n};
  99. }
  100. const auto k = static_cast<axis::index_type>(
  101. detail::static_if<std::is_floating_point<value_type>>(
  102. [](auto x) { return std::floor(x); }, [](auto x) { return x; }, i));
  103. min_ += k;
  104. size_ -= k;
  105. return {0, -k};
  106. };
  107. return detail::static_if<std::is_floating_point<value_type>>(
  108. [this, impl](auto x) -> std::pair<index_type, index_type> {
  109. if (std::isfinite(x)) return impl(static_cast<long>(std::floor(x)));
  110. return {x < 0 ? -1 : this->size(), 0};
  111. },
  112. impl, x);
  113. }
  114. /// Return value for index argument.
  115. value_type value(local_index_type i) const noexcept {
  116. if (!options_type::test(option::circular) &&
  117. std::is_floating_point<value_type>::value) {
  118. if (i < 0) return detail::lowest<value_type>();
  119. if (i > size()) return detail::highest<value_type>();
  120. }
  121. return min_ + i;
  122. }
  123. /// Return bin for index argument.
  124. decltype(auto) bin(index_type idx) const noexcept {
  125. return detail::static_if<std::is_floating_point<value_type>>(
  126. [this](auto idx) { return interval_view<integer>(*this, idx); },
  127. [this](auto idx) { return this->value(idx); }, idx);
  128. }
  129. /// Returns the number of bins, without over- or underflow.
  130. index_type size() const noexcept { return size_; }
  131. /// Returns the options.
  132. static constexpr unsigned options() noexcept { return options_type::value; }
  133. /// Whether the axis is inclusive (see axis::traits::is_inclusive).
  134. static constexpr bool inclusive() noexcept {
  135. return (options() & option::underflow || options() & option::overflow) ||
  136. (std::is_integral<value_type>::value &&
  137. (options() & (option::growth | option::circular)));
  138. }
  139. template <class V, class M, class O>
  140. bool operator==(const integer<V, M, O>& o) const noexcept {
  141. return size() == o.size() && min_ == o.min_ &&
  142. detail::relaxed_equal{}(this->metadata(), o.metadata());
  143. }
  144. template <class V, class M, class O>
  145. bool operator!=(const integer<V, M, O>& o) const noexcept {
  146. return !operator==(o);
  147. }
  148. template <class Archive>
  149. void serialize(Archive& ar, unsigned /* version */) {
  150. ar& make_nvp("size", size_);
  151. ar& make_nvp("meta", this->metadata());
  152. ar& make_nvp("min", min_);
  153. }
  154. private:
  155. // axis not circular
  156. template <class B>
  157. index_type index_impl(std::false_type, B, double z) const noexcept {
  158. if (z < size()) return z >= 0 ? static_cast<index_type>(z) : -1;
  159. return size();
  160. }
  161. // value_type is integer, axis circular
  162. index_type index_impl(std::true_type, std::false_type, double z) const noexcept {
  163. return static_cast<index_type>(z - std::floor(z / size()) * size());
  164. }
  165. // value_type is floating point, must handle +/-infinite or nan, axis circular
  166. index_type index_impl(std::true_type, std::true_type, double z) const noexcept {
  167. if (std::isfinite(z)) return index_impl(std::true_type{}, std::false_type{}, z);
  168. return z < size() ? -1 : size();
  169. }
  170. index_type size_{0};
  171. value_type min_{0};
  172. template <class V, class M, class O>
  173. friend class integer;
  174. };
  175. #if __cpp_deduction_guides >= 201606
  176. template <class T>
  177. integer(T, T)->integer<detail::convert_integer<T, index_type>, null_type>;
  178. template <class T, class M>
  179. integer(T, T, M)
  180. ->integer<detail::convert_integer<T, index_type>,
  181. detail::replace_type<std::decay_t<M>, const char*, std::string>>;
  182. #endif
  183. } // namespace axis
  184. } // namespace histogram
  185. } // namespace boost
  186. #endif