Stan  2.10.0
probability, sampling & optimization
stream_writer.hpp
Go to the documentation of this file.
1 #ifndef STAN_INTERFACE_CALLBACKS_WRITER_STREAM_WRITER_HPP
2 #define STAN_INTERFACE_CALLBACKS_WRITER_STREAM_WRITER_HPP
3 
5 #include <ostream>
6 #include <vector>
7 #include <string>
8 
9 namespace stan {
10  namespace interface_callbacks {
11  namespace writer {
12 
16  class stream_writer : public base_writer {
17  public:
25  stream_writer(std::ostream& output,
26  const std::string& key_value_prefix = ""):
27  output__(output), key_value_prefix__(key_value_prefix) {}
28 
29  void operator()(const std::string& key, double value) {
30  output__ << key_value_prefix__ << key << " = " << value << std::endl;
31  }
32 
33  void operator()(const std::string& key, int value) {
34  output__ << key_value_prefix__ << key << " = " << value << std::endl;
35  }
36 
37  void operator()(const std::string& key, const std::string& value) {
38  output__ << key_value_prefix__ << key << " = " << value << std::endl;
39  }
40 
41  void operator()(const std::string& key,
42  const double* values,
43  int n_values) {
44  if (n_values == 0) return;
45 
46  output__ << key_value_prefix__ << key << ": ";
47 
48  output__ << values[0];
49  for (int n = 1; n < n_values; ++n)
50  output__ << "," << values[n];
51  output__ << std::endl;
52  }
53 
54  void operator()(const std::string& key,
55  const double* values,
56  int n_rows, int n_cols) {
57  if (n_rows == 0 || n_cols == 0) return;
58 
59  output__ << key_value_prefix__ << key << std::endl;
60 
61  for (int i = 0; i < n_rows; ++i) {
62  output__ << key_value_prefix__ << values[i * n_cols];
63  for (int j = 1; j < n_cols; ++j)
64  output__ << "," << values[i * n_cols + j];
65  output__ << std::endl;
66  }
67  }
68 
69  void operator()(const std::vector<std::string>& names) {
70  if (names.empty()) return;
71 
72  std::vector<std::string>::const_iterator last = names.end();
73  --last;
74 
75  for (std::vector<std::string>::const_iterator it = names.begin();
76  it != last; ++it)
77  output__ << *it << ",";
78  output__ << names.back() << std::endl;
79  }
80 
81  void operator()(const std::vector<double>& state) {
82  if (state.empty()) return;
83 
84  std::vector<double>::const_iterator last = state.end();
85  --last;
86 
87  for (std::vector<double>::const_iterator it = state.begin();
88  it != last; ++it)
89  output__ << *it << ",";
90  output__ << state.back() << std::endl;
91  }
92 
93  void operator()() {
94  output__ << key_value_prefix__ << std::endl;
95  }
96 
97  void operator()(const std::string& message) {
98  output__ << key_value_prefix__ << message << std::endl;
99  }
100 
101  private:
102  std::ostream& output__;
103  std::string key_value_prefix__;
104  };
105 
106  }
107  }
108 }
109 
110 #endif
stream_writer writes to an std::ostream.
Probability, optimization and sampling library.
void operator()(const std::string &key, double value)
Writes a key, value pair.
void operator()(const std::string &key, const double *values, int n_rows, int n_cols)
Writes a key, value pair.
void operator()(const std::vector< std::string > &names)
Writes a set of names.
void operator()(const std::string &key, int value)
Writes a key, value pair.
void operator()(const std::string &key, const double *values, int n_values)
Writes a key, value pair.
base_writer is an abstract base class defining the interface for Stan writer callbacks.
Definition: base_writer.hpp:20
void operator()(const std::vector< double > &state)
Writes a set of values.
stream_writer(std::ostream &output, const std::string &key_value_prefix="")
Constructor.
void operator()(const std::string &key, const std::string &value)
Writes a key, value pair.
void operator()(const std::string &message)
Writes a string.

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