[iter] add specialized implementation for hb_concat forward when iterators are not random access.

This commit is contained in:
Garret Rieger 2021-11-30 16:16:06 -08:00 committed by Behdad Esfahbod
parent 2e935514d9
commit 071aea42c2
2 changed files with 34 additions and 5 deletions

View File

@ -607,9 +607,10 @@ struct hb_concat_iter_t :
__item_t__ __item_at__ (unsigned i) const
{
if (i < a.len())
unsigned a_len = a.len ();
if (i < a_len)
return a[i];
return b[i - a.len()];
return b[i - a_len];
}
bool __more__ () const { return bool (a) || bool (b); }
@ -626,9 +627,18 @@ struct hb_concat_iter_t :
void __forward__ (unsigned n)
{
if (n > a.len ()) {
n -= a.len ();
a.__forward__ (a.len ());
if (!n) return;
if (!is_random_access_iterator) {
while (n-- && *this) {
(*this)++;
}
return;
}
unsigned a_len = a.len ();
if (n > a_len) {
n -= a_len;
a.__forward__ (a_len);
b.__forward__ (n);
} else {
a.__forward__ (n);

View File

@ -133,6 +133,7 @@ static void test_concat ()
auto it1 = hb_concat (a, b);
assert (it1.len () == 5);
assert (it1.is_random_access_iterator);
auto it2 = hb_concat (c, d);
assert (it2.len () == 5);
auto it3 = hb_concat (d, c);
@ -148,6 +149,9 @@ static void test_concat ()
check_sequential (it3);
auto it4 = +it1;
it4 += 0;
assert (*it4 == 1);
it4 += 2;
assert (*it4 == 3);
assert (it4);
@ -165,6 +169,21 @@ static void test_concat ()
auto it5 = +it1;
it5 += 3;
assert (*it5 == 4);
hb_set_t s_a = {1, 2, 3};
hb_set_t s_b = {4, 5};
auto it6 = hb_concat (s_a, s_b);
assert (!it6.is_random_access_iterator);
check_sequential (it6);
assert (it6.len () == 5);
it6 += 0;
assert (*it6 == 1);
it6 += 3;
assert (*it6 == 4);
assert (it6);
assert (it6.len () == 2);
}
int