c++实现hashtable缓存

编程实例:

#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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值