Make sure the enumerator stops after all items have been enumerated.

This also changes how insert_before behaves, before enumeration items
are inserted first, after enumeration last.
This commit is contained in:
Tobias Brunner
2011-07-06 09:43:46 +02:00
parent c225f9b558
commit 2bf9d39da6
2 changed files with 23 additions and 4 deletions
+19 -1
View File
@@ -110,11 +110,20 @@ struct private_enumerator_t {
* current item
*/
element_t *current;
/**
* enumerator has enumerated all items
*/
bool finished;
};
METHOD(enumerator_t, enumerate, bool,
private_enumerator_t *this, void **item)
{
if (this->finished)
{
return FALSE;
}
if (!this->current)
{
this->current = this->list->first;
@@ -125,6 +134,7 @@ METHOD(enumerator_t, enumerate, bool,
}
if (!this->current)
{
this->finished = TRUE;
return FALSE;
}
*item = this->current->value;
@@ -151,6 +161,7 @@ METHOD(linked_list_t, reset_enumerator, void,
private_linked_list_t *this, private_enumerator_t *enumerator)
{
enumerator->current = NULL;
enumerator->finished = FALSE;
}
METHOD(linked_list_t, get_count, int,
@@ -267,7 +278,14 @@ METHOD(linked_list_t, insert_before, void,
current = enumerator->current;
if (!current)
{
this->public.insert_last(&this->public, item);
if (enumerator->finished)
{
this->public.insert_last(&this->public, item);
}
else
{
this->public.insert_first(&this->public, item);
}
return;
}
element = element_create(item);
+4 -3
View File
@@ -95,9 +95,10 @@ struct linked_list_t {
/**
* Inserts a new item before the item the enumerator currently points to.
*
* If the enumerator's position is invalid, e.g. at the end of the list,
* the item is inserted last. This is helpful when inserting items into a
* sorted list.
* If this method is called before starting the enumeration the item is
* inserted first. If it is called after all items have been enumerated
* the item is inserted last. This is helpful when inserting items into
* a sorted list.
*
* @note The position of the enumerator is not changed.
*