[iter] Add hb_zip()
authorBehdad Esfahbod <behdad@behdad.org>
Wed, 9 Jan 2019 07:57:16 +0000 (23:57 -0800)
committerBehdad Esfahbod <behdad@behdad.org>
Mon, 21 Jan 2019 01:12:12 +0000 (20:12 -0500)
src/hb-iter.hh
src/test-iter.cc

index c69b03d..657e707 100644 (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 */
index e5abef4..e6beb07 100644 (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 ();