用哈希表实现简陋的无序set

7 篇文章 0 订阅
#include <iostream>
#include <utility>
#include <vector>
#include <cstdio>
using namespace std;

const int hash_prime_num = 15;
const unsigned int _stl_prime[] = {
	2,		5,		11,		23,		53,
	97, 	193, 	389, 	769, 	1543,
	3079, 	6151, 	12289, 	24593, 	49157
};
template<class T, class H, class E>
class Hash_set{
	size_t count, table_size;
	vector<T> *v;//这里用vector套vector可能好点
	H hash;
	E equal;
	size_t find_prime(int num){//寻找更大的质数,作为哈希表的大小
		for(int i = 0; i < hash_prime_num; i++){
			if(_stl_prime[i] > num) return _stl_prime[i];
		}
	}
	bool add(const T& data){//加入节点
		size_t index = hash(data) % table_size;
		for(int i = 0; i < v[index].size(); i++) 
			if(equal(data, v[index][i])) return false;
		v[index].push_back(data);
		return true;
	}
public:
	typedef T type;
	struct iterator{// 迭代器,这个迭代器写的很渣,我都不想看了
		int x, y;
		Hash_set<T, H, E> *st;
		iterator(int _x = 0, int _y = 0, Hash_set<T, H, E>* _st = NULL){x = _x, y = _y, st = _st;}
		iterator(const iterator& q) {x = q.x, y = q.y, st = q.st;}
		T& operator *(){ return st->v[x][y];}
		bool operator ==(const iterator& R){return R.x == x && R.y == y;}
		bool operator != (const iterator& R){return !(R.x == x && R.y == y);}
		iterator& operator ++(){
			vector<T> &k = st->v[x];
			if(y < k.size()-1) { y++; return *this;}
			for(int i = x+1; i < st->hash_size(); i++) if(st->v[i].size()) { x = i, y = 0; return *this;}
			x = y = -1;
			return *this;
		}
	};
	
	iterator begin(){
		for(int i = 0; i < table_size; i++) if(v[i].size()) 
			return iterator(i, 0, this);
		return iterator(-1, -1, this);
	}
	iterator end(){ return iterator(-1, -1, this);}
	
	Hash_set(){table_size = 2, count = 0, v = new vector<T>[table_size];}
	size_t size()const{ return count;}
	size_t hash_size()const{ return table_size;}
	void insert(const T& data){//插入
		if(count == table_size/2){
			vector<T> buf;
			for(int i = 0; i < table_size; i++) 
				for(int j = 0; j < v[i].size(); j++)
					buf.push_back(v[i][j]);
			delete []v;
			table_size = find_prime(table_size);
			v = new vector<T>[table_size];
			for(int i = 0; i < buf.size(); i++) add(buf[i]);
		}
		count += add(data);
	}
	bool find(const T& data)const{//查询
		size_t index = (hash(data)) % table_size;
		for(int i = 0; i < v[index].size(); i++) 
			if(equal(data, v[index][i])) return true;
		return false;
	}
	void erase(const T& data){//删除
		size_t index = (hash(data)) % table_size;
		for(int i = 0; i < v[index].size(); i++) 
			if(equal(data, v[index][i])) v[index].erase(data);
	}
};


struct hasher{
	size_t operator()(const int& x)const{
		return x;
	}
};
struct equaler{
	bool operator()(const int& a, const int& b)const{
		return a == b;
	}
};

struct point{
    int x, y;
    point(int _x = 0, int _y = 0):x(_x), y(_y){}
    friend ostream& operator << (ostream& out, const point& p){
        out << p.x << " " << p.y << endl;
        return out;
    }
};
struct Phash{
    size_t operator()(const point& R)const{
        return (size_t)R.x * 10000007 + (size_t)(R.y);
    }
};
struct Pequal{
    bool operator()(const point& a, const point& b)const{
        return a.x == b.x && a.y == b.y;
    }
};

int main(int argc, char** argv) {
	Hash_set<int, hasher, equaler> st;
	for(int i = 0; i < 10; i++) st.insert(i*2-3);
	for(int i = 0; i < 10; i++) cout << st.find(i) << " ";
	puts("");
	for(Hash_set<int, hasher, equaler>::iterator it = st.begin(); it != st.end(); ++it){
		cout << *it << " " << endl;
	}

    puts("--------\n-------");
    Hash_set<point, Phash, Pequal> s2;
    for(int i = 0; i < 10; i++) s2.insert(point(i, i*2-5));
	for(Hash_set<point, Phash, Pequal>::iterator it = s2.begin(); it != s2.end(); ++it){
		cout << *it << " " << endl;
	}
    
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值