Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
json_data.hpp
Go to the documentation of this file.
1 #ifndef STAN__IO__JSON__JSON_DATA_HPP
2 #define STAN__IO__JSON__JSON_DATA_HPP
3 
4 #include <cctype>
5 #include <iostream>
6 #include <limits>
7 #include <map>
8 #include <sstream>
9 #include <string>
10 #include <vector>
11 #include <boost/throw_exception.hpp>
12 #include <boost/lexical_cast.hpp>
13 #include <stan/math/matrix.hpp>
14 #include <stan/io/var_context.hpp>
18 
19 
20 namespace stan {
21 
22  namespace json {
23 
47  private:
48  vars_map_r vars_r_;
49  vars_map_i vars_i_;
50 
51  std::vector<double> const empty_vec_r_;
52  std::vector<int> const empty_vec_i_;
53  std::vector<size_t> const empty_vec_ui_;
54 
64  bool contains_r_only(const std::string& name) const {
65  return vars_r_.find(name) != vars_r_.end();
66  }
67 
68  public:
69 
78  json_data(std::istream& in) : vars_r_(), vars_i_() {
79  json_data_handler handler(vars_r_, vars_i_);
80  stan::json::parse(in, handler);
81  }
82 
91  bool contains_r(const std::string& name) const {
92  return contains_r_only(name) || contains_i(name);
93  }
94 
103  bool contains_i(const std::string& name) const {
104  return vars_i_.find(name) != vars_i_.end();
105  }
106 
114  std::vector<double> vals_r(const std::string& name) const {
115  if (contains_r_only(name)) {
116  return (vars_r_.find(name)->second).first;
117  } else if (contains_i(name)) {
118  std::vector<int> vec_int = (vars_i_.find(name)->second).first;
119  std::vector<double> vec_r(vec_int.size());
120  for (size_t ii = 0; ii < vec_int.size(); ii++) {
121  vec_r[ii] = vec_int[ii];
122  }
123  return vec_r;
124  }
125  return empty_vec_r_;
126  }
127 
135  std::vector<size_t> dims_r(const std::string& name) const {
136  if (contains_r_only(name)) {
137  return (vars_r_.find(name)->second).second;
138  } else if (contains_i(name)) {
139  return (vars_i_.find(name)->second).second;
140  }
141  return empty_vec_ui_;
142  }
143 
151  std::vector<int> vals_i(const std::string& name) const {
152  if (contains_i(name)) {
153  return (vars_i_.find(name)->second).first;
154  }
155  return empty_vec_i_;
156  }
157 
165  std::vector<size_t> dims_i(const std::string& name) const {
166  if (contains_i(name)) {
167  return (vars_i_.find(name)->second).second;
168  }
169  return empty_vec_ui_;
170  }
171 
178  virtual void names_r(std::vector<std::string>& names) const {
179  names.resize(0);
180  for (vars_map_r::const_iterator it = vars_r_.begin();
181  it != vars_r_.end(); ++it)
182  names.push_back((*it).first);
183  }
184 
191  virtual void names_i(std::vector<std::string>& names) const {
192  names.resize(0);
193  for (vars_map_i::const_iterator it = vars_i_.begin();
194  it != vars_i_.end(); ++it)
195  names.push_back((*it).first);
196  }
197 
205  bool remove(const std::string& name) {
206  return (vars_i_.erase(name) > 0)
207  || (vars_r_.erase(name) > 0);
208  }
209 
210  };
211 
212  }
213 
214 }
215 #endif
std::vector< double > vals_r(const std::string &name) const
Return the double values for the variable with the specified name or null.
Definition: json_data.hpp:114
bool contains_r(const std::string &name) const
Return true if this json_data contains the specified variable name.
Definition: json_data.hpp:91
std::vector< int > vals_i(const std::string &name) const
Return the integer values for the variable with the specified name.
Definition: json_data.hpp:151
A json_data_handler is an implementation of a json_handler that restricts the allowed JSON text a set...
bool contains_i(const std::string &name) const
Return true if this json_data contains an integer valued array with the specified name...
Definition: json_data.hpp:103
json_data(std::istream &in)
Construct a json_data object from the specified input stream.
Definition: json_data.hpp:78
A var_reader reads array variables of integer and floating point type by name and dimension...
Definition: var_context.hpp:29
virtual void names_i(std::vector< std::string > &names) const
Return a list of the names of the integer variables in the json_data.
Definition: json_data.hpp:191
virtual void names_r(std::vector< std::string > &names) const
Return a list of the names of the floating point variables in the json_data.
Definition: json_data.hpp:178
std::vector< size_t > dims_i(const std::string &name) const
Return the dimensions for the integer variable with the specified name.
Definition: json_data.hpp:165
void parse(std::istream &in, Handler &handler)
Parse the JSON text represented by the specified input stream, sending events to the specified handle...
std::vector< size_t > dims_r(const std::string &name) const
Return the dimensions for the variable with the specified name.
Definition: json_data.hpp:135
A json_data is a var_context object that represents a set of named values which are typed to either d...
Definition: json_data.hpp:46

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