aboutsummaryrefslogtreecommitdiff
path: root/arraylist.c
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2020-10-31 19:09:25 +0100
committerThomas Schmucker <ts@its1.de>2020-10-31 19:09:25 +0100
commitf320cd9027b9bee52442e8c58d2f3b2281c87bfc (patch)
treed6013852eb8e0b89a65a6aac651174b2593eb054 /arraylist.c
parent8d515c516c019ff51a782923c0a13ba240fda197 (diff)
downloaddata-structures-f320cd9027b9bee52442e8c58d2f3b2281c87bfc.tar.gz
data-structures-f320cd9027b9bee52442e8c58d2f3b2281c87bfc.tar.bz2
data-structures-f320cd9027b9bee52442e8c58d2f3b2281c87bfc.zip
zusätzliches Argument beim binären Suchen...
Diffstat (limited to 'arraylist.c')
-rw-r--r--arraylist.c18
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
212struct bsearchHelper_context { 212struct 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
217static int 218static 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
226bool 227bool
227ArrayList_binarySearch(struct ArrayList *arrayList, void *key, int cmp(void *, const T *), size_t *loc) 228ArrayList_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
289static int 291static int
290my_search_compare(void *key, const T *element) 292my_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