Commit 809677a9 authored by Christophe Gonzales's avatar Christophe Gonzales

made the DBTranslators for discretized and continuous variables return the…

made the DBTranslators for discretized and continuous variables return the original variable set by the user when calling method variable()
parent b79d7fa7
Pipeline #23129150 passed with stages
in 67 minutes and 35 seconds
......@@ -364,7 +364,7 @@ namespace gum {
reorder() final;
/// returns the variable stored into the translator
virtual const ContinuousVariable< float >* variable() const final;
virtual const IContinuousVariable* variable() const final;
/// @}
......@@ -376,6 +376,11 @@ namespace gum {
// are floats, this speeds-up translations
ContinuousVariable< float > __variable;
// the ContinuousVariablee returned by method variable ()
// We must return a IContinuousVariable because the user may have
// saved into the translator a ContinuousVariable<X>, with X != float
IContinuousVariable* __real_variable;
// assign to each float missing symbol a Boolean indicating whether
// we already translated it or not. If we translated it, then we cannot
// change the range of the variable so that this range contains the symbol.
......
......@@ -46,7 +46,7 @@ namespace gum {
alloc) :
DBTranslator< ALLOC >(
DBTranslatedValueType::CONTINUOUS, missing_symbols, fit_range, 1, alloc),
__variable("var", ""), __fit_range(fit_range) {
__variable("var", ""), __fit_range(fit_range) {
// Here, if fit_range is set to false, and the range of the
// random variable will remain (-inf,+inf). So all the missing symbols
// that are numbers should be discarded since they lie in the domain
......@@ -72,6 +72,9 @@ namespace gum {
if (__fit_range)
__variable.setLowerBound(std::numeric_limits< float >::infinity());
// store a copy of the variable, that should be used by method variable ()
__real_variable = __variable.clone ();
GUM_CONSTRUCTOR(DBTranslator4ContinuousVariable);
}
......@@ -91,6 +94,9 @@ namespace gum {
if (__fit_range)
__variable.setLowerBound(std::numeric_limits< float >::infinity());
// store a copy of the variable, that should be used by method variable ()
__real_variable = __variable.clone ();
GUM_CONSTRUCTOR(DBTranslator4ContinuousVariable);
}
......@@ -131,6 +137,9 @@ namespace gum {
}
}
// store a copy of the variable, that should be used by method variable ()
__real_variable = var.clone ();
GUM_CONSTRUCTOR(DBTranslator4ContinuousVariable);
}
......@@ -152,6 +161,9 @@ namespace gum {
__variable.setLowerBound(lower_bound);
__variable.setUpperBound(upper_bound);
// store a copy of the variable, that should be used by method variable ()
__real_variable = var.clone ();
GUM_CONSTRUCTOR(DBTranslator4ContinuousVariable);
}
......@@ -192,6 +204,9 @@ namespace gum {
}
}
// store a copy of the variable, that should be used by method variable ()
__real_variable = var.clone ();
GUM_CONSTRUCTOR(DBTranslator4ContinuousVariable);
}
......@@ -212,6 +227,9 @@ namespace gum {
__variable.setLowerBound(lower_bound);
__variable.setUpperBound(upper_bound);
// store a copy of the variable, that should be used by method variable ()
__real_variable = var.clone ();
GUM_CONSTRUCTOR(DBTranslator4ContinuousVariable);
}
......@@ -227,6 +245,9 @@ namespace gum {
__status_float_missing_symbols(from.__status_float_missing_symbols),
__nonfloat_missing_symbol(from.__nonfloat_missing_symbol),
__fit_range(from.__fit_range) {
// store a copy of the variable, that should be used by method variable ()
__real_variable = from.__real_variable->clone ();
GUM_CONS_CPY(DBTranslator4ContinuousVariable);
}
......@@ -250,6 +271,10 @@ namespace gum {
std::move(from.__status_float_missing_symbols)),
__nonfloat_missing_symbol(std::move(from.__nonfloat_missing_symbol)),
__fit_range(from.__fit_range) {
// store a copy of the variable, that should be used by method variable ()
__real_variable = from.__real_variable;
from.__real_variable = nullptr;
GUM_CONS_MOV(DBTranslator4ContinuousVariable);
}
......@@ -291,6 +316,8 @@ namespace gum {
template < template < typename > class ALLOC >
INLINE DBTranslator4ContinuousVariable<
ALLOC >::~DBTranslator4ContinuousVariable() {
if ( __real_variable != nullptr ) delete __real_variable;
GUM_DESTRUCTOR(DBTranslator4ContinuousVariable);
}
......@@ -306,6 +333,9 @@ namespace gum {
__status_float_missing_symbols = from.__status_float_missing_symbols;
__nonfloat_missing_symbol = from.__nonfloat_missing_symbol;
__fit_range = from.__fit_range;
if ( __real_variable != nullptr ) delete __real_variable;
__real_variable = from.__real_variable->clone ();
}
return *this;
......@@ -324,6 +354,10 @@ namespace gum {
std::move(from.__status_float_missing_symbols);
__nonfloat_missing_symbol = std::move(from.__nonfloat_missing_symbol);
__fit_range = from.__fit_range;
if ( __real_variable != nullptr ) delete __real_variable;
__real_variable = from.__real_variable;
from.__real_variable = nullptr;
}
return *this;
......@@ -502,9 +536,11 @@ namespace gum {
/// returns the variable stored into the translator
template < template < typename > class ALLOC >
INLINE const ContinuousVariable< float >*
DBTranslator4ContinuousVariable< ALLOC >::variable() const {
return &__variable;
INLINE const IContinuousVariable*
DBTranslator4ContinuousVariable< ALLOC >::variable() const {
__real_variable->setLowerBoundFromFloat ( __variable.lowerBound () );
__real_variable->setUpperBoundFromFloat ( __variable.upperBound () );
return __real_variable;
}
......
......@@ -313,17 +313,22 @@ namespace gum {
reorder() final;
/// returns the variable stored into the translator
virtual const DiscretizedVariable< float >* variable() const final;
virtual const IDiscretizedVariable* variable() const final;
/// @}
#ifndef DOXYGEN_SHOULD_SKIP_THIS
private:
// the DiscretizedVariable assigned to the translator
// the DiscretizedVariable used for translations
DiscretizedVariable< float > __variable;
// the DiscretizedVariable returned by method variable ()
// We must return a IDiscretizedVariable because the user may have
// saved into the translator a DiscretizedVariable<X>, with X != float
IDiscretizedVariable* __real_variable;
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
};
......
......@@ -97,6 +97,9 @@ namespace gum {
++size;
}
// store a copy of the variable, that should be used by method variable ()
__real_variable = var.clone ();
GUM_CONSTRUCTOR(DBTranslator4DiscretizedVariable);
}
......@@ -161,6 +164,9 @@ namespace gum {
++size;
}
// store a copy of the variable, that should be used by method variable ()
__real_variable = var.clone ();
GUM_CONSTRUCTOR(DBTranslator4DiscretizedVariable);
}
......@@ -195,6 +201,9 @@ namespace gum {
++size;
}
// store a copy of the variable, that should be used by method variable ()
__real_variable = var.clone ();
GUM_CONSTRUCTOR(DBTranslator4DiscretizedVariable);
}
......@@ -228,6 +237,9 @@ namespace gum {
++size;
}
// store a copy of the variable, that should be used by method variable ()
__real_variable = var.clone ();
GUM_CONSTRUCTOR(DBTranslator4DiscretizedVariable);
}
......@@ -238,8 +250,11 @@ namespace gum {
const DBTranslator4DiscretizedVariable< ALLOC >& from,
const typename DBTranslator4DiscretizedVariable< ALLOC >::allocator_type&
alloc) :
DBTranslator< ALLOC >(from, alloc),
__variable(from.__variable) {
DBTranslator< ALLOC >(from, alloc),
__variable(from.__variable) {
// store a copy of the variable, that should be used by method variable ()
__real_variable = from.__real_variable->clone ();
GUM_CONS_CPY(DBTranslator4DiscretizedVariable);
}
......@@ -259,6 +274,10 @@ namespace gum {
alloc) :
DBTranslator< ALLOC >(std::move(from), alloc),
__variable(std::move(from.__variable)) {
// moves the copy of the variable, that should be used by method variable ()
__real_variable = from.__real_variable;
from.__real_variable = nullptr;
GUM_CONS_MOV(DBTranslator4DiscretizedVariable);
}
......@@ -278,7 +297,7 @@ namespace gum {
const typename DBTranslator4DiscretizedVariable< ALLOC >::allocator_type&
alloc) const {
ALLOC< DBTranslator4DiscretizedVariable< ALLOC > > allocator(alloc);
DBTranslator4DiscretizedVariable< ALLOC >* translator =
DBTranslator4DiscretizedVariable< ALLOC >* translator =
allocator.allocate(1);
try {
allocator.construct(translator, *this, alloc);
......@@ -302,6 +321,8 @@ namespace gum {
template < template < typename > class ALLOC >
INLINE DBTranslator4DiscretizedVariable<
ALLOC >::~DBTranslator4DiscretizedVariable() {
if ( __real_variable != nullptr ) delete __real_variable;
GUM_DESTRUCTOR(DBTranslator4DiscretizedVariable);
}
......@@ -314,6 +335,9 @@ namespace gum {
if (this != &from) {
DBTranslator< ALLOC >::operator=(from);
__variable = from.__variable;
if ( __real_variable != nullptr ) delete __real_variable;
__real_variable = from.__real_variable->clone ();
}
return *this;
......@@ -328,6 +352,10 @@ namespace gum {
if (this != &from) {
DBTranslator< ALLOC >::operator=(std::move(from));
__variable = std::move(from.__variable);
if ( __real_variable != nullptr ) delete __real_variable;
__real_variable = from.__real_variable;
from.__real_variable = nullptr;
}
return *this;
......@@ -424,9 +452,9 @@ namespace gum {
/// returns the variable stored into the translator
template < template < typename > class ALLOC >
INLINE const DiscretizedVariable< float >*
INLINE const IDiscretizedVariable*
DBTranslator4DiscretizedVariable< ALLOC >::variable() const {
return &__variable;
return __real_variable;
}
......
......@@ -175,7 +175,7 @@ namespace gum {
std::size_t max_dico_entries = std::numeric_limits< std::size_t >::max(),
const allocator_type& alloc = allocator_type());
/// default constructor with a discrete variable as translator
/// default constructor with a range variable as translator
/** @param var a range variable which will be used for translations.
* The translator keeps a copy of this variable
* @param missing_symbols the set of symbols in the dataset
......@@ -203,7 +203,7 @@ namespace gum {
std::size_t max_dico_entries = std::numeric_limits< std::size_t >::max(),
const allocator_type& alloc = allocator_type());
/** @brief default constructor with a discrete variable as translator
/** @brief default constructor with a range variable as translator
* but without missing symbols
*
* @param var a range variable which will be used for translations.
......
......@@ -34,7 +34,12 @@
namespace gum {
/** @class IContinuousVariable
* @headerfile IContinuousVariable.h <agrum/variables/IContinuousVariable.h>
* @ingroup multidim_group
* @brief A base class for continuous variables, independent of the
* GUM_SCALAR type
*/
class IContinuousVariable : public Variable {
public:
// ############################################################################
......
......@@ -29,6 +29,11 @@
namespace gum {
/** @class IDiscretizedVariable
* @headerfile IDiscretizedVariable.h <agrum/variables/IDiscretizedVariable.h>
* @ingroup multidim_group
* @brief A base class for discretized variables, independent of the ticks type
*/
class IDiscretizedVariable : public DiscreteVariable {
public:
// ############################################################################
......
......@@ -237,27 +237,26 @@ namespace gum_tests {
allocator.destroy(translator5);
allocator.deallocate(translator5, 1);
TS_ASSERT(translator4.translate("12").cont_val == 12.0f);
TS_ASSERT(translator4.variable()->lowerBound() == -2.0);
TS_ASSERT(translator4.variable()->upperBound() == 12.0);
TS_ASSERT(translator4.variable()->lowerBoundAsFloat() == -2.0f);
TS_ASSERT(translator4.variable()->upperBoundAsFloat() == 12.0f);
translator4 = translator2;
TS_ASSERT(translator4.variable()->lowerBound() == -2.0);
TS_ASSERT(translator4.variable()->upperBound() == 10.0);
TS_ASSERT(translator4.variable()->lowerBoundAsFloat() == -2.0f);
TS_ASSERT(translator4.variable()->upperBoundAsFloat() == 10.0f);
TS_ASSERT(translator.translate("12").cont_val == 12.0f);
TS_ASSERT(translator.variable()->lowerBound() == -2.0);
TS_ASSERT(translator.variable()->upperBound() == 12.0);
TS_ASSERT(translator.variable()->lowerBoundAsFloat() == -2.0f);
TS_ASSERT(translator.variable()->upperBoundAsFloat() == 12.0f);
translator = translator2;
TS_ASSERT(translator.variable()->lowerBound() == -2.0);
TS_ASSERT(translator.variable()->upperBound() == 10.0);
TS_ASSERT(translator.variable()->lowerBoundAsFloat() == -2.0f);
TS_ASSERT(translator.variable()->upperBoundAsFloat() == 10.0f);
TS_ASSERT(translator.translate("12").cont_val == 12.0f);
TS_ASSERT(translator.variable()->lowerBound() == -2.0);
TS_ASSERT(translator.variable()->upperBound() == 12.0);
TS_ASSERT(translator.variable()->lowerBoundAsFloat() == -2.0f);
TS_ASSERT(translator.variable()->upperBoundAsFloat() == 12.0f);
translator = std::move(translator2);
TS_ASSERT(translator.variable()->lowerBound() == -2.0);
TS_ASSERT(translator.variable()->upperBound() == 10.0);
TS_ASSERT(translator.variable()->lowerBoundAsFloat() == -2.0f);
TS_ASSERT(translator.variable()->upperBoundAsFloat() == 10.0f);
}
TS_ASSERT(MyAllocCount::hasMeroryLeak() == false);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment