[algs] Streamline bsearch() API more towards hb_array_t::bsearch_impl()

Preparing to merge the two finally!
This commit is contained in:
Behdad Esfahbod 2019-12-06 03:35:24 +00:00
parent fd6df520a1
commit bd55d4b49f
9 changed files with 47 additions and 68 deletions

View File

@ -172,11 +172,7 @@ static const hb_aat_feature_mapping_t feature_mappings[] =
const hb_aat_feature_mapping_t *
hb_aat_layout_find_feature_mapping (hb_tag_t tag)
{
return (const hb_aat_feature_mapping_t *) hb_bsearch (&tag,
feature_mappings,
ARRAY_LENGTH (feature_mappings),
sizeof (feature_mappings[0]),
hb_aat_feature_mapping_t::cmp);
return hb_bsearch (tag, feature_mappings, ARRAY_LENGTH (feature_mappings));
}
#endif

View File

@ -39,14 +39,8 @@ struct hb_aat_feature_mapping_t
hb_aat_layout_feature_selector_t selectorToEnable;
hb_aat_layout_feature_selector_t selectorToDisable;
HB_INTERNAL static int cmp (const void *key_, const void *entry_)
{
hb_tag_t key = * (unsigned int *) key_;
const hb_aat_feature_mapping_t * entry = (const hb_aat_feature_mapping_t *) entry_;
return key < entry->otFeatureTag ? -1 :
key > entry->otFeatureTag ? 1 :
0;
}
int cmp (hb_tag_t key) const
{ return key < otFeatureTag ? -1 : key > otFeatureTag ? 1 : 0; }
};
HB_INTERNAL const hb_aat_feature_mapping_t *

View File

@ -636,25 +636,36 @@ hb_in_ranges (T u, T lo1, T hi1, T lo2, T hi2, T lo3, T hi3)
/*
* Sort and search.
*/
template <typename ...Ts>
static inline void *
hb_bsearch (const void *key, const void *base,
size_t nmemb, size_t size,
int (*compar)(const void *_key, const void *_item, Ts... _ds),
template <typename K, typename V, typename ...Ts>
static int
_cmp_method (const void *pkey, const void *pval, Ts... ds)
{
const K& key = * (const K*) pkey;
const V& val = * (const V*) pval;
return val.cmp (key, ds...);
}
template <typename V, typename K, typename ...Ts>
static inline V*
hb_bsearch (const K& key, V* base,
size_t nmemb, size_t stride = sizeof (V),
int (*compar)(const void *_key, const void *_item, Ts... _ds) = _cmp_method<K, V, Ts...>,
Ts... ds)
{
int min = 0, max = (int) nmemb - 1;
while (min <= max)
{
int mid = ((unsigned int) min + (unsigned int) max) / 2;
const void *p = (const void *) (((const char *) base) + (mid * size));
int c = compar (key, p, ds...);
V* p = (V*) (((const char *) base) + (mid * stride));
int c = compar ((const void *) &key, (const void *) p, ds...);
if (c < 0)
max = mid - 1;
else if (c > 0)
min = mid + 1;
else
return (void *) p;
return p;
}
return nullptr;
}

View File

@ -37,12 +37,8 @@
struct hb_ot_language_map_t
{
static int cmp (const void *key, const void *item)
{
unsigned int a = * (unsigned int *) key;
unsigned int b = ((const hb_ot_language_map_t *) item)->code;
return a < b ? -1 : a > b ? +1 : 0;
}
int cmp (unsigned int key) const
{ return key < code ? -1 : key > code ? +1 : 0; }
uint16_t code;
char lang[6];
@ -433,12 +429,7 @@ _hb_ot_name_language_for (unsigned int code,
#ifdef HB_NO_OT_NAME_LANGUAGE
return HB_LANGUAGE_INVALID;
#endif
const hb_ot_language_map_t *entry = (const hb_ot_language_map_t *)
hb_bsearch (&code,
array,
len,
sizeof (array[0]),
hb_ot_language_map_t::cmp);
auto *entry = hb_bsearch (code, array, len);
if (entry)
return hb_language_from_string (entry->lang, -1);

View File

@ -281,16 +281,14 @@ struct name
this->table.destroy ();
}
int get_index (hb_ot_name_id_t name_id,
hb_language_t language,
unsigned int *width=nullptr) const
int get_index (hb_ot_name_id_t name_id,
hb_language_t language,
unsigned int *width=nullptr) const
{
const hb_ot_name_entry_t key = {name_id, {0}, language};
const hb_ot_name_entry_t *entry = (const hb_ot_name_entry_t *)
hb_bsearch (&key,
(const hb_ot_name_entry_t *) this->names,
const hb_ot_name_entry_t *entry = hb_bsearch (key, (const hb_ot_name_entry_t *) this->names,
this->names.length,
sizeof (key),
sizeof (hb_ot_name_entry_t),
_hb_ot_name_entry_cmp_key);
if (!entry)
return -1;

View File

@ -33,19 +33,8 @@ namespace OT {
struct OS2Range
{
static int
cmp (const void *_key, const void *_item)
{
hb_codepoint_t cp = *((hb_codepoint_t *) _key);
const OS2Range *range = (OS2Range *) _item;
if (cp < range->start)
return -1;
else if (cp <= range->end)
return 0;
else
return +1;
}
int cmp (hb_codepoint_t key) const
{ return (key < start) ? -1 : key <= end ? 0 : +1; }
hb_codepoint_t start;
hb_codepoint_t end;
@ -233,10 +222,7 @@ static const OS2Range _hb_os2_unicode_ranges[] =
static unsigned int
_hb_ot_os2_get_unicode_range_bit (hb_codepoint_t cp)
{
OS2Range *range = (OS2Range*) hb_bsearch (&cp, _hb_os2_unicode_ranges,
ARRAY_LENGTH (_hb_os2_unicode_ranges),
sizeof (OS2Range),
OS2Range::cmp);
auto* range = hb_bsearch (cp, _hb_os2_unicode_ranges, ARRAY_LENGTH (_hb_os2_unicode_ranges));
if (range != nullptr)
return range->bit;
return -1;

View File

@ -165,8 +165,7 @@ struct post
}
hb_bytes_t st (name, len);
const uint16_t *gid = (const uint16_t *) hb_bsearch (hb_addressof (st), gids, count,
sizeof (gids[0]), cmp_key, (void *) this);
auto* gid = hb_bsearch (hb_addressof (st), gids, count, sizeof (gids[0]), cmp_key, (void *) this);
if (gid)
{
*glyph = *gid;

View File

@ -77,7 +77,9 @@ struct MVAR
const int *coords, unsigned int coord_count) const
{
const VariationValueRecord *record;
record = (VariationValueRecord *) hb_bsearch (&tag, valuesZ.arrayZ,
record = (VariationValueRecord *) hb_bsearch (tag,
(const VariationValueRecord *)
(const HBUINT8 *) valuesZ,
valueRecordCount, valueRecordSize,
tag_compare);
if (!record)

View File

@ -136,20 +136,22 @@ hb_ucd_compose (hb_unicode_funcs_t *ufuncs HB_UNUSED,
if ((a & 0xFFFFF800u) == 0x0000u && (b & 0xFFFFFF80) == 0x0300u)
{
uint32_t k = HB_CODEPOINT_ENCODE3_11_7_14 (a, b, 0);
uint32_t *v = (uint32_t*) hb_bsearch (&k, _hb_ucd_dm2_u32_map,
ARRAY_LENGTH (_hb_ucd_dm2_u32_map),
sizeof (*_hb_ucd_dm2_u32_map),
_cmp_pair_11_7_14);
const uint32_t *v = hb_bsearch (k,
_hb_ucd_dm2_u32_map,
ARRAY_LENGTH (_hb_ucd_dm2_u32_map),
sizeof (*_hb_ucd_dm2_u32_map),
_cmp_pair_11_7_14);
if (likely (!v)) return false;
u = HB_CODEPOINT_DECODE3_11_7_14_3 (*v);
}
else
{
uint64_t k = HB_CODEPOINT_ENCODE3 (a, b, 0);
uint64_t *v = (uint64_t*) hb_bsearch (&k, _hb_ucd_dm2_u64_map,
ARRAY_LENGTH (_hb_ucd_dm2_u64_map),
sizeof (*_hb_ucd_dm2_u64_map),
_cmp_pair);
const uint64_t *v = hb_bsearch (k,
_hb_ucd_dm2_u64_map,
ARRAY_LENGTH (_hb_ucd_dm2_u64_map),
sizeof (*_hb_ucd_dm2_u64_map),
_cmp_pair);
if (likely (!v)) return false;
u = HB_CODEPOINT_DECODE3_3 (*v);
}