1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
/*
* 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
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Authors: Benjamin Otte <otte@gnome.org>
*/
#include <config.h>
#include "gtkallocatedbitmaskprivate.h"
#include "gtkprivate.h"
#define VALUE_TYPE gsize
#define VALUE_SIZE_BITS (sizeof (VALUE_TYPE) * 8)
#define VALUE_BIT(idx) (((VALUE_TYPE) 1) << (idx))
#define ALL_BITS (~((VALUE_TYPE) 0))
struct _GtkBitmask {
gsize len;
VALUE_TYPE data[1];
};
#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
static GtkBitmask *
gtk_allocated_bitmask_resize (GtkBitmask *mask,
gsize size) G_GNUC_WARN_UNUSED_RESULT;
static GtkBitmask *
gtk_allocated_bitmask_resize (GtkBitmask *mask,
gsize size)
{
gsize i;
if (size == mask->len)
return mask;
mask = g_realloc (mask, sizeof (GtkBitmask) + sizeof(VALUE_TYPE) * (size - 1));
for (i = mask->len; i < size; i++)
mask->data[i] = 0;
mask->len = size;
return mask;
}
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)
{
if (_gtk_bitmask_is_allocated (mask))
return mask;
else
return gtk_allocated_bitmask_new_for_bits (_gtk_bitmask_to_bits (mask));
}
GtkBitmask *
_gtk_allocated_bitmask_copy (const GtkBitmask *mask)
{
GtkBitmask *copy;
gtk_internal_return_val_if_fail (mask != NULL, NULL);
copy = gtk_allocated_bitmask_new_for_bits (0);
return _gtk_allocated_bitmask_union (copy, mask);
}
void
_gtk_allocated_bitmask_free (GtkBitmask *mask)
{
gtk_internal_return_if_fail (mask != NULL);
g_free (mask);
}
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);
}
void
_gtk_allocated_bitmask_print (const GtkBitmask *mask,
GString *string)
{
GtkBitmask mask_allocated;
int i;
gtk_internal_return_if_fail (mask != NULL);
gtk_internal_return_if_fail (string != NULL);
ENSURE_ALLOCATED (mask, mask_allocated);
for (i = mask->len * VALUE_SIZE_BITS - 1; i >= 0; i--)
{
if (_gtk_allocated_bitmask_get (mask, i))
break;
}
if (i < 0)
{
g_string_append_c (string, '0');
return;
}
for (; i >= 0; i--)
{
g_string_append_c (string, _gtk_allocated_bitmask_get (mask, i) ? '1' : '0');
}
}
/* NB: Call this function whenever the
* array might have become too large.
* _gtk_allocated_bitmask_is_empty() depends on this.
*/
static GtkBitmask *
gtk_allocated_bitmask_shrink (GtkBitmask *mask) G_GNUC_WARN_UNUSED_RESULT;
static GtkBitmask *
gtk_allocated_bitmask_shrink (GtkBitmask *mask)
{
guint i;
for (i = mask->len; i; i--)
{
if (mask->data[i - 1])
break;
}
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;
}
return gtk_allocated_bitmask_resize (mask, i);
}
GtkBitmask *
_gtk_allocated_bitmask_intersect (GtkBitmask *mask,
const GtkBitmask *other)
{
GtkBitmask other_allocated;
guint i;
gtk_internal_return_val_if_fail (mask != NULL, NULL);
gtk_internal_return_val_if_fail (other != NULL, NULL);
mask = gtk_bitmask_ensure_allocated (mask);
ENSURE_ALLOCATED (other, other_allocated);
for (i = 0; i < MIN (mask->len, other->len); i++)
{
mask->data[i] &= other->data[i];
}
for (; i < mask->len; i++)
{
mask->data[i] = 0;
}
return gtk_allocated_bitmask_shrink (mask);
}
GtkBitmask *
_gtk_allocated_bitmask_union (GtkBitmask *mask,
const GtkBitmask *other)
{
GtkBitmask other_allocated;
guint i;
gtk_internal_return_val_if_fail (mask != NULL, NULL);
gtk_internal_return_val_if_fail (other != NULL, NULL);
mask = gtk_bitmask_ensure_allocated (mask);
ENSURE_ALLOCATED (other, other_allocated);
mask = gtk_allocated_bitmask_resize (mask, MAX (mask->len, other->len));
for (i = 0; i < other->len; i++)
{
mask->data[i] |= other->data[i];
}
return mask;
}
GtkBitmask *
_gtk_allocated_bitmask_subtract (GtkBitmask *mask,
const GtkBitmask *other)
{
GtkBitmask other_allocated;
guint i, len;
gtk_internal_return_val_if_fail (mask != NULL, NULL);
gtk_internal_return_val_if_fail (other != NULL, NULL);
mask = gtk_bitmask_ensure_allocated (mask);
ENSURE_ALLOCATED (other, other_allocated);
len = MIN (mask->len, other->len);
for (i = 0; i < len; i++)
{
mask->data[i] &= ~other->data[i];
}
return gtk_allocated_bitmask_shrink (mask);
}
static inline void
gtk_allocated_bitmask_indexes (guint index_,
guint *array_index,
guint *bit_index)
{
*array_index = index_ / VALUE_SIZE_BITS;
*bit_index = index_ % VALUE_SIZE_BITS;
}
gboolean
_gtk_allocated_bitmask_get (const GtkBitmask *mask,
guint index_)
{
guint array_index, bit_index;
gtk_internal_return_val_if_fail (mask != NULL, FALSE);
gtk_allocated_bitmask_indexes (index_, &array_index, &bit_index);
if (array_index >= mask->len)
return FALSE;
return (mask->data[array_index] & VALUE_BIT (bit_index)) ? TRUE : FALSE;
}
GtkBitmask *
_gtk_allocated_bitmask_set (GtkBitmask *mask,
guint index_,
gboolean value)
{
guint array_index, bit_index;
gtk_internal_return_val_if_fail (mask != NULL, NULL);
mask = gtk_bitmask_ensure_allocated (mask);
gtk_allocated_bitmask_indexes (index_, &array_index, &bit_index);
if (value)
{
if (array_index >= mask->len)
mask = gtk_allocated_bitmask_resize (mask, array_index + 1);
mask->data[array_index] |= VALUE_BIT (bit_index);
}
else
{
if (array_index < mask->len)
{
mask->data[array_index] &= ~ VALUE_BIT (bit_index);
mask = gtk_allocated_bitmask_shrink (mask);
}
}
return mask;
}
GtkBitmask *
_gtk_allocated_bitmask_invert_range (GtkBitmask *mask,
guint start,
guint end)
{
guint i;
guint start_word, start_bit;
guint end_word, end_bit;
gtk_internal_return_val_if_fail (mask != NULL, NULL);
gtk_internal_return_val_if_fail (start < end, NULL);
mask = gtk_bitmask_ensure_allocated (mask);
gtk_allocated_bitmask_indexes (start, &start_word, &start_bit);
gtk_allocated_bitmask_indexes (end - 1, &end_word, &end_bit);
if (end_word >= mask->len)
mask = gtk_allocated_bitmask_resize (mask, end_word + 1);
for (i = start_word; i <= end_word; i++)
mask->data[i] ^= ALL_BITS;
mask->data[start_word] ^= (((VALUE_TYPE) 1) << start_bit) - 1;
if (end_bit != VALUE_SIZE_BITS - 1)
mask->data[end_word] ^= ALL_BITS << (end_bit + 1);
return gtk_allocated_bitmask_shrink (mask);
}
gboolean
_gtk_allocated_bitmask_equals (const GtkBitmask *mask,
const GtkBitmask *other)
{
guint i;
gtk_internal_return_val_if_fail (mask != NULL, FALSE);
gtk_internal_return_val_if_fail (other != NULL, FALSE);
if (mask->len != other->len)
return FALSE;
for (i = 0; i < mask->len; i++)
{
if (mask->data[i] != other->data[i])
return FALSE;
}
return TRUE;
}
gboolean
_gtk_allocated_bitmask_intersects (const GtkBitmask *mask,
const GtkBitmask *other)
{
GtkBitmask mask_allocated, other_allocated;
int i;
gtk_internal_return_val_if_fail (mask != NULL, FALSE);
gtk_internal_return_val_if_fail (other != NULL, FALSE);
ENSURE_ALLOCATED (mask, mask_allocated);
ENSURE_ALLOCATED (other, other_allocated);
for (i = MIN (mask->len, other->len) - 1; i >= 0; i--)
{
if (mask->data[i] & other->data[i])
return TRUE;
}
return FALSE;
}
|