[arrays] Improve bfind() interface

Much more useful now. :)
This commit is contained in:
Behdad Esfahbod 2018-11-24 10:06:13 -05:00
parent 1204a247a5
commit d77a098b73
6 changed files with 43 additions and 29 deletions

View File

@ -650,6 +650,13 @@ template <typename T>
inline hb_array_t<T> hb_array (T *array, unsigned int len) inline hb_array_t<T> hb_array (T *array, unsigned int len)
{ return hb_array_t<T> (array, len); } { return hb_array_t<T> (array, len); }
enum hb_bfind_not_found_t
{
HB_BFIND_NOT_FOUND_DONT_STORE,
HB_BFIND_NOT_FOUND_STORE,
HB_BFIND_NOT_FOUND_STORE_CLOSEST,
};
template <typename Type> template <typename Type>
struct hb_sorted_array_t : hb_array_t<Type> struct hb_sorted_array_t : hb_array_t<Type>
{ {
@ -669,7 +676,9 @@ struct hb_sorted_array_t : hb_array_t<Type>
return bfind (x, &i) ? &this->arrayZ[i] : not_found; return bfind (x, &i) ? &this->arrayZ[i] : not_found;
} }
template <typename T> template <typename T>
inline bool bfind (const T &x, unsigned int *i = nullptr) const inline bool bfind (const T &x, unsigned int *i = nullptr,
hb_bfind_not_found_t not_found = HB_BFIND_NOT_FOUND_DONT_STORE,
unsigned int to_store = (unsigned int) -1) const
{ {
int min = 0, max = (int) this->len - 1; int min = 0, max = (int) this->len - 1;
const Type *array = this->arrayZ; const Type *array = this->arrayZ;
@ -690,9 +699,21 @@ struct hb_sorted_array_t : hb_array_t<Type>
} }
if (i) if (i)
{ {
switch (not_found)
{
case HB_BFIND_NOT_FOUND_DONT_STORE:
break;
case HB_BFIND_NOT_FOUND_STORE:
*i = to_store;
break;
case HB_BFIND_NOT_FOUND_STORE_CLOSEST:
if (max < 0 || (max < (int) this->len && array[max].cmp (x) > 0)) if (max < 0 || (max < (int) this->len && array[max].cmp (x) > 0))
max++; max++;
*i = max; *i = max;
break;
}
} }
return false; return false;
} }

View File

@ -111,14 +111,7 @@ typedef struct OffsetTable
{ {
Tag t; Tag t;
t.set (tag); t.set (tag);
unsigned int i; return tables.bfind (t, table_index, HB_BFIND_NOT_FOUND_STORE, Index::NOT_FOUND_INDEX);
if (tables.bfind (t, &i))
{
if (table_index) *table_index = i;
return true;
}
if (table_index) *table_index = (unsigned) Index::NOT_FOUND_INDEX;
return false;
} }
inline const TableRecord& get_table_by_tag (hb_tag_t tag) const inline const TableRecord& get_table_by_tag (hb_tag_t tag) const
{ {

View File

@ -475,8 +475,10 @@ struct SortedUnsizedArrayOf : UnsizedArrayOf<Type>
inline const Type &bsearch (unsigned int len, const T &x) const inline const Type &bsearch (unsigned int len, const T &x) const
{ return *as_array (len).bsearch (x, &Null (Type)); } { return *as_array (len).bsearch (x, &Null (Type)); }
template <typename T> template <typename T>
inline bool bfind (unsigned int len, const T &x, unsigned int *i = nullptr) const inline bool bfind (unsigned int len, const T &x, unsigned int *i = nullptr,
{ return as_array (len).bfind (x, i); } hb_bfind_not_found_t not_found = HB_BFIND_NOT_FOUND_DONT_STORE,
unsigned int to_store = (unsigned int) -1) const
{ return as_array (len).bfind (x, i, not_found, to_store); }
}; };
@ -782,8 +784,10 @@ struct SortedArrayOf : ArrayOf<Type, LenType>
inline const Type &bsearch (const T &x) const inline const Type &bsearch (const T &x) const
{ return *as_array ().bsearch (x, &Null (Type)); } { return *as_array ().bsearch (x, &Null (Type)); }
template <typename T> template <typename T>
inline bool bfind (const T &x, unsigned int *i = nullptr) const inline bool bfind (const T &x, unsigned int *i = nullptr,
{ return as_array ().bfind (x, i); } hb_bfind_not_found_t not_found = HB_BFIND_NOT_FOUND_DONT_STORE,
unsigned int to_store = (unsigned int) -1) const
{ return as_array ().bfind (x, i, not_found, to_store); }
}; };
/* /*

View File

@ -128,12 +128,7 @@ 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
{ {
if (!this->bfind (tag, index)) return this->bfind (tag, index, HB_BFIND_NOT_FOUND_STORE, Index::NOT_FOUND_INDEX);
{
if (index) *index = Index::NOT_FOUND_INDEX;
return false;
}
return true;
} }
}; };
@ -821,8 +816,7 @@ struct CoverageFormat1
inline unsigned int get_coverage (hb_codepoint_t glyph_id) const inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
{ {
unsigned int i; unsigned int i;
if (!glyphArray.bfind (glyph_id, &i)) glyphArray.bfind (glyph_id, &i, HB_BFIND_NOT_FOUND_STORE, NOT_COVERED);
return NOT_COVERED;
return i; return i;
} }

View File

@ -544,7 +544,7 @@ struct hb_set_t
page_map_t map = {get_major (*codepoint), 0}; page_map_t map = {get_major (*codepoint), 0};
unsigned int i; unsigned int i;
page_map.bfind (map, &i); page_map.bfind (map, &i, HB_BFIND_NOT_FOUND_STORE_CLOSEST);
if (i < page_map.len && page_map[i].major == map.major) if (i < page_map.len && page_map[i].major == map.major)
{ {
if (pages[page_map[i].index].next (codepoint)) if (pages[page_map[i].index].next (codepoint))
@ -575,7 +575,7 @@ struct hb_set_t
page_map_t map = {get_major (*codepoint), 0}; page_map_t map = {get_major (*codepoint), 0};
unsigned int i; unsigned int i;
page_map.bfind (map, &i); page_map.bfind (map, &i, HB_BFIND_NOT_FOUND_STORE_CLOSEST);
if (i < page_map.len && page_map[i].major == map.major) if (i < page_map.len && page_map[i].major == map.major)
{ {
if (pages[page_map[i].index].previous (codepoint)) if (pages[page_map[i].index].previous (codepoint))
@ -670,7 +670,7 @@ struct hb_set_t
{ {
page_map_t map = {get_major (g), pages.len}; page_map_t map = {get_major (g), pages.len};
unsigned int i; unsigned int i;
if (!page_map.bfind (map, &i)) if (!page_map.bfind (map, &i, HB_BFIND_NOT_FOUND_STORE_CLOSEST))
{ {
if (!resize (pages.len + 1)) if (!resize (pages.len + 1))
return nullptr; return nullptr;

View File

@ -239,8 +239,10 @@ struct hb_vector_t
inline const Type *bsearch (const T &x, const Type *not_found = nullptr) const inline const Type *bsearch (const T &x, const Type *not_found = nullptr) const
{ return as_sorted_array ().bsearch (x, not_found); } { return as_sorted_array ().bsearch (x, not_found); }
template <typename T> template <typename T>
inline bool bfind (const T &x, unsigned int *i = nullptr) const inline bool bfind (const T &x, unsigned int *i = nullptr,
{ return as_sorted_array ().bfind (x, i); } hb_bfind_not_found_t not_found = HB_BFIND_NOT_FOUND_DONT_STORE,
unsigned int to_store = (unsigned int) -1) const
{ return as_sorted_array ().bfind (x, i, not_found, to_store); }
}; };