sdlBsearch function

Pointer<NativeType> sdlBsearch(
  1. Pointer<NativeType> key,
  2. Pointer<NativeType> base,
  3. int nmemb,
  4. int size,
  5. Pointer<NativeFunction<SdlCompareCallback>> compare,
)

Perform a binary search on a previously sorted array.

For example:

typedef struct {
int key;
const char *string;
} data;

int SDLCALL compare(const void *a, const void *b)
{
const data *A = (const data *)a;
const data *B = (const data *)b;

if (A->n < B->n) {
return -1;
} else if (B->n < A->n) {
return 1;
} else {
return 0;
}
}

data values[] = {
{ 1, "first" }, { 2, "second" }, { 3, "third" }
};
data key = { 2, NULL };

data *result = SDL_bsearch(&key, values, SDL_arraysize(values), sizeof(values[0]), compare);

\param key a pointer to a key equal to the element being searched for. \param base a pointer to the start of the array. \param nmemb the number of elements in the array. \param size the size of the elements in the array. \param compare a function used to compare elements in the array. \returns a pointer to the matching element in the array, or NULL if not found.

\threadsafety It is safe to call this function from any thread.

\since This function is available since SDL 3.1.3.

\sa SDL_bsearch_r \sa SDL_qsort

extern SDL_DECLSPEC void * SDLCALL SDL_bsearch(const void *key, const void *base, size_t nmemb, size_t size, SDL_CompareCallback compare)

Implementation

Pointer<NativeType> sdlBsearch(
    Pointer<NativeType> key,
    Pointer<NativeType> base,
    int nmemb,
    int size,
    Pointer<NativeFunction<SdlCompareCallback>> compare) {
  final sdlBsearchLookupFunction = libSdl3.lookupFunction<
      Pointer<NativeType> Function(
          Pointer<NativeType> key,
          Pointer<NativeType> base,
          Uint32 nmemb,
          Uint32 size,
          Pointer<NativeFunction<SdlCompareCallback>> compare),
      Pointer<NativeType> Function(
          Pointer<NativeType> key,
          Pointer<NativeType> base,
          int nmemb,
          int size,
          Pointer<NativeFunction<SdlCompareCallback>> compare)>('SDL_bsearch');
  return sdlBsearchLookupFunction(key, base, nmemb, size, compare);
}