Another linked list bites the dust.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3928 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2022-07-12 01:51:15 +00:00
parent aee99af953
commit 1efc0fd73b
5 changed files with 94 additions and 70 deletions

View File

@ -214,3 +214,28 @@ int tf_util_get_length(JSContext* context, JSValue value)
JS_FreeValue(context, length);
return result;
}
int tf_util_insert_index(const void* key, const void* base, size_t count, size_t size, int (*compare)(const void*, const void*))
{
int lower = 0;
int upper = count;
while (lower < upper && lower < (int)count)
{
int guess = (lower + upper) / 2;
int result = compare(key, ((char*)base) + size * guess);
if (result < 0)
{
upper = guess;
}
else if (result > 0)
{
lower = guess + 1;
}
else
{
return guess;
}
};
return lower;
}