22size_t gp_htable_tsize(
size_t used);
25 size_t (*hash)(
const void *key,
size_t htable_size),
29 unsigned int h = hash(key, htable_size);
32 h = (h+1) % htable_size;
38static inline void gp_htable_rehash(
gp_htable *self,
size_t new_size,
39 size_t (*hash)(
const void *key,
size_t htable_size))
43 GP_DEBUG(1,
"Rehashing from %zu to %zu", self->
size, new_size);
54 for (i = 0; i < self->
size; i++) {
55 if (!self->
recs[i].key)
58 gp_htable_put_(recs, hash, new_size, self->
recs[i].val, self->
recs[i].key);
63 self->
size = new_size;
66static inline void gp_htable_put2(
gp_htable *self,
67 size_t (*hash)(
const void *key,
size_t htable_size),
71 gp_htable_rehash(self, gp_htable_tsize(self->
used), hash);
73 gp_htable_put_(self->
recs, hash, self->
size, val, key);
76static inline void *gp_htable_get2(
gp_htable *self,
77 size_t (*hash)(
const void *key,
size_t htable_size),
78 int (*cmp)(
const void *key1,
const void *key2),
84 size_t h = hash(key, self->
size);
86 while (self->
recs[h].key) {
87 if (cmp(self->
recs[h].key, key))
88 return self->
recs[h].val;
89 h = (h+1) % self->
size;
95static inline void *gp_htable_rem2_(
gp_htable *self,
96 size_t (*hash)(
const void *key,
size_t htable_size),
102 free(self->
recs[h].key);
104 ret = self->
recs[h].val;
106 self->
recs[h].key = NULL;
107 self->
recs[h].val = NULL;
110 gp_htable_rehash(self, gp_htable_tsize(self->
used), hash);
118 i = (i+1)%self->
size;
120 if (!self->
recs[i].key)
123 h = hash(self->
recs[i].key, self->
size);
138 self->
recs[i].key = NULL;
139 self->
recs[i].val = NULL;
147static inline void *gp_htable_rem2(
gp_htable *self,
148 size_t (*hash)(
const void *key,
size_t htable_size),
149 int (*cmp)(
const void *key1,
const void *key2),
152 size_t h = hash(key, self->
size);
154 while (self->
recs[h].key) {
155 if (cmp(self->
recs[h].key, key))
156 return gp_htable_rem2_(self, hash, h);
158 h = (h+1) % self->
size;
173static inline void gp_htable_trim2(
gp_htable *self,
174 size_t (*hash)(
const void *key,
size_t htable_size),
175 int (*cmp)(
const void *key1,
const void *key2),
176 int (*trim)(
void *val),
177 void (*free_val)(
void *val))
182 for (i = 0; i < self->
size; i++) {
183 if (!self->
recs[i].key)
186 if (!trim(self->
recs[i].val))
190 free_val(self->
recs[i].val);
192 self->
recs[i].val = key;
193 key = self->
recs[i].key;
197 key = gp_htable_rem2(self, hash, cmp, key);
#define GP_WARN(...)
A debug WARN printf-like macro.
#define GP_DEBUG(level,...)
A debug printf-like macro.
Simple hash table implementation.
size_t used
Number of used slots in the hash table.
enum gp_htable_flags flags
Flags.
size_t size
Hash table record array size.
struct gp_htable_rec * recs
Array for the hash table records.