【代碼】key-value模式下的哈希二次探測與簡單的哈希類的實現

    二次探測是避免哈希衝突的一種常見手段,思想是:
ide

    插入:
blog

    找到哈希位置(serch)->若是不衝突就插入,衝突就進行第一次探測ci

    第1次探測:
get

    哈希位置變爲原有哈希位置加上1*1的偏移->進行插入
string

    ....
it

    ....
io

    第i次探測:table

    哈希位置變爲原有哈希位置加上i*i的偏移->進行插入class

    知道插入完成爲止。im

如圖所示:

wKioL1cy0tmC56kiAAAhs-x0KiE606.png  

    哈希類代碼以下:

#pragma once
#include<vector>
#include<string>
enum Status
{
	DELETE,
	EMPTY,
	EXIST,
};
template<class K, class V>
struct KV
{
	KV()
	{}
	KV(K _key, V _value)
	:key(_key)
	, value(_value)
	{}
	Status s;
	K key;
	V value;
};
template<class K>
struct DefaultHash
{
	size_t operator()(const K& k)
	{
		return k;
	}
};
template<>
struct DefaultHash<string>
{
	size_t operator()(const string&k)
	{
		size_t ret = 0;
		for (size_t i = 0; i < k.size(); ++i)
		{
			ret *= 10;
			ret += k[i];
		}
		return ret;
	}
};
template<class K,class V,
class Hash = DefaultHash<K> >
class HashTable
{
public:
	HashTable(size_t capacity)
		:tables(new KV<K,V>[capacity])
		,_size(0)
		, _capacity(capacity)
	{
		for (size_t i = 0; i < _capacity; ++i)
		{
			tables[i].s = EMPTY;
		}
	}
	HashTable()
		:_size(0)
		, _capacity(0)
	{}
	void Push(const KV<K,V> &x)
	{
		size_t index = search(x.key);
		tables[index].s = EXIST;
		tables[index].key = x.key;
		tables[index].value = x.value;
		++_size;
	}
	void Print()
	{
		for (size_t i = 0; i < _capacity; ++i)
		{
			cout << i << ":";
			if (tables[i].s == EXIST)
				cout <<"["<<tables[i].key<<","<< tables[i].value<<"]";
			else
				cout << "NULL";
			cout << endl;
		}
		cout << endl;
	}
	int Find(const K& key)
	{
		Hash Search;
		int index = Search(key) % _capacity;
		size_t i = 0;
		while (tables[index].s != EMPTY)
		{
			if (tables[index].key == key&&tables[index].s == EXIST)
				return index;
			i++;
			index += 2*i-1;
			if (index >= _capacity)
				index %= _capacity;
		}
		return -1;
	}
	void Pop(const K& key)
	{
		int index = Find(key);
		if (index > 0)
		{
			tables[index].s = DELETE;
			_size--;
		}
	}
	~HashTable()
	{
		if (tables)
			delete[]tables;
		_size = 0;
		_capacity = 0;
	}
protected:
	size_t search(K key)
	{
		if (_size * 2 >= _capacity)
		{
			HashTable tmp(_capacity * 2 + 10);
			for (size_t i = 0; i < _capacity; ++i)
			{
				if (tables[i].s == EXIST)
					tmp.Push(tables[i]);
			}
			std::swap(tables, tmp.tables);
			std::swap(_size, tmp._size);
			std::swap(_capacity, tmp._capacity);
		}
		Hash Search;
		size_t index = Search(key)%_capacity;
		size_t i = 0;
		while (tables[index].s == EXIST)
		{
			i++;
			index += i*i;
			if (index >= _capacity)
				index %= _capacity;
		}
		return index;
	}
protected:
	KV<K,V>* tables;
	size_t _size;
	size_t _capacity;
};

    若有疑問但願提出,有不足也但願指正

相關文章
相關標籤/搜索