hashtable: Add destroy_function method

This commit is contained in:
Tobias Brunner
2014-06-19 14:00:48 +02:00
parent dcb168413f
commit 3c206f2e81
2 changed files with 37 additions and 11 deletions
+20 -3
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2008-2012 Tobias Brunner * Copyright (C) 2008-2014 Tobias Brunner
* Hochschule fuer Technik Rapperswil * Hochschule fuer Technik Rapperswil
* *
* This program is free software; you can redistribute it and/or modify it * This program is free software; you can redistribute it and/or modify it
@@ -427,8 +427,8 @@ METHOD(hashtable_t, create_enumerator, enumerator_t*,
return &enumerator->enumerator; return &enumerator->enumerator;
} }
METHOD(hashtable_t, destroy, void, static void destroy_internal(private_hashtable_t *this,
private_hashtable_t *this) void (*fn)(void*,const void*))
{ {
pair_t *pair, *next; pair_t *pair, *next;
u_int row; u_int row;
@@ -438,6 +438,10 @@ METHOD(hashtable_t, destroy, void,
pair = this->table[row]; pair = this->table[row];
while (pair) while (pair)
{ {
if (fn)
{
fn(pair->value, pair->key);
}
next = pair->next; next = pair->next;
free(pair); free(pair);
pair = next; pair = next;
@@ -447,6 +451,18 @@ METHOD(hashtable_t, destroy, void,
free(this); free(this);
} }
METHOD(hashtable_t, destroy, void,
private_hashtable_t *this)
{
destroy_internal(this, NULL);
}
METHOD(hashtable_t, destroy_function, void,
private_hashtable_t *this, void (*fn)(void*,const void*))
{
destroy_internal(this, fn);
}
/* /*
* Described in header. * Described in header.
*/ */
@@ -465,6 +481,7 @@ hashtable_t *hashtable_create(hashtable_hash_t hash, hashtable_equals_t equals,
.get_count = _get_count, .get_count = _get_count,
.create_enumerator = _create_enumerator, .create_enumerator = _create_enumerator,
.destroy = _destroy, .destroy = _destroy,
.destroy_function = _destroy_function,
}, },
.hash = hash, .hash = hash,
.equals = equals, .equals = equals,
@@ -156,6 +156,15 @@ struct hashtable_t {
* Destroys a hash table object. * Destroys a hash table object.
*/ */
void (*destroy) (hashtable_t *this); void (*destroy) (hashtable_t *this);
/**
* Destroys a hash table object and calls the given function for each
* item and its key in the hash table.
*
* @param function function to call on each item and key
*/
void (*destroy_function)(hashtable_t *this,
void (*)(void *val, const void *key));
}; };
/** /**