From 1f88dae9f54d18cd740f149d020b2bb435dc9378 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 31 Aug 2019 12:24:42 -0500 Subject: [PATCH] [iter] Make iota() accept invokable for increasing to next item --- src/hb-iter.hh | 21 ++++++++++++++++----- src/test-iter.cc | 1 + 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/hb-iter.hh b/src/hb-iter.hh index a0f57ef7f..f2d81e9e5 100644 --- a/src/hb-iter.hh +++ b/src/hb-iter.hh @@ -656,21 +656,32 @@ HB_FUNCOBJ (hb_range); template struct hb_iota_iter_t : - hb_iter_t, T> + hb_iter_with_fallback_t, T> { hb_iota_iter_t (T start, S step) : v (start), step (step) {} + private: + + template + auto + inc (S s, hb_priority<1>) + -> hb_void_t (s), hb_declval ()))> + { v = hb_invoke (hb_forward (s), v); } + + void + inc (S s, hb_priority<0>) + { v += s; } + + public: + typedef T __item_t__; static constexpr bool is_random_access_iterator = true; static constexpr bool is_sorted_iterator = true; __item_t__ __item__ () const { return hb_ridentity (v); } - __item_t__ __item_at__ (unsigned j) const { return v + j * step; } bool __more__ () const { return true; } unsigned __len__ () const { return UINT_MAX; } - void __next__ () { v += step; } - void __forward__ (unsigned n) { v += n * step; } + void __next__ () { inc (step, hb_prioritize); } void __prev__ () { v -= step; } - void __rewind__ (unsigned n) { v -= n * step; } hb_iota_iter_t __end__ () const { return *this; } bool operator != (const hb_iota_iter_t& o) const { return true; } diff --git a/src/test-iter.cc b/src/test-iter.cc index f53a71745..0225d976a 100644 --- a/src/test-iter.cc +++ b/src/test-iter.cc @@ -267,6 +267,7 @@ main (int argc, char **argv) hb_iota (); hb_iota (3); hb_iota (3, 2); + assert ((&vl) + 1 == *++hb_iota (&vl, hb_inc)); hb_range (); hb_repeat (7u); hb_repeat (nullptr);