/*
*平方探测二次散列
*gd-hash-table
*/
#include <stdio.h>
#include <stdbool.h>
constexpr int MOD = 65537,
NN{(int)(1e4)},MM{NN<<4|1},
SS{MM<<1|1};
struct gd_hash_table{
int tab[SS]{0,},
tot = 0;
_Bool v[SS]{0,};
static inline constexpr int _hash(int x) noexcept {return (x%MOD+MOD)%MOD;}
inline int find(int x){
int h = _hash(x),hh = h,d = 1;
while(v[h] && !(tab[hh] == x))hh = (h + d*(d++))%SS;
return hh;
}
typedef struct{int id;_Bool res;} pres;
pres insert(int x){
int p = find(x);
if(v[p]) return (pres){-1,0};
tab[p] = x;++tot;
return (pres){p,(_Bool)(v[p]=1)};
}
_Bool erase(int x){
int p = find(x);
if(!v[p] || !(tab[p] == x)) return 0;
--tot;return !(_Bool)(v[p] = 0);
}
inline size_t size(void)const noexcept {return tot;}
inline _Bool empty(void)const noexcept {return (_Bool)!tot;}
};
探测哈希 (gd_hash | 平方探测二次散列 )
于 2023-12-17 22:57:53 首次发布