From 9d92eaedf1aaf51db420f2acf0b66bce463f1ac1 Mon Sep 17 00:00:00 2001 From: Elfyn McBratney Date: Fri, 9 Feb 2007 00:52:19 +0000 Subject: [PATCH] Improve. --- include/eruntime/list.h | 18 ++++++++++----- src/list.c | 61 ++++++++++++++++++++++++++++++++----------------- 2 files changed, 52 insertions(+), 27 deletions(-) diff --git a/include/eruntime/list.h b/include/eruntime/list.h index c05b502..4d92ba3 100644 --- a/include/eruntime/list.h +++ b/include/eruntime/list.h @@ -3,6 +3,9 @@ # include +# define LIST_FOREACH(list, cur) \ + for ((cur) = (list)->head; (cur); (cur) = (cur)->next) + typedef struct _list_node_struct list_node_t; typedef struct _list_struct @@ -30,12 +33,15 @@ struct _list_node_struct void *data; }; -int8_t list_init (list_t *list); -list_t *list_create (void); -int8_t list_append (list_t *list, void *data); -int8_t list_prepend (list_t *list, void *data); -void list_clear (list_t *list); -void list_free (list_t *list); +typedef void (*list_apply_func_t) (void *, void *); + +int8_t list_init (list_t *list); +list_t *list_create (void); +int8_t list_append (list_t *list, void *data); +int8_t list_prepend (list_t *list, void *data); +int32_t list_apply (list_t *list, list_apply_func_t func, void *arg); +void list_clear (list_t *list); +void list_destroy (list_t *list); #endif /* ! ERUNTIME_LIST_H */ diff --git a/src/list.c b/src/list.c index 09f9f4c..a291e21 100644 --- a/src/list.c +++ b/src/list.c @@ -14,8 +14,9 @@ extern int errno; * list_create() - create a new linked list object. * list_append() - append an item to the head of the list. * list_prepend() - prepend an item to the tail of the list. + * list_apply() - apply a function to all items in a list. * list_clear() - clear (free) all nodes in a list. - * list_free() - free memory associated with list object and its nodes. + * list_destroy() - free memory associated with list object and its nodes. */ /* {{{ int8_t list_init() */ @@ -29,10 +30,6 @@ list_init (list_t *list) return -1; } - /* - * Could do some leak checking here... - */ - list->head = NULL; list->tail = NULL; list->length = 0; @@ -58,7 +55,7 @@ list_create (void) } /* }}} */ -/* {{{ int8_t list_append () */ +/* {{{ int8_t list_append(list, data) */ int8_t list_append (list_t *list, void *data) { @@ -76,12 +73,6 @@ list_append (list_t *list, void *data) new->next = NULL; new->data = data; - /* - * Consistency checks... - */ - assert(list->head && !list->tail); - assert(!list->head && list->tail); - if (!list->head) { list->head = new; @@ -90,14 +81,16 @@ list_append (list_t *list, void *data) else { new->prev = list->tail; + list->tail->next = new; list->tail = new; } + ++list->length; return 0; } /* }}} */ -/* {{{ int8_t list_prepend() */ +/* {{{ int8_t list_prepend(list, data) */ int8_t list_prepend (list_t *list, void *data) { @@ -115,12 +108,6 @@ list_prepend (list_t *list, void *data) new->next = NULL; new->data = data; - /* - * Consistency checks... - */ - assert(list->head && !list->tail); - assert(!list->head && list->tail); - if (!list->head) { list->head = new; @@ -129,13 +116,44 @@ list_prepend (list_t *list, void *data) else { new->next = list->head; + list->head->prev = new; list->head = new; } + ++list->length; return 0; } /* }}} */ +/* {{{ int32_t list_apply(list, func, arg) */ +int32_t +list_apply (list_t *list, list_apply_func_t func, void *arg) +{ + list_node_t *cur = NULL; + int32_t i; + + assert(list && func); + if (!list || !func) + { + errno = EINVAL; + return -1; + } + + /* Empty list? */ + if (!list->head) + return 0; + + i = 0; + LIST_FOREACH (list, cur) + { + func(cur->data, arg); + ++i; + } + + return i; +} +/* }}} */ + /* {{{ void list_clear() */ void list_clear (list_t *list) @@ -153,13 +171,14 @@ list_clear (list_t *list) next = cur->next; sfree(cur); cur = next; + --list->length; } } /* }}} */ -/* {{{ void list_free() */ +/* {{{ void list_destroy() */ void -list_free (list_t *list) +list_destroy (list_t *list) { assert(list); if (!list) -- 2.11.4.GIT