[iter] Make them work, mostly

This commit is contained in:
Behdad Esfahbod 2018-12-26 18:54:27 -05:00
parent 959bb58bdd
commit d3976b7e63
3 changed files with 43 additions and 11 deletions

View File

@ -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) {}
};

View File

@ -671,20 +671,22 @@ struct hb_set_t
/*
* Iterator implementation.
*/
struct const_iter_t : hb_sorted_iter_t<const_iter_t, const hb_codepoint_t>
struct const_iter_t :
hb_sorted_iter_t<const_iter_t, const hb_codepoint_t>,
hb_iter_mixin_t<const_iter_t, const hb_codepoint_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;
};

View File

@ -61,6 +61,27 @@ struct some_array_t
hb_array_t<T> arr;
};
template <typename Iterator> 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 <typename Iterable> 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;
}