[arrays] Port ArrayOf.qsort() and hb_vector_t.qsort() to hb_array_t

This commit is contained in:
Behdad Esfahbod 2018-11-24 01:59:50 -05:00
parent 073d837aa2
commit 70d80c90fe
2 changed files with 24 additions and 4 deletions

View File

@ -559,6 +559,9 @@ struct hb_bytes_t
unsigned int len;
};
template <typename Type>
struct hb_sorted_array_t;
template <typename Type>
struct hb_array_t
{
@ -600,13 +603,20 @@ struct hb_array_t
return not_found;
}
inline void qsort (int (*cmp)(const void*, const void*))
inline hb_sorted_array_t<Type> qsort (int (*cmp)(const void*, const void*))
{
::qsort (arrayZ, len, sizeof (Type), cmp);
return hb_sorted_array_t<Type> (*this);
}
inline void qsort (unsigned int start = 0, unsigned int end = (unsigned int) -1)
inline hb_sorted_array_t<Type> qsort (void)
{
::qsort (arrayZ, len, sizeof (Type), Type::cmp);
return hb_sorted_array_t<Type> (*this);
}
inline void qsort (unsigned int start, unsigned int end)
{
end = MIN (end, len);
assert (start <= end);
::qsort (arrayZ + start, end - start, sizeof (Type), Type::cmp);
}

View File

@ -375,6 +375,16 @@ struct UnsizedArrayOf
inline hb_array_t<const Type> as_array (unsigned int len) const
{ return hb_array (arrayZ, len); }
template <typename T>
inline Type &lsearch (unsigned int len, const T &x)
{ return *as_array (len).lsearch (x, &Crap (T)); }
template <typename T>
inline const Type &lsearch (unsigned int len, const T &x) const
{ return *as_array (len).lsearch (x, &Null (T)); }
inline void qsort (unsigned int len, unsigned int start = 0, unsigned int end = (unsigned int) -1)
{ as_array (len).qsort (start, end); }
inline bool sanitize (hb_sanitize_context_t *c, unsigned int count) const
{
TRACE_SANITIZE (this);
@ -577,8 +587,8 @@ struct ArrayOf
inline const Type &lsearch (const T &x) const
{ return *as_array ().lsearch (x, &Null (T)); }
inline void qsort (void)
{ as_array ().qsort (); }
inline void qsort (unsigned int start = 0, unsigned int end = (unsigned int) -1)
{ as_array ().qsort (start, end); }
inline bool sanitize_shallow (hb_sanitize_context_t *c) const
{