From 70d80c90fe2f4eca66bec3e1d313bbf7e4d0ab65 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 24 Nov 2018 01:59:50 -0500 Subject: [PATCH] [arrays] Port ArrayOf.qsort() and hb_vector_t.qsort() to hb_array_t --- src/hb-dsalgs.hh | 14 ++++++++++++-- src/hb-open-type.hh | 14 ++++++++++++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/hb-dsalgs.hh b/src/hb-dsalgs.hh index 876060721..cc1a1d425 100644 --- a/src/hb-dsalgs.hh +++ b/src/hb-dsalgs.hh @@ -559,6 +559,9 @@ struct hb_bytes_t unsigned int len; }; +template +struct hb_sorted_array_t; + template 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 qsort (int (*cmp)(const void*, const void*)) { ::qsort (arrayZ, len, sizeof (Type), cmp); + return hb_sorted_array_t (*this); } - inline void qsort (unsigned int start = 0, unsigned int end = (unsigned int) -1) + inline hb_sorted_array_t qsort (void) + { + ::qsort (arrayZ, len, sizeof (Type), Type::cmp); + return hb_sorted_array_t (*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); } diff --git a/src/hb-open-type.hh b/src/hb-open-type.hh index 4704861d4..c7154c424 100644 --- a/src/hb-open-type.hh +++ b/src/hb-open-type.hh @@ -375,6 +375,16 @@ struct UnsizedArrayOf inline hb_array_t as_array (unsigned int len) const { return hb_array (arrayZ, len); } + template + inline Type &lsearch (unsigned int len, const T &x) + { return *as_array (len).lsearch (x, &Crap (T)); } + template + 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 {