From fa7edf87c99a46d29a9f0d58b2896bc24a43853e Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 7 Dec 2019 22:01:13 -0600 Subject: [PATCH] [bsearch] Massage API some more --- src/hb-algs.hh | 25 +++++++++++++++---------- src/hb-array.hh | 23 ++++++++++------------- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/src/hb-algs.hh b/src/hb-algs.hh index d6f643d05..8ae69af4b 100644 --- a/src/hb-algs.hh +++ b/src/hb-algs.hh @@ -649,7 +649,7 @@ _hb_cmp_method (const void *pkey, const void *pval, Ts... ds) template static inline bool -hb_bsearch_impl (V** out, +hb_bsearch_impl (unsigned *pos, /* Out */ const K& key, V* base, size_t nmemb, size_t stride, int (*compar)(const void *_key, const void *_item, Ts... _ds), @@ -672,14 +672,11 @@ hb_bsearch_impl (V** out, min = mid + 1; else { - *out = p; + *pos = mid; return true; } } -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wcast-align" - *out = (V*) (((const char *) base) + (min * stride)); -#pragma GCC diagnostic pop + *pos = min; return false; } @@ -689,8 +686,12 @@ hb_bsearch (const K& key, V* base, size_t nmemb, size_t stride = sizeof (V), int (*compar)(const void *_key, const void *_item) = _hb_cmp_method) { - V* p; - return hb_bsearch_impl (&p, key, base, nmemb, stride, compar) ? p : nullptr; + unsigned pos; +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wcast-align" + return hb_bsearch_impl (&pos, key, base, nmemb, stride, compar) ? + (V*) (((const char *) base) + (pos * stride)) : nullptr; +#pragma GCC diagnostic pop } template static inline V* @@ -699,8 +700,12 @@ hb_bsearch (const K& key, V* base, int (*compar)(const void *_key, const void *_item, Ts... _ds), Ts... ds) { - V* p; - return hb_bsearch_impl (&p, key, base, nmemb, stride, compar, ds...) ? p : nullptr; + unsigned pos; +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wcast-align" + return hb_bsearch_impl (&pos, key, base, nmemb, stride, compar, ds...) ? + (V*) (((const char *) base) + (pos * stride)) : nullptr; +#pragma GCC diagnostic pop } diff --git a/src/hb-array.hh b/src/hb-array.hh index cca6f5165..cbd6485ac 100644 --- a/src/hb-array.hh +++ b/src/hb-array.hh @@ -297,19 +297,6 @@ struct hb_sorted_array_t : return bfind (x, &i) ? &this->arrayZ[i] : not_found; } template - bool bsearch_impl (const T &x, unsigned *pos) const - { - Type* p; - bool ret = hb_bsearch_impl (&p, - x, - this->arrayZ, - this->length, - sizeof (Type), - _hb_cmp_method); - *pos = p - this->arrayZ; - return ret; - } - 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 @@ -341,6 +328,16 @@ struct hb_sorted_array_t : } return false; } + template + bool bsearch_impl (const T &x, unsigned *pos) const + { + return hb_bsearch_impl (pos, + x, + this->arrayZ, + this->length, + sizeof (Type), + _hb_cmp_method); + } }; template inline hb_sorted_array_t hb_sorted_array (T *array, unsigned int length)