From 19d2b5013d8ac7aa45b3b8e8c61ad90773c86925 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 21 Dec 2018 01:17:35 -0500 Subject: [PATCH] [iter] Add bidirectionality --- src/hb-iter.hh | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/hb-iter.hh b/src/hb-iter.hh index 269da97bf..53bfbb518 100644 --- a/src/hb-iter.hh +++ b/src/hb-iter.hh @@ -58,16 +58,22 @@ struct hb_iter_t item_type_t& operator [] (unsigned i) { return item (i); } type_t& operator += (unsigned count) { forward (count); return thiz(); } type_t& operator ++ () { next (); return thiz(); } + type_t& operator -= (unsigned count) { rewind (count); return thiz(); } + type_t& operator -- () { prev (); return thiz(); } type_t operator + (unsigned count) { type_t c (*thiz()); c += count; return c; } - type_t operator ++ (int) { type_t c (*thiz()); ++*this; return c; } + type_t operator ++ (int) { type_t c (*thiz()); ++*thiz(); return c; } + type_t operator - (unsigned count) { type_t c (*thiz()); c -= count; return c; } + type_t operator -- (int) { type_t c (*thiz()); --*thiz(); return c; } /* Methods. */ type_t iter () const { return *thiz(); } item_type_t item () const { return thiz()->__item__ (); } item_type_t item (unsigned i) const { return thiz()->__item__ (i); } bool more () const { return thiz()->__more__ (); } - void next () { thiz()->next (); } + void next () { thiz()->__next__ (); } void forward (unsigned n) { thiz()->__forward__ (n); } + void prev () { thiz()->__prev__ (); } + void rewind (unsigned n) { thiz()->__rewind__ (n); } unsigned len () const { return thiz()->__len__ (); } /* @@ -89,6 +95,10 @@ struct hb_iter_t void __next__ () { thiz()->forward (1); } void __forward__ (unsigned n) { while (n--) next (); } + /* Rewinding: Implement __prev__() or __rewind__() if bidirectional. */ + void __prev__ () { thiz()->rewind (1); } + void __rewind__ (unsigned n) { while (n--) prev (); } + /* Population: Implement __len__() if known. */ unsigned __len__ () const { type_t c (*thiz()); unsigned l = 0; while (c) { c++; l++; }; return l; }