问题描述:
1.blizard hash algorithm 是众所周知的算法,关于它极小的碰撞概率和实现的简洁性一直为热爱技术的朋友津津乐道;
2.blizard hash algorithm 有个致命的问题就是它的实现受制于一个固定的(预先开辟的buffer)的限制,暴雪给出的是1024,也即当hash table 的填充的元素(key value pair)查过1024时,就没办法再往里面进行key value 对填充,这极大的限制了它的使用。在实现的应用,我们经常会向hash table里百万,千万插入key, value对,所以有必要尝试改造扩展它的算法,使得其能动态扩展,以适用我们实现编程中的使用场景;
3.本文以Rehash方式为扩展策略对hash table按素数长度进行扩展;
4.本文使用c++语言重新封装这个算法,并和STL map进行了操作性能上的比较。执行相同的操作,结果是blizard hash所消耗时间为map的五分之一;
程序代码:
#ifndef _BLIZARD_HASH_H_
#define _BLIZARD_HASH_H_
#include <stdlib.h>
#include <stdio.h>
#include <map>
#include "windows.h"
static const int HashSize[] = {17, 37, 79, 163, 331,
673, 1361, 2729, 5471, 10949,
21911, 43853, 87719, 175447, 350899,
701819, 1403641, 2807303, 5614657, 11229331,
22458671, 44917381, 89834777, 179669557, 359339171,
718678369, 1437356741, 2147483647 };
/*
*
*
*
*/
template<class T>
class BlizardHash
{
public:
static int const INIT_HASH_SIZE = 8;
typedef struct tagHashNode
{
unsigned int hashFirst;
unsigned int hashSecond;
char* key;
T value;
tagHashNode():hashFirst(0), hashSecond(0),
key(0), value()
{
}
tagHashNode( const char* _key, const T& _value ): hashFirst(0),
hashSecond(0), value(_value)
{
SetKey( _key );
}
tagHashNode( unsigned int hash1, unsigned int hash2,const char* _key, const T& _value ):
hashFirst(hash1),
hashSecond(hash2),
value(_value)
{
SetKey( _key );
}
void SetKey( const char* _key )
{
size_t len = strl