Skip to content

Commit 1d6e5e4

Browse files
committed
Add a few more type traits for use in stanc
1 parent 45bd042 commit 1d6e5e4

5 files changed

Lines changed: 112 additions & 0 deletions

File tree

stan/math/prim/meta.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@
9595
#include <stan/math/prim/meta/is_eigen_matrix.hpp>
9696
#include <stan/math/prim/meta/is_eigen_matrix_base.hpp>
9797
#include <stan/math/prim/meta/is_eigen_sparse_base.hpp>
98+
#include <stan/math/prim/meta/is_floating_point.hpp>
9899
#include <stan/math/prim/meta/is_fvar.hpp>
100+
#include <stan/math/prim/meta/is_integral.hpp>
99101
#include <stan/math/prim/meta/is_kernel_expression.hpp>
100102
#include <stan/math/prim/meta/is_matrix_cl.hpp>
101103
#include <stan/math/prim/meta/is_matrix.hpp>
@@ -128,6 +130,7 @@
128130
#include <stan/math/prim/meta/scalar_type.hpp>
129131
#include <stan/math/prim/meta/seq_view.hpp>
130132
#include <stan/math/prim/meta/static_select.hpp>
133+
#include <stan/math/prim/meta/tuple_elt.hpp>
131134
#include <stan/math/prim/meta/value_type.hpp>
132135
#include <stan/math/prim/meta/void_t.hpp>
133136
#include <stan/math/prim/meta/StdVectorBuilder.hpp>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#ifndef STAN_MATH_PRIM_META_IS_FLOATING_POINT_HPP
2+
#define STAN_MATH_PRIM_META_IS_FLOATING_POINT_HPP
3+
4+
#include <type_traits>
5+
6+
namespace stan {
7+
8+
/**
9+
* Checks if decayed type is a floating point type
10+
* @tparam The type to check
11+
* @ingroup type_trait
12+
*/
13+
template <typename T>
14+
using is_floating_point = std::is_floating_point<std::decay_t<T>>;
15+
16+
template <typename T>
17+
inline constexpr bool is_floating_point_v = is_floating_point<T>::value;
18+
19+
template <typename... Types>
20+
inline constexpr bool is_all_floating_point_v
21+
= (is_floating_point_v<Types> && ...);
22+
23+
template <typename... Types>
24+
inline constexpr bool is_any_floating_point_v
25+
= (is_floating_point_v<Types> || ...);
26+
27+
} // namespace stan
28+
29+
#endif
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef STAN_MATH_PRIM_META_IS_INTEGRAL_HPP
2+
#define STAN_MATH_PRIM_META_IS_INTEGRAL_HPP
3+
4+
#include <type_traits>
5+
6+
namespace stan {
7+
8+
/**
9+
* Checks if decayed type is integral
10+
* @tparam The type to check
11+
* @ingroup type_trait
12+
*/
13+
template <typename T>
14+
using is_integral = std::is_integral<std::decay_t<T>>;
15+
16+
template <typename T>
17+
inline constexpr bool is_integral_v = is_integral<T>::value;
18+
19+
template <typename... Types>
20+
inline constexpr bool is_all_integral_v = (is_integral_v<Types> && ...);
21+
22+
template <typename... Types>
23+
inline constexpr bool is_any_integral_v = (is_integral_v<Types> || ...);
24+
25+
} // namespace stan
26+
27+
#endif

stan/math/prim/meta/is_tuple.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef STAN_MATH_PRIM_META_IS_TUPLE_HPP
22
#define STAN_MATH_PRIM_META_IS_TUPLE_HPP
33

4+
#include <cstddef>
45
#include <stan/math/prim/meta/require_helpers.hpp>
56
#include <tuple>
67
#include <type_traits>
@@ -49,6 +50,26 @@ using require_all_not_tuple_t
4950
/*! @} */
5051
} // namespace math
5152

53+
template <typename T>
54+
using is_tuple = math::is_tuple<T>;
55+
56+
template <typename T>
57+
inline constexpr bool is_tuple_v = math::is_tuple_v<T>;
58+
59+
/**
60+
* Checks both that T is a tuple and that its size is N
61+
* @tparam T type to retrieve the element from
62+
* @tparam N expected size
63+
* @ingroup type_trait
64+
*/
65+
template <typename T, std::size_t N>
66+
struct is_tuple_of_size
67+
: std::bool_constant<is_tuple_v<T>
68+
&& std::tuple_size_v<std::decay_t<T>> == N> {};
69+
70+
template <typename T, std::size_t N>
71+
inline constexpr bool is_tuple_of_size_v = is_tuple_of_size<T, N>::value;
72+
5273
} // namespace stan
5374

5475
#endif

stan/math/prim/meta/tuple_elt.hpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef STAN_MATH_PRIM_META_IS_TUPLE_ELT_HPP
2+
#define STAN_MATH_PRIM_META_IS_TUPLE_ELT_HPP
3+
4+
#include <type_traits>
5+
#include <cstddef>
6+
#include <tuple>
7+
8+
namespace stan {
9+
10+
/**
11+
* Equivalent to std::tuple_element but returns void if N is out of range
12+
* to avoid a static assertion failure in libstdc++.
13+
* @tparam N index of the element to retrieve
14+
* @tparam T type to retrieve the element from
15+
* @ingroup type_trait
16+
*/
17+
template <std::size_t N, typename T, typename = void>
18+
struct tuple_element {
19+
using type = void;
20+
};
21+
22+
template <std::size_t N, typename T>
23+
struct tuple_element<
24+
N, T, std::enable_if_t<(N < std::tuple_size_v<std::decay_t<T>>)>> {
25+
using type = std::tuple_element_t<N, std::decay_t<T>>;
26+
};
27+
28+
template <std::size_t N, typename T>
29+
using tuple_element_t = typename tuple_element<N, T>::type;
30+
} // namespace stan
31+
32+
#endif

0 commit comments

Comments
 (0)