1 #ifndef STAN_MATH_PRIM_FUN_ARRAY_BUILDER_HPP 2 #define STAN_MATH_PRIM_FUN_ARRAY_BUILDER_HPP 3 4 #include <stan/math/prim/meta.hpp> 5 #include <stan/math/prim/fun/promote_elements.hpp> 6 #include <vector> 7 8 namespace stan { 9 namespace math { 10 11 /** 12 * Structure for building up arrays in an expression (rather than 13 * in statements) using an argument-chaining add() method and 14 * a getter method array() to return the result. 15 * Array elements are held in std::vector of type T. 16 * 17 * @tparam T type of array elements 18 */ 19 template <typename T> 20 class array_builder { 21 private: 22 std::vector<T> x_; 23 24 public: 25 /** 26 * Construct an array_builder. 27 */ array_builder()28 array_builder() : x_() {} 29 30 /** 31 * Add one element of type S to array, promoting to type T. 32 * 33 * @param u element to add 34 * @returns this array_builder object 35 */ 36 template <typename S> add(const S & u)37 array_builder& add(const S& u) { 38 x_.push_back(promote_elements<T, S>::promote(u)); 39 return *this; 40 } 41 42 /** 43 * Getter method to return array itself. 44 * 45 * @returns std:vector<T> of composed array elements. 46 */ array()47 std::vector<T> array() { return x_; } 48 }; 49 50 } // namespace math 51 } // namespace stan 52 #endif 53