traits.hpp 16 KB

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