traits.hpp 16 KB

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