mirror of
https://github.com/libsdl-org/SDL.git
synced 2025-10-08 19:06:26 +00:00
add SDL_bsearch
This commit is contained in:
@@ -865,3 +865,4 @@
|
||||
#define SDL_EncloseFPoints SDL_EncloseFPoints_REAL
|
||||
#define SDL_IntersectFRectAndLine SDL_IntersectFRectAndLine_REAL
|
||||
#define SDL_RenderGetWindow SDL_RenderGetWindow_REAL
|
||||
#define SDL_bsearch SDL_bsearch_REAL
|
||||
|
@@ -936,3 +936,4 @@ SDL_DYNAPI_PROC(void,SDL_UnionFRect,(const SDL_FRect *a, const SDL_FRect *b, SDL
|
||||
SDL_DYNAPI_PROC(SDL_bool,SDL_EncloseFPoints,(const SDL_FPoint *a, int b, const SDL_FRect *c, SDL_FRect *d),(a,b,c,d),return)
|
||||
SDL_DYNAPI_PROC(SDL_bool,SDL_IntersectFRectAndLine,(const SDL_FRect *a, float *b, float *c, float *d, float *e),(a,b,c,d,e),return)
|
||||
SDL_DYNAPI_PROC(SDL_Window*,SDL_RenderGetWindow,(SDL_Renderer *a),(a),return)
|
||||
SDL_DYNAPI_PROC(void*,SDL_bsearch,(const void *a, const void *b, size_t c, size_t d, int (*e)(const void *, const void *)),(a,b,c,d,e),return)
|
||||
|
@@ -534,5 +534,38 @@ extern void qsortG(void *base, size_t nmemb, size_t size,
|
||||
|
||||
#endif /* HAVE_QSORT */
|
||||
|
||||
void *
|
||||
SDL_bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (*compare)(const void *, const void *))
|
||||
{
|
||||
#if defined(HAVE_BSEARCH)
|
||||
return bsearch(key, base, nmemb, size, compare);
|
||||
#else
|
||||
/* SDL's replacement: Taken from the Public Domain C Library (PDCLib):
|
||||
Permission is granted to use, modify, and / or redistribute at will.
|
||||
*/
|
||||
const void *pivot;
|
||||
size_t corr;
|
||||
int rc;
|
||||
|
||||
while (nmemb) {
|
||||
/* algorithm needs -1 correction if remaining elements are an even number. */
|
||||
corr = nmemb % 2;
|
||||
nmemb /= 2;
|
||||
pivot = (const char *)base + (nmemb * size);
|
||||
rc = compare(key, pivot);
|
||||
|
||||
if (rc > 0) {
|
||||
base = (const char *)pivot + size;
|
||||
/* applying correction */
|
||||
nmemb -= (1 - corr);
|
||||
} else if (rc == 0) {
|
||||
return (void *)pivot;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
#endif /* HAVE_BSEARCH */
|
||||
}
|
||||
|
||||
/* vi: set ts=4 sw=4 expandtab: */
|
||||
|
||||
|
Reference in New Issue
Block a user