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>
7namespace stan {
8namespace math {
9namespace internal {
10
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>
27inline constexpr decltype(auto) apply_impl(F&& f, Tuple&& t,
28 std::index_sequence<I...> /* i */,
29 PreArgs&&... pre_args) {
30 return std::forward<F>(f)(std::forward<PreArgs>(pre_args)...,
31 std::get<I>(std::forward<Tuple>(t))...);
32}
33} // namespace internal
34
35/*
36 * Call the functor f with the tuple of arguments t, like:
37 *
38 * f(std::get<0>(t), std::get<1>(t), ...)
39 *
40 * TODO: replace this with implementation in C++ std when C++17 is available
41 *
42 * @tparam F Type of functor
43 * @tparam Tuple Type of tuple containing arguments
44 * @tparam PreArgs Parameter pack of arguments before the tuple
45 * @param f functor callable
46 * @param t tuple of arguments
47 * @param pre_args parameter pack of arguments to place before elements in
48 * tuple.
49 */
50template <class F, class Tuple, typename... PreArgs>
51inline constexpr decltype(auto) apply(F&& f, Tuple&& t, PreArgs&&... pre_args) {
53 std::forward<F>(f), std::forward<Tuple>(t),
54 std::make_index_sequence<
55 std::tuple_size<std::remove_reference_t<Tuple>>{}>{},
56 std::forward<PreArgs>(pre_args)...);
57}
58
59} // namespace math
60} // namespace stan
61
62#endif
constexpr decltype(auto) apply_impl(F &&f, Tuple &&t, std::index_sequence< I... >, PreArgs &&... pre_args)
Definition apply.hpp:27
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 ...