Mercurial > code
comparison array.c @ 106:06c968b92090
Use pointer to pointer in array_find, better to reuse
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sat, 28 Jan 2012 19:38:59 +0100 |
parents | d534fdcbb319 |
children | 4efd3873a457 |
comparison
equal
deleted
inserted
replaced
105:eac1f370910a | 106:06c968b92090 |
---|---|
269 fn((char *)arr->data + OFFSET(i), udata); | 269 fn((char *)arr->data + OFFSET(i), udata); |
270 } | 270 } |
271 | 271 |
272 /* | 272 /* |
273 * Compare each object with the user supplied function. If the `fn' function | 273 * Compare each object with the user supplied function. If the `fn' function |
274 * returns 1 then the index of the data position is returned and the parameter | 274 * returns 1, 1 is returned and dst points to the correct object, dst should |
275 * data is filled with the array data at the correct index. If the comparison | 275 * be a pointer to a pointer of object, like (int **) for a array of int. |
276 * function nevers returns 1, array_find returns -1. | 276 */ |
277 */ | 277 |
278 | 278 int |
279 int | 279 array_find(const struct array *arr, array_cmp_fn fn, void *dst, void *u) |
280 array_find(const struct array *arr, array_cmp_fn fn, void *data, void *u) | |
281 { | 280 { |
282 int st, i; | 281 int st, i; |
283 | 282 |
284 for (i = st = 0; i < arr->length && st != 1; ++i) | 283 for (i = st = 0; i < arr->length && !st; ++i) |
285 st = fn((char *)arr->data + OFFSET(i), u); | 284 st = fn((char *)arr->data + OFFSET(i), u); |
286 | 285 |
287 if (st) | 286 if (st) |
288 memcpy(data, (char *)arr->data + OFFSET(--i), arr->unit); | 287 *(char **)dst = (char *)arr->data + OFFSET(--i); |
289 else | 288 else |
290 i = -1; | 289 st = 0; |
291 | 290 |
292 return i; | 291 return st; |
293 } | 292 } |
294 | 293 |
295 /* | 294 /* |
296 * Erase every bytes and set the length to 0. | 295 * Erase every bytes and set the length to 0. |
297 */ | 296 */ |