traits.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. #else
  159. struct get_options;
  160. #endif
  161. /** Meta-function to detect whether an axis is inclusive.
  162. Doxygen does not render this well. This is a meta-function (template alias), it accepts
  163. an axis type and represents compile-time boolean which is true or false, depending on
  164. whether the axis is inclusive or not.
  165. An inclusive axis has a bin for every possible input value. In other words, all
  166. possible input values always end up in a valid cell and there is no need to keep track
  167. of input tuples that need to be discarded. A histogram which consists entirely of
  168. inclusive axes can be filled more efficiently, which can be a factor 2 faster.
  169. An axis with underflow and overflow bins is always inclusive, but an axis may be
  170. inclusive under other conditions. The meta-function checks for the method `constexpr
  171. static bool inclusive()`, and uses the result. If this method is not present, it uses
  172. get_options<Axis> and checks whether the underflow and overflow bits are present.
  173. @tparam Axis axis type
  174. */
  175. template <class Axis>
  176. #ifndef BOOST_HISTOGRAM_DOXYGEN_INVOKED
  177. using is_inclusive = decltype(detail::traits_is_inclusive<Axis>(detail::priority<1>{}));
  178. #else
  179. struct is_inclusive;
  180. #endif
  181. /** Meta-function to detect whether an axis is ordered.
  182. Doxygen does not render this well. This is a meta-function (template alias), it accepts
  183. an axis type and returns a compile-time boolean. If the boolean is true, the axis is
  184. ordered.
  185. The meta-function checks for the method `constexpr static bool ordered()`, and uses the
  186. result. If this method is not present, it returns true if the value type of the Axis is
  187. arithmetic and false otherwise.
  188. An ordered axis has a value type that is ordered, which means that indices i <
  189. j < k implies either value(i) < value(j) < value(k) or value(i) > value(j) > value(k)
  190. for all i,j,k. For example, the integer axis is ordered, but the category axis is not.
  191. Axis which are not ordered must not have underflow bins, because they only have an
  192. "other" category, which is identified with the overflow bin if it is available.
  193. @tparam Axis axis type
  194. */
  195. template <class Axis>
  196. #ifndef BOOST_HISTOGRAM_DOXYGEN_INVOKED
  197. using is_ordered = decltype(detail::traits_is_ordered<Axis>(detail::priority<1>{}));
  198. #else
  199. struct is_ordered;
  200. #endif
  201. /** Returns axis options as unsigned integer.
  202. See get_options for details.
  203. @param axis any axis instance
  204. */
  205. template <class Axis>
  206. constexpr unsigned options(const Axis& axis) noexcept {
  207. (void)axis;
  208. return get_options<Axis>::value;
  209. }
  210. // specialization for variant
  211. template <class... Ts>
  212. unsigned options(const variant<Ts...>& axis) noexcept {
  213. return axis.options();
  214. }
  215. /** Returns true if axis is inclusive or false.
  216. See is_inclusive for details.
  217. @param axis any axis instance
  218. */
  219. template <class Axis>
  220. constexpr bool inclusive(const Axis& axis) noexcept {
  221. (void)axis;
  222. return is_inclusive<Axis>::value;
  223. }
  224. // specialization for variant
  225. template <class... Ts>
  226. bool inclusive(const variant<Ts...>& axis) noexcept {
  227. return axis.inclusive();
  228. }
  229. /** Returns true if axis is ordered or false.
  230. See is_ordered for details.
  231. @param axis any axis instance
  232. */
  233. template <class Axis>
  234. constexpr bool ordered(const Axis& axis) noexcept {
  235. (void)axis;
  236. return is_ordered<Axis>::value;
  237. }
  238. // specialization for variant
  239. template <class... Ts>
  240. bool ordered(const variant<Ts...>& axis) noexcept {
  241. return axis.ordered();
  242. }
  243. /** Returns true if axis is continuous or false.
  244. See is_continuous for details.
  245. @param axis any axis instance
  246. */
  247. template <class Axis>
  248. constexpr bool continuous(const Axis& axis) noexcept {
  249. (void)axis;
  250. return is_continuous<Axis>::value;
  251. }
  252. // specialization for variant
  253. template <class... Ts>
  254. bool continuous(const variant<Ts...>& axis) noexcept {
  255. return axis.continuous();
  256. }
  257. /** Returns axis size plus any extra bins for under- and overflow.
  258. @param axis any axis instance
  259. */
  260. template <class Axis>
  261. index_type extent(const Axis& axis) noexcept {
  262. const auto opt = options(axis);
  263. return axis.size() + (opt & option::underflow ? 1 : 0) +
  264. (opt & option::overflow ? 1 : 0);
  265. }
  266. /** Returns reference to metadata of an axis.
  267. If the expression x.metadata() for an axis instance `x` (maybe const) is valid, return
  268. the result. Otherwise, return a reference to a static instance of
  269. boost::histogram::axis::null_type.
  270. @param axis any axis instance
  271. */
  272. template <class Axis>
  273. decltype(auto) metadata(Axis&& axis) noexcept {
  274. return detail::metadata_impl(std::forward<Axis>(axis), 0);
  275. }
  276. /** Returns axis value for index.
  277. If the axis has no `value` method, throw std::runtime_error. If the method exists and
  278. accepts a floating point index, pass the index and return the result. If the method
  279. exists but accepts only integer indices, cast the floating point index to int, pass this
  280. index and return the result.
  281. @param axis any axis instance
  282. @param index floating point axis index
  283. */
  284. template <class Axis>
  285. decltype(auto) value(const Axis& axis, real_index_type index) {
  286. return detail::value_method_switch(
  287. [index](const auto& a) { return a.value(static_cast<index_type>(index)); },
  288. [index](const auto& a) { return a.value(index); }, axis, detail::priority<1>{});
  289. }
  290. /** Returns axis value for index if it is convertible to target type or throws.
  291. Like boost::histogram::axis::traits::value, but converts the result into the requested
  292. return type. If the conversion is not possible, throws std::runtime_error.
  293. @tparam Result requested return type
  294. @tparam Axis axis type
  295. @param axis any axis instance
  296. @param index floating point axis index
  297. */
  298. template <class Result, class Axis>
  299. Result value_as(const Axis& axis, real_index_type index) {
  300. return detail::try_cast<Result, std::runtime_error>(
  301. axis::traits::value(axis, index)); // avoid conversion warning
  302. }
  303. /** Returns axis index for value.
  304. Throws std::invalid_argument if the value argument is not implicitly convertible.
  305. @param axis any axis instance
  306. @param value argument to be passed to `index` method
  307. */
  308. template <class Axis, class U>
  309. axis::index_type index(const Axis& axis, const U& value) noexcept(
  310. std::is_convertible<U, value_type<Axis>>::value) {
  311. return axis.index(detail::try_cast<value_type<Axis>, std::invalid_argument>(value));
  312. }
  313. // specialization for variant
  314. template <class... Ts, class U>
  315. axis::index_type index(const variant<Ts...>& axis, const U& value) {
  316. return axis.index(value);
  317. }
  318. /** Return axis rank (how many arguments it processes).
  319. @param axis any axis instance
  320. */
  321. // gcc workaround: must use unsigned int not unsigned as return type
  322. template <class Axis>
  323. constexpr unsigned int rank(const Axis& axis) {
  324. (void)axis;
  325. using T = value_type<Axis>;
  326. // cannot use mp_eval_or since T could be a fixed-sized sequence
  327. return mp11::mp_eval_if_not<detail::is_tuple<T>, mp11::mp_size_t<1>, mp11::mp_size,
  328. T>::value;
  329. }
  330. // specialization for variant
  331. // gcc workaround: must use unsigned int not unsigned as return type
  332. template <class... Ts>
  333. unsigned int rank(const axis::variant<Ts...>& axis) {
  334. return detail::variant_access::visit(
  335. [](const auto& a) { return axis::traits::rank(a); }, axis);
  336. }
  337. /** Returns pair of axis index and shift for the value argument.
  338. Throws `std::invalid_argument` if the value argument is not implicitly convertible to
  339. the argument expected by the `index` method. If the result of
  340. boost::histogram::axis::traits::get_options<decltype(axis)> has the growth flag set,
  341. call `update` method with the argument and return the result. Otherwise, call `index`
  342. and return the pair of the result and a zero shift.
  343. @param axis any axis instance
  344. @param value argument to be passed to `update` or `index` method
  345. */
  346. template <class Axis, class U>
  347. std::pair<index_type, index_type> update(Axis& axis, const U& value) noexcept(
  348. std::is_convertible<U, value_type<Axis>>::value) {
  349. return detail::static_if_c<get_options<Axis>::test(option::growth)>(
  350. [&value](auto& a) {
  351. return a.update(detail::try_cast<value_type<Axis>, std::invalid_argument>(value));
  352. },
  353. [&value](auto& a) -> std::pair<index_type, index_type> {
  354. return {axis::traits::index(a, value), 0};
  355. },
  356. axis);
  357. }
  358. // specialization for variant
  359. template <class... Ts, class U>
  360. std::pair<index_type, index_type> update(variant<Ts...>& axis, const U& value) {
  361. return visit([&value](auto& a) { return a.update(value); }, axis);
  362. }
  363. /** Returns bin width at axis index.
  364. If the axis has no `value` method, throw std::runtime_error. If the method exists and
  365. accepts a floating point index, return the result of `axis.value(index + 1) -
  366. axis.value(index)`. If the method exists but accepts only integer indices, return 0.
  367. @param axis any axis instance
  368. @param index bin index
  369. */
  370. template <class Axis>
  371. decltype(auto) width(const Axis& axis, index_type index) {
  372. return detail::value_method_switch(
  373. [](const auto&) { return 0; },
  374. [index](const auto& a) { return a.value(index + 1) - a.value(index); }, axis,
  375. detail::priority<1>{});
  376. }
  377. /** Returns bin width at axis index.
  378. Like boost::histogram::axis::traits::width, but converts the result into the requested
  379. return type. If the conversion is not possible, throw std::runtime_error.
  380. @param axis any axis instance
  381. @param index bin index
  382. */
  383. template <class Result, class Axis>
  384. Result width_as(const Axis& axis, index_type index) {
  385. return detail::value_method_switch(
  386. [](const auto&) { return Result{}; },
  387. [index](const auto& a) {
  388. return detail::try_cast<Result, std::runtime_error>(a.value(index + 1) -
  389. a.value(index));
  390. },
  391. axis, detail::priority<1>{});
  392. }
  393. } // namespace traits
  394. } // namespace axis
  395. } // namespace histogram
  396. } // namespace boost
  397. #endif