add bsearch for arrays

Arrays can already be sorted with json_object_array_sort() which uses
qsort() of the standard C library. This adds a counterpart using the
bsearch() from C.
This commit is contained in:
Alexander Dahl
2014-08-21 15:42:50 +02:00
parent d4e81f9ec8
commit 2f5789bdef
4 changed files with 49 additions and 2 deletions

View File

@@ -889,6 +889,23 @@ void json_object_array_sort(struct json_object *jso, int(*sort_fn)(const void *,
array_list_sort(jso->o.c_array, sort_fn);
}
struct json_object* json_object_array_bsearch(
const struct json_object *key,
const struct json_object *jso,
int (*sort_fn)(const void *, const void *) )
{
struct json_object **result;
result = (struct json_object **) array_list_bsearch(
(const void **) &key, jso->o.c_array, sort_fn );
if ( result == NULL ) {
return NULL;
} else {
return *result;
}
}
int json_object_array_length(struct json_object *jso)
{
return array_list_length(jso->o.c_array);