牵着老婆满街逛

严以律己,宽以待人. 三思而后行.
GMail/GTalk: yanglinbo#google.com;
MSN/Email: tx7do#yahoo.com.cn;
QQ: 3 0 3 3 9 6 9 2 0 .

关于Google的SparseHashMap

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

}
;

posted on 2013-04-22 17:43 杨粼波 阅读(7456) 评论(4)  编辑 收藏 引用 所属分类: C++

评论

# re: 关于Google的SparseHashMap 2013-04-24 11:04 工口君

2货啊,稀疏容器和普通容器应用方向不同啊   回复  更多评论   

# re: 关于Google的SparseHashMap[未登录] 2013-04-24 12:28 杨粼波

@工口君
嘿嘿,随便用,胡乱用,哇卡卡卡.  回复  更多评论   

# re: 关于Google的SparseHashMap 2015-02-27 12:14 viki627

您好呀 我是菜鸟一只 我想问一下这个dense_hash_map是直接吧google的库函数丢到vc的include文件下就能使用的么?在我#include<google/dense_hash_map>成功之后using GOOGLE_NAMESPACE::dense_hash_map;不能成功 显示命名空间内内有dense_hash_map..您有时间的话能麻烦回下我嘛。。万分感谢!!!!!  回复  更多评论   

# re: 关于Google的SparseHashMap 2015-12-18 15:14 问问

在codeblocks里如何使用Google sparse hash 啊 菜鸟一只,求指导  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理