From b1378d8a217a53e17562abebee22276e09528f8c Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Aug 2019 12:10:45 -0500 Subject: [PATCH] [iter] Add hb_repeat() --- src/hb-iter.hh | 37 +++++++++++++++++++++++++++++++++---- src/test-iter.cc | 3 +++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/hb-iter.hh b/src/hb-iter.hh index 099fd5bea..a0f57ef7f 100644 --- a/src/hb-iter.hh +++ b/src/hb-iter.hh @@ -602,7 +602,7 @@ struct } HB_FUNCOBJ (hb_apply); -/* hb_range()/hb_iota() */ +/* hb_range()/hb_iota()/hb_repeat() */ template struct hb_range_iter_t : @@ -671,14 +671,13 @@ struct hb_iota_iter_t : void __forward__ (unsigned n) { v += n * step; } void __prev__ () { v -= step; } void __rewind__ (unsigned n) { v -= n * step; } - hb_iota_iter_t __end__ () const { return hb_iota_iter_t (v, step); } + hb_iota_iter_t __end__ () const { return *this; } bool operator != (const hb_iota_iter_t& o) const { return true; } private: T v; S step; }; - struct { template hb_iota_iter_t @@ -687,6 +686,37 @@ struct } HB_FUNCOBJ (hb_iota); +template +struct hb_repeat_iter_t : + hb_iter_t, T> +{ + hb_repeat_iter_t (T value) : v (value) {} + + typedef T __item_t__; + static constexpr bool is_random_access_iterator = true; + static constexpr bool is_sorted_iterator = true; + __item_t__ __item__ () const { return v; } + __item_t__ __item_at__ (unsigned j) const { return v; } + bool __more__ () const { return true; } + unsigned __len__ () const { return UINT_MAX; } + void __next__ () {} + void __forward__ (unsigned) {} + void __prev__ () {} + void __rewind__ (unsigned) {} + hb_repeat_iter_t __end__ () const { return *this; } + bool operator != (const hb_repeat_iter_t& o) const { return true; } + + private: + T v; +}; +struct +{ + template hb_repeat_iter_t + operator () (T value) const + { return hb_repeat_iter_t (value); } +} +HB_FUNCOBJ (hb_repeat); + /* hb_enumerate */ struct @@ -699,7 +729,6 @@ struct } HB_FUNCOBJ (hb_enumerate); - /* hb_sink() */ template diff --git a/src/test-iter.cc b/src/test-iter.cc index 156a5fcad..f53a71745 100644 --- a/src/test-iter.cc +++ b/src/test-iter.cc @@ -268,6 +268,9 @@ main (int argc, char **argv) hb_iota (3); hb_iota (3, 2); hb_range (); + hb_repeat (7u); + hb_repeat (nullptr); + hb_repeat (vl); assert (hb_range (9).len () == 9); assert (hb_range (2, 9).len () == 7); assert (hb_range (2, 9, 3).len () == 3);