CASM  1.1.0
A Clusters Approach to Statistical Mechanics
EigenDataStream.hh
Go to the documentation of this file.
1 #ifndef EIGENDATASTREAM_HH
2 #define EIGENDATASTREAM_HH
3 
5 #include "casm/external/Eigen/Dense"
6 #include "casm/global/eigen.hh"
7 
8 namespace CASM {
9 
13  public:
15  : DataStream(_traits), m_matrix(1, 0), m_row(0), m_col(0) {}
16  DataStream &operator<<(double _d) {
17  if (m_col == m_matrix.cols()) {
18  if (m_row > 0 || m_matrix.rows() != 1)
19  throw std::runtime_error(
20  "Attempting to stream non-rectangular data to Eigen::MatrixXd "
21  "using MatrixXdDataStream, at row=" +
22  std::to_string(m_row) + ", col=" + std::to_string(m_row) + "\n");
23  m_matrix.conservativeResize(Eigen::NoChange, m_col + 1);
24  }
25  if (m_row == m_matrix.rows()) {
26  m_matrix.conservativeResize(m_row + 1, Eigen::NoChange);
27  }
28  m_matrix(m_row, m_col++) = _d;
29  return *this;
30  }
31 
32  DataStream &operator<<(long _l) { return operator<<((double)_l); }
33 
34  virtual DataStream &newline() {
35  if (fail() && _skipfail()) {
36  clear_fail();
37  } else
38  ++m_row;
39  m_col = 0;
40  return *this;
41  }
42 
43  const Eigen::MatrixXd &matrix() { return m_matrix; }
44 
45  protected:
46  EigenIndex row() const { return m_row; }
47 
48  private:
51 };
52 
53 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
54 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
55 
59  public:
61  : MatrixXdDataStream(_traits) {}
62  const std::vector<std::string> &labels() const { return m_labels; }
63 
64  DataStream &operator<<(const std::string &_str) {
65  if (labels().size() == row())
66  m_labels.push_back(_str);
67  else if (row() + 1 == labels().size()) {
68  std::cerr << "WARNING: Attempting to collect labeled data from a "
69  "datastream, but too many labels exist for row "
70  << row() << "\n";
72  }
73  return *this;
74  }
75 
77  if (!fail() && labels().size() < row() + 1) {
78  std::cerr << "WARNING: Attempting to collect labeled data from a "
79  "datastream, but no label was available for row "
80  << row() << "\n";
82  } else if (_skipfail() && fail() && row() + 1 == labels().size()) {
83  m_labels.pop_back();
84  }
85 
87  }
88 
89  private:
90  std::vector<std::string> m_labels;
91 };
92 } // namespace CASM
93 
94 #endif
virtual DataStream & operator<<(const std::string &)
Definition: DataStream.hh:26
bool fail() const
Definition: DataStream.hh:60
const std::vector< std::string > & labels() const
std::vector< std::string > m_labels
DataStream & operator<<(const std::string &_str)
LabeledMatrixXdDataStream(DataStreamTraits _traits=none)
EigenIndex row() const
virtual DataStream & newline()
DataStream & operator<<(long _l)
MatrixXdDataStream(DataStreamTraits _traits=none)
const Eigen::MatrixXd & matrix()
DataStream & operator<<(double _d)
std::string to_string(ENUM val)
Return string representation of enum class.
Definition: io_traits.hh:172
Main CASM namespace.
Definition: APICommand.hh:8
Eigen::MatrixXd MatrixXd
Eigen::MatrixXd::Index EigenIndex
Definition: eigen.hh:7