ArrayG.binarySearch

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.get

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);
...
class ArrayG
bool
binarySearch

Parameters

target void*

a pointer to the item to look up.

compareFunc GCompareFunc

A GCompareFunc used to locate target.

outMatchIndex uint

return location for the index of the element, if found.

Return Value

Type: bool

TRUE if target is one of the elements of array, FALSE otherwise.

Meta

Since

2.62