From 7675d0d3a6cc13ade1a2047019ef7fac8c373a3c Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 9 May 2019 11:02:56 -0700 Subject: [PATCH] [iter] Add hb_range() hb_range() is like Python range. hb_iota() has slightly different API. Ie. it takes a start, instead of end. --- src/hb-iter.hh | 29 ++++++++++++++++++----------- src/test-iter.cc | 19 +++++++++++-------- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/hb-iter.hh b/src/hb-iter.hh index 9748554..2557d3a 100644 --- a/src/hb-iter.hh +++ b/src/hb-iter.hh @@ -572,13 +572,13 @@ struct } HB_FUNCOBJ (hb_apply); -/* hb_iota() */ +/* hb_iota()/hb_range() */ template -struct hb_iota_iter_t : - hb_iter_t, T> +struct hb_counter_iter_t : + hb_iter_t, T> { - hb_iota_iter_t (T start, T end_, S step) : v (start), end_ (end__for (start, end_, step)), step (step) {} + hb_counter_iter_t (T start, T end_, S step) : v (start), end_ (end__for (start, end_, step)), step (step) {} typedef T __item_t__; static constexpr bool is_random_access_iterator = true; @@ -591,8 +591,8 @@ 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 { hb_iota_iter_t (end_, end_, step); } - bool operator != (const hb_iota_iter_t& o) const + hb_counter_iter_t __end___ () const { hb_counter_iter_t (end_, end_, step); } + bool operator != (const hb_counter_iter_t& o) const { return v != o.v || end_ != o.end_ || step != o.step; } private: @@ -612,15 +612,22 @@ struct hb_iota_iter_t : }; struct { - template hb_iota_iter_t + template hb_counter_iter_t + operator () (T start = 0u, S&& step = 1u) const + { return hb_counter_iter_t (start, hb_int_max (T), step); } +} +HB_FUNCOBJ (hb_iota); +struct +{ + template hb_counter_iter_t operator () (T end = (unsigned) -1) const - { return hb_iota_iter_t (0, end, 1u); } + { return hb_counter_iter_t (0, end, 1u); } - template hb_iota_iter_t + template hb_counter_iter_t operator () (T start, T end, S&& step = 1u) const - { return hb_iota_iter_t (start, end, step); } + { return hb_counter_iter_t (start, end, step); } } -HB_FUNCOBJ (hb_iota); +HB_FUNCOBJ (hb_range); /* hb_sink() */ diff --git a/src/test-iter.cc b/src/test-iter.cc index 944c234..8f19789 100644 --- a/src/test-iter.cc +++ b/src/test-iter.cc @@ -264,14 +264,17 @@ main (int argc, char **argv) s >> vl; hb_iota (); - assert (hb_iota (9).len () == 9); - assert (hb_iota (2, 9).len () == 7); - assert (hb_iota (2, 9, 3).len () == 3); - assert (hb_iota (2, 8, 3).len () == 2); - assert (hb_iota (2, 7, 3).len () == 2); - assert (hb_iota (-2, -9, -3).len () == 3); - assert (hb_iota (-2, -8, -3).len () == 2); - assert (hb_iota (-2, -7, -3).len () == 2); + hb_iota (3); + hb_iota (3, 2); + hb_range (); + assert (hb_range (9).len () == 9); + assert (hb_range (2, 9).len () == 7); + assert (hb_range (2, 9, 3).len () == 3); + assert (hb_range (2, 8, 3).len () == 2); + assert (hb_range (2, 7, 3).len () == 2); + assert (hb_range (-2, -9, -3).len () == 3); + assert (hb_range (-2, -8, -3).len () == 2); + assert (hb_range (-2, -7, -3).len () == 2); return 0; } -- 2.7.4