[algs] Simplify hb_qsort: single template, inline all callers

Reduce to one templated hb_qsort, one templated qsort() on
hb_array_t/hb_vector_t, plus the no-arg qsort() that casts
Type::cmp to resolve ambiguity.

Convert all external call sites to lambdas so comparators
inline everywhere.  Remove the void*-arg overload and the
plain function-pointer overload.

225 tests pass, 0 failures.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Behdad EsfahbodandClaude Opus 4.6 committed 2026-03-27 12:50:30 -06:00
1 parent 05762b2fac
commit df433afe6c
6 files changed
+11 -46

No files matched your search

-20
View File
@@ -1450,27 +1450,7 @@ hb_qsort (void *base, size_t nel, size_t width,
sort_r_simple (base, nel, width, compar);
}
static inline void
hb_qsort (void *base, size_t nel, size_t width,
int (*compar)(const void *_a, const void *_b))
{
#if defined(__OPTIMIZE_SIZE__) && !defined(HB_USE_INTERNAL_QSORT)
qsort (base, nel, width, compar);
#else
sort_r_simple (base, nel, width, compar);
#endif
}
static inline void
hb_qsort (void *base, size_t nel, size_t width,
int (*compar)(const void *_a, const void *_b, void *_arg),
void *arg)
{
hb_qsort (base, nel, width,
[compar, arg] (const void *a, const void *b) {
return compar (a, b, arg);
});
}
template <typename T, typename T2, typename T3 = int> static inline void
+1 -10
View File
@@ -219,12 +219,6 @@ struct hb_array_t : hb_iter_with_fallback_t<hb_array_t<Type>, Type&>
return false;
}
hb_sorted_array_t<Type> qsort (int (*cmp_)(const void*, const void*))
{
if (likely (length))
hb_qsort (arrayZ, length, this->get_item_size (), cmp_);
return hb_sorted_array_t<Type> (*this);
}
template <typename Compar>
hb_sorted_array_t<Type> qsort (Compar compar)
{
@@ -234,10 +228,7 @@ struct hb_array_t : hb_iter_with_fallback_t<hb_array_t<Type>, Type&>
}
hb_sorted_array_t<Type> qsort ()
{
//static_assert (hb_enable_if (hb_is_trivially_copy_assignable(Type)), "");
if (likely (length))
hb_qsort (arrayZ, length, this->get_item_size (), Type::cmp);
return hb_sorted_array_t<Type> (*this);
return qsort ((int(*)(const void*, const void*)) Type::cmp);
}
/*
+2 -3
View File
@@ -229,9 +229,8 @@ _hb_cairo_reduce_anchors (float x0, float y0,
*yy1 = y1 - k * q2y;
}
static int
_hb_cairo_cmp_color_stop (const void *p1,
const void *p2)
static inline int
_hb_cairo_cmp_color_stop (const void *p1, const void *p2)
{
const hb_color_stop_t *c1 = (const hb_color_stop_t *) p1;
const hb_color_stop_t *c2 = (const hb_color_stop_t *) p2;
+6 -9
View File
@@ -194,7 +194,12 @@ struct post
for (unsigned int i = 0; i < count; i++)
gids[i] = i;
hb_qsort (gids, count, sizeof (gids[0]), cmp_gids, (void *) this);
hb_qsort (gids, count, sizeof (gids[0]),
[this] (const void *pa, const void *pb) -> int {
uint16_t a = * (const uint16_t *) pa;
uint16_t b = * (const uint16_t *) pb;
return find_glyph_name (b).cmp (find_glyph_name (a));
});
if (unlikely (!gids_sorted_by_name.cmpexch (nullptr, gids)))
{
@@ -235,14 +240,6 @@ struct post
return 0;
}
static int cmp_gids (const void *pa, const void *pb, void *arg)
{
const accelerator_t *thiz = (const accelerator_t *) arg;
uint16_t a = * (const uint16_t *) pa;
uint16_t b = * (const uint16_t *) pb;
return thiz->find_glyph_name (b).cmp (thiz->find_glyph_name (a));
}
static int cmp_key (const void *pk, const void *po, void *arg)
{
const accelerator_t *thiz = (const accelerator_t *) arg;
+1 -1
View File
@@ -869,7 +869,7 @@ hb_raster_paint_image (hb_paint_funcs_t *pfuncs HB_UNUSED,
#define GRADIENT_LUT_SIZE 256
#define GRADIENT_LUT_MIN_PIXELS (64u * 64u)
static int
static inline int
cmp_color_stop (const void *p1, const void *p2)
{
const hb_color_stop_t *c1 = (const hb_color_stop_t *) p1;
+1 -3
View File
@@ -705,13 +705,11 @@ struct hb_vector_t
/* Sorting API. */
void qsort (int (*cmp)(const void*, const void*))
{ as_array ().qsort (cmp); }
template <typename Compar>
void qsort (Compar compar)
{ as_array ().qsort (compar); }
void qsort ()
{ as_array ().qsort (Type::cmp); }
{ as_array ().qsort (); }
/* Unsorted search API. */
template <typename T>