Optimized Belief Propagation (CPU and GPU)
RunData.cpp
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 
27 #include "RunEvalConstsEnums.h"
28 #include "RunData.h"
29 
30 //get header to add...use input header if not yet used
31 //use original header with number appended if original header is already used
32 std::string RunData::GetHeaderToAdd(const std::string& in_header) const
33 {
34  auto header_to_add = in_header;
35  unsigned int num{0};
36  while (std::ranges::any_of(headers_in_order_,
37  [&header_to_add](const auto& ordered_header){
38  return (ordered_header == header_to_add);
39  }))
40  {
41  //add "_{num}" to header if header already in data
42  num++;
43  header_to_add = in_header + "_" + std::to_string(num);
44  }
45  return header_to_add;
46 }
47 
48 //add data with header describing added data
50  const std::string& header,
51  const std::string& data)
52 {
53  const auto header_to_add{GetHeaderToAdd(header)};
54  headers_in_order_.push_back(header_to_add);
55  headers_w_data_[header_to_add] = data;
56 }
57 
58 //add data with header describing added data
60  const std::string& header,
61  const char* data)
62 {
63  const auto header_to_add{GetHeaderToAdd(header)};
64  headers_in_order_.push_back(header_to_add);
65  headers_w_data_[header_to_add] = std::string(data);
66 }
67 
68 //add data with header describing added data
70  const std::string& header,
71  double data)
72 {
73  const auto header_to_add{GetHeaderToAdd(header)};
74  headers_in_order_.push_back(header_to_add);
75  headers_w_data_[header_to_add] = data;
76 }
77 
78 //add data with header describing added data
80  const std::string& header,
81  bool data)
82 {
83  const auto header_to_add{GetHeaderToAdd(header)};
84  headers_in_order_.push_back(header_to_add);
85  headers_w_data_[header_to_add] = data;
86 }
87 
88 //add data with header describing added data
90  const std::string& header,
91  unsigned int data)
92 {
93  const auto header_to_add{GetHeaderToAdd(header)};
94  headers_in_order_.push_back(header_to_add);
95  headers_w_data_[header_to_add] = data;
96 }
97 
98 //append current RunData with input RunData
99 void RunData::AppendData(const RunData& rundata)
100 {
101  for (const auto& header : rundata.HeadersInOrder()) {
102  if (rundata.GetDataAsDouble(header)) {
103  AddDataWHeader(header, *(rundata.GetDataAsDouble(header)));
104  }
105  else if (rundata.GetDataAsUInt(header)) {
106  AddDataWHeader(header, *(rundata.GetDataAsUInt(header)));
107  }
108  else if (rundata.GetDataAsBool(header)) {
109  AddDataWHeader(header, *(rundata.GetDataAsBool(header)));
110  }
111  else {
112  AddDataWHeader(header, rundata.GetDataAsStr(header));
113  }
114  }
115 }
116 
117 //get data corresponding to header
118 std::string RunData::GetDataAsStr(const std::string_view header) const
119 {
120  const auto& data_variant = headers_w_data_.at(std::string(header));
121  //check if data is string, double, unsigned int, or bool, convert to
122  //string if needed, and return as string
123  if (const std::string* data_str_ptr = std::get_if<std::string>(&data_variant)) {
124  return *data_str_ptr;
125  }
126  if (const double* data_double_ptr = std::get_if<double>(&data_variant)) {
127  return std::to_string(*data_double_ptr);
128  }
129  if (const unsigned int* data_uint_ptr = std::get_if<unsigned int>(&data_variant)) {
130  return std::to_string(*data_uint_ptr);
131  }
132  if (const bool* data_bool_ptr = std::get_if<bool>(&data_variant)) {
133  return (*data_bool_ptr) ?
134  std::string(run_eval::kBoolValFalseTrueDispStr[1]) :
135  std::string(run_eval::kBoolValFalseTrueDispStr[0]);
136  }
137  return "";
138 }
139 
140 //get data as double if variant corresponding to header is double type
141 //return null if data corresponds to a different data type
142 std::optional<double> RunData::GetDataAsDouble(
143  const std::string_view header) const
144 {
145  const auto& data_variant = headers_w_data_.at(std::string(header));
146  if (const double* data_double_ptr = std::get_if<double>(&data_variant)) {
147  return *data_double_ptr;
148  }
149  return {};
150 }
151 
152 //get data as unsigned integer if variant corresponding to header is
153 //unsigned integer type
154 //return null if data corresponds to a different data type
155 std::optional<unsigned int> RunData::GetDataAsUInt(
156  const std::string_view header) const
157 {
158  const auto& data_variant = headers_w_data_.at(std::string(header));
159  if (const unsigned int* data_uint_ptr = std::get_if<unsigned int>(&data_variant)) {
160  return *data_uint_ptr;
161  }
162  return {};
163 }
164 
165 //get data as boolean if variant corresponding to header is boolean type
166 //return null if data corresponds to a different data type
167 std::optional<bool> RunData::GetDataAsBool(
168  const std::string_view header) const
169 {
170  const auto& data_variant = headers_w_data_.at(std::string(header));
171  if (const bool* data_bool_ptr = std::get_if<bool>(&data_variant)) {
172  return *data_bool_ptr;
173  }
174  return {};
175 }
Declares class to store headers with data corresponding to current program run and evaluation.
Contains namespace with enums and constants for implementation run evaluation.
Class to store headers with data corresponding to current program run and evaluation.
Definition: RunData.h:42
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
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
constexpr std::array< std::string_view, 2 > kBoolValFalseTrueDispStr
Define string for display of "true" and "false" values of bool value.