50#ifndef DOXYGEN_SHOULD_SKIP_THIS
63 const IDiscretizedVariable& var,
64 const std::vector< std::string >& missing_symbols,
65 std::size_t max_dico_entries) :
71 _variable_(var.name(), var.description()) {
73 if (var.domainSize() > max_dico_entries) {
74 GUM_ERROR(SizeError,
"the dictionary induced by the variable is too large")
78 const auto ticks = var.ticksAsDoubles();
79 for (
const auto tick: ticks) {
80 _variable_.addTick((
float)tick);
84 _variable_.setEmpirical(var.isEmpirical());
87 const auto lower_bound =
float(ticks[0]);
88 const auto upper_bound =
float(ticks.back());
92 for (
auto iter = this->missing_symbols_.beginSafe(); iter != this->missing_symbols_.endSafe();
94 if (DBCell::isReal(*iter)) {
95 const float missing_val = std::stof(*iter);
96 if ((missing_val >= lower_bound) && (missing_val <= upper_bound)) {
97 this->missing_symbols_.erase(iter);
103 std::size_t size = 0;
104 for (
const auto& label: var.labels()) {
110 if (this->missing_symbols_.exists(label)) { this->missing_symbols_.erase(label); }
117 _real_variable_ = var.clone();
119 GUM_CONSTRUCTOR(DBTranslator4DiscretizedVariable);
123 DBTranslator4DiscretizedVariable::DBTranslator4DiscretizedVariable(
124 const IDiscretizedVariable& var,
125 std::size_t max_dico_entries) :
126 DBTranslator(DBTranslatedValueType::
DISCRETE, false, false, max_dico_entries),
127 _variable_(var.name(), var.description()) {
129 if (var.domainSize() > max_dico_entries) {
130 GUM_ERROR(SizeError,
"the dictionary induced by the variable is too large")
134 const auto ticks = var.ticksAsDoubles();
135 for (
const auto tick: ticks) {
136 _variable_.addTick((
float)tick);
140 std::size_t size = 0;
141 for (
const auto& label: var.labels()) {
142 this->back_dico_.insert(size, label);
147 _real_variable_ = var.clone();
149 GUM_CONSTRUCTOR(DBTranslator4DiscretizedVariable);
153 DBTranslator4DiscretizedVariable::DBTranslator4DiscretizedVariable(
154 const DBTranslator4DiscretizedVariable& from) :
155 DBTranslator(from), _variable_(from._variable_) {
157 _real_variable_ = from._real_variable_->clone();
159 GUM_CONS_CPY(DBTranslator4DiscretizedVariable);
163 DBTranslator4DiscretizedVariable::DBTranslator4DiscretizedVariable(
164 DBTranslator4DiscretizedVariable&& from) :
165 DBTranslator(
std::move(from)), _variable_(
std::move(from._variable_)) {
167 _real_variable_ = from._real_variable_;
168 from._real_variable_ =
nullptr;
170 GUM_CONS_MOV(DBTranslator4DiscretizedVariable);
174 DBTranslator4DiscretizedVariable* DBTranslator4DiscretizedVariable::clone()
const {
175 return new DBTranslator4DiscretizedVariable(*
this);
179 DBTranslator4DiscretizedVariable&
180 DBTranslator4DiscretizedVariable::operator=(
const DBTranslator4DiscretizedVariable& from) {
182 DBTranslator::operator=(from);
183 _variable_ = from._variable_;
185 if (_real_variable_ !=
nullptr)
delete _real_variable_;
186 _real_variable_ = from._real_variable_->clone();
193 DBTranslator4DiscretizedVariable&
194 DBTranslator4DiscretizedVariable::operator=(DBTranslator4DiscretizedVariable&& from) {
196 DBTranslator::operator=(std::move(from));
197 _variable_ = std::move(from._variable_);
199 if (_real_variable_ !=
nullptr)
delete _real_variable_;
200 _real_variable_ = from._real_variable_;
201 from._real_variable_ =
nullptr;
207 DBTranslatedValue DBTranslator4DiscretizedVariable::translate(std::string_view str) {
210 return DBTranslatedValue{std::size_t(_variable_[str])};
213 if (this->isMissingSymbol(str))
214 return DBTranslatedValue{std::numeric_limits< std::size_t >::max()};
220 return DBTranslatedValue{this->back_dico_.first(std::string(str))};
222 if (!DBCell::isReal(str)) {
224 "String \"" << str <<
"\" cannot be translated because it is not a value for "
228 "The translation of \"" << str <<
"\" could not be found : " << e.
what()
229 <<
" for variable " << _variable_);
The databases' cell translators for discretized variables.
void insert(const T1 &first, const T2 &second)
Base class for all aGrUM's exceptions.
GUM_NODISCARD const char * what() const noexcept override
Exception : wrong type for this operation.
Error: An unknown label is found in the database.
DBTranslator4DiscretizedVariable(const DiscretizedVariable< GUM_SCALAR > &var, const std::vector< std::string > &missing_symbols, std::size_t max_dico_entries=std::numeric_limits< std::size_t >::max())
default constructor with a discretized variable as translator
The base class for all the tabular database cell translators.
Bijection< std::size_t, std::string > back_dico_
the bijection relating back translated values and their original strings.
#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