ucx/list.c

changeset 128
b79b1ce438dd
parent 125
fca8efb122de
child 161
1031dd910f8e
equal deleted inserted replaced
127:5418bda21896 128:b79b1ce438dd
116 } 116 }
117 return nl; 117 return nl;
118 } 118 }
119 119
120 UcxList *ucx_list_concat(UcxList *l1, UcxList *l2) { 120 UcxList *ucx_list_concat(UcxList *l1, UcxList *l2) {
121 if (l1 == NULL) { 121 if (l1) {
122 return l2;
123 } else {
124 UcxList *last = ucx_list_last(l1); 122 UcxList *last = ucx_list_last(l1);
125 last->next = l2; 123 last->next = l2;
126 l2->prev = last; 124 l2->prev = last;
127 return l1; 125 return l1;
126 } else {
127 return l2;
128 } 128 }
129 } 129 }
130 130
131 UcxList *ucx_list_last(const UcxList *l) { 131 UcxList *ucx_list_last(const UcxList *l) {
132 if (l == NULL) return NULL; 132 if (l == NULL) return NULL;
152 152
153 UcxList *ucx_list_get(const UcxList *l, int index) { 153 UcxList *ucx_list_get(const UcxList *l, int index) {
154 if (l == NULL) return NULL; 154 if (l == NULL) return NULL;
155 155
156 const UcxList *e = l; 156 const UcxList *e = l;
157 while (e->next != NULL && index > 0) { 157 while (e->next && index > 0) {
158 e = e->next; 158 e = e->next;
159 index--; 159 index--;
160 } 160 }
161 161
162 return (UcxList*)(index == 0 ? e : NULL); 162 return (UcxList*)(index == 0 ? e : NULL);
163 } 163 }
164 164
165 ssize_t ucx_list_find(UcxList *l, void *elem, cmp_func fnc, void *cmpdata) { 165 ssize_t ucx_list_find(UcxList *l, void *elem, cmp_func fnc, void *cmpdata) {
166 ssize_t index = 0; 166 ssize_t index = 0;
167 UCX_FOREACH(e, l) { 167 UCX_FOREACH(e, l) {
168 if (fnc(elem, e->data, cmpdata) == 0) { 168 if (fnc) {
169 return index; 169 if (fnc(elem, e->data, cmpdata) == 0) {
170 return index;
171 }
172 } else {
173 if (elem == e->data) {
174 return index;
175 }
170 } 176 }
171 index++; 177 index++;
172 } 178 }
173 return -1; 179 return -1;
174 } 180 }

mercurial