Align lfind() API to match bfind()

This commit is contained in:
Behdad Esfahbod 2021-07-22 11:27:33 -07:00
parent f54f5c1b16
commit 03cd9c5cba
6 changed files with 48 additions and 25 deletions

View File

@ -36,6 +36,14 @@
template <typename Type> template <typename Type>
struct hb_sorted_array_t; struct hb_sorted_array_t;
enum hb_not_found_t
{
HB_NOT_FOUND_DONT_STORE,
HB_NOT_FOUND_STORE,
HB_NOT_FOUND_STORE_CLOSEST,
};
template <typename Type> template <typename Type>
struct hb_array_t : hb_iter_with_fallback_t<hb_array_t<Type>, Type&> struct hb_array_t : hb_iter_with_fallback_t<hb_array_t<Type>, Type&>
{ {
@ -139,7 +147,9 @@ struct hb_array_t : hb_iter_with_fallback_t<hb_array_t<Type>, Type&>
return lfind (x, &i) ? &this->arrayZ[i] : not_found; return lfind (x, &i) ? &this->arrayZ[i] : not_found;
} }
template <typename T> template <typename T>
bool lfind (const T &x, unsigned *pos = nullptr) const bool lfind (const T &x, unsigned *pos = nullptr,
hb_not_found_t not_found = HB_NOT_FOUND_DONT_STORE,
unsigned int to_store = (unsigned int) -1) const
{ {
for (unsigned i = 0; i < length; ++i) for (unsigned i = 0; i < length; ++i)
if (hb_equal (x, this->arrayZ[i])) if (hb_equal (x, this->arrayZ[i]))
@ -149,6 +159,22 @@ struct hb_array_t : hb_iter_with_fallback_t<hb_array_t<Type>, Type&>
return true; return true;
} }
if (pos)
{
switch (not_found)
{
case HB_NOT_FOUND_DONT_STORE:
break;
case HB_NOT_FOUND_STORE:
*pos = to_store;
break;
case HB_NOT_FOUND_STORE_CLOSEST:
*pos = length;
break;
}
}
return false; return false;
} }
@ -266,13 +292,6 @@ template <typename T, unsigned int length_> inline hb_array_t<T>
hb_array (T (&array_)[length_]) hb_array (T (&array_)[length_])
{ return hb_array_t<T> (array_); } { return hb_array_t<T> (array_); }
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 : struct hb_sorted_array_t :
hb_iter_t<hb_sorted_array_t<Type>, Type&>, hb_iter_t<hb_sorted_array_t<Type>, Type&>,
@ -323,7 +342,7 @@ struct hb_sorted_array_t :
} }
template <typename T> template <typename T>
bool bfind (const T &x, unsigned int *i = nullptr, bool bfind (const T &x, unsigned int *i = nullptr,
hb_bfind_not_found_t not_found = HB_BFIND_NOT_FOUND_DONT_STORE, hb_not_found_t not_found = HB_NOT_FOUND_DONT_STORE,
unsigned int to_store = (unsigned int) -1) const unsigned int to_store = (unsigned int) -1) const
{ {
unsigned pos; unsigned pos;
@ -339,14 +358,14 @@ struct hb_sorted_array_t :
{ {
switch (not_found) switch (not_found)
{ {
case HB_BFIND_NOT_FOUND_DONT_STORE: case HB_NOT_FOUND_DONT_STORE:
break; break;
case HB_BFIND_NOT_FOUND_STORE: case HB_NOT_FOUND_STORE:
*i = to_store; *i = to_store;
break; break;
case HB_BFIND_NOT_FOUND_STORE_CLOSEST: case HB_NOT_FOUND_STORE_CLOSEST:
*i = pos; *i = pos;
break; break;
} }

View File

@ -102,7 +102,7 @@ typedef struct OpenTypeOffsetTable
{ {
Tag t; Tag t;
t = tag; t = tag;
return tables.bfind (t, table_index, HB_BFIND_NOT_FOUND_STORE, Index::NOT_FOUND_INDEX); return tables.bfind (t, table_index, HB_NOT_FOUND_STORE, Index::NOT_FOUND_INDEX);
} }
const TableRecord& get_table_by_tag (hb_tag_t tag) const const TableRecord& get_table_by_tag (hb_tag_t tag) const
{ {

View File

@ -468,8 +468,10 @@ struct UnsizedArrayOf
const Type &lsearch (unsigned int len, const T &x, const Type &not_found = Null (Type)) const const Type &lsearch (unsigned int len, const T &x, const Type &not_found = Null (Type)) const
{ return *as_array (len).lsearch (x, &not_found); } { return *as_array (len).lsearch (x, &not_found); }
template <typename T> template <typename T>
bool lfind (unsigned int len, const T &x, unsigned *pos = nullptr) const bool lfind (unsigned int len, const T &x, unsigned int *i = nullptr,
{ return as_array (len).lfind (x, pos); } hb_not_found_t not_found = HB_NOT_FOUND_DONT_STORE,
unsigned int to_store = (unsigned int) -1) const
{ return as_array (len).lfind (x, i, not_found, to_store); }
void qsort (unsigned int len, unsigned int start = 0, unsigned int end = (unsigned int) -1) void qsort (unsigned int len, unsigned int start = 0, unsigned int end = (unsigned int) -1)
{ as_array (len).qsort (start, end); } { as_array (len).qsort (start, end); }
@ -577,7 +579,7 @@ struct SortedUnsizedArrayOf : UnsizedArrayOf<Type>
{ return *as_array (len).bsearch (x, &not_found); } { return *as_array (len).bsearch (x, &not_found); }
template <typename T> template <typename T>
bool bfind (unsigned int len, const T &x, unsigned int *i = nullptr, bool bfind (unsigned int len, const T &x, unsigned int *i = nullptr,
hb_bfind_not_found_t not_found = HB_BFIND_NOT_FOUND_DONT_STORE, hb_not_found_t not_found = HB_NOT_FOUND_DONT_STORE,
unsigned int to_store = (unsigned int) -1) const unsigned int to_store = (unsigned int) -1) const
{ return as_array (len).bfind (x, i, not_found, to_store); } { return as_array (len).bfind (x, i, not_found, to_store); }
}; };
@ -639,8 +641,10 @@ struct ArrayOf
const Type &lsearch (const T &x, const Type &not_found = Null (Type)) const const Type &lsearch (const T &x, const Type &not_found = Null (Type)) const
{ return *as_array ().lsearch (x, &not_found); } { return *as_array ().lsearch (x, &not_found); }
template <typename T> template <typename T>
bool lfind (const T &x, unsigned *pos = nullptr) const bool lfind (const T &x, unsigned int *i = nullptr,
{ return as_array ().lfind (x, pos); } hb_not_found_t not_found = HB_NOT_FOUND_DONT_STORE,
unsigned int to_store = (unsigned int) -1) const
{ return as_array ().lfind (x, i, not_found, to_store); }
void qsort (unsigned int start = 0, unsigned int end = (unsigned int) -1) void qsort (unsigned int start = 0, unsigned int end = (unsigned int) -1)
{ as_array ().qsort (start, end); } { as_array ().qsort (start, end); }
@ -941,7 +945,7 @@ struct SortedArrayOf : ArrayOf<Type, LenType>
{ return *as_array ().bsearch (x, &not_found); } { return *as_array ().bsearch (x, &not_found); }
template <typename T> template <typename T>
bool bfind (const T &x, unsigned int *i = nullptr, bool bfind (const T &x, unsigned int *i = nullptr,
hb_bfind_not_found_t not_found = HB_BFIND_NOT_FOUND_DONT_STORE, hb_not_found_t not_found = HB_NOT_FOUND_DONT_STORE,
unsigned int to_store = (unsigned int) -1) const unsigned int to_store = (unsigned int) -1) const
{ return as_array ().bfind (x, i, not_found, to_store); } { return as_array ().bfind (x, i, not_found, to_store); }
}; };

View File

@ -415,7 +415,7 @@ struct RecordArrayOf : SortedArray16Of<Record<Type>>
} }
bool find_index (hb_tag_t tag, unsigned int *index) const bool find_index (hb_tag_t tag, unsigned int *index) const
{ {
return this->bfind (tag, index, HB_BFIND_NOT_FOUND_STORE, Index::NOT_FOUND_INDEX); return this->bfind (tag, index, HB_NOT_FOUND_STORE, Index::NOT_FOUND_INDEX);
} }
}; };
@ -1393,7 +1393,7 @@ struct CoverageFormat1
unsigned int get_coverage (hb_codepoint_t glyph_id) const unsigned int get_coverage (hb_codepoint_t glyph_id) const
{ {
unsigned int i; unsigned int i;
glyphArray.bfind (glyph_id, &i, HB_BFIND_NOT_FOUND_STORE, NOT_COVERED); glyphArray.bfind (glyph_id, &i, HB_NOT_FOUND_STORE, NOT_COVERED);
return i; return i;
} }

View File

@ -758,7 +758,7 @@ struct hb_set_t
if (unlikely (i >= page_map.length || page_map_array[i].major != major)) if (unlikely (i >= page_map.length || page_map_array[i].major != major))
{ {
page_map.bfind (major, &i, HB_BFIND_NOT_FOUND_STORE_CLOSEST); page_map.bfind (major, &i, HB_NOT_FOUND_STORE_CLOSEST);
if (i >= page_map.length) { if (i >= page_map.length) {
*codepoint = INVALID; *codepoint = INVALID;
return false; return false;
@ -802,7 +802,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, HB_BFIND_NOT_FOUND_STORE_CLOSEST); page_map.bfind (map, &i, HB_NOT_FOUND_STORE_CLOSEST);
if (i < page_map.length && page_map[i].major == map.major) if (i < page_map.length && page_map[i].major == map.major)
{ {
if (pages[page_map[i].index].previous (codepoint)) if (pages[page_map[i].index].previous (codepoint))
@ -933,7 +933,7 @@ struct hb_set_t
{ {
page_map_t map = {get_major (g), pages.length}; page_map_t map = {get_major (g), pages.length};
unsigned int i; unsigned int i;
if (!page_map.bfind (map, &i, HB_BFIND_NOT_FOUND_STORE_CLOSEST)) if (!page_map.bfind (map, &i, HB_NOT_FOUND_STORE_CLOSEST))
{ {
if (!resize (pages.length + 1)) if (!resize (pages.length + 1))
return nullptr; return nullptr;

View File

@ -315,7 +315,7 @@ struct hb_sorted_vector_t : hb_vector_t<Type>
{ return as_array ().bsearch (x, not_found); } { return as_array ().bsearch (x, not_found); }
template <typename T> template <typename T>
bool bfind (const T &x, unsigned int *i = nullptr, bool bfind (const T &x, unsigned int *i = nullptr,
hb_bfind_not_found_t not_found = HB_BFIND_NOT_FOUND_DONT_STORE, hb_not_found_t not_found = HB_NOT_FOUND_DONT_STORE,
unsigned int to_store = (unsigned int) -1) const unsigned int to_store = (unsigned int) -1) const
{ return as_array ().bfind (x, i, not_found, to_store); } { return as_array ().bfind (x, i, not_found, to_store); }
}; };