[arrays] Improve bfind() interface
Much more useful now. :)
diff --git a/src/hb-dsalgs.hh b/src/hb-dsalgs.hh
index cc1a1d4..1680bf9 100644
--- a/src/hb-dsalgs.hh
+++ b/src/hb-dsalgs.hh
@@ -650,6 +650,13 @@
inline hb_array_t<T> hb_array (T *array, unsigned int 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>
struct hb_sorted_array_t : hb_array_t<Type>
{
@@ -669,7 +676,9 @@
return bfind (x, &i) ? &this->arrayZ[i] : not_found;
}
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;
const Type *array = this->arrayZ;
@@ -690,9 +699,21 @@
}
if (i)
{
- if (max < 0 || (max < (int) this->len && array[max].cmp (x) > 0))
- max++;
- *i = max;
+ 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))
+ max++;
+ *i = max;
+ break;
+ }
}
return false;
}
diff --git a/src/hb-open-file.hh b/src/hb-open-file.hh
index d3ecc60..af68892 100644
--- a/src/hb-open-file.hh
+++ b/src/hb-open-file.hh
@@ -111,14 +111,7 @@
{
Tag t;
t.set (tag);
- unsigned int i;
- 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;
+ return tables.bfind (t, table_index, HB_BFIND_NOT_FOUND_STORE, Index::NOT_FOUND_INDEX);
}
inline const TableRecord& get_table_by_tag (hb_tag_t tag) const
{
diff --git a/src/hb-open-type.hh b/src/hb-open-type.hh
index c7154c4..1c810e7 100644
--- a/src/hb-open-type.hh
+++ b/src/hb-open-type.hh
@@ -475,8 +475,10 @@
inline const Type &bsearch (unsigned int len, const T &x) const
{ return *as_array (len).bsearch (x, &Null (Type)); }
template <typename T>
- inline bool bfind (unsigned int len, const T &x, unsigned int *i = nullptr) const
- { return as_array (len).bfind (x, i); }
+ inline 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,
+ unsigned int to_store = (unsigned int) -1) const
+ { return as_array (len).bfind (x, i, not_found, to_store); }
};
@@ -782,8 +784,10 @@
inline const Type &bsearch (const T &x) const
{ return *as_array ().bsearch (x, &Null (Type)); }
template <typename T>
- inline bool bfind (const T &x, unsigned int *i = nullptr) const
- { return as_array ().bfind (x, i); }
+ 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
+ { return as_array ().bfind (x, i, not_found, to_store); }
};
/*
diff --git a/src/hb-ot-layout-common.hh b/src/hb-ot-layout-common.hh
index 6c6bcf5..b5af73a 100644
--- a/src/hb-ot-layout-common.hh
+++ b/src/hb-ot-layout-common.hh
@@ -128,12 +128,7 @@
}
inline bool find_index (hb_tag_t tag, unsigned int *index) const
{
- if (!this->bfind (tag, index))
- {
- if (index) *index = Index::NOT_FOUND_INDEX;
- return false;
- }
- return true;
+ return this->bfind (tag, index, HB_BFIND_NOT_FOUND_STORE, Index::NOT_FOUND_INDEX);
}
};
@@ -821,8 +816,7 @@
inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
{
unsigned int i;
- if (!glyphArray.bfind (glyph_id, &i))
- return NOT_COVERED;
+ glyphArray.bfind (glyph_id, &i, HB_BFIND_NOT_FOUND_STORE, NOT_COVERED);
return i;
}
diff --git a/src/hb-set.hh b/src/hb-set.hh
index a464131..8b7a0f3 100644
--- a/src/hb-set.hh
+++ b/src/hb-set.hh
@@ -544,7 +544,7 @@
page_map_t map = {get_major (*codepoint), 0};
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 (pages[page_map[i].index].next (codepoint))
@@ -575,7 +575,7 @@
page_map_t map = {get_major (*codepoint), 0};
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 (pages[page_map[i].index].previous (codepoint))
@@ -670,7 +670,7 @@
{
page_map_t map = {get_major (g), pages.len};
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))
return nullptr;
diff --git a/src/hb-vector.hh b/src/hb-vector.hh
index a8c98d2..787512f 100644
--- a/src/hb-vector.hh
+++ b/src/hb-vector.hh
@@ -239,8 +239,10 @@
inline const Type *bsearch (const T &x, const Type *not_found = nullptr) const
{ return as_sorted_array ().bsearch (x, not_found); }
template <typename T>
- inline bool bfind (const T &x, unsigned int *i = nullptr) const
- { return as_sorted_array ().bfind (x, i); }
+ 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
+ { return as_sorted_array ().bfind (x, i, not_found, to_store); }
};