From d3976b7e63559b87ef34abc62acf5033f3369197 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 26 Dec 2018 18:54:27 -0500 Subject: [PATCH] [iter] Make them work, mostly --- src/hb-iter.hh | 11 ++++++++--- src/hb-set.hh | 18 ++++++++++-------- src/test-iter.cc | 25 +++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 11 deletions(-) diff --git a/src/hb-iter.hh b/src/hb-iter.hh index c83ccb39b..60b3b6503 100644 --- a/src/hb-iter.hh +++ b/src/hb-iter.hh @@ -65,9 +65,9 @@ struct hb_iter_t iter_t& operator ++ () { next (); return *thiz(); } iter_t& operator -= (unsigned count) { rewind (count); return *thiz(); } iter_t& operator -- () { prev (); return *thiz(); } - iter_t operator + (unsigned count) { iter_t c (*thiz()); c += count; return c; } + iter_t operator + (unsigned count) const { iter_t c (*thiz()); c += count; return c; } iter_t operator ++ (int) { iter_t c (*thiz()); ++*thiz(); return c; } - iter_t operator - (unsigned count) { iter_t c (*thiz()); c -= count; return c; } + iter_t operator - (unsigned count) const { iter_t c (*thiz()); c -= count; return c; } iter_t operator -- (int) { iter_t c (*thiz()); --*thiz(); return c; } /* Methods. */ @@ -112,7 +112,7 @@ struct hb_iter_mixin_t /* Access: Implement __item__(), or __item_at__() if random-access. */ item_t& __item__ () const { return thiz()->item_at (0); } - item_t& __item_at__ (unsigned i) const { return *(thiz() + i); } + item_t& __item_at__ (unsigned i) const { return *(*thiz() + i); } /* Termination: Implement __more__(), or __len__() if random-access. */ bool __more__ () const { return thiz()->__len__ (); } @@ -129,6 +129,11 @@ struct hb_iter_mixin_t /* Random access: Return true if item_at(), len(), forward() are fast. */ bool __random_access__ () const { return false; } + + protected: + hb_iter_mixin_t () {} + hb_iter_mixin_t (const hb_iter_mixin_t &o HB_UNUSED) {} + void operator = (const hb_iter_mixin_t &o HB_UNUSED) {} }; diff --git a/src/hb-set.hh b/src/hb-set.hh index 81b9064d5..640723e4f 100644 --- a/src/hb-set.hh +++ b/src/hb-set.hh @@ -671,20 +671,22 @@ struct hb_set_t /* * Iterator implementation. */ - struct const_iter_t : hb_sorted_iter_t + struct const_iter_t : + hb_sorted_iter_t, + hb_iter_mixin_t { - const_iter_t (const hb_set_t &s_) : - s (s_), v (INVALID), l (s.get_population () + 1) { __next__ (); } + const_iter_t (const hb_set_t &s_ = Null(hb_set_t)) : + s (&s_), v (INVALID), l (s->get_population () + 1) { __next__ (); } typedef hb_codepoint_t __item_type__; - hb_codepoint_t __item__ () const { return v; } + const hb_codepoint_t& __item__ () const { return v; } bool __more__ () const { return v != INVALID; } - void __next__ () { s.next (&v); if (l) l--; } - void __prev__ () { s.previous (&v); } - unsigned __len__ () { return l; } + void __next__ () { s->next (&v); if (l) l--; } + void __prev__ () { s->previous (&v); } + unsigned __len__ () const { return l; } protected: - const hb_set_t &s; + const hb_set_t *s; hb_codepoint_t v; unsigned l; }; diff --git a/src/test-iter.cc b/src/test-iter.cc index 05430b009..5f69a928c 100644 --- a/src/test-iter.cc +++ b/src/test-iter.cc @@ -61,6 +61,27 @@ struct some_array_t hb_array_t arr; }; + +template static void +test_iterator (Iterator it) +{ + /* Iterate over a copy of it. */ + for (auto c = it.iter (); c; c++) + *c; + + it += it.len () + 10; + it = it + 10; + + assert (*it == it[0]); +} + +template static void +test_iterable (Iterable &lst) +{ + // Test that can take iterator from. + test_iterator (lst.iter ()); +} + int main (int argc, char **argv) { @@ -80,5 +101,9 @@ main (int argc, char **argv) hb_copy (t, s); // hb_copy (t, a.iter ()); + test_iterable (v); + hb_set_t st; + test_iterable (st); + return 0; }