1 #ifndef __STAN__IO__VAR_CONTEXT_HPP__
2 #define __STAN__IO__VAR_CONTEXT_HPP__
41 virtual bool contains_r(
const std::string& name)
const = 0;
55 virtual std::vector<double>
vals_r(
const std::string& name)
const = 0;
65 virtual std::vector<size_t>
dims_r(
const std::string& name)
const = 0;
75 virtual bool contains_i(
const std::string& name)
const = 0;
85 virtual std::vector<int>
vals_i(
const std::string& name)
const = 0;
95 virtual std::vector<size_t>
dims_i(
const std::string& name)
const = 0;
103 virtual void names_r(std::vector<std::string>& names)
const = 0;
111 virtual void names_i(std::vector<std::string>& names)
const = 0;
114 const std::vector<size_t>& dims)
const {
116 for (
size_t i = 0; i < dims.size(); ++i) {
117 if (i > 0) msg <<
',';
124 const std::string& name,
125 const std::string& base_type,
126 const std::vector<size_t>& dims_declared)
const {
127 bool is_int_type = base_type ==
"int";
130 std::stringstream msg;
132 ?
"int variable contained non-int values"
133 :
"variable does not exist" )
134 <<
"; processing stage=" << stage
135 <<
"; variable name=" << name
136 <<
"; base type=" << base_type;
137 throw std::runtime_error(msg.str());
141 std::stringstream msg;
142 msg <<
"variable does not exist"
143 <<
"; processing stage=" << stage
144 <<
"; variable name=" << name
145 <<
"; base type=" << base_type;
146 throw std::runtime_error(msg.str());
149 std::vector<size_t> dims =
dims_r(name);
150 if (dims.size() != dims_declared.size()) {
151 std::stringstream msg;
152 msg <<
"mismatch in number dimensions declared and found in context"
153 <<
"; processing stage=" << stage
154 <<
"; dims declared=";
156 msg <<
"; dims found=";
158 throw std::runtime_error(msg.str());
160 for (
size_t i = 0; i < dims.size(); ++i) {
161 if (dims_declared[i] != dims[i]) {
162 std::stringstream msg;
163 msg <<
"mismatch in dimension declared and found in context"
164 <<
"; processing stage=" << stage
167 <<
"; dims declared=";
169 msg <<
"; dims found=";
171 throw std::runtime_error(msg.str());
177 return std::vector<size_t>();
179 static std::vector<size_t>
to_vec(
size_t n1) {
180 std::vector<size_t> v(1);
184 static std::vector<size_t>
to_vec(
size_t n1,
size_t n2) {
185 std::vector<size_t> v(2);
190 static std::vector<size_t>
to_vec(
size_t n1,
size_t n2,
192 std::vector<size_t> v(3);
198 static std::vector<size_t>
to_vec(
size_t n1,
size_t n2,
199 size_t n3,
size_t n4) {
200 std::vector<size_t> v(4);
207 static std::vector<size_t>
to_vec(
size_t n1,
size_t n2,
208 size_t n3,
size_t n4,
210 std::vector<size_t> v(5);
218 static std::vector<size_t>
to_vec(
size_t n1,
size_t n2,
219 size_t n3,
size_t n4,
220 size_t n5,
size_t n6) {
221 std::vector<size_t> v(6);
230 static std::vector<size_t>
to_vec(
size_t n1,
size_t n2,
231 size_t n3,
size_t n4,
232 size_t n5,
size_t n6,
234 std::vector<size_t> v(7);
244 static std::vector<size_t>
to_vec(
size_t n1,
size_t n2,
245 size_t n3,
size_t n4,
246 size_t n5,
size_t n6,
247 size_t n7,
size_t n8) {
248 std::vector<size_t> v(8);
A var_reader reads array variables of integer and floating point type by name and dimension.
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 void names_i(std::vector< std::string > &names) const =0
Return a list of the names of the integer variables in the context.
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 std::vector< size_t > dims_i(const std::string &name) const =0
Return the dimensions of the specified floating point variable.
static std::vector< size_t > to_vec()
static std::vector< size_t > to_vec(size_t n1, size_t n2, size_t n3, size_t n4, size_t n5)
virtual bool contains_i(const std::string &name) const =0
Return true if the specified variable name has integer values.
void add_vec(std::stringstream &msg, const std::vector< size_t > &dims) const
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)
virtual void names_r(std::vector< std::string > &names) const =0
Return a list of the names of the floating point variables in the context.
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, size_t n7, size_t n8)
virtual std::vector< double > vals_r(const std::string &name) const =0
Return the floating point values for the variable of the specified variable name in last-index-major ...
virtual bool contains_r(const std::string &name) const =0
Return true if the specified variable name is defined.
static std::vector< size_t > to_vec(size_t n1)
static std::vector< size_t > to_vec(size_t n1, size_t n2, size_t n3)
virtual std::vector< size_t > dims_r(const std::string &name) const =0
Return the dimensions for the specified floating point variable.
virtual std::vector< int > vals_i(const std::string &name) const =0
Return the integer values for the variable of the specified name in last-index-major order or the emp...
Probability, optimization and sampling library.