diff --git a/src/graph/graph.hh b/src/graph/graph.hh index 49cb4bdb0..2e90bc849 100644 --- a/src/graph/graph.hh +++ b/src/graph/graph.hh @@ -762,16 +762,14 @@ struct graph_t void find_subgraph (unsigned node_idx, hb_set_t& subgraph) { - if (subgraph.has (node_idx)) return; - subgraph.add (node_idx); + if (!subgraph.test_and_add (node_idx)) return; for (const auto& link : vertices_[node_idx].obj.all_links ()) find_subgraph (link.objidx, subgraph); } size_t find_subgraph_size (unsigned node_idx, hb_set_t& subgraph, unsigned max_depth = -1) { - if (subgraph.has (node_idx)) return 0; - subgraph.add (node_idx); + if (!subgraph.test_and_add (node_idx)) return 0; const auto& o = vertices_[node_idx].obj; size_t size = o.tail - o.head; @@ -1159,8 +1157,7 @@ struct graph_t hb_set_t visited; for (unsigned p : vertices_[node_idx].parents) { - if (visited.has (p)) continue; - visited.add (p); + if (!visited.test_and_add (p)) continue; // Only real links can be wide for (const auto& l : vertices_[p].obj.real_links) @@ -1364,14 +1361,10 @@ struct graph_t hb_set_t& connected) { if (unlikely (!check_success (!visited.in_error ()))) return; - if (visited.has (start_idx)) return; - visited.add (start_idx); + if (!visited.test_and_add (start_idx)) return; - if (targets.has (start_idx)) - { - targets.del (start_idx); + if (targets.test_and_del (start_idx)) connected.add (start_idx); - } const auto& v = vertices_[start_idx]; diff --git a/src/hb-bit-page.hh b/src/hb-bit-page.hh index e1826e12a..a58150ff7 100644 --- a/src/hb-bit-page.hh +++ b/src/hb-bit-page.hh @@ -115,6 +115,31 @@ struct hb_bit_page_t void set (hb_codepoint_t g, bool value) { if (value) add (g); else del (g); } bool get (hb_codepoint_t g) const { return elt (g) & mask (g); } + bool test_and_add (hb_codepoint_t g) + { + elt_t *e = &elt (g); + elt_t m = mask (g); + + bool ret = !(*e & m); + if (ret) + { + *e |= m; dirty (); + } + return ret; + } + bool test_and_del (hb_codepoint_t g) + { + elt_t *e = &elt (g); + elt_t m = mask (g); + + bool ret = *e & m; + if (ret) + { + *e &= ~m; dirty (); + } + return ret; + } + void add_range (hb_codepoint_t a, hb_codepoint_t b) { elt_t *la = &elt (a); diff --git a/src/hb-bit-set-invertible.hh b/src/hb-bit-set-invertible.hh index e765a479a..7cd6e7082 100644 --- a/src/hb-bit-set-invertible.hh +++ b/src/hb-bit-set-invertible.hh @@ -104,6 +104,8 @@ struct hb_bit_set_invertible_t void add (hb_codepoint_t g) { unlikely (inverted) ? s.del (g) : s.add (g); } + bool test_and_add (hb_codepoint_t g) { return unlikely (inverted) ? s.test_and_del (g) : s.test_and_add (g); } + bool test_and_del (hb_codepoint_t g) { return unlikely (inverted) ? s.test_and_add (g) : s.test_and_del (g); } bool add_range (hb_codepoint_t a, hb_codepoint_t b) { return unlikely (inverted) ? ((void) s.del_range (a, b), true) : s.add_range (a, b); } diff --git a/src/hb-bit-set.hh b/src/hb-bit-set.hh index 17eb06541..de5c4ea70 100644 --- a/src/hb-bit-set.hh +++ b/src/hb-bit-set.hh @@ -154,6 +154,22 @@ struct hb_bit_set_t page_t *page = page_for (g, true); if (unlikely (!page)) return; page->add (g); } + bool test_and_add (hb_codepoint_t g) + { + if (unlikely (!successful)) return false; + if (unlikely (g == INVALID)) return false; + dirty (); + page_t *page = page_for (g, true); if (unlikely (!page)) return false; + return page->test_and_add (g); + } + bool test_and_del (hb_codepoint_t g) + { + if (unlikely (!successful)) return false; + if (unlikely (g == INVALID)) return false; + dirty (); + page_t *page = page_for (g); if (!page) return false; + return page->test_and_del (g); + } bool add_range (hb_codepoint_t a, hb_codepoint_t b) { if (unlikely (!successful)) return true; /* https://github.com/harfbuzz/harfbuzz/issues/657 */ diff --git a/src/hb-set.hh b/src/hb-set.hh index 7d1c941e4..7a61de7c8 100644 --- a/src/hb-set.hh +++ b/src/hb-set.hh @@ -84,6 +84,8 @@ struct hb_sparseset_t uint32_t hash () const { return s.hash (); } void add (hb_codepoint_t g) { s.add (g); } + bool test_and_add (hb_codepoint_t g) { return s.test_and_add (g); } + bool test_and_del (hb_codepoint_t g) { return s.test_and_del (g); } bool add_range (hb_codepoint_t a, hb_codepoint_t b) { return s.add_range (a, b); } template