2013-01-23 00:23:35 +04:00
|
|
|
#ifndef UTIL_STRING_PIECE_HASH__
|
|
|
|
#define UTIL_STRING_PIECE_HASH__
|
|
|
|
|
|
|
|
#include "util/string_piece.hh"
|
|
|
|
|
2013-05-19 18:22:50 +04:00
|
|
|
#include <set>
|
|
|
|
|
2013-01-23 00:23:35 +04:00
|
|
|
#include <boost/functional/hash.hpp>
|
|
|
|
#include <boost/version.hpp>
|
|
|
|
|
|
|
|
inline size_t hash_value(const StringPiece &str) {
|
|
|
|
return boost::hash_range(str.data(), str.data() + str.length());
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Support for lookup of StringPiece in boost::unordered_map<std::string> */
|
|
|
|
struct StringPieceCompatibleHash : public std::unary_function<const StringPiece &, size_t> {
|
|
|
|
size_t operator()(const StringPiece &str) const {
|
|
|
|
return hash_value(str);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct StringPieceCompatibleEquals : public std::binary_function<const StringPiece &, const std::string &, bool> {
|
|
|
|
bool operator()(const StringPiece &first, const StringPiece &second) const {
|
|
|
|
return first == second;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template <class T> typename T::const_iterator FindStringPiece(const T &t, const StringPiece &key) {
|
|
|
|
#if BOOST_VERSION < 104200
|
|
|
|
std::string temp(key.data(), key.size());
|
|
|
|
return t.find(temp);
|
|
|
|
#else
|
2013-04-29 21:46:48 +04:00
|
|
|
std::string temp(key.data(), key.size());
|
|
|
|
return t.find(temp);
|
|
|
|
//return t.find(key, StringPieceCompatibleHash(), StringPieceCompatibleEquals());
|
2013-01-23 00:23:35 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T> typename T::iterator FindStringPiece(T &t, const StringPiece &key) {
|
|
|
|
#if BOOST_VERSION < 104200
|
|
|
|
std::string temp(key.data(), key.size());
|
|
|
|
return t.find(temp);
|
|
|
|
#else
|
|
|
|
return t.find(key, StringPieceCompatibleHash(), StringPieceCompatibleEquals());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-05-19 18:22:50 +04:00
|
|
|
// Horribly inefficient versions because Hieu undid my changes.
|
|
|
|
template <class Entry, class Compare, class Alloc> typename std::set<Entry, Compare, Alloc>::const_iterator FindStringPiece(const std::set<Entry, Compare, Alloc> &t, const StringPiece &key) {
|
|
|
|
std::string temp(key.data(), key.size());
|
|
|
|
return t.find(temp);
|
|
|
|
}
|
|
|
|
template <class Entry, class Compare, class Alloc> typename std::set<Entry, Compare, Alloc>::iterator FindStringPiece(std::set<Entry, Compare, Alloc> &t, const StringPiece &key) {
|
|
|
|
std::string temp(key.data(), key.size());
|
|
|
|
return t.find(temp);
|
|
|
|
}
|
|
|
|
|
2013-01-23 00:23:35 +04:00
|
|
|
#endif // UTIL_STRING_PIECE_HASH__
|