hashtable: Add destroy_function method

This commit is contained in:
Tobias Brunner 2013-08-27 16:37:41 +02:00
parent dcb168413f
commit 3c206f2e81
2 changed files with 37 additions and 11 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2008-2012 Tobias Brunner
* Copyright (C) 2008-2014 Tobias Brunner
* Hochschule fuer Technik Rapperswil
*
* 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;
}
METHOD(hashtable_t, destroy, void,
private_hashtable_t *this)
static void destroy_internal(private_hashtable_t *this,
void (*fn)(void*,const void*))
{
pair_t *pair, *next;
u_int row;
@ -438,6 +438,10 @@ METHOD(hashtable_t, destroy, void,
pair = this->table[row];
while (pair)
{
if (fn)
{
fn(pair->value, pair->key);
}
next = pair->next;
free(pair);
pair = next;
@ -447,6 +451,18 @@ METHOD(hashtable_t, destroy, void,
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.
*/
@ -465,6 +481,7 @@ hashtable_t *hashtable_create(hashtable_hash_t hash, hashtable_equals_t equals,
.get_count = _get_count,
.create_enumerator = _create_enumerator,
.destroy = _destroy,
.destroy_function = _destroy_function,
},
.hash = hash,
.equals = equals,

View File

@ -156,6 +156,15 @@ struct hashtable_t {
* Destroys a hash table object.
*/
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));
};
/**