ipc/chromium/src/base/hash_tables.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
michael@0 2 // Use of this source code is governed by a BSD-style license that can be
michael@0 3 // found in the LICENSE file.
michael@0 4 //
michael@0 5
michael@0 6 //
michael@0 7 // Deal with the differences between Microsoft and GNU implemenations
michael@0 8 // of hash_map. Allows all platforms to use |base::hash_map| and
michael@0 9 // |base::hash_set|.
michael@0 10 // eg:
michael@0 11 // base::hash_map<int> my_map;
michael@0 12 // base::hash_set<int> my_set;
michael@0 13 //
michael@0 14
michael@0 15 #ifndef BASE_HASH_TABLES_H_
michael@0 16 #define BASE_HASH_TABLES_H_
michael@0 17
michael@0 18 #include "build/build_config.h"
michael@0 19
michael@0 20 #include "base/string16.h"
michael@0 21
michael@0 22 #if defined(COMPILER_MSVC) || (defined(ANDROID) && defined(_STLP_STD_NAME))
michael@0 23 #include <hash_map>
michael@0 24 #include <hash_set>
michael@0 25 namespace base {
michael@0 26 #ifdef ANDROID
michael@0 27 using _STLP_STD_NAME::hash_map;
michael@0 28 using _STLP_STD_NAME::hash_set;
michael@0 29 #else
michael@0 30 using stdext::hash_map;
michael@0 31 using stdext::hash_set;
michael@0 32 #endif
michael@0 33 }
michael@0 34 #elif defined(COMPILER_GCC)
michael@0 35 // This is a hack to disable the gcc 4.4 warning about hash_map and hash_set
michael@0 36 // being deprecated. We can get rid of this when we upgrade to VS2008 and we
michael@0 37 // can use <tr1/unordered_map> and <tr1/unordered_set>.
michael@0 38 #ifdef __DEPRECATED
michael@0 39 #define CHROME_OLD__DEPRECATED __DEPRECATED
michael@0 40 #undef __DEPRECATED
michael@0 41 #endif
michael@0 42
michael@0 43 #include <ext/hash_map>
michael@0 44 #include <ext/hash_set>
michael@0 45 #include <string>
michael@0 46
michael@0 47 #ifdef CHROME_OLD__DEPRECATED
michael@0 48 #define __DEPRECATED CHROME_OLD__DEPRECATED
michael@0 49 #undef CHROME_OLD__DEPRECATED
michael@0 50 #endif
michael@0 51
michael@0 52 namespace base {
michael@0 53 using __gnu_cxx::hash_map;
michael@0 54 using __gnu_cxx::hash_set;
michael@0 55 } // namespace base
michael@0 56
michael@0 57 namespace __gnu_cxx {
michael@0 58
michael@0 59 // The GNU C++ library provides identiy hash functions for many integral types,
michael@0 60 // but not for |long long|. This hash function will truncate if |size_t| is
michael@0 61 // narrower than |long long|. This is probably good enough for what we will
michael@0 62 // use it for.
michael@0 63
michael@0 64 #define DEFINE_TRIVIAL_HASH(integral_type) \
michael@0 65 template<> \
michael@0 66 struct hash<integral_type> { \
michael@0 67 std::size_t operator()(integral_type value) const { \
michael@0 68 return static_cast<std::size_t>(value); \
michael@0 69 } \
michael@0 70 }
michael@0 71
michael@0 72 DEFINE_TRIVIAL_HASH(long long);
michael@0 73 DEFINE_TRIVIAL_HASH(unsigned long long);
michael@0 74
michael@0 75 #undef DEFINE_TRIVIAL_HASH
michael@0 76
michael@0 77 // Implement string hash functions so that strings of various flavors can
michael@0 78 // be used as keys in STL maps and sets. The hash algorithm comes from the
michael@0 79 // GNU C++ library, in <tr1/functional>. It is duplicated here because GCC
michael@0 80 // versions prior to 4.3.2 are unable to compile <tr1/functional> when RTTI
michael@0 81 // is disabled, as it is in our build.
michael@0 82
michael@0 83 #define DEFINE_STRING_HASH(string_type) \
michael@0 84 template<> \
michael@0 85 struct hash<string_type> { \
michael@0 86 std::size_t operator()(const string_type& s) const { \
michael@0 87 std::size_t result = 0; \
michael@0 88 for (string_type::const_iterator i = s.begin(); i != s.end(); ++i) \
michael@0 89 result = (result * 131) + *i; \
michael@0 90 return result; \
michael@0 91 } \
michael@0 92 }
michael@0 93
michael@0 94 DEFINE_STRING_HASH(std::string);
michael@0 95 DEFINE_STRING_HASH(std::wstring);
michael@0 96
michael@0 97 #if defined(WCHAR_T_IS_UTF32)
michael@0 98 // If string16 and std::wstring are not the same type, provide a
michael@0 99 // specialization for string16.
michael@0 100 DEFINE_STRING_HASH(string16);
michael@0 101 #endif // WCHAR_T_IS_UTF32
michael@0 102
michael@0 103 #undef DEFINE_STRING_HASH
michael@0 104
michael@0 105 } // namespace __gnu_cxx
michael@0 106
michael@0 107 #endif // COMPILER
michael@0 108
michael@0 109 #endif // BASE_HASH_TABLES_H_

mercurial