diff options
-rw-r--r-- | mert/BleuScorer.cpp | 3 | ||||
-rw-r--r-- | mert/Jamfile | 2 | ||||
-rw-r--r-- | mert/Scorer.cpp | 7 | ||||
-rw-r--r-- | mert/Scorer.h | 2 | ||||
-rw-r--r-- | mert/Singleton.h | 33 | ||||
-rw-r--r-- | mert/SingletonTest.cpp | 27 | ||||
-rw-r--r-- | mert/Vocabulary.cpp | 21 | ||||
-rw-r--r-- | mert/Vocabulary.h | 10 | ||||
-rw-r--r-- | mert/VocabularyTest.cpp | 26 |
9 files changed, 123 insertions, 8 deletions
diff --git a/mert/BleuScorer.cpp b/mert/BleuScorer.cpp index cae390a18..58450cffb 100644 --- a/mert/BleuScorer.cpp +++ b/mert/BleuScorer.cpp @@ -10,6 +10,7 @@ #include "Ngram.h" #include "Reference.h" #include "Util.h" +#include "Vocabulary.h" namespace { @@ -64,7 +65,7 @@ void BleuScorer::setReferenceFiles(const vector<string>& referenceFiles) { // Make sure reference data is clear m_references.reset(); - ClearVocabulary(); + mert::VocabularyFactory::GetVocabulary()->clear(); //load reference data for (size_t i = 0; i < referenceFiles.size(); ++i) { diff --git a/mert/Jamfile b/mert/Jamfile index ab105db43..874ff8014 100644 --- a/mert/Jamfile +++ b/mert/Jamfile @@ -37,6 +37,7 @@ TER/tools.cpp TerScorer.cpp CderScorer.cpp MergeScorer.cpp +Vocabulary.cpp ../util//kenutil m ..//z ; exe mert : mert.cpp mert_lib ../moses/src//ThreadPool ; @@ -54,6 +55,7 @@ unit-test data_test : DataTest.cpp mert_lib ..//boost_unit_test_framework ; unit-test ngram_test : NgramTest.cpp mert_lib ..//boost_unit_test_framework ; unit-test optimizer_factory_test : OptimizerFactoryTest.cpp mert_lib ..//boost_unit_test_framework ; unit-test reference_test : ReferenceTest.cpp mert_lib ..//boost_unit_test_framework ; +unit-test singleton_test : SingletonTest.cpp mert_lib ..//boost_unit_test_framework ; unit-test timer_test : TimerTest.cpp mert_lib ..//boost_unit_test_framework ; unit-test util_test : UtilTest.cpp mert_lib ..//boost_unit_test_framework ; unit-test vocabulary_test : VocabularyTest.cpp mert_lib ..//boost_unit_test_framework ; diff --git a/mert/Scorer.cpp b/mert/Scorer.cpp index 6142a88dd..3aaae54c0 100644 --- a/mert/Scorer.cpp +++ b/mert/Scorer.cpp @@ -3,6 +3,7 @@ #include <limits> #include "Vocabulary.h" #include "Util.h" +#include "Singleton.h" namespace { @@ -36,14 +37,14 @@ inline float score_average(const statscores_t& scores, size_t start, size_t end) Scorer::Scorer(const string& name, const string& config) : m_name(name), - m_vocab(new mert::Vocabulary), + m_vocab(mert::VocabularyFactory::GetVocabulary()), m_score_data(0), m_enable_preserve_case(true) { InitConfig(config); } Scorer::~Scorer() { - delete m_vocab; + Singleton<mert::Vocabulary>::Delete(); } void Scorer::InitConfig(const string& config) { @@ -81,8 +82,6 @@ void Scorer::TokenizeAndEncode(const string& line, vector<int>& encoded) { } } -void Scorer::ClearVocabulary() { m_vocab->clear(); } - /** * Set the factors, which should be used for this metric */ diff --git a/mert/Scorer.h b/mert/Scorer.h index f57293cb8..e9f0e1fe4 100644 --- a/mert/Scorer.h +++ b/mert/Scorer.h @@ -144,8 +144,6 @@ class Scorer * Note: We assume that all tokens are separated by whitespaces. */ void TokenizeAndEncode(const string& line, vector<int>& encoded); - - void ClearVocabulary(); }; /** diff --git a/mert/Singleton.h b/mert/Singleton.h new file mode 100644 index 000000000..9fef3e639 --- /dev/null +++ b/mert/Singleton.h @@ -0,0 +1,33 @@ +#ifndef MERT_SINGLETON_H_ +#define MERT_SINGLETON_H_ + +#include <cstdlib> + +// thread *un*safe singleton. +// TODO: replace this with thread-safe singleton. +template <typename T> +class Singleton { + public: + static T* GetInstance() { + if (m_instance == NULL) { + m_instance = new T; + } + return m_instance; + } + + static void Delete() { + if (m_instance) { + delete m_instance; + m_instance = NULL; + } + } + + private: + Singleton(); + static T* m_instance; +}; + +template <typename T> +T* Singleton<T>::m_instance = NULL; + +#endif // MERT_SINGLETON_H_ diff --git a/mert/SingletonTest.cpp b/mert/SingletonTest.cpp new file mode 100644 index 000000000..2c44bdc1f --- /dev/null +++ b/mert/SingletonTest.cpp @@ -0,0 +1,27 @@ +#include "Singleton.h" + +#define BOOST_TEST_MODULE MertSingleton +#include <boost/test/unit_test.hpp> + +namespace { + +static int g_count = 0; + +class Instance { + public: + Instance() { ++g_count; } + ~Instance() {} +}; + +} // namespace + +BOOST_AUTO_TEST_CASE(singleton_basic) { + Instance* instance1 = Singleton<Instance>::GetInstance(); + Instance* instance2 = Singleton<Instance>::GetInstance(); + Instance* instance3 = Singleton<Instance>::GetInstance(); + BOOST_REQUIRE(instance1 == instance2); + BOOST_REQUIRE(instance2 == instance3); + BOOST_CHECK_EQUAL(1, g_count); + + Singleton<Instance>::Delete(); +} diff --git a/mert/Vocabulary.cpp b/mert/Vocabulary.cpp new file mode 100644 index 000000000..40b04bf99 --- /dev/null +++ b/mert/Vocabulary.cpp @@ -0,0 +1,21 @@ +#include "Vocabulary.h" +#include "Singleton.h" + +namespace mert { +namespace { +Vocabulary* g_vocab = NULL; +} // namespace + +Vocabulary* VocabularyFactory::GetVocabulary() { + if (g_vocab == NULL) { + return Singleton<Vocabulary>::GetInstance(); + } else { + return g_vocab; + } +} + +void VocabularyFactory::SetVocabulary(Vocabulary* vocab) { + g_vocab = vocab; +} + +} // namespace mert diff --git a/mert/Vocabulary.h b/mert/Vocabulary.h index f9493823d..12c8c1727 100644 --- a/mert/Vocabulary.h +++ b/mert/Vocabulary.h @@ -64,6 +64,16 @@ class Vocabulary { std::map<std::string, int> m_vocab; }; +class VocabularyFactory { + public: + static Vocabulary* GetVocabulary(); + static void SetVocabulary(Vocabulary* vocab); + + private: + VocabularyFactory() {} + virtual ~VocabularyFactory() {} +}; + } // namespace mert #endif // MERT_VOCABULARY_H_ diff --git a/mert/VocabularyTest.cpp b/mert/VocabularyTest.cpp index 2439301a5..0e67ba62a 100644 --- a/mert/VocabularyTest.cpp +++ b/mert/VocabularyTest.cpp @@ -3,8 +3,19 @@ #define BOOST_TEST_MODULE MertVocabulary #include <boost/test/unit_test.hpp> +#include "Singleton.h" + +namespace mert { +namespace { + +void TearDown() { + Singleton<Vocabulary>::Delete(); +} + +} // namespace + BOOST_AUTO_TEST_CASE(vocab_basic) { - mert::Vocabulary vocab; + Vocabulary vocab; BOOST_REQUIRE(vocab.empty()); vocab.clear(); @@ -26,3 +37,16 @@ BOOST_AUTO_TEST_CASE(vocab_basic) { BOOST_CHECK(!vocab.Lookup("hello", &v)); BOOST_CHECK(!vocab.Lookup("world", &v)); } + +BOOST_AUTO_TEST_CASE(vocab_factory_test) { + Vocabulary* vocab1 = VocabularyFactory::GetVocabulary(); + Vocabulary* vocab2 = VocabularyFactory::GetVocabulary(); + Vocabulary* vocab3 = VocabularyFactory::GetVocabulary(); + + BOOST_REQUIRE(vocab1 != NULL); + BOOST_CHECK(vocab1 == vocab2); + BOOST_CHECK(vocab2 == vocab3); + + TearDown(); +} +} // namespace mert |