Joshua
open source statistical hierarchical phrase-based machine translation system
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
src/kenlm/util/string_piece_hash.hh
00001 #ifndef UTIL_STRING_PIECE_HASH_H
00002 #define UTIL_STRING_PIECE_HASH_H
00003 
00004 #include "util/string_piece.hh"
00005 
00006 #include <boost/functional/hash.hpp>
00007 #include <boost/version.hpp>
00008 
00009 inline size_t hash_value(const StringPiece &str) {
00010   return boost::hash_range(str.data(), str.data() + str.length());
00011 }
00012 
00013 /* Support for lookup of StringPiece in boost::unordered_map<std::string> */
00014 struct StringPieceCompatibleHash : public std::unary_function<const StringPiece &, size_t> {
00015   size_t operator()(const StringPiece &str) const {
00016     return hash_value(str);
00017   }
00018 };
00019 
00020 struct StringPieceCompatibleEquals : public std::binary_function<const StringPiece &, const std::string &, bool> {
00021   bool operator()(const StringPiece &first, const StringPiece &second) const {
00022     return first == second;
00023   }
00024 };
00025 template <class T> typename T::const_iterator FindStringPiece(const T &t, const StringPiece &key) {
00026 #if BOOST_VERSION < 104200
00027   std::string temp(key.data(), key.size());
00028   return t.find(temp);
00029 #else
00030   return t.find(key, StringPieceCompatibleHash(), StringPieceCompatibleEquals());
00031 #endif
00032 }
00033 
00034 template <class T> typename T::iterator FindStringPiece(T &t, const StringPiece &key) {
00035 #if BOOST_VERSION < 104200
00036   std::string temp(key.data(), key.size());
00037   return t.find(temp);
00038 #else
00039   return t.find(key, StringPieceCompatibleHash(), StringPieceCompatibleEquals());
00040 #endif
00041 }
00042 
00043 #endif // UTIL_STRING_PIECE_HASH_H