From 2a4e5fbb838517d03c760974ccc6a2ff8939dd08 Mon Sep 17 00:00:00 2001 From: "pinkerton@google.com" Date: Mon, 4 Aug 2008 18:36:53 +0000 Subject: 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 --- base/hash_tables.h | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 66 insertions(+), 5 deletions(-) (limited to 'base') 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 my_map; +// base::hash_set my_set; // #ifndef BASE_HASH_TABLES_H__ @@ -38,15 +44,70 @@ #include #include namespace base { - using stdext::hash_map; - using stdext::hash_set; +using stdext::hash_map; +using stdext::hash_set; } #else #include #include 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 { + size_t operator()(const wchar_t* s) const { + return stl_hash_wstring(s); + } +}; + +template<> +struct hash { + size_t operator()(const wchar_t* s) const { + return stl_hash_wstring(s); + } +}; + +template<> +struct hash { + size_t operator()(const std::wstring& s) const { + return stl_hash_wstring(s.c_str()); + } +}; + +template<> +struct hash { + size_t operator()(const std::wstring& s) const { + return stl_hash_wstring(s.c_str()); + } +}; + +template<> +struct hash { + size_t operator()(const std::string& s) const { + return __stl_hash_string(s.c_str()); + } +}; + +template<> +struct hash { + size_t operator()(const std::string& s) const { + return __stl_hash_string(s.c_str()); + } +}; + } #endif -- cgit v1.1