From 84e5d002290eb2f58392743bc841fa7def7fc96d Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 8 Jan 2019 23:57:16 -0800 Subject: [PATCH] [iter] Add hb_zip() --- src/hb-iter.hh | 34 ++++++++++++++++++++++++++++++++++ src/test-iter.cc | 2 ++ 2 files changed, 36 insertions(+) diff --git a/src/hb-iter.hh b/src/hb-iter.hh index c69b03d..657e707 100644 --- a/src/hb-iter.hh +++ b/src/hb-iter.hh @@ -218,4 +218,38 @@ hb_copy (D id, S is) } +template +struct hb_zip_t : + hb_iter_t, hb_pair_t > +{ + hb_zip_t () {} + hb_zip_t (A a, B b) : a (a), b (b) {} + + typedef hb_pair_t __item_t__; + enum { is_random_access_iterator = + A::is_random_access_iterator && + B::is_random_access_iterator }; + enum { is_sorted_iterator = + A::is_sorted_iterator && + B::is_sorted_iterator }; + __item_t__ __item__ () const { return __item_t__ (*a, *b); } + __item_t__ __item_at__ (unsigned i) const { return __item_t__ (a[i], b[i]); } + bool __more__ () const { return a && b; } + unsigned __len__ () const { return MIN (a.len (), b.len ()); } + void __next__ () { ++a; ++b; } + void __forward__ (unsigned n) { a += n; b += n; } + void __prev__ () { --a; --b; } + void __rewind__ (unsigned n) { a -= n; b -= n; } + + private: + A a; + B b; +}; + +template inline +typename hb_enable_if >::type +hb_zip (A& a, B &b) +{ return hb_zip_t (a.iter (), b.iter ()); } + #endif /* HB_ITER_HH */ diff --git a/src/test-iter.cc b/src/test-iter.cc index e5abef4..e6beb07 100644 --- a/src/test-iter.cc +++ b/src/test-iter.cc @@ -127,6 +127,8 @@ main (int argc, char **argv) test_iterable (); test_iterable (); + test_iterator (hb_zip (st, v)); + hb_array_t > pa; pa->as_array (); -- 2.7.4