[iter] Add hb_repeat()

This commit is contained in:
Behdad Esfahbod 2019-08-30 12:10:45 -05:00
parent 966a18b92a
commit b1378d8a21
2 changed files with 36 additions and 4 deletions

View File

@ -602,7 +602,7 @@ struct
}
HB_FUNCOBJ (hb_apply);
/* hb_range()/hb_iota() */
/* hb_range()/hb_iota()/hb_repeat() */
template <typename T, typename S>
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 <typename T = unsigned, typename S = unsigned> hb_iota_iter_t<T, S>
@ -687,6 +686,37 @@ struct
}
HB_FUNCOBJ (hb_iota);
template <typename T>
struct hb_repeat_iter_t :
hb_iter_t<hb_repeat_iter_t<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 <typename T> hb_repeat_iter_t<T>
operator () (T value) const
{ return hb_repeat_iter_t<T> (value); }
}
HB_FUNCOBJ (hb_repeat);
/* hb_enumerate */
struct
@ -699,7 +729,6 @@ struct
}
HB_FUNCOBJ (hb_enumerate);
/* hb_sink() */
template <typename Sink>

View File

@ -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);