brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.5 KiB · e909881 Raw
141 lines · plain
1//  Copyright John Maddock 2007.2//  Copyright Matt Borland 2021.3//  Use, modification and distribution are subject to the4//  Boost Software License, Version 1.0. (See accompanying file5//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)6 7/*8This header defines two traits classes, both in namespace boost::math::tools.9 10is_distribution<D>::value is true iff D has overloaded "cdf" and11"quantile" functions, plus member typedefs value_type and policy_type.  12It's not much of a definitive test frankly,13but if it looks like a distribution and quacks like a distribution14then it must be a distribution.15 16is_scaled_distribution<D>::value is true iff D is a distribution17as defined above, and has member functions "scale" and "location".18 19*/20 21#ifndef BOOST_STATS_IS_DISTRIBUTION_HPP22#define BOOST_STATS_IS_DISTRIBUTION_HPP23 24#ifdef _MSC_VER25#pragma once26#endif27 28#include <type_traits>29 30namespace boost{ namespace math{ namespace tools{31 32namespace detail{33 34#define BOOST_MATH_HAS_NAMED_TRAIT(trait, name)                         \35template <typename T>                                                   \36class trait                                                             \37{                                                                       \38private:                                                                \39   using yes = char;                                                    \40   struct no { char x[2]; };                                            \41                                                                        \42   template <typename U>                                                \43   static yes test(typename U::name* = nullptr);                        \44                                                                        \45   template <typename U>                                                \46   static no test(...);                                                 \47                                                                        \48public:                                                                 \49   static constexpr bool value = (sizeof(test<T>(0)) == sizeof(char));  \50};51 52BOOST_MATH_HAS_NAMED_TRAIT(has_value_type, value_type)53BOOST_MATH_HAS_NAMED_TRAIT(has_policy_type, policy_type)54BOOST_MATH_HAS_NAMED_TRAIT(has_backend_type, backend_type)55 56// C++17-esque helpers57#if defined(__cpp_variable_templates) && __cpp_variable_templates >= 201304L58template <typename T>59constexpr bool has_value_type_v = has_value_type<T>::value;60 61template <typename T>62constexpr bool has_policy_type_v = has_policy_type<T>::value;63 64template <typename T>65constexpr bool has_backend_type_v = has_backend_type<T>::value;66#endif67 68template <typename D>69char cdf(const D&, ...);70template <typename D>71char quantile(const D&, ...);72 73template <typename D>74struct has_cdf75{76   static D d;77   static constexpr bool value = sizeof(cdf(d, 0.0f)) != 1;78};79 80template <typename D>81struct has_quantile82{83   static D d;84   static constexpr bool value = sizeof(quantile(d, 0.0f)) != 1;85};86 87template <typename D>88struct is_distribution_imp89{90   static constexpr bool value =91      has_quantile<D>::value 92      && has_cdf<D>::value93      && has_value_type<D>::value94      && has_policy_type<D>::value;95};96 97template <typename sig, sig val>98struct result_tag{};99 100template <typename D>101double test_has_location(const volatile result_tag<typename D::value_type (D::*)()const, &D::location>*);102template <typename D>103char test_has_location(...);104 105template <typename D>106double test_has_scale(const volatile result_tag<typename D::value_type (D::*)()const, &D::scale>*);107template <typename D>108char test_has_scale(...);109 110template <typename D, bool b>111struct is_scaled_distribution_helper112{113   static constexpr bool value = false;114};115 116template <typename D>117struct is_scaled_distribution_helper<D, true>118{119   static constexpr bool value = 120      (sizeof(test_has_location<D>(0)) != 1) 121      && 122      (sizeof(test_has_scale<D>(0)) != 1);123};124 125template <typename D>126struct is_scaled_distribution_imp127{128   static constexpr bool value = (::boost::math::tools::detail::is_scaled_distribution_helper<D, ::boost::math::tools::detail::is_distribution_imp<D>::value>::value);129};130 131} // namespace detail132 133template <typename T> struct is_distribution : public std::integral_constant<bool, ::boost::math::tools::detail::is_distribution_imp<T>::value> {};134template <typename T> struct is_scaled_distribution : public std::integral_constant<bool, ::boost::math::tools::detail::is_scaled_distribution_imp<T>::value> {};135 136}}}137 138#endif139 140 141