}
/* allocates and clears the hash */
-struct hashtable_t *hash_new(int size, hashdata_compare_cb compare,
- hashdata_choose_cb choose)
+struct hashtable_t *hash_new(int size, hashdata_choose_cb choose)
{
struct hashtable_t *hash;
hash_init(hash);
- hash->compare = compare;
hash->choose = choose;
return hash;
}
/* adds data to the hashtable. returns 0 on success, -1 on error */
-int hash_add(struct hashtable_t *hash, void *data)
+int hash_add(struct hashtable_t *hash, hashdata_compare_cb compare, void *data)
{
int index;
struct element_t *bucket, *prev_bucket = NULL;
bucket = hash->table[index];
while (bucket != NULL) {
- if (hash->compare(bucket->data, data))
+ if (compare(bucket->data, data))
return -1;
prev_bucket = bucket;
/* finds data, based on the key in keydata. returns the found data on success,
* or NULL on error */
-void *hash_find(struct hashtable_t *hash, void *keydata)
+void *hash_find(struct hashtable_t *hash, hashdata_compare_cb compare,
+ void *keydata)
{
int index;
struct element_t *bucket;
bucket = hash->table[index];
while (bucket != NULL) {
- if (hash->compare(bucket->data, keydata))
+ if (compare(bucket->data, keydata))
return bucket->data;
bucket = bucket->next;
* can remove the used structure yourself, or NULL on error . data could be the
* structure you use with just the key filled, we just need the key for
* comparing. */
-void *hash_remove(struct hashtable_t *hash, void *data)
+void *hash_remove(struct hashtable_t *hash, hashdata_compare_cb compare,
+ void *data)
{
struct hash_it_t hash_it_t;
hash_it_t.prev_bucket = NULL;
while (hash_it_t.bucket != NULL) {
- if (hash->compare(hash_it_t.bucket->data, data)) {
+ if (compare(hash_it_t.bucket->data, data)) {
hash_it_t.first_bucket =
(hash_it_t.bucket ==
hash->table[hash_it_t.index] ?
/* resize the hash, returns the pointer to the new hash or NULL on
* error. removes the old hash on success. */
-struct hashtable_t *hash_resize(struct hashtable_t *hash, int size)
+struct hashtable_t *hash_resize(struct hashtable_t *hash,
+ hashdata_compare_cb compare, int size)
{
struct hashtable_t *new_hash;
struct element_t *bucket;
int i;
/* initialize a new hash with the new size */
- new_hash = hash_new(size, hash->compare, hash->choose);
+ new_hash = hash_new(size, hash->choose);
if (new_hash == NULL)
return NULL;
bucket = hash->table[i];
while (bucket != NULL) {
- hash_add(new_hash, bucket->data);
+ hash_add(new_hash, compare, bucket->data);
bucket = bucket->next;
}
}