bitmask: Make setters return a new value

(Actually, it's not a real 'new' value yet, but will be soon.

This is the first step to make bitmasks immutable.
This commit is contained in:
Benjamin Otte 2012-02-08 19:19:59 +01:00
parent 47402cc26a
commit 64425a28de
5 changed files with 63 additions and 54 deletions

View File

@ -41,9 +41,8 @@ _gtk_bitmask_copy (const GtkBitmask *mask)
g_return_val_if_fail (mask != NULL, NULL);
copy = _gtk_bitmask_new ();
_gtk_bitmask_union (copy, mask);
return copy;
return _gtk_bitmask_union (copy, mask);
}
void
@ -109,14 +108,14 @@ gtk_bitmask_shrink (GtkBitmask *mask)
g_array_set_size (mask, i);
}
void
GtkBitmask *
_gtk_bitmask_intersect (GtkBitmask *mask,
const GtkBitmask *other)
{
guint i;
g_return_if_fail (mask != NULL);
g_return_if_fail (other != NULL);
g_return_val_if_fail (mask != NULL, NULL);
g_return_val_if_fail (other != NULL, NULL);
g_array_set_size (mask, MIN (mask->len, other->len));
for (i = 0; i < mask->len; i++)
@ -125,32 +124,36 @@ _gtk_bitmask_intersect (GtkBitmask *mask,
}
gtk_bitmask_shrink (mask);
return mask;
}
void
GtkBitmask *
_gtk_bitmask_union (GtkBitmask *mask,
const GtkBitmask *other)
{
guint i;
g_return_if_fail (mask != NULL);
g_return_if_fail (other != NULL);
g_return_val_if_fail (mask != NULL, NULL);
g_return_val_if_fail (other != NULL, NULL);
g_array_set_size (mask, MAX (mask->len, other->len));
for (i = 0; i < other->len; i++)
{
g_array_index (mask, VALUE_TYPE, i) |= g_array_index (other, VALUE_TYPE, i);
}
return mask;
}
void
GtkBitmask *
_gtk_bitmask_subtract (GtkBitmask *mask,
const GtkBitmask *other)
{
guint i;
g_return_if_fail (mask != NULL);
g_return_if_fail (other != NULL);
g_return_val_if_fail (mask != NULL, NULL);
g_return_val_if_fail (other != NULL, NULL);
for (i = 0; i < other->len; i++)
{
@ -158,6 +161,8 @@ _gtk_bitmask_subtract (GtkBitmask *mask,
}
gtk_bitmask_shrink (mask);
return mask;
}
static void
@ -185,14 +190,14 @@ _gtk_bitmask_get (const GtkBitmask *mask,
return (g_array_index (mask, VALUE_TYPE, array_index) & VALUE_BIT (bit_index)) ? TRUE : FALSE;
}
void
GtkBitmask *
_gtk_bitmask_set (GtkBitmask *mask,
guint index_,
gboolean value)
{
guint array_index, bit_index;
g_return_if_fail (mask != NULL);
g_return_val_if_fail (mask != NULL, NULL);
gtk_bitmask_indexes (index_, &array_index, &bit_index);
@ -211,22 +216,26 @@ _gtk_bitmask_set (GtkBitmask *mask,
gtk_bitmask_shrink (mask);
}
}
return mask;
}
void
GtkBitmask *
_gtk_bitmask_invert_range (GtkBitmask *mask,
guint start,
guint end)
{
guint i;
g_return_if_fail (mask != NULL);
g_return_if_fail (start < end);
g_return_val_if_fail (mask != NULL, NULL);
g_return_val_if_fail (start < end, NULL);
/* I CAN HAS SPEEDUP? */
for (i = start; i < end; i++)
_gtk_bitmask_set (mask, i, !_gtk_bitmask_get (mask, i));
mask = _gtk_bitmask_set (mask, i, !_gtk_bitmask_get (mask, i));
return mask;
}
gboolean

View File

@ -39,22 +39,22 @@ char * _gtk_bitmask_to_string (const GtkBitmask *mask);
void _gtk_bitmask_print (const GtkBitmask *mask,
GString *string);
void _gtk_bitmask_intersect (GtkBitmask *mask,
const GtkBitmask *other);
void _gtk_bitmask_union (GtkBitmask *mask,
const GtkBitmask *other);
void _gtk_bitmask_subtract (GtkBitmask *mask,
const GtkBitmask *other);
GtkBitmask * _gtk_bitmask_intersect (GtkBitmask *mask,
const GtkBitmask *other) G_GNUC_WARN_UNUSED_RESULT;
GtkBitmask * _gtk_bitmask_union (GtkBitmask *mask,
const GtkBitmask *other) G_GNUC_WARN_UNUSED_RESULT;
GtkBitmask * _gtk_bitmask_subtract (GtkBitmask *mask,
const GtkBitmask *other) G_GNUC_WARN_UNUSED_RESULT;
gboolean _gtk_bitmask_get (const GtkBitmask *mask,
guint index_);
void _gtk_bitmask_set (GtkBitmask *mask,
GtkBitmask * _gtk_bitmask_set (GtkBitmask *mask,
guint index_,
gboolean value);
gboolean value) G_GNUC_WARN_UNUSED_RESULT;
void _gtk_bitmask_invert_range (GtkBitmask *mask,
GtkBitmask * _gtk_bitmask_invert_range (GtkBitmask *mask,
guint start,
guint end);
guint end) G_GNUC_WARN_UNUSED_RESULT;
gboolean _gtk_bitmask_is_empty (const GtkBitmask *mask);
gboolean _gtk_bitmask_equals (const GtkBitmask *mask,

View File

@ -43,7 +43,7 @@ _gtk_css_lookup_new (void)
lookup = g_malloc0 (sizeof (GtkCssLookup) + sizeof (GtkCssLookupValue) * n);
lookup->missing = _gtk_bitmask_new ();
_gtk_bitmask_invert_range (lookup->missing, 0, n);
lookup->missing = _gtk_bitmask_invert_range (lookup->missing, 0, n);
return lookup;
}
@ -97,7 +97,7 @@ _gtk_css_lookup_set (GtkCssLookup *lookup,
g_return_if_fail (_gtk_bitmask_get (lookup->missing, id));
g_return_if_fail (value != NULL);
_gtk_bitmask_set (lookup->missing, id, FALSE);
lookup->missing = _gtk_bitmask_set (lookup->missing, id, FALSE);
lookup->values[id].value = value;
lookup->values[id].section = section;
}
@ -131,7 +131,7 @@ _gtk_css_lookup_set_computed (GtkCssLookup *lookup,
g_return_if_fail (_gtk_bitmask_get (lookup->missing, id));
g_return_if_fail (value != NULL);
_gtk_bitmask_set (lookup->missing, id, FALSE);
lookup->missing = _gtk_bitmask_set (lookup->missing, id, FALSE);
lookup->values[id].computed = value;
lookup->values[id].section = section;
}

View File

@ -1253,9 +1253,9 @@ gtk_css_ruleset_add (GtkCssRuleset *ruleset,
if (ruleset->set_styles == NULL)
ruleset->set_styles = _gtk_bitmask_new ();
_gtk_bitmask_set (ruleset->set_styles,
_gtk_css_style_property_get_id (property),
TRUE);
ruleset->set_styles = _gtk_bitmask_set (ruleset->set_styles,
_gtk_css_style_property_get_id (property),
TRUE);
ruleset->owns_styles = TRUE;

View File

@ -46,9 +46,9 @@ gtk_bitmask_new_parse (const char *string)
for (i = 0; i < length; i++)
{
if (string[i] == '0')
_gtk_bitmask_set (mask, length - i - 1, FALSE);
mask = _gtk_bitmask_set (mask, length - i - 1, FALSE);
else if (string[i] == '1')
_gtk_bitmask_set (mask, length - i - 1, TRUE);
mask = _gtk_bitmask_set (mask, length - i - 1, TRUE);
else
g_assert_not_reached ();
}
@ -140,12 +140,12 @@ test_set (void)
for (j = 0; j < N_TRIES; j++)
{
indexes[j] = g_test_rand_int_range (0, MAX_INDEX);
_gtk_bitmask_set (copy, indexes[j], g_test_rand_bit ());
copy = _gtk_bitmask_set (copy, indexes[j], g_test_rand_bit ());
}
for (j = 0; j < N_TRIES; j++)
{
_gtk_bitmask_set (copy, indexes[j], _gtk_bitmask_get (mask, indexes[j]));
copy = _gtk_bitmask_set (copy, indexes[j], _gtk_bitmask_get (mask, indexes[j]));
}
assert_cmpmasks (copy, mask);
@ -171,15 +171,15 @@ test_union (void)
guint id = g_test_rand_int_range (0, MAX_INDEX);
if (g_test_rand_bit ())
_gtk_bitmask_set (left, id, TRUE);
left = _gtk_bitmask_set (left, id, TRUE);
else
_gtk_bitmask_set (right, id, TRUE);
right = _gtk_bitmask_set (right, id, TRUE);
_gtk_bitmask_set (expected, id, TRUE);
expected = _gtk_bitmask_set (expected, id, TRUE);
}
_gtk_bitmask_union (left, right);
_gtk_bitmask_union (right, left);
left = _gtk_bitmask_union (left, right);
right = _gtk_bitmask_union (right, left);
assert_cmpmasks (left, expected);
assert_cmpmasks (right, expected);
@ -209,13 +209,13 @@ test_intersect (void)
if (g_test_rand_bit ())
{
_gtk_bitmask_set (left, id, set);
_gtk_bitmask_set (expected, id, set ? _gtk_bitmask_get (right, id) : 0);
left = _gtk_bitmask_set (left, id, set);
expected = _gtk_bitmask_set (expected, id, set ? _gtk_bitmask_get (right, id) : 0);
}
else
{
_gtk_bitmask_set (right, id, set);
_gtk_bitmask_set (expected, id, set ? _gtk_bitmask_get (left, id) : 0);
right = _gtk_bitmask_set (right, id, set);
expected = _gtk_bitmask_set (expected, id, set ? _gtk_bitmask_get (left, id) : 0);
}
}
@ -223,8 +223,8 @@ test_intersect (void)
g_assert_cmpint (intersects, ==, _gtk_bitmask_intersects (right, left));
g_assert_cmpint (intersects, !=, _gtk_bitmask_is_empty (expected));
_gtk_bitmask_intersect (left, right);
_gtk_bitmask_intersect (right, left);
left = _gtk_bitmask_intersect (left, right);
right = _gtk_bitmask_intersect (right, left);
assert_cmpmasks (left, expected);
assert_cmpmasks (right, expected);
@ -265,19 +265,19 @@ test_invert_range (void)
end = MIN (left_end, right_end);
if (left_start != left_end)
_gtk_bitmask_invert_range (left, left_start, left_end);
left = _gtk_bitmask_invert_range (left, left_start, left_end);
if (right_start != right_end)
_gtk_bitmask_invert_range (right, right_start, right_end);
right = _gtk_bitmask_invert_range (right, right_start, right_end);
if (start < end)
_gtk_bitmask_invert_range (expected, start, end);
expected = _gtk_bitmask_invert_range (expected, start, end);
intersection = _gtk_bitmask_copy (left);
_gtk_bitmask_intersect (intersection, right);
intersection = _gtk_bitmask_intersect (intersection, right);
assert_cmpmasks (intersection, expected);
if (start < end)
_gtk_bitmask_invert_range (expected, start, end);
expected = _gtk_bitmask_invert_range (expected, start, end);
g_assert_cmpint (_gtk_bitmask_is_empty (expected), ==, TRUE);