哈希表__C++泛型实现简易的哈希表

哈希表__C++泛型实现简易的哈希表

1.简易的哈希表支持Value的泛型, Key值在这里只能用int型,如果要使用其他类型作为Key还需要定义hashCode函数,获取从其他类型映射到int型的哈希函数。这里的扩容机制采用的是负载因子超过限制或者桶中元素超过桶数,则扩容到二倍Size附近的质数,质数可以减少哈希碰撞。下面是简单的原理实现。

template<typename K, typename V>
struct Pair {
	Pair(const K& k, const V& v) : k_(k), v_(v) {}

	K k_;
	V v_;
};

template<typename K, typename V>
class unorderedMap {
public:
	unorderedMap(int sz = 3, double loadFactor = 0.75) : loadFactor_(loadFactor), useBucketNum_(0), size_(0) {
		hashMap_.resize(sz);
	}

	void insert(Pair<K, V> pair) {
		double loadFactor = useBucketNum_ * 1.0 / hashMap_.size();
		if (loadFactor > loadFactor_ ) expand();

		int idx = pair.k_ % hashMap_.size();

		list<Pair<K, V>>& bucket = hashMap_[idx];

		if (bucket.empty()) {
			useBucketNum_++;
		}
		else {
			if (bucket.size() >= hashMap_.size()) expand();

			for (auto it = bucket.begin(); it != bucket.end(); it++) {
				if (it->k_ == pair.k_) {
					it->v_ = pair.v_;
					return;
				}
			}
		}

		hashMap_[idx].push_back(pair);
		++size_;

		return;
	}

	void erase(const K& k) {
		int idx = k % hashMap_.size();

		list<Pair<K, V>>& bucket = hashMap_[idx];

		if (!bucket.empty()) {
			for (auto it = bucket.begin(); it != bucket.end(); it++) {
				if (it->k_ == k) {
					bucket.erase(it);
					--size_;
					if (bucket.empty()) useBucketNum_--;
					return;
				}
			}
		}

		return;
	}

	Pair<K, V>* find(const K& k) {
		int idx = k % hashMap_.size();

		list<Pair<K, V>>& bucket = hashMap_[idx];

		if (!bucket.empty()) {
			for (auto it = bucket.begin(); it != bucket.end(); it++) {
				if (it->k_ == k) {
					return &(*it);
				}
			}
		}

		return nullptr;
	}

	V& operator[](const K& k) {
		int idx = k % hashMap_.size();

		list<Pair<K, V>>& bucket = hashMap_[idx];

		if (!bucket.empty()) {
			for (auto it = bucket.begin(); it != bucket.end(); it++) {
				if (it->k_ == k) {
					return it->v_;
				}
			}
		}
		else {
			useBucketNum_++;
			insert({k, V()});
			++size_;
		}

		return bucket.back().v_;
	}

	int size() {
		return size_;
	}

private:
	vector<list<Pair<K, V>>> hashMap_;
	int useBucketNum_ = 0;
	double loadFactor_ = 1;
	int size_ = 0;

	bool isPrime(int num) {
		if (num % 6 != 1 && num % 6 != 5)  return false;

		int sq = sqrt(num);
		for (int i = 5; i <= sq; i += 6) {
			if (num % i == 0 || num % (i + 2) == 0) return false;
		}

		return  true;
	}

	void expand() {
		cout << "start expand!" << endl;
		int n = hashMap_.size();

		int prime = 2 * n + 1;
		while (!isPrime(prime)) prime++;

		vector<list<Pair<K, V>>> oldHashMap;
		oldHashMap.swap(hashMap_);

		useBucketNum_ = 0;
		hashMap_.resize(prime);

		for (list<Pair<K, V>>& ls : oldHashMap) {
			while (!ls.empty()) {
				int idx = ls.front().k_ % hashMap_.size();
				list<Pair<K, V>>& newList = hashMap_[idx];
				if (newList.empty()) useBucketNum_++;

				newList.splice(newList.begin(), ls, ls.begin());
			}
		}
	}
};

2.下面是简易的测试代码

int testUnorderedMap1() {
	unorderedMap<int, string> map;

	for (int i = 0; i <= 20; i++) {
		map.insert({i, to_string(i)});
		cout << "insert k = " << i << ", v = " << map[i] << endl;
	}
	cout << "unorderedMap size = " << map.size() << endl;

	cout << endl;
	cout << "map[1] = " << map[1] << endl;

	cout << endl;
	int t0 = 5;
	map.erase(t0);
	cout << "erase k = " << t0 << endl;
	cout << "unorderedMap size = " << map.size() << endl;

	cout << endl;
	int t1 = 5;
	Pair<int, string>* it = map.find(t1);

	if (it == nullptr)
	{
		cout << "not find " << t1 << endl;
	}
	else
	{
		cout << "find " << t1 << " = " << it->v_ << endl;
	}

	cout << endl;
	int t2 = 4;
	Pair<int, string>* it2 = map.find(t2);

	if (it2 == nullptr)
	{
		cout << "not find" << t2 << endl;
	}
	else
	{
		cout << "find " << t2 << " = " << it2->v_ << endl;
	}

	return 0;
}

int main() {
	testUnorderedMap1();
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值