diff options
Diffstat (limited to 'arraylist.c')
| -rw-r--r-- | arraylist.c | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/arraylist.c b/arraylist.c index 2d348f3..982e466 100644 --- a/arraylist.c +++ b/arraylist.c | |||
| @@ -211,7 +211,8 @@ ArrayList_linearSearch(struct ArrayList *arrayList, void *key, int cmp(void *, c | |||
| 211 | 211 | ||
| 212 | struct bsearchHelper_context { | 212 | struct bsearchHelper_context { |
| 213 | void *key; | 213 | void *key; |
| 214 | int (*cmp)(void *, const T *); | 214 | int (*cmp)(void *, const T *, void *); |
| 215 | void *args; | ||
| 215 | }; | 216 | }; |
| 216 | 217 | ||
| 217 | static int | 218 | static int |
| @@ -220,19 +221,20 @@ ArrayList_bsearchHelper(const void *ctx, const void *el) | |||
| 220 | const struct bsearchHelper_context *context = ctx; | 221 | const struct bsearchHelper_context *context = ctx; |
| 221 | const const_T_ptr * element = el; | 222 | const const_T_ptr * element = el; |
| 222 | 223 | ||
| 223 | return context->cmp(context->key, *element); | 224 | return context->cmp(context->key, *element, context->args); |
| 224 | } | 225 | } |
| 225 | 226 | ||
| 226 | bool | 227 | bool |
| 227 | ArrayList_binarySearch(struct ArrayList *arrayList, void *key, int cmp(void *, const T *), size_t *loc) | 228 | ArrayList_binarySearch(struct ArrayList *arrayList, void *key, int cmp(void *, const T *, void *), void *args, size_t *loc) |
| 228 | { | 229 | { |
| 229 | assert(arrayList != NULL); | 230 | assert(arrayList != NULL); |
| 230 | assert(cmp != NULL); | 231 | assert(cmp != NULL); |
| 231 | assert(loc != NULL); | 232 | assert(loc != NULL); |
| 232 | 233 | ||
| 233 | struct bsearchHelper_context context = { | 234 | struct bsearchHelper_context context = { |
| 234 | .key = key, | 235 | .key = key, |
| 235 | .cmp = cmp | 236 | .cmp = cmp, |
| 237 | .args = args | ||
| 236 | }; | 238 | }; |
| 237 | 239 | ||
| 238 | T **pos = bsearch(&context, arrayList->array, arrayList->size, sizeof(arrayList->array[0]), ArrayList_bsearchHelper); | 240 | T **pos = bsearch(&context, arrayList->array, arrayList->size, sizeof(arrayList->array[0]), ArrayList_bsearchHelper); |
| @@ -287,8 +289,10 @@ my_compare(const T *a, const T *b, void *args) | |||
| 287 | } | 289 | } |
| 288 | 290 | ||
| 289 | static int | 291 | static int |
| 290 | my_search_compare(void *key, const T *element) | 292 | my_search_compare(void *key, const T *element, void *args) |
| 291 | { | 293 | { |
| 294 | (void) args; | ||
| 295 | |||
| 292 | int *a = key; | 296 | int *a = key; |
| 293 | 297 | ||
| 294 | if ( *a < *element ) | 298 | if ( *a < *element ) |
| @@ -345,7 +349,7 @@ main(void) | |||
| 345 | 349 | ||
| 346 | size_t loc; | 350 | size_t loc; |
| 347 | int dummy = 44; | 351 | int dummy = 44; |
| 348 | if ( ArrayList_binarySearch(arrayList, &dummy, my_search_compare, &loc) ) { | 352 | if ( ArrayList_binarySearch(arrayList, &dummy, my_search_compare, NULL, &loc) ) { |
| 349 | printf("Gefunden: %zu\n", loc); | 353 | printf("Gefunden: %zu\n", loc); |
| 350 | } | 354 | } |
| 351 | 355 | ||
