[aGrUM] add Instantiation::operator== and HashFunc<Instantiation> (Instantiation…

[aGrUM] add Instantiation::operator== and HashFunc<Instantiation> (Instantiation can now be key for set, hashtable, etc.)
parent f558bf90
......@@ -775,7 +775,6 @@ namespace gum {
*/
Size operator()(const Debug& key) const;
};
} /* namespace gum */
/// include the inlined functions if necessary
......
......@@ -109,7 +109,6 @@ namespace gum {
/// returns a hashed key for hash tables the keys of which are represented
/// by vectors of Idx
INLINE Size HashFunc< std::vector< Idx > >::
operator()(const std::vector< Idx >& key) const {
Size h = 0;
......
......@@ -1038,6 +1038,11 @@ namespace gum {
// =========================================================================
/// @{
/**
* @brief operator==
*/
bool operator==(const Instantiation& other) const;
/**
* @brief Alias of Instantiation::inc().
* @return Returns this Instantiation.
......@@ -1230,6 +1235,20 @@ namespace gum {
*/
std::ostream& operator<<(std::ostream&, const Instantiation&);
/**
* @brief Hash function for gum::Instantiation.
* @ingroup hashfunctions_group
*/
template <>
class HashFunc< Instantiation > : public HashFuncBase< Instantiation > {
public:
/**
* @brief Computes the hashed value of a key.
* @param key The key to compute the hashed value.
* @return Returns the hashed value of a key.
*/
Size operator()(const Instantiation& key) const;
};
} /* namespace gum */
#ifndef GUM_NO_INLINE
......
......@@ -821,4 +821,23 @@ namespace gum {
__vars.setAtPos(__vars.pos(x), y);
}
/// returns a hashed key for hash tables the keys of which are represented
/// by vectors of Idx
INLINE Size HashFunc< Instantiation >::
operator()(const Instantiation& key) const {
Size h = 0;
for (const auto& k : key.variablesSequence()) // k are unique only by address (not by name)
h += Size(k) * key.val(*k);
return ((h * HashFuncConst::gold) & this->_hash_mask);
}
INLINE bool Instantiation::operator==(const Instantiation& other) const {
if (other.nbrDim() != nbrDim()) return false;
for (const auto& k : variablesSequence()) {
if (!other.contains(k)) return false;
if (val(*k) != other.val(*k)) return false;
}
return true;
}
} /* namespace gum */
......@@ -326,6 +326,22 @@ namespace gum_tests {
}
}
void testOperatorEqual() {
gum::LabelizedVariable a("a", "first var", 2), b("b", "second var", 4),
c("c", "third var", 5), d("d", "fourth var", 2);
gum::MultiDimArray< char > p;
p << a << b << c;
gum::Instantiation i1(p), i2(p);
i2.setFirst();
++i2; // for i2 to be !=i1 in the first iteration
for (i1.setFirst(); !i1.end(); ++i1) {
TS_ASSERT_DIFFERS(i1, i2);
p.fromOffset(i2, p.toOffset(i1));
TS_ASSERT_EQUALS(i1, i2);
}
}
private:
// Builds a BN to test the inference
void fill(gum::BayesNet< double >& bn) {
......
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