Optimized Belief Propagation (CPU and GPU)
RunData.h
Go to the documentation of this file.
1 /*
2 Copyright (C) 2024 Scott Grauer-Gray
3 
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 
28 #ifndef RUN_DATA_H
29 #define RUN_DATA_H
30 
31 #include <vector>
32 #include <map>
33 #include <string>
34 #include <algorithm>
35 #include <iostream>
36 #include <optional>
37 #include <variant>
38 
42 class RunData {
43 public:
50  void AddDataWHeader(const std::string& header, const std::string& data);
51 
58  void AddDataWHeader(const std::string& header, const char* data);
59 
66  void AddDataWHeader(const std::string& header, double data);
67 
74  void AddDataWHeader(const std::string& header, bool data);
75 
82  void AddDataWHeader(const std::string& header, unsigned int data);
83 
89  const std::vector<std::string>& HeadersInOrder() const { return headers_in_order_; }
90 
97  bool IsData(const std::string_view header) const {
98  const std::string header_str(header);
99  return (std::ranges::any_of(headers_in_order_,
100  [&header_str](const auto& ordered_header) {
101  return (ordered_header == header_str);
102  }));
103  }
104 
112  std::string GetDataAsStr(const std::string_view header) const;
113 
122  std::optional<double> GetDataAsDouble(const std::string_view header) const;
123 
132  std::optional<unsigned int> GetDataAsUInt(const std::string_view header) const;
133 
141  std::optional<bool> GetDataAsBool(const std::string_view header) const;
142 
148  void AppendData(const RunData& rundata);
149 
157  friend std::ostream& operator<<(std::ostream& os, const RunData& run_data);
158 
159 private:
167  std::string GetHeaderToAdd(const std::string& in_header) const;
168 
171  std::map<std::string, std::variant<unsigned int, double, bool, std::string>> headers_w_data_;
172 
174  std::vector<std::string> headers_in_order_;
175 };
176 
177 //overloaded << operator for output to stream
178 inline std::ostream& operator<<(std::ostream& os, const RunData& run_data) {
179  //add each header with corresponding data separated by ':' to stream in a separate line
180  for (const auto& header : run_data.headers_in_order_) {
181  os << header << ": " << run_data.GetDataAsStr(header) << std::endl;
182  }
183  return os;
184 }
185 
186 #endif //RUN_DATA_H
std::ostream & operator<<(std::ostream &os, const RunData &run_data)
Definition: RunData.h:178
Class to store headers with data corresponding to current program run and evaluation.
Definition: RunData.h:42
friend std::ostream & operator<<(std::ostream &os, const RunData &run_data)
Overloaded << operator for output to stream.
Definition: RunData.h:178
const std::vector< std::string > & HeadersInOrder() const
Return data headers in order.
Definition: RunData.h:89
void AppendData(const RunData &rundata)
Append current RunData with input RunData.
Definition: RunData.cpp:99
std::optional< unsigned int > GetDataAsUInt(const std::string_view header) const
Get data corresponding to header as unsigned int Return null if data corresponds to a different data ...
Definition: RunData.cpp:155
bool IsData(const std::string_view header) const
Return whether or not there is data corresponding to a specific header.
Definition: RunData.h:97
std::string GetDataAsStr(const std::string_view header) const
Get data corresponding to header as a string Returns data as string regardless of underlying data typ...
Definition: RunData.cpp:118
void AddDataWHeader(const std::string &header, const std::string &data)
Add string data with header describing added data.
Definition: RunData.cpp:49
std::optional< double > GetDataAsDouble(const std::string_view header) const
Get data corresponding to header as double Return null if data corresponds to a different data type.
Definition: RunData.cpp:142
std::optional< bool > GetDataAsBool(const std::string_view header) const
Get data corresponding to header as boolean Return null if data corresponds to a different data type.
Definition: RunData.cpp:167