50#ifndef DOXYGEN_SHOULD_SKIP_THIS
63 const std::vector< std::string >& missing_symbols,
64 std::size_t max_dico_entries) :
70 _variable_(
"var",
"", 0) {
71 GUM_CONSTRUCTOR(DBTranslator4LabelizedVariable);
75 DBTranslator4LabelizedVariable::DBTranslator4LabelizedVariable(std::size_t max_dico_entries) :
76 DBTranslator(DBTranslatedValueType::
DISCRETE, true, true, max_dico_entries),
77 _variable_(
"var",
"", 0) {
78 GUM_CONSTRUCTOR(DBTranslator4LabelizedVariable);
82 DBTranslator4LabelizedVariable::DBTranslator4LabelizedVariable(
83 const LabelizedVariable& var,
84 const std::vector< std::string >& missing_symbols,
85 const bool editable_dictionary,
86 std::size_t max_dico_entries) :
87 DBTranslator(DBTranslatedValueType::
DISCRETE,
94 if (var.domainSize() > max_dico_entries) {
95 GUM_ERROR(SizeError,
"the dictionary induced by the variable is too large")
100 for (
const auto& label: var.labels()) {
103 if (this->missing_symbols_.exists(label)) { this->missing_symbols_.erase(label); }
106 this->back_dico_.insert(size, label);
110 GUM_CONSTRUCTOR(DBTranslator4LabelizedVariable);
114 DBTranslator4LabelizedVariable::DBTranslator4LabelizedVariable(
const LabelizedVariable& var,
115 const bool editable_dictionary,
116 std::size_t max_dico_entries) :
117 DBTranslator(DBTranslatedValueType::
DISCRETE, true, editable_dictionary, max_dico_entries),
120 if (var.domainSize() > max_dico_entries) {
121 GUM_ERROR(SizeError,
"the dictionary induced by the variable is too large")
125 std::size_t size = 0;
126 for (
const auto& label: var.labels()) {
128 this->back_dico_.insert(size, label);
132 GUM_CONSTRUCTOR(DBTranslator4LabelizedVariable);
136 DBTranslator4LabelizedVariable* DBTranslator4LabelizedVariable::clone()
const {
137 return new DBTranslator4LabelizedVariable(*
this);
141 DBTranslator4LabelizedVariable&
142 DBTranslator4LabelizedVariable::operator=(
const DBTranslator4LabelizedVariable& from) {
144 DBTranslator::operator=(from);
145 _variable_ = from._variable_;
152 DBTranslator4LabelizedVariable&
153 DBTranslator4LabelizedVariable::operator=(DBTranslator4LabelizedVariable&& from) {
155 DBTranslator::operator=(std::move(from));
156 _variable_ = std::move(from._variable_);
163 DBTranslatedValue DBTranslator4LabelizedVariable::translate(
const std::string& str) {
169 return DBTranslatedValue{std::size_t(_variable_[str])};
172 if (this->isMissingSymbol(str)) {
173 return DBTranslatedValue{std::numeric_limits< std::size_t >::max()};
177 if (this->hasEditableDictionary()) {
178 const std::size_t size = _variable_.domainSize();
179 if (size >= this->max_dico_entries_)
181 "String \"" << str <<
"\" cannot be translated "
182 <<
"because the dictionary is already full");
183 _variable_.addLabel(str);
184 this->back_dico_.insert(size, str);
185 return DBTranslatedValue{size};
188 "The translation of \"" << str <<
"\" could not be found for variable '"
189 << _variable_ <<
"'.");
194 bool DBTranslator4LabelizedVariable::needsReordering()
const {
196 const auto& labels = _variable_.labels();
197 float last_number = std::numeric_limits< float >::lowest();
199 bool only_numbers =
true;
200 for (
const auto& label: labels) {
201 if (!DBCell::isReal(label)) {
202 only_numbers =
false;
205 number = std::stof(label);
206 if (number < last_number)
return true;
207 last_number = number;
213 const std::size_t size = labels.size();
214 for (std::size_t i = 1; i < size; ++i) {
215 if (labels[i] < labels[i - 1])
return true;
223 HashTable< std::size_t, std::size_t > DBTranslator4LabelizedVariable::reorder() {
226 const auto& labels = _variable_.labels();
227 const std::size_t size = labels.size();
229 bool only_numbers =
true;
230 for (
const auto& label: labels) {
231 if (!DBCell::isReal(label)) {
232 only_numbers =
false;
238 std::vector< std::pair< std::size_t, std::string > > xlabels;
239 xlabels.reserve(size);
240 for (std::size_t i = std::size_t(0); i < size; ++i)
241 xlabels.push_back(std::make_pair(i, labels[i]));
245 std::sort(xlabels.begin(),
247 [](
const std::pair< std::size_t, std::string >& a,
248 const std::pair< std::size_t, std::string >& b) ->
bool {
249 return std::stof(a.second) < std::stof(b.second);
252 std::sort(xlabels.begin(),
254 [](
const std::pair< std::size_t, std::string >& a,
255 const std::pair< std::size_t, std::string >& b) ->
bool {
256 return a.second < b.second;
260 bool modifications =
false;
261 for (std::size_t i = std::size_t(0); i < size; ++i) {
262 if (xlabels[i].first != i) {
263 modifications =
true;
269 if (!modifications) {
return HashTable< std::size_t, std::size_t >(); }
273 _variable_.eraseLabels();
274 for (
auto& label: xlabels)
275 _variable_.addLabel(label.second);
279 this->back_dico_.clear();
280 HashTable< std::size_t, std::size_t > mapping((Size)size);
281 for (std::size_t i = std::size_t(0); i < size; ++i) {
282 mapping.insert(xlabels[i].first, i);
283 this->back_dico_.insert(i, xlabels[i].second);
The databases' cell translators for labelized variables.
The databases' cell translators for labelized variables.
Base class for all aGrUM's exceptions.
Exception : problem with size.
Error: An unknown label is found in the database.
DBTranslator4LabelizedVariable(const std::vector< std::string > &missing_symbols, std::size_t max_dico_entries=std::numeric_limits< std::size_t >::max())
default constructor without any initial variable
The base class for all the tabular database cell translators.
#define GUM_ERROR(type, msg)
DBTranslatedValueType
The nature of the elements handled by translators (discrete, continuous).
include the inlined functions if necessary
gum is the global namespace for all aGrUM entities