00001 /*********************************************************************** 00002 Moses - statistical machine translation system 00003 Copyright (C) 2006-2012 University of Edinburgh 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Lesser General Public 00007 License as published by the Free Software Foundation; either 00008 version 2.1 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Lesser General Public License for more details. 00014 00015 You should have received a copy of the GNU Lesser General Public 00016 License along with this library; if not, write to the Free Software 00017 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00018 ***********************************************************************/ 00019 00020 #pragma once 00021 00022 #include "TypeDef.h" 00023 #include "Word.h" 00024 #include "Factor.h" 00025 00026 #include <boost/functional/hash.hpp> 00027 00028 namespace Moses 00029 { 00030 00031 class TerminalHasher 00032 { 00033 public: 00034 // Generate a hash value for a word representing a terminal. It's 00035 // assumed that the same subset of factors will be active for all words 00036 // that are hashed. 00037 size_t operator()(const Word &t) const { 00038 size_t seed = 0; 00039 for (size_t i = 0; i < MAX_NUM_FACTORS; ++i) { 00040 const Factor *f = t[i]; 00041 if (f) { 00042 boost::hash_combine(seed, *f); 00043 } 00044 } 00045 return seed; 00046 } 00047 }; 00048 00049 class TerminalEqualityPred 00050 { 00051 public: 00052 // Equality predicate for comparing words representing terminals. As 00053 // with the hasher, it's assumed that all words will have the same 00054 // subset of active factors. 00055 bool operator()(const Word &t1, const Word &t2) const { 00056 for (size_t i = 0; i < MAX_NUM_FACTORS; ++i) { 00057 const Factor *f1 = t1[i]; 00058 const Factor *f2 = t2[i]; 00059 if (f1 && f1->Compare(*f2)) { 00060 return false; 00061 } 00062 } 00063 return true; 00064 } 00065 }; 00066 00067 } // namespace Moses