Function

GLibArraybinary_search

since: 2.62

Declaration [src]

gboolean
g_array_binary_search (
  GArray* array,
  gconstpointer target,
  GCompareFunc compare_func,
  guint* out_match_index
)

Description [src]

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:

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);
...

Available since: 2.62

This function is not directly available to language bindings.

Parameters

array

Type: An array of gpointer

A GArray.

The data is owned by the caller of the function.
target

Type: gconstpointer

A pointer to the item to look up.

The argument can be NULL.
The data is owned by the caller of the function.
compare_func

Type: GCompareFunc

A GCompareFunc used to locate target.

out_match_index

Type: guint*

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

The argument will be set by the function.
The argument can be NULL.

Return value

Type: gboolean

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