Skip to content

Commit

Permalink
Merge pull request protocolbuffers#1520 from pherl/hashmapvs2008
Browse files Browse the repository at this point in the history
Bridge vs2008 hashmaps.
  • Loading branch information
liujisi committed May 12, 2016
2 parents f9fd450 + dbdf6d9 commit 9f84114
Showing 1 changed file with 55 additions and 3 deletions.
58 changes: 55 additions & 3 deletions src/google/protobuf/stubs/hash.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,13 @@
# define GOOGLE_PROTOBUF_HAS_CXX11_HASH
# define GOOGLE_PROTOBUF_HASH_COMPARE std::hash_compare
# elif _MSC_VER >= 1500 // Since Visual Studio 2008
# undef GOOGLE_PROTOBUF_HAVE_HASH_MAP
# undef GOOGLE_PROTOBUF_HAVE_HASH_SET
# define GOOGLE_PROTOBUF_HASH_NAMESPACE stdext
# include <hash_map>
# define GOOGLE_PROTOBUF_HASH_MAP_CLASS hash_map
# include <hash_set>
# define GOOGLE_PROTOBUF_HASH_SET_CLASS hash_set
# define GOOGLE_PROTOBUF_HASH_COMPARE stdext::hash_compare
# define GOOGLE_PROTOBUF_CONTAINERS_NEED_HASH_COMPARE
# elif _MSC_VER >= 1310
# define GOOGLE_PROTOBUF_HASH_NAMESPACE stdext
# include <hash_map>
Expand Down Expand Up @@ -247,6 +252,52 @@ template <>
struct hash<const char*>
: public GOOGLE_PROTOBUF_HASH_COMPARE<const char*, CstringLess> {};

#ifdef GOOGLE_PROTOBUF_CONTAINERS_NEED_HASH_COMPARE

template <typename Key, typename HashFcn, typename EqualKey>
struct InternalHashCompare : public GOOGLE_PROTOBUF_HASH_COMPARE<Key> {
InternalHashCompare() {}
InternalHashCompare(HashFcn hashfcn, EqualKey equalkey)
: hashfcn_(hashfcn), equalkey_(equalkey) {}
size_t operator()(const Key& key) const { return hashfcn_(key); }
bool operator()(const Key& key1, const Key& key2) const {
return !equalkey_(key1, key2);
}
HashFcn hashfcn_;
EqualKey equalkey_;
};

template <typename Key, typename Data,
typename HashFcn = hash<Key>,
typename EqualKey = std::equal_to<Key>,
typename Alloc = std::allocator< std::pair<const Key, Data> > >
class hash_map
: public GOOGLE_PROTOBUF_HASH_NAMESPACE::GOOGLE_PROTOBUF_HASH_MAP_CLASS<
Key, Data, InternalHashCompare<Key, HashFcn, EqualKey>, Alloc> {
typedef GOOGLE_PROTOBUF_HASH_NAMESPACE::GOOGLE_PROTOBUF_HASH_MAP_CLASS<
Key, Data, InternalHashCompare<Key, HashFcn, EqualKey>, Alloc> BaseClass;

public:
hash_map(int a = 0, const HashFcn& b = HashFcn(),
const EqualKey& c = EqualKey(), const Alloc& d = Alloc())
: BaseClass(InternalHashCompare<Key, HashFcn, EqualKey>(b, c), d) {}

HashFcn hash_function() const { return HashFcn(); }
};

template <typename Key, typename HashFcn = hash<Key>,
typename EqualKey = std::equal_to<Key> >
class hash_set
: public GOOGLE_PROTOBUF_HASH_NAMESPACE::GOOGLE_PROTOBUF_HASH_SET_CLASS<
Key, InternalHashCompare<Key, HashFcn, EqualKey> > {
public:
hash_set(int = 0) {}

HashFcn hash_function() const { return HashFcn(); }
};

#else // GOOGLE_PROTOBUF_CONTAINERS_NEED_HASH_COMPARE

template <typename Key, typename Data,
typename HashFcn = hash<Key>,
typename EqualKey = std::equal_to<Key>,
Expand Down Expand Up @@ -275,8 +326,9 @@ class hash_set

HashFcn hash_function() const { return HashFcn(); }
};
#endif // GOOGLE_PROTOBUF_CONTAINERS_NEED_HASH_COMPARE

#else
#else // defined(_MSC_VER) && !defined(_STLPORT_VERSION)

template <typename Key>
struct hash : public GOOGLE_PROTOBUF_HASH_NAMESPACE::hash<Key> {
Expand Down

0 comments on commit 9f84114

Please sign in to comment.