1 #ifndef STAN_IO_ARRAY_VAR_CONTEXT_HPP
2 #define STAN_IO_ARRAY_VAR_CONTEXT_HPP
5 #include <boost/throw_exception.hpp>
18 T product(std::vector<T> dims) {
20 for (
size_t i = 0; i < dims.size(); ++i)
34 std::pair<std::vector<double>,
35 std::vector<size_t> > > vars_r_;
37 std::pair<std::vector<int>,
38 std::vector<size_t> > > vars_i_;
39 std::vector<double>
const empty_vec_r_;
40 std::vector<int>
const empty_vec_i_;
41 std::vector<size_t>
const empty_vec_ui_;
43 bool contains_r_only(
const std::string& name)
const {
44 return vars_r_.find(name) != vars_r_.end();
53 void validate(
const std::vector<std::string>& names,
54 const std::vector<T>& array,
55 const std::vector<std::vector<size_t> >& dims) {
57 size_t num_par = names.size();
58 if (num_par > dims.size()) {
59 std::stringstream msg;
60 msg <<
"size of vector of dimensions (found " << dims.size() <<
") "
61 <<
"should be no smaller than number of parameters (found "
63 BOOST_THROW_EXCEPTION(std::invalid_argument(msg.str()));
65 for (
size_t i = 0; i < num_par; i++)
66 total += stan::io::product(dims[i]);
67 size_t array_len = array.size();
68 if (total > array_len) {
69 std::stringstream msg;
70 msg <<
"array is not long enough for all elements: " << array_len
72 << total <<
" is needed.";
73 BOOST_THROW_EXCEPTION(std::invalid_argument(msg.str()));
77 void add_r(
const std::vector<std::string>& names,
78 const std::vector<double>& values,
79 const std::vector<std::vector<size_t> >& dims) {
80 validate(names, values, dims);
83 for (
size_t i = 0; i < names.size(); i++) {
84 end += product(dims[i]);
85 std::vector<double> v(values.begin() + start, values.begin() + end);
87 = std::pair<std::vector<double>,
88 std::vector<size_t> >(v, dims[i]);
92 void add_i(
const std::vector<std::string>& names,
93 const std::vector<int>& values,
94 const std::vector<std::vector<size_t> >& dims) {
95 validate(names, values, dims);
98 for (
size_t i = 0; i < names.size(); i++) {
99 end += product(dims[i]);
100 std::vector<int> v(values.begin() + start, values.begin() + end);
102 = std::pair<std::vector<int>,
103 std::vector<size_t> >(v, dims[i]);
117 const std::vector<double>& values_r,
118 const std::vector<std::vector<size_t> >& dim_r) {
119 add_r(names_r, values_r, dim_r);
130 const std::vector<int>& values_i,
131 const std::vector<std::vector<size_t> >& dim_i) {
132 add_i(names_i, values_i, dim_i);
141 const std::vector<double>& values_r,
142 const std::vector<std::vector<size_t> >& dim_r,
143 const std::vector<std::string>&
names_i,
144 const std::vector<int>& values_i,
145 const std::vector<std::vector<size_t> >& dim_i) {
146 add_i(names_i, values_i, dim_i);
147 add_r(names_r, values_r, dim_r);
159 return contains_r_only(name) ||
contains_i(name);
171 return vars_i_.find(name) != vars_i_.end();
181 std::vector<double>
vals_r(
const std::string& name)
const {
182 if (contains_r_only(name)) {
183 return (vars_r_.find(name)->second).first;
185 std::vector<int> vec_int = (vars_i_.find(name)->second).first;
186 std::vector<double> vec_r(vec_int.size());
187 for (
size_t ii = 0; ii < vec_int.size(); ii++) {
188 vec_r[ii] = vec_int[ii];
202 std::vector<size_t>
dims_r(
const std::string& name)
const {
203 if (contains_r_only(name)) {
204 return (vars_r_.find(name)->second).second;
206 return (vars_i_.find(name)->second).second;
208 return empty_vec_ui_;
218 std::vector<int>
vals_i(
const std::string& name)
const {
220 return (vars_i_.find(name)->second).first;
232 std::vector<size_t>
dims_i(
const std::string& name)
const {
234 return (vars_i_.find(name)->second).second;
236 return empty_vec_ui_;
245 virtual void names_r(std::vector<std::string>& names)
const {
247 for (std::map<std::string,
248 std::pair<std::vector<double>,
249 std::vector<size_t> > >
250 ::const_iterator it = vars_r_.begin();
251 it != vars_r_.end(); ++it)
252 names.push_back((*it).first);
261 virtual void names_i(std::vector<std::string>& names)
const {
263 for (std::map<std::string,
264 std::pair<std::vector<int>,
265 std::vector<size_t> > >
266 ::const_iterator it = vars_i_.begin();
267 it != vars_i_.end(); ++it)
268 names.push_back((*it).first);
278 bool remove(
const std::string& name) {
279 return (vars_i_.erase(name) > 0)
280 || (vars_r_.erase(name) > 0);
std::vector< int > vals_i(const std::string &name) const
Return the integer values for the variable with the specified name.
std::vector< size_t > dims_i(const std::string &name) const
Return the dimensions for the integer variable with the specified name.
virtual void names_i(std::vector< std::string > &names) const
Return a list of the names of the integer variables in the dump.
Probability, optimization and sampling library.
array_var_context(const std::vector< std::string > &names_r, const std::vector< double > &values_r, const std::vector< std::vector< size_t > > &dim_r, const std::vector< std::string > &names_i, const std::vector< int > &values_i, const std::vector< std::vector< size_t > > &dim_i)
Construct an array_var_context from arrays of both double and integer separately. ...
std::vector< size_t > dims_r(const std::string &name) const
Return the dimensions for the double variable with the specified name.
bool contains_i(const std::string &name) const
Return true if this dump contains an integer valued array with the specified name.
array_var_context(const std::vector< std::string > &names_i, const std::vector< int > &values_i, const std::vector< std::vector< size_t > > &dim_i)
Construct an array_var_context from only integer value arrays.
virtual void names_r(std::vector< std::string > &names) const
Return a list of the names of the floating point variables in the dump.
A var_reader reads array variables of integer and floating point type by name and dimension...
bool contains_r(const std::string &name) const
Return true if this dump contains the specified variable name is defined.
std::vector< double > vals_r(const std::string &name) const
Return the double values for the variable with the specified name or null.
An array_var_context object represents a named arrays with dimensions constructed from an array...
array_var_context(const std::vector< std::string > &names_r, const std::vector< double > &values_r, const std::vector< std::vector< size_t > > &dim_r)
Construct an array_var_context from only real value arrays.