for (i=0; i<bitcount; i++)
check (tr_bitfieldHas (&field, i));
+ /* test tr_bitfieldRem */
+ for (i=0; i<bitcount; i++)
+ if ((i % 7) != 0)
+ tr_bitfieldRem (&field, i);
+ for (i=0; i<bitcount; i++)
+ check (tr_bitfieldHas (&field, i) == (! (i % 7)));
+
/* test tr_bitfieldRemRange in the middle of a boundary */
+ tr_bitfieldAddRange (&field, 0, 64);
tr_bitfieldRemRange (&field, 4, 21);
for (i=0; i<64; i++)
check (tr_bitfieldHas (&field, i) == ((i < 4) || (i >= 21)));
static void
tr_bitfieldSetTrueCount (tr_bitfield * b, size_t n)
{
+ assert (n <= b->bit_count);
+
b->true_count = n;
if (tr_bitfieldHasAll (b) || tr_bitfieldHasNone (b))
}
static void
-tr_bitfieldIncTrueCount (tr_bitfield * b, int i)
+tr_bitfieldIncTrueCount (tr_bitfield * b, size_t i)
{
+ assert (i <= b->bit_count);
+ assert (b->true_count <= b->bit_count - i);
+
tr_bitfieldSetTrueCount (b, b->true_count + i);
}
+static void
+tr_bitfieldDecTrueCount (tr_bitfield * b, size_t i)
+{
+ assert (i <= b->bit_count);
+ assert (b->true_count >= i);
+
+ tr_bitfieldSetTrueCount (b, b->true_count - i);
+}
+
/****
*****
****/
{
assert (tr_bitfieldIsValid (b));
- if (!tr_bitfieldHas (b, nth) && tr_bitfieldEnsureNthBitAlloced (b, nth))
+ if (tr_bitfieldHas (b, nth) && tr_bitfieldEnsureNthBitAlloced (b, nth))
{
b->bits[nth >> 3u] &= (0xff7f >> (nth & 7u));
- tr_bitfieldIncTrueCount (b, -1);
+ tr_bitfieldDecTrueCount (b, 1);
}
}
memset (b->bits + sb, 0, eb - sb);
}
- tr_bitfieldIncTrueCount (b, -diff);
+ tr_bitfieldDecTrueCount (b, diff);
}