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