52#ifndef DOXYGEN_SHOULD_SKIP_THIS
64 int DBCell::_string_max_index_ = 0;
66 Bijection< std::string, int >& DBCell::_strings_() {
68 static bool first_time =
true;
71 __debug__::_dec_creation_(
"Bijection",
" __strings", 0,
"BCell string bijection", 0);
72 __debug__::_dec_creation_(
"BijectionImplementation",
75 "BCell string bijection",
77 __debug__::_dec_creation_(
"HashTable",
" __strings", 0,
"BCell string bijection", 0);
78 __debug__::_dec_creation_(
"HashTable",
" __strings", 0,
"BCell string bijection", 0);
81 static Bijection< std::string, int > strings;
87 if (str.empty())
return false;
90 auto start_iter = str.begin() + str.find_first_not_of(
" \t");
91 auto end_iter = str.begin() + str.find_last_not_of(
" \t\r\n") + 1;
93 if (start_iter == end_iter)
return false;
96 if (*start_iter ==
'-') ++start_iter;
99 for (; start_iter != end_iter; ++start_iter) {
100 if ((*start_iter <
'0') || (*start_iter >
'9'))
return false;
108 if (str.empty())
return false;
111 auto start_iter = str.begin() + str.find_first_not_of(
" \t");
112 auto end_iter = str.begin() + str.find_last_not_of(
" \t\r\n") + 1;
114 if (start_iter == end_iter)
return false;
117 bool has_dot =
false;
118 bool has_exponent =
false;
119 bool has_digit =
false;
120 bool has_negation =
false;
121 for (; start_iter != end_iter; ++start_iter) {
122 if (*start_iter ==
'-') {
123 if (has_negation)
return false;
124 }
else if (*start_iter ==
'.') {
125 if (has_dot || has_exponent)
return false;
127 }
else if ((*start_iter ==
'e') || (*start_iter ==
'E')) {
128 if (has_exponent || !has_digit)
return false;
130 has_negation =
false;
131 }
else if ((*start_iter <
'0') || (*start_iter >
'9'))
return false;
132 else has_digit =
true;
140 if (new_type == _type_)
return true;
146 _val_real_ = float(_val_integer_);
152 const std::string& str = _strings_().first(_val_index_);
153 if (!
isReal(str))
return false;
154 _val_real_ = std::stof(str);
157 }
catch (std::invalid_argument&) {
return false; }
161 default :
GUM_ERROR(NotImplementedYet,
"type not supported by DBCell convertType")
168 const int nb = int(_val_real_);
169 if (nb == _val_real_) {
178 const std::string& str = _strings_().first(_val_index_);
180 _val_integer_ = std::stoi(str);
183 }
catch (std::invalid_argument&) {
return false; }
187 default :
GUM_ERROR(NotImplementedYet,
"type not supported by DBCell convertType")
195 snprintf(buffer, 100,
"%g", _val_real_);
196 const std::string str(buffer);
197 if (!_strings_().existsFirst(str)) {
198 _strings_().insert(str, _string_max_index_);
199 _val_index_ = _string_max_index_;
200 ++_string_max_index_;
202 _val_index_ = _strings_().second(str);
209 const std::string str = std::to_string(_val_integer_);
210 if (!_strings_().existsFirst(str)) {
211 _strings_().insert(str, _string_max_index_);
212 _val_index_ = _string_max_index_;
213 ++_string_max_index_;
215 _val_index_ = _strings_().second(str);
223 default :
GUM_ERROR(NotImplementedYet,
"type not supported by DBCell convertType")
229 default :
GUM_ERROR(NotImplementedYet,
"type not supported by DBCell convertType")
236 std::string DBCell::_typeErrorMsg_(
const std::string& true_type)
const {
237 std::stringstream str;
240 str <<
"The DBCell contains a real number instead of " << true_type;
244 str <<
"The DBCell contains an integer instead of " << true_type;
248 str <<
"The DBCell contains a string instead of " << true_type;
252 str <<
"The DBCell contains a missing value instead of " << true_type;
255 default :
GUM_ERROR(NotImplementedYet,
"DBCell type not implemented yet")
262 std::string
DBCell::toString(
const std::vector< std::string >& missingVals)
const {
268 snprintf(buffer, 100,
"%g", _val_real_);
269 return std::string(buffer);
275 if (missingVals.size())
return missingVals[0];
276 else GUM_ERROR(UndefinedElement,
"no missing value symbol found")
279 GUM_ERROR(NotImplementedYet,
"type not supported by DBCell toString")
The class representing the original values of the cells of databases.
The inlined implementation of DBCells.
static bool isInteger(const std::string &str)
determines whether a string corresponds precisely to an integer
std::string toString(const std::vector< std::string > &missingVals) const
returns the content of the DBCell as a string, whatever its type
static bool isReal(const std::string &str)
determine whether a string corresponds precisely to a real number
bool convertType(const EltType newtype)
try to convert the content of the DBCell into another type
#define GUM_ERROR(type, msg)
include the inlined functions if necessary
gum is the global namespace for all aGrUM entities