sparseset.c 5.27 KB
Newer Older
1
/* SparseSet implementation.
2
   Copyright (C) 2007-2014 Free Software Foundation, Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
   Contributed by Peter Bergner <bergner@vnet.ibm.com>

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.

GCC 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 General Public License
for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

21 22
#include "config.h"
#include "system.h"
23 24 25 26 27 28 29 30 31 32
#include "sparseset.h"

/* Allocate and clear a n_elms SparseSet.  */

sparseset
sparseset_alloc (SPARSESET_ELT_TYPE n_elms)
{
  unsigned int n_bytes = sizeof (struct sparseset_def)
			 + ((n_elms - 1) * 2 * sizeof (SPARSESET_ELT_TYPE));

33
  sparseset set = XNEWVAR (struct sparseset_def, n_bytes);
34 35

  /* Mark the sparseset as defined to silence some valgrind uninitialized
36 37
     read errors when accessing set->sparse[n] when "n" is not, and never has
     been, in the set.  These uninitialized reads are expected, by design and
38 39 40
     harmless.  */
  VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (set, n_bytes));

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
  set->dense = &(set->elms[0]);
  set->sparse = &(set->elms[n_elms]);
  set->size = n_elms;
  sparseset_clear (set);
  return set;
}

/* Low level routine not meant for use outside of sparseset.[ch].
   Assumes idx1 < s->members and idx2 < s->members.  */

static inline void
sparseset_swap (sparseset s, SPARSESET_ELT_TYPE idx1, SPARSESET_ELT_TYPE idx2)
{
  SPARSESET_ELT_TYPE tmp = s->dense[idx2];
  sparseset_insert_bit (s, s->dense[idx1], idx2);
  sparseset_insert_bit (s, tmp, idx1);
}

/* Operation: S = S - {e}
   Delete e from the set S if it is a member of S.  */

void
sparseset_clear_bit (sparseset s, SPARSESET_ELT_TYPE e)
{
  if (sparseset_bit_p (s, e))
    {
      SPARSESET_ELT_TYPE idx = s->sparse[e];
      SPARSESET_ELT_TYPE iter = s->iter;
      SPARSESET_ELT_TYPE mem = s->members - 1;

      /* If we are iterating over this set and we want to delete a
	 member we've already visited, then we swap the element we
	 want to delete with the element at the current iteration
	 index so that it plays well together with the code below
	 that actually removes the element.  */
      if (s->iterating && idx <= iter)
	{
	  if (idx < iter)
	    {
	      sparseset_swap (s, idx, iter);
	      idx = iter;
	    }
	  s->iter_inc = 0;
	}

      /* Replace the element we want to delete with the last element
	 in the dense array and then decrement s->members, effectively
	 removing the element we want to delete.  */
      sparseset_insert_bit (s, s->dense[mem], idx);
      s->members = mem;
    }
}

/* Operation: D = S
   Restrictions: none.  */

void
sparseset_copy (sparseset d, sparseset s)
{
  SPARSESET_ELT_TYPE i;

  if (d == s)
    return;

  sparseset_clear (d);
  for (i = 0; i < s->members; i++)
    sparseset_insert_bit (d, s->dense[i], i);
  d->members = s->members;
}

/* Operation: D = A & B.
   Restrictions: none.  */

void
sparseset_and (sparseset d, sparseset a, sparseset b)
{
  SPARSESET_ELT_TYPE e;

  if (a == b)
    {
      if (d != a)
	sparseset_copy (d, a);
      return;
    }

  if (d == a || d == b)
    {
      sparseset s = (d == a) ? b : a;

      EXECUTE_IF_SET_IN_SPARSESET (d, e)
	if (!sparseset_bit_p (s, e))
	  sparseset_clear_bit (d, e);
    }
  else
    {
      sparseset sml, lrg;

      if (sparseset_cardinality (a) < sparseset_cardinality (b))
	{
	  sml = a;
	  lrg = b;
	}
      else
	{
	  sml = b;
	  lrg = a;
	}

      sparseset_clear (d);
      EXECUTE_IF_SET_IN_SPARSESET (sml, e)
	if (sparseset_bit_p (lrg, e))
	  sparseset_set_bit (d, e);
    }
}

/* Operation: D = A & ~B.
   Restrictions: D != B, unless D == A == B.  */

void
sparseset_and_compl (sparseset d, sparseset a, sparseset b)
{
  SPARSESET_ELT_TYPE e;

  if (a == b)
    {
      sparseset_clear (d);
      return;
    }

  gcc_assert (d != b);

  if (d == a)
    {
      if (sparseset_cardinality (d) < sparseset_cardinality (b))
	{
	  EXECUTE_IF_SET_IN_SPARSESET (d, e)
	    if (sparseset_bit_p (b, e))
	      sparseset_clear_bit (d, e);
	}
      else
	{
	  EXECUTE_IF_SET_IN_SPARSESET (b, e)
	    sparseset_clear_bit (d, e);
	}
    }
  else
    {
      sparseset_clear (d);
      EXECUTE_IF_SET_IN_SPARSESET (a, e)
	if (!sparseset_bit_p (b, e))
	  sparseset_set_bit (d, e);
    }
}

/* Operation: D = A | B.
   Restrictions: none.  */

void
sparseset_ior (sparseset d, sparseset a, sparseset b)
{
  SPARSESET_ELT_TYPE e;

  if (a == b)
    sparseset_copy (d, a);
  else if (d == b)
    {
      EXECUTE_IF_SET_IN_SPARSESET (a, e)
	sparseset_set_bit (d, e);
    }
  else
    {
      if (d != a)
        sparseset_copy (d, a);
      EXECUTE_IF_SET_IN_SPARSESET (b, e)
	sparseset_set_bit (d, e);
    }
}

/* Operation: A == B
   Restrictions: none.  */

bool
sparseset_equal_p (sparseset a, sparseset b)
{
  SPARSESET_ELT_TYPE e;

  if (a == b)
    return true;

  if (sparseset_cardinality (a) != sparseset_cardinality (b))
    return false;

  EXECUTE_IF_SET_IN_SPARSESET (a, e)
    if (!sparseset_bit_p (b, e))
      return false;

  return true;
}