Wed, 04 Jan 2012 14:51:54 +0100
added clone and equals to lists
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@8 | 48 | return nl; |
universe@8 | 49 | } else { |
universe@8 | 50 | UcxDlist *t = ucx_dlist_last(l); |
universe@8 | 51 | t->next = nl; |
universe@8 | 52 | nl->prev = t; |
universe@8 | 53 | return l; |
universe@8 | 54 | } |
universe@7 | 55 | } |
universe@7 | 56 | |
universe@7 | 57 | UcxDlist *ucx_dlist_prepend(UcxDlist *l, void *data) { |
universe@8 | 58 | UcxDlist *nl = ucx_dlist_append(NULL, data); |
universe@8 | 59 | if (nl == NULL) return NULL; |
universe@7 | 60 | |
universe@8 | 61 | if (l != NULL) { |
universe@8 | 62 | nl->next = l; |
universe@8 | 63 | l->prev = nl; |
universe@8 | 64 | } |
universe@8 | 65 | return nl; |
universe@7 | 66 | } |
universe@7 | 67 | |
universe@7 | 68 | UcxDlist *ucx_dlist_concat(UcxDlist *l1, UcxDlist *l2) { |
universe@8 | 69 | if (l1 == NULL) { |
universe@8 | 70 | return l2; |
universe@8 | 71 | } else { |
universe@8 | 72 | UcxDlist *last = ucx_dlist_last(l1); |
universe@8 | 73 | last->next = l2; |
universe@8 | 74 | l2->prev = last; |
universe@8 | 75 | return l1; |
universe@8 | 76 | } |
universe@7 | 77 | } |
universe@7 | 78 | |
universe@7 | 79 | UcxDlist *ucx_dlist_last(UcxDlist *l) { |
universe@7 | 80 | if (l == NULL) return NULL; |
universe@7 | 81 | |
universe@7 | 82 | UcxDlist *e = l; |
universe@7 | 83 | while (e->next != NULL) { |
universe@7 | 84 | e = e->next; |
universe@7 | 85 | } |
universe@7 | 86 | return e; |
universe@7 | 87 | } |
universe@7 | 88 | |
universe@7 | 89 | UcxDlist *ucx_dlist_get(UcxDlist *l, int index) { |
universe@8 | 90 | if (l == NULL) return NULL; |
universe@8 | 91 | |
universe@8 | 92 | UcxDlist *e = l; |
universe@8 | 93 | while (e->next != NULL && index > 0) { |
universe@8 | 94 | e = e->next; |
universe@8 | 95 | index--; |
universe@8 | 96 | } |
universe@7 | 97 | |
universe@8 | 98 | return index == 0 ? e : NULL; |
universe@7 | 99 | } |
universe@7 | 100 | |
universe@7 | 101 | size_t ucx_dlist_size(UcxDlist *l) { |
universe@7 | 102 | if (l == NULL) return 0; |
universe@7 | 103 | |
universe@7 | 104 | UcxDlist *e = l; |
universe@7 | 105 | size_t s = 1; |
universe@7 | 106 | while (e->next != NULL) { |
universe@7 | 107 | e = e->next; |
universe@7 | 108 | s++; |
universe@7 | 109 | } |
universe@7 | 110 | |
universe@7 | 111 | return s; |
universe@7 | 112 | } |
universe@7 | 113 | |
universe@7 | 114 | void ucx_dlist_foreach(UcxDlist *l, ucx_callback fnc, void* data) { |
universe@8 | 115 | UcxDlist *e = l; |
universe@8 | 116 | while (e != NULL) { |
universe@8 | 117 | fnc(e, data); |
universe@8 | 118 | e = e->next; |
universe@8 | 119 | } |
universe@7 | 120 | } |
universe@7 | 121 | |
universe@7 | 122 | /* dlist specific functions */ |
universe@7 | 123 | UcxDlist *ucx_dlist_first(UcxDlist *l) { |
universe@8 | 124 | if (l == NULL) return NULL; |
universe@7 | 125 | |
universe@8 | 126 | UcxDlist *e = l; |
universe@8 | 127 | while (e->prev != NULL) { |
universe@8 | 128 | e = e->prev; |
universe@8 | 129 | } |
universe@8 | 130 | return e; |
olaf@13 | 131 | } |