Automatic Differentiation
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
filter_map.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUNCTOR_FILTER_MAP_HPP
2#define STAN_MATH_PRIM_FUNCTOR_FILTER_MAP_HPP
3
8#include <functional>
9#include <tuple>
10#include <utility>
11
12namespace stan {
13namespace math {
14
15namespace internal {
16
17template <template <typename...> class Filter, typename T>
19 static constexpr bool value = Filter<T>::value;
20};
21
22template <template <typename...> class Filter, typename... Types>
23struct inspect_tuple<Filter, std::tuple<Types...>> {
24 static constexpr bool value = Filter<std::tuple<Types...>>::value
26};
27
28template <template <typename...> class Filter, typename T, typename... VecArgs>
29struct inspect_tuple<Filter, std::vector<T, VecArgs...>> {
30 static constexpr bool value
31 = Filter<std::vector<T, VecArgs...>>::value
33};
34
42template <template <typename...> class Filter, typename T>
43inline constexpr bool inspect_tuple_v
45
71template <template <typename...> class Filter, bool InVector = false,
72 bool InTuple = false, typename F, typename T>
73inline constexpr decltype(auto) filter_map(F&& f, T&& x) {
74 if constexpr (inspect_tuple_v<Filter, T>) {
75 if constexpr (is_tuple_v<T>) {
76 auto ret = stan::math::apply(
77 [&f](auto&&... args) {
78 return stan::math::tuple_concat(filter_map<Filter, false, true>(
79 f, std::forward<decltype(args)>(args))...);
80 },
81 std::forward<T>(x));
92 if constexpr (InTuple) {
93 return make_holder_tuple(std::move(ret));
94 } else {
95 return ret;
96 }
97 } else if constexpr (is_std_vector_v<T>) {
98 /* 3 cases for vectors
99 * 1. value_type is a tuple
100 * 2. value_type is a scalar or Eigen matrix
101 * 3. value_type is a std::vector which can hold either (1) or (2)
102 */
103 if constexpr (contains_tuple<T>::value) {
104 std::vector<decltype(filter_map<Filter, true>(f, x[0]))> ret;
105 for (size_t i = 0; i < x.size(); ++i) {
106 ret.push_back(filter_map<Filter, true>(f, x[i]));
107 }
108 /*
109 * If we are in a vector, return the raw type, otherwise we are in
110 * a tuple and we want to return a tuple of the vector.
111 */
112 if constexpr (InVector) {
113 return ret;
114 } else {
115 return std::make_tuple(std::move(ret));
116 }
117 } else {
118 if constexpr (InVector) {
119 return std::forward<F>(f)(std::forward<T>(x));
120 } else {
121 return make_holder_tuple(std::forward<F>(f)(std::forward<T>(x)));
122 }
123 }
124 } else {
125 if constexpr (InVector) {
126 return std::forward<F>(f)(std::forward<T>(x));
127 } else {
128 return make_holder_tuple(std::forward<F>(f)(std::forward<T>(x)));
129 }
130 }
131 } else {
132 return std::make_tuple();
133 }
134}
135} // namespace internal
152template <template <typename...> class Filter, typename F, typename T,
153 require_tuple_t<T>* = nullptr>
154inline constexpr decltype(auto) filter_map(F&& f, T&& x) {
155 return internal::filter_map<Filter>(std::forward<F>(f), std::forward<T>(x));
156}
157} // namespace math
158} // namespace stan
159
160#endif
constexpr decltype(auto) filter_map(F &&f, T &&x)
Filter a tuple and apply a functor to each element that passes the filter.
constexpr bool inspect_tuple_v
Check if a tuple or type contains a tuple that passes the filter.
constexpr auto make_holder_tuple(Types &&... args)
Holds ownership of rvalues and forwards lvalues into a tuple.
constexpr auto tuple_concat() noexcept
Base case to pass a tuple forward.
constexpr decltype(auto) filter_map(F &&f, T &&x)
Filter a tuple and apply a functor to each element that passes the filter.
constexpr decltype(auto) apply(F &&f, Tuple &&t, PreArgs &&... pre_args)
Definition apply.hpp:51
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
STL namespace.
Check if the type is a tuple or contains a tuple.