Sun, 15 Jan 2012 14:12:34 +0100
added ucx_dlist_remove and tests + fixed makefile error
test/list_tests.c | file | annotate | diff | comparison | revisions | |
ucx/Makefile | file | annotate | diff | comparison | revisions | |
ucx/dlist.c | file | annotate | diff | comparison | revisions | |
ucx/dlist.h | file | annotate | diff | comparison | revisions | |
ucx/list.c | file | annotate | diff | comparison | revisions |
--- 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; }
--- 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))
--- 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; +}