Hash 函数
key -> size_t( 就是 int ) 映射
Hash函数设计得越集中就越好,因为空桶越少. 这与key是否是线性集中关系不大, 关键是它映射之后的 int values 要集中而又不冲突
为何用pool
对于有很多的小对象而言, New开销很大 ,可以用 boost 提供的 pool解决
String 做key
gnu 的 extension 提供函数了
__gnu_cxx::__stl_hash_string
参见下面代码
#include <string>
using std::string;
using namespace __gnu_cxx;
struct str_hash{
size_t operator()(const string& s) const {
return __stl_hash_string(s.c_str());
}
};
struct str_compare{
int operator()(const string& a,const string& b) const {
return (a==b);
}
};
#include <ext/hash_map>
hash_map<string,int,str_hash,str_compare,boost::pool_allocator<Node>>
减少 rehash
可以根据Node总数,先对Hash_map 进行resize(), 从而减少 rehash