Keep count of remaining elements to enumerate in hashtable_t.

This improves performance during enumeration as not all buckets have to be
checked.
This commit is contained in:
Tobias Brunner
2011-05-24 19:23:45 +02:00
parent 8af0177189
commit ee0fb2ab81
+8 -1
View File
@@ -132,6 +132,11 @@ struct private_enumerator_t {
*/ */
u_int row; u_int row;
/**
* number of remaining items in hashtable
*/
u_int count;
/** /**
* current pair * current pair
*/ */
@@ -326,7 +331,7 @@ METHOD(hashtable_t, get_count, u_int,
METHOD(enumerator_t, enumerate, bool, METHOD(enumerator_t, enumerate, bool,
private_enumerator_t *this, void **key, void **value) private_enumerator_t *this, void **key, void **value)
{ {
while (this->row < this->table->capacity) while (this->count && this->row < this->table->capacity)
{ {
this->prev = this->current; this->prev = this->current;
if (this->current) if (this->current)
@@ -347,6 +352,7 @@ METHOD(enumerator_t, enumerate, bool,
{ {
*value = this->current->value; *value = this->current->value;
} }
this->count--;
return TRUE; return TRUE;
} }
this->row++; this->row++;
@@ -365,6 +371,7 @@ METHOD(hashtable_t, create_enumerator, enumerator_t*,
.destroy = (void*)free, .destroy = (void*)free,
}, },
.table = this, .table = this,
.count = this->count,
); );
return &enumerator->enumerator; return &enumerator->enumerator;