Sun, 15 Jan 2012 14:12:34 +0100
added ucx_dlist_remove and tests + fixed makefile error
universe@4 | 1 | #include "dlist.h" |
universe@4 | 2 | |
universe@18 | 3 | UcxDlist *ucx_dlist_clone(UcxDlist *l, copy_func fnc, void *data) { |
universe@18 | 4 | UcxDlist *ret = NULL; |
universe@18 | 5 | while (l != NULL) { |
universe@18 | 6 | if (fnc != NULL) { |
universe@18 | 7 | ret = ucx_dlist_append(ret, fnc(l->data, data)); |
universe@18 | 8 | } else { |
universe@18 | 9 | ret = ucx_dlist_append(ret, l->data); |
universe@18 | 10 | } |
universe@18 | 11 | l = l->next; |
universe@18 | 12 | } |
universe@18 | 13 | return ret; |
universe@18 | 14 | } |
universe@18 | 15 | |
universe@18 | 16 | int ucx_dlist_equals(UcxDlist *l1, UcxDlist *l2, cmp_func fnc, void* data) { |
universe@18 | 17 | if (l1 == l2) return 1; |
universe@18 | 18 | |
universe@18 | 19 | while (l1 != NULL && l2 != NULL) { |
universe@18 | 20 | if (fnc == NULL) { |
universe@18 | 21 | if (l1->data != l2->data) return 0; |
universe@18 | 22 | } else { |
universe@18 | 23 | if (fnc(l1->data, l2->data, data) != 0) return 0; |
universe@18 | 24 | } |
universe@18 | 25 | l1 = l1->next; |
universe@18 | 26 | l2 = l2->next; |
universe@18 | 27 | } |
universe@18 | 28 | |
universe@18 | 29 | return (l1 == NULL && l2 == NULL); |
universe@18 | 30 | } |
universe@18 | 31 | |
universe@8 | 32 | void ucx_dlist_free(UcxDlist *l) { |
universe@8 | 33 | UcxDlist *e = l, *f; |
universe@8 | 34 | while (e != NULL) { |
universe@8 | 35 | f = e; |
universe@8 | 36 | e = e->next; |
universe@8 | 37 | free(f); |
universe@8 | 38 | } |
universe@8 | 39 | } |
universe@8 | 40 | |
universe@7 | 41 | UcxDlist *ucx_dlist_append(UcxDlist *l, void *data) { |
universe@8 | 42 | UcxDlist *nl = (UcxDlist*) malloc(sizeof(UcxDlist)); |
universe@8 | 43 | if (nl == NULL) return NULL; |
universe@7 | 44 | |
universe@8 | 45 | nl->data = data; |
universe@8 | 46 | nl->next = NULL; |
universe@8 | 47 | if (l == NULL) { |
universe@22 | 48 | nl->prev = NULL; |
universe@8 | 49 | return nl; |
universe@8 | 50 | } else { |
universe@8 | 51 | UcxDlist *t = ucx_dlist_last(l); |
universe@8 | 52 | t->next = nl; |
universe@8 | 53 | nl->prev = t; |
universe@8 | 54 | return l; |
universe@8 | 55 | } |
universe@7 | 56 | } |
universe@7 | 57 | |
universe@7 | 58 | UcxDlist *ucx_dlist_prepend(UcxDlist *l, void *data) { |
universe@8 | 59 | UcxDlist *nl = ucx_dlist_append(NULL, data); |
universe@8 | 60 | if (nl == NULL) return NULL; |
universe@7 | 61 | |
universe@8 | 62 | if (l != NULL) { |
universe@8 | 63 | nl->next = l; |
universe@8 | 64 | l->prev = nl; |
universe@8 | 65 | } |
universe@8 | 66 | return nl; |
universe@7 | 67 | } |
universe@7 | 68 | |
universe@7 | 69 | UcxDlist *ucx_dlist_concat(UcxDlist *l1, UcxDlist *l2) { |
universe@8 | 70 | if (l1 == NULL) { |
universe@8 | 71 | return l2; |
universe@8 | 72 | } else { |
universe@8 | 73 | UcxDlist *last = ucx_dlist_last(l1); |
universe@8 | 74 | last->next = l2; |
universe@8 | 75 | l2->prev = last; |
universe@8 | 76 | return l1; |
universe@8 | 77 | } |
universe@7 | 78 | } |
universe@7 | 79 | |
universe@7 | 80 | UcxDlist *ucx_dlist_last(UcxDlist *l) { |
universe@7 | 81 | if (l == NULL) return NULL; |
universe@7 | 82 | |
universe@7 | 83 | UcxDlist *e = l; |
universe@7 | 84 | while (e->next != NULL) { |
universe@7 | 85 | e = e->next; |
universe@7 | 86 | } |
universe@7 | 87 | return e; |
universe@7 | 88 | } |
universe@7 | 89 | |
universe@7 | 90 | UcxDlist *ucx_dlist_get(UcxDlist *l, int index) { |
universe@8 | 91 | if (l == NULL) return NULL; |
universe@8 | 92 | |
universe@8 | 93 | UcxDlist *e = l; |
universe@8 | 94 | while (e->next != NULL && index > 0) { |
universe@8 | 95 | e = e->next; |
universe@8 | 96 | index--; |
universe@8 | 97 | } |
universe@7 | 98 | |
universe@8 | 99 | return index == 0 ? e : NULL; |
universe@7 | 100 | } |
universe@7 | 101 | |
universe@7 | 102 | size_t ucx_dlist_size(UcxDlist *l) { |
universe@7 | 103 | if (l == NULL) return 0; |
universe@7 | 104 | |
universe@7 | 105 | UcxDlist *e = l; |
universe@7 | 106 | size_t s = 1; |
universe@7 | 107 | while (e->next != NULL) { |
universe@7 | 108 | e = e->next; |
universe@7 | 109 | s++; |
universe@7 | 110 | } |
universe@7 | 111 | |
universe@7 | 112 | return s; |
universe@7 | 113 | } |
universe@7 | 114 | |
universe@7 | 115 | void ucx_dlist_foreach(UcxDlist *l, ucx_callback fnc, void* data) { |
universe@8 | 116 | UcxDlist *e = l; |
universe@22 | 117 | UcxDlist *n; |
universe@8 | 118 | while (e != NULL) { |
universe@22 | 119 | n = e->next; |
universe@8 | 120 | fnc(e, data); |
universe@22 | 121 | e = n; |
universe@8 | 122 | } |
universe@7 | 123 | } |
universe@7 | 124 | |
universe@7 | 125 | /* dlist specific functions */ |
universe@7 | 126 | UcxDlist *ucx_dlist_first(UcxDlist *l) { |
universe@8 | 127 | if (l == NULL) return NULL; |
universe@7 | 128 | |
universe@8 | 129 | UcxDlist *e = l; |
universe@8 | 130 | while (e->prev != NULL) { |
universe@8 | 131 | e = e->prev; |
universe@8 | 132 | } |
universe@8 | 133 | return e; |
olaf@13 | 134 | } |
universe@22 | 135 | |
universe@22 | 136 | UcxDlist *ucx_dlist_remove(UcxDlist *l, UcxDlist *e) { |
universe@22 | 137 | if (e->prev == NULL) { |
universe@22 | 138 | e->next->prev = NULL; |
universe@22 | 139 | l = e->next; |
universe@22 | 140 | } else { |
universe@22 | 141 | e->prev->next = e->next; |
universe@22 | 142 | e->next->prev = e->prev; |
universe@22 | 143 | } |
universe@22 | 144 | free(e); |
universe@22 | 145 | return l; |
universe@22 | 146 | } |