Automatic Differentiation
 
Loading...
Searching...
No Matches
for_each.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUNCTOR_FOR_EACH_HPP
2#define STAN_MATH_PRIM_FUNCTOR_FOR_EACH_HPP
3
5#include <functional>
6#include <tuple>
7#include <utility>
8
9namespace stan {
10namespace math {
11namespace internal {
12
18template <typename F, typename T, size_t... Is>
19constexpr inline auto for_each(F&& f, T&& t, std::index_sequence<Is...>) {
20 using Swallow = int[];
21 static_cast<void>(Swallow{
22 (static_cast<void>(std::forward<F>(f)(std::get<Is>(std::forward<T>(t)))),
23 0)...});
24}
25
31template <typename F, typename T1, typename T2, size_t... Is>
32constexpr inline auto for_each(F&& f, T1&& t1, T2&& t2,
33 std::index_sequence<Is...>) {
34 using Swallow = int[];
35 static_cast<void>(Swallow{(
36 static_cast<void>(std::forward<F>(f)(std::get<Is>(std::forward<T1>(t1)),
37 std::get<Is>(std::forward<T2>(t2)))),
38 0)...});
39}
40
46template <typename F, typename T1, typename T2, typename T3, size_t... Is>
47constexpr inline auto for_each(F&& f, T1&& t1, T2&& t2, T3&& t3,
48 std::index_sequence<Is...>) {
49 using Swallow = int[];
50 static_cast<void>(Swallow{(
51 static_cast<void>(std::forward<F>(f)(std::get<Is>(std::forward<T1>(t1)),
52 std::get<Is>(std::forward<T2>(t2)),
53 std::get<Is>(std::forward<T3>(t3)))),
54 0)...});
55}
56} // namespace internal
57
65template <typename F, typename T>
66constexpr inline auto for_each(F&& f, T&& t) {
67 return internal::for_each(
68 std::forward<F>(f), std::forward<T>(t),
69 std::make_index_sequence<std::tuple_size<std::decay_t<T>>::value>());
70}
71
81template <typename F, typename T1, typename T2>
82constexpr inline auto for_each(F&& f, T1&& t1, T2&& t2) {
83 constexpr auto t1_size = std::tuple_size<std::decay_t<T1>>::value;
84 constexpr auto t2_size = std::tuple_size<std::decay_t<T2>>::value;
85 static_assert(t1_size == t2_size,
86 "Size Mismatch between t1 and t2 in for_each");
87 return internal::for_each(std::forward<F>(f), std::forward<T1>(t1),
88 std::forward<T2>(t2),
89 std::make_index_sequence<t1_size>());
90}
91
103template <typename F, typename T1, typename T2, typename T3>
104constexpr inline auto for_each(F&& f, T1&& t1, T2&& t2, T3&& t3) {
105 constexpr auto t1_size = std::tuple_size<std::decay_t<T1>>::value;
106 constexpr auto t2_size = std::tuple_size<std::decay_t<T2>>::value;
107 constexpr auto t3_size = std::tuple_size<std::decay_t<T3>>::value;
108 static_assert(t1_size == t2_size,
109 "Size Mismatch between t1 and t2 in for_each");
110 static_assert(t1_size == t3_size,
111 "Size Mismatch between t1 and t3 in for_each");
112 return internal::for_each(std::forward<F>(f), std::forward<T1>(t1),
113 std::forward<T2>(t2), std::forward<T3>(t3),
114 std::make_index_sequence<t1_size>());
115}
116
117} // namespace math
118} // namespace stan
119
120#endif
constexpr auto for_each(F &&f, T &&t, std::index_sequence< Is... >)
Implementation of for_each.
Definition for_each.hpp:19
constexpr auto for_each(F &&f, T &&t)
Apply a function to each element of a tuple.
Definition for_each.hpp:66
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...