gtkbitmask.c 6.03 KB
Newer Older
Benjamin Otte's avatar
Benjamin Otte committed
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
/*
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Authors: Benjamin Otte <otte@gnome.org>
 */

#include <config.h>

#define GTK_INSIDE_BITMASK_C
#include "gtk/gtkbitmaskprivate.h"

#define VALUE_TYPE gsize

#define VALUE_SIZE_BITS (sizeof (VALUE_TYPE) * 8)
#define VALUE_BIT(idx) (((VALUE_TYPE) 1) << (idx))

GtkBitmask *
_gtk_bitmask_new (void)
{
  return g_array_new (FALSE, TRUE, sizeof (VALUE_TYPE));
}

GtkBitmask *
_gtk_bitmask_copy (const GtkBitmask *mask)
{
  GtkBitmask *copy;

  g_return_val_if_fail (mask != NULL, NULL);

  copy = _gtk_bitmask_new ();
  _gtk_bitmask_union (copy, mask);

  return copy;
}

void
_gtk_bitmask_free (GtkBitmask *mask)
{
  g_return_if_fail (mask != NULL);

  g_array_free (mask, TRUE);
}

void
_gtk_bitmask_print (const GtkBitmask *mask,
                    GString          *string)
{
  int i;

  g_return_if_fail (mask != NULL);
  g_return_if_fail (string != NULL);

  for (i = mask->len * VALUE_SIZE_BITS - 1; i >= 0; i--)
    {
      if (_gtk_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_bitmask_get (mask, i) ? '1' : '0');
    }
}

char *
_gtk_bitmask_to_string (const GtkBitmask *mask)
{
  GString *string;
  
  string = g_string_new (NULL);
  _gtk_bitmask_print (mask, string);
  return g_string_free (string, FALSE);
}

/* NB: Call this function whenever the
 * array might have become too large.
 * _gtk_bitmask_is_empty() depends on this.
 */
static void
gtk_bitmask_shrink (GtkBitmask *mask)
{
  guint i;

  for (i = mask->len; i; i--)
    {
      if (g_array_index (mask, VALUE_TYPE, i - 1))
        break;
    }

  g_array_set_size (mask, i);
}

void
_gtk_bitmask_intersect (GtkBitmask       *mask,
                        const GtkBitmask *other)
{
  guint i;

  g_return_if_fail (mask != NULL);
  g_return_if_fail (other != NULL);

  g_array_set_size (mask, MIN (mask->len, other->len));
  for (i = 0; i < mask->len; i++)
    {
      g_array_index (mask, VALUE_TYPE, i) &= g_array_index (other, VALUE_TYPE, i);
    }

  gtk_bitmask_shrink (mask);
}

void
_gtk_bitmask_union (GtkBitmask       *mask,
                    const GtkBitmask *other)
{
  guint i;

  g_return_if_fail (mask != NULL);
  g_return_if_fail (other != 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);
    }
}

void
_gtk_bitmask_subtract (GtkBitmask       *mask,
                       const GtkBitmask *other)
{
  guint i;

  g_return_if_fail (mask != NULL);
  g_return_if_fail (other != NULL);

  for (i = 0; i < other->len; i++)
    {
      g_array_index (mask, VALUE_TYPE, i) &= ~g_array_index (other, VALUE_TYPE, i);
    }

  gtk_bitmask_shrink (mask);
}

static void
gtk_bitmask_indexes (guint index_,
                     guint *array_index,
                     guint *bit_index)
{
  *array_index = index_ / VALUE_SIZE_BITS;
  *bit_index = index_ % VALUE_SIZE_BITS;
}

gboolean
_gtk_bitmask_get (const GtkBitmask *mask,
                  guint             index_)
{
  guint array_index, bit_index;

  g_return_val_if_fail (mask != NULL, FALSE);

  gtk_bitmask_indexes (index_, &array_index, &bit_index);

  if (array_index >= mask->len)
    return FALSE;

  return (g_array_index (mask, VALUE_TYPE, array_index) & VALUE_BIT (bit_index)) ? TRUE : FALSE;
}

void
_gtk_bitmask_set (GtkBitmask *mask,
                  guint       index_,
                  gboolean    value)
{
  guint array_index, bit_index;

  g_return_if_fail (mask != NULL);

  gtk_bitmask_indexes (index_, &array_index, &bit_index);

  if (value)
    {
      if (array_index >= mask->len)
        g_array_set_size (mask, array_index + 1);
      
      g_array_index (mask, VALUE_TYPE, array_index) |= VALUE_BIT (bit_index);
    }
  else
    {
      if (array_index < mask->len)
        {
          g_array_index (mask, VALUE_TYPE, array_index) &= ~ VALUE_BIT (bit_index);
          gtk_bitmask_shrink (mask);
        }
    }
}

void
_gtk_bitmask_invert_range (GtkBitmask *mask,
                           guint       start,
                           guint       end)
{
  guint i;

  g_return_if_fail (mask != NULL);
  g_return_if_fail (start < end);

  /* I CAN HAS SPEEDUP? */

  for (i = start; i < end; i++)
    _gtk_bitmask_set (mask, i, !_gtk_bitmask_get (mask, i));
}

gboolean
_gtk_bitmask_is_empty (const GtkBitmask *mask)
{
  g_return_val_if_fail (mask != NULL, FALSE);

  return mask->len == 0;
}

gboolean
_gtk_bitmask_equals (const GtkBitmask  *mask,
                     const GtkBitmask  *other)
{
  guint i;

  g_return_val_if_fail (mask != NULL, FALSE);
  g_return_val_if_fail (other != NULL, FALSE);

  if (mask->len != other->len)
    return FALSE;

  for (i = 0; i < mask->len; i++)
    {
      if (g_array_index (mask, VALUE_TYPE, i) != g_array_index (other, VALUE_TYPE, i))
        return FALSE;
    }

  return TRUE;
}

gboolean
_gtk_bitmask_intersects (const GtkBitmask *mask,
                         const GtkBitmask *other)
{
  int i;

  g_return_val_if_fail (mask != NULL, FALSE);
  g_return_val_if_fail (other != NULL, FALSE);

  for (i = MIN (mask->len, other->len) - 1; i >= 0; i--)
    {
      if (g_array_index (mask, VALUE_TYPE, i) & g_array_index (other, VALUE_TYPE, i))
        return TRUE;
    }

  return FALSE;
}