hash and equals for AlignmentInfo

This commit is contained in:
Barry Haddow 2012-10-11 22:03:32 +01:00
parent a638736722
commit 2bef14a43e
2 changed files with 107 additions and 0 deletions

View File

@ -25,6 +25,8 @@
#include <vector>
#include <cstdlib>
#include <boost/functional/hash.hpp>
namespace Moses
{
@ -37,6 +39,8 @@ class AlignmentInfo
{
friend std::ostream& operator<<(std::ostream &, const AlignmentInfo &);
friend struct AlignmentInfoOrderer;
friend struct AlignmentInfoComparator;
friend struct AlignmentInfoHasher;
friend class AlignmentInfoCollection;
public:
@ -115,4 +119,32 @@ struct AlignmentInfoOrderer
}
};
/**
* Equality functoid
**/
struct AlignmentInfoComparator
{
inline bool operator()(const AlignmentInfo &a, const AlignmentInfo &b) const {
return a.m_collection == b.m_collection &&
a.m_terminalCollection == b.m_terminalCollection &&
a.m_nonTermIndexMap == b.m_nonTermIndexMap;
}
};
/**
* Hashing functoid
**/
struct AlignmentInfoHasher
{
size_t operator()(const AlignmentInfo& a) const
{
size_t seed = 0;
boost::hash_combine(seed,a.m_collection);
boost::hash_combine(seed,a.m_terminalCollection);
boost::hash_combine(seed,a.m_nonTermIndexMap);
return seed;
}
};
}

View File

@ -0,0 +1,75 @@
/***********************************************************************
Moses - factored phrase-based language decoder
Copyright (C) 2010- University of Edinburgh
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
***********************************************************************/
#include <boost/test/unit_test.hpp>
#include "AlignmentInfo.h"
#include "AlignmentInfoCollection.h"
using namespace Moses;
using namespace std;
BOOST_AUTO_TEST_SUITE(alignment_info)
typedef pair<size_t,size_t> IndexPair;
typedef set<pair<size_t,size_t> > IndexSet;
struct AlignmentInfoFixture {
const AlignmentInfo* ai1;
const AlignmentInfo* ai2;
const AlignmentInfo* ai3;
AlignmentInfoFixture()
{
AlignmentInfoCollection& collection = AlignmentInfoCollection::Instance();
IndexSet aligns1,aligns2,aligns3;
aligns1.insert(IndexPair(1,1));
aligns1.insert(IndexPair(2,1));
aligns2.insert(IndexPair(1,1));
aligns2.insert(IndexPair(2,1));
aligns3.insert(IndexPair(1,2));
aligns3.insert(IndexPair(2,1));
int ind1[] = {0,1};
int ind2[] = {0};
ai1 = collection.Add(aligns1, ind1);
ai2 = collection.Add(aligns2, ind1);
ai3 = collection.Add(aligns3, ind2);
}
};
BOOST_FIXTURE_TEST_CASE(comparator, AlignmentInfoFixture)
{
AlignmentInfoComparator comp;
BOOST_CHECK(comp(*ai1,*ai2));
BOOST_CHECK(comp(*ai1,*ai1));
BOOST_CHECK(comp(*ai2,*ai2));
BOOST_CHECK(comp(*ai3,*ai3));
BOOST_CHECK(!comp(*ai2,*ai3));
BOOST_CHECK(!comp(*ai1,*ai3));
}
BOOST_FIXTURE_TEST_CASE(hasher, AlignmentInfoFixture)
{
//simple test that same objects give same hash
AlignmentInfoHasher hash;
BOOST_CHECK_EQUAL(hash(*ai1), hash(*ai2));
}
BOOST_AUTO_TEST_SUITE_END()