From 4f02f19ab43f8f566bc621943a2be8fa95edf45c Mon Sep 17 00:00:00 2001 From: Hieu Hoang Date: Sun, 10 May 2015 10:19:26 +0100 Subject: [PATCH] beautify --- moses/ContextScope.h | 89 +++++++++---------- .../LexicalReordering/LexicalReordering.cpp | 19 ++-- symal/cmd.h | 3 +- symal/symal.cpp | 26 +++--- 4 files changed, 62 insertions(+), 75 deletions(-) diff --git a/moses/ContextScope.h b/moses/ContextScope.h index cd12d6d32..e1173cbdc 100644 --- a/moses/ContextScope.h +++ b/moses/ContextScope.h @@ -20,20 +20,20 @@ namespace Moses { class ContextScope { - protected: - typedef std::map > scratchpad_t; - typedef scratchpad_t::iterator iter_t; - typedef scratchpad_t::value_type entry_t; - typedef scratchpad_t::const_iterator const_iter_t; - scratchpad_t m_scratchpad; +protected: + typedef std::map > scratchpad_t; + typedef scratchpad_t::iterator iter_t; + typedef scratchpad_t::value_type entry_t; + typedef scratchpad_t::const_iterator const_iter_t; + scratchpad_t m_scratchpad; #ifdef WITH_THREADS - mutable boost::shared_mutex m_lock; + mutable boost::shared_mutex m_lock; #endif - public: - // class write_access - // { - // boost::unique_lock m_lock; - // public: +public: + // class write_access + // { + // boost::unique_lock m_lock; + // public: // write_access(boost::shared_mutex& lock) // : m_lock(lock) @@ -50,53 +50,48 @@ class ContextScope // return write_access(m_lock); // } - template - boost::shared_ptr const& - set(void const* const key, boost::shared_ptr const& val) - { + template + boost::shared_ptr const& + set(void const* const key, boost::shared_ptr const& val) { #ifdef WITH_THREADS - boost::unique_lock lock(m_lock); + boost::unique_lock lock(m_lock); #endif - return (m_scratchpad[key] = val); - } + return (m_scratchpad[key] = val); + } - template - boost::shared_ptr const - get(void const* key, bool CreateNewIfNecessary=false) - { + template + boost::shared_ptr const + get(void const* key, bool CreateNewIfNecessary=false) { #ifdef WITH_THREADS - using boost::shared_mutex; - using boost::upgrade_lock; - // T const* key = reinterpret_cast(xkey); - upgrade_lock lock(m_lock); - #endif - iter_t m = m_scratchpad.find(key); - boost::shared_ptr< T > ret; - if (m != m_scratchpad.end()) - { - if (m->second == NULL && CreateNewIfNecessary) - { -#ifdef WITH_THREADS - boost::upgrade_to_unique_lock xlock(lock); + using boost::shared_mutex; + using boost::upgrade_lock; + // T const* key = reinterpret_cast(xkey); + upgrade_lock lock(m_lock); #endif - m->second.reset(new T); - } - ret = boost::static_pointer_cast< T >(m->second); - return ret; - } - if (!CreateNewIfNecessary) return ret; + iter_t m = m_scratchpad.find(key); + boost::shared_ptr< T > ret; + if (m != m_scratchpad.end()) { + if (m->second == NULL && CreateNewIfNecessary) { #ifdef WITH_THREADS - boost::upgrade_to_unique_lock xlock(lock); + boost::upgrade_to_unique_lock xlock(lock); #endif - ret.reset(new T); - m_scratchpad[key] = ret; + m->second.reset(new T); + } + ret = boost::static_pointer_cast< T >(m->second); return ret; } + if (!CreateNewIfNecessary) return ret; +#ifdef WITH_THREADS + boost::upgrade_to_unique_lock xlock(lock); +#endif + ret.reset(new T); + m_scratchpad[key] = ret; + return ret; + } ContextScope() { } - ContextScope(ContextScope const& other) - { + ContextScope(ContextScope const& other) { #ifdef WITH_THREADS boost::unique_lock lock1(this->m_lock); boost::unique_lock lock2(other.m_lock); diff --git a/moses/FF/LexicalReordering/LexicalReordering.cpp b/moses/FF/LexicalReordering/LexicalReordering.cpp index 74ba3bf33..fc8258631 100644 --- a/moses/FF/LexicalReordering/LexicalReordering.cpp +++ b/moses/FF/LexicalReordering/LexicalReordering.cpp @@ -140,17 +140,14 @@ SetCache(TranslationOption& to) const if (to.GetLexReorderingScores(this)) return; // Scores were were set already (e.g., by sampling phrase table) - if (m_table) - { - Phrase const& sphrase = to.GetInputPath().GetPhrase(); - Phrase const& tphrase = to.GetTargetPhrase(); - to.CacheLexReorderingScores(*this, this->GetProb(sphrase,tphrase)); - } - else // e.g. OOV with Mmsapt - { - Scores vals(GetNumScoreComponents(), 0); - to.CacheLexReorderingScores(*this, vals); - } + if (m_table) { + Phrase const& sphrase = to.GetInputPath().GetPhrase(); + Phrase const& tphrase = to.GetTargetPhrase(); + to.CacheLexReorderingScores(*this, this->GetProb(sphrase,tphrase)); + } else { // e.g. OOV with Mmsapt + Scores vals(GetNumScoreComponents(), 0); + to.CacheLexReorderingScores(*this, vals); + } } LRModel const& diff --git a/symal/cmd.h b/symal/cmd.h index 09f3bf9c5..71b2cd94a 100644 --- a/symal/cmd.h +++ b/symal/cmd.h @@ -5,8 +5,7 @@ #define CMD_H -enum CommandType -{ +enum CommandType { CMDDOUBLETYPE = 1, CMDENUMTYPE, CMDINTTYPE, diff --git a/symal/symal.cpp b/symal/symal.cpp index 76749bc9e..e1108d236 100644 --- a/symal/symal.cpp +++ b/symal/symal.cpp @@ -20,8 +20,7 @@ const int MAX_WORD = 10000; // maximum lengthsource/target strings const int MAX_M = 400; // maximum length of source strings const int MAX_N = 400; // maximum length of target strings -enum Alignment -{ +enum Alignment { UNION = 1, INTERSECT, GROW, @@ -429,25 +428,24 @@ int main(int argc, char** argv) istream *inp = &std::cin; ostream *out = &std::cout; - try - { + try { if (input) { fstream *fin = new fstream(input,ios::in); if (!fin->is_open()) throw runtime_error("cannot open " + string(input)); inp = fin; } - + if (output) { fstream *fout = new fstream(output,ios::out); if (!fout->is_open()) throw runtime_error("cannot open " + string(output)); out = fout; } - + int a[MAX_M],b[MAX_N],m,n; fa=new int[MAX_M+1]; ea=new int[MAX_N+1]; - - + + int sents = 0; A=new int *[MAX_N+1]; for (int i=1; i<=MAX_N; i++) A[i]=new int[MAX_M+1]; @@ -473,14 +471,14 @@ int main(int argc, char** argv) cerr << "symal: computing grow alignment: diagonal (" << diagonal << ") final ("<< isfinal << ")" << "both-uncovered (" << bothuncovered <<")\n"; - + while(getals(*inp,m,a,n,b)) printgrow(*out,m,a,n,b,diagonal,isfinal,bothuncovered); - + break; case TGTTOSRC: cerr << "symal: computing target-to-source alignment\n"; - + while(getals(*inp,m,a,n,b)) { printtgttosrc(*out,m,a,n,b); sents++; @@ -489,7 +487,7 @@ int main(int argc, char** argv) break; case SRCTOTGT: cerr << "symal: computing source-to-target alignment\n"; - + while(getals(*inp,m,a,n,b)) { printsrctotgt(*out,m,a,n,b); sents++; @@ -511,9 +509,7 @@ int main(int argc, char** argv) if (out != &std::cout) { delete inp; } - } - catch (const std::exception &e) - { + } catch (const std::exception &e) { cerr << e.what() << std::endl; exit(1); }