Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:40:42

0001 //  Copyright John Maddock 2007.
0002 //  Copyright Matt Borland 2021.
0003 //  Use, modification and distribution are subject to the
0004 //  Boost Software License, Version 1.0. (See accompanying file
0005 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 
0007 /*
0008 This header defines two traits classes, both in namespace boost::math::tools.
0009 
0010 is_distribution<D>::value is true iff D has overloaded "cdf" and
0011 "quantile" functions, plus member typedefs value_type and policy_type.  
0012 It's not much of a definitive test frankly,
0013 but if it looks like a distribution and quacks like a distribution
0014 then it must be a distribution.
0015 
0016 is_scaled_distribution<D>::value is true iff D is a distribution
0017 as defined above, and has member functions "scale" and "location".
0018 
0019 */
0020 
0021 #ifndef BOOST_STATS_IS_DISTRIBUTION_HPP
0022 #define BOOST_STATS_IS_DISTRIBUTION_HPP
0023 
0024 #ifdef _MSC_VER
0025 #pragma once
0026 #endif
0027 
0028 #include <type_traits>
0029 
0030 namespace boost{ namespace math{ namespace tools{
0031 
0032 namespace detail{
0033 
0034 #define BOOST_MATH_HAS_NAMED_TRAIT(trait, name)                         \
0035 template <typename T>                                                   \
0036 class trait                                                             \
0037 {                                                                       \
0038 private:                                                                \
0039    using yes = char;                                                    \
0040    struct no { char x[2]; };                                            \
0041                                                                         \
0042    template <typename U>                                                \
0043    static yes test(typename U::name* = nullptr);                        \
0044                                                                         \
0045    template <typename U>                                                \
0046    static no test(...);                                                 \
0047                                                                         \
0048 public:                                                                 \
0049    static constexpr bool value = (sizeof(test<T>(0)) == sizeof(char));  \
0050 };
0051 
0052 BOOST_MATH_HAS_NAMED_TRAIT(has_value_type, value_type)
0053 BOOST_MATH_HAS_NAMED_TRAIT(has_policy_type, policy_type)
0054 BOOST_MATH_HAS_NAMED_TRAIT(has_backend_type, backend_type)
0055 
0056 // C++17-esque helpers
0057 #if defined(__cpp_variable_templates) && __cpp_variable_templates >= 201304L
0058 template <typename T>
0059 constexpr bool has_value_type_v = has_value_type<T>::value;
0060 
0061 template <typename T>
0062 constexpr bool has_policy_type_v = has_policy_type<T>::value;
0063 
0064 template <typename T>
0065 constexpr bool has_backend_type_v = has_backend_type<T>::value;
0066 #endif
0067 
0068 template <typename D>
0069 char cdf(const D& ...);
0070 template <typename D>
0071 char quantile(const D& ...);
0072 
0073 template <typename D>
0074 struct has_cdf
0075 {
0076    static D d;
0077    static constexpr bool value = sizeof(cdf(d, 0.0f)) != 1;
0078 };
0079 
0080 template <typename D>
0081 struct has_quantile
0082 {
0083    static D d;
0084    static constexpr bool value = sizeof(quantile(d, 0.0f)) != 1;
0085 };
0086 
0087 template <typename D>
0088 struct is_distribution_imp
0089 {
0090    static constexpr bool value =
0091       has_quantile<D>::value 
0092       && has_cdf<D>::value
0093       && has_value_type<D>::value
0094       && has_policy_type<D>::value;
0095 };
0096 
0097 template <typename sig, sig val>
0098 struct result_tag{};
0099 
0100 template <typename D>
0101 double test_has_location(const volatile result_tag<typename D::value_type (D::*)()const, &D::location>*);
0102 template <typename D>
0103 char test_has_location(...);
0104 
0105 template <typename D>
0106 double test_has_scale(const volatile result_tag<typename D::value_type (D::*)()const, &D::scale>*);
0107 template <typename D>
0108 char test_has_scale(...);
0109 
0110 template <typename D, bool b>
0111 struct is_scaled_distribution_helper
0112 {
0113    static constexpr bool value = false;
0114 };
0115 
0116 template <typename D>
0117 struct is_scaled_distribution_helper<D, true>
0118 {
0119    static constexpr bool value = 
0120       (sizeof(test_has_location<D>(0)) != 1) 
0121       && 
0122       (sizeof(test_has_scale<D>(0)) != 1);
0123 };
0124 
0125 template <typename D>
0126 struct is_scaled_distribution_imp
0127 {
0128    static constexpr bool value = (::boost::math::tools::detail::is_scaled_distribution_helper<D, ::boost::math::tools::detail::is_distribution_imp<D>::value>::value);
0129 };
0130 
0131 } // namespace detail
0132 
0133 template <typename T> struct is_distribution : public std::integral_constant<bool, ::boost::math::tools::detail::is_distribution_imp<T>::value> {};
0134 template <typename T> struct is_scaled_distribution : public std::integral_constant<bool, ::boost::math::tools::detail::is_scaled_distribution_imp<T>::value> {};
0135 
0136 }}}
0137 
0138 #endif
0139 
0140