-METHOD(iterator_t, iterator_get_count, int,
- private_iterator_t *this)
-{
- return this->list->count;
-}
-
-METHOD(iterator_t, iterate, bool,
- private_iterator_t *this, void** value)
-{
- if (this->forward)
- {
- this->current = this->current ? this->current->next : this->list->first;
- }
- else
- {
- this->current = this->current ? this->current->previous : this->list->last;
- }
- if (this->current == NULL)
- {
- return FALSE;
- }
- *value = this->current->value;
- return TRUE;
-}
-
-METHOD(iterator_t, iterator_reset, void,
- private_iterator_t *this)
-{
- this->current = NULL;
-}
-
-METHOD(iterator_t, iterator_remove, status_t,
- private_iterator_t *this)
-{
- element_t *new_current;
-
- if (this->current == NULL)
- {
- return NOT_FOUND;
- }
-
- if (this->list->count == 0)
- {
- return NOT_FOUND;
- }
- /* find out the new iterator position, depending on iterator direction */
- if (this->forward && this->current->previous != NULL)
- {
- new_current = this->current->previous;
- }
- else if (!this->forward && this->current->next != NULL)
- {
- new_current = this->current->next;
- }
- else
- {
- new_current = NULL;
- }
-
- /* now delete the entry :-) */
- if (this->current->previous == NULL)
- {
- if (this->current->next == NULL)
- {
- this->list->first = NULL;
- this->list->last = NULL;
- }
- else
- {
- this->current->next->previous = NULL;
- this->list->first = this->current->next;
- }
- }
- else if (this->current->next == NULL)
- {
- this->current->previous->next = NULL;
- this->list->last = this->current->previous;
- }
- else
- {
- this->current->previous->next = this->current->next;
- this->current->next->previous = this->current->previous;
- }
-
- this->list->count--;
- free(this->current);
- /* set the new iterator position */
- this->current = new_current;
- return SUCCESS;
-}
-
-static void insert_item_before(private_linked_list_t *this, element_t *current,
- void *item)
-{
- if (!current)
- {
- this->public.insert_last(&this->public, item);
- return;
- }
- element_t *element = element_create(item);
- if (current->previous)
- {
- current->previous->next = element;
- element->previous = current->previous;
- current->previous = element;
- element->next = current;
- }
- else
- {
- current->previous = element;
- element->next = current;
- this->first = element;
- }
- this->count++;
-}
-
-static void insert_item_after(private_linked_list_t *this, element_t *current,
- void *item)
-{
- if (!current)
- {
- this->public.insert_last(&this->public, item);
- return;
- }
- element_t *element = element_create(item);
- if (current->next)
- {
- current->next->previous = element;
- element->next = current->next;
- current->next = element;
- element->previous = current;
- }
- else
- {
- current->next = element;
- element->previous = current;
- this->last = element;
- }
- this->count++;
-}
-
-METHOD(iterator_t, iterator_insert_before, void,
- private_iterator_t * iterator, void *item)
-{
- insert_item_before(iterator->list, iterator->current, item);
-}
-
-METHOD(iterator_t, iterator_replace, status_t,
- private_iterator_t *this, void **old_item, void *new_item)
-{
- if (this->current == NULL)
- {
- return NOT_FOUND;
- }
- if (old_item != NULL)
- {
- *old_item = this->current->value;
- }
- this->current->value = new_item;
-
- return SUCCESS;
-}
-
-METHOD(iterator_t, iterator_insert_after, void,
- private_iterator_t *iterator, void *item)
-{
- insert_item_after(iterator->list, iterator->current, item);
-}
-
-METHOD(iterator_t, iterator_destroy, void,
- private_iterator_t *this)
-{
- free(this);
-}
-