From fd6df520a1a4aa9cdaa0c2e515f29ba93d2910d1 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 6 Dec 2019 03:00:23 +0000 Subject: [PATCH] [array] Isolate bsearch implementation more --- src/hb-array.hh | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/hb-array.hh b/src/hb-array.hh index 4353772e9..dfd8ed841 100644 --- a/src/hb-array.hh +++ b/src/hb-array.hh @@ -297,9 +297,7 @@ struct hb_sorted_array_t : return bfind (x, &i) ? &this->arrayZ[i] : not_found; } template - 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 + bool bsearch_impl (const T &x, unsigned int *pos) const { int min = 0, max = (int) this->length - 1; const Type *array = this->arrayZ; @@ -313,11 +311,27 @@ struct hb_sorted_array_t : min = mid + 1; else { - if (i) - *i = mid; + *pos = (unsigned) mid; return true; } } + *pos = (unsigned) min; + return false; + } + template + 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 + { + unsigned int pos = 0; + + if (bsearch_impl (x, &pos)) + { + if (i) + *i = pos; + return true; + } + if (i) { switch (not_found) @@ -330,7 +344,7 @@ struct hb_sorted_array_t : break; case HB_BFIND_NOT_FOUND_STORE_CLOSEST: - *i = min; + *i = pos; break; } }