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
|
/**************************************************************/
/* ********************************************************** */
/* * * */
/* * PARTITION * */
/* * * */
/* * $Module: PARTITION * */
/* * * */
/* * Copyright (C) 1999, 2001 MPI fuer Informatik * */
/* * * */
/* * This program is free software; you can redistribute * */
/* * it and/or modify it under the terms of the FreeBSD * */
/* * Licence. * */
/* * * */
/* * This program 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 LICENCE file * */
/* * for more details. * */
/* * * */
/* * * */
/* $Revision: 1.2 $ * */
/* $State: Exp $ * */
/* $Date: 2010-02-22 14:09:58 $ * */
/* $Author: weidenb $ * */
/* * * */
/* * Contact: * */
/* * Christoph Weidenbach * */
/* * MPI fuer Informatik * */
/* * Stuhlsatzenhausweg 85 * */
/* * 66123 Saarbruecken * */
/* * Email: spass@mpi-inf.mpg.de * */
/* * Germany * */
/* * * */
/* ********************************************************** */
/**************************************************************/
/**************************************************************/
/* Include */
/**************************************************************/
#include "partition.h"
/**************************************************************/
/* Inline functions */
/**************************************************************/
static __inline__ ECLASS part_GetClass(PARTITION p, ELEMENT e)
{
return p[e];
}
static __inline__ PARTITION part_SetClass(PARTITION p, ELEMENT e, ECLASS c)
{
p[e] = c;
return p;
}
static __inline__ int part_GetClassSize(PARTITION p, ELEMENT e)
{
return p[p[part_CARD] + e];
}
static __inline__ PARTITION part_SetClassSize(PARTITION p, ELEMENT e, int
classsize)
{
p[p[part_CARD] + e] = classsize;
return p;
}
static __inline__ int part_GetStamp(PARTITION p, ELEMENT e)
{
return p[-part_HEAD - 1 - e];
}
static __inline__ PARTITION part_SetStamp(PARTITION p, ELEMENT e, int stamp)
{
p[-part_HEAD - 1 - e] = stamp;
return p;
}
static __inline__ BOOL part_Stamped(PARTITION p, ELEMENT e)
{
return part_GetStamp(p, e) == p[part_STAMPCOUNTER];
}
static __inline__ ELEMENT part_DelayedInit(PARTITION p, ELEMENT e)
/***************************************************************
RETURNS: the (now stamped) element
EFFECT: establishes the equivalence class {e} in p, thus
partially initializing p
***************************************************************/
{
if (!part_Stamped(p, e)) {
part_SetClass(p, e, -e - 1); /* representative e (>= 0) of {e} is coded */
/* as -e - 1 (< 0) */
part_SetClassSize(p, e, 1);
part_SetStamp(p, e, p[part_STAMPCOUNTER]);
}
return e;
}
/**************************************************************/
/* Functions */
/**************************************************************/
PARTITION part_Create(int size)
/****************************************************************
RETURNS: the initial partition {{0}, {1}, {2}, ..., {size - 1}}
of the set {0, 1, 2, ..., size - 1}
****************************************************************/
{
PARTITION result;
#ifdef CHECK
if (size < 0) {
misc_StartErrorReport();
misc_ErrorReport("\n In part_Create: negative size %d.", size);
misc_FinishErrorReport();
}
#endif
result = (PARTITION) memory_Calloc(size * 3 + part_HEAD, sizeof(int))
+ size + part_HEAD; /* move pointer to the middle of the array */
/* to allow negative indices */
result[part_CARD] = size;
result[part_ALLOC] = size * 3 + part_HEAD;
result[part_STAMPCOUNTER] = 1;
return result;
}
PARTITION part_Init(PARTITION p, int size)
/****************************************************************
RETURNS: the initial partition {{0}, {1}, {2}, ..., {size - 1}}
of the set {0, 1, 2, ..., size - 1}
EFFECT: stores the initial partition to p if it's big enough,
otherwise creates a new partition, therefore
CAUTION: must be called inside an assignment like:
p = part_Init(p, ...)
****************************************************************/
{
int alloc, i;
#ifdef CHECK
if (size < 0) {
misc_StartErrorReport();
misc_ErrorReport("\n In part_Init: negative size %d.", size);
misc_FinishErrorReport();
}
#endif
alloc = (p[part_ALLOC] - part_HEAD) / 3;
if (size > alloc) {
part_Free(p);
p = part_Create(size);
}
else {
p[part_CARD] = size;
p[part_STAMPCOUNTER]++;
/* if a stamp overflow occurs, reinit stamps: */
if (p[part_STAMPCOUNTER] <= 0) {
for (i = 0; i < alloc; i++)
part_SetStamp(p, i, 0);
p[part_STAMPCOUNTER] = 1;
}
}
return p;
}
static ELEMENT part_NF(PARTITION p, ELEMENT e)
/***************************************************************
RETURNS: the normal form element of the class [e]; this is an
element of [e] that sometimes differ from the
representative
EFFECT: makes the normal form to the direct parent of all
elements visited on the search path from e to this
normal form ("path compression")
***************************************************************/
{
ELEMENT nf, aux;
#ifdef CHECK
if (!part_Element(p, e)) {
misc_StartErrorReport();
misc_ErrorReport("\n In part_NF: %d not in partitioned set.", e);
misc_FinishErrorReport();
}
#endif
nf = e;
while (part_GetClass(p, part_DelayedInit(p, nf)) >= 0)
nf = part_GetClass(p, nf);
/* path compression: */
while (e != nf) {
aux = part_GetClass(p, e);
part_SetClass(p, e, nf);
e = aux;
}
return nf;
}
ECLASS part_Find(PARTITION p, ELEMENT e)
/***************************************************************
RETURNS: (the representative of) class [e]
***************************************************************/
{
#ifdef CHECK
if (!part_Element(p, e)) {
misc_StartErrorReport();
misc_ErrorReport("\n In part_Find: %d not in partitioned set.", e);
misc_FinishErrorReport();
}
#endif
return -part_GetClass(p, part_NF(p, e)) - 1;
/* representative e is coded as -e - 1 (cf. part_DelayedInit) */
}
PARTITION part_Union(PARTITION p, ECLASS c1, ECLASS c2)
/***************************************************************
RETURNS: the union of the classes
EFFECT: the representative of c1 is the representative of the
union
***************************************************************/
{
ELEMENT nf1, nf2, aux;
#ifdef CHECK
if (!part_Element(p, c1)) {
misc_StartErrorReport();
misc_ErrorReport("\n In part_Union: first class %d not in partitioned set.",
c1);
misc_FinishErrorReport();
}
if (!part_Element(p, c2)) {
misc_StartErrorReport();
misc_ErrorReport
("\n In part_Union: second class %d not in partitioned set.", c2);
misc_FinishErrorReport();
}
#endif
nf1 = part_NF(p, c1);
nf2 = part_NF(p, c2);
if (nf1 != nf2) {
/* make [nf1] the bigger (or at least not smaller) class: */
if (part_GetClassSize(p, nf1) < part_GetClassSize(p, nf2)) {
aux = nf1;
nf1 = nf2;
nf2 = aux;
part_SetClass(p, nf1, part_GetClass(p, nf2));
part_SetClass(p, -part_GetClass(p, nf2) - 1, nf1);
}
part_SetClass(p, nf2, nf1);
part_SetClassSize(p, nf1,
part_GetClassSize(p, nf1) + part_GetClassSize(p, nf2));
}
return p;
}
|