Make hash functions equality comparable.

This commit is contained in:
Matthias Vallentin 2013-07-22 18:03:55 +02:00
parent 9c2f57a9d9
commit eb64f5f961
3 changed files with 93 additions and 38 deletions

View file

@ -58,6 +58,7 @@
#define H3_H
#include <climits>
#include <cstring>
// The number of values representable by a byte.
#define H3_BYTE_RANGE (UCHAR_MAX+1)
@ -112,6 +113,17 @@ public:
return result;
}
friend bool operator==(const H3& x, const H3& y)
{
return ! std::memcmp(x.byte_lookup, y.byte_lookup, N * H3_BYTE_RANGE);
}
friend bool operator!=(const H3& x, const H3& y)
{
return ! (x == y);
}
private:
T byte_lookup[N][H3_BYTE_RANGE];
};

View file

@ -59,6 +59,19 @@ Hasher::digest_vector DefaultHasher::Hash(const void* x, size_t n) const
return h;
}
DefaultHasher* DefaultHasher::Clone() const
{
return new DefaultHasher(*this);
}
bool DefaultHasher::Equals(const Hasher* other) const /* final */
{
if ( typeid(*this) != typeid(*other) )
return false;
const DefaultHasher* o = static_cast<const DefaultHasher*>(other);
return hash_functions_ == o->hash_functions_;
}
DoubleHasher::DoubleHasher(size_t k, const std::string& name)
: Hasher(k, name),
h1_(1, name),
@ -76,4 +89,16 @@ Hasher::digest_vector DoubleHasher::Hash(const void* x, size_t n) const
return h;
}
DoubleHasher* DoubleHasher::Clone() const
{
return new DoubleHasher(*this);
}
bool DoubleHasher::Equals(const Hasher* other) const /* final */
{
if ( typeid(*this) != typeid(*other) )
return false;
const DoubleHasher* o = static_cast<const DoubleHasher*>(other);
return h1_ == o->h1_ && h2_ == o->h2_;
}

View file

@ -31,6 +31,10 @@ public:
virtual digest_vector Hash(const void* x, size_t n) const = 0;
virtual Hasher* Clone() const = 0;
virtual bool Equals(const Hasher* other) const = 0;
size_t K() const { return k_; }
const std::string& Name() const { return name_; }
@ -64,6 +68,16 @@ protected:
return hash(x, n);
}
friend bool operator==(const UHF& x, const UHF& y)
{
return x.h_ == y.h_;
}
friend bool operator!=(const UHF& x, const UHF& y)
{
return ! (x == y);
}
digest hash(const void* x, size_t n) const;
private:
@ -87,6 +101,8 @@ public:
DefaultHasher(size_t k, const std::string& name);
virtual digest_vector Hash(const void* x, size_t n) const /* final */;
virtual DefaultHasher* Clone() const /* final */;
virtual bool Equals(const Hasher* other) const /* final */;
private:
std::vector<UHF> hash_functions_;
@ -100,6 +116,8 @@ public:
DoubleHasher(size_t k, const std::string& name);
virtual digest_vector Hash(const void* x, size_t n) const /* final */;
virtual DoubleHasher* Clone() const /* final */;
virtual bool Equals(const Hasher* other) const /* final */;
private:
UHF h1_;