Reaktoro  v2.11.0
A unified framework for modeling chemically reactive systems
Table.hpp
1 // Reaktoro is a unified framework for modeling chemically reactive systems.
2 //
3 // Copyright © 2014-2024 Allan Leal
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this library. If not, see <http://www.gnu.org/licenses/>.
17 
18 #pragma once
19 
20 // Reaktoro includes
21 #include <Reaktoro/Common/Exception.hpp>
22 #include <Reaktoro/Common/TraitsUtils.hpp>
23 #include <Reaktoro/Common/Types.hpp>
24 
25 namespace Reaktoro {
26 
30 {
31 public:
33  enum class DataType
34  {
35  Float,
36  Integer,
37  String,
38  Boolean,
39  Undefined,
40  };
41 
44 
47  auto appendFloat(double value) -> void;
48 
51  auto appendInteger(long value) -> void;
52 
55  auto appendString(String const& value) -> void;
56 
59  auto appendBoolean(bool value) -> void;
60 
62  auto dataType() const -> DataType;
63 
66  auto floats() const -> Deque<double> const&;
67 
70  auto floats() -> Deque<double>&; // TODO: These methods in Table that return Deque<T>& are dangerous in which the user can change its length, and this will not be reflected in mrows. Replace this by std::span when migrating to C++20.
71 
74  auto integers() const -> Deque<long> const&;
75 
78  auto integers() -> Deque<long>&;
79 
82  auto strings() const -> Deque<String> const&;
83 
86  auto strings() -> Deque<String>&;
87 
90  auto booleans() const -> Deque<bool> const&;
91 
94  auto booleans() -> Deque<bool>&;
95 
97  auto rows() const -> Index;
98 
100  auto operator[](Index row) const -> std::variant<double, long, String, bool>;
101 
105  template<typename T>
106  auto append(T const& value) -> void
107  {
108  if constexpr(isSame<T, bool>)
109  appendBoolean(value);
110  else if constexpr(isInteger<T>) // keep integer check after booleans, as isInteger<bool> is true!
111  if(datatype == DataType::Float)
112  appendFloat(value); // allow integers to be cast to float and stored in a float column!
113  else appendInteger(value);
114  else if constexpr(isFloatingPoint<T> || isConvertible<T, double>)
115  appendFloat(value);
116  else if constexpr(isSame<T, String>)
117  appendString(value);
118  else errorif(true, "You cannot append this value with an unsupported type to a table column.");
119  }
120 
122  auto append(Chars value) -> void
123  {
124  appendString(value);
125  }
126 
129  template<typename T>
130  auto operator<<(T const& value) -> TableColumn&
131  {
132  append(value);
133  return *this;
134  }
135 
137  auto operator<<(Chars value) -> TableColumn&
138  {
139  appendString(value);
140  return *this;
141  }
142 
144  template<typename T>
145  auto cast() -> Deque<T>&
146  {
147  return const_cast<Deque<T>&>(std::as_const(*this).cast<T>());
148  }
149 
151  template<typename T>
152  auto cast() const -> Deque<T> const&
153  {
154  if constexpr(isSame<T, bool>)
155  return booleans();
156  else if constexpr(isFloatingPoint<T>)
157  return floats();
158  else if constexpr(isInteger<T>) // keep integer check after booleans, as isInteger<bool> is true!
159  return integers();
160  else if constexpr(isSame<T, String> || isSame<T, Chars>)
161  return strings();
162  else errorif(true, "You cannot cast this table column to a list of values with an unsupported type.");
163  }
164 
165 private:
167  Any data;
168 
170  Index mrows = 0;
171 
173  DataType datatype = DataType::Undefined;
174 };
175 
177 class Table
178 {
179 public:
181  Table();
182 
184  auto columns() const -> Dict<String, TableColumn> const&;
185 
187  auto column(String const& columnname) const -> TableColumn const&;
188 
190  auto column(String const& columnname) -> TableColumn&;
191 
193  auto operator[](String const& columnname) const -> Deque<double> const&;
194 
196  auto operator[](String const& columnname) -> Deque<double>&;
197 
199  auto rows() const -> Index;
200 
202  auto cols() const -> Index;
203 
206  {
209 
212 
215 
218 
220  bool fixed;
221  };
222 
225  auto dump(OutputOptions const& outputopts = {}) const -> String;
226 
231  auto save(String const& filepath, OutputOptions const& outputopts = {}) const -> void;
232 
233 private:
235  Dict<String, TableColumn> mcolumns;
236 };
237 
238 } // namespace Reaktoro
Used to represent the data stored in a table column.
Definition: Table.hpp:30
auto booleans() const -> Deque< bool > const &
Convert this TableColumn object to a constant reference to its underlying Deque<bool> object.
auto integers() const -> Deque< long > const &
Convert this TableColumn object to a constant reference to its underlying Deque<long> object.
auto rows() const -> Index
Get the number of rows in the column.
auto operator<<(Chars value) -> TableColumn &
Append a new string value to the TableColumn object.
Definition: Table.hpp:137
auto append(T const &value) -> void
Append a new value to the TableColumn object.
Definition: Table.hpp:106
auto operator<<(T const &value) -> TableColumn &
Append a new value to the TableColumn object.
Definition: Table.hpp:130
auto strings() const -> Deque< String > const &
Convert this TableColumn object to a constant reference to its underlying Deque<String> object.
auto appendBoolean(bool value) -> void
Append a new boolean value to the TableColumn object.
auto appendFloat(double value) -> void
Append a new floating-point value to the TableColumn object.
auto dataType() const -> DataType
Get the data type of the column.
auto append(Chars value) -> void
Append a new string value to the TableColumn object.
Definition: Table.hpp:122
auto floats() const -> Deque< double > const &
Convert this TableColumn object to a constant reference to its underlying Deque<double> object.
auto cast() -> Deque< T > &
Cast this TableColumn object to a mutable reference to a list of values with type compatible with giv...
Definition: Table.hpp:145
auto appendString(String const &value) -> void
Append a new string value to the TableColumn object.
auto cast() const -> Deque< T > const &
Cast this TableColumn object to a constant reference to a list of values with type compatible with gi...
Definition: Table.hpp:152
TableColumn()
Construct a default TableColumn object.
DataType
Used in the identification of the value type along each column in the table.
Definition: Table.hpp:34
@ Float
The column data type indicating a double floating-point value value.
@ String
The column data type indicating a string value.
@ Boolean
The column data type indicating a boolean value.
@ Integer
The column data type indicating an integer value.
@ Undefined
The column data type has not been specified yet.
auto appendInteger(long value) -> void
Append a new integer value to the TableColumn object.
Used to store computed data in columns.
Definition: Table.hpp:178
auto rows() const -> Index
Get the number of rows in the table (i.e., the length of the longest column in the table).
auto columns() const -> Dict< String, TableColumn > const &
Get the columns in the Table object.
Table()
Construct a default Table object.
auto dump(OutputOptions const &outputopts={}) const -> String
Assemble a string representation of the Table object.
auto save(String const &filepath, OutputOptions const &outputopts={}) const -> void
Save the Table object to a file.
auto column(String const &columnname) const -> TableColumn const &
Get a constant reference to a column in the table with given name.
auto cols() const -> Index
Get the number of columns in the table.
#define errorif(condition,...)
Define a macro to raise a runtime exception if condition is true.
Definition: Exception.hpp:140
The namespace containing all components of the Reaktoro library.
Definition: Algorithms.hpp:29
std::string String
Convenient alias for std::string.
Definition: Types.hpp:52
std::size_t Index
Define a type that represents an index.
Definition: Index.hpp:26
std::deque< T > Deque
Convenient alias for std::deque<T>.
Definition: Types.hpp:70
const char * Chars
Convenient alias for const char*.
Definition: Types.hpp:49
std::any Any
Convenient alias for std::any.
Definition: Types.hpp:125
Used to specify formatting options when printing or outputting the Table object.
Definition: Table.hpp:206
String delimiter
The symbol used to separate column values on a table row (defaults to " | ").
Definition: Table.hpp:211
OutputOptions()
Construct a default OutputOptions object.
int precision
The precision used when printing floating-point values (defaults to 6).
Definition: Table.hpp:214
bool fixed
The boolean flag indicating if floating-point values should be printed in fixed notation (defaults to...
Definition: Table.hpp:220
bool scientific
The boolean flag indicating if floating-point values should be printed in scientific notation (defaul...
Definition: Table.hpp:217