跳到正文

剪贴板

剪贴板 n8vqjyl2

原文
剪贴板信息

剪贴板信息

收录
更新
C++
template <typename Key, typename Value, const unsigned int Maxn, const unsigned int MOD> class GP_HashTable { // Key, Value 跟 map 一样,Maxn 表示多少个数,MOD 表示 hash 模数
private:
	unsigned int head[MOD], sz;
	struct Edge { unsigned int next; Key key; Value val; } edge[Maxn];
	inline unsigned int hash(const std::string &s) {
		unsigned int hs = 0;
		for(unsigned int i=0; i<(unsigned int)s.size(); i++)	hs = (hs+s[i])*26;
		return hs%MOD;
	}
	inline unsigned int hash(const char &ch) { return ch%MOD; }
	template <typename T1, typename T2> inline unsigned int hash(const pair<T1, T2> &x) { return (x.first*x.second+x.first+x.second)%MOD; }
	template <typename T> inline unsigned int hash(const T &x) { return x%MOD; }
public:
	inline GP_HashTable() {}
	Value& operator [] (Key x) {
		unsigned int hs = hash(x);
		for(unsigned int i=head[hs]; i; i=edge[i].next) {
			if(edge[i].key == x)	return edge[i].val;
		}
		edge[++sz] = {head[hs], x, Value()};
		return head[hs] = sz, edge[sz].val;
	}
	inline bool count(Key x) {
		unsigned int hs = hash(x);
		for(unsigned int i=head[hs]; i; i=edge[i].next) {
			if(edge[i].key == x)	return true;
		}
		return false;
	}
	inline Value get(Key x) {
		unsigned int hs = hash(x);
		for(unsigned int i=head[hs]; i; i=edge[i].next) {
			if(edge[i].key == x)	return edge[i].val;
		}
		return Value();
	}
	inline void clear() {
		memset(head, 0, MOD<<2), sz = 0;
	}
};

使用:

  • 例:GP_HashTable <u64, u64, Maxn, 1000003> hs;

  • 一开始有说明