32 #include <agrum/tools/database/DBTranslator4DiscretizedVariable.h> 33 #include <agrum/tools/database/DBCell.h> 35 #ifndef DOXYGEN_SHOULD_SKIP_THIS 43 template <
template <
typename >
class ALLOC >
44 template <
typename GUM_SCALAR,
template <
typename >
class XALLOC >
45 DBTranslator4DiscretizedVariable< ALLOC >::DBTranslator4DiscretizedVariable(
46 const DiscretizedVariable< GUM_SCALAR >& var,
47 const std::vector< std::string, XALLOC< std::string > >& missing_symbols,
48 std::size_t max_dico_entries,
49 const typename DBTranslator4DiscretizedVariable< ALLOC >::allocator_type& alloc) :
50 DBTranslator< ALLOC >(DBTranslatedValueType::DISCRETE,
55 _variable_(var.name(), var.description()) {
57 if (var.domainSize() > max_dico_entries) {
58 GUM_ERROR(SizeError,
"the dictionary induced by the variable is too large")
62 const auto& ticks = var.ticks();
63 for (
const auto tick: ticks) {
64 _variable_.addTick((
float)tick);
68 const float lower_bound = (
float)ticks[0];
69 const float upper_bound = (
float)ticks.back();
73 for (
auto iter =
this->missing_symbols_.beginSafe(); iter !=
this->missing_symbols_.endSafe();
75 if (DBCell::isReal(*iter)) {
76 const float missing_val = std::stof(*iter);
77 if ((missing_val >= lower_bound) && (missing_val <= upper_bound)) {
78 this->missing_symbols_.erase(iter);
85 for (
const auto& label: var.labels()) {
91 if (
this->missing_symbols_.exists(label)) {
this->missing_symbols_.erase(label); }
93 this->back_dico_.insert(size, label);
98 _real_variable_ = var.clone();
100 GUM_CONSTRUCTOR(DBTranslator4DiscretizedVariable);
105 template <
template <
typename >
class ALLOC >
106 template <
template <
typename >
class XALLOC >
107 DBTranslator4DiscretizedVariable< ALLOC >::DBTranslator4DiscretizedVariable(
108 const IDiscretizedVariable& var,
109 const std::vector< std::string, XALLOC< std::string > >& missing_symbols,
110 std::size_t max_dico_entries,
111 const typename DBTranslator4DiscretizedVariable< ALLOC >::allocator_type& alloc) :
112 DBTranslator< ALLOC >(DBTranslatedValueType::DISCRETE,
117 _variable_(var.name(), var.description()) {
119 if (var.domainSize() > max_dico_entries) {
120 GUM_ERROR(SizeError,
"the dictionary induced by the variable is too large")
124 const auto ticks = var.ticksAsDoubles();
125 for (
const auto tick: ticks) {
126 _variable_.addTick((
float)tick);
130 const float lower_bound =
float(ticks[0]);
131 const float upper_bound =
float(ticks.back());
135 for (
auto iter =
this->missing_symbols_.beginSafe(); iter !=
this->missing_symbols_.endSafe();
137 if (DBCell::isReal(*iter)) {
138 const float missing_val = std::stof(*iter);
139 if ((missing_val >= lower_bound) && (missing_val <= upper_bound)) {
140 this->missing_symbols_.erase(iter);
146 std::size_t size = 0;
147 for (
const auto& label: var.labels()) {
153 if (
this->missing_symbols_.exists(label)) {
this->missing_symbols_.erase(label); }
155 this->back_dico_.insert(size, label);
160 _real_variable_ = var.clone();
162 GUM_CONSTRUCTOR(DBTranslator4DiscretizedVariable);
167 template <
template <
typename >
class ALLOC >
168 template <
typename GUM_SCALAR >
169 DBTranslator4DiscretizedVariable< ALLOC >::DBTranslator4DiscretizedVariable(
170 const DiscretizedVariable< GUM_SCALAR >& var,
171 std::size_t max_dico_entries,
172 const typename DBTranslator4DiscretizedVariable< ALLOC >::allocator_type& alloc) :
173 DBTranslator< ALLOC >(DBTranslatedValueType::DISCRETE,
false, max_dico_entries, alloc),
174 _variable_(var.name(), var.description()) {
176 if (var.domainSize() > max_dico_entries) {
177 GUM_ERROR(SizeError,
"the dictionary induced by the variable is too large")
181 const auto& ticks = var.ticks();
182 for (
const auto tick: ticks) {
183 _variable_.addTick((
float)tick);
187 std::size_t size = 0;
188 for (
const auto& label: var.labels()) {
189 this->back_dico_.insert(size, label);
194 _real_variable_ = var.clone();
196 GUM_CONSTRUCTOR(DBTranslator4DiscretizedVariable);
201 template <
template <
typename >
class ALLOC >
202 DBTranslator4DiscretizedVariable< ALLOC >::DBTranslator4DiscretizedVariable(
203 const IDiscretizedVariable& var,
204 std::size_t max_dico_entries,
205 const typename DBTranslator4DiscretizedVariable< ALLOC >::allocator_type& alloc) :
206 DBTranslator< ALLOC >(DBTranslatedValueType::DISCRETE,
false, max_dico_entries, alloc),
207 _variable_(var.name(), var.description()) {
209 if (var.domainSize() > max_dico_entries) {
210 GUM_ERROR(SizeError,
"the dictionary induced by the variable is too large")
214 const auto ticks = var.ticksAsDoubles();
215 for (
const auto tick: ticks) {
216 _variable_.addTick((
float)tick);
220 std::size_t size = 0;
221 for (
const auto& label: var.labels()) {
222 this->back_dico_.insert(size, label);
227 _real_variable_ = var.clone();
229 GUM_CONSTRUCTOR(DBTranslator4DiscretizedVariable);
234 template <
template <
typename >
class ALLOC >
235 DBTranslator4DiscretizedVariable< ALLOC >::DBTranslator4DiscretizedVariable(
236 const DBTranslator4DiscretizedVariable< ALLOC >& from,
237 const typename DBTranslator4DiscretizedVariable< ALLOC >::allocator_type& alloc) :
238 DBTranslator< ALLOC >(from, alloc),
239 _variable_(from._variable_) {
241 _real_variable_ = from._real_variable_->clone();
243 GUM_CONS_CPY(DBTranslator4DiscretizedVariable);
248 template <
template <
typename >
class ALLOC >
249 DBTranslator4DiscretizedVariable< ALLOC >::DBTranslator4DiscretizedVariable(
250 const DBTranslator4DiscretizedVariable< ALLOC >& from) :
251 DBTranslator4DiscretizedVariable< ALLOC >(from, from.getAllocator()) {}
255 template <
template <
typename >
class ALLOC >
256 DBTranslator4DiscretizedVariable< ALLOC >::DBTranslator4DiscretizedVariable(
257 DBTranslator4DiscretizedVariable< ALLOC >&& from,
258 const typename DBTranslator4DiscretizedVariable< ALLOC >::allocator_type& alloc) :
259 DBTranslator< ALLOC >(std::move(from), alloc),
260 _variable_(std::move(from._variable_)) {
262 _real_variable_ = from._real_variable_;
263 from._real_variable_ =
nullptr;
265 GUM_CONS_MOV(DBTranslator4DiscretizedVariable);
270 template <
template <
typename >
class ALLOC >
271 DBTranslator4DiscretizedVariable< ALLOC >::DBTranslator4DiscretizedVariable(
272 DBTranslator4DiscretizedVariable< ALLOC >&& from) :
273 DBTranslator4DiscretizedVariable< ALLOC >(std::move(from), from.getAllocator()) {}
277 template <
template <
typename >
class ALLOC >
278 DBTranslator4DiscretizedVariable< ALLOC >* DBTranslator4DiscretizedVariable< ALLOC >::clone(
279 const typename DBTranslator4DiscretizedVariable< ALLOC >::allocator_type& alloc)
const {
280 ALLOC< DBTranslator4DiscretizedVariable< ALLOC > > allocator(alloc);
281 DBTranslator4DiscretizedVariable< ALLOC >* translator = allocator.allocate(1);
283 allocator.construct(translator, *
this, alloc);
285 allocator.deallocate(translator, 1);
293 template <
template <
typename >
class ALLOC >
294 INLINE DBTranslator4DiscretizedVariable< ALLOC >*
295 DBTranslator4DiscretizedVariable< ALLOC >::clone()
const {
296 return clone(
this->getAllocator());
301 template <
template <
typename >
class ALLOC >
302 INLINE DBTranslator4DiscretizedVariable< ALLOC >::~DBTranslator4DiscretizedVariable() {
303 if (_real_variable_ !=
nullptr)
delete _real_variable_;
305 GUM_DESTRUCTOR(DBTranslator4DiscretizedVariable);
310 template <
template <
typename >
class ALLOC >
311 DBTranslator4DiscretizedVariable< ALLOC >& DBTranslator4DiscretizedVariable< ALLOC >::operator=(
312 const DBTranslator4DiscretizedVariable< ALLOC >& from) {
314 DBTranslator< ALLOC >::operator=(from);
315 _variable_ = from._variable_;
317 if (_real_variable_ !=
nullptr)
delete _real_variable_;
318 _real_variable_ = from._real_variable_->clone();
326 template <
template <
typename >
class ALLOC >
327 DBTranslator4DiscretizedVariable< ALLOC >& DBTranslator4DiscretizedVariable< ALLOC >::operator=(
328 DBTranslator4DiscretizedVariable< ALLOC >&& from) {
330 DBTranslator< ALLOC >::operator=(std::move(from));
331 _variable_ = std::move(from._variable_);
333 if (_real_variable_ !=
nullptr)
delete _real_variable_;
334 _real_variable_ = from._real_variable_;
335 from._real_variable_ =
nullptr;
343 template <
template <
typename >
class ALLOC >
344 INLINE DBTranslatedValue
345 DBTranslator4DiscretizedVariable< ALLOC >::translate(
const std::string& str) {
348 return DBTranslatedValue{std::size_t(_variable_[str])};
349 }
catch (gum::Exception&) {
351 if (
this->isMissingSymbol(str))
352 return DBTranslatedValue{std::numeric_limits< std::size_t >::max()};
358 return DBTranslatedValue{
this->back_dico_.first(str)};
359 }
catch (gum::Exception&) {
360 if (!DBCell::isReal(str)) {
362 "String \"" << str <<
"\" cannot be translated because it is not a number");
364 GUM_ERROR(UnknownLabelInDatabase,
365 "The translation of \"" << str <<
"\" could not be found")
373 template <
template <
typename >
class ALLOC >
374 INLINE std::string DBTranslator4DiscretizedVariable< ALLOC >::translateBack(
375 const DBTranslatedValue translated_val)
const {
377 return this->back_dico_.second(translated_val.discr_val);
378 }
catch (Exception&) {
380 if ((translated_val.discr_val == std::numeric_limits< std::size_t >::max())
381 && !
this->missing_symbols_.empty())
382 return *(
this->missing_symbols_.begin());
384 GUM_ERROR(UnknownLabelInDatabase,
385 "The back translation of \"" << translated_val.discr_val
386 <<
"\" could not be found");
392 template <
template <
typename >
class ALLOC >
393 INLINE
bool DBTranslator4DiscretizedVariable< ALLOC >::hasEditableDictionary()
const {
399 template <
template <
typename >
class ALLOC >
400 INLINE
void DBTranslator4DiscretizedVariable< ALLOC >::setEditableDictionaryMode(
bool) {}
404 template <
template <
typename >
class ALLOC >
405 bool DBTranslator4DiscretizedVariable< ALLOC >::needsReordering()
const {
411 template <
template <
typename >
class ALLOC >
412 INLINE HashTable< std::size_t, std::size_t, ALLOC< std::pair< std::size_t, std::size_t > > >
413 DBTranslator4DiscretizedVariable< ALLOC >::reorder() {
414 return HashTable< std::size_t,
416 ALLOC< std::pair< std::size_t, std::size_t > > >();
421 template <
template <
typename >
class ALLOC >
422 INLINE std::size_t DBTranslator4DiscretizedVariable< ALLOC >::domainSize()
const {
423 return _variable_.domainSize();
428 template <
template <
typename >
class ALLOC >
429 INLINE
const IDiscretizedVariable* DBTranslator4DiscretizedVariable< ALLOC >::variable()
const {
430 return _real_variable_;
435 template <
template <
typename >
class ALLOC >
436 INLINE DBTranslatedValue DBTranslator4DiscretizedVariable< ALLOC >::missingValue()
const {
437 return DBTranslatedValue{std::numeric_limits< std::size_t >::max()};