From 255dadc152a967219b447f02bfe95dd9d6bce07c Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 4 Feb 2025 22:02:11 +0000 Subject: [PATCH] [set] Speed up may_intersect() --- src/hb-bit-page.hh | 7 +++++++ src/hb-bit-set-invertible.hh | 3 +++ src/hb-bit-set.hh | 20 ++++++++++++++++++++ src/hb-set.hh | 8 ++------ 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/hb-bit-page.hh b/src/hb-bit-page.hh index 869c67895..df08c834e 100644 --- a/src/hb-bit-page.hh +++ b/src/hb-bit-page.hh @@ -227,6 +227,13 @@ struct hb_bit_page_t return false; return true; } + bool may_intersect (const hb_bit_page_t &other) const + { + for (unsigned i = 0; i < len (); i++) + if (v[i] & other.v[i]) + return true; + return false; + } bool operator <= (const hb_bit_page_t &larger_page) const { return is_subset (larger_page); } bool is_subset (const hb_bit_page_t &larger_page) const { diff --git a/src/hb-bit-set-invertible.hh b/src/hb-bit-set-invertible.hh index d5d1326d9..862b1a659 100644 --- a/src/hb-bit-set-invertible.hh +++ b/src/hb-bit-set-invertible.hh @@ -139,6 +139,9 @@ struct hb_bit_set_invertible_t hb_bit_set_invertible_t& operator << (const hb_codepoint_pair_t& range) { add_range (range.first, range.second); return *this; } + bool may_intersect (const hb_bit_set_invertible_t &other) const + { return inverted || other.inverted || s.may_intersect (other.s); } + bool intersects (hb_codepoint_t first, hb_codepoint_t last) const { hb_codepoint_t c = first - 1; diff --git a/src/hb-bit-set.hh b/src/hb-bit-set.hh index 032c5b5c4..c303e2bef 100644 --- a/src/hb-bit-set.hh +++ b/src/hb-bit-set.hh @@ -358,6 +358,26 @@ struct hb_bit_set_t hb_bit_set_t& operator << (const hb_codepoint_pair_t& range) { add_range (range.first, range.second); return *this; } + bool may_intersect (const hb_bit_set_t &other) const + { + unsigned int na = pages.length; + unsigned int nb = other.pages.length; + + unsigned int a = 0, b = 0; + for (; a < na && b < nb; ) + { + if (page_map.arrayZ[a].major == other.page_map.arrayZ[b].major && + page_at (a).may_intersect (other.page_at (b))) + return true; + + if (page_map.arrayZ[a].major < other.page_map.arrayZ[b].major) + a++; + else + b++; + } + return false; + } + bool intersects (hb_codepoint_t first, hb_codepoint_t last) const { hb_codepoint_t c = first - 1; diff --git a/src/hb-set.hh b/src/hb-set.hh index 8d0d9692b..8ea20bb71 100644 --- a/src/hb-set.hh +++ b/src/hb-set.hh @@ -120,12 +120,8 @@ struct hb_sparseset_t hb_sparseset_t& operator << (const hb_codepoint_pair_t& range) { add_range (range.first, range.second); return *this; } - bool may_intersect (const hb_set_t &other) const - { - hb_sparseset_t tmp = *this; - tmp.intersect (other); - return !tmp.is_empty (); - } + bool may_intersect (const hb_sparseset_t &other) const + { return s.may_intersect (other.s); } bool intersects (hb_codepoint_t first, hb_codepoint_t last) const { return s.intersects (first, last); }