diff options
author | pinkerton@google.com <pinkerton@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-08-04 18:36:53 +0000 |
---|---|---|
committer | pinkerton@google.com <pinkerton@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-08-04 18:36:53 +0000 |
commit | 2a4e5fbb838517d03c760974ccc6a2ff8939dd08 (patch) | |
tree | 34967adb03a0305fb5d4cf64a8974067d155a768 /base | |
parent | 372d63e5389ff61ac6043302a14711497be2999f (diff) | |
download | chromium_src-2a4e5fbb838517d03c760974ccc6a2ff8939dd08.zip chromium_src-2a4e5fbb838517d03c760974ccc6a2ff8939dd08.tar.gz chromium_src-2a4e5fbb838517d03c760974ccc6a2ff8939dd08.tar.bz2 |
Adding specializations of hash() for various string types. GCC requires you bring your own hash function for maps and sets.
Review URL: http://chrome-reviews.prom.corp.google.com/1085
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@327 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/hash_tables.h | 71 |
1 files changed, 66 insertions, 5 deletions
diff --git a/base/hash_tables.h b/base/hash_tables.h index 13c201b..10539d7 100644 --- a/base/hash_tables.h +++ b/base/hash_tables.h @@ -27,8 +27,14 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // + +// // Deal with the differences between Microsoft and GNU implemenations -// of hash_map +// of hash_map. Allows all platforms to use |base::hash_map| and +// |base::hash_set|. +// eg: +// base::hash_map<int> my_map; +// base::hash_set<int> my_set; // #ifndef BASE_HASH_TABLES_H__ @@ -38,15 +44,70 @@ #include <hash_map> #include <hash_set> namespace base { - using stdext::hash_map; - using stdext::hash_set; +using stdext::hash_map; +using stdext::hash_set; } #else #include <ext/hash_map> #include <ext/hash_set> namespace base { - using __gnu_cxx::hash_map; - using __gnu_cxx::hash_set; +using __gnu_cxx::hash_map; +using __gnu_cxx::hash_set; +} + +// Implement string hash functions so that strings of various flavors can +// be used as keys in STL maps and sets. +namespace __gnu_cxx { + +inline size_t stl_hash_wstring(const wchar_t* s) { + unsigned long h = 0; + for ( ; *s; ++s) + h = 5 * h + *s; + return size_t(h); +} + +template<> +struct hash<wchar_t*> { + size_t operator()(const wchar_t* s) const { + return stl_hash_wstring(s); + } +}; + +template<> +struct hash<const wchar_t*> { + size_t operator()(const wchar_t* s) const { + return stl_hash_wstring(s); + } +}; + +template<> +struct hash<std::wstring> { + size_t operator()(const std::wstring& s) const { + return stl_hash_wstring(s.c_str()); + } +}; + +template<> +struct hash<const std::wstring> { + size_t operator()(const std::wstring& s) const { + return stl_hash_wstring(s.c_str()); + } +}; + +template<> +struct hash<std::string> { + size_t operator()(const std::string& s) const { + return __stl_hash_string(s.c_str()); + } +}; + +template<> +struct hash<const std::string> { + size_t operator()(const std::string& s) const { + return __stl_hash_string(s.c_str()); + } +}; + } #endif |