Minsky: 3.17.0
godleyTable.h
Go to the documentation of this file.
1 /*
2  @copyright Steve Keen 2012
3  @author Russell Standish
4  This file is part of Minsky.
5 
6  Minsky is free software: you can redistribute it and/or modify it
7  under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Minsky is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with Minsky. If not, see <http://www.gnu.org/licenses/>.
18 */
19 #ifndef GODLEYTABLE_H
20 #define GODLEYTABLE_H
21 
22 #include <set>
23 #include <vector>
24 
25 #include "variable.h"
26 #include "assetClass.h"
27 
28 namespace minsky
29 {
30  using namespace std;
31  using classdesc::shared_ptr;
32 
34  {
35  public:
36 
37  friend struct SchemaHelper;
38  friend class GodleyIcon;
39  typedef std::vector<std::vector<string>> Data;
40  private:
43  vector<AssetClass> m_assetClass{noAssetClass, asset, liability, equity};
45 
46  static void markEdited();
47  void _resize(unsigned rows, unsigned cols) {
48  // resize existing
49  for (std::size_t i=0; i<data.size(); ++i) data[i].resize(cols);
50  data.resize(rows, vector<string>(cols));
51  m_assetClass.resize(cols, noAssetClass);
52  }
53  public:
55 
57 
58  std::string title;
59 
60  static const char* initialConditions;
61  GodleyTable(): doubleEntryCompliant(true)
62  {
63  _resize(2,4);
64  cell(1,0)=initialConditions;
65 
66  }
67 
68  // Perform deep comparison of Godley tables in history to avoid spurious noAssetClass columns from arising during undo. For ticket 1118.
69  bool operator==(const GodleyTable& other) const
70  {
71  return (data==other.data && m_assetClass==other.m_assetClass &&
72  doubleEntryCompliant==other.doubleEntryCompliant && title==other.title);
73  }
74 
76  const vector<AssetClass>& assetClass() const {return m_assetClass;}
77  AssetClass assetClass(std::size_t col) const;
78  AssetClass assetClass(std::size_t col, AssetClass cls);
79 
82  bool singleEquity() const;
83 
87  void nameUnique();
88 
92  bool signConventionReversed(int col) const
93  {
94  return doubleEntryCompliant &&
95  (assetClass(col)==liability || assetClass(col)==equity);
96  }
97 
99  bool initialConditionRow(unsigned row) const;
100 
102  bool singularRow(unsigned row, unsigned col);
103 
104  std::size_t rows() const {return data.size();}
105  std::size_t cols() const {return data.empty()? 0: data[0].size();}
106 
107  void clear() {data.clear(); m_assetClass.clear(); markEdited();}
108  void resize(unsigned rows, unsigned cols){_resize(rows,cols); markEdited();}
109 
118  void insertRow(unsigned row);
121  void deleteRow(unsigned row) {data.erase(data.begin()+row);}
123  void insertCol(unsigned col);
125  void deleteCol(unsigned col);
128  void moveRow(int row, int n);
130  void moveCol(int col, int n);
131 
132  void dimension(unsigned rows, unsigned cols) {clear(); resize(rows,cols);}
133 
134  string& cell(unsigned row, unsigned col) {
135  if (row>=rows() || col>=cols())
136  _resize(row+1, col+1);
137  if (data[row].size()<=col) data[row].resize(cols());
138  return data[row][col];
139  }
140  const string& cell(unsigned row, unsigned col) const {
141  if (row>=data.size() || col>=data[row].size())
142  throw std::out_of_range("Godley table index error");
143  return data[row][col];
144  }
145  bool cellInTable(int row, int col) const
146  {return row>=0 && std::size_t(row)<rows() && col>=0 && std::size_t(col)<cols();}
147  string getCell(unsigned row, unsigned col) const {
148  if (row<rows() && col<cols())
149  return cell(row,col);
150  else
151  return "";
152  }
153  void setCell(unsigned row, unsigned col, const string& data) {
154  cell(row,col)=data;
155  }
156 
158  void balanceEquity(int col);
159 
161  std::vector<std::string> getColumnVariables() const;
164  std::vector<std::string> getVariables() const;
167  std::string savedText;
168 
170  std::vector<std::string> getColumn(unsigned col) const;
171 
173  void setDEmode(bool doubleEntryCompliant);
174 
175  std::map<std::string,double> rowSumAsMap(int row) const;
176  static std::string stringify(const std::map<std::string,double>&);
177 
179  std::string rowSum(int row) const {return stringify(rowSumAsMap(row));}
180 
182  const Data& getData() const {return data;}
183 
184  void exportToLaTeX(const std::string& filename) const;
185  void exportToCSV(const std::string& filename) const;
186 
188  void orderAssetClasses();
189 
191  void rename(const std::string& from, const std::string& to);
193  void renameFlows(const std::string& from, const std::string& to);
195  void renameStock(const std::string& from, const std::string& to);
196  };
197 
198 }
199 
200 #include "godleyTable.cd"
201 #include "godleyTable.xcd"
202 #endif
std::size_t cols() const
Definition: godleyTable.h:105
const Data & getData() const
accessor for schema access
Definition: godleyTable.h:182
bool cellInTable(int row, int col) const
Definition: godleyTable.h:145
const vector< AssetClass > & assetClass() const
class of each column (used in DE compliant mode)
Definition: godleyTable.h:76
void exportToLaTeX(std::ostream &f, const GodleyTable &g)
Definition: godleyExport.cc:84
vector< AssetClass > m_assetClass
class of each column (used in DE compliant mode)
Definition: godleyTable.h:43
void deleteRow(unsigned row)
delete row at row
Definition: godleyTable.h:121
STL namespace.
std::string rowSum(int row) const
return the symbolic sum across a row
Definition: godleyTable.h:179
string & cell(unsigned row, unsigned col)
Definition: godleyTable.h:134
string getCell(unsigned row, unsigned col) const
Definition: godleyTable.h:147
std::size_t rows() const
Definition: godleyTable.h:104
void resize(unsigned rows, unsigned cols)
Definition: godleyTable.h:108
void setCell(unsigned row, unsigned col, const string &data)
Definition: godleyTable.h:153
#define CLASSDESC_ACCESS(type)
const string & cell(unsigned row, unsigned col) const
Definition: godleyTable.h:140
GodleyAssetClass::AssetClass AssetClass
Definition: godleyTable.h:54
std::string title
Definition: godleyTable.h:58
void dimension(unsigned rows, unsigned cols)
Definition: godleyTable.h:132
void _resize(unsigned rows, unsigned cols)
Definition: godleyTable.h:47
std::string savedText
save text in currently highlighted column heading for renaming all variable instances and to enable u...
Definition: godleyTable.h:167
void exportToCSV(std::ostream &s, const GodleyTable &g)
Definition: godleyExport.cc:61
static const char * initialConditions
Definition: godleyTable.h:60
bool signConventionReversed(int col) const
The usual mathematical sign convention is reversed in double entry book keeping conventions if the as...
Definition: godleyTable.h:92
std::vector< std::vector< string > > Data
Definition: godleyTable.h:39
bool operator==(const GodleyTable &other) const
Definition: godleyTable.h:69