From 849e40b4db814bc6fe5defa5b802cc534aab209a Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 19 Jan 2022 11:06:33 -0700 Subject: [PATCH 1/5] [ms-feature-ranges] Use preferred vector search API --- src/hb-ms-feature-ranges.cc | 2 +- src/hb-ms-feature-ranges.hh | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/hb-ms-feature-ranges.cc b/src/hb-ms-feature-ranges.cc index 6d09b252d..a5a01f4b8 100644 --- a/src/hb-ms-feature-ranges.cc +++ b/src/hb-ms-feature-ranges.cc @@ -114,7 +114,7 @@ hb_ms_setup_features (const hb_feature_t *features, } else { - auto *feature = active_features.find (&event->feature); + auto *feature = active_features.lsearch (event->feature); if (feature) active_features.remove (feature - active_features.arrayZ); } diff --git a/src/hb-ms-feature-ranges.hh b/src/hb-ms-feature-ranges.hh index 401d1e1d9..3a13c1b0f 100644 --- a/src/hb-ms-feature-ranges.hh +++ b/src/hb-ms-feature-ranges.hh @@ -52,8 +52,8 @@ struct hb_ms_active_feature_t { a->fea.value < b->fea.value ? -1 : a->fea.value > b->fea.value ? 1 : 0; } - bool operator== (const hb_ms_active_feature_t *f) - { return cmp (this, f) == 0; } + bool operator== (const hb_ms_active_feature_t f) const + { return cmp (this, &f) == 0; } }; struct hb_ms_feature_event_t { From cd9799784cc6deb8829a3c54a9dee654379cc03e Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 19 Jan 2022 11:14:25 -0700 Subject: [PATCH 2/5] [vector] Remove old find() method --- src/hb-algs.hh | 12 +++++++++--- src/hb-object.hh | 6 +++--- src/hb-vector.hh | 17 ----------------- 3 files changed, 12 insertions(+), 23 deletions(-) diff --git a/src/hb-algs.hh b/src/hb-algs.hh index d380a6fa5..3a3ab0804 100644 --- a/src/hb-algs.hh +++ b/src/hb-algs.hh @@ -447,23 +447,29 @@ struct private: template auto - impl (T1&& v1, T2 &&v2, hb_priority<2>) const HB_AUTO_RETURN + impl (T1&& v1, T2 &&v2, hb_priority<3>) const HB_AUTO_RETURN ( std::forward (v2).cmp (std::forward (v1)) == 0 ) template auto - impl (T1&& v1, T2 &&v2, hb_priority<1>) const HB_AUTO_RETURN + impl (T1&& v1, T2 &&v2, hb_priority<2>) const HB_AUTO_RETURN ( std::forward (v1).cmp (std::forward (v2)) == 0 ) template auto - impl (T1&& v1, T2 &&v2, hb_priority<0>) const HB_AUTO_RETURN + impl (T1&& v1, T2 &&v2, hb_priority<1>) const HB_AUTO_RETURN ( std::forward (v1) == std::forward (v2) ) + template auto + impl (T1&& v1, T2 &&v2, hb_priority<0>) const HB_AUTO_RETURN + ( + std::forward (v2) == std::forward (v1) + ) + public: template auto diff --git a/src/hb-object.hh b/src/hb-object.hh index 0e15cb12c..4b5bc32ad 100644 --- a/src/hb-object.hh +++ b/src/hb-object.hh @@ -53,7 +53,7 @@ struct hb_lockable_set_t item_t *replace_or_insert (T v, lock_t &l, bool replace) { l.lock (); - item_t *item = items.find (v); + item_t *item = items.lsearch (v); if (item) { if (replace) { item_t old = *item; @@ -76,7 +76,7 @@ struct hb_lockable_set_t void remove (T v, lock_t &l) { l.lock (); - item_t *item = items.find (v); + item_t *item = items.lsearch (v); if (item) { item_t old = *item; @@ -93,7 +93,7 @@ struct hb_lockable_set_t bool find (T v, item_t *i, lock_t &l) { l.lock (); - item_t *item = items.find (v); + item_t *item = items.lsearch (v); if (item) *i = *item; l.unlock (); diff --git a/src/hb-vector.hh b/src/hb-vector.hh index 97311a29d..b3c3ac8cd 100644 --- a/src/hb-vector.hh +++ b/src/hb-vector.hh @@ -362,23 +362,6 @@ struct hb_vector_t shrink_vector (size); } - template - Type *find (T v) - { - for (unsigned int i = 0; i < length; i++) - if (arrayZ[i] == v) - return &arrayZ[i]; - return nullptr; - } - template - const Type *find (T v) const - { - for (unsigned int i = 0; i < length; i++) - if (arrayZ[i] == v) - return &arrayZ[i]; - return nullptr; - } - void qsort (int (*cmp)(const void*, const void*)) { as_array ().qsort (cmp); } void qsort (unsigned int start = 0, unsigned int end = (unsigned int) -1) From 294d50ff353f77be2b2f0a560a2a876e4a80d094 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 19 Jan 2022 11:32:14 -0700 Subject: [PATCH 3/5] [vector] Add sorted template argument --- src/hb-vector.hh | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/hb-vector.hh b/src/hb-vector.hh index b3c3ac8cd..9eb45f502 100644 --- a/src/hb-vector.hh +++ b/src/hb-vector.hh @@ -32,8 +32,9 @@ #include "hb-null.hh" -template -struct hb_vector_t +template +struct hb_vector_t : std::conditional, hb_empty_t>::type { typedef Type item_t; static constexpr unsigned item_size = hb_static_size (Type); @@ -362,11 +363,14 @@ struct hb_vector_t shrink_vector (size); } + + /* Sorting API. */ void qsort (int (*cmp)(const void*, const void*)) { as_array ().qsort (cmp); } void qsort (unsigned int start = 0, unsigned int end = (unsigned int) -1) { as_array ().qsort (start, end); } + /* Unsorted search API. */ template Type *lsearch (const T &x, Type *not_found = nullptr) { return as_array ().lsearch (x, not_found); } @@ -376,6 +380,22 @@ struct hb_vector_t template bool lfind (const T &x, unsigned *pos = nullptr) const { return as_array ().lfind (x, pos); } + + /* Sorted search API. */ + template + Type *bsearch (const T &x, Type *not_found = nullptr) + { return as_array ().bsearch (x, not_found); } + template + const Type *bsearch (const T &x, const Type *not_found = nullptr) const + { return as_array ().bsearch (x, not_found); } + template + bool bfind (const T &x, unsigned int *i = nullptr, + hb_not_found_t not_found = HB_NOT_FOUND_DONT_STORE, + unsigned int to_store = (unsigned int) -1) const + { return as_array ().bfind (x, i, not_found, to_store); } }; template From 106388e7665984b545b2b47b0fac63ab2484a4f3 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 19 Jan 2022 11:38:37 -0700 Subject: [PATCH 4/5] [vector] Merge sorted-vector into vector Was easier than I thought! Let's see what the bot gods think... --- src/hb-vector.hh | 58 +++++++++--------------------------------------- 1 file changed, 11 insertions(+), 47 deletions(-) diff --git a/src/hb-vector.hh b/src/hb-vector.hh index 9eb45f502..6c7d32e49 100644 --- a/src/hb-vector.hh +++ b/src/hb-vector.hh @@ -38,6 +38,8 @@ struct hb_vector_t : std::conditional, hb_empty { typedef Type item_t; static constexpr unsigned item_size = hb_static_size (Type); + using array_t = typename std::conditional, hb_array_t>::type; + using c_array_t = typename std::conditional, hb_array_t>::type; hb_vector_t () = default; hb_vector_t (std::initializer_list lst) : hb_vector_t () @@ -147,24 +149,24 @@ struct hb_vector_t : std::conditional, hb_empty template hb_vector_t& operator << (T&& v) { push (std::forward (v)); return *this; } - hb_array_t< Type> as_array () { return hb_array (arrayZ, length); } - hb_array_t as_array () const { return hb_array (arrayZ, length); } + array_t as_array () { return hb_array (arrayZ, length); } + c_array_t as_array () const { return hb_array (arrayZ, length); } /* Iterator. */ - typedef hb_array_t iter_t; - typedef hb_array_t< Type> writer_t; + typedef c_array_t iter_t; + typedef array_t writer_t; iter_t iter () const { return as_array (); } writer_t writer () { return as_array (); } operator iter_t () const { return iter (); } operator writer_t () { return writer (); } - hb_array_t sub_array (unsigned int start_offset, unsigned int count) const + c_array_t sub_array (unsigned int start_offset, unsigned int count) const { return as_array ().sub_array (start_offset, count); } - hb_array_t sub_array (unsigned int start_offset, unsigned int *count = nullptr /* IN/OUT */) const + c_array_t sub_array (unsigned int start_offset, unsigned int *count = nullptr /* IN/OUT */) const { return as_array ().sub_array (start_offset, count); } - hb_array_t sub_array (unsigned int start_offset, unsigned int count) + array_t sub_array (unsigned int start_offset, unsigned int count) { return as_array ().sub_array (start_offset, count); } - hb_array_t sub_array (unsigned int start_offset, unsigned int *count = nullptr /* IN/OUT */) + array_t sub_array (unsigned int start_offset, unsigned int *count = nullptr /* IN/OUT */) { return as_array ().sub_array (start_offset, count); } hb_sorted_array_t as_sorted_array () @@ -399,44 +401,6 @@ struct hb_vector_t : std::conditional, hb_empty }; template -struct hb_sorted_vector_t : hb_vector_t -{ - hb_sorted_vector_t () = default; - ~hb_sorted_vector_t () = default; - hb_sorted_vector_t (hb_sorted_vector_t& o) = default; - hb_sorted_vector_t (hb_sorted_vector_t &&o) = default; - hb_sorted_vector_t (std::initializer_list lst) : hb_vector_t (lst) {} - template - hb_sorted_vector_t (const Iterable &o) : hb_vector_t (o) {} - hb_sorted_vector_t& operator = (const hb_sorted_vector_t &o) = default; - hb_sorted_vector_t& operator = (hb_sorted_vector_t &&o) = default; - friend void swap (hb_sorted_vector_t& a, hb_sorted_vector_t& b) - { hb_swap ((hb_vector_t&) (a), (hb_vector_t&) (b)); } - - hb_sorted_array_t< Type> as_array () { return hb_sorted_array (this->arrayZ, this->length); } - hb_sorted_array_t as_array () const { return hb_sorted_array (this->arrayZ, this->length); } - - /* Iterator. */ - typedef hb_sorted_array_t const_iter_t; - typedef hb_sorted_array_t< Type> iter_t; - const_iter_t iter () const { return as_array (); } - const_iter_t citer () const { return as_array (); } - iter_t iter () { return as_array (); } - operator iter_t () { return iter (); } - operator const_iter_t () const { return iter (); } - - template - Type *bsearch (const T &x, Type *not_found = nullptr) - { return as_array ().bsearch (x, not_found); } - template - const Type *bsearch (const T &x, const Type *not_found = nullptr) const - { return as_array ().bsearch (x, not_found); } - template - bool bfind (const T &x, unsigned int *i = nullptr, - hb_not_found_t not_found = HB_NOT_FOUND_DONT_STORE, - unsigned int to_store = (unsigned int) -1) const - { return as_array ().bfind (x, i, not_found, to_store); } -}; +using hb_sorted_vector_t = hb_vector_t; #endif /* HB_VECTOR_HH */ From 376738d41104dc10d483c8eb135d1befd5b18855 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 19 Jan 2022 11:46:21 -0700 Subject: [PATCH 5/5] [coretext] Fix lsearch --- src/hb-coretext.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/hb-coretext.cc b/src/hb-coretext.cc index a512f3b8b..24075e146 100644 --- a/src/hb-coretext.cc +++ b/src/hb-coretext.cc @@ -481,8 +481,8 @@ struct active_feature_t { a->rec.setting < b->rec.setting ? -1 : a->rec.setting > b->rec.setting ? 1 : 0; } - bool operator== (const active_feature_t *f) { - return cmp (this, f) == 0; + bool operator== (const active_feature_t& f) const { + return cmp (this, &f) == 0; } }; @@ -677,7 +677,7 @@ _hb_coretext_shape (hb_shape_plan_t *shape_plan, { active_features.push (event->feature); } else { - active_feature_t *feature = active_features.find (&event->feature); + active_feature_t *feature = active_features.lsearch (event->feature); if (feature) active_features.remove (feature - active_features.arrayZ); }