Browse Source

CPP: Use map instead of deprecated hash_map when unordered_map is not available.

pull/567/head
Philippe Liard 15 years ago
committed by Mihaela Rosca
parent
commit
d4648520d2
3 changed files with 15 additions and 42 deletions
  1. +6
    -11
      cpp/CMakeLists.txt
  2. +6
    -23
      cpp/src/phonenumbers/regexp_cache.cc
  3. +3
    -8
      cpp/src/phonenumbers/regexp_cache.h

+ 6
- 11
cpp/CMakeLists.txt View File

@ -67,6 +67,7 @@ endfunction (find_required_program)
# Options that can be passed to CMake using 'cmake -DKEY=VALUE'.
option ("USE_LITE_METADATA" "Use lite metadata" "OFF")
option ("USE_RE2" "Use RE2 instead of ICU" "OFF")
option ("USE_STD_MAP" "Force the use of std::map" "OFF")
# Find all the required libraries and programs.
find_package (Boost 1.40.0 COMPONENTS thread)
@ -109,17 +110,11 @@ if (APPLE)
FIND_LIBRARY (FOUNDATION_LIB Foundation)
endif ()
INCLUDE (CheckIncludeFileCXX)
CHECK_INCLUDE_FILE_CXX ("tr1/unordered_map" HAVE_CXX_TR1_UNORDERED_MAP)
if (HAVE_CXX_TR1_UNORDERED_MAP)
add_definitions ("-DUSE_TR1_UNORDERED_MAP")
else ()
CHECK_INCLUDE_FILE_CXX (hash_map HAVE_CXX_HASH_MAP)
if (HAVE_CXX_HASH_MAP)
add_definitions ("-DUSE_HASH_MAP")
else ()
print_error ("C++ map class" "tr1/unordered_map or hash_map")
if (${USE_STD_MAP} STREQUAL "OFF")
INCLUDE (CheckIncludeFileCXX)
CHECK_INCLUDE_FILE_CXX ("tr1/unordered_map" HAVE_CXX_TR1_UNORDERED_MAP)
if (HAVE_CXX_TR1_UNORDERED_MAP)
add_definitions ("-DUSE_TR1_UNORDERED_MAP")
endif ()
endif ()


+ 6
- 23
cpp/src/phonenumbers/regexp_cache.cc View File

@ -26,35 +26,18 @@
using std::string;
#ifdef USE_HASH_MAP
// A basic text book string hash function implementation, this one taken from
// The Practice of Programming (Kernighan and Pike 1999). It could be a good
// idea in the future to evaluate how well it actually performs and possibly
// switch to another hash function better suited to this particular use case.
namespace __gnu_cxx {
template<> struct hash<string> {
enum { MULTIPLIER = 31 };
size_t operator()(const string& key) const {
size_t h = 0;
for (const char* p = key.c_str(); *p != '\0'; ++p) {
h *= MULTIPLIER;
h += *p;
}
return h;
}
};
} // namespace __gnu_cxx
#endif
namespace i18n {
namespace phonenumbers {
using base::AutoLock;
RegExpCache::RegExpCache(size_t min_items)
: cache_impl_(new CacheImpl(min_items)) {}
#ifdef USE_TR1_UNORDERED_MAP
: cache_impl_(new CacheImpl(min_items))
#else
: cache_impl_(new CacheImpl())
#endif
{}
RegExpCache::~RegExpCache() {
AutoLock l(lock_);


+ 3
- 8
cpp/src/phonenumbers/regexp_cache.h View File

@ -36,13 +36,8 @@
#ifdef USE_TR1_UNORDERED_MAP
# include <tr1/unordered_map>
#elif defined(USE_HASH_MAP)
# ifndef __DEPRECATED
# define __DEPRECATED
# endif
# include <hash_map>
#else
# error STL map type unsupported on this platform!
# include <map>
#endif
namespace i18n {
@ -56,8 +51,8 @@ class RegExpCache {
private:
#ifdef USE_TR1_UNORDERED_MAP
typedef std::tr1::unordered_map<string, const RegExp*> CacheImpl;
#elif defined(USE_HASH_MAP)
typedef std::hash_map<string, const RegExp*> CacheImpl;
#else
typedef std::map<string, const RegExp*> CacheImpl;
#endif
public:


Loading…
Cancel
Save