[aGrUM] API change for LabelizedVariable : new constructor with labels as vector…

[aGrUM] API change for LabelizedVariable : new constructor with labels as vector of string + LabelizedVariable::posLabel(string)
parent 0f5f4722
......@@ -53,7 +53,7 @@ namespace gum {
// ############################################################################
/// @{
/** @brief Default constructor
/** @brief constructor
* @param aName the name
* @param aDesc the Description
* @param nbrLabel the domainSize (2 by default)
......@@ -63,6 +63,16 @@ namespace gum {
const std::string& aDesc = "",
const int nbrLabel = 2);
/** @brief constructor
* @param aName the name
* @param aDesc the Description
* @param labels the labels
*/
LabelizedVariable(const std::string& aName,
const std::string& aDesc,
const std::vector< std::string >& labels);
/// Copy constructor
/**
* @param aLDRV the variable we copy
......@@ -138,6 +148,9 @@ namespace gum {
* @return the ith label */
virtual std::string label(Idx i) const;
/// return the pos from label
Idx posLabel(const std::string& label) const;
/// get a numerical representation of he indice-the value.
virtual double numerical(Idx indice) const;
......
......@@ -83,6 +83,21 @@ namespace gum {
addLabel(oss.str());
}
}
INLINE LabelizedVariable::LabelizedVariable(
const std::string& aName,
const std::string& aDesc,
const std::vector< std::string >& labels)
: DiscreteVariable(aName, aDesc) {
// for debugging purposes
GUM_CONSTRUCTOR(LabelizedVariable);
__labels.clear();
for (Idx i = 0; i < labels.size(); ++i)
__labels.insert(labels[i]);
}
INLINE Idx LabelizedVariable::posLabel(const std::string& label) const {
return __labels.pos(label);
}
// Copy constructor
......
......@@ -95,5 +95,13 @@ namespace gum_tests {
TS_ASSERT_EQUALS(var1.numerical(2), 2);
TS_ASSERT_EQUALS(var1.numerical(3), 3);
}
void testAndConstructorWithLabels() {
gum::LabelizedVariable var1(
"var1", "this is var1", {"rouge", "vert", "bleu"});
TS_ASSERT_EQUALS(var1.toString(), "var1<rouge,vert,bleu>");
TS_ASSERT_EQUALS(var1.posLabel("vert"), gum::Idx(1));
}
};
}
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