[iter] Add hb_zip()

This commit is contained in:
Behdad Esfahbod 2019-01-08 23:57:16 -08:00
parent 636786ecaf
commit 84e5d00229
2 changed files with 36 additions and 0 deletions

View File

@ -218,4 +218,38 @@ hb_copy (D id, S is)
}
template <typename A, typename B>
struct hb_zip_t :
hb_iter_t<hb_zip_t<A, B>, hb_pair_t<typename A::item_t, typename B::item_t> >
{
hb_zip_t () {}
hb_zip_t (A a, B b) : a (a), b (b) {}
typedef hb_pair_t<typename A::item_t, typename B::item_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 <typename A, typename B> inline
typename hb_enable_if<hb_is_iterable (A) && hb_is_iterable (B),
hb_zip_t<typename A::iter_t, typename B::iter_t> >::type
hb_zip (A& a, B &b)
{ return hb_zip_t<typename A::iter_t, typename B::iter_t> (a.iter (), b.iter ()); }
#endif /* HB_ITER_HH */

View File

@ -127,6 +127,8 @@ main (int argc, char **argv)
test_iterable<hb_set_t> ();
test_iterable<OT::Coverage> ();
test_iterator (hb_zip (st, v));
hb_array_t<hb_vector_t<int> > pa;
pa->as_array ();