mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-21 12:05:15 +00:00
LibC: Implement bsearch
Nothing fancy, just a simple implementation of bsearch(3).
This commit is contained in:
parent
4ef6be8212
commit
680fd3999e
Notes:
sideshowbarker
2024-07-19 11:02:12 +09:00
Author: https://github.com/willmcpherson2 Commit: https://github.com/SerenityOS/serenity/commit/680fd3999ef Pull-request: https://github.com/SerenityOS/serenity/pull/830
1 changed files with 15 additions and 2 deletions
|
@ -509,8 +509,21 @@ char* mkdtemp(char* pattern)
|
|||
|
||||
void* bsearch(const void* key, const void* base, size_t nmemb, size_t size, int (*compar)(const void*, const void*))
|
||||
{
|
||||
dbgprintf("FIXME(LibC): bsearch(%p, %p, %u, %u, %p)\n", key, base, nmemb, size, compar);
|
||||
ASSERT_NOT_REACHED();
|
||||
int low = 0;
|
||||
int high = nmemb - 1;
|
||||
while (low <= high) {
|
||||
int middle = (low + high) / 2;
|
||||
void* middle_memb = const_cast<char*>((const char*)base + middle * size);
|
||||
int comparison = compar(key, middle_memb);
|
||||
if (comparison < 0)
|
||||
high = middle - 1;
|
||||
else if (comparison > 0)
|
||||
low = middle + 1;
|
||||
else
|
||||
return middle_memb;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
div_t div(int numerator, int denominator)
|
||||
|
|
Loading…
Add table
Reference in a new issue