2011-12-23 11:16:18 +00:00
|
|
|
/*
|
|
|
|
* Copyright © 2011 Red Hat Inc.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-02-27 13:01:10 +00:00
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
2011-12-23 11:16:18 +00:00
|
|
|
*
|
|
|
|
* Authors: Benjamin Otte <otte@gnome.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2015-09-08 10:14:38 +00:00
|
|
|
#include "gtkallocatedbitmaskprivate.h"
|
|
|
|
#include "gtkprivate.h"
|
|
|
|
|
2011-12-23 11:16:18 +00:00
|
|
|
|
|
|
|
#define VALUE_TYPE gsize
|
|
|
|
|
|
|
|
#define VALUE_SIZE_BITS (sizeof (VALUE_TYPE) * 8)
|
|
|
|
#define VALUE_BIT(idx) (((VALUE_TYPE) 1) << (idx))
|
2015-09-07 12:31:26 +00:00
|
|
|
#define ALL_BITS (~((VALUE_TYPE) 0))
|
2011-12-23 11:16:18 +00:00
|
|
|
|
2012-02-09 00:15:20 +00:00
|
|
|
struct _GtkBitmask {
|
|
|
|
gsize len;
|
|
|
|
VALUE_TYPE data[1];
|
|
|
|
};
|
|
|
|
|
2012-02-09 03:53:43 +00:00
|
|
|
#define ENSURE_ALLOCATED(mask, heap_mask) G_STMT_START { \
|
|
|
|
if (!_gtk_bitmask_is_allocated (mask)) \
|
|
|
|
{ \
|
|
|
|
heap_mask.data[0] = _gtk_bitmask_to_bits (mask); \
|
|
|
|
heap_mask.len = heap_mask.data[0] ? 1 : 0; \
|
|
|
|
mask = &heap_mask; \
|
|
|
|
} \
|
|
|
|
} G_STMT_END
|
|
|
|
|
2012-02-09 00:15:20 +00:00
|
|
|
static GtkBitmask *
|
2012-02-09 00:44:59 +00:00
|
|
|
gtk_allocated_bitmask_resize (GtkBitmask *mask,
|
|
|
|
gsize size) G_GNUC_WARN_UNUSED_RESULT;
|
2012-02-09 00:15:20 +00:00
|
|
|
static GtkBitmask *
|
2012-02-09 00:44:59 +00:00
|
|
|
gtk_allocated_bitmask_resize (GtkBitmask *mask,
|
|
|
|
gsize size)
|
2012-02-09 00:15:20 +00:00
|
|
|
{
|
|
|
|
gsize i;
|
|
|
|
|
2015-09-10 22:48:29 +00:00
|
|
|
if (size == mask->len)
|
|
|
|
return mask;
|
|
|
|
|
2012-02-09 03:53:43 +00:00
|
|
|
mask = g_realloc (mask, sizeof (GtkBitmask) + sizeof(VALUE_TYPE) * (size - 1));
|
2012-02-09 00:15:20 +00:00
|
|
|
|
|
|
|
for (i = mask->len; i < size; i++)
|
|
|
|
mask->data[i] = 0;
|
|
|
|
|
|
|
|
mask->len = size;
|
|
|
|
|
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
2012-02-09 03:53:43 +00:00
|
|
|
static GtkBitmask *
|
|
|
|
gtk_allocated_bitmask_new_for_bits (gsize bits)
|
|
|
|
{
|
|
|
|
GtkBitmask *mask;
|
|
|
|
|
|
|
|
mask = g_malloc (sizeof (GtkBitmask));
|
|
|
|
mask->len = bits ? 1 : 0;
|
|
|
|
mask->data[0] = bits;
|
|
|
|
|
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GtkBitmask *
|
|
|
|
gtk_bitmask_ensure_allocated (GtkBitmask *mask)
|
2011-12-23 11:16:18 +00:00
|
|
|
{
|
2012-02-09 03:53:43 +00:00
|
|
|
if (_gtk_bitmask_is_allocated (mask))
|
|
|
|
return mask;
|
|
|
|
else
|
|
|
|
return gtk_allocated_bitmask_new_for_bits (_gtk_bitmask_to_bits (mask));
|
2011-12-23 11:16:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GtkBitmask *
|
2012-02-09 00:44:59 +00:00
|
|
|
_gtk_allocated_bitmask_copy (const GtkBitmask *mask)
|
2011-12-23 11:16:18 +00:00
|
|
|
{
|
|
|
|
GtkBitmask *copy;
|
|
|
|
|
2015-09-08 10:14:38 +00:00
|
|
|
gtk_internal_return_val_if_fail (mask != NULL, NULL);
|
2011-12-23 11:16:18 +00:00
|
|
|
|
2012-02-09 03:53:43 +00:00
|
|
|
copy = gtk_allocated_bitmask_new_for_bits (0);
|
2012-02-08 18:19:59 +00:00
|
|
|
|
2012-02-09 00:44:59 +00:00
|
|
|
return _gtk_allocated_bitmask_union (copy, mask);
|
2011-12-23 11:16:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-02-09 00:44:59 +00:00
|
|
|
_gtk_allocated_bitmask_free (GtkBitmask *mask)
|
2011-12-23 11:16:18 +00:00
|
|
|
{
|
2015-09-08 10:14:38 +00:00
|
|
|
gtk_internal_return_if_fail (mask != NULL);
|
2011-12-23 11:16:18 +00:00
|
|
|
|
2012-02-09 00:15:20 +00:00
|
|
|
g_free (mask);
|
2011-12-23 11:16:18 +00:00
|
|
|
}
|
|
|
|
|
2019-08-22 15:16:21 +00:00
|
|
|
char *
|
|
|
|
_gtk_allocated_bitmask_to_string (const GtkBitmask *mask)
|
|
|
|
{
|
|
|
|
GString *str = g_string_new (NULL);
|
|
|
|
|
|
|
|
_gtk_allocated_bitmask_print (mask, str);
|
|
|
|
|
|
|
|
return g_string_free (str, FALSE);
|
|
|
|
}
|
|
|
|
|
2011-12-23 11:16:18 +00:00
|
|
|
void
|
2012-02-09 00:44:59 +00:00
|
|
|
_gtk_allocated_bitmask_print (const GtkBitmask *mask,
|
|
|
|
GString *string)
|
2011-12-23 11:16:18 +00:00
|
|
|
{
|
2012-02-09 03:53:43 +00:00
|
|
|
GtkBitmask mask_allocated;
|
2011-12-23 11:16:18 +00:00
|
|
|
int i;
|
|
|
|
|
2015-09-08 10:14:38 +00:00
|
|
|
gtk_internal_return_if_fail (mask != NULL);
|
|
|
|
gtk_internal_return_if_fail (string != NULL);
|
2011-12-23 11:16:18 +00:00
|
|
|
|
2012-02-09 03:53:43 +00:00
|
|
|
ENSURE_ALLOCATED (mask, mask_allocated);
|
|
|
|
|
2011-12-23 11:16:18 +00:00
|
|
|
for (i = mask->len * VALUE_SIZE_BITS - 1; i >= 0; i--)
|
|
|
|
{
|
2012-02-09 00:44:59 +00:00
|
|
|
if (_gtk_allocated_bitmask_get (mask, i))
|
2011-12-23 11:16:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i < 0)
|
|
|
|
{
|
|
|
|
g_string_append_c (string, '0');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; i >= 0; i--)
|
|
|
|
{
|
2012-02-09 00:44:59 +00:00
|
|
|
g_string_append_c (string, _gtk_allocated_bitmask_get (mask, i) ? '1' : '0');
|
2011-12-23 11:16:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* NB: Call this function whenever the
|
|
|
|
* array might have become too large.
|
2012-02-09 00:44:59 +00:00
|
|
|
* _gtk_allocated_bitmask_is_empty() depends on this.
|
2011-12-23 11:16:18 +00:00
|
|
|
*/
|
2012-02-09 00:15:20 +00:00
|
|
|
static GtkBitmask *
|
2012-02-09 00:44:59 +00:00
|
|
|
gtk_allocated_bitmask_shrink (GtkBitmask *mask) G_GNUC_WARN_UNUSED_RESULT;
|
2012-02-09 00:15:20 +00:00
|
|
|
static GtkBitmask *
|
2012-02-09 00:44:59 +00:00
|
|
|
gtk_allocated_bitmask_shrink (GtkBitmask *mask)
|
2011-12-23 11:16:18 +00:00
|
|
|
{
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
for (i = mask->len; i; i--)
|
|
|
|
{
|
2012-02-09 00:15:20 +00:00
|
|
|
if (mask->data[i - 1])
|
2011-12-23 11:16:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-02-09 03:53:43 +00:00
|
|
|
if (i == 0 ||
|
|
|
|
(i == 1 && mask->data[0] < VALUE_BIT (GTK_BITMASK_N_DIRECT_BITS)))
|
|
|
|
{
|
|
|
|
GtkBitmask *result = _gtk_bitmask_from_bits (i == 0 ? 0 : mask->data[0]);
|
|
|
|
_gtk_allocated_bitmask_free (mask);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-02-09 00:44:59 +00:00
|
|
|
return gtk_allocated_bitmask_resize (mask, i);
|
2011-12-23 11:16:18 +00:00
|
|
|
}
|
|
|
|
|
2012-02-08 18:19:59 +00:00
|
|
|
GtkBitmask *
|
2012-02-09 00:44:59 +00:00
|
|
|
_gtk_allocated_bitmask_intersect (GtkBitmask *mask,
|
|
|
|
const GtkBitmask *other)
|
2011-12-23 11:16:18 +00:00
|
|
|
{
|
2012-02-09 03:53:43 +00:00
|
|
|
GtkBitmask other_allocated;
|
2011-12-23 11:16:18 +00:00
|
|
|
guint i;
|
|
|
|
|
2015-09-08 10:14:38 +00:00
|
|
|
gtk_internal_return_val_if_fail (mask != NULL, NULL);
|
|
|
|
gtk_internal_return_val_if_fail (other != NULL, NULL);
|
2011-12-23 11:16:18 +00:00
|
|
|
|
2012-02-09 03:53:43 +00:00
|
|
|
mask = gtk_bitmask_ensure_allocated (mask);
|
|
|
|
ENSURE_ALLOCATED (other, other_allocated);
|
|
|
|
|
2015-09-10 22:46:58 +00:00
|
|
|
for (i = 0; i < MIN (mask->len, other->len); i++)
|
2011-12-23 11:16:18 +00:00
|
|
|
{
|
2012-02-09 00:15:20 +00:00
|
|
|
mask->data[i] &= other->data[i];
|
2011-12-23 11:16:18 +00:00
|
|
|
}
|
2015-09-10 22:46:58 +00:00
|
|
|
for (; i < mask->len; i++)
|
|
|
|
{
|
|
|
|
mask->data[i] = 0;
|
|
|
|
}
|
2011-12-23 11:16:18 +00:00
|
|
|
|
2012-02-09 00:44:59 +00:00
|
|
|
return gtk_allocated_bitmask_shrink (mask);
|
2011-12-23 11:16:18 +00:00
|
|
|
}
|
|
|
|
|
2012-02-08 18:19:59 +00:00
|
|
|
GtkBitmask *
|
2012-02-09 00:44:59 +00:00
|
|
|
_gtk_allocated_bitmask_union (GtkBitmask *mask,
|
|
|
|
const GtkBitmask *other)
|
2011-12-23 11:16:18 +00:00
|
|
|
{
|
2012-02-09 03:53:43 +00:00
|
|
|
GtkBitmask other_allocated;
|
2011-12-23 11:16:18 +00:00
|
|
|
guint i;
|
|
|
|
|
2015-09-08 10:14:38 +00:00
|
|
|
gtk_internal_return_val_if_fail (mask != NULL, NULL);
|
|
|
|
gtk_internal_return_val_if_fail (other != NULL, NULL);
|
2011-12-23 11:16:18 +00:00
|
|
|
|
2012-02-09 03:53:43 +00:00
|
|
|
mask = gtk_bitmask_ensure_allocated (mask);
|
|
|
|
ENSURE_ALLOCATED (other, other_allocated);
|
|
|
|
|
2012-02-09 00:44:59 +00:00
|
|
|
mask = gtk_allocated_bitmask_resize (mask, MAX (mask->len, other->len));
|
2011-12-23 11:16:18 +00:00
|
|
|
for (i = 0; i < other->len; i++)
|
|
|
|
{
|
2012-02-09 00:15:20 +00:00
|
|
|
mask->data[i] |= other->data[i];
|
2011-12-23 11:16:18 +00:00
|
|
|
}
|
2012-02-08 18:19:59 +00:00
|
|
|
|
|
|
|
return mask;
|
2011-12-23 11:16:18 +00:00
|
|
|
}
|
|
|
|
|
2012-02-08 18:19:59 +00:00
|
|
|
GtkBitmask *
|
2012-02-09 00:44:59 +00:00
|
|
|
_gtk_allocated_bitmask_subtract (GtkBitmask *mask,
|
|
|
|
const GtkBitmask *other)
|
2011-12-23 11:16:18 +00:00
|
|
|
{
|
2012-02-09 03:53:43 +00:00
|
|
|
GtkBitmask other_allocated;
|
2015-02-24 23:35:45 +00:00
|
|
|
guint i, len;
|
2011-12-23 11:16:18 +00:00
|
|
|
|
2015-09-08 10:14:38 +00:00
|
|
|
gtk_internal_return_val_if_fail (mask != NULL, NULL);
|
|
|
|
gtk_internal_return_val_if_fail (other != NULL, NULL);
|
2011-12-23 11:16:18 +00:00
|
|
|
|
2012-02-09 03:53:43 +00:00
|
|
|
mask = gtk_bitmask_ensure_allocated (mask);
|
|
|
|
ENSURE_ALLOCATED (other, other_allocated);
|
|
|
|
|
2015-02-24 23:35:45 +00:00
|
|
|
len = MIN (mask->len, other->len);
|
|
|
|
for (i = 0; i < len; i++)
|
2011-12-23 11:16:18 +00:00
|
|
|
{
|
2015-02-24 23:35:45 +00:00
|
|
|
mask->data[i] &= ~other->data[i];
|
2011-12-23 11:16:18 +00:00
|
|
|
}
|
|
|
|
|
2012-02-09 00:44:59 +00:00
|
|
|
return gtk_allocated_bitmask_shrink (mask);
|
2011-12-23 11:16:18 +00:00
|
|
|
}
|
|
|
|
|
2015-09-07 05:55:16 +00:00
|
|
|
static inline void
|
2012-02-09 00:44:59 +00:00
|
|
|
gtk_allocated_bitmask_indexes (guint index_,
|
|
|
|
guint *array_index,
|
|
|
|
guint *bit_index)
|
2011-12-23 11:16:18 +00:00
|
|
|
{
|
|
|
|
*array_index = index_ / VALUE_SIZE_BITS;
|
|
|
|
*bit_index = index_ % VALUE_SIZE_BITS;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
2012-02-09 00:44:59 +00:00
|
|
|
_gtk_allocated_bitmask_get (const GtkBitmask *mask,
|
|
|
|
guint index_)
|
2011-12-23 11:16:18 +00:00
|
|
|
{
|
|
|
|
guint array_index, bit_index;
|
|
|
|
|
2015-09-08 10:14:38 +00:00
|
|
|
gtk_internal_return_val_if_fail (mask != NULL, FALSE);
|
2011-12-23 11:16:18 +00:00
|
|
|
|
2012-02-09 00:44:59 +00:00
|
|
|
gtk_allocated_bitmask_indexes (index_, &array_index, &bit_index);
|
2011-12-23 11:16:18 +00:00
|
|
|
|
|
|
|
if (array_index >= mask->len)
|
|
|
|
return FALSE;
|
|
|
|
|
2012-02-09 00:15:20 +00:00
|
|
|
return (mask->data[array_index] & VALUE_BIT (bit_index)) ? TRUE : FALSE;
|
2011-12-23 11:16:18 +00:00
|
|
|
}
|
|
|
|
|
2012-02-08 18:19:59 +00:00
|
|
|
GtkBitmask *
|
2012-02-09 00:44:59 +00:00
|
|
|
_gtk_allocated_bitmask_set (GtkBitmask *mask,
|
|
|
|
guint index_,
|
|
|
|
gboolean value)
|
2011-12-23 11:16:18 +00:00
|
|
|
{
|
|
|
|
guint array_index, bit_index;
|
|
|
|
|
2015-09-08 10:14:38 +00:00
|
|
|
gtk_internal_return_val_if_fail (mask != NULL, NULL);
|
2011-12-23 11:16:18 +00:00
|
|
|
|
2012-02-09 03:53:43 +00:00
|
|
|
mask = gtk_bitmask_ensure_allocated (mask);
|
2012-02-09 00:44:59 +00:00
|
|
|
gtk_allocated_bitmask_indexes (index_, &array_index, &bit_index);
|
2011-12-23 11:16:18 +00:00
|
|
|
|
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
if (array_index >= mask->len)
|
2012-02-09 00:44:59 +00:00
|
|
|
mask = gtk_allocated_bitmask_resize (mask, array_index + 1);
|
2011-12-23 11:16:18 +00:00
|
|
|
|
2012-02-09 00:15:20 +00:00
|
|
|
mask->data[array_index] |= VALUE_BIT (bit_index);
|
2011-12-23 11:16:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (array_index < mask->len)
|
|
|
|
{
|
2012-02-09 00:15:20 +00:00
|
|
|
mask->data[array_index] &= ~ VALUE_BIT (bit_index);
|
2012-02-09 00:44:59 +00:00
|
|
|
mask = gtk_allocated_bitmask_shrink (mask);
|
2011-12-23 11:16:18 +00:00
|
|
|
}
|
|
|
|
}
|
2012-02-08 18:19:59 +00:00
|
|
|
|
|
|
|
return mask;
|
2011-12-23 11:16:18 +00:00
|
|
|
}
|
|
|
|
|
2012-02-08 18:19:59 +00:00
|
|
|
GtkBitmask *
|
2012-02-09 00:44:59 +00:00
|
|
|
_gtk_allocated_bitmask_invert_range (GtkBitmask *mask,
|
|
|
|
guint start,
|
|
|
|
guint end)
|
2011-12-23 11:16:18 +00:00
|
|
|
{
|
|
|
|
guint i;
|
2015-09-07 05:55:16 +00:00
|
|
|
guint start_word, start_bit;
|
|
|
|
guint end_word, end_bit;
|
2011-12-23 11:16:18 +00:00
|
|
|
|
2015-09-08 10:14:38 +00:00
|
|
|
gtk_internal_return_val_if_fail (mask != NULL, NULL);
|
|
|
|
gtk_internal_return_val_if_fail (start < end, NULL);
|
2011-12-23 11:16:18 +00:00
|
|
|
|
2012-02-09 03:53:43 +00:00
|
|
|
mask = gtk_bitmask_ensure_allocated (mask);
|
2011-12-23 11:16:18 +00:00
|
|
|
|
2015-09-07 05:55:16 +00:00
|
|
|
gtk_allocated_bitmask_indexes (start, &start_word, &start_bit);
|
|
|
|
gtk_allocated_bitmask_indexes (end - 1, &end_word, &end_bit);
|
2012-02-08 18:19:59 +00:00
|
|
|
|
2015-09-07 05:55:16 +00:00
|
|
|
if (end_word >= mask->len)
|
|
|
|
mask = gtk_allocated_bitmask_resize (mask, end_word + 1);
|
|
|
|
|
2015-09-07 12:31:26 +00:00
|
|
|
for (i = start_word; i <= end_word; i++)
|
|
|
|
mask->data[i] ^= ALL_BITS;
|
|
|
|
mask->data[start_word] ^= (((VALUE_TYPE) 1) << start_bit) - 1;
|
2015-09-27 13:10:15 +00:00
|
|
|
if (end_bit != VALUE_SIZE_BITS - 1)
|
2015-09-11 13:06:46 +00:00
|
|
|
mask->data[end_word] ^= ALL_BITS << (end_bit + 1);
|
2015-09-07 05:55:16 +00:00
|
|
|
|
|
|
|
return gtk_allocated_bitmask_shrink (mask);
|
2011-12-23 11:16:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
2012-02-09 00:44:59 +00:00
|
|
|
_gtk_allocated_bitmask_equals (const GtkBitmask *mask,
|
|
|
|
const GtkBitmask *other)
|
2011-12-23 11:16:18 +00:00
|
|
|
{
|
|
|
|
guint i;
|
|
|
|
|
2015-09-08 10:14:38 +00:00
|
|
|
gtk_internal_return_val_if_fail (mask != NULL, FALSE);
|
|
|
|
gtk_internal_return_val_if_fail (other != NULL, FALSE);
|
2011-12-23 11:16:18 +00:00
|
|
|
|
|
|
|
if (mask->len != other->len)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
for (i = 0; i < mask->len; i++)
|
|
|
|
{
|
2012-02-09 00:15:20 +00:00
|
|
|
if (mask->data[i] != other->data[i])
|
2011-12-23 11:16:18 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
2012-02-09 00:44:59 +00:00
|
|
|
_gtk_allocated_bitmask_intersects (const GtkBitmask *mask,
|
|
|
|
const GtkBitmask *other)
|
2011-12-23 11:16:18 +00:00
|
|
|
{
|
2012-02-09 03:53:43 +00:00
|
|
|
GtkBitmask mask_allocated, other_allocated;
|
2011-12-23 11:16:18 +00:00
|
|
|
int i;
|
|
|
|
|
2015-09-08 10:14:38 +00:00
|
|
|
gtk_internal_return_val_if_fail (mask != NULL, FALSE);
|
|
|
|
gtk_internal_return_val_if_fail (other != NULL, FALSE);
|
2011-12-23 11:16:18 +00:00
|
|
|
|
2012-02-09 03:53:43 +00:00
|
|
|
ENSURE_ALLOCATED (mask, mask_allocated);
|
|
|
|
ENSURE_ALLOCATED (other, other_allocated);
|
|
|
|
|
2011-12-23 11:16:18 +00:00
|
|
|
for (i = MIN (mask->len, other->len) - 1; i >= 0; i--)
|
|
|
|
{
|
2012-02-09 00:15:20 +00:00
|
|
|
if (mask->data[i] & other->data[i])
|
2011-12-23 11:16:18 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|