traits.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. // Copyright 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_TRAITS_HPP
  7. #define BOOST_HISTOGRAM_AXIS_TRAITS_HPP
  8. #include <boost/core/ignore_unused.hpp>
  9. #include <boost/histogram/axis/option.hpp>
  10. #include <boost/histogram/detail/args_type.hpp>
  11. #include <boost/histogram/detail/detect.hpp>
  12. #include <boost/histogram/detail/static_if.hpp>
  13. #include <boost/histogram/detail/try_cast.hpp>
  14. #include <boost/histogram/detail/type_name.hpp>
  15. #include <boost/histogram/fwd.hpp>
  16. #include <boost/mp11/algorithm.hpp>
  17. #include <boost/mp11/list.hpp>
  18. #include <boost/mp11/utility.hpp>
  19. #include <boost/throw_exception.hpp>
  20. #include <boost/variant2/variant.hpp>
  21. #include <stdexcept>
  22. #include <string>
  23. #include <utility>
  24. namespace boost {
  25. namespace histogram {
  26. namespace detail {
  27. template <class T>
  28. using get_options_from_method = axis::option::bitset<T::options()>;
  29. template <class Axis>
  30. struct static_options_impl {
  31. static_assert(std::is_same<std::decay_t<Axis>, Axis>::value,
  32. "support of static_options for qualified types was removed, please use "
  33. "static_options<std::decay_t<...>>");
  34. using type = mp11::mp_eval_or<
  35. mp11::mp_if<has_method_update<Axis>, axis::option::growth_t, axis::option::none_t>,
  36. get_options_from_method, Axis>;
  37. };
  38. template <class T>
  39. using get_inclusive_from_method = std::integral_constant<bool, T::inclusive()>;
  40. template <class Axis>
  41. struct static_is_inclusive_impl {
  42. using type = mp11::mp_eval_or<decltype(static_options_impl<Axis>::type::test(
  43. axis::option::underflow | axis::option::overflow)),
  44. get_inclusive_from_method, Axis>;
  45. };
  46. template <class I, class D, class A>
  47. double value_method_switch_impl1(std::false_type, I&&, D&&, const A&) {
  48. // comma trick to make all compilers happy; some would complain about
  49. // unreachable code after the throw, others about a missing return
  50. return BOOST_THROW_EXCEPTION(
  51. std::runtime_error(type_name<A>() + " has no value method")),
  52. double{};
  53. }
  54. template <class I, class D, class A>
  55. decltype(auto) value_method_switch_impl1(std::true_type, I&& i, D&& d, const A& a) {
  56. using T = arg_type<decltype(&A::value)>;
  57. return static_if<std::is_same<T, axis::index_type>>(std::forward<I>(i),
  58. std::forward<D>(d), a);
  59. }
  60. template <class I, class D, class A>
  61. decltype(auto) value_method_switch(I&& i, D&& d, const A& a) {
  62. return value_method_switch_impl1(has_method_value<A>{}, std::forward<I>(i),
  63. std::forward<D>(d), a);
  64. }
  65. static axis::null_type null_value;
  66. struct variant_access {
  67. template <class T, class Variant>
  68. static auto get_if(Variant* v) noexcept {
  69. using T0 = mp11::mp_first<std::decay_t<Variant>>;
  70. return static_if<std::is_pointer<T0>>(
  71. [](auto* vptr) {
  72. using TP = mp11::mp_if<std::is_const<std::remove_pointer_t<T0>>, const T*, T*>;
  73. auto ptp = variant2::get_if<TP>(vptr);
  74. return ptp ? *ptp : nullptr;
  75. },
  76. [](auto* vptr) { return variant2::get_if<T>(vptr); }, &(v->impl));
  77. }
  78. template <class T0, class Visitor, class Variant>
  79. static decltype(auto) visit_impl(mp11::mp_identity<T0>, Visitor&& vis, Variant&& v) {
  80. return variant2::visit(std::forward<Visitor>(vis), v.impl);
  81. }
  82. template <class T0, class Visitor, class Variant>
  83. static decltype(auto) visit_impl(mp11::mp_identity<T0*>, Visitor&& vis, Variant&& v) {
  84. return variant2::visit(
  85. [&vis](auto&& x) -> decltype(auto) { return std::forward<Visitor>(vis)(*x); },
  86. v.impl);
  87. }
  88. template <class Visitor, class Variant>
  89. static decltype(auto) visit(Visitor&& vis, Variant&& v) {
  90. using T0 = mp11::mp_first<std::decay_t<Variant>>;
  91. return visit_impl(mp11::mp_identity<T0>{}, std::forward<Visitor>(vis),
  92. std::forward<Variant>(v));
  93. }
  94. };
  95. } // namespace detail
  96. namespace axis {
  97. namespace traits {
  98. /** Get value type for axis type.
  99. Doxygen does not render this well. This is a meta-function (template alias), it accepts
  100. an axis type and returns the value type.
  101. */
  102. template <class Axis>
  103. #ifndef BOOST_HISTOGRAM_DOXYGEN_INVOKED
  104. using value_type =
  105. std::remove_cv_t<std::remove_reference_t<detail::arg_type<decltype(&Axis::index)>>>;
  106. #else
  107. struct value_type;
  108. #endif
  109. /** Whether axis is continuous or discrete.
  110. Doxygen does not render this well. This is a meta-function (template alias), it accepts
  111. an axis type and returns a compile-time boolean. If the boolean is true, the axis is
  112. continuous. Otherwise it is discrete.
  113. */
  114. template <class Axis>
  115. #ifndef BOOST_HISTOGRAM_DOXYGEN_INVOKED
  116. using is_continuous = typename std::is_floating_point<traits::value_type<Axis>>::type;
  117. #else
  118. struct is_continuous;
  119. #endif
  120. /** Meta-function to detect whether an axis is reducible.
  121. Doxygen does not render this well. This is a meta-function (template alias), it accepts
  122. an axis type and represents compile-time boolean which is true or false, depending on
  123. whether the axis can be reduced with boost::histogram::algorithm::reduce().
  124. @tparam Axis axis type.
  125. */
  126. template <class Axis>
  127. #ifndef BOOST_HISTOGRAM_DOXYGEN_INVOKED
  128. using is_reducible = std::is_constructible<Axis, const Axis&, axis::index_type,
  129. axis::index_type, unsigned>;
  130. #else
  131. struct is_reducible;
  132. #endif
  133. /** Get static axis options for axis type.
  134. Doxygen does not render this well. This is a meta-function (template alias), it accepts
  135. an axis type and returns the boost::histogram::axis::option::bitset.
  136. If Axis::options() is valid and constexpr, static_options is the corresponding
  137. option type. Otherwise, it is boost::histogram::axis::option::growth_t, if the
  138. axis has a method `update`, else boost::histogram::axis::option::none_t.
  139. @tparam Axis axis type
  140. */
  141. template <class Axis>
  142. #ifndef BOOST_HISTOGRAM_DOXYGEN_INVOKED
  143. using static_options = typename detail::static_options_impl<Axis>::type;
  144. #else
  145. struct static_options;
  146. #endif
  147. /** Meta-function to detect whether an axis is inclusive.
  148. Doxygen does not render this well. This is a meta-function (template alias), it accepts
  149. an axis type and represents compile-time boolean which is true or false, depending on
  150. whether the axis is inclusive or not.
  151. An inclusive axis has a bin for every possible input value. A histogram which consists
  152. only of inclusive axes can be filled more efficiently, since input values always
  153. end up in a valid cell and there is no need to keep track of input tuples that need to
  154. be discarded.
  155. An axis with underflow and overflow bins is always inclusive, but an axis may be
  156. inclusive under other conditions. The meta-function checks for the method `constexpr
  157. static bool inclusive()`, and uses the result. If this method is not present, it uses
  158. static_options<Axis> and checks whether the underflow and overflow bits are present.
  159. @tparam axis type
  160. */
  161. template <class Axis>
  162. #ifndef BOOST_HISTOGRAM_DOXYGEN_INVOKED
  163. using static_is_inclusive = typename detail::static_is_inclusive_impl<Axis>::type;
  164. #else
  165. struct static_is_inclusive;
  166. #endif
  167. /** Returns axis options as unsigned integer.
  168. If axis.options() is a valid expression, return the result. Otherwise, return
  169. static_options<Axis>::value.
  170. @param axis any axis instance
  171. */
  172. template <class Axis>
  173. constexpr unsigned options(const Axis& axis) noexcept {
  174. boost::ignore_unused(axis);
  175. return static_options<Axis>::value;
  176. }
  177. // specialization for variant
  178. template <class... Ts>
  179. unsigned options(const variant<Ts...>& axis) noexcept {
  180. return axis.options();
  181. }
  182. /** Returns true if axis is inclusive or false.
  183. See static_is_inclusive for details.
  184. @param axis any axis instance
  185. */
  186. template <class Axis>
  187. constexpr bool inclusive(const Axis& axis) noexcept {
  188. boost::ignore_unused(axis);
  189. return static_is_inclusive<Axis>::value;
  190. }
  191. // specialization for variant
  192. template <class... Ts>
  193. bool inclusive(const variant<Ts...>& axis) noexcept {
  194. return axis.inclusive();
  195. }
  196. /** Returns axis size plus any extra bins for under- and overflow.
  197. @param axis any axis instance
  198. */
  199. template <class Axis>
  200. index_type extent(const Axis& axis) noexcept {
  201. const auto opt = options(axis);
  202. return axis.size() + (opt & option::underflow ? 1 : 0) +
  203. (opt & option::overflow ? 1 : 0);
  204. }
  205. /** Returns reference to metadata of an axis.
  206. If the expression x.metadata() for an axis instance `x` (maybe const) is valid, return
  207. the result. Otherwise, return a reference to a static instance of
  208. boost::histogram::axis::null_type.
  209. @param axis any axis instance
  210. */
  211. template <class Axis>
  212. decltype(auto) metadata(Axis&& axis) noexcept {
  213. return detail::static_if<detail::has_method_metadata<std::decay_t<Axis>>>(
  214. [](auto&& a) -> decltype(auto) { return a.metadata(); },
  215. [](auto &&) -> mp11::mp_if<std::is_const<std::remove_reference_t<Axis>>,
  216. axis::null_type const&, axis::null_type&> {
  217. return detail::null_value;
  218. },
  219. std::forward<Axis>(axis));
  220. }
  221. /** Returns axis value for index.
  222. If the axis has no `value` method, throw std::runtime_error. If the method exists and
  223. accepts a floating point index, pass the index and return the result. If the method
  224. exists but accepts only integer indices, cast the floating point index to int, pass this
  225. index and return the result.
  226. @param axis any axis instance
  227. @param index floating point axis index
  228. */
  229. template <class Axis>
  230. decltype(auto) value(const Axis& axis, real_index_type index) {
  231. return detail::value_method_switch(
  232. [index](const auto& a) { return a.value(static_cast<index_type>(index)); },
  233. [index](const auto& a) { return a.value(index); }, axis);
  234. }
  235. /** Returns axis value for index if it is convertible to target type or throws.
  236. Like boost::histogram::axis::traits::value, but converts the result into the requested
  237. return type. If the conversion is not possible, throws std::runtime_error.
  238. @tparam Result requested return type
  239. @tparam Axis axis type
  240. @param axis any axis instance
  241. @param index floating point axis index
  242. */
  243. template <class Result, class Axis>
  244. Result value_as(const Axis& axis, real_index_type index) {
  245. return detail::try_cast<Result, std::runtime_error>(
  246. value(axis, index)); // avoid conversion warning
  247. }
  248. /** Returns axis index for value.
  249. Throws std::invalid_argument if the value argument is not implicitly convertible.
  250. @param axis any axis instance
  251. @param value argument to be passed to `index` method
  252. */
  253. template <class Axis, class U>
  254. axis::index_type index(const Axis& axis, const U& value) noexcept(
  255. std::is_convertible<U, value_type<Axis>>::value) {
  256. return axis.index(detail::try_cast<value_type<Axis>, std::invalid_argument>(value));
  257. }
  258. // specialization for variant
  259. template <class... Ts, class U>
  260. axis::index_type index(const variant<Ts...>& axis, const U& value) {
  261. return axis.index(value);
  262. }
  263. /** Return axis rank (how many arguments it processes).
  264. @param axis any axis instance
  265. */
  266. template <class Axis>
  267. constexpr unsigned rank(const Axis& axis) {
  268. boost::ignore_unused(axis);
  269. using T = value_type<Axis>;
  270. // cannot use mp_eval_or since T could be a fixed-sized sequence
  271. return mp11::mp_eval_if_not<detail::is_tuple<T>, mp11::mp_size_t<1>, mp11::mp_size,
  272. T>::value;
  273. }
  274. // specialization for variant
  275. template <class... Ts>
  276. unsigned rank(const axis::variant<Ts...>& axis) {
  277. return detail::variant_access::visit([](const auto& a) { return rank(a); }, axis);
  278. }
  279. /** Returns pair of axis index and shift for the value argument.
  280. Throws `std::invalid_argument` if the value argument is not implicitly convertible to
  281. the argument expected by the `index` method. If the result of
  282. boost::histogram::axis::traits::static_options<decltype(axis)> has the growth flag set,
  283. call `update` method with the argument and return the result. Otherwise, call `index`
  284. and return the pair of the result and a zero shift.
  285. @param axis any axis instance
  286. @param value argument to be passed to `update` or `index` method
  287. */
  288. template <class Axis, class U>
  289. std::pair<index_type, index_type> update(Axis& axis, const U& value) noexcept(
  290. std::is_convertible<U, value_type<Axis>>::value) {
  291. return detail::static_if_c<static_options<Axis>::test(option::growth)>(
  292. [&value](auto& a) {
  293. return a.update(detail::try_cast<value_type<Axis>, std::invalid_argument>(value));
  294. },
  295. [&value](auto& a) { return std::make_pair(index(a, value), index_type{0}); }, axis);
  296. }
  297. // specialization for variant
  298. template <class... Ts, class U>
  299. std::pair<index_type, index_type> update(variant<Ts...>& axis, const U& value) {
  300. return visit([&value](auto& a) { return a.update(value); }, axis);
  301. }
  302. /** Returns bin width at axis index.
  303. If the axis has no `value` method, throw std::runtime_error. If the method exists and
  304. accepts a floating point index, return the result of `axis.value(index + 1) -
  305. axis.value(index)`. If the method exists but accepts only integer indices, return 0.
  306. @param axis any axis instance
  307. @param index bin index
  308. */
  309. template <class Axis>
  310. decltype(auto) width(const Axis& axis, index_type index) {
  311. return detail::value_method_switch(
  312. [](const auto&) { return 0; },
  313. [index](const auto& a) { return a.value(index + 1) - a.value(index); }, axis);
  314. }
  315. /** Returns bin width at axis index.
  316. Like boost::histogram::axis::traits::width, but converts the result into the requested
  317. return type. If the conversion is not possible, throw std::runtime_error.
  318. @param axis any axis instance
  319. @param index bin index
  320. */
  321. template <class Result, class Axis>
  322. Result width_as(const Axis& axis, index_type index) {
  323. return detail::value_method_switch(
  324. [](const auto&) { return Result{}; },
  325. [index](const auto& a) {
  326. return detail::try_cast<Result, std::runtime_error>(a.value(index + 1) -
  327. a.value(index));
  328. },
  329. axis);
  330. }
  331. } // namespace traits
  332. } // namespace axis
  333. } // namespace histogram
  334. } // namespace boost
  335. #endif