Wed, 04 Jan 2012 14:51:54 +0100
added clone and equals to lists
universe@10 | 1 | #include "list.h" |
olaf@2 | 2 | |
universe@18 | 3 | UcxList *ucx_list_clone(UcxList *l, copy_func fnc, void *data) { |
universe@18 | 4 | UcxList *ret = NULL; |
universe@18 | 5 | while (l != NULL) { |
universe@18 | 6 | if (fnc != NULL) { |
universe@18 | 7 | ret = ucx_list_append(ret, fnc(l->data, data)); |
universe@18 | 8 | } else { |
universe@18 | 9 | ret = ucx_list_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_list_equals(UcxList *l1, UcxList *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@10 | 32 | void ucx_list_free(UcxList *l) { |
universe@10 | 33 | UcxList *e = l, *f; |
universe@10 | 34 | while (e != NULL) { |
universe@10 | 35 | f = e; |
universe@10 | 36 | e = e->next; |
universe@10 | 37 | free(f); |
universe@10 | 38 | } |
universe@10 | 39 | } |
universe@10 | 40 | |
universe@10 | 41 | UcxList *ucx_list_append(UcxList *l, void *data) { |
universe@10 | 42 | UcxList *nl = (UcxList*) malloc(sizeof(UcxList)); |
universe@10 | 43 | if (nl == NULL) return NULL; |
universe@10 | 44 | |
universe@10 | 45 | nl->data = data; |
universe@10 | 46 | nl->next = NULL; |
universe@10 | 47 | if (l == NULL) { |
universe@10 | 48 | return nl; |
universe@10 | 49 | } else { |
universe@10 | 50 | UcxList *t = ucx_list_last(l); |
universe@10 | 51 | t->next = nl; |
universe@10 | 52 | return l; |
universe@10 | 53 | } |
universe@10 | 54 | } |
universe@10 | 55 | |
universe@10 | 56 | UcxList *ucx_list_prepend(UcxList *l, void *data) { |
universe@10 | 57 | UcxList *nl = ucx_list_append(NULL, data); |
universe@10 | 58 | if (nl == NULL) return NULL; |
universe@10 | 59 | |
universe@10 | 60 | if (l != NULL) { |
universe@10 | 61 | nl->next = l; |
universe@10 | 62 | } |
universe@10 | 63 | return nl; |
universe@10 | 64 | } |
universe@10 | 65 | |
universe@10 | 66 | UcxList *ucx_list_concat(UcxList *l1, UcxList *l2) { |
universe@10 | 67 | if (l1 == NULL) { |
universe@10 | 68 | return l2; |
universe@10 | 69 | } else { |
universe@10 | 70 | UcxList *last = ucx_list_last(l1); |
universe@10 | 71 | last->next = l2; |
universe@10 | 72 | return l1; |
universe@10 | 73 | } |
universe@10 | 74 | } |
universe@10 | 75 | |
universe@10 | 76 | UcxList *ucx_list_last(UcxList *l) { |
universe@10 | 77 | if (l == NULL) return NULL; |
universe@10 | 78 | |
universe@10 | 79 | UcxList *e = l; |
universe@10 | 80 | while (e->next != NULL) { |
universe@10 | 81 | e = e->next; |
universe@10 | 82 | } |
universe@10 | 83 | return e; |
universe@10 | 84 | } |
universe@10 | 85 | |
universe@10 | 86 | UcxList *ucx_list_get(UcxList *l, int index) { |
universe@10 | 87 | if (l == NULL) return NULL; |
universe@10 | 88 | |
universe@10 | 89 | UcxList *e = l; |
universe@10 | 90 | while (e->next != NULL && index > 0) { |
universe@10 | 91 | e = e->next; |
universe@10 | 92 | index--; |
universe@10 | 93 | } |
universe@10 | 94 | |
universe@10 | 95 | return index == 0 ? e : NULL; |
universe@10 | 96 | } |
universe@10 | 97 | |
universe@10 | 98 | size_t ucx_list_size(UcxList *l) { |
universe@10 | 99 | if (l == NULL) return 0; |
universe@10 | 100 | |
universe@10 | 101 | UcxList *e = l; |
universe@10 | 102 | size_t s = 1; |
universe@10 | 103 | while (e->next != NULL) { |
universe@10 | 104 | e = e->next; |
universe@10 | 105 | s++; |
universe@10 | 106 | } |
universe@10 | 107 | |
universe@10 | 108 | return s; |
universe@10 | 109 | } |
universe@10 | 110 | |
universe@10 | 111 | void ucx_list_foreach(UcxList *l, ucx_callback fnc, void* data) { |
universe@10 | 112 | UcxList *e = l; |
universe@10 | 113 | while (e != NULL) { |
universe@10 | 114 | fnc(e, data); |
universe@10 | 115 | e = e->next; |
universe@10 | 116 | } |
universe@10 | 117 | } |