Checks whether @target exists in @array by performing a binary
search based on the given comparison function @compare_func which
get pointers to items as arguments. If the element is found, %TRUE
is returned and the element’s index is returned in @out_match_index
(if non-%NULL). Otherwise, %FALSE is returned and @out_match_index
is undefined. If @target exists multiple times in @array, the index
of the first instance is returned. This search is using a binary
search, so the @array must absolutely be sorted to return a correct
result (if not, the function may produce false-negative).
This example defines a comparison function and search an element in a #GArray:
|[<!-- language="C" -->
static gint*
cmpint (gconstpointer a, gconstpointer b)
{
const gint *_a = a;
const gint *_b = b;
Checks whether @target exists in @array by performing a binary search based on the given comparison function @compare_func which get pointers to items as arguments. If the element is found, %TRUE is returned and the element’s index is returned in @out_match_index (if non-%NULL). Otherwise, %FALSE is returned and @out_match_index is undefined. If @target exists multiple times in @array, the index of the first instance is returned. This search is using a binary search, so the @array must absolutely be sorted to return a correct result (if not, the function may produce false-negative).
This example defines a comparison function and search an element in a #GArray: |[<!-- language="C" --> static gint* cmpint (gconstpointer a, gconstpointer b) { const gint *_a = a; const gint *_b = b;
return *_a - *_b; } ... gint i = 424242; guint matched_index; gboolean result = g_array_binary_search (garray, &i, cmpint, &matched_index); ... ]|