From b8b3b3e38b08ee7bb8d44481dd25febcee693554 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 15 Feb 2019 16:05:36 -0800 Subject: [PATCH] [iter] Add hb_enumerate() and use it --- src/hb-iter.hh | 39 ++++++++++++++++++++++++++++++++++-- src/hb-ot-layout-gsubgpos.hh | 32 +++++++++++------------------ 2 files changed, 49 insertions(+), 22 deletions(-) diff --git a/src/hb-iter.hh b/src/hb-iter.hh index df7372e4e..c74ea951e 100644 --- a/src/hb-iter.hh +++ b/src/hb-iter.hh @@ -339,7 +339,7 @@ struct hb_zip_iter_t : hb_pair_t > { hb_zip_iter_t () {} - hb_zip_iter_t (A a, B b) : a (a), b (b) {} + hb_zip_iter_t (const A& a, const B& b) : a (a), b (b) {} typedef hb_pair_t __item_t__; static constexpr bool is_random_access_iterator = @@ -366,10 +366,45 @@ static const struct template hb_zip_iter_t - operator () (const A& a, const B &b) const + operator () (A& a, B &b) const { return hb_zip_iter_t (hb_iter (a), hb_iter (b)); } } hb_zip HB_UNUSED; +/* hb_enumerate */ + +template +struct hb_enumerate_iter_t : + hb_iter_t, + hb_pair_t > +{ + hb_enumerate_iter_t (const Iter& it) : i (0), it (it) {} + + typedef hb_pair_t __item_t__; + static constexpr bool is_random_access_iterator = Iter::is_random_access_iterator; + static constexpr bool is_sorted_iterator = true; + __item_t__ __item__ () const { return __item_t__ (+i, *it); } + __item_t__ __item_at__ (unsigned j) const { return __item_t__ (i + j, it[j]); } + bool __more__ () const { return bool (it); } + unsigned __len__ () const { return it.len (); } + void __next__ () { ++i; ++it; } + void __forward__ (unsigned n) { i += n; it += n; } + void __prev__ () { --i; --it; } + void __rewind__ (unsigned n) { i -= n; it -= n; } + + private: + unsigned i; + Iter it; +}; +static const struct +{ + template + hb_enumerate_iter_t + operator () (Iterable& it) const + { return hb_enumerate_iter_t (hb_iter (it)); } +} hb_enumerate HB_UNUSED; + /* hb_apply() */ template diff --git a/src/hb-ot-layout-gsubgpos.hh b/src/hb-ot-layout-gsubgpos.hh index 70ddc28e6..dabb98c39 100644 --- a/src/hb-ot-layout-gsubgpos.hh +++ b/src/hb-ot-layout-gsubgpos.hh @@ -1533,10 +1533,9 @@ struct ContextFormat2 &class_def }; - unsigned int count = ruleSet.len; - for (unsigned int i = 0; i < count; i++) - if (class_def.intersects_class (glyphs, i) && - (this+ruleSet[i]).intersects (glyphs, lookup_context)) + for (auto it = hb_enumerate (ruleSet); it; ++it) + if (class_def.intersects_class (glyphs, (*it).first) && + (this+(*it).second).intersects (glyphs, lookup_context)) return true; return false; @@ -1554,12 +1553,9 @@ struct ContextFormat2 &class_def }; - unsigned int count = ruleSet.len; - for (unsigned int i = 0; i < count; i++) - if (class_def.intersects_class (c->glyphs, i)) { - const RuleSet &rule_set = this+ruleSet[i]; - rule_set.closure (c, lookup_context); - } + for (auto it = hb_enumerate (ruleSet); it; ++it) + if (class_def.intersects_class (c->glyphs, (*it).first)) + (this+(*it).second).closure (c, lookup_context); } void collect_glyphs (hb_collect_glyphs_context_t *c) const @@ -2171,10 +2167,9 @@ struct ChainContextFormat2 &lookahead_class_def} }; - unsigned int count = ruleSet.len; - for (unsigned int i = 0; i < count; i++) - if (input_class_def.intersects_class (glyphs, i) && - (this+ruleSet[i]).intersects (glyphs, lookup_context)) + for (auto it = hb_enumerate (ruleSet); it; ++it) + if (input_class_def.intersects_class (glyphs, (*it).first) && + (this+(*it).second).intersects (glyphs, lookup_context)) return true; return false; @@ -2195,12 +2190,9 @@ struct ChainContextFormat2 &lookahead_class_def} }; - unsigned int count = ruleSet.len; - for (unsigned int i = 0; i < count; i++) - if (input_class_def.intersects_class (c->glyphs, i)) { - const ChainRuleSet &rule_set = this+ruleSet[i]; - rule_set.closure (c, lookup_context); - } + for (auto it = hb_enumerate (ruleSet); it; ++it) + if (input_class_def.intersects_class (c->glyphs, (*it).first)) + (this+(*it).second).closure (c, lookup_context); } void collect_glyphs (hb_collect_glyphs_context_t *c) const