官方網站:
http://google-sparsehash.googlecode.com官方說,可以輕松的用dense_hash_map和sparse_hash_map替換掉hash_map或者unordered_map.
dense系列適用于"小字典",sparse系列適用于"大字典".
這貨挺好使的,性能上也是不錯的.
但是呢,有兩點比較傷腦筋,這和其他的hashmap不兼容,
那就是要在dense要在插入之前必須執行一次set_empty_key,dense和sparse要在刪除之前必須執行一次set_deleted_key.為了適配這個不兼容性,我整了這么個玩意:
#include <google/dense_hash_map>
#include <google/sparse_hash_map>
using GOOGLE_NAMESPACE::dense_hash_map;
using GOOGLE_NAMESPACE::sparse_hash_map;

template <typename Value, typename Key = std::string>
class TStringDenseHashMap : public dense_hash_map<Key, Value>


{
public:
TStringDenseHashMap()

{
set_empty_key( std::string("") );
set_deleted_key( std::string(" ") );
}
};
template <typename Value, typename Key = std::string>
class TStringSparseHashMap : public sparse_hash_map<Key, Value>


{
public:
TStringSparseHashMap()

{
set_deleted_key( (" ") );
}
};

namespace detail


{
struct eqstr

{
bool operator()(const char* s1, const char* s2) const

{
return (s1 == s2) || (s1 && s2 && strcmp(s1, s2) == 0);
}
};
}
template <typename Value, typename Key = const char*>
class TConstCharDenseHashMap : public dense_hash_map<Key, Value, stdext::hash_compare<Key>, detail::eqstr>


{
public:
TConstCharDenseHashMap()

{
set_empty_key( ("") );
set_deleted_key( (" ") );
}
};
template <typename Value, typename Key = const char*>
class TConstCharSparseHashMap : public sparse_hash_map<Key, Value, stdext::hash_compare<Key>, detail::eqstr>


{
public:
TConstCharSparseHashMap()

{
set_deleted_key( (" ") );
}
};嗯,這樣就可以很好的兼容了.
雖然.....很惡心.
以下附上測試代碼:
void testGoogleHashMap()


{

{
typedef TStringDenseHashMap<std::string> StringDenseHashMap;
StringDenseHashMap map1;
size_t nSize = 0;

map1["張三"] = "張三";
map1["李四"] = "李四";
map1["王五"] = "王五";
nSize = map1.size();

StringDenseHashMap::iterator iter = map1.find("張三");
std::string strResult;
if (iter != map1.end())

{
strResult = iter->second;
}
std::cout << map1["張三"] << "," << map1["李四"] << "," << map1["王五"] << std::endl;

map1.erase("張三");
map1.erase("李四");
map1.erase("王五");
nSize = map1.size();
}


{
typedef TStringSparseHashMap<std::string> StringSparseHashMap;
StringSparseHashMap map1;
size_t nSize = 0;

map1["張三"] = "張三";
map1["李四"] = "李四";
map1["王五"] = "王五";
nSize = map1.size();

StringSparseHashMap::iterator iter = map1.find("張三");
std::string strResult;
if (iter != map1.end())

{
strResult = iter->second;
}
std::cout << map1["張三"] << "," << map1["李四"] << "," << map1["王五"] << std::endl;

map1.erase("張三");
map1.erase("李四");
map1.erase("王五");
nSize = map1.size();
}


{
typedef TConstCharDenseHashMap<const char*> ConstCharDenseHashMap;
ConstCharDenseHashMap map1;
size_t nSize = 0;

map1["張三"] = "張三";
map1["李四"] = "李四";
map1["王五"] = "王五";
nSize = map1.size();

ConstCharDenseHashMap::iterator iter = map1.find("張三");
std::string strResult;
if (iter != map1.end())

{
strResult = iter->second;
}
std::cout << map1["張三"] << "," << map1["李四"] << "," << map1["王五"] << std::endl;

map1.erase("張三");
map1.erase("李四");
map1.erase("王五");
nSize = map1.size();
}


{
typedef TConstCharSparseHashMap<std::string> ConstCharSparseHashMap;
ConstCharSparseHashMap map1;
size_t nSize = 0;

map1["張三"] = "張三";
map1["李四"] = "李四";
map1["王五"] = "王五";
nSize = map1.size();

ConstCharSparseHashMap::iterator iter = map1.find("張三");
std::string strResult;
if (iter != map1.end())

{
strResult = iter->second;
}
std::cout << map1["張三"] << "," << map1["李四"] << "," << map1["王五"] << std::endl;

map1.erase("張三");
map1.erase("李四");
map1.erase("王五");
nSize = map1.size();
}
};