Commit f75cc683 authored by Christian Würdig's avatar Christian Würdig
Browse files

made code more firm style

added iterator macro

[r8144]
parent 7cfc603a
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
* elements. * elements.
* @author Kimon Hoffmann * @author Kimon Hoffmann
* @date 14.07.2005 * @date 14.07.2005
* @cvs-id $Id$
* @note Until now the code is entirely untested so it probably contains * @note Until now the code is entirely untested so it probably contains
* plenty of errors. * plenty of errors.
*/ */
...@@ -20,128 +21,155 @@ ...@@ -20,128 +21,155 @@
* @param list the list for which to allocate the element. * @param list the list for which to allocate the element.
* @return the newly allocated, uninitialized element. * @return the newly allocated, uninitialized element.
*/ */
static PListElement* allocate_element(PList* list) { static plist_element_t* allocate_element(plist_t* list) {
PListElement* newElement; plist_element_t* newElement;
if (list->firstFreeElement != NULL) {
newElement = list->firstFreeElement; if (list->first_free_element != NULL) {
list->firstFreeElement = newElement->next; newElement = list->first_free_element;
list->first_free_element = newElement->next;
newElement->next = NULL; newElement->next = NULL;
} else { }
else {
newElement = obstack_alloc(&list->obst, sizeof(*newElement)); newElement = obstack_alloc(&list->obst, sizeof(*newElement));
} }
return newElement; return newElement;
} }
PList* plist_new(void) { plist_t* plist_new(void) {
PList* list = xmalloc(sizeof(*list)); plist_t* list = xmalloc(sizeof(*list));
obstack_init(&list->obst); obstack_init(&list->obst);
list->firstElement = NULL; list->first_element = NULL;
list->lastElement = NULL; list->last_element = NULL;
list->firstFreeElement = NULL; list->first_free_element = NULL;
list->elementCount = 0; list->element_count = 0;
return list; return list;
} }
void plist_free(PList* list) { void plist_free(plist_t* list) {
obstack_free(&list->obst, NULL); obstack_free(&list->obst, NULL);
list->firstElement = NULL;
list->lastElement = NULL; list->first_element = NULL;
list->firstFreeElement = NULL; list->last_element = NULL;
list->elementCount = 0; list->first_free_element = NULL;
list->element_count = 0;
xfree(list); xfree(list);
} }
void plist_insert_back(PList* list, void* value) { void plist_insert_back(plist_t* list, void* value) {
if (list->lastElement != NULL) { if (list->last_element != NULL) {
plist_insert_after(list, list->lastElement, value); plist_insert_after(list, list->last_element, value);
} else { }
PListElement* newElement = allocate_element(list); else {
plist_element_t* newElement = allocate_element(list);
newElement->data = value; newElement->data = value;
newElement->prev = NULL; newElement->prev = NULL;
newElement->next = NULL; newElement->next = NULL;
list->firstElement = list->lastElement = newElement; list->first_element = list->last_element = newElement;
list->elementCount = 1; list->element_count = 1;
} }
} }
void plist_insert_front(PList* list, void* value) { void plist_insert_front(plist_t* list, void* value) {
if (list->firstElement != NULL) { if (list->first_element != NULL) {
plist_insert_before(list, list->firstElement, value); plist_insert_before(list, list->first_element, value);
} else { }
PListElement* newElement = allocate_element(list); else {
plist_element_t* newElement = allocate_element(list);
newElement->data = value; newElement->data = value;
newElement->prev = NULL; newElement->prev = NULL;
newElement->next = NULL; newElement->next = NULL;
list->firstElement = list->lastElement = newElement; list->first_element = list->last_element = newElement;
list->elementCount = 1; list->element_count = 1;
} }
} }
void plist_insert_before(PList* list, PListElement* element, void* value) { void plist_insert_before(plist_t* list, plist_element_t* element, void* value) {
PListElement* prevElement; plist_element_t* prevElement;
PListElement* newElement = allocate_element(list); plist_element_t* newElement = allocate_element(list);
newElement->data = value; newElement->data = value;
newElement->next = element; newElement->next = element;
prevElement = element->prev; prevElement = element->prev;
newElement->prev = prevElement; newElement->prev = prevElement;
if (prevElement != NULL) { if (prevElement != NULL) {
prevElement->next = newElement; prevElement->next = newElement;
} else {
list->firstElement = newElement;
} }
else {
list->first_element = newElement;
}
element->prev = newElement; element->prev = newElement;
++list->elementCount; ++list->element_count;
} }
void plist_insert_after(PList* list, PListElement* element, void* value) { void plist_insert_after(plist_t* list, plist_element_t* element, void* value) {
PListElement* nextElement; plist_element_t* nextElement;
PListElement* newElement = allocate_element(list); plist_element_t* newElement = allocate_element(list);
newElement->data = value; newElement->data = value;
newElement->prev = element; newElement->prev = element;
nextElement = element->next; nextElement = element->next;
newElement->next = nextElement; newElement->next = nextElement;
if (nextElement != NULL) { if (nextElement != NULL) {
nextElement->prev = newElement; nextElement->prev = newElement;
} else {
list->lastElement = newElement;
} }
else {
list->last_element = newElement;
}
element->next = newElement; element->next = newElement;
++list->elementCount; ++list->element_count;
} }
void plist_erase(PList* list, PListElement* element) { void plist_erase(plist_t* list, plist_element_t* element) {
PListElement* nextElement = element->next; plist_element_t* nextElement = element->next;
PListElement* prevElement = element->prev; plist_element_t* prevElement = element->prev;
if (nextElement != NULL) { if (nextElement != NULL) {
nextElement->prev = prevElement; nextElement->prev = prevElement;
} else {
list->lastElement = prevElement;
} }
else {
list->last_element = prevElement;
}
if (prevElement != NULL) { if (prevElement != NULL) {
prevElement->next = nextElement; prevElement->next = nextElement;
} else {
list->firstElement = nextElement;
} }
--list->elementCount; else {
list->first_element = nextElement;
}
--list->element_count;
/* Clean the element and prepend it to the free list */ /* Clean the element and prepend it to the free list */
element->prev = NULL; /* The allocation code expects prev to be NULL */ element->prev = NULL; /* The allocation code expects prev to be NULL */
element->next = list->firstFreeElement; element->next = list->first_free_element;
list->firstFreeElement = element; list->first_free_element = element;
} }
void plist_clear(PList* list) { void plist_clear(plist_t* list) {
PListElement* currentElement = list->firstElement; plist_element_t* currentElement = list->first_element;
while (currentElement != NULL) { while (currentElement != NULL) {
currentElement->prev = NULL; currentElement->prev = NULL;
currentElement = currentElement->next; currentElement = currentElement->next;
} }
currentElement = list->lastElement;
currentElement = list->last_element;
if (currentElement != NULL) { if (currentElement != NULL) {
currentElement->next = list->firstFreeElement; currentElement->next = list->first_free_element;
} }
list->firstFreeElement = list->firstElement;
list->firstElement = list->lastElement = 0; list->first_free_element = list->first_element;
list->elementCount = 0; list->first_element = 0;
list->last_element = 0;
list->element_count = 0;
} }
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
* elements. * elements.
* @author Kimon Hoffmann * @author Kimon Hoffmann
* @date 14.07.2005 * @date 14.07.2005
* @cvs-id $Id$
* @note Until now the code is entirely untested so it probably contains * @note Until now the code is entirely untested so it probably contains
* plenty of errors. * plenty of errors.
*/ */
...@@ -15,13 +16,13 @@ ...@@ -15,13 +16,13 @@
#include <stddef.h> #include <stddef.h>
#include "obst.h" #include "obst.h"
typedef struct PListElement PListElement; typedef struct _plist_element plist_element_t;
typedef struct PList PList; typedef struct _plist plist_t;
/** /**
* The Plist data type. * The plist data type.
*/ */
struct PList { struct _plist {
/** /**
* The obstack used for all allocations. * The obstack used for all allocations.
*/ */
...@@ -29,29 +30,29 @@ struct PList { ...@@ -29,29 +30,29 @@ struct PList {
/** /**
* First element in the list. * First element in the list.
*/ */
PListElement* firstElement; plist_element_t *first_element;
/** /**
* Last element in the list. * Last element in the list.
*/ */
PListElement* lastElement; plist_element_t *last_element;
/** /**
* Current number of elements in the list. * Current number of elements in the list.
*/ */
int elementCount; int element_count;
/** /**
* First element in the free list. * First element in the free list.
* Please note that the free list is a single linked list and all back * Please note that the free list is a single linked list and all back
* references are invalid. * references are invalid.
*/ */
PListElement* firstFreeElement; plist_element_t* first_free_element;
}; };
/** /**
* An element in the pointer list. * An element in the pointer list.
*/ */
struct PListElement { struct _plist_element {
PListElement* next; plist_element_t* next;
PListElement* prev; plist_element_t* prev;
void* data; void* data;
}; };
...@@ -59,14 +60,14 @@ struct PListElement { ...@@ -59,14 +60,14 @@ struct PListElement {
* Creates a new pointer list and initializes it. * Creates a new pointer list and initializes it.
* @return The newly created pointer list. * @return The newly created pointer list.
*/ */
PList* plist_new(void); plist_t* plist_new(void);
/** /**
* Frees the passed pointer list. * Frees the passed pointer list.
* After a call to this function all references to the list and any of * After a call to this function all references to the list and any of
* its elements are invalid. * its elements are invalid.
*/ */
void plist_free(PList* list); void plist_free(plist_t* list);
/** /**
* Returns the number of elements in a pointer list. * Returns the number of elements in a pointer list.
...@@ -74,21 +75,21 @@ void plist_free(PList* list); ...@@ -74,21 +75,21 @@ void plist_free(PList* list);
* @return The number of elements in a pointer list. * @return The number of elements in a pointer list.
*/ */
#define plist_count(list) \ #define plist_count(list) \
((list)->elementCount) ((list)->element_count)
/** /**
* Inserts an element at the back of a pointer list. * Inserts an element at the back of a pointer list.
* @param list the pointer list to append the new element to. * @param list the pointer list to append the new element to.
* @param value the element value to append. * @param value the element value to append.
*/ */
void plist_insert_back(PList* list, void* value); void plist_insert_back(plist_t* list, void* value);
/** /**
* Inserts an element at the front of a pointer list. * Inserts an element at the front of a pointer list.
* @param list the pointer list to prepend the new element to. * @param list the pointer list to prepend the new element to.
* @param value the element value to prepend. * @param value the element value to prepend.
*/ */
void plist_insert_front(PList* list, void* value); void plist_insert_front(plist_t* list, void* value);
/** /**
* Inserts an element into a pointer list before the specified element, * Inserts an element into a pointer list before the specified element,
...@@ -98,7 +99,7 @@ void plist_insert_front(PList* list, void* value); ...@@ -98,7 +99,7 @@ void plist_insert_front(PList* list, void* value);
* be inserted. This element must be a part of @p list. * be inserted. This element must be a part of @p list.
* @param value the element value to insert. * @param value the element value to insert.
*/ */
void plist_insert_before(PList* list, PListElement* element, void* value); void plist_insert_before(plist_t* list, plist_element_t* element, void* value);
/** /**
* Inserts an element into a pointer list after the specified element, * Inserts an element into a pointer list after the specified element,
...@@ -108,7 +109,7 @@ void plist_insert_before(PList* list, PListElement* element, void* value); ...@@ -108,7 +109,7 @@ void plist_insert_before(PList* list, PListElement* element, void* value);
* be inserted. This element must be a part of @p list. * be inserted. This element must be a part of @p list.
* @param value the element value to insert. * @param value the element value to insert.
*/ */
void plist_insert_after(PList* list, PListElement* element, void* value); void plist_insert_after(plist_t* list, plist_element_t* element, void* value);
/** /**
* Erases the specified element from the pointer list. * Erases the specified element from the pointer list.
...@@ -116,13 +117,13 @@ void plist_insert_after(PList* list, PListElement* element, void* value); ...@@ -116,13 +117,13 @@ void plist_insert_after(PList* list, PListElement* element, void* value);
* @param element the list element to erase. This element must be a part * @param element the list element to erase. This element must be a part
* of @p list. * of @p list.
*/ */
void plist_erase(PList* list, PListElement* element); void plist_erase(plist_t* list, plist_element_t* element);
/** /**
* Erases all elements from the specified pointer list. * Erases all elements from the specified pointer list.
* @param list the pointer list that should be cleard. * @param list the pointer list that should be cleard.
*/ */
void plist_clear(PList* list); void plist_clear(plist_t* list);
/** /**
* Returns the first element of a pointer list. * Returns the first element of a pointer list.
...@@ -130,7 +131,7 @@ void plist_clear(PList* list); ...@@ -130,7 +131,7 @@ void plist_clear(PList* list);
* @return a pointer to the element or NULL if the list is empty * @return a pointer to the element or NULL if the list is empty
*/ */
#define plist_first(list) \ #define plist_first(list) \
((list)->firstElement) ((list)->first_element)
/** /**
* Returns the last element of a pointer list. * Returns the last element of a pointer list.
...@@ -138,7 +139,7 @@ void plist_clear(PList* list); ...@@ -138,7 +139,7 @@ void plist_clear(PList* list);
* @return a pointer to the element or NULL if the list is empty * @return a pointer to the element or NULL if the list is empty
*/ */
#define plist_last(list) \ #define plist_last(list) \
((list)->lastElement) ((list)->last_element)
/** /**
* Checks whether a pointer list element has a successor or not. * Checks whether a pointer list element has a successor or not.
...@@ -184,4 +185,10 @@ void plist_clear(PList* list); ...@@ -184,4 +185,10 @@ void plist_clear(PList* list);
#define plist_element_get_value(element) \ #define plist_element_get_value(element) \
((element)->data) ((element)->data)
/**
* Convenience macro to iterate over a plist.
*/
#define foreach_plist(list, el) \
for (el = plist_first(list); el; el = plist_element_get_next(el))
#endif /*_PLIST_H_*/ #endif /*_PLIST_H_*/
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment