mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-22 15:31:05 +01:00
[set] Compute iterator length lazily
Set iteration does not always need the remaining item count. Defer the population scan until len() is first requested, using unsigned accounting to track advances without adding a branch to the iterator hot path. This preserves fast length for iterator consumers that use it while avoiding unnecessary population scans for the others. Noto Nastaliq dependency construction is instruction-neutral because its existing set iterators already encounter cached populations. Tested with: meson test -C build Assisted-by: OpenAI Codex <codex@openai.com>
This commit is contained in:
1 parent
375b3f4b33
commit
7951998565
2 files changed
+49
-29
No files matched your search
@@ -430,20 +430,56 @@ struct hb_bit_set_invertible_t
|
||||
static constexpr bool is_sorted_iterator = true;
|
||||
static constexpr bool has_fast_len = true;
|
||||
iter_t (const hb_bit_set_invertible_t &s_ = Null (hb_bit_set_invertible_t),
|
||||
bool init = true) : s (&s_), v (INVALID), l (0),
|
||||
base (INVALID), bits (0)
|
||||
bool init = true) : s (&s_), v (INVALID), remaining (0),
|
||||
have_len (!init), base (INVALID), bits (0)
|
||||
{
|
||||
if (init)
|
||||
{
|
||||
l = s->get_population () + 1;
|
||||
__next__ ();
|
||||
}
|
||||
advance ();
|
||||
}
|
||||
|
||||
typedef hb_codepoint_t __item_t__;
|
||||
hb_codepoint_t __item__ () const { return v; }
|
||||
bool __more__ () const { return v != INVALID; }
|
||||
void __next__ ()
|
||||
{
|
||||
remaining--;
|
||||
advance ();
|
||||
}
|
||||
void __prev__ ()
|
||||
{
|
||||
if (s->previous (&v))
|
||||
{
|
||||
sync ();
|
||||
remaining++;
|
||||
}
|
||||
else
|
||||
{
|
||||
base = INVALID;
|
||||
bits = 0;
|
||||
}
|
||||
}
|
||||
unsigned __len__ () const
|
||||
{
|
||||
if (unlikely (!have_len))
|
||||
{
|
||||
remaining += s->get_population ();
|
||||
have_len = true;
|
||||
}
|
||||
return remaining;
|
||||
}
|
||||
iter_t end () const { return iter_t (*s, false); }
|
||||
bool operator != (const iter_t& o) const
|
||||
{ return v != o.v; }
|
||||
|
||||
protected:
|
||||
const hb_bit_set_invertible_t *s;
|
||||
hb_codepoint_t v;
|
||||
mutable unsigned remaining;
|
||||
mutable bool have_len;
|
||||
hb_codepoint_t base;
|
||||
uint64_t bits;
|
||||
|
||||
void advance ()
|
||||
{
|
||||
if (likely (bits))
|
||||
{
|
||||
@@ -457,30 +493,7 @@ struct hb_bit_set_invertible_t
|
||||
}
|
||||
else
|
||||
v = INVALID;
|
||||
if (likely (l)) l--;
|
||||
}
|
||||
void __prev__ ()
|
||||
{
|
||||
if (s->previous (&v))
|
||||
sync ();
|
||||
else
|
||||
{
|
||||
base = INVALID;
|
||||
bits = 0;
|
||||
}
|
||||
l++;
|
||||
}
|
||||
unsigned __len__ () const { return l; }
|
||||
iter_t end () const { return iter_t (*s, false); }
|
||||
bool operator != (const iter_t& o) const
|
||||
{ return v != o.v; }
|
||||
|
||||
protected:
|
||||
const hb_bit_set_invertible_t *s;
|
||||
hb_codepoint_t v;
|
||||
unsigned l;
|
||||
hb_codepoint_t base;
|
||||
uint64_t bits;
|
||||
|
||||
void sync ()
|
||||
{
|
||||
|
||||
@@ -218,8 +218,10 @@ main (int argc, char **argv)
|
||||
|
||||
auto it = s.iter ();
|
||||
hb_always_assert (*it == 0);
|
||||
hb_always_assert (it.len () == ARRAY_LENGTH (expected));
|
||||
++it;
|
||||
hb_always_assert (*it == 1);
|
||||
hb_always_assert (it.len () == ARRAY_LENGTH (expected) - 1);
|
||||
--it;
|
||||
hb_always_assert (*it == 0);
|
||||
++it;
|
||||
@@ -240,6 +242,10 @@ main (int argc, char **argv)
|
||||
hb_always_assert (*it == 1024);
|
||||
++it;
|
||||
hb_always_assert (*it == HB_SET_VALUE_INVALID - 1);
|
||||
|
||||
it = s.iter ();
|
||||
it += 2;
|
||||
hb_always_assert (it.len () == ARRAY_LENGTH (expected) - 2);
|
||||
}
|
||||
|
||||
/* Test inverted-set iteration. */
|
||||
@@ -248,6 +254,7 @@ main (int argc, char **argv)
|
||||
s.invert ();
|
||||
auto it = s.iter ();
|
||||
hb_always_assert (*it == 2);
|
||||
hb_always_assert (it.len () == HB_SET_VALUE_INVALID - 4);
|
||||
++it;
|
||||
hb_always_assert (*it == 3);
|
||||
--it;
|
||||
|
||||
Reference in new issue
Block a user