- inserting and removing now needs a linked_list_iterator_t* instead of

a linked_list_element_t*
- linked_list_element_t removed
- has_next now returns bool instead of status_t
This commit is contained in:
Jan Hutter
2005-11-08 13:31:39 +00:00
parent 6fb28da078
commit e3dd1393d5
4 changed files with 279 additions and 220 deletions
+4 -23
View File
@@ -224,10 +224,8 @@ static status_t get(private_event_queue_t *this, job_t **job)
static status_t add_absolute(private_event_queue_t *this, job_t *job, timeval_t time) static status_t add_absolute(private_event_queue_t *this, job_t *job, timeval_t time)
{ {
event_t *event = event_create(time,job); event_t *event = event_create(time,job);
linked_list_element_t * current_list_element;
event_t *current_event; event_t *current_event;
status_t status; status_t status;
bool has_next;
int count; int count;
if (event == NULL) if (event == NULL)
@@ -275,34 +273,17 @@ static status_t add_absolute(private_event_queue_t *this, job_t *job, timeval_t
} }
status = iterator->has_next(iterator,&has_next); iterator->has_next(iterator);
/* first element has not to be checked (already done) */ /* first element has not to be checked (already done) */
status = iterator->has_next(iterator,&has_next);
if (status != SUCCESS)
{
iterator->destroy(iterator);
break;
}
while(has_next) while(iterator->has_next(iterator))
{ {
status = iterator->current(iterator,&current_list_element); status = iterator->current(iterator,(void **) &current_event);
if (status != SUCCESS)
{
break;
}
current_event = (event_t *) current_list_element->value;
if (time_difference(&(event->time), &(current_event->time)) <= 0) if (time_difference(&(event->time), &(current_event->time)) <= 0)
{ {
/* my event has to be fired before the current event in list */ /* my event has to be fired before the current event in list */
status = this->list->insert_before(this->list,current_list_element,event); status = this->list->insert_before(this->list,iterator,event);
break;
}
iterator->has_next(iterator,&has_next);
if (status != SUCCESS)
{
break; break;
} }
} }
+181 -79
View File
@@ -28,18 +28,19 @@
#include "linked_list.h" #include "linked_list.h"
typedef struct private_linked_list_element_s private_linked_list_element_t; typedef struct linked_list_element_s linked_list_element_t;
/** /**
* Private Data of a linked list element * @brief Element of the linked_list.
* *
* This element holds a pointer to the value of the list item itself.
*/ */
struct private_linked_list_element_s{ struct linked_list_element_s{
/** /**
* public data of element * value of a list item
*/ */
linked_list_element_t public; void *value;
/** /**
* @brief Destroys a linked_list_element object * @brief Destroys a linked_list_element object
@@ -47,24 +48,24 @@ struct private_linked_list_element_s{
* @param linked_list_element_t calling object * @param linked_list_element_t calling object
* @returns SUCCESS if succeeded, FAILED otherwise * @returns SUCCESS if succeeded, FAILED otherwise
*/ */
status_t (*destroy) (private_linked_list_element_t *this); status_t (*destroy) (linked_list_element_t *this);
/** /**
* previous list element * previous list element
* NULL if first element in list * NULL if first element in list
*/ */
private_linked_list_element_t *previous; linked_list_element_t *previous;
/** /**
* next list element * next list element
* NULL if last element in list * NULL if last element in list
*/ */
private_linked_list_element_t *next; linked_list_element_t *next;
}; };
/** /**
* @brief implements function destroy of linked_list_item_t * @brief implements function destroy of linked_list_item_t
*/ */
static status_t linked_list_element_destroy(private_linked_list_element_t *this) static status_t linked_list_element_destroy(linked_list_element_t *this)
{ {
if (this == NULL) if (this == NULL)
{ {
@@ -74,12 +75,19 @@ static status_t linked_list_element_destroy(private_linked_list_element_t *this)
return SUCCESS; return SUCCESS;
} }
/* /**
* Creates an empty linked list (described in header-file) * @brief Creates an empty linked list object
*
* @param[in] value value of item to be set
*
* @warning only the pointer to the value is stored
*
* @return linked_list_element object
*/ */
linked_list_element_t *linked_list_element_create(void *value) linked_list_element_t *linked_list_element_create(void *value)
{ {
private_linked_list_element_t *this = alloc_thing(private_linked_list_element_t, "private_linked_list_element_t"); linked_list_element_t *this = alloc_thing(linked_list_element_t, "linked_list_element_t");
if (this == NULL) if (this == NULL)
{ {
@@ -90,9 +98,9 @@ linked_list_element_t *linked_list_element_create(void *value)
this->previous=NULL; this->previous=NULL;
this->next=NULL; this->next=NULL;
this->public.value = value; this->value = value;
return (&this->public); return (this);
} }
/** /**
@@ -116,12 +124,12 @@ struct private_linked_list_s{
* First element in list * First element in list
* NULL if no elements in list * NULL if no elements in list
*/ */
private_linked_list_element_t *first; linked_list_element_t *first;
/** /**
* Last element in list * Last element in list
* NULL if no elements in list * NULL if no elements in list
*/ */
private_linked_list_element_t *last; linked_list_element_t *last;
}; };
@@ -145,7 +153,7 @@ struct private_linked_list_iterator_s{
/** /**
* current element of the iterator * current element of the iterator
*/ */
private_linked_list_element_t *current; linked_list_element_t *current;
/** /**
* direction of iterator * direction of iterator
@@ -156,47 +164,49 @@ struct private_linked_list_iterator_s{
/** /**
* Implements function has_next of linked_list_iteratr * Implements function has_next of linked_list_iteratr
*/ */
static status_t iterator_has_next(private_linked_list_iterator_t *this, bool * has_next) bool iterator_has_next(private_linked_list_iterator_t *this)
{ {
if (this->list->count == 0) if (this->list->count == 0)
{ {
*has_next = FALSE; return FALSE;
return SUCCESS;
} }
if (this->current == NULL) if (this->current == NULL)
{ {
this->current = (this->forward) ? this->list->first : this->list->last; this->current = (this->forward) ? this->list->first : this->list->last;
*has_next = TRUE; return TRUE;
return SUCCESS;
} }
if (this->forward) if (this->forward)
{ {
if (this->current->next == NULL) if (this->current->next == NULL)
{ {
*has_next = FALSE; return FALSE;
return SUCCESS;
} }
this->current = this->current->next; this->current = this->current->next;
*has_next = TRUE; return TRUE;
return SUCCESS;
} }
/* backward */ /* backward */
if (this->current->previous == NULL) if (this->current->previous == NULL)
{ {
*has_next = FALSE; return FALSE;
return SUCCESS;
} }
this->current = this->current->previous; this->current = this->current->previous;
*has_next = TRUE; return TRUE;
return SUCCESS;
} }
/** /**
* Implements function current of linked_list_iteratr * Implements function current of linked_list_iteratr
*/ */
static status_t iterator_current(private_linked_list_iterator_t *this, linked_list_element_t **element) static status_t iterator_current(private_linked_list_iterator_t *this, void **value)
{ {
*element = &(this->current->public); if (this == NULL)
{
return FAILED;
}
if (this->current == NULL)
{
return FAILED;
}
*value = this->current->value;
return SUCCESS; return SUCCESS;
} }
@@ -205,6 +215,10 @@ static status_t iterator_current(private_linked_list_iterator_t *this, linked_li
*/ */
static status_t iterator_reset(private_linked_list_iterator_t *this) static status_t iterator_reset(private_linked_list_iterator_t *this)
{ {
if (this == NULL)
{
return FAILED;
}
this->current = NULL; this->current = NULL;
return SUCCESS; return SUCCESS;
} }
@@ -214,6 +228,10 @@ static status_t iterator_reset(private_linked_list_iterator_t *this)
*/ */
static status_t iterator_destroy(private_linked_list_iterator_t *this) static status_t iterator_destroy(private_linked_list_iterator_t *this)
{ {
if (this == NULL)
{
return FAILED;
}
pfree(this); pfree(this);
return SUCCESS; return SUCCESS;
} }
@@ -223,6 +241,10 @@ static status_t iterator_destroy(private_linked_list_iterator_t *this)
*/ */
static status_t get_count(private_linked_list_t *this, int *count) static status_t get_count(private_linked_list_t *this, int *count)
{ {
if (this == NULL)
{
return FAILED;
}
*count = this->count; *count = this->count;
return SUCCESS; return SUCCESS;
} }
@@ -237,8 +259,8 @@ static status_t create_iterator (private_linked_list_t *linked_list, linked_list
return FAILED; return FAILED;
} }
this->public.has_next = (status_t (*) (linked_list_iterator_t *this, bool * has_next)) iterator_has_next; this->public.has_next = (bool (*) (linked_list_iterator_t *this)) iterator_has_next;
this->public.current = (status_t (*) (linked_list_iterator_t *this, linked_list_element_t **element)) iterator_current; this->public.current = (status_t (*) (linked_list_iterator_t *this, void **value)) iterator_current;
this->public.reset = (status_t (*) (linked_list_iterator_t *this)) iterator_reset; this->public.reset = (status_t (*) (linked_list_iterator_t *this)) iterator_reset;
this->public.destroy = (status_t (*) (linked_list_iterator_t *this)) iterator_destroy; this->public.destroy = (status_t (*) (linked_list_iterator_t *this)) iterator_destroy;
@@ -258,7 +280,14 @@ static status_t create_iterator (private_linked_list_t *linked_list, linked_list
*/ */
static status_t insert_first(private_linked_list_t *this, void *item) static status_t insert_first(private_linked_list_t *this, void *item)
{ {
private_linked_list_element_t *element =(private_linked_list_element_t *) linked_list_element_create(item); linked_list_element_t *element;
if (this == NULL)
{
return FAILED;
}
element =(linked_list_element_t *) linked_list_element_create(item);
if (element == NULL) if (element == NULL)
{ {
@@ -281,7 +310,7 @@ static status_t insert_first(private_linked_list_t *this, void *item)
element->destroy(element); element->destroy(element);
return FAILED; return FAILED;
} }
private_linked_list_element_t *old_first_element = this->first; linked_list_element_t *old_first_element = this->first;
element->next = old_first_element; element->next = old_first_element;
element->previous = NULL; element->previous = NULL;
old_first_element->previous = element; old_first_element->previous = element;
@@ -298,6 +327,11 @@ static status_t insert_first(private_linked_list_t *this, void *item)
*/ */
static status_t remove_first(private_linked_list_t *this, void **item) static status_t remove_first(private_linked_list_t *this, void **item)
{ {
if (this == NULL)
{
return FAILED;
}
if (this->count == 0) if (this->count == 0)
{ {
return FAILED; return FAILED;
@@ -308,7 +342,7 @@ static status_t remove_first(private_linked_list_t *this, void **item)
return FAILED; return FAILED;
} }
private_linked_list_element_t *element = this->first; linked_list_element_t *element = this->first;
if (element->next != NULL) if (element->next != NULL)
{ {
@@ -316,7 +350,7 @@ static status_t remove_first(private_linked_list_t *this, void **item)
} }
this->first = element->next; this->first = element->next;
*item = element->public.value; *item = element->value;
this->count--; this->count--;
@@ -328,6 +362,11 @@ static status_t remove_first(private_linked_list_t *this, void **item)
*/ */
static status_t get_first(private_linked_list_t *this, void **item) static status_t get_first(private_linked_list_t *this, void **item)
{ {
if (this == NULL)
{
return FAILED;
}
if (this->count == 0) if (this->count == 0)
{ {
return FAILED; return FAILED;
@@ -338,7 +377,7 @@ static status_t get_first(private_linked_list_t *this, void **item)
return FAILED; return FAILED;
} }
*item = this->first->public.value; *item = this->first->value;
return SUCCESS; return SUCCESS;
} }
@@ -348,7 +387,12 @@ static status_t get_first(private_linked_list_t *this, void **item)
*/ */
static status_t insert_last(private_linked_list_t *this, void *item) static status_t insert_last(private_linked_list_t *this, void *item)
{ {
private_linked_list_element_t *element = (private_linked_list_element_t *) linked_list_element_create(item); if (this == NULL)
{
return FAILED;
}
linked_list_element_t *element = (linked_list_element_t *) linked_list_element_create(item);
if (element == NULL) if (element == NULL)
{ {
@@ -370,7 +414,7 @@ static status_t insert_last(private_linked_list_t *this, void *item)
element->destroy(element); element->destroy(element);
return FAILED; return FAILED;
} }
private_linked_list_element_t *old_last_element = this->last; linked_list_element_t *old_last_element = this->last;
element->previous = old_last_element; element->previous = old_last_element;
element->next = NULL; element->next = NULL;
old_last_element->next = element; old_last_element->next = element;
@@ -387,6 +431,11 @@ static status_t insert_last(private_linked_list_t *this, void *item)
*/ */
static status_t remove_last(private_linked_list_t *this, void **item) static status_t remove_last(private_linked_list_t *this, void **item)
{ {
if (this == NULL)
{
return FAILED;
}
if (this->count == 0) if (this->count == 0)
{ {
return FAILED; return FAILED;
@@ -397,7 +446,7 @@ static status_t remove_last(private_linked_list_t *this, void **item)
return FAILED; return FAILED;
} }
private_linked_list_element_t *element = this->last; linked_list_element_t *element = this->last;
if (element->previous != NULL) if (element->previous != NULL)
{ {
@@ -405,7 +454,7 @@ static status_t remove_last(private_linked_list_t *this, void **item)
} }
this->last = element->previous; this->last = element->previous;
*item = element->public.value; *item = element->value;
this->count--; this->count--;
@@ -417,6 +466,11 @@ static status_t remove_last(private_linked_list_t *this, void **item)
*/ */
static status_t get_last(private_linked_list_t *this, void **item) static status_t get_last(private_linked_list_t *this, void **item)
{ {
if (this == NULL)
{
return FAILED;
}
if (this->count == 0) if (this->count == 0)
{ {
return FAILED; return FAILED;
@@ -427,7 +481,7 @@ static status_t get_last(private_linked_list_t *this, void **item)
return FAILED; return FAILED;
} }
*item = this->last->public.value; *item = this->last->value;
return SUCCESS; return SUCCESS;
} }
@@ -435,38 +489,48 @@ static status_t get_last(private_linked_list_t *this, void **item)
/** /**
* @brief implements function insert_before of linked_list_t * @brief implements function insert_before of linked_list_t
*/ */
static status_t insert_before(private_linked_list_t *this, private_linked_list_element_t * next_element, void *item) static status_t insert_before(private_linked_list_t *this, private_linked_list_iterator_t * iterator, void *item)
{ {
if ((this == NULL) || (iterator == NULL))
{
return FAILED;
}
if (this->count == 0) if (this->count == 0)
{ {
return FAILED; return FAILED;
} }
private_linked_list_element_t *element =(private_linked_list_element_t *) linked_list_element_create(item); if (iterator->current == NULL)
{
return (this->public.insert_first(&this->public,item));
}
linked_list_element_t *element =(linked_list_element_t *) linked_list_element_create(item);
if (element == NULL) if (element == NULL)
{ {
return FAILED; return FAILED;
} }
if (next_element->previous == NULL) if (iterator->current->previous == NULL)
{ {
if (this->first != next_element) if (this->first != iterator->current)
{ {
element->destroy(element); element->destroy(element);
return FAILED; return FAILED;
} }
next_element->previous = element; iterator->current->previous = element;
element->next = next_element; element->next = iterator->current;
this->first = element; this->first = element;
} }
else else
{ {
next_element->previous->next = element; iterator->current->previous->next = element;
element->previous = next_element->previous; element->previous = iterator->current->previous;
next_element->previous = element; iterator->current->previous = element;
element->next = next_element; element->next = iterator->current;
} }
this->count++; this->count++;
@@ -477,38 +541,48 @@ static status_t insert_before(private_linked_list_t *this, private_linked_list_e
/** /**
* @brief implements function insert_after of linked_list_t * @brief implements function insert_after of linked_list_t
*/ */
static status_t insert_after(private_linked_list_t *this, private_linked_list_element_t * previous_element, void *item) static status_t insert_after(private_linked_list_t *this, private_linked_list_iterator_t * iterator, void *item)
{ {
if ((this == NULL) || (iterator == NULL))
{
return FAILED;
}
if (this->count == 0) if (this->count == 0)
{ {
return FAILED; return FAILED;
} }
private_linked_list_element_t *element =(private_linked_list_element_t *) linked_list_element_create(item); if (iterator->current == NULL)
{
return (this->public.insert_first(&this->public,item));
}
linked_list_element_t *element =(linked_list_element_t *) linked_list_element_create(item);
if (element == NULL) if (element == NULL)
{ {
return FAILED; return FAILED;
} }
if (previous_element->next == NULL) if (iterator->current->next == NULL)
{ {
if (this->last != previous_element) if (this->last != iterator->current)
{ {
element->destroy(element); element->destroy(element);
return FAILED; return FAILED;
} }
previous_element->next = element; iterator->current->next = element;
element->previous = previous_element; element->previous = iterator->current;
this->last = element; this->last = element;
} }
else else
{ {
previous_element->next->previous = element; iterator->current->next->previous = element;
element->next = previous_element->next; element->next = iterator->current->next;
previous_element->next = element; iterator->current->next = element;
element->previous = previous_element; element->previous = iterator->current;
} }
this->count++; this->count++;
@@ -518,39 +592,62 @@ static status_t insert_after(private_linked_list_t *this, private_linked_list_el
/** /**
* @brief implements function remove of linked_list_t * @brief implements function remove of linked_list_t
*/ */
static status_t linked_list_remove(private_linked_list_t *this, private_linked_list_element_t * element) static status_t linked_list_remove(private_linked_list_t *this, private_linked_list_iterator_t * iterator)
{ {
if (this->count == 0) linked_list_element_t *new_current;
if ((this == NULL) || (iterator == NULL) || (iterator->current == NULL))
{ {
return FAILED; return FAILED;
} }
if (element->previous == NULL) if (this->count == 0)
{ {
if (element->next == NULL) return FAILED;
}
/* find out the new iterator position */
if (iterator->current->previous != NULL)
{
new_current = iterator->current->previous;
}
else if (iterator->current->next != NULL)
{
new_current = iterator->current->next;
}
else
{
new_current = NULL;
}
/* now delete the entry :-) */
if (iterator->current->previous == NULL)
{
if (iterator->current->next == NULL)
{ {
this->first = NULL; this->first = NULL;
this->last = NULL; this->last = NULL;
} }
else else
{ {
element->next->previous = NULL; iterator->current->next->previous = NULL;
this->first = element->next; this->first = iterator->current->next;
} }
} }
else if (element->next == NULL) else if (iterator->current->next == NULL)
{ {
element->previous->next = NULL; iterator->current->previous->next = NULL;
this->last = element->previous; this->last = iterator->current->previous;
} }
else else
{ {
element->previous->next = element->next; iterator->current->previous->next = iterator->current->next;
element->next->previous = element->previous; iterator->current->next->previous = iterator->current->previous;
} }
this->count--; this->count--;
element->destroy(element); iterator->current->destroy(iterator->current);
/* set the new iterator position */
iterator->current = new_current;
return SUCCESS; return SUCCESS;
} }
@@ -559,6 +656,11 @@ static status_t linked_list_remove(private_linked_list_t *this, private_linked_l
*/ */
static status_t linked_list_destroy(private_linked_list_t *this) static status_t linked_list_destroy(private_linked_list_t *this)
{ {
if (this == NULL)
{
return FAILED;
}
/* Remove all list items before destroying list */ /* Remove all list items before destroying list */
while (this->count > 0) while (this->count > 0)
{ {
@@ -588,9 +690,9 @@ linked_list_t *linked_list_create()
this->public.get_last = (status_t (*) (linked_list_t *linked_list, void **item)) get_last; this->public.get_last = (status_t (*) (linked_list_t *linked_list, void **item)) get_last;
this->public.insert_first = (status_t (*) (linked_list_t *linked_list, void *item)) insert_first; this->public.insert_first = (status_t (*) (linked_list_t *linked_list, void *item)) insert_first;
this->public.insert_last = (status_t (*) (linked_list_t *linked_list, void *item)) insert_last; this->public.insert_last = (status_t (*) (linked_list_t *linked_list, void *item)) insert_last;
this->public.insert_before = (status_t (*) (linked_list_t *linked_list, linked_list_element_t * element, void *item)) insert_before; this->public.insert_before = (status_t (*) (linked_list_t *linked_list, linked_list_iterator_t * element, void *item)) insert_before;
this->public.insert_after = (status_t (*) (linked_list_t *linked_list, linked_list_element_t * element, void *item)) insert_after; this->public.insert_after = (status_t (*) (linked_list_t *linked_list, linked_list_iterator_t * element, void *item)) insert_after;
this->public.remove = (status_t (*) (linked_list_t *linked_list, linked_list_element_t * element)) linked_list_remove; this->public.remove = (status_t (*) (linked_list_t *linked_list, linked_list_iterator_t * element)) linked_list_remove;
this->public.remove_first = (status_t (*) (linked_list_t *linked_list, void **item)) remove_first; this->public.remove_first = (status_t (*) (linked_list_t *linked_list, void **item)) remove_first;
this->public.remove_last = (status_t (*) (linked_list_t *linked_list, void **item)) remove_last; this->public.remove_last = (status_t (*) (linked_list_t *linked_list, void **item)) remove_last;
this->public.destroy = (status_t (*) (linked_list_t *linked_list)) linked_list_destroy; this->public.destroy = (status_t (*) (linked_list_t *linked_list)) linked_list_destroy;
+38 -56
View File
@@ -29,32 +29,6 @@
#include "types.h" #include "types.h"
/**
* @brief Element of the linked_list.
*
* This element holds a pointer to the value of the list item itself.
*/
typedef struct linked_list_element_s linked_list_element_t;
struct linked_list_element_s {
/**
* value of a list item
*/
void *value;
};
/**
* @brief Creates an empty linked list object
*
* @param[in] value value of item to be set
*
* @warning only the pointer to the value is stored
*
* @return linked_list_element object
*/
linked_list_element_t *linked_list_element_create(void *value);
/** /**
* @brief Iterator for a linked list * @brief Iterator for a linked list
* *
@@ -70,25 +44,24 @@ struct linked_list_iterator_s {
* @brief returns TRUE if more elements are available * @brief returns TRUE if more elements are available
* *
* @param this calling object * @param this calling object
* @param[out] has_next if more elements are avaiable TRUE is set, FALSE otherwise * @return if more elements are avaiable TRUE, FALSE otherwise
* @returns SUCCESS if succeeded, FAILED otherwise
*/ */
status_t (*has_next) (linked_list_iterator_t *this, bool * has_next); bool (*has_next) (linked_list_iterator_t *this);
/** /**
* @brief returns the current element at the iterator position * @brief returns the current value at the iterator position
* *
* @param this calling object * @param this calling object
* @param[out] element element is set to the current element in iterator * @param[out] value value is set to the current value at iterator position
* @returns SUCCESS if succeeded, FAILED otherwise * @return SUCCESS if succeeded, FAILED otherwise
*/ */
status_t (*current) (linked_list_iterator_t *this, linked_list_element_t **element); status_t (*current) (linked_list_iterator_t *this, void **value);
/** /**
* @brief Resets a linked_list_iterator object * @brief Resets a linked_list_iterator object
* *
* @param this calling object * @param this calling object
* @returns SUCCESS if succeeded, FAILED otherwise * @return SUCCESS if succeeded, FAILED otherwise
*/ */
status_t (*reset) (linked_list_iterator_t *this); status_t (*reset) (linked_list_iterator_t *this);
@@ -96,7 +69,7 @@ struct linked_list_iterator_s {
* @brief Destroys a linked_list_iterator object * @brief Destroys a linked_list_iterator object
* *
* @param this calling object * @param this calling object
* @returns SUCCESS if succeeded, FAILED otherwise * @return SUCCESS if succeeded, FAILED otherwise
*/ */
status_t (*destroy) (linked_list_iterator_t *this); status_t (*destroy) (linked_list_iterator_t *this);
}; };
@@ -120,7 +93,7 @@ struct linked_list_s {
* *
* @param linked_list calling object * @param linked_list calling object
* @param[in] count place where the count is written * @param[in] count place where the count is written
* @returns SUCCESS if succeeded, FAILED otherwise * @return SUCCESS if succeeded, FAILED otherwise
*/ */
status_t (*get_count) (linked_list_t *linked_list, int *count); status_t (*get_count) (linked_list_t *linked_list, int *count);
@@ -132,7 +105,7 @@ struct linked_list_s {
* @param linked_list calling object * @param linked_list calling object
* @param[out] iterator place where the iterator is written * @param[out] iterator place where the iterator is written
* @param[in] forward iterator direction (TRUE: front to end) * @param[in] forward iterator direction (TRUE: front to end)
* @returns SUCCESS if succeeded, FAILED otherwise * @return SUCCESS if succeeded, FAILED otherwise
*/ */
status_t (*create_iterator) (linked_list_t *linked_list, linked_list_iterator_t **iterator,bool forward); status_t (*create_iterator) (linked_list_t *linked_list, linked_list_iterator_t **iterator,bool forward);
@@ -141,45 +114,54 @@ struct linked_list_s {
* *
* @param linked_list calling object * @param linked_list calling object
* @param[in] item value to insert in list * @param[in] item value to insert in list
* @returns SUCCESS if succeeded, FAILED otherwise * @return SUCCESS if succeeded, FAILED otherwise
*/ */
status_t (*insert_first) (linked_list_t *linked_list, void *item); status_t (*insert_first) (linked_list_t *linked_list, void *item);
/** /**
* @brief inserts a new item before the given element * @brief inserts a new item before the given iterator position
*
* The iterator position is not changed after inserting
* *
* @param linked_list calling object * @param linked_list calling object
* @param element new element is inserted before this element * @param iterator new element is inserted before this iterator
* @param[in] item value to insert in list * @param[in] item value to insert in list
* @returns SUCCESS if succeeded, FAILED otherwise * @return SUCCESS if succeeded, FAILED otherwise
*/ */
status_t (*insert_before) (linked_list_t *linked_list, linked_list_element_t *element, void *item); status_t (*insert_before) (linked_list_t *linked_list, linked_list_iterator_t *iterator, void *item);
/** /**
* @brief inserts a new item after the given element * @brief inserts a new item after the given iterator position
*
* The iterator position is not changed after inserting
* *
* @param linked_list calling object * @param linked_list calling object
* @param element new element is inserted after this element * @param iterator new element is inserted after this iterator
* @param[in] item value to insert in list * @param[in] item value to insert in list
* @returns SUCCESS if succeeded, FAILED otherwise * @return SUCCESS if succeeded, FAILED otherwise
*/ */
status_t (*insert_after) (linked_list_t *linked_list, linked_list_element_t *element, void *item); status_t (*insert_after) (linked_list_t *linked_list, linked_list_iterator_t *iterator, void *item);
/** /**
* @brief removes an element from list * @brief removes an element from list at the given iterator position
*
* The position of the iterator is set in the following order:
* - to the item before, if available
* - otherwise to the item after, if available
* - otherwise it gets reseted
* *
* @param linked_list calling object * @param linked_list calling object
* @param element element to remove * @param iterator iterator holding the position of the element to remove
* @returns SUCCESS if succeeded, FAILED otherwise * @return SUCCESS if succeeded, FAILED otherwise
*/ */
status_t (*remove) (linked_list_t *linked_list, linked_list_element_t *element); status_t (*remove) (linked_list_t *linked_list, linked_list_iterator_t *iterator);
/** /**
* @brief removes the first item in the list and returns its value * @brief removes the first item in the list and returns its value
* *
* @param linked_list calling object * @param linked_list calling object
* @param[in] item returned value of first item * @param[in] item returned value of first item
* @returns SUCCESS if succeeded, FAILED otherwise * @return SUCCESS if succeeded, FAILED otherwise
*/ */
status_t (*remove_first) (linked_list_t *linked_list, void **item); status_t (*remove_first) (linked_list_t *linked_list, void **item);
@@ -188,7 +170,7 @@ struct linked_list_s {
* *
* @param linked_list calling object * @param linked_list calling object
* @param[out] item returned value of first item * @param[out] item returned value of first item
* @returns SUCCESS if succeeded, FAILED otherwise * @return SUCCESS if succeeded, FAILED otherwise
*/ */
status_t (*get_first) (linked_list_t *linked_list, void **item); status_t (*get_first) (linked_list_t *linked_list, void **item);
@@ -197,7 +179,7 @@ struct linked_list_s {
* *
* @param linked_list calling object * @param linked_list calling object
* @param[in] item value to insert into list * @param[in] item value to insert into list
* @returns SUCCESS if succeeded, FAILED otherwise * @return SUCCESS if succeeded, FAILED otherwise
*/ */
status_t (*insert_last) (linked_list_t *linked_list, void *item); status_t (*insert_last) (linked_list_t *linked_list, void *item);
@@ -206,7 +188,7 @@ struct linked_list_s {
* *
* @param linked_list calling object * @param linked_list calling object
* @param[out] item returned value of last item * @param[out] item returned value of last item
* @returns SUCCESS if succeeded, FAILED otherwise * @return SUCCESS if succeeded, FAILED otherwise
*/ */
status_t (*remove_last) (linked_list_t *linked_list, void **item); status_t (*remove_last) (linked_list_t *linked_list, void **item);
@@ -215,7 +197,7 @@ struct linked_list_s {
* *
* @param linked_list calling object * @param linked_list calling object
* @param[out] item returned value of last item * @param[out] item returned value of last item
* @returns SUCCESS if succeeded, FAILED otherwise * @return SUCCESS if succeeded, FAILED otherwise
*/ */
status_t (*get_last) (linked_list_t *linked_list, void **item); status_t (*get_last) (linked_list_t *linked_list, void **item);
@@ -228,7 +210,7 @@ struct linked_list_s {
* memory leaks! * memory leaks!
* *
* @param linked_list calling object * @param linked_list calling object
* @returns SUCCESS if succeeded, FAILED otherwise * @return SUCCESS if succeeded, FAILED otherwise
*/ */
status_t (*destroy) (linked_list_t *linked_list); status_t (*destroy) (linked_list_t *linked_list);
}; };
+54 -60
View File
@@ -105,8 +105,7 @@ void test_linked_list(tester_t *tester)
*/ */
void test_linked_list_iterator(tester_t *tester) void test_linked_list_iterator(tester_t *tester)
{ {
bool has_next; void * value;
linked_list_element_t * element;
linked_list_t *linked_list = linked_list_create(); linked_list_t *linked_list = linked_list_create();
linked_list->insert_first(linked_list,"one"); linked_list->insert_first(linked_list,"one");
@@ -121,57 +120,45 @@ void test_linked_list_iterator(tester_t *tester)
tester->assert_true(tester,(linked_list->create_iterator(linked_list,&iterator,TRUE) == SUCCESS), "create_iterator for it 1 call check"); tester->assert_true(tester,(linked_list->create_iterator(linked_list,&iterator,TRUE) == SUCCESS), "create_iterator for it 1 call check");
iterator->has_next(iterator,&has_next); tester->assert_true(tester,iterator->has_next(iterator), "it 1 has_next value check");
tester->assert_true(tester,has_next, "it 1 has_next value check"); iterator->current(iterator,&value);
iterator->current(iterator,&element); tester->assert_true(tester,(strcmp((char *) value,"five") == 0), "it 1 current value check");
tester->assert_true(tester,(strcmp((char *) element->value,"five") == 0), "it 1 current value check");
iterator->has_next(iterator,&has_next); tester->assert_true(tester,iterator->has_next(iterator), "it 1 has_next value check");
tester->assert_true(tester,has_next, "it 1 has_next value check"); iterator->current(iterator,&value);
iterator->current(iterator,&element); tester->assert_true(tester,(strcmp((char *) value,"four") == 0), "it 1 current value check");
tester->assert_true(tester,(strcmp((char *) element->value,"four") == 0), "it 1 current value check");
tester->assert_true(tester,(linked_list->create_iterator(linked_list,&iterator2,FALSE) == SUCCESS), "create_iterator for it 2 call check"); tester->assert_true(tester,(linked_list->create_iterator(linked_list,&iterator2,FALSE) == SUCCESS), "create_iterator for it 2 call check");
iterator2->has_next(iterator2,&has_next); tester->assert_true(tester,iterator2->has_next(iterator2), "it 2 has_next value check");
tester->assert_true(tester,has_next, "it 2 has_next value check"); iterator2->current(iterator2,&value);
iterator2->current(iterator2,&element); tester->assert_true(tester,(strcmp((char *) value,"one") == 0), "it 2 current value check");
tester->assert_true(tester,(strcmp((char *) element->value,"one") == 0), "it 2 current value check");
iterator->has_next(iterator,&has_next); tester->assert_true(tester,iterator->has_next(iterator), "it 1 has_next value check");
tester->assert_true(tester,has_next, "it 1 has_next value check"); iterator->current(iterator,&value);
iterator->current(iterator,&element); tester->assert_true(tester,(strcmp((char *) value,"three") == 0), "it 1 current value check");
tester->assert_true(tester,(strcmp((char *) element->value,"three") == 0), "it 1 current value check");
iterator2->has_next(iterator2,&has_next); tester->assert_true(tester,iterator2->has_next(iterator2), "it 2 has_next value check");
tester->assert_true(tester,has_next, "it 2 has_next value check"); iterator2->current(iterator2,&value);
iterator2->current(iterator2,&element); tester->assert_true(tester,(strcmp((char *) value,"two") == 0), "it 2 current value check");
tester->assert_true(tester,(strcmp((char *) element->value,"two") == 0), "it 2 current value check");
iterator->has_next(iterator,&has_next); tester->assert_true(tester,iterator->has_next(iterator), "it 1 has_next value check");
tester->assert_true(tester,has_next, "it 1 has_next value check"); iterator->current(iterator,&value);
iterator->current(iterator,&element); tester->assert_true(tester,(strcmp((char *) value,"two") == 0), "it 1 current value check");
tester->assert_true(tester,(strcmp((char *) element->value,"two") == 0), "it 1 current value check");
iterator2->has_next(iterator2,&has_next); tester->assert_true(tester,iterator2->has_next(iterator2), "it 2 has_next value check");
tester->assert_true(tester,has_next, "it 2 has_next value check"); iterator2->current(iterator2,&value);
iterator2->current(iterator2,&element); tester->assert_true(tester,(strcmp((char *) value,"three") == 0), "it 2 current value check");
tester->assert_true(tester,(strcmp((char *) element->value,"three") == 0), "it 2 current value check");
iterator->has_next(iterator,&has_next); tester->assert_true(tester,iterator->has_next(iterator), "it 1 has_next value check");
tester->assert_true(tester,has_next, "it 1 has_next value check"); iterator->current(iterator,&value);
iterator->current(iterator,&element); tester->assert_true(tester,(strcmp((char *) value,"one") == 0), "it 1 current value check");
tester->assert_true(tester,(strcmp((char *) element->value,"one") == 0), "it 1 current value check");
iterator->has_next(iterator,&has_next); tester->assert_false(tester,iterator->has_next(iterator), "it 1 has_next value check");
tester->assert_false(tester,has_next, "it 1 has_next value check");
iterator2->has_next(iterator2,&has_next); tester->assert_true(tester,iterator2->has_next(iterator2), "it 2 has_next value check");
tester->assert_true(tester,has_next, "it 2 has_next value check"); tester->assert_true(tester,iterator2->has_next(iterator2), "it 2 has_next value check");
iterator2->has_next(iterator2,&has_next); tester->assert_false(tester,iterator2->has_next(iterator2), "it 2 has_next value check");
tester->assert_true(tester,has_next, "it 2 has_next value check");
iterator2->has_next(iterator2,&has_next);
tester->assert_false(tester,has_next, "it 2 has_next value check");
tester->assert_true(tester,(iterator->destroy(iterator) == SUCCESS), "it 1 destroy call check"); tester->assert_true(tester,(iterator->destroy(iterator) == SUCCESS), "it 1 destroy call check");
@@ -185,8 +172,7 @@ void test_linked_list_iterator(tester_t *tester)
*/ */
void test_linked_list_insert_and_remove(tester_t *tester) void test_linked_list_insert_and_remove(tester_t *tester)
{ {
bool has_next; void *value;
linked_list_element_t * element;
linked_list_iterator_t * iterator; linked_list_iterator_t * iterator;
linked_list_t *linked_list = linked_list_create(); linked_list_t *linked_list = linked_list_create();
@@ -201,28 +187,36 @@ void test_linked_list_insert_and_remove(tester_t *tester)
linked_list->create_iterator(linked_list,&iterator,TRUE); linked_list->create_iterator(linked_list,&iterator,TRUE);
iterator->has_next(iterator,&has_next); iterator->has_next(iterator);
iterator->has_next(iterator,&has_next); iterator->has_next(iterator);
iterator->has_next(iterator,&has_next); iterator->has_next(iterator);
iterator->current(iterator,&element); iterator->current(iterator,&value);
tester->assert_true(tester,(strcmp((char *) element->value,"three") == 0), "current value check"); tester->assert_true(tester,(strcmp((char *) value,"three") == 0), "current value check");
tester->assert_true(tester,(linked_list->insert_before(linked_list,element,"before_three") == SUCCESS), "insert_before call check"); tester->assert_true(tester,(linked_list->insert_before(linked_list,iterator,"before_three") == SUCCESS), "insert_before call check");
iterator->current(iterator,&value);
tester->assert_true(tester,(strcmp((char *) value,"three") == 0), "current value check");
tester->assert_true(tester,(linked_list->insert_after(linked_list,element,"after_three") == SUCCESS), "insert_after call check");
tester->assert_true(tester,(linked_list->remove(linked_list,element) == SUCCESS), "remove call check"); tester->assert_true(tester,(linked_list->insert_after(linked_list,iterator,"after_three") == SUCCESS), "insert_after call check");
iterator->current(iterator,&value);
tester->assert_true(tester,(strcmp((char *) value,"three") == 0), "current value check");
tester->assert_true(tester,(linked_list->remove(linked_list,iterator) == SUCCESS), "remove call check");
iterator->current(iterator,&value);
tester->assert_true(tester,(strcmp((char *) value,"before_three") == 0), "current value check");
iterator->reset(iterator); iterator->reset(iterator);
iterator->has_next(iterator,&has_next); iterator->has_next(iterator);
iterator->has_next(iterator,&has_next); iterator->has_next(iterator);
iterator->has_next(iterator,&has_next); iterator->has_next(iterator);
iterator->current(iterator,&element); iterator->current(iterator,&value);
tester->assert_true(tester,(strcmp((char *) element->value,"before_three") == 0), "current value check"); tester->assert_true(tester,(strcmp((char *) value,"before_three") == 0), "current value check");
iterator->has_next(iterator,&has_next); iterator->has_next(iterator);
iterator->current(iterator,&element); iterator->current(iterator,&value);
tester->assert_true(tester,(strcmp((char *) element->value,"after_three") == 0), "current value check"); tester->assert_true(tester,(strcmp((char *) value,"after_three") == 0), "current value check");
iterator->destroy(iterator); iterator->destroy(iterator);