diff options
author | Jisi Liu <liujisi@google.com> | 2016-05-12 10:34:40 -0700 |
---|---|---|
committer | Jisi Liu <liujisi@google.com> | 2016-05-12 10:34:40 -0700 |
commit | 9f841148643deae5380c80229277089f3f3093fb (patch) | |
tree | 02d564bb0483fb4a0d3a1a1f08834b1b67fdc2a2 /src | |
parent | f9fd4509f60d9fb1cf02b67db6d1099d9d7fd8ab (diff) | |
parent | dbdf6d9085297d28d55caf7a35b37d9c2b624f2c (diff) | |
download | protobuf-9f841148643deae5380c80229277089f3f3093fb.tar.gz protobuf-9f841148643deae5380c80229277089f3f3093fb.tar.bz2 protobuf-9f841148643deae5380c80229277089f3f3093fb.zip |
Merge pull request #1520 from pherl/hashmapvs2008
Bridge vs2008 hashmaps.
Diffstat (limited to 'src')
-rw-r--r--[-rwxr-xr-x] | src/google/protobuf/stubs/hash.h | 58 |
1 files changed, 55 insertions, 3 deletions
diff --git a/src/google/protobuf/stubs/hash.h b/src/google/protobuf/stubs/hash.h index bbd8ee65..4eac7d5d 100755..100644 --- a/src/google/protobuf/stubs/hash.h +++ b/src/google/protobuf/stubs/hash.h @@ -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> @@ -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>, @@ -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> { |