基于字符串的hash算法

来源:互联网 发布:php微信公众号开发 编辑:程序博客网 时间:2024/05/22 10:44

 

truct hash_map_func
{
    
enum
    
{    // parameters for hash table
        bucket_size = 2,    // 0 < bucket_size
        min_buckets = 8
    }
;    // min_buckets = 2 ^^ N, 0 < N
    size_t operator()(const string& str)const
    
{
        
const char* p =str.c_str();
        unsigned 
long hash = 5381;
        
int c;
        
while (c = *p++)
            hash 
= ((hash << 5+ hash) + c; /* hash * 33 + c */
        
return hash;
    }

    
bool operator()(const string& _Keyval1, const string& _Keyval2) const
    
{    // test if _Keyval1 ordered before _Keyval2
        return (gt(_Keyval1, _Keyval2));
    }

    greater
<string> gt;
}
;

 

原创粉丝点击