Linked list style cleanups

This commit is contained in:
Martin Willi
2011-07-06 09:43:46 +02:00
committed by Tobias Brunner
parent 629fd2f4f6
commit 75fc9d3136
+18 -19
View File
@@ -170,15 +170,11 @@ METHOD(linked_list_t, insert_first, void,
/* first entry in list */ /* first entry in list */
this->first = element; this->first = element;
this->last = element; this->last = element;
element->previous = NULL;
element->next = NULL;
} }
else else
{ {
element_t *old_first_element = this->first; element->next = this->first;
element->next = old_first_element; this->first->previous = element;
element->previous = NULL;
old_first_element->previous = element;
this->first = element; this->first = element;
} }
this->count++; this->count++;
@@ -244,22 +240,19 @@ METHOD(linked_list_t, remove_first, status_t,
METHOD(linked_list_t, insert_last, void, METHOD(linked_list_t, insert_last, void,
private_linked_list_t *this, void *item) private_linked_list_t *this, void *item)
{ {
element_t *element = element_create(item); element_t *element;
element = element_create(item);
if (this->count == 0) if (this->count == 0)
{ {
/* first entry in list */ /* first entry in list */
this->first = element; this->first = element;
this->last = element; this->last = element;
element->previous = NULL;
element->next = NULL;
} }
else else
{ {
element_t *old_last_element = this->last; element->previous = this->last;
element->previous = old_last_element; this->last->next = element;
element->next = NULL;
old_last_element->next = element;
this->last = element; this->last = element;
} }
this->count++; this->count++;
@@ -269,13 +262,15 @@ METHOD(linked_list_t, insert_before, void,
private_linked_list_t *this, private_enumerator_t *enumerator, private_linked_list_t *this, private_enumerator_t *enumerator,
void *item) void *item)
{ {
element_t *current = enumerator->current; element_t *current, *element;
current = enumerator->current;
if (!current) if (!current)
{ {
this->public.insert_last(&this->public, item); this->public.insert_last(&this->public, item);
return; return;
} }
element_t *element = element_create(item); element = element_create(item);
if (current->previous) if (current->previous)
{ {
current->previous->next = element; current->previous->next = element;
@@ -297,6 +292,7 @@ METHOD(linked_list_t, replace, void*,
void *item) void *item)
{ {
void *old = NULL; void *old = NULL;
if (enumerator->current) if (enumerator->current)
{ {
old = enumerator->current->value; old = enumerator->current->value;
@@ -411,10 +407,11 @@ METHOD(linked_list_t, invoke_offset, void,
void *d1, void *d2, void *d3, void *d4, void *d5) void *d1, void *d2, void *d3, void *d4, void *d5)
{ {
element_t *current = this->first; element_t *current = this->first;
linked_list_invoke_t *method;
while (current) while (current)
{ {
linked_list_invoke_t *method = current->value + offset; method = current->value + offset;
(*method)(current->value, d1, d2, d3, d4, d5); (*method)(current->value, d1, d2, d3, d4, d5);
current = current->next; current = current->next;
} }
@@ -436,9 +433,10 @@ METHOD(linked_list_t, invoke_function, void,
METHOD(linked_list_t, clone_offset, linked_list_t*, METHOD(linked_list_t, clone_offset, linked_list_t*,
private_linked_list_t *this, size_t offset) private_linked_list_t *this, size_t offset)
{ {
linked_list_t *clone = linked_list_create();
element_t *current = this->first; element_t *current = this->first;
linked_list_t *clone;
clone = linked_list_create();
while (current) while (current)
{ {
void* (**method)(void*) = current->value + offset; void* (**method)(void*) = current->value + offset;
@@ -452,15 +450,15 @@ METHOD(linked_list_t, clone_offset, linked_list_t*,
METHOD(linked_list_t, clone_function, linked_list_t*, METHOD(linked_list_t, clone_function, linked_list_t*,
private_linked_list_t *this, void* (*fn)(void*)) private_linked_list_t *this, void* (*fn)(void*))
{ {
linked_list_t *clone = linked_list_create();
element_t *current = this->first; element_t *current = this->first;
linked_list_t *clone;
clone = linked_list_create();
while (current) while (current)
{ {
clone->insert_last(clone, fn(current->value)); clone->insert_last(clone, fn(current->value));
current = current->next; current = current->next;
} }
return clone; return clone;
} }
@@ -468,6 +466,7 @@ METHOD(linked_list_t, destroy, void,
private_linked_list_t *this) private_linked_list_t *this)
{ {
void *value; void *value;
/* Remove all list items before destroying list */ /* Remove all list items before destroying list */
while (remove_first(this, &value) == SUCCESS) while (remove_first(this, &value) == SUCCESS)
{ {