Added an insert_after and insert_before function to linked_list_t.

This commit is contained in:
Tobias Brunner
2011-07-06 09:43:45 +02:00
parent 178760012c
commit fbf014b0c1
2 changed files with 92 additions and 44 deletions
+64 -42
View File
@@ -145,20 +145,16 @@ METHOD(enumerator_t, enumerate, bool,
{ {
if (!this->current) if (!this->current)
{ {
if (!this->list->first)
{
return FALSE;
}
this->current = this->list->first; this->current = this->list->first;
} }
else else
{ {
if (!this->current->next) this->current = this->current->next;
}
if (!this->current)
{ {
return FALSE; return FALSE;
} }
this->current = this->current->next;
}
*item = this->current->value; *item = this->current->value;
return TRUE; return TRUE;
} }
@@ -270,30 +266,60 @@ METHOD(iterator_t, iterator_remove, status_t,
return SUCCESS; return SUCCESS;
} }
METHOD(iterator_t, iterator_insert_before, void, static void insert_item_before(private_linked_list_t *this, element_t *current,
private_iterator_t * iterator, void *item) 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; return;
} }
element_t *element = element_create(item); element_t *element = element_create(item);
if (iterator->current->previous == NULL) if (current->previous)
{ {
iterator->current->previous = element; current->previous->next = element;
element->next = iterator->current; element->previous = current->previous;
iterator->list->first = element; current->previous = element;
element->next = current;
} }
else else
{ {
iterator->current->previous->next = element; current->previous = element;
element->previous = iterator->current->previous; element->next = current;
iterator->current->previous = element; this->first = element;
element->next = iterator->current;
} }
iterator->list->count++; 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, 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, METHOD(iterator_t, iterator_insert_after, void,
private_iterator_t *iterator, void *item) private_iterator_t *iterator, void *item)
{ {
if (iterator->current == NULL) insert_item_after(iterator->list, iterator->current, item);
{
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++;
} }
METHOD(iterator_t, iterator_destroy, void, METHOD(iterator_t, iterator_destroy, void,
@@ -456,6 +462,20 @@ METHOD(linked_list_t, insert_last, void,
this->count++; 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, METHOD(linked_list_t, get_last, status_t,
private_linked_list_t *this, void **item) private_linked_list_t *this, void **item)
{ {
@@ -700,6 +720,8 @@ linked_list_t *linked_list_create()
.find_last = (void*)_find_last, .find_last = (void*)_find_last,
.insert_first = _insert_first, .insert_first = _insert_first,
.insert_last = _insert_last, .insert_last = _insert_last,
.insert_after = (void*)_insert_after,
.insert_before = (void*)_insert_before,
.remove_first = _remove_first, .remove_first = _remove_first,
.remove_last = _remove_last, .remove_last = _remove_last,
.remove = _remove_, .remove = _remove_,
+27 -1
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2007-2008 Tobias Brunner * Copyright (C) 2007-2011 Tobias Brunner
* Copyright (C) 2005-2008 Martin Willi * Copyright (C) 2005-2008 Martin Willi
* Copyright (C) 2005 Jan Hutter * Copyright (C) 2005 Jan Hutter
* Hochschule fuer Technik Rapperswil * Hochschule fuer Technik Rapperswil
@@ -99,6 +99,32 @@ struct linked_list_t {
*/ */
status_t (*remove_first) (linked_list_t *this, void **item); status_t (*remove_first) (linked_list_t *this, void **item);
/**
* Inserts a new item before the item the enumerator currently points to.
*
* @note The position of the enumerator is not changed.
* @note If the enumerator's position is invalid, the item is inserted last.
*
* @param enumerator enumerator with position
* @param item item value to insert in list
*/
void (*insert_before)(linked_list_t *this, enumerator_t *enumerator,
void *item);
/**
* Inserts a new item after the item the enumerator currently points to.
*
* @note The position of the enumerator is not changed (thus the next item
* the enumerator returns will be the inserted item).
*
* @note If the enumerator's position is invalid, the item is inserted last.
*
* @param enumerator enumerator with position
* @param item item value to insert in list
*/
void (*insert_after)(linked_list_t *this, enumerator_t *enumerator,
void *item);
/** /**
* Remove an item from the list where the enumerator points to. * Remove an item from the list where the enumerator points to.
* *