Automatic Differentiation
 
Loading...
Searching...
No Matches
apply.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUNCTOR_APPLY_HPP
2#define STAN_MATH_PRIM_FUNCTOR_APPLY_HPP
3
4#include <functional>
5#include <tuple>
6#include <utility>
7
8namespace stan {
9namespace math {
10namespace internal {
11/*
12 * Invoke the functor f with arguments given in t and indexed in the index
13 * sequence I with other arguments possibly before or after
14 *
15 * @tparam F Type of functor
16 * @tparam Tuple Type of tuple containing arguments
17 * @tparam PreArgs Parameter pack of arguments before the tuple
18 * @tparam I Parameter pack of an index sequence going from 0 to
19 * std::tuple_size<T>::value - 1 inclusive
20 * @param f functor callable
21 * @param t tuple of arguments
22 * @param i placeholder variable for index sequence
23 * @param pre_args parameter pack of arguments to place before elements in
24 * tuple.
25 */
26template <class F, class Tuple, typename... PreArgs, std::size_t... I>
27constexpr decltype(auto) apply_impl(F&& f, Tuple&& t,
28 std::index_sequence<I...> i,
29 PreArgs&&... pre_args) {
30 return std::forward<F>(f)(
31 std::forward<PreArgs>(pre_args)...,
32 std::forward<decltype(std::get<I>(t))>(std::get<I>(t))...);
33}
34} // namespace internal
35
36/*
37 * Call the functor f with the tuple of arguments t, like:
38 *
39 * f(std::get<0>(t), std::get<1>(t), ...)
40 *
41 * TODO: replace this with implementation in C++ std when C++17 is available
42 *
43 * @tparam F Type of functor
44 * @tparam Tuple Type of tuple containing arguments
45 * @tparam PreArgs Parameter pack of arguments before the tuple
46 * @param f functor callable
47 * @param t tuple of arguments
48 * @param pre_args parameter pack of arguments to place before elements in
49 * tuple.
50 */
51template <class F, class Tuple, typename... PreArgs>
52constexpr decltype(auto) apply(F&& f, Tuple&& t, PreArgs&&... pre_args) {
54 std::forward<F>(f), std::forward<Tuple>(t),
55 std::make_index_sequence<
56 std::tuple_size<std::remove_reference_t<Tuple>>{}>{},
57 std::forward<PreArgs>(pre_args)...);
58}
59
60} // namespace math
61} // namespace stan
62
63#endif
constexpr decltype(auto) apply_impl(F &&f, Tuple &&t, std::index_sequence< I... > i, PreArgs &&... pre_args)
Definition apply.hpp:27
constexpr decltype(auto) apply(F &&f, Tuple &&t, PreArgs &&... pre_args)
Definition apply.hpp:52
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...