Stan  2.10.0
probability, sampling & optimization
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 <boost/throw_exception.hpp>
5 #include <boost/lexical_cast.hpp>
10 #include <cctype>
11 #include <iostream>
12 #include <limits>
13 #include <map>
14 #include <sstream>
15 #include <string>
16 #include <vector>
17 
18 namespace stan {
19 
20  namespace json {
21 
45  private:
46  vars_map_r vars_r_;
47  vars_map_i vars_i_;
48 
49  std::vector<double> const empty_vec_r_;
50  std::vector<int> const empty_vec_i_;
51  std::vector<size_t> const empty_vec_ui_;
52 
62  bool contains_r_only(const std::string& name) const {
63  return vars_r_.find(name) != vars_r_.end();
64  }
65 
66  public:
75  explicit json_data(std::istream& in) : vars_r_(), vars_i_() {
76  json_data_handler handler(vars_r_, vars_i_);
77  stan::json::parse(in, handler);
78  }
79 
88  bool contains_r(const std::string& name) const {
89  return contains_r_only(name) || contains_i(name);
90  }
91 
100  bool contains_i(const std::string& name) const {
101  return vars_i_.find(name) != vars_i_.end();
102  }
103 
111  std::vector<double> vals_r(const std::string& name) const {
112  if (contains_r_only(name)) {
113  return (vars_r_.find(name)->second).first;
114  } else if (contains_i(name)) {
115  std::vector<int> vec_int = (vars_i_.find(name)->second).first;
116  std::vector<double> vec_r(vec_int.size());
117  for (size_t ii = 0; ii < vec_int.size(); ii++) {
118  vec_r[ii] = vec_int[ii];
119  }
120  return vec_r;
121  }
122  return empty_vec_r_;
123  }
124 
132  std::vector<size_t> dims_r(const std::string& name) const {
133  if (contains_r_only(name)) {
134  return (vars_r_.find(name)->second).second;
135  } else if (contains_i(name)) {
136  return (vars_i_.find(name)->second).second;
137  }
138  return empty_vec_ui_;
139  }
140 
148  std::vector<int> vals_i(const std::string& name) const {
149  if (contains_i(name)) {
150  return (vars_i_.find(name)->second).first;
151  }
152  return empty_vec_i_;
153  }
154 
162  std::vector<size_t> dims_i(const std::string& name) const {
163  if (contains_i(name)) {
164  return (vars_i_.find(name)->second).second;
165  }
166  return empty_vec_ui_;
167  }
168 
175  virtual void names_r(std::vector<std::string>& names) const {
176  names.resize(0);
177  for (vars_map_r::const_iterator it = vars_r_.begin();
178  it != vars_r_.end(); ++it)
179  names.push_back((*it).first);
180  }
181 
188  virtual void names_i(std::vector<std::string>& names) const {
189  names.resize(0);
190  for (vars_map_i::const_iterator it = vars_i_.begin();
191  it != vars_i_.end(); ++it)
192  names.push_back((*it).first);
193  }
194 
202  bool remove(const std::string& name) {
203  return (vars_i_.erase(name) > 0)
204  || (vars_r_.erase(name) > 0);
205  }
206  };
207 
208  }
209 
210 }
211 #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:111
bool contains_r(const std::string &name) const
Return true if this json_data contains the specified variable name.
Definition: json_data.hpp:88
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:148
Probability, optimization and sampling library.
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:100
json_data(std::istream &in)
Construct a json_data object from the specified input stream.
Definition: json_data.hpp:75
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:188
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:175
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:162
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:132
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:44

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