29 #ifndef DOXYGEN_SHOULD_SKIP_THIS 41 int DBCell::__string_max_index = 0;
44 Bijection< std::string, int >& DBCell::__strings() {
45 # ifdef GUM_DEBUG_MODE 46 static bool first_time =
true;
49 __debug__::__dec_creation(
50 "Bijection",
"__strings", 0,
"BCell string bijection", 0);
51 __debug__::__dec_creation(
52 "BijectionImplementation",
"__strings", 0,
"BCell string bijection", 0);
53 __debug__::__dec_creation(
54 "HashTable",
"__strings", 0,
"BCell string bijection", 0);
55 __debug__::__dec_creation(
56 "HashTable",
"__strings", 0,
"BCell string bijection", 0);
59 static Bijection< std::string, int > strings;
66 if (str.empty())
return false;
69 auto start_iter = str.begin() + str.find_first_not_of(
" \t");
70 auto end_iter = str.begin() + str.find_last_not_of(
" \t\r\n") + 1;
72 if (start_iter == end_iter)
return false;
75 if (*start_iter ==
'-') ++start_iter;
78 for (; start_iter != end_iter; ++start_iter) {
79 if ((*start_iter <
'0') || (*start_iter >
'9'))
return false;
88 if (str.empty())
return false;
91 auto start_iter = str.begin() + str.find_first_not_of(
" \t");
92 auto end_iter = str.begin() + str.find_last_not_of(
" \t\r\n") + 1;
94 if (start_iter == end_iter)
return false;
98 bool has_exponent =
false;
99 bool has_digit =
false;
100 bool has_negation =
false;
101 for (; start_iter != end_iter; ++start_iter) {
102 if (*start_iter ==
'-') {
103 if (has_negation)
return false;
104 }
else if (*start_iter ==
'.') {
105 if (has_dot || has_exponent)
return false;
107 }
else if ((*start_iter ==
'e') || (*start_iter ==
'E')) {
108 if (has_exponent || !has_digit)
return false;
110 has_negation =
false;
111 }
else if ((*start_iter <
'0') || (*start_iter >
'9'))
123 if (new_type == __type)
return true;
129 __val_real = float(__val_integer);
135 const std::string& str = __strings().first(__val_index);
136 if (!
isReal(str))
return false;
137 __val_real = std::stof(str);
140 }
catch (std::invalid_argument&) {
return false; }
146 "type not supported by DBCell convertType");
153 const int nb = int(__val_real);
154 if (nb == __val_real) {
164 const std::string& str = __strings().first(__val_index);
166 __val_integer = std::stoi(str);
169 }
catch (std::invalid_argument&) {
return false; }
175 "type not supported by DBCell convertType");
183 sprintf(buffer,
"%g", __val_real);
184 const std::string str(buffer);
185 if (!__strings().existsFirst(str)) {
186 __strings().insert(str, __string_max_index);
187 __val_index = __string_max_index;
188 ++__string_max_index;
190 __val_index = __strings().second(str);
198 if (!__strings().existsFirst(str)) {
199 __strings().insert(str, __string_max_index);
200 __val_index = __string_max_index;
201 ++__string_max_index;
203 __val_index = __strings().second(str);
213 "type not supported by DBCell convertType");
220 GUM_ERROR(NotImplementedYet,
"type not supported by DBCell convertType");
228 std::string DBCell::__typeErrorMsg(
const std::string& true_type)
const {
229 std::stringstream str;
232 str <<
"The DBCell contains a real number instead of " << true_type;
236 str <<
"The DBCell contains an integer instead of " << true_type;
240 str <<
"The DBCell contains a string instead of " << true_type;
244 str <<
"The DBCell contains a missing value instead of " << true_type;
247 default:
GUM_ERROR(NotImplementedYet,
"DBCell type not implemented yet");
bool convertType(const EltType newtype)
try to convert the content of the DBCell into another type
static bool isInteger(const std::string &str)
determines whether a string corresponds precisely to an integer
The inlined implementation of DBCells.
gum is the global namespace for all aGrUM entities
The class representing the original values of the cells of databases.
std::string to_string(const Formula &f)
static bool isReal(const std::string &str)
determine whether a string corresponds precisely to a real number
EltType
the set of types possibly taken by the last element read
#define GUM_ERROR(type, msg)