Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
csv_writer.hpp
Go to the documentation of this file.
1 #ifndef STAN__IO__CSV_WRITER_HPP
2 #define STAN__IO__CSV_WRITER_HPP
3 
4 #include <ostream>
5 #include <limits>
6 #include <iomanip>
7 #include <stan/math/matrix.hpp>
10 
11 namespace stan {
12 
13  namespace io {
14 
23  class csv_writer {
24  private:
25  std::ostream& o_;
26  bool at_bol_;
27 
28  public:
29 
36  void comma() {
37  if (at_bol_) {
38  at_bol_ = false;
39  return;
40  }
41  o_ << ",";
42  }
43 
44 
51  csv_writer(std::ostream& o)
52  : o_(o), at_bol_(true) {
53  }
54 
63  void newline() {
64  o_ << "\n";
65  at_bol_ = true;
66  }
67 
76  void write(int n) {
77  comma();
78  o_ << n;
79  }
80 
89  void write(double x) {
90  comma();
91  o_ << std::setprecision (std::numeric_limits<double>::digits10 + 1)
92  << x;
93  }
94 
103  template<int R, int C>
104  void write_row_major(const Eigen::Matrix<double,R,C>& m) {
105  using Eigen::Matrix;
107  typedef typename index_type<Matrix<double,R,C> >::type idx_t;
108  for (idx_t i = 0; i < m.rows(); ++i)
109  for (idx_t j = 0; j < m.cols(); ++j)
110  write(m(i,j));
111  }
112 
121  template<int R, int C>
122  void write_col_major(const Eigen::Matrix<double,R,C>& m) {
123  using Eigen::Matrix;
125  typedef typename index_type<Matrix<double,R,C> >::type idx_t;
126 
127  for (idx_t j = 0; j < m.cols(); ++j)
128  for (idx_t i = 0; i < m.rows(); ++i)
129  write(m(i,j));
130  }
131 
132  template<int R, int C>
133  void write(const Eigen::Matrix<double,R,C>& m) {
134  write_col_major(m);
135  }
136 
146  void write(const std::string& s) {
147  comma();
148 
149  o_ << '"';
150  for (size_t i = 0; i < s.size(); ++i) {
151  if (s.at(i) == '"') {
152  o_ << '"' << '"'; // double quotes
153  } else {
154  o_ << s.at(i);
155  }
156  }
157  o_ << '"';
158  }
159 
160 
161  };
162 
163  }
164 
165 }
166 
167 #endif
void newline()
Write a newline character.
Definition: csv_writer.hpp:63
void write_col_major(const Eigen::Matrix< double, R, C > &m)
Write a value in column-major order.
Definition: csv_writer.hpp:122
void write(const std::string &s)
Write a value.
Definition: csv_writer.hpp:146
Primary template class for the metaprogram to compute the index type of a container.
Definition: index_type.hpp:21
Writes Stan variables in comma-separated-value format to an output stream.
Definition: csv_writer.hpp:23
void write(int n)
Write a value.
Definition: csv_writer.hpp:76
csv_writer(std::ostream &o)
Construct a CSV writer that writes to the specified output stream.
Definition: csv_writer.hpp:51
void comma()
Write a comma.
Definition: csv_writer.hpp:36
void write_row_major(const Eigen::Matrix< double, R, C > &m)
Write a value.
Definition: csv_writer.hpp:104
void write(double x)
Write a value.
Definition: csv_writer.hpp:89
void write(const Eigen::Matrix< double, R, C > &m)
Definition: csv_writer.hpp:133

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