Re-implement hb_set_t::del_range (#2194)
* optimize hb_set_del_range() fix issue #2193 * fixed bug & added tests * coding & comment tweaks
This commit is contained in:
commit
c400cb8863
|
@ -89,6 +89,23 @@ struct hb_set_t
|
|||
}
|
||||
}
|
||||
|
||||
void del_range (hb_codepoint_t a, hb_codepoint_t b)
|
||||
{
|
||||
elt_t *la = &elt (a);
|
||||
elt_t *lb = &elt (b);
|
||||
if (la == lb)
|
||||
*la &= ~((mask (b) << 1) - mask(a));
|
||||
else
|
||||
{
|
||||
*la &= mask (a) - 1;
|
||||
la++;
|
||||
|
||||
memset (la, 0, (char *) lb - (char *) la);
|
||||
|
||||
*lb &= ~((mask (b) << 1) - 1);
|
||||
}
|
||||
}
|
||||
|
||||
bool is_equal (const page_t *other) const
|
||||
{
|
||||
return 0 == hb_memcmp (&v, &other->v, sizeof (v));
|
||||
|
@ -366,14 +383,55 @@ struct hb_set_t
|
|||
dirty ();
|
||||
page->del (g);
|
||||
}
|
||||
|
||||
private:
|
||||
void del_pages (int ds, int de)
|
||||
{
|
||||
if (ds <= de)
|
||||
{
|
||||
unsigned int write_index = 0;
|
||||
for (unsigned int i = 0; i < page_map.length; i++)
|
||||
{
|
||||
int m = (int) page_map[i].major;
|
||||
if (m < ds || de < m)
|
||||
page_map[write_index++] = page_map[i];
|
||||
}
|
||||
compact (write_index);
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
void del_range (hb_codepoint_t a, hb_codepoint_t b)
|
||||
{
|
||||
/* TODO perform op even if !successful. */
|
||||
/* TODO Optimize, like add_range(). */
|
||||
if (unlikely (!successful)) return;
|
||||
for (unsigned int i = a; i < b + 1; i++)
|
||||
del (i);
|
||||
if (unlikely (a > b || a == INVALID || b == INVALID)) return;
|
||||
dirty ();
|
||||
unsigned int ma = get_major (a);
|
||||
unsigned int mb = get_major (b);
|
||||
/* Delete pages from ds through de if ds <= de. */
|
||||
int ds = (a == major_start (ma))? (int) ma: (int) (ma + 1);
|
||||
int de = (b + 1 == major_start (mb + 1))? (int) mb: ((int) mb - 1);
|
||||
if (ds > de || (int) ma < ds)
|
||||
{
|
||||
page_t *page = page_for (a);
|
||||
if (page)
|
||||
{
|
||||
if (ma == mb)
|
||||
page->del_range (a, b);
|
||||
else
|
||||
page->del_range (a, major_start (ma + 1) - 1);
|
||||
}
|
||||
}
|
||||
if (de < (int) mb && ma != mb)
|
||||
{
|
||||
page_t *page = page_for (b);
|
||||
if (page)
|
||||
page->del_range (major_start (mb), b);
|
||||
}
|
||||
del_pages (ds, de);
|
||||
}
|
||||
|
||||
bool get (hb_codepoint_t g) const
|
||||
{
|
||||
const page_t *page = page_for (g);
|
||||
|
|
|
@ -470,6 +470,53 @@ test_set_empty (void)
|
|||
hb_set_destroy (b);
|
||||
}
|
||||
|
||||
static void
|
||||
test_set_delrange (void)
|
||||
{
|
||||
const unsigned P = 512; /* Page size. */
|
||||
struct { unsigned b, e; } ranges[] = {
|
||||
{ 35, P-15 }, /* From page middle thru middle. */
|
||||
{ P, P+100 }, /* From page start thru middle. */
|
||||
{ P+300, P*2-1 }, /* From page middle thru end. */
|
||||
{ P*3, P*4+100 }, /* From page start thru next page middle. */
|
||||
{ P*4+300, P*6-1 }, /* From page middle thru next page end. */
|
||||
{ P*6+200,P*8+100 }, /* From page middle covering one page thru page middle. */
|
||||
{ P*9, P*10+105 }, /* From page start covering one page thru page middle. */
|
||||
{ P*10+305, P*12-1 }, /* From page middle covering one page thru page end. */
|
||||
{ P*13, P*15-1 }, /* From page start covering two pages thru page end. */
|
||||
{ P*15+100, P*18+100 } /* From page middle covering two pages thru page middle. */
|
||||
};
|
||||
unsigned n = sizeof (ranges) / sizeof(ranges[0]);
|
||||
|
||||
hb_set_t *s = hb_set_create ();
|
||||
|
||||
test_empty (s);
|
||||
for (unsigned int g = 0; g < ranges[n - 1].e + P; g += 2)
|
||||
hb_set_add (s, g);
|
||||
|
||||
hb_set_add (s, P*2-1);
|
||||
hb_set_add (s, P*6-1);
|
||||
hb_set_add (s, P*12-1);
|
||||
hb_set_add (s, P*15-1);
|
||||
|
||||
for (unsigned i = 0; i < n; i++)
|
||||
hb_set_del_range (s, ranges[i].b, ranges[i].e);
|
||||
|
||||
hb_set_del_range (s, P*13+5, P*15-10); /* Deletion from deleted pages. */
|
||||
|
||||
for (unsigned i = 0; i < n; i++)
|
||||
{
|
||||
unsigned b = ranges[i].b;
|
||||
unsigned e = ranges[i].e;
|
||||
g_assert (hb_set_has (s, (b-2)&~1));
|
||||
while (b <= e)
|
||||
g_assert (!hb_set_has (s, b++));
|
||||
g_assert (hb_set_has (s, (e+2)&~1));
|
||||
}
|
||||
|
||||
hb_set_destroy (s);
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
|
@ -479,6 +526,7 @@ main (int argc, char **argv)
|
|||
hb_test_add (test_set_algebra);
|
||||
hb_test_add (test_set_iter);
|
||||
hb_test_add (test_set_empty);
|
||||
hb_test_add (test_set_delrange);
|
||||
|
||||
hb_test_add (test_set_intersect_empty);
|
||||
hb_test_add (test_set_intersect_page_reduction);
|
||||
|
|
Loading…
Reference in New Issue