src/server/ucx/list.c

changeset 16
a9bbd82d2dce
parent 15
cff9c4101dd7
child 23
a2c8fc23c90e
equal deleted inserted replaced
15:cff9c4101dd7 16:a9bbd82d2dce
13 return ret; 13 return ret;
14 } 14 }
15 15
16 int ucx_list_equals(UcxList *l1, UcxList *l2, cmp_func fnc, void* data) { 16 int ucx_list_equals(UcxList *l1, UcxList *l2, cmp_func fnc, void* data) {
17 if (l1 == l2) return 1; 17 if (l1 == l2) return 1;
18 18
19 while (l1 != NULL && l2 != NULL) { 19 while (l1 != NULL && l2 != NULL) {
20 if (fnc == NULL) { 20 if (fnc == NULL) {
21 if (l1->data != l2->data) return 0; 21 if (l1->data != l2->data) return 0;
22 } else { 22 } else {
23 if (fnc(l1->data, l2->data, data) != 0) return 0; 23 if (fnc(l1->data, l2->data, data) != 0) return 0;
24 } 24 }
25 l1 = l1->next; 25 l1 = l1->next;
26 l2 = l2->next; 26 l2 = l2->next;
27 } 27 }
28 28
29 return (l1 == NULL && l2 == NULL); 29 return (l1 == NULL && l2 == NULL);
30 } 30 }
31 31
32 void ucx_list_free(UcxList *l) { 32 void ucx_list_free(UcxList *l) {
33 UcxList *e = l, *f; 33 UcxList *e = l, *f;
39 } 39 }
40 40
41 UcxList *ucx_list_append(UcxList *l, void *data) { 41 UcxList *ucx_list_append(UcxList *l, void *data) {
42 UcxList *nl = (UcxList*) malloc(sizeof(UcxList)); 42 UcxList *nl = (UcxList*) malloc(sizeof(UcxList));
43 if (nl == NULL) return NULL; 43 if (nl == NULL) return NULL;
44 44
45 nl->data = data; 45 nl->data = data;
46 nl->next = NULL; 46 nl->next = NULL;
47 if (l == NULL) { 47 if (l == NULL) {
48 return nl; 48 return nl;
49 } else { 49 } else {
54 } 54 }
55 55
56 UcxList *ucx_list_prepend(UcxList *l, void *data) { 56 UcxList *ucx_list_prepend(UcxList *l, void *data) {
57 UcxList *nl = ucx_list_append(NULL, data); 57 UcxList *nl = ucx_list_append(NULL, data);
58 if (nl == NULL) return NULL; 58 if (nl == NULL) return NULL;
59 59
60 if (l != NULL) { 60 if (l != NULL) {
61 nl->next = l; 61 nl->next = l;
62 } 62 }
63 return nl; 63 return nl;
64 } 64 }
73 } 73 }
74 } 74 }
75 75
76 UcxList *ucx_list_last(UcxList *l) { 76 UcxList *ucx_list_last(UcxList *l) {
77 if (l == NULL) return NULL; 77 if (l == NULL) return NULL;
78 78
79 UcxList *e = l; 79 UcxList *e = l;
80 while (e->next != NULL) { 80 while (e->next != NULL) {
81 e = e->next; 81 e = e->next;
82 } 82 }
83 return e; 83 return e;
89 UcxList *e = l; 89 UcxList *e = l;
90 while (e->next != NULL && index > 0) { 90 while (e->next != NULL && index > 0) {
91 e = e->next; 91 e = e->next;
92 index--; 92 index--;
93 } 93 }
94 94
95 return index == 0 ? e : NULL; 95 return index == 0 ? e : NULL;
96 } 96 }
97 97
98 size_t ucx_list_size(UcxList *l) { 98 size_t ucx_list_size(UcxList *l) {
99 if (l == NULL) return 0; 99 if (l == NULL) return 0;
100 100
101 UcxList *e = l; 101 UcxList *e = l;
102 size_t s = 1; 102 size_t s = 1;
103 while (e->next != NULL) { 103 while (e->next != NULL) {
104 e = e->next; 104 e = e->next;
105 s++; 105 s++;
108 return s; 108 return s;
109 } 109 }
110 110
111 void ucx_list_foreach(UcxList *l, ucx_callback fnc, void* data) { 111 void ucx_list_foreach(UcxList *l, ucx_callback fnc, void* data) {
112 UcxList *e = l; 112 UcxList *e = l;
113 UcxList *n;
113 while (e != NULL) { 114 while (e != NULL) {
115 n = e->next;
114 fnc(e, data); 116 fnc(e, data);
115 e = e->next; 117 e = n;
116 } 118 }
117 } 119 }
120
121 /* list specific functions */
122 UcxList *ucx_list_remove(UcxList *l, UcxList *e) {
123 if (e == l) {
124 l = e->next;
125 free(e);
126 } else {
127 UcxList *f = l;
128 while (f->next != NULL && f->next != e) {
129 f = f->next;
130 }
131 /* perform remove iff this element is found in this list */
132 if (f->next == e) {
133 f->next = e->next;
134 free(e);
135 }
136 }
137 return l;
138 }

mercurial