diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index d9bac861c..ba3fa9026 100644 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -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 () diff --git a/cpp/src/phonenumbers/regexp_cache.cc b/cpp/src/phonenumbers/regexp_cache.cc index 1f130c769..7a0c9e3ac 100644 --- a/cpp/src/phonenumbers/regexp_cache.cc +++ b/cpp/src/phonenumbers/regexp_cache.cc @@ -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 { - 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_); diff --git a/cpp/src/phonenumbers/regexp_cache.h b/cpp/src/phonenumbers/regexp_cache.h index 7670824f7..08e19127d 100644 --- a/cpp/src/phonenumbers/regexp_cache.h +++ b/cpp/src/phonenumbers/regexp_cache.h @@ -36,13 +36,8 @@ #ifdef USE_TR1_UNORDERED_MAP # include -#elif defined(USE_HASH_MAP) -# ifndef __DEPRECATED -# define __DEPRECATED -# endif -# include #else -# error STL map type unsupported on this platform! +# include #endif namespace i18n { @@ -56,8 +51,8 @@ class RegExpCache { private: #ifdef USE_TR1_UNORDERED_MAP typedef std::tr1::unordered_map CacheImpl; -#elif defined(USE_HASH_MAP) - typedef std::hash_map CacheImpl; +#else + typedef std::map CacheImpl; #endif public: