Rename search() to bsearch() and lsearch()

Such that the complexity of the algorithm used is clear at
call site.
This commit is contained in:
Behdad Esfahbod 2014-06-19 15:39:18 -04:00
parent fb8cc86ff9
commit df554af99d
3 changed files with 14 additions and 13 deletions

View File

@ -837,7 +837,7 @@ struct GenericArrayOf
} }
template <typename SearchType> template <typename SearchType>
inline int search (const SearchType &x) const inline int lsearch (const SearchType &x) const
{ {
unsigned int count = len; unsigned int count = len;
for (unsigned int i = 0; i < count; i++) for (unsigned int i = 0; i < count; i++)
@ -962,7 +962,7 @@ template <typename LenType, typename Type>
struct GenericSortedArrayOf : GenericArrayOf<LenType, Type> struct GenericSortedArrayOf : GenericArrayOf<LenType, Type>
{ {
template <typename SearchType> template <typename SearchType>
inline int search (const SearchType &x) const inline int bsearch (const SearchType &x) const
{ {
/* Hand-coded bsearch here since this is in the hot inner loop. */ /* Hand-coded bsearch here since this is in the hot inner loop. */
int min = 0, max = (int) this->len - 1; int min = 0, max = (int) this->len - 1;

View File

@ -92,7 +92,7 @@ struct CmapSubtableFormat4
glyphIdArray = idRangeOffset + segCount; glyphIdArray = idRangeOffset + segCount;
glyphIdArrayLength = (this->length - 16 - 8 * segCount) / 2; glyphIdArrayLength = (this->length - 16 - 8 * segCount) / 2;
/* Custom bsearch. */ /* Custom two-array bsearch. */
int min = 0, max = (int) segCount - 1; int min = 0, max = (int) segCount - 1;
unsigned int i; unsigned int i;
while (min <= max) while (min <= max)
@ -247,7 +247,7 @@ struct CmapSubtableLongSegmented
private: private:
inline bool get_glyph (hb_codepoint_t codepoint, hb_codepoint_t *glyph) const inline bool get_glyph (hb_codepoint_t codepoint, hb_codepoint_t *glyph) const
{ {
int i = groups.search (codepoint); int i = groups.bsearch (codepoint);
if (i == -1) if (i == -1)
return false; return false;
*glyph = T::group_get_glyph (groups[i], codepoint); *glyph = T::group_get_glyph (groups[i], codepoint);
@ -367,7 +367,10 @@ struct cmap
key.platformID.set (platform_id); key.platformID.set (platform_id);
key.encodingID.set (encoding_id); key.encodingID.set (encoding_id);
int result = encodingRecord.search (key); /* Note: We can use bsearch, but since it has no performance
* implications, we use lsearch and as such accept fonts with
* unsorted subtable list. */
int result = encodingRecord./*bsearch*/lsearch (key);
if (result == -1 || !encodingRecord[result].subtable) if (result == -1 || !encodingRecord[result].subtable)
return NULL; return NULL;
@ -382,10 +385,7 @@ struct cmap
} }
USHORT version; /* Table version number (0). */ USHORT version; /* Table version number (0). */
/* Note: We can use the Sorted array variant, but since it SortedArrayOf<EncodingRecord>
* has no performance implications, we use non-sorted array and
* as such accept fonts with unsorted subtable list. */
/*Sorted*/ArrayOf<EncodingRecord>
encodingRecord; /* Encoding tables. */ encodingRecord; /* Encoding tables. */
public: public:
DEFINE_SIZE_ARRAY (4, encodingRecord); DEFINE_SIZE_ARRAY (4, encodingRecord);

View File

@ -103,7 +103,8 @@ struct RecordArrayOf : SortedArrayOf<Record<Type> > {
} }
inline bool find_index (hb_tag_t tag, unsigned int *index) const inline bool find_index (hb_tag_t tag, unsigned int *index) const
{ {
int i = this->search (tag); /* If we want to allow non-sorted data, we can lsearch(). */
int i = this->/*lsearch*/bsearch (tag);
if (i != -1) { if (i != -1) {
if (index) *index = i; if (index) *index = i;
return true; return true;
@ -631,7 +632,7 @@ struct CoverageFormat1
private: private:
inline unsigned int get_coverage (hb_codepoint_t glyph_id) const inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
{ {
int i = glyphArray.search (glyph_id); int i = glyphArray.bsearch (glyph_id);
ASSERT_STATIC (((unsigned int) -1) == NOT_COVERED); ASSERT_STATIC (((unsigned int) -1) == NOT_COVERED);
return i; return i;
} }
@ -696,7 +697,7 @@ struct CoverageFormat2
private: private:
inline unsigned int get_coverage (hb_codepoint_t glyph_id) const inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
{ {
int i = rangeRecord.search (glyph_id); int i = rangeRecord.bsearch (glyph_id);
if (i != -1) { if (i != -1) {
const RangeRecord &range = rangeRecord[i]; const RangeRecord &range = rangeRecord[i];
return (unsigned int) range.value + (glyph_id - range.start); return (unsigned int) range.value + (glyph_id - range.start);
@ -992,7 +993,7 @@ struct ClassDefFormat2
private: private:
inline unsigned int get_class (hb_codepoint_t glyph_id) const inline unsigned int get_class (hb_codepoint_t glyph_id) const
{ {
int i = rangeRecord.search (glyph_id); int i = rangeRecord.bsearch (glyph_id);
if (i != -1) if (i != -1)
return rangeRecord[i].value; return rangeRecord[i].value;
return 0; return 0;