From 2c24ea37b1ef63f79fcc24752dd180ce53540eda Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 9 May 2019 11:07:38 -0700 Subject: [PATCH] [iter] Take start value in hb_enumerate() Also rewrite it via composition. --- src/hb-iter.hh | 61 +++++++++++------------------------------------- src/test-iter.cc | 1 + 2 files changed, 14 insertions(+), 48 deletions(-) diff --git a/src/hb-iter.hh b/src/hb-iter.hh index 2557d3abc..885c122e2 100644 --- a/src/hb-iter.hh +++ b/src/hb-iter.hh @@ -490,58 +490,11 @@ struct template hb_zip_iter_t, hb_iter_type> - operator () (A& a, B &b) const + operator () (A&& a, B&& b) const { return hb_zip_iter_t, hb_iter_type> (hb_iter (a), hb_iter (b)); } } HB_FUNCOBJ (hb_zip); -/* 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; } - hb_enumerate_iter_t __end__ () const - { - if (is_random_access_iterator) - return *this + this->len (); - /* Above expression loops twice. Following loops once. */ - auto it = *this; - while (it) ++it; - return it; - } - bool operator != (const hb_enumerate_iter_t& o) const - { return i != o.i || it != o.it; } - - private: - unsigned i; - Iter it; -}; -struct -{ - template - hb_enumerate_iter_t> - operator () (Iterable&& it) const - { return hb_enumerate_iter_t> (hb_iter (it)); } -} -HB_FUNCOBJ (hb_enumerate); - /* hb_apply() */ template @@ -629,6 +582,18 @@ struct } HB_FUNCOBJ (hb_range); +/* hb_enumerate */ + +struct +{ + template + auto operator () (Iterable&& it, Index start = 0u) const HB_AUTO_RETURN + ( hb_zip (hb_iota (start), it) ) +} +HB_FUNCOBJ (hb_enumerate); + /* hb_sink() */ diff --git a/src/test-iter.cc b/src/test-iter.cc index 8f197898e..e6c5d45c0 100644 --- a/src/test-iter.cc +++ b/src/test-iter.cc @@ -158,6 +158,7 @@ main (int argc, char **argv) test_iterator (hb_zip (st, v)); test_iterator_non_default_constructable (hb_enumerate (st)); + test_iterator_non_default_constructable (hb_enumerate (st, -5)); test_iterator_non_default_constructable (hb_enumerate (hb_iter (st))); test_iterator_non_default_constructable (hb_enumerate (hb_iter (st) + 1)); test_iterator_non_default_constructable (hb_iter (st) | hb_filter ());