# HG changeset patch # User Mike Becker # Date 1326633154 -3600 # Node ID 76cdd8209f1f2853b2e9187a8cd276c04cb3dab0 # Parent d599fefc76208a4d543577efca31c0a4ad29f956 added ucx_dlist_remove and tests + fixed makefile error diff -r d599fefc7620 -r 76cdd8209f1f test/list_tests.c --- a/test/list_tests.c Sat Jan 14 13:07:18 2012 +0100 +++ b/test/list_tests.c Sun Jan 15 14:12:34 2012 +0100 @@ -119,6 +119,34 @@ printf(" TODO: test clone with copy\n"); + ucx_dlist_free(dl); + + dl = NULL; + printf(" Test ucx_dlist_remove\n"); + dl = ucx_dlist_append(dl, &v[4]); + dl = ucx_dlist_append(dl, &v[0]); + dl = ucx_dlist_append(dl, &v[3]); + dl = ucx_dlist_remove(dl, dl->next); + if (ucx_dlist_size(dl) == 2) { + if ((*((int*)(dl->data)) != 4) || (*((int*)(dl->next->data)) != 3)) { + fprintf(stderr, "ucx_dlist_remove failed (wrong data)\n"); + r--; + } + } else { + fprintf(stderr, "ucx_dlist_remove failed (wrong size)\n"); + r--; + } + dl = ucx_dlist_remove(dl, dl); + if (ucx_dlist_size(dl) == 1) { + if ((*((int*)(dl->data)) != 3)) { + fprintf(stderr, "ucx_dlist_remove first failed (wrong data)\n"); + r--; + } + } else { + fprintf(stderr, "ucx_dlist_remove first failed (wrong size)\n"); + r--; + } + return r; } diff -r d599fefc7620 -r 76cdd8209f1f ucx/Makefile --- a/ucx/Makefile Sat Jan 14 13:07:18 2012 +0100 +++ b/ucx/Makefile Sun Jan 15 14:12:34 2012 +0100 @@ -29,7 +29,7 @@ include ../$(CONF).mk # list of source files -SRC = list.c dlist.c map.c mempool.c string.o +SRC = list.c dlist.c map.c mempool.c string.c OBJ = $(SRC:%.c=../build/%.$(OBJ_EXT)) diff -r d599fefc7620 -r 76cdd8209f1f ucx/dlist.c --- a/ucx/dlist.c Sat Jan 14 13:07:18 2012 +0100 +++ b/ucx/dlist.c Sun Jan 15 14:12:34 2012 +0100 @@ -45,6 +45,7 @@ nl->data = data; nl->next = NULL; if (l == NULL) { + nl->prev = NULL; return nl; } else { UcxDlist *t = ucx_dlist_last(l); @@ -113,9 +114,11 @@ void ucx_dlist_foreach(UcxDlist *l, ucx_callback fnc, void* data) { UcxDlist *e = l; + UcxDlist *n; while (e != NULL) { + n = e->next; fnc(e, data); - e = e->next; + e = n; } } @@ -129,3 +132,15 @@ } return e; } + +UcxDlist *ucx_dlist_remove(UcxDlist *l, UcxDlist *e) { + if (e->prev == NULL) { + e->next->prev = NULL; + l = e->next; + } else { + e->prev->next = e->next; + e->next->prev = e->prev; + } + free(e); + return l; +} diff -r d599fefc7620 -r 76cdd8209f1f ucx/dlist.h --- a/ucx/dlist.h Sat Jan 14 13:07:18 2012 +0100 +++ b/ucx/dlist.h Sun Jan 15 14:12:34 2012 +0100 @@ -33,6 +33,7 @@ /* dlist specific functions */ UcxDlist *ucx_dlist_first(UcxDlist *l); +UcxDlist *ucx_dlist_remove(UcxDlist *l, UcxDlist *e); #ifdef __cplusplus } diff -r d599fefc7620 -r 76cdd8209f1f ucx/list.c --- a/ucx/list.c Sat Jan 14 13:07:18 2012 +0100 +++ b/ucx/list.c Sun Jan 15 14:12:34 2012 +0100 @@ -110,8 +110,10 @@ void ucx_list_foreach(UcxList *l, ucx_callback fnc, void* data) { UcxList *e = l; + UcxList *n; while (e != NULL) { + n = e->next; fnc(e, data); - e = e->next; + e = n; } }