bitset: clean up vbitset.c

* lib/vbitset.c: Reduce scopes, etc.
This commit is contained in:
Akim Demaille
2018-10-27 18:47:29 +02:00
parent d1f57a4ae5
commit fef272bdf3

View File

@@ -73,12 +73,7 @@ vbitset_resize (bitset src, bitset_bindex n_bits)
grow the bitset 25% larger than requested to reduce grow the bitset 25% larger than requested to reduce
number of reallocations. */ number of reallocations. */
bitset_windex size; bitset_windex size = oldsize == 0 ? newsize : newsize + newsize / 4;
if (oldsize == 0)
size = newsize;
else
size = newsize + newsize / 4;
VBITSET_WORDS (src) VBITSET_WORDS (src)
= realloc (VBITSET_WORDS (src), size * sizeof (bitset_word)); = realloc (VBITSET_WORDS (src), size * sizeof (bitset_word));
VBITSET_ASIZE (src) = size; VBITSET_ASIZE (src) = size;
@@ -142,7 +137,7 @@ vbitset_test (bitset src ATTRIBUTE_UNUSED,
{ {
/* We must be accessing outside the cache so the bit is /* We must be accessing outside the cache so the bit is
zero anyway. */ zero anyway. */
return 0; return false;
} }
@@ -206,15 +201,15 @@ static bitset_bindex
vbitset_list (bitset src, bitset_bindex *list, vbitset_list (bitset src, bitset_bindex *list,
bitset_bindex num, bitset_bindex *next) bitset_bindex num, bitset_bindex *next)
{ {
bitset_windex windex;
bitset_bindex bitoff;
bitset_windex size = VBITSET_SIZE (src); bitset_windex size = VBITSET_SIZE (src);
bitset_word *srcp = VBITSET_WORDS (src); bitset_word *srcp = VBITSET_WORDS (src);
bitset_bindex bitno = *next;
bitset_bindex count = 0;
bitset_windex windex;
bitset_bindex bitoff;
bitset_word word; bitset_word word;
bitset_bindex bitno = *next;
bitset_bindex count = 0;
if (!bitno) if (!bitno)
{ {
/* Many bitsets are zero, so make this common case fast. */ /* Many bitsets are zero, so make this common case fast. */
@@ -339,9 +334,8 @@ vbitset_empty_p (bitset dst)
for (unsigned i = 0; i < VBITSET_SIZE (dst); i++) for (unsigned i = 0; i < VBITSET_SIZE (dst); i++)
if (dstp[i]) if (dstp[i])
return 0; return false;
return true;
return 1;
} }
@@ -349,7 +343,7 @@ static void
vbitset_copy1 (bitset dst, bitset src) vbitset_copy1 (bitset dst, bitset src)
{ {
if (src == dst) if (src == dst)
return; return;
vbitset_resize (dst, BITSET_SIZE_ (src)); vbitset_resize (dst, BITSET_SIZE_ (src));
@@ -395,22 +389,22 @@ vbitset_equal_p (bitset dst, bitset src)
unsigned i; unsigned i;
for (i = 0; i < min (ssize, dsize); i++) for (i = 0; i < min (ssize, dsize); i++)
if (*srcp++ != *dstp++) if (*srcp++ != *dstp++)
return 0; return false;
if (ssize > dsize) if (ssize > dsize)
{ {
for (; i < ssize; i++) for (; i < ssize; i++)
if (*srcp++) if (*srcp++)
return 0; return false;
} }
else else
{ {
for (; i < dsize; i++) for (; i < dsize; i++)
if (*dstp++) if (*dstp++)
return 0; return false;
} }
return 1; return true;
} }
@@ -431,10 +425,10 @@ vbitset_subset_p (bitset dst, bitset src)
{ {
for (; i < ssize; i++) for (; i < ssize; i++)
if (*srcp++) if (*srcp++)
return 0; return false;
} }
return 1; return true;
} }
@@ -448,9 +442,9 @@ vbitset_disjoint_p (bitset dst, bitset src)
for (unsigned i = 0; i < min (ssize, dsize); i++) for (unsigned i = 0; i < min (ssize, dsize); i++)
if (*srcp++ & *dstp++) if (*srcp++ & *dstp++)
return 0; return false;
return 1; return true;
} }
@@ -476,7 +470,7 @@ vbitset_and (bitset dst, bitset src1, bitset src2)
static bool static bool
vbitset_and_cmp (bitset dst, bitset src1, bitset src2) vbitset_and_cmp (bitset dst, bitset src1, bitset src2)
{ {
int changed = 0; bool changed = false;
vbitset_resize (dst, max (BITSET_SIZE_ (src1), BITSET_SIZE_ (src2))); vbitset_resize (dst, max (BITSET_SIZE_ (src1), BITSET_SIZE_ (src2)));
bitset_windex dsize = VBITSET_SIZE (dst); bitset_windex dsize = VBITSET_SIZE (dst);
@@ -493,7 +487,7 @@ vbitset_and_cmp (bitset dst, bitset src1, bitset src2)
if (*dstp != tmp) if (*dstp != tmp)
{ {
changed = 1; changed = true;
*dstp = tmp; *dstp = tmp;
} }
} }
@@ -508,7 +502,7 @@ vbitset_and_cmp (bitset dst, bitset src1, bitset src2)
{ {
if (*dstp != 0) if (*dstp != 0)
{ {
changed = 1; changed = true;
*dstp = 0; *dstp = 0;
} }
} }
@@ -555,7 +549,7 @@ vbitset_andn (bitset dst, bitset src1, bitset src2)
static bool static bool
vbitset_andn_cmp (bitset dst, bitset src1, bitset src2) vbitset_andn_cmp (bitset dst, bitset src1, bitset src2)
{ {
int changed = 0; bool changed = false;
vbitset_resize (dst, max (BITSET_SIZE_ (src1), BITSET_SIZE_ (src2))); vbitset_resize (dst, max (BITSET_SIZE_ (src1), BITSET_SIZE_ (src2)));
bitset_windex dsize = VBITSET_SIZE (dst); bitset_windex dsize = VBITSET_SIZE (dst);
@@ -572,7 +566,7 @@ vbitset_andn_cmp (bitset dst, bitset src1, bitset src2)
if (*dstp != tmp) if (*dstp != tmp)
{ {
changed = 1; changed = true;
*dstp = tmp; *dstp = tmp;
} }
} }
@@ -583,7 +577,7 @@ vbitset_andn_cmp (bitset dst, bitset src1, bitset src2)
{ {
if (*dstp != 0) if (*dstp != 0)
{ {
changed = 1; changed = true;
*dstp = 0; *dstp = 0;
} }
} }
@@ -598,7 +592,7 @@ vbitset_andn_cmp (bitset dst, bitset src1, bitset src2)
if (*dstp != tmp) if (*dstp != tmp)
{ {
changed = 1; changed = true;
*dstp = tmp; *dstp = tmp;
} }
} }
@@ -642,7 +636,7 @@ vbitset_or (bitset dst, bitset src1, bitset src2)
static bool static bool
vbitset_or_cmp (bitset dst, bitset src1, bitset src2) vbitset_or_cmp (bitset dst, bitset src1, bitset src2)
{ {
int changed = 0; bool changed = false;
vbitset_resize (dst, max (BITSET_SIZE_ (src1), BITSET_SIZE_ (src2))); vbitset_resize (dst, max (BITSET_SIZE_ (src1), BITSET_SIZE_ (src2)));
@@ -660,7 +654,7 @@ vbitset_or_cmp (bitset dst, bitset src1, bitset src2)
if (*dstp != tmp) if (*dstp != tmp)
{ {
changed = 1; changed = true;
*dstp = tmp; *dstp = tmp;
} }
} }
@@ -677,7 +671,7 @@ vbitset_or_cmp (bitset dst, bitset src1, bitset src2)
if (*dstp != tmp) if (*dstp != tmp)
{ {
changed = 1; changed = true;
*dstp = tmp; *dstp = tmp;
} }
} }
@@ -720,7 +714,7 @@ vbitset_xor (bitset dst, bitset src1, bitset src2)
static bool static bool
vbitset_xor_cmp (bitset dst, bitset src1, bitset src2) vbitset_xor_cmp (bitset dst, bitset src1, bitset src2)
{ {
int changed = 0; bool changed = false;
vbitset_resize (dst, max (BITSET_SIZE_ (src1), BITSET_SIZE_ (src2))); vbitset_resize (dst, max (BITSET_SIZE_ (src1), BITSET_SIZE_ (src2)));
bitset_windex dsize = VBITSET_SIZE (dst); bitset_windex dsize = VBITSET_SIZE (dst);
@@ -737,7 +731,7 @@ vbitset_xor_cmp (bitset dst, bitset src1, bitset src2)
if (*dstp != tmp) if (*dstp != tmp)
{ {
changed = 1; changed = true;
*dstp = tmp; *dstp = tmp;
} }
} }
@@ -754,7 +748,7 @@ vbitset_xor_cmp (bitset dst, bitset src1, bitset src2)
if (*dstp != tmp) if (*dstp != tmp)
{ {
changed = 1; changed = true;
*dstp = tmp; *dstp = tmp;
} }
} }
@@ -771,12 +765,6 @@ vbitset_xor_cmp (bitset dst, bitset src1, bitset src2)
static void static void
vbitset_and_or (bitset dst, bitset src1, bitset src2, bitset src3) vbitset_and_or (bitset dst, bitset src1, bitset src2, bitset src3)
{ {
bitset_word *src1p;
bitset_word *src2p;
bitset_word *src3p;
bitset_word *dstp;
bitset_windex size;
if (BITSET_NBITS_ (src1) != BITSET_NBITS_ (src2) if (BITSET_NBITS_ (src1) != BITSET_NBITS_ (src2)
|| BITSET_NBITS_ (src1) != BITSET_NBITS_ (src3)) || BITSET_NBITS_ (src1) != BITSET_NBITS_ (src3))
{ {
@@ -786,11 +774,11 @@ vbitset_and_or (bitset dst, bitset src1, bitset src2, bitset src3)
vbitset_resize (dst, BITSET_NBITS_ (src1)); vbitset_resize (dst, BITSET_NBITS_ (src1));
src1p = VBITSET_WORDS (src1); bitset_word *src1p = VBITSET_WORDS (src1);
src2p = VBITSET_WORDS (src2); bitset_word *src2p = VBITSET_WORDS (src2);
src3p = VBITSET_WORDS (src3); bitset_word *src3p = VBITSET_WORDS (src3);
dstp = VBITSET_WORDS (dst); bitset_word *dstp = VBITSET_WORDS (dst);
size = VBITSET_SIZE (dst); bitset_windex size = VBITSET_SIZE (dst);
for (unsigned i = 0; i < size; i++) for (unsigned i = 0; i < size; i++)
*dstp++ = (*src1p++ & *src2p++) | *src3p++; *dstp++ = (*src1p++ & *src2p++) | *src3p++;
@@ -800,32 +788,26 @@ vbitset_and_or (bitset dst, bitset src1, bitset src2, bitset src3)
static bool static bool
vbitset_and_or_cmp (bitset dst, bitset src1, bitset src2, bitset src3) vbitset_and_or_cmp (bitset dst, bitset src1, bitset src2, bitset src3)
{ {
int changed = 0;
bitset_word *src1p;
bitset_word *src2p;
bitset_word *src3p;
bitset_word *dstp;
bitset_windex size;
if (BITSET_NBITS_ (src1) != BITSET_NBITS_ (src2) if (BITSET_NBITS_ (src1) != BITSET_NBITS_ (src2)
|| BITSET_NBITS_ (src1) != BITSET_NBITS_ (src3)) || BITSET_NBITS_ (src1) != BITSET_NBITS_ (src3))
return bitset_and_or_cmp_ (dst, src1, src2, src3); return bitset_and_or_cmp_ (dst, src1, src2, src3);
vbitset_resize (dst, BITSET_NBITS_ (src1)); vbitset_resize (dst, BITSET_NBITS_ (src1));
src1p = VBITSET_WORDS (src1); bitset_word *src1p = VBITSET_WORDS (src1);
src2p = VBITSET_WORDS (src2); bitset_word *src2p = VBITSET_WORDS (src2);
src3p = VBITSET_WORDS (src3); bitset_word *src3p = VBITSET_WORDS (src3);
dstp = VBITSET_WORDS (dst); bitset_word *dstp = VBITSET_WORDS (dst);
size = VBITSET_SIZE (dst); bitset_windex size = VBITSET_SIZE (dst);
bool changed = false;
for (unsigned i = 0; i < size; i++, dstp++) for (unsigned i = 0; i < size; i++, dstp++)
{ {
bitset_word tmp = (*src1p++ & *src2p++) | *src3p++; bitset_word tmp = (*src1p++ & *src2p++) | *src3p++;
if (*dstp != tmp) if (*dstp != tmp)
{ {
changed = 1; changed = true;
*dstp = tmp; *dstp = tmp;
} }
} }
@@ -836,12 +818,6 @@ vbitset_and_or_cmp (bitset dst, bitset src1, bitset src2, bitset src3)
static void static void
vbitset_andn_or (bitset dst, bitset src1, bitset src2, bitset src3) vbitset_andn_or (bitset dst, bitset src1, bitset src2, bitset src3)
{ {
bitset_word *src1p;
bitset_word *src2p;
bitset_word *src3p;
bitset_word *dstp;
bitset_windex size;
if (BITSET_NBITS_ (src1) != BITSET_NBITS_ (src2) if (BITSET_NBITS_ (src1) != BITSET_NBITS_ (src2)
|| BITSET_NBITS_ (src1) != BITSET_NBITS_ (src3)) || BITSET_NBITS_ (src1) != BITSET_NBITS_ (src3))
{ {
@@ -851,11 +827,11 @@ vbitset_andn_or (bitset dst, bitset src1, bitset src2, bitset src3)
vbitset_resize (dst, BITSET_NBITS_ (src1)); vbitset_resize (dst, BITSET_NBITS_ (src1));
src1p = VBITSET_WORDS (src1); bitset_word *src1p = VBITSET_WORDS (src1);
src2p = VBITSET_WORDS (src2); bitset_word *src2p = VBITSET_WORDS (src2);
src3p = VBITSET_WORDS (src3); bitset_word *src3p = VBITSET_WORDS (src3);
dstp = VBITSET_WORDS (dst); bitset_word *dstp = VBITSET_WORDS (dst);
size = VBITSET_SIZE (dst); bitset_windex size = VBITSET_SIZE (dst);
for (unsigned i = 0; i < size; i++) for (unsigned i = 0; i < size; i++)
*dstp++ = (*src1p++ & ~(*src2p++)) | *src3p++; *dstp++ = (*src1p++ & ~(*src2p++)) | *src3p++;
@@ -865,32 +841,26 @@ vbitset_andn_or (bitset dst, bitset src1, bitset src2, bitset src3)
static bool static bool
vbitset_andn_or_cmp (bitset dst, bitset src1, bitset src2, bitset src3) vbitset_andn_or_cmp (bitset dst, bitset src1, bitset src2, bitset src3)
{ {
int changed = 0;
bitset_word *src1p;
bitset_word *src2p;
bitset_word *src3p;
bitset_word *dstp;
bitset_windex size;
if (BITSET_NBITS_ (src1) != BITSET_NBITS_ (src2) if (BITSET_NBITS_ (src1) != BITSET_NBITS_ (src2)
|| BITSET_NBITS_ (src1) != BITSET_NBITS_ (src3)) || BITSET_NBITS_ (src1) != BITSET_NBITS_ (src3))
return bitset_andn_or_cmp_ (dst, src1, src2, src3); return bitset_andn_or_cmp_ (dst, src1, src2, src3);
vbitset_resize (dst, BITSET_NBITS_ (src1)); vbitset_resize (dst, BITSET_NBITS_ (src1));
src1p = VBITSET_WORDS (src1); bitset_word *src1p = VBITSET_WORDS (src1);
src2p = VBITSET_WORDS (src2); bitset_word *src2p = VBITSET_WORDS (src2);
src3p = VBITSET_WORDS (src3); bitset_word *src3p = VBITSET_WORDS (src3);
dstp = VBITSET_WORDS (dst); bitset_word *dstp = VBITSET_WORDS (dst);
size = VBITSET_SIZE (dst); bitset_windex size = VBITSET_SIZE (dst);
bool changed = false;
for (unsigned i = 0; i < size; i++, dstp++) for (unsigned i = 0; i < size; i++, dstp++)
{ {
bitset_word tmp = (*src1p++ & ~(*src2p++)) | *src3p++; bitset_word tmp = (*src1p++ & ~(*src2p++)) | *src3p++;
if (*dstp != tmp) if (*dstp != tmp)
{ {
changed = 1; changed = true;
*dstp = tmp; *dstp = tmp;
} }
} }
@@ -924,33 +894,27 @@ vbitset_or_and (bitset dst, bitset src1, bitset src2, bitset src3)
static bool static bool
vbitset_or_and_cmp (bitset dst, bitset src1, bitset src2, bitset src3) vbitset_or_and_cmp (bitset dst, bitset src1, bitset src2, bitset src3)
{ {
unsigned i;
int changed = 0;
bitset_word *src1p;
bitset_word *src2p;
bitset_word *src3p;
bitset_word *dstp;
bitset_windex size;
if (BITSET_NBITS_ (src1) != BITSET_NBITS_ (src2) if (BITSET_NBITS_ (src1) != BITSET_NBITS_ (src2)
|| BITSET_NBITS_ (src1) != BITSET_NBITS_ (src3)) || BITSET_NBITS_ (src1) != BITSET_NBITS_ (src3))
return bitset_or_and_cmp_ (dst, src1, src2, src3); return bitset_or_and_cmp_ (dst, src1, src2, src3);
vbitset_resize (dst, BITSET_NBITS_ (src1)); vbitset_resize (dst, BITSET_NBITS_ (src1));
src1p = VBITSET_WORDS (src1); bitset_word *src1p = VBITSET_WORDS (src1);
src2p = VBITSET_WORDS (src2); bitset_word *src2p = VBITSET_WORDS (src2);
src3p = VBITSET_WORDS (src3); bitset_word *src3p = VBITSET_WORDS (src3);
dstp = VBITSET_WORDS (dst); bitset_word *dstp = VBITSET_WORDS (dst);
size = VBITSET_SIZE (dst); bitset_windex size = VBITSET_SIZE (dst);
unsigned i;
bool changed = false;
for (i = 0; i < size; i++, dstp++) for (i = 0; i < size; i++, dstp++)
{ {
bitset_word tmp = (*src1p++ | *src2p++) & *src3p++; bitset_word tmp = (*src1p++ | *src2p++) & *src3p++;
if (*dstp != tmp) if (*dstp != tmp)
{ {
changed = 1; changed = true;
*dstp = tmp; *dstp = tmp;
} }
} }
@@ -962,9 +926,9 @@ static void
vbitset_copy (bitset dst, bitset src) vbitset_copy (bitset dst, bitset src)
{ {
if (BITSET_COMPATIBLE_ (dst, src)) if (BITSET_COMPATIBLE_ (dst, src))
vbitset_copy1 (dst, src); vbitset_copy1 (dst, src);
else else
bitset_copy_ (dst, src); bitset_copy_ (dst, src);
} }
@@ -1017,9 +981,7 @@ bitset
vbitset_init (bitset bset, bitset_bindex n_bits) vbitset_init (bitset bset, bitset_bindex n_bits)
{ {
bset->b.vtable = &vbitset_vtable; bset->b.vtable = &vbitset_vtable;
bset->b.cindex = 0; bset->b.cindex = 0;
VBITSET_SIZE (bset) = 0; VBITSET_SIZE (bset) = 0;
vbitset_resize (bset, n_bits); vbitset_resize (bset, n_bits);
return bset; return bset;