var_context.hpp
Go to the documentation of this file.
1 #ifndef STAN_IO_VAR_CONTEXT_HPP
2 #define STAN_IO_VAR_CONTEXT_HPP
3 
4 #include <sstream>
5 #include <stdexcept>
6 #include <string>
7 #include <vector>
8 
9 namespace stan {
10 
11  namespace io {
12 
13  /**
14  * A <code>var_context</code> reads array variables of integer and
15  * floating point type by name and dimension.
16  *
17  * <p>An array's dimensionality is described by a sequence of
18  * (unsigned) integers. For instance, <code>(7, 2, 3)</code> picks
19  * out a 7 by 2 by 3 array. The empty dimensionality sequence
20  * <code>()</code> is used for scalars.
21  *
22  * <p>Multidimensional arrays are stored in column-major order,
23  * meaning the first index changes the most quickly.
24  *
25  * <p>If a variable has integer variables, it should return
26  * those integer values cast to floating point values when
27  * accessed through the floating-point methods.
28  */
29  class var_context {
30  public:
31  virtual ~var_context() {}
32 
33  /**
34  * Return <code>true</code> if the specified variable name is
35  * defined. This method should return <code>true</code> even
36  * if the values are all integers.
37  *
38  * @param name Name of variable.
39  * @return <code>true</code> if the variable exists with real
40  * values.
41  */
42  virtual bool contains_r(const std::string& name) const = 0;
43 
44  /**
45  * Return the floating point values for the variable of the
46  * specified variable name in last-index-major order. This
47  * method should cast integers to floating point values if the
48  * values of the named variable are all integers.
49  *
50  * <p>If there is no variable of the specified name, the empty
51  * vector is returned.
52  *
53  * @param name Name of variable.
54  * @return Sequence of values for the named variable.
55  */
56  virtual std::vector<double> vals_r(const std::string& name) const = 0;
57 
58  /**
59  * Return the dimensions for the specified floating point variable.
60  * If the variable doesn't exist or if it is a scalar, the
61  * return result should be the empty vector.
62  *
63  * @param name Name of variable.
64  * @return Sequence of dimensions for the variable.
65  */
66  virtual std::vector<size_t> dims_r(const std::string& name) const = 0;
67 
68  /**
69  * Return <code>true</code> if the specified variable name has
70  * integer values.
71  *
72  * @param name Name of variable.
73  * @return <code>true</code> if an integer variable of the specified
74  * name is defined.
75  */
76  virtual bool contains_i(const std::string& name) const = 0;
77 
78  /**
79  * Return the integer values for the variable of the specified
80  * name in last-index-major order or the empty sequence if the
81  * variable is not defined.
82  *
83  * @param name Name of variable.
84  * @return Sequence of integer values.
85  */
86  virtual std::vector<int> vals_i(const std::string& name) const = 0;
87 
88  /**
89  * Return the dimensions of the specified floating point variable.
90  * If the variable doesn't exist (or if it is a scalar), the
91  * return result should be the empty vector.
92  *
93  * @param name Name of variable.
94  * @return Sequence of dimensions for the variable.
95  */
96  virtual std::vector<size_t> dims_i(const std::string& name) const = 0;
97 
98  /**
99  * Fill a list of the names of the floating point variables in
100  * the context.
101  *
102  * @param names Vector to store the list of names in.
103  */
104  virtual void names_r(std::vector<std::string>& names) const = 0;
105 
106  /**
107  * Fill a list of the names of the integer variables in
108  * the context.
109  *
110  * @param names Vector to store the list of names in.
111  */
112  virtual void names_i(std::vector<std::string>& names) const = 0;
113 
114  void add_vec(std::stringstream& msg,
115  const std::vector<size_t>& dims) const {
116  msg << '(';
117  for (size_t i = 0; i < dims.size(); ++i) {
118  if (i > 0) msg << ',';
119  msg << dims[i];
120  }
121  msg << ')';
122  }
123 
124  void validate_dims(const std::string& stage,
125  const std::string& name,
126  const std::string& base_type,
127  const std::vector<size_t>& dims_declared) const {
128  bool is_int_type = base_type == "int";
129  if (is_int_type) {
130  if (!contains_i(name)) {
131  std::stringstream msg;
132  msg << (contains_r(name)
133  ? "int variable contained non-int values"
134  : "variable does not exist" )
135  << "; processing stage=" << stage
136  << "; variable name=" << name
137  << "; base type=" << base_type;
138  throw std::runtime_error(msg.str());
139  }
140  } else {
141  if (!contains_r(name)) {
142  std::stringstream msg;
143  msg << "variable does not exist"
144  << "; processing stage=" << stage
145  << "; variable name=" << name
146  << "; base type=" << base_type;
147  throw std::runtime_error(msg.str());
148  }
149  }
150  std::vector<size_t> dims = dims_r(name);
151  if (dims.size() != dims_declared.size()) {
152  std::stringstream msg;
153  msg << "mismatch in number dimensions declared and found in context"
154  << "; processing stage=" << stage
155  << "; variable name=" << name
156  << "; dims declared=";
157  add_vec(msg, dims_declared);
158  msg << "; dims found=";
159  add_vec(msg, dims);
160  throw std::runtime_error(msg.str());
161  }
162  for (size_t i = 0; i < dims.size(); ++i) {
163  if (dims_declared[i] != dims[i]) {
164  std::stringstream msg;
165  msg << "mismatch in dimension declared and found in context"
166  << "; processing stage=" << stage
167  << "; variable name=" << name
168  << "; position="
169  << i
170  << "; dims declared=";
171  add_vec(msg, dims_declared);
172  msg << "; dims found=";
173  add_vec(msg, dims);
174  throw std::runtime_error(msg.str());
175  }
176  }
177  }
178 
179  static std::vector<size_t> to_vec() {
180  return std::vector<size_t>();
181  }
182  static std::vector<size_t> to_vec(size_t n1) {
183  std::vector<size_t> v(1);
184  v[0] = n1;
185  return v;
186  }
187  static std::vector<size_t> to_vec(size_t n1, size_t n2) {
188  std::vector<size_t> v(2);
189  v[0] = n1;
190  v[1] = n2;
191  return v;
192  }
193  static std::vector<size_t> to_vec(size_t n1, size_t n2,
194  size_t n3) {
195  std::vector<size_t> v(3);
196  v[0] = n1;
197  v[1] = n2;
198  v[2] = n3;
199  return v;
200  }
201  static std::vector<size_t> to_vec(size_t n1, size_t n2,
202  size_t n3, size_t n4) {
203  std::vector<size_t> v(4);
204  v[0] = n1;
205  v[1] = n2;
206  v[2] = n3;
207  v[3] = n4;
208  return v;
209  }
210  static std::vector<size_t> to_vec(size_t n1, size_t n2,
211  size_t n3, size_t n4,
212  size_t n5) {
213  std::vector<size_t> v(5);
214  v[0] = n1;
215  v[1] = n2;
216  v[2] = n3;
217  v[3] = n4;
218  v[4] = n5;
219  return v;
220  }
221  static std::vector<size_t> to_vec(size_t n1, size_t n2,
222  size_t n3, size_t n4,
223  size_t n5, size_t n6) {
224  std::vector<size_t> v(6);
225  v[0] = n1;
226  v[1] = n2;
227  v[2] = n3;
228  v[3] = n4;
229  v[4] = n5;
230  v[5] = n6;
231  return v;
232  }
233  static std::vector<size_t> to_vec(size_t n1, size_t n2,
234  size_t n3, size_t n4,
235  size_t n5, size_t n6,
236  size_t n7) {
237  std::vector<size_t> v(7);
238  v[0] = n1;
239  v[1] = n2;
240  v[2] = n3;
241  v[3] = n4;
242  v[4] = n5;
243  v[5] = n6;
244  v[6] = n7;
245  return v;
246  }
247  static std::vector<size_t> to_vec(size_t n1, size_t n2,
248  size_t n3, size_t n4,
249  size_t n5, size_t n6,
250  size_t n7, size_t n8) {
251  std::vector<size_t> v(8);
252  v[0] = n1;
253  v[1] = n2;
254  v[2] = n3;
255  v[3] = n4;
256  v[4] = n5;
257  v[5] = n6;
258  v[6] = n7;
259  v[7] = n8;
260  return v;
261  }
262  };
263 
264 
265  }
266 
267 }
268 
269 #endif
const XML_Char * name
Definition: expat.h:151
static std::vector< size_t > to_vec(size_t n1, size_t n2, size_t n3, size_t n4)
static std::vector< size_t > to_vec(size_t n1, size_t n2, size_t n3, size_t n4, size_t n5, size_t n6)
virtual std::vector< int > vals_i(const std::string &name) const =0
static std::vector< size_t > to_vec()
virtual std::vector< double > vals_r(const std::string &name) const =0
void validate_dims(const std::string &stage, const std::string &name, const std::string &base_type, const std::vector< size_t > &dims_declared) const
virtual bool contains_r(const std::string &name) const =0
virtual bool contains_i(const std::string &name) const =0
static std::vector< size_t > to_vec(size_t n1)
virtual void names_i(std::vector< std::string > &names) const =0
static std::vector< size_t > to_vec(size_t n1, size_t n2, size_t n3, size_t n4, size_t n5, size_t n6, size_t n7)
virtual void names_r(std::vector< std::string > &names) const =0
static std::vector< size_t > to_vec(size_t n1, size_t n2, size_t n3)
void add_vec(std::stringstream &msg, const std::vector< size_t > &dims) const
virtual std::vector< size_t > dims_r(const std::string &name) const =0
virtual std::vector< size_t > dims_i(const std::string &name) const =0
static std::vector< size_t > to_vec(size_t n1, size_t n2)
static std::vector< size_t > to_vec(size_t n1, size_t n2, size_t n3, size_t n4, size_t n5, size_t n6, size_t n7, size_t n8)
static std::vector< size_t > to_vec(size_t n1, size_t n2, size_t n3, size_t n4, size_t n5)
enum BeamMode string