[array] Add chop()

Takes n, and returns iterator of iterators that contain up to
n items each.  Basically cuts the array into subarrays of size n.
The last sub-array might contain fewer.

Ideally this should become a generic iter tool, not array-specific,
so we can use it in GPOS to chop a value matrix into rows and elements.
This commit is contained in:
Behdad Esfahbod 2019-08-29 15:48:21 -07:00
parent c72589f13f
commit 545fe9d9f0
1 changed files with 14 additions and 0 deletions

View File

@ -183,6 +183,20 @@ struct hb_array_t : hb_iter_with_fallback_t<hb_array_t<Type>, Type&>
hb_array_t truncate (unsigned length) const { return sub_array (0, length); }
auto chop (unsigned n) const -> decltype
(
+ hb_range (0u, 0u, n)
| hb_map (hb_partial<1> (hb_add, *this))
| hb_map (hb_partial<2> (&hb_array_t::truncate, n))
)
{
return
+ hb_range (0u, length, n)
| hb_map (hb_partial<1> (hb_add, *this))
| hb_map (hb_partial<2> (&hb_array_t::truncate, n))
;
}
template <typename T,
unsigned P = sizeof (Type),
hb_enable_if (P == 1)>