编程实例:
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <pthread.h>
using namespace std;
typedef unsigned int uint32_t;
uint32_t DecodeFixed32(const char* data)
{
uint32_t res;
memcpy(&res, data, sizeof(res));
return res;
}
uint32_t Hash(const char* data, uint32_t n, uint32_t seed=0)
{
const uint32_t m = 0xc6a4a793;
const uint32_t r = 24;
const char* limit = data + n;
uint32_t h = seed ^ (n * m);
// Pick up four bytes at a time
while (data + 4 <= limit) {
uint32_t w = DecodeFixed32(data);
data += 4;
h += w;
h *= m;
h ^= (h >> 16);
}
// Pick up remaining bytes
switch (limit - data) {
case 3:
h += data[2] << 16;
// fall through
case 2:
h += data[1] << 8;
// fall through
case 1:
h += data[0];
h *= m;
h ^= (h >> r);
break;
}
return h;
}
struct Node
{
void* value;
Node* nextList;
Node* next;
Node* pre;
uint32_t keyLen;
uint32_t hash;
char keydata[1];
uint32_t refs;
};
class HashTable
{
public:
HashTable():elems(0),length(0),list(NULL)
{
resize();
}
virtual ~HashTable()
{
if(list)
delete[] list;
}
Node* find(uint32_t hash, void * value)
{
return *findPoint