[bsearch] Massage API some more

This commit is contained in:
Behdad Esfahbod 2019-12-07 22:01:13 -06:00
parent 70aa5071d8
commit fa7edf87c9
2 changed files with 25 additions and 23 deletions

View File

@ -649,7 +649,7 @@ _hb_cmp_method (const void *pkey, const void *pval, Ts... ds)
template <typename V, typename K, typename ...Ts>
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<K, V>)
{
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 <typename V, typename K, typename ...Ts>
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
}

View File

@ -297,19 +297,6 @@ struct hb_sorted_array_t :
return bfind (x, &i) ? &this->arrayZ[i] : not_found;
}
template <typename T>
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<T, Type>);
*pos = p - this->arrayZ;
return ret;
}
template <typename T>
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 <typename T>
bool bsearch_impl (const T &x, unsigned *pos) const
{
return hb_bsearch_impl (pos,
x,
this->arrayZ,
this->length,
sizeof (Type),
_hb_cmp_method<T, Type>);
}
};
template <typename T> inline hb_sorted_array_t<T>
hb_sorted_array (T *array, unsigned int length)