1 #ifndef STAN_MATH_PRIM_FUN_VEC_CONCAT_HPP
2 #define STAN_MATH_PRIM_FUN_VEC_CONCAT_HPP
3 
4 #include <stan/math/prim/meta.hpp>
5 #include <type_traits>
6 #include <vector>
7 
8 namespace stan {
9 namespace math {
10 namespace internal {
sum_vector_sizes()11 inline auto sum_vector_sizes() { return 0; }
12 /**
13  * Get the internal sizes of a pack of vectors.
14  * @tparam Vec Type of first vector to count.
15  * @tparam VecArgs Parameter pack of vector types to accumulate sizes of
16  * @param x vector to start accumulation of sizes with.
17  * @param args pack of vectors to accumulate sizes for.
18  */
19 template <typename Vec, typename... VecArgs>
sum_vector_sizes(const Vec & x,const VecArgs &...args)20 inline auto sum_vector_sizes(const Vec& x, const VecArgs&... args) {
21   return x.size() + sum_vector_sizes(args...);
22 }
23 /**
24  * End of recursion for appending to vector.
25  */
26 template <typename VecInOut>
append_vectors(VecInOut & x)27 inline void append_vectors(VecInOut& x) {}
28 /**
29  * Fill a vector with other vectors.
30  * @tparam VecInOut Type of vector to be filled.
31  * @tparam VecIn Type of the first vector to insert into the in/out vector.
32  * @tparam VecArgs Parameter pack of other vectors to fill in the in/out vector.
33  * @param x Vector to be filled.
34  * @param y First vector to insert into the in/out vector.
35  * @param args Pack of other vectors to fill in the in/out vector.
36  */
37 template <typename VecInOut, typename VecIn, typename... VecArgs>
append_vectors(VecInOut & x,const VecIn & y,const VecArgs &...args)38 inline void append_vectors(VecInOut& x, const VecIn& y,
39                            const VecArgs&... args) {
40   x.insert(x.end(), y.begin(), y.end());
41   append_vectors(x, args...);
42 }
43 }  // namespace internal
44 
45 /**
46  * Get the event stack from a vector of events and other arguments.
47  *
48  * @tparam Vec type of elements in the array
49  * @tparam Args types for variadic arguments
50  * @param v1 event stack to roll up
51  * @param args variadic arguments passed down to the next recursion
52  * @return Vector of OpenCL events
53  */
54 template <typename Vec, typename... Args>
vec_concat(const Vec & v1,const Args &...args)55 inline auto vec_concat(const Vec& v1, const Args&... args) {
56   std::vector<value_type_t<Vec>> vec;
57   vec.reserve(internal::sum_vector_sizes(v1, args...));
58   internal::append_vectors(vec, v1, args...);
59   return vec;
60 }
61 
62 }  // namespace math
63 }  // namespace stan
64 
65 #endif
66