42 static const unsigned int primes[] = {
44 769, 1543, 3079, 6151,
45 12289, 24593, 49157, 98317,
46 196613, 393241, 786433, 1572869,
47 3145739, 6291469, 12582917, 25165843,
48 50331653, 100663319, 201326611, 402653189,
57 unsigned int (*hashf) (
void*),
58 int (*eqf) (
void*,
void*))
61 unsigned int pindex, size =
primes[0];
67 if (
primes[pindex] > minsize) {
74 if (NULL == h) abort();
78 if (NULL == h->
table) abort();
86 h->
loadlimit = (
unsigned int) ceil(size * max_load_factor);
97 struct entry **newtable;
100 unsigned int newsize, i, index;
105 newtable = (
struct entry **)malloc(
sizeof(
struct entry*) * newsize);
106 if (NULL != newtable)
108 memset(newtable, 0, newsize *
sizeof(
struct entry *));
112 while (NULL != (e = h->
table[i])) {
115 e->
next = newtable[index];
125 newtable = (
struct entry **)
126 realloc(h->
table, newsize *
sizeof(
struct entry *));
127 if (NULL == newtable) { (h->
primeindex)--;
return 0; }
131 for (pE = &(newtable[i]), e = *pE; e != NULL; e = *pE) {
138 e->
next = newtable[index];
145 h->
loadlimit = (
unsigned int) ceil(newsize * max_load_factor);
153 return h->entrycount;
165 pE = &(h->
table[index]);
169 if ((hashvalue == e->
h) && (h->
eqfn(k, e->
k))) {
197 unsigned int hashvalue =
hash(h, k);
198 unsigned index =
indexFor(h->tablelength, hashvalue);
204 if (++(h->entrycount) > h->loadlimit)
211 index =
indexFor(h->tablelength, hashvalue);
213 e = (
struct entry *)malloc(
sizeof(
struct entry));
214 if (NULL == e) { --(h->entrycount);
return 0; }
220 e->
next = h->table[index];
230 unsigned int hashvalue, index;
231 hashvalue =
hash(h,k);
232 index =
indexFor(h->tablelength,hashvalue);
236 if ((hashvalue == e->
h) && (h->eqfn(k, e->
k)))
return e->
v;
246 unsigned int hashvalue =
hash(h,k);
257 struct entry **table = (*h)->table;
259 for (i = 0; i < (*h)->tablelength; i++) {
289 if ((i->
e = i->
e->
next) != 0) {
349 *klen = (int)strlen(i->e->k);
struct switch_hashtable * h
const unsigned int prime_table_length
const float max_load_factor
void switch_hashtable_destroy(switch_hashtable_t **h)
void * switch_hashtable_search(switch_hashtable_t *h, void *k)
static __inline__ unsigned int indexFor(unsigned int tablelength, unsigned int hashvalue)
unsigned int switch_hashtable_count(switch_hashtable_t *h)
unsigned int(* hashfn)(void *k)
int switch_hashtable_insert_destructor(switch_hashtable_t *h, void *k, void *v, hashtable_flag_t flags, hashtable_destructor_t destructor)
#define switch_zmalloc(ptr, len)
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
void * switch_hashtable_remove(switch_hashtable_t *h, void *k)
static int hashtable_expand(switch_hashtable_t *h)
switch_status_t
Common return values.
void switch_hashtable_this_val(switch_hashtable_iterator_t *i, void *val)
static void * _switch_hashtable_remove(switch_hashtable_t *h, void *k, unsigned int hashvalue, unsigned int index)
switch_status_t switch_create_hashtable(switch_hashtable_t **hp, unsigned int minsize, unsigned int(*hashf)(void *), int(*eqf)(void *, void *))
void switch_hashtable_this(switch_hashtable_iterator_t *i, const void **key, switch_ssize_t *klen, void **val)
int(* eqfn)(void *k1, void *k2)
static const unsigned int primes[]
switch_hashtable_iterator_t * switch_hashtable_first_iter(switch_hashtable_t *h, switch_hashtable_iterator_t *it)
hashtable_destructor_t destructor
switch_hashtable_iterator_t * switch_hashtable_next(switch_hashtable_iterator_t **iP)
void(* hashtable_destructor_t)(void *ptr)