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 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
|
/**************************************************************/
/* ********************************************************** */
/* * * */
/* * SIGNATURE TABLE * */
/* * * */
/* * $Module: TABLE * */
/* * * */
/* * Copyright (C) 1999, 2000, 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.3 $ * */
/* $State: Exp $ * */
/* $Date: 2011-05-25 10:01:24 $ * */
/* $Author: weidenb $ * */
/* * * */
/* * Contact: * */
/* * Christoph Weidenbach * */
/* * MPI fuer Informatik * */
/* * Stuhlsatzenhausweg 85 * */
/* * 66123 Saarbruecken * */
/* * Email: spass@mpi-inf.mpg.de * */
/* * Germany * */
/* * * */
/* ********************************************************** */
/**************************************************************/
/**************************************************************/
/* Includes */
/**************************************************************/
#include "table.h"
/**************************************************************/
/* Inline functions */
/**************************************************************/
int table_Index(SYMBOL symbol)
{
if (symbol_IsVariable(symbol))
return -(int) symbol;
else
return symbol_Index(symbol);
}
TERM table_GetTerm(TERMARRAY ta)
{
return ta->term;
}
TERMARRAY table_SetTerm(TERMARRAY ta, TERM term)
{
ta->term = term;
return ta;
}
int table_GetStamp(TERMARRAY ta)
{
return ta->stamp;
}
TERMARRAY table_SetStamp(TERMARRAY ta, int stamp)
{
ta->stamp = stamp;
return ta;
}
TERMARRAY table_GetChild(TERMARRAY ta)
{
return ta->child;
}
TERMARRAY table_SetChild(TERMARRAY ta, TERMARRAY child)
{
ta->child = child;
return ta;
}
TERMARRAY table_GetTermarray(TABLE table)
{
return table->ta;
}
TABLE table_SetTermarray(TABLE table, TERMARRAY ta)
{
table->ta = ta;
return table;
}
TERMARRAY *table_GetPoss(TABLE table)
{
return table->pos;
}
TERMARRAY table_GetPos(TABLE table, int tab_index)
{
return table_GetPoss(table)[tab_index];
}
TABLE table_SetPoss(TABLE table, TERMARRAY *ref)
{
table->pos = ref;
return table;
}
TABLE table_SetPos(TABLE table, int tab_index, TERMARRAY ta)
{
table_GetPoss(table)[tab_index] = ta;
return table;
}
int *table_GetPosstamps(TABLE table)
{
return table->posstamp;
}
int table_GetPosstamp(TABLE table, int tab_index)
{
return table_GetPosstamps(table)[tab_index];
}
TABLE table_SetPosstamps(TABLE table, int *ref)
{
table->posstamp = ref;
return table;
}
TABLE table_SetPosstamp(TABLE table, int tab_index, int stamp)
{
table_GetPosstamps(table)[tab_index] = stamp;
return table;
}
int table_GetStampcounter(TABLE table)
{
return table->stampcounter;
}
TABLE table_SetStampcounter(TABLE table, int stampcounter)
{
table->stampcounter = stampcounter;
return table;
}
int table_GetOpbound(TABLE table)
{
return table->opbound;
}
TABLE table_SetOpbound(TABLE table, int opbound)
{
table->opbound = opbound;
return table;
}
int table_GetVarbound(TABLE table)
{
return table->varbound;
}
TABLE table_SetVarbound(TABLE table, int varbound)
{
table->varbound = varbound;
return table;
}
int table_GetTermbound(TABLE table)
{
return table->termbound;
}
TABLE table_SetTermbound(TABLE table, int termbound)
{
table->termbound = termbound;
return table;
}
BOOL table_LegalPosIndex(TABLE table, int tab_index)
{
return 0 <= tab_index && tab_index <= table_GetTermbound(table);
}
BOOL table_Stamped(TABLE table, TERMARRAY ta)
{
return table_GetStamp(ta) == table_GetStampcounter(table);
}
TERMARRAY table_DelayedInit(TABLE table, TERMARRAY ta)
/***************************************************************
INPUT: a table and a termarray
RETURNS: the (now stamped) termarray
EFFECT: partially initializes table by setting the
termarray's entry to the empty term
***************************************************************/
{
if (!table_Stamped(table, ta)) {
table_SetTerm(ta, term_Null());
table_SetStamp(ta, table_GetStampcounter(table));
}
return ta;
}
BOOL table_PosStamped(TABLE table, int tab_index)
{
return table_GetPosstamp(table, tab_index) == table_GetStampcounter(table);
}
int table_DelayedPosInit(TABLE table, int tab_index)
/***************************************************************
INPUT: a table and a position tab_index
RETURNS: the (now stamped) position tab_index
EFFECT: partially initializes table by setting the Indexed
position to the empty pointer, which means that the
term with this Index is not stored in table
***************************************************************/
{
if (!table_PosStamped(table, tab_index)) {
table_SetPos(table, tab_index, (TERMARRAY) NULL);
table_SetPosstamp(table, tab_index, table_GetStampcounter(table));
}
return tab_index;
}
/**************************************************************/
/* Functions */
/**************************************************************/
TABLE table_Null(void)
{
return (TABLE) NULL;
}
TABLE table_Create(int opbound, int varbound, int termbound)
/***************************************************************
INPUT: bounds for the operator symbol, variable and term
indices of the terms to be stored in the signature
table (i. e. for every such term its top symbol Index
has to be in [1, opbound] and the term numbers of its
arguments in [0, termbound] - or its variable Index
in [1, varbound] if it is a variable)
RETURNS: a new (and empty) signature table
***************************************************************/
{
TABLE result;
#ifdef CHECK
if (opbound < 0) {
misc_StartErrorReport();
misc_ErrorReport("\n In table_Create: negative opbound.");
misc_FinishErrorReport();
}
if (varbound < 0) {
misc_StartErrorReport();
misc_ErrorReport("\n In table_Create: negative varbound.");
misc_FinishErrorReport();
}
if (termbound < 0) {
misc_StartErrorReport();
misc_ErrorReport("\n In table_Create: negative termbound.");
misc_FinishErrorReport();
}
#endif
result = (TABLE) memory_Malloc(sizeof(struct table));
table_SetTermarray(result, (TERMARRAY) memory_Calloc (
opbound + varbound + 1,
sizeof(struct termarray)
) + varbound);
/* move pointer to the middle of the array to allow negative indices */
table_SetPoss(
result,
(TERMARRAY*) memory_Malloc((termbound + 1) * sizeof(TERMARRAY))
);
table_SetPosstamps(result, (int*) memory_Calloc(termbound + 1, sizeof(int)));
table_SetOpbound(result, opbound);
table_SetVarbound(result, varbound);
table_SetTermbound(result, termbound);
table_SetStampcounter(result, 1);
return result;
}
static void table_FreeTermarray(TERMARRAY ta, int size)
/***************************************************************
INPUT: the termarray to free and its size
EFFECT: recursively frees the tree structure allocated for the
signature array
***************************************************************/
{
int i;
if (ta) {
for (i = 0; i < size; i++)
table_FreeTermarray(table_GetChild(ta + i), size);
memory_Free(ta, size * sizeof(struct termarray));
}
}
void table_Free(TABLE table)
{
int i;
if (table != table_Null()) {
for (i = -table_GetVarbound(table); i <= table_GetOpbound(table); i++)
table_FreeTermarray(
table_GetChild(table_GetTermarray(table) + i),
table_GetTermbound(table) + 1
);
memory_Free(
table_GetTermarray(table) - table_GetVarbound(table),
(table_GetOpbound(table) + table_GetVarbound(table) + 1) * sizeof(struct
termarray)
);
memory_Free(
table_GetPoss(table),
(table_GetTermbound(table) + 1) * sizeof(TERMARRAY)
);
memory_Free(
table_GetPosstamps(table),
(table_GetTermbound(table) + 1) * sizeof(int)
);
memory_Free(table, sizeof(struct table));
}
}
TABLE table_Init(TABLE table, int opbound, int varbound, int termbound)
/***************************************************************
INPUT: the table to recycle and bounds for the operator
symbol, variable and term indices of the terms to be
stored in the signature table (i. e. for every such
term its top symbol Index has to be in [1, opbound]
and the term numbers of its arguments in
[0, termbound] - or its variable Index in
[1, varbound] if it is a variable)
RETURNS: a cleaned up signature table
CAUTION: potentially frees the old table, therefore must be
called inside of an assignment like:
table = table_Init(table, ...)
***************************************************************/
{
int opmax, varmax, termmax, i;
TERMARRAY old;
#ifdef CHECK
if (opbound < 0) {
misc_StartErrorReport();
misc_ErrorReport("\n In table_Init: negative opbound.");
misc_FinishErrorReport();
}
if (varbound < 0) {
misc_StartErrorReport();
misc_ErrorReport("\n In table_Init: negative varbound.");
misc_FinishErrorReport();
}
if (termbound < 0) {
misc_StartErrorReport();
misc_ErrorReport("\n In table_Init: negative termbound.");
misc_FinishErrorReport();
}
#endif
opmax = table_GetOpbound(table) > opbound ? table_GetOpbound(table) :
opbound;
varmax = table_GetVarbound(table) > varbound ? table_GetVarbound(table) :
varbound;
termmax = table_GetTermbound(table) > termbound ? table_GetTermbound(table) :
termbound;
table_SetStampcounter(table, table_GetStampcounter(table) + 1);
/* in case of stamp overflow or too small termarray nodes get a new table: */
if (table_GetStampcounter(table)<=0 || termbound>table_GetTermbound(table)) {
table_Free(table);
return table_Create(opmax, varmax, termmax);
}
/* if only the top layer of the tree is too small get a larger top layer: */
else if (opbound+varbound > table_GetOpbound(table)+table_GetVarbound(table)){
old = table_GetTermarray(table);
table_SetTermarray(table, (TERMARRAY) memory_Calloc(
opmax + varmax + 1,
sizeof(struct termarray)
) + varmax);
for (i = -table_GetVarbound(table); i <= table_GetOpbound(table); i++)
table_SetChild(table_GetTermarray(table) + i, table_GetChild(old + i));
memory_Free(
old - table_GetVarbound(table),
(table_GetOpbound(table) + table_GetVarbound(table) + 1) * sizeof(struct
termarray)
);
table_SetOpbound(table, opmax);
table_SetVarbound(table, varmax);
return table;
}
else {
/* move pointer to termarray's new middle: */
table_SetTermarray(
table,
table_GetTermarray(table) + table_GetOpbound(table) - opbound
);
table_SetVarbound(
table,
table_GetOpbound(table) + table_GetVarbound(table) - opbound
);
table_SetOpbound(table, opbound);
return table;
}
}
TERM table_QueryAndEnter(TABLE table, PARTITION p, TERM term)
/***************************************************************
RETURNS: a term with the same p-signature (sigtab_Index(top
symbol), [arg 1] , ..., [arg n] ) as term - or the
p p
empty term if no such term exists
EFFECT: term enters table in the latter case
***************************************************************/
{
TERMARRAY ta;
LIST terms;
#ifdef CHECK
if (part_Size(p) - 1 > table_GetTermbound(table)) {
misc_StartErrorReport();
misc_ErrorReport("\n In table_QueryAndEnter: partition not suitable.");
misc_FinishErrorReport();
}
if (table_Index(term_TopSymbol(term)) > table_GetOpbound(table)) {
misc_StartErrorReport();
misc_ErrorReport
("\n In table_QueryAndEnter: term's operation symbol out of bounds.");
misc_FinishErrorReport();
}
if (table_Index(term_TopSymbol(term)) < -table_GetVarbound(table)) {
misc_StartErrorReport();
misc_ErrorReport("\n In table_QueryAndEnter: variable out of bounds.");
misc_FinishErrorReport();
}
if (!table_LegalPosIndex(table, term_Size(term))) {
misc_StartErrorReport();
misc_ErrorReport("\n In table_QueryAndEnter: term out of bounds.");
misc_FinishErrorReport();
}
#endif
ta = table_GetTermarray(table) + table_Index(term_TopSymbol(term));
for (terms = term_ArgumentList(term); !list_Empty(terms); terms =
list_Cdr(terms)) {
if (!table_GetChild(ta))
table_SetChild(ta, (TERMARRAY) memory_Calloc (
table_GetTermbound(table) + 1,
sizeof(struct termarray)
));
ta = table_GetChild(ta) + part_Find(p, term_Size(list_Car(terms)));
}
table_DelayedInit(table, ta);
if (table_GetTerm(ta))
return table_GetTerm(ta);
else {
table_SetTerm(ta, term);
table_SetPos(table, table_DelayedPosInit(table, term_Size(term)), ta);
return term_Null();
}
}
TABLE table_Delete(TABLE table, TERM term)
/***************************************************************
EFFECT: if term has entered table before, it is deleted
***************************************************************/
{
int no;
#ifdef CHECK
if (!table_LegalPosIndex(table, term_Size(term))) {
misc_StartErrorReport();
misc_ErrorReport("\n In table_Delete: illegal table access.");
misc_FinishErrorReport();
}
#endif
no = term_Size(term);
table_DelayedPosInit(table, no);
if (table_GetPos(table, no)) {
#ifdef CHECK
if (!table_Stamped(table, table_GetPos(table, no))) {
misc_StartErrorReport();
misc_ErrorReport("\n In table_Delete: table corrupted.");
misc_FinishErrorReport();
}
#endif
table_SetTerm(table_GetPos(table, no), term_Null());
table_SetPos(table, no, (TERMARRAY) NULL);
}
return table;
}
|