Faster binary search

This commit is contained in:
Clownacy 2020-09-18 02:55:45 +01:00
parent 729565b5ca
commit f2c0f94e42

View file

@ -1073,13 +1073,19 @@ static Glyph* GetGlyph(Font *font, unsigned long unicode_value)
size_t left = 0; size_t left = 0;
size_t right = font->total_local_glyphs; size_t right = font->total_local_glyphs;
for (;;) while (right - left >= 2)
{ {
size_t index = (left + right) / 2; const size_t index = (left + right) / 2;
if (font->local_glyphs[index].unicode_value == unicode_value) if (font->local_glyphs[index].unicode_value > unicode_value)
right = index;
else
left = index;
}
if (font->local_glyphs[left].unicode_value == unicode_value)
{ {
const Glyph *local_glyph = &font->local_glyphs[index]; const Glyph *local_glyph = &font->local_glyphs[left];
glyph->unicode_value = local_glyph->unicode_value; glyph->unicode_value = local_glyph->unicode_value;
glyph->width = font->glyph_slot_width; glyph->width = font->glyph_slot_width;
@ -1096,15 +1102,6 @@ static Glyph* GetGlyph(Font *font, unsigned long unicode_value)
return glyph; return glyph;
} }
if (index == left)
break;
if (font->local_glyphs[index].unicode_value < unicode_value)
left = index;
else //if (font->local_glyphs[index].unicode_value > unicode_value)
right = index;
}
#endif #endif
return NULL; return NULL;