diff options
author | Tobias Brunner <tobias@strongswan.org> | 2011-05-13 11:51:58 +0200 |
---|---|---|
committer | Tobias Brunner <tobias@strongswan.org> | 2011-07-06 09:43:45 +0200 |
commit | fbf014b0c10365d241ff8aeb83618bb929d09f8f (patch) | |
tree | 78cfe84fc540bb6219a14e9e07e391a6d048cf8e /src/libstrongswan/utils/linked_list.c | |
parent | 178760012cc65ab77988f07cfce9dd56dd004a6f (diff) | |
download | strongswan-fbf014b0c10365d241ff8aeb83618bb929d09f8f.tar.bz2 strongswan-fbf014b0c10365d241ff8aeb83618bb929d09f8f.tar.xz |
Added an insert_after and insert_before function to linked_list_t.
Diffstat (limited to 'src/libstrongswan/utils/linked_list.c')
-rw-r--r-- | src/libstrongswan/utils/linked_list.c | 106 |
1 files changed, 64 insertions, 42 deletions
diff --git a/src/libstrongswan/utils/linked_list.c b/src/libstrongswan/utils/linked_list.c index 564722243..cc5590e9b 100644 --- a/src/libstrongswan/utils/linked_list.c +++ b/src/libstrongswan/utils/linked_list.c @@ -145,20 +145,16 @@ METHOD(enumerator_t, enumerate, bool, { if (!this->current) { - if (!this->list->first) - { - return FALSE; - } this->current = this->list->first; } else { - if (!this->current->next) - { - return FALSE; - } this->current = this->current->next; } + if (!this->current) + { + return FALSE; + } *item = this->current->value; return TRUE; } @@ -270,30 +266,60 @@ METHOD(iterator_t, iterator_remove, status_t, return SUCCESS; } -METHOD(iterator_t, iterator_insert_before, void, - private_iterator_t * iterator, void *item) +static void insert_item_before(private_linked_list_t *this, element_t *current, + void *item) { - if (iterator->current == NULL) + if (!current) { - iterator->list->public.insert_first(&(iterator->list->public), item); + 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 (iterator->current->previous == NULL) + if (current->next) { - iterator->current->previous = element; - element->next = iterator->current; - iterator->list->first = element; + current->next->previous = element; + element->next = current->next; + current->next = element; + element->previous = current; } else { - iterator->current->previous->next = element; - element->previous = iterator->current->previous; - iterator->current->previous = element; - element->next = iterator->current; + current->next = element; + element->previous = current; + this->last = element; } - iterator->list->count++; + 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, @@ -315,27 +341,7 @@ METHOD(iterator_t, iterator_replace, status_t, METHOD(iterator_t, iterator_insert_after, void, private_iterator_t *iterator, void *item) { - if (iterator->current == NULL) - { - iterator->list->public.insert_first(&(iterator->list->public),item); - return; - } - - element_t *element = element_create(item); - if (iterator->current->next == NULL) - { - iterator->current->next = element; - element->previous = iterator->current; - iterator->list->last = element; - } - else - { - iterator->current->next->previous = element; - element->next = iterator->current->next; - iterator->current->next = element; - element->previous = iterator->current; - } - iterator->list->count++; + insert_item_after(iterator->list, iterator->current, item); } METHOD(iterator_t, iterator_destroy, void, @@ -456,6 +462,20 @@ METHOD(linked_list_t, insert_last, void, this->count++; } +METHOD(linked_list_t, insert_before, void, + private_linked_list_t *this, private_enumerator_t *enumerator, + void *item) +{ + insert_item_before(this, enumerator->current, item); +} + +METHOD(linked_list_t, insert_after, void, + private_linked_list_t *this, private_enumerator_t *enumerator, + void *item) +{ + insert_item_after(this, enumerator->current, item); +} + METHOD(linked_list_t, get_last, status_t, private_linked_list_t *this, void **item) { @@ -700,6 +720,8 @@ linked_list_t *linked_list_create() .find_last = (void*)_find_last, .insert_first = _insert_first, .insert_last = _insert_last, + .insert_after = (void*)_insert_after, + .insert_before = (void*)_insert_before, .remove_first = _remove_first, .remove_last = _remove_last, .remove = _remove_, |