32 #include <agrum/tools/database/DBTranslator4IntegerVariable.h> 33 #include <agrum/tools/database/DBCell.h> 35 #ifndef DOXYGEN_SHOULD_SKIP_THIS 43 template <
template <
typename >
class ALLOC >
44 template <
template <
typename >
class XALLOC >
45 DBTranslator4IntegerVariable< ALLOC >::DBTranslator4IntegerVariable(
46 const IntegerVariable& var,
47 const std::vector< std::string, XALLOC< std::string > >& missing_symbols,
48 std::size_t max_dico_entries,
49 const typename DBTranslator4IntegerVariable< ALLOC >::allocator_type& alloc) :
50 DBTranslator< ALLOC >(DBTranslatedValueType::DISCRETE,
58 if (var.domainSize() > max_dico_entries) {
59 GUM_ERROR(SizeError,
"the dictionary induced by the variable is too large")
63 for (
auto iter =
this->missing_symbols_.beginSafe(); iter !=
this->missing_symbols_.endSafe();
65 if (DBCell::isInteger(*iter)) {
66 const int missing_val = std::stoi(*iter);
67 if (_variable_.integerDomain().exists(missing_val)) {
68 this->missing_symbols_.erase(iter);
75 for (
const auto& label: var.labels()) {
81 if (
this->missing_symbols_.exists(label)) {
this->missing_symbols_.erase(label); }
83 this->back_dico_.insert(size, label);
88 GUM_CONSTRUCTOR(DBTranslator4IntegerVariable);
93 template <
template <
typename >
class ALLOC >
94 DBTranslator4IntegerVariable< ALLOC >::DBTranslator4IntegerVariable(
95 const IntegerVariable& var,
96 std::size_t max_dico_entries,
97 const typename DBTranslator4IntegerVariable< ALLOC >::allocator_type& alloc) :
98 DBTranslator< ALLOC >(DBTranslatedValueType::DISCRETE,
105 if (var.domainSize() > max_dico_entries) {
106 GUM_ERROR(SizeError,
"the dictionary induced by the variable is too large")
110 std::size_t size = 0;
111 for (
const auto& label: var.labels()) {
112 this->back_dico_.insert(size, label);
117 GUM_CONSTRUCTOR(DBTranslator4IntegerVariable);
122 template <
template <
typename >
class ALLOC >
123 DBTranslator4IntegerVariable< ALLOC >::DBTranslator4IntegerVariable(
124 const DBTranslator4IntegerVariable< ALLOC >& from,
125 const typename DBTranslator4IntegerVariable< ALLOC >::allocator_type& alloc) :
126 DBTranslator< ALLOC >(from, alloc),
127 _variable_(from._variable_) {
129 GUM_CONS_CPY(DBTranslator4IntegerVariable);
134 template <
template <
typename >
class ALLOC >
135 DBTranslator4IntegerVariable< ALLOC >::DBTranslator4IntegerVariable(
136 const DBTranslator4IntegerVariable< ALLOC >& from) :
137 DBTranslator4IntegerVariable< ALLOC >(from, from.getAllocator()) {}
141 template <
template <
typename >
class ALLOC >
142 DBTranslator4IntegerVariable< ALLOC >::DBTranslator4IntegerVariable(
143 DBTranslator4IntegerVariable< ALLOC >&& from,
144 const typename DBTranslator4IntegerVariable< ALLOC >::allocator_type& alloc) :
145 DBTranslator< ALLOC >(std::move(from), alloc),
146 _variable_(std::move(from._variable_)) {
148 GUM_CONS_MOV(DBTranslator4IntegerVariable);
153 template <
template <
typename >
class ALLOC >
154 DBTranslator4IntegerVariable< ALLOC >::DBTranslator4IntegerVariable(
155 DBTranslator4IntegerVariable< ALLOC >&& from) :
156 DBTranslator4IntegerVariable< ALLOC >(std::move(from), from.getAllocator()) {}
160 template <
template <
typename >
class ALLOC >
161 DBTranslator4IntegerVariable< ALLOC >* DBTranslator4IntegerVariable< ALLOC >::clone(
162 const typename DBTranslator4IntegerVariable< ALLOC >::allocator_type& alloc)
const {
163 ALLOC< DBTranslator4IntegerVariable< ALLOC > > allocator(alloc);
164 DBTranslator4IntegerVariable< ALLOC >* translator = allocator.allocate(1);
166 allocator.construct(translator, *
this, alloc);
168 allocator.deallocate(translator, 1);
176 template <
template <
typename >
class ALLOC >
177 INLINE DBTranslator4IntegerVariable< ALLOC >*
178 DBTranslator4IntegerVariable< ALLOC >::clone()
const {
179 return clone(
this->getAllocator());
184 template <
template <
typename >
class ALLOC >
185 INLINE DBTranslator4IntegerVariable< ALLOC >::~DBTranslator4IntegerVariable() {
187 GUM_DESTRUCTOR(DBTranslator4IntegerVariable);
192 template <
template <
typename >
class ALLOC >
193 DBTranslator4IntegerVariable< ALLOC >& DBTranslator4IntegerVariable< ALLOC >::operator=(
194 const DBTranslator4IntegerVariable< ALLOC >& from) {
196 DBTranslator< ALLOC >::operator=(from);
197 _variable_ = from._variable_;
205 template <
template <
typename >
class ALLOC >
206 DBTranslator4IntegerVariable< ALLOC >& DBTranslator4IntegerVariable< ALLOC >::operator=(
207 DBTranslator4IntegerVariable< ALLOC >&& from) {
209 DBTranslator< ALLOC >::operator=(std::move(from));
210 _variable_ = std::move(from._variable_);
218 template <
template <
typename >
class ALLOC >
219 INLINE DBTranslatedValue
220 DBTranslator4IntegerVariable< ALLOC >::translate(
const std::string& str) {
223 return DBTranslatedValue{std::size_t(_variable_[str])};
224 }
catch (gum::Exception&) {
226 if (
this->isMissingSymbol(str))
227 return DBTranslatedValue{std::numeric_limits< std::size_t >::max()};
233 return DBTranslatedValue{
this->back_dico_.first(str)};
234 }
catch (gum::Exception&) {
235 if (!DBCell::isInteger(str)) {
237 "String \"" << str <<
"\" cannot be translated because it is not a number");
239 GUM_ERROR(UnknownLabelInDatabase,
240 "The translation of \"" << str <<
"\" could not be found")
248 template <
template <
typename >
class ALLOC >
249 INLINE std::string DBTranslator4IntegerVariable< ALLOC >::translateBack(
250 const DBTranslatedValue translated_val)
const {
252 return this->back_dico_.second(translated_val.discr_val);
253 }
catch (Exception&) {
255 if ((translated_val.discr_val == std::numeric_limits< std::size_t >::max())
256 && !
this->missing_symbols_.empty())
257 return *(
this->missing_symbols_.begin());
259 GUM_ERROR(UnknownLabelInDatabase,
260 "The back translation of \"" << translated_val.discr_val
261 <<
"\" could not be found");
267 template <
template <
typename >
class ALLOC >
268 INLINE
bool DBTranslator4IntegerVariable< ALLOC >::hasEditableDictionary()
const {
274 template <
template <
typename >
class ALLOC >
275 INLINE
void DBTranslator4IntegerVariable< ALLOC >::setEditableDictionaryMode(
bool) {}
279 template <
template <
typename >
class ALLOC >
280 bool DBTranslator4IntegerVariable< ALLOC >::needsReordering()
const {
286 template <
template <
typename >
class ALLOC >
287 INLINE HashTable< std::size_t, std::size_t, ALLOC< std::pair< std::size_t, std::size_t > > >
288 DBTranslator4IntegerVariable< ALLOC >::reorder() {
289 return HashTable< std::size_t,
291 ALLOC< std::pair< std::size_t, std::size_t > > >();
296 template <
template <
typename >
class ALLOC >
297 INLINE std::size_t DBTranslator4IntegerVariable< ALLOC >::domainSize()
const {
298 return _variable_.domainSize();
303 template <
template <
typename >
class ALLOC >
304 INLINE
const IntegerVariable* DBTranslator4IntegerVariable< ALLOC >::variable()
const {
310 template <
template <
typename >
class ALLOC >
311 INLINE DBTranslatedValue DBTranslator4IntegerVariable< ALLOC >::missingValue()
const {
312 return DBTranslatedValue{std::numeric_limits< std::size_t >::max()};