From df554af99db390e42d378983bb3fcf583477a1d7 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 19 Jun 2014 15:39:18 -0400 Subject: [PATCH] Rename search() to bsearch() and lsearch() Such that the complexity of the algorithm used is clear at call site. --- src/hb-open-type-private.hh | 4 ++-- src/hb-ot-cmap-table.hh | 14 +++++++------- src/hb-ot-layout-common-private.hh | 9 +++++---- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/hb-open-type-private.hh b/src/hb-open-type-private.hh index c4446ce08..a95afc20f 100644 --- a/src/hb-open-type-private.hh +++ b/src/hb-open-type-private.hh @@ -837,7 +837,7 @@ struct GenericArrayOf } template - inline int search (const SearchType &x) const + inline int lsearch (const SearchType &x) const { unsigned int count = len; for (unsigned int i = 0; i < count; i++) @@ -962,7 +962,7 @@ template struct GenericSortedArrayOf : GenericArrayOf { template - 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. */ int min = 0, max = (int) this->len - 1; diff --git a/src/hb-ot-cmap-table.hh b/src/hb-ot-cmap-table.hh index e21baed4c..b182b53bf 100644 --- a/src/hb-ot-cmap-table.hh +++ b/src/hb-ot-cmap-table.hh @@ -92,7 +92,7 @@ struct CmapSubtableFormat4 glyphIdArray = idRangeOffset + segCount; glyphIdArrayLength = (this->length - 16 - 8 * segCount) / 2; - /* Custom bsearch. */ + /* Custom two-array bsearch. */ int min = 0, max = (int) segCount - 1; unsigned int i; while (min <= max) @@ -247,7 +247,7 @@ struct CmapSubtableLongSegmented private: 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) return false; *glyph = T::group_get_glyph (groups[i], codepoint); @@ -367,7 +367,10 @@ struct cmap key.platformID.set (platform_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) return NULL; @@ -382,10 +385,7 @@ struct cmap } USHORT version; /* Table version number (0). */ - /* Note: We can use the Sorted array variant, but since it - * has no performance implications, we use non-sorted array and - * as such accept fonts with unsorted subtable list. */ - /*Sorted*/ArrayOf + SortedArrayOf encodingRecord; /* Encoding tables. */ public: DEFINE_SIZE_ARRAY (4, encodingRecord); diff --git a/src/hb-ot-layout-common-private.hh b/src/hb-ot-layout-common-private.hh index 688bf6555..3904c2dd6 100644 --- a/src/hb-ot-layout-common-private.hh +++ b/src/hb-ot-layout-common-private.hh @@ -103,7 +103,8 @@ struct RecordArrayOf : SortedArrayOf > { } 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 (index) *index = i; return true; @@ -631,7 +632,7 @@ struct CoverageFormat1 private: 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); return i; } @@ -696,7 +697,7 @@ struct CoverageFormat2 private: 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) { const RangeRecord &range = rangeRecord[i]; return (unsigned int) range.value + (glyph_id - range.start); @@ -992,7 +993,7 @@ struct ClassDefFormat2 private: 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) return rangeRecord[i].value; return 0;