官方网站:
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();
}
};