Stan  2.10.0
probability, sampling & optimization
array_var_context.hpp
Go to the documentation of this file.
1 #ifndef STAN_IO_ARRAY_VAR_CONTEXT_HPP
2 #define STAN_IO_ARRAY_VAR_CONTEXT_HPP
3 
5 #include <boost/throw_exception.hpp>
6 #include <map>
7 #include <sstream>
8 #include <string>
9 #include <vector>
10 #include <utility>
11 
12 namespace stan {
13 
14  namespace io {
15 
16  namespace {
17  template<typename T>
18  T product(std::vector<T> dims) {
19  T y = 1;
20  for (size_t i = 0; i < dims.size(); ++i)
21  y *= dims[i];
22  return y;
23  }
24  }
25 
31  class array_var_context : public var_context {
32  private:
33  std::map<std::string,
34  std::pair<std::vector<double>,
35  std::vector<size_t> > > vars_r_;
36  std::map<std::string,
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_;
42 
43  bool contains_r_only(const std::string& name) const {
44  return vars_r_.find(name) != vars_r_.end();
45  }
46 
52  template <typename T>
53  void validate(const std::vector<std::string>& names,
54  const std::vector<T>& array,
55  const std::vector<std::vector<size_t> >& dims) {
56  size_t total = 0;
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 "
62  << num_par << ").";
63  BOOST_THROW_EXCEPTION(std::invalid_argument(msg.str()));
64  }
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
71  << " is found, but "
72  << total << " is needed.";
73  BOOST_THROW_EXCEPTION(std::invalid_argument(msg.str()));
74  }
75  }
76 
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);
81  size_t start = 0;
82  size_t end = 0;
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);
86  vars_r_[names[i]]
87  = std::pair<std::vector<double>,
88  std::vector<size_t> >(v, dims[i]);
89  start = end;
90  }
91  }
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);
96  size_t start = 0;
97  size_t end = 0;
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);
101  vars_i_[names[i]]
102  = std::pair<std::vector<int>,
103  std::vector<size_t> >(v, dims[i]);
104  start = end;
105  }
106  }
107 
108  public:
116  array_var_context(const std::vector<std::string>& names_r,
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);
120  }
121 
129  array_var_context(const std::vector<std::string>& names_i,
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);
133  }
134 
140  array_var_context(const std::vector<std::string>& names_r,
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);
148  }
149 
158  bool contains_r(const std::string& name) const {
159  return contains_r_only(name) || contains_i(name);
160  }
161 
170  bool contains_i(const std::string& name) const {
171  return vars_i_.find(name) != vars_i_.end();
172  }
173 
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;
184  } else if (contains_i(name)) {
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];
189  }
190  return vec_r;
191  }
192  return empty_vec_r_;
193  }
194 
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;
205  } else if (contains_i(name)) {
206  return (vars_i_.find(name)->second).second;
207  }
208  return empty_vec_ui_;
209  }
210 
218  std::vector<int> vals_i(const std::string& name) const {
219  if (contains_i(name)) {
220  return (vars_i_.find(name)->second).first;
221  }
222  return empty_vec_i_;
223  }
224 
232  std::vector<size_t> dims_i(const std::string& name) const {
233  if (contains_i(name)) {
234  return (vars_i_.find(name)->second).second;
235  }
236  return empty_vec_ui_;
237  }
238 
245  virtual void names_r(std::vector<std::string>& names) const {
246  names.resize(0);
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);
253  }
254 
261  virtual void names_i(std::vector<std::string>& names) const {
262  names.resize(0);
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);
269  }
270 
278  bool remove(const std::string& name) {
279  return (vars_i_.erase(name) > 0)
280  || (vars_r_.erase(name) > 0);
281  }
282  };
283  }
284 }
285 #endif
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...
Definition: var_context.hpp:29
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.

     [ Stan Home Page ] © 2011–2016, Stan Development Team.