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 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
|
/**************************************************************/
/* ********************************************************** */
/* * * */
/* * STRUCTURE SHARING * */
/* * * */
/* * $Module: SHARING * */
/* * * */
/* * Copyright (C) 1996, 1997, 1998, 1999, 2000 * */
/* * 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:59 $ * */
/* $Author: weidenb $ * */
/* * * */
/* * Contact: * */
/* * Christoph Weidenbach * */
/* * MPI fuer Informatik * */
/* * Stuhlsatzenhausweg 85 * */
/* * 66123 Saarbruecken * */
/* * Email: spass@mpi-inf.mpg.de * */
/* * Germany * */
/* * * */
/* ********************************************************** */
/**************************************************************/
/* $RCSfile: sharing.c,v $ */
#include "sharing.h"
/**************************************************************/
/* Static Variables */
/**************************************************************/
#ifdef CHECK
static BOOL sharing_DATABLOCKED;
#endif
static LIST sharing_DATALIST = (LIST) NULL;
#define sharing_STACKSIZE 500
static LIST sharing_STACK[sharing_STACKSIZE];
static LIST* sharing_STACKPOINTER = sharing_STACK;
/**************************************************************/
/* Prototypes for static functions used only in this module */
/**************************************************************/
static BOOL sharing_IsNoMoreUsed(TERM);
static LIST sharing_InternGetDataList(TERM);
static TERM sharing_InsertIntoSharing(TERM, SHARED_INDEX);
static void sharing_DeleteFromSharing(TERM, SHARED_INDEX);
static void sharing_ResetTermStamp(TERM);
static void sharing_PrintWithSuperterms(TERM);
/**************************************************************/
/* ********************************************************** */
/* * * */
/* * PRIMITIVE SHARING FUNCTIONS * */
/* * * */
/* ********************************************************** */
/**************************************************************/
SHARED_INDEX sharing_IndexCreate(void)
/**********************************************************
INPUT: None.
RETURNS: A shared index, consisting of an Index, a Consttable
and a Vartable.
EFFECTS: Initializes the shared Index for the sharing_Vartable and the
sharing_Consttable with NULL-Pointers and an empty st_index.
**********************************************************/
{
SHARED_INDEX Result;
int i;
Result = (SHARED_INDEX)memory_Malloc(sizeof(SHARED_INDEX_NODE));
sharing_SetIndex(Result, st_IndexCreate());
for (i=0; (i < symbol_MaxVars()); i++)
sharing_SetVartableEntry(Result, i, NULL);
for (i=0; (i < symbol_MaxConsts()); i++)
sharing_SetConsttableEntry(Result, i, NULL);
sharing_SetStampID(Result, term_GetStampID());
return Result;
}
void sharing_IndexDelete(SHARED_INDEX ShIndex)
/**********************************************************
INPUT: A shared Index.
RETURNS: None.
EFFECTS: Deletes the Index and frees the memory for the
structure including the Const- and Vartable.
**********************************************************/
{
st_IndexDelete(sharing_Index(ShIndex));
memory_Free(ShIndex, sizeof(SHARED_INDEX_NODE));
}
void sharing_PushOnStack(TERM Term)
/**********************************************************
INPUT: A term.
RETURNS: None.
EFFECTS: Creates a Stack of Pointers to the
term and all of its subterms in their order in
the arglist (thus ordered by depth).
top of the stack is bottom term
**********************************************************/
{
LIST ArgList;
#ifdef CHECK
if (!term_IsTerm(Term)) {
misc_StartErrorReport();
misc_ErrorReport("\n In sharing_PushOnStack: Illegal input.\n");
misc_FinishErrorReport();
}
#endif
stack_Push(Term);
ArgList = term_ArgumentList(Term);
while (!list_Empty(ArgList)){
sharing_PushOnStack(list_Car(ArgList));
ArgList = list_Cdr(ArgList);
}
}
void sharing_PushReverseOnStack(TERM Term)
/**********************************************************
INPUT: A term.
RETURNS: None.
EFFECTS: Creates a Stack of Pointers to the
term and all of its subterms, except variables in their order in
the arglist (thus ordered by depth).
top of the stack is top term
**********************************************************/
{
LIST ArgList;
#ifdef CHECK
if (!term_IsTerm(Term)) {
misc_StartErrorReport();
misc_ErrorReport("\n In sharing_PushReverseOnStack: Illegal input.\n");
misc_FinishErrorReport();
}
#endif
if (!term_IsVariable(Term)) {
ArgList = term_ArgumentList(Term);
while (!list_Empty(ArgList)){
sharing_PushReverseOnStack(list_Car(ArgList));
ArgList = list_Cdr(ArgList);
}
stack_Push(Term);
}
}
void sharing_PushReverseOnStackExcept(TERM Term, LIST DontTermList)
/**********************************************************
INPUT: A term and an exception list.
RETURNS: None.
EFFECTS: Creates a Stack of Pointers to the
term and all of its subterms that are not contained
or below the terms in DontTermList in their order in
the arglist (thus ordered by depth).
top of the stack is top term
**********************************************************/
{
LIST ArgList;
#ifdef CHECK
if (!term_IsTerm(Term)) {
misc_StartErrorReport();
misc_ErrorReport("\n In sharing_PushReverseOnStack: Illegal input.\n");
misc_FinishErrorReport();
}
#endif
if (!term_IsVariable(Term) && !term_ListContainsTerm(DontTermList, Term)) {
ArgList = term_ArgumentList(Term);
while (!list_Empty(ArgList)){
sharing_PushReverseOnStackExcept(list_Car(ArgList), DontTermList);
ArgList = list_Cdr(ArgList);
}
stack_Push(Term);
}
}
void sharing_PushOnStackNoStamps(TERM Term)
/**********************************************************
INPUT: A term.
RETURNS: None.
EFFECTS: Creates a Stack of Pointers to the
term and all of its subterms that are not stamped
or below stamped terms.
top of the stack is top term.
**********************************************************/
{
LIST ArgList;
#ifdef CHECK
if (!term_IsTerm(Term)) {
misc_StartErrorReport();
misc_ErrorReport("\n In sharing_PushReverseOnStackNoStamps: Illegal input.\n");
misc_FinishErrorReport();
}
#endif
if (!term_IsVariable(Term) && !term_HasTermStamp(Term)) {
stack_Push(Term);
ArgList = term_ArgumentList(Term);
while (!list_Empty(ArgList)){
sharing_PushOnStackNoStamps(list_Car(ArgList));
ArgList = list_Cdr(ArgList);
}
}
}
void sharing_PushListOnStack(LIST TermList)
/**********************************************************
INPUT: A list of terms.
RETURNS: None.
EFFECTS: Creates a Stack of Pointers to the
terms in <TermList> and all of subterms in their order in
the arglist (thus ordered by depth).
**********************************************************/
{
while (!list_Empty(TermList)) {
sharing_PushOnStack(list_Car(TermList));
TermList = list_Cdr(TermList);
}
}
void sharing_PushListReverseOnStack(LIST TermList)
/**********************************************************
INPUT: A list of terms.
RETURNS: None.
EFFECTS: Puts all terms in Termlist and their superterms
in their order in the arglist (thus ordered by depth)
on the stack.
On top of the stack are subterms.
**********************************************************/
{
while (!list_Empty(TermList)) {
sharing_PushReverseOnStack(list_Car(TermList));
TermList = list_Cdr(TermList);
}
}
void sharing_PushListReverseOnStackExcept(LIST TermList, LIST DontPushList)
/**********************************************************
INPUT: Two lists of terms.
RETURNS: None.
EFFECTS: Puts all terms in Termlist except those contained in
DontPushList and their superterms
in their order in the arglist (thus ordered by depth)
on the stack.
On top of the stack are subterms.
**********************************************************/
{
while (!list_Empty(TermList)) {
sharing_PushReverseOnStackExcept(list_Car(TermList), DontPushList);
TermList = list_Cdr(TermList);
}
}
void sharing_PushListOnStackNoStamps(LIST TermList)
/**********************************************************
INPUT: A list of terms.
RETURNS: None.
EFFECTS: Puts all terms in Termlist on the stack.
On top of the stack are subterms.
**********************************************************/
{
while (!list_Empty(TermList)) {
sharing_PushOnStackNoStamps(list_Car(TermList));
TermList = list_Cdr(TermList);
}
}
static BOOL sharing_IsNoMoreUsed(TERM Term)
/**********************************************************
INPUT: A term.
RETURNS: TRUE, if the Term has no superterms,
FALSE, else.
**********************************************************/
{
#ifdef CHECK
if (!term_IsTerm(Term)) {
misc_StartErrorReport();
misc_ErrorReport("\n In sharing_IsNoMoreUsed: Illegal input.\n");
misc_FinishErrorReport();
}
#endif
return (list_Empty(term_SupertermList(Term)));
}
/**************************************************************/
/* ********************************************************** */
/* * * */
/* * FUNCTIONS FOR INSERTION AND DELETION * */
/* * * */
/* ********************************************************** */
/**************************************************************/
/**************************************************************/
/* ********************************************************** */
/* * * */
/* * 1) FUNCTIONS FOR INSERTION * */
/* * * */
/* ********************************************************** */
/**************************************************************/
TERM sharing_Insert(POINTER Data, TERM Atom, SHARED_INDEX SharedIndex)
/**********************************************************
INPUT: A data element, it's unshared atom and a SharedIndex.
RETURNS: The shared term inserted into the SharedIndex.
CAUTION: The superterm slot of <Atom> is destructively used!
MEMORY: The term 'Atom' still exists, memory needed for
the shared version is allocated.
**********************************************************/
{
#ifdef CHECK
if (!term_IsTerm(Atom) || (!sharing_IsNoMoreUsed(Atom))){
misc_StartErrorReport();
misc_ErrorReport("\n In sharing_Insert: Illegal input.\n");
misc_FinishErrorReport();
}
#endif
Atom = sharing_InsertIntoSharing(Atom, SharedIndex);
term_RplacSupertermList(Atom, list_Cons(Data, term_SupertermList(Atom)));
return(Atom);
}
static TERM sharing_InsertIntoSharing(TERM Term, SHARED_INDEX SharedIndex)
/**********************************************************
INPUT: A term and a shared index.
RETURNS: A copy of 'Term' which is inserted into the Sharing
and the "SharedIndex".
MEMORY: The unshared version isn't freed, needed memory
is allocated.
**********************************************************/
{
int B_Stack;
TERM InsTerm;
#ifdef CHECK
if (!term_IsTerm(Term)) {
misc_StartErrorReport();
misc_ErrorReport("\n In sharing_InsertIntoSharing: Illegal input.\n");
misc_FinishErrorReport();
}
#endif
B_Stack = stack_Bottom();
sharing_PushOnStack(Term);
InsTerm = stack_Top(); /* not necessary initialization ... */
while (! stack_Empty(B_Stack)){
InsTerm = stack_PopResult();
if (term_IsVariable(InsTerm)){
if (!sharing_IsSharedVar(InsTerm, SharedIndex)) {
sharing_SetVartableEntry(SharedIndex, sharing_VariableIndex(InsTerm),
term_Create(term_TopSymbol(InsTerm),
list_Nil()));
st_EntryCreate(sharing_Index(SharedIndex),
sharing_VartableEntry(SharedIndex,
sharing_VariableIndex(InsTerm)),
sharing_VartableEntry(SharedIndex,
sharing_VariableIndex(InsTerm)),
cont_LeftContext());
}
/* The NULL-Pointer in the Vartable is replaced by a copy of the */
/* InsTerm, which is referenced by the InsTerms SharedTermCopy. */
/* A complex T still has to be insert. into its Args superlists. */
/* The unshared Term isn't yet in the 'SharedIndex' and thus is */
/* inserted, too. */
sharing_RememberSharedTermCopy(InsTerm,
sharing_VartableEntry(SharedIndex, sharing_VariableIndex(InsTerm)));
}else if (term_IsConstant(InsTerm)){
if (!sharing_IsSharedConst(InsTerm, SharedIndex)) {
sharing_SetConsttableEntry(SharedIndex, sharing_ConstantIndex(InsTerm),
term_Create(term_TopSymbol(InsTerm),
list_Nil()));
st_EntryCreate(sharing_Index(SharedIndex),
sharing_ConsttableEntry(SharedIndex, sharing_ConstantIndex(InsTerm)),
sharing_ConsttableEntry(SharedIndex, sharing_ConstantIndex(InsTerm)),
cont_LeftContext());
}
/* The NULL-Pointer in the Consttable is replaced by a copy of the */
/* InsTerm, which is referenced by the InsTerms SharedTermCopy. */
/* A complex T still has to be insert. into its Args superlists. */
/* The unshared Term isn't yet in the 'SharedIndex' and thus is */
/* inserted, too. */
sharing_RememberSharedTermCopy(InsTerm,
sharing_ConsttableEntry(SharedIndex, sharing_ConstantIndex(InsTerm)));
}else{ /* term_IsComplex(InsTerm) */
/* -> owns subterms which now have copies in Sharing. */
TERM SharedDuplicate;
LIST HelpList;
BOOL DuplCandHasSameArgs;
SharedDuplicate = (TERM)NULL;
HelpList =
term_SupertermList(sharing_SharedTermCopy(term_FirstArgument(InsTerm)));
if (list_Empty(HelpList)){
SharedDuplicate = term_Create(term_TopSymbol(InsTerm), list_Nil());
DuplCandHasSameArgs = FALSE;
}
else{
DuplCandHasSameArgs = FALSE;
while (!DuplCandHasSameArgs && !list_Empty(HelpList)){
SharedDuplicate = (TERM) list_First(HelpList);
if (term_TopSymbol(SharedDuplicate) == term_TopSymbol(InsTerm)){
LIST OrigHelpArgList, DuplHelpArgList;
OrigHelpArgList = term_ArgumentList(InsTerm);
DuplHelpArgList = term_ArgumentList(SharedDuplicate);
while (!list_Empty(OrigHelpArgList) &&
(DuplCandHasSameArgs =
(sharing_SharedTermCopy(list_First(OrigHelpArgList)) ==
list_First(DuplHelpArgList)))){
DuplHelpArgList = list_Cdr(DuplHelpArgList);
OrigHelpArgList = list_Cdr(OrigHelpArgList);
}
}
HelpList = list_Cdr(HelpList);
}
if (!DuplCandHasSameArgs)
SharedDuplicate = term_Create(term_TopSymbol(InsTerm), list_Nil());
} /* end of else fuer Behandlung von InsTerm mit shared FirstArg. */
if (!DuplCandHasSameArgs){
/* Falls neue Kopie gemacht wurde, wird diese hier "eingeshared": */
for (HelpList = term_ArgumentList(InsTerm); !list_Empty(HelpList);
HelpList = list_Cdr(HelpList)){
TERM SharedArg;
SharedArg = sharing_SharedTermCopy((TERM)list_First(HelpList));
term_RplacArgumentList(SharedDuplicate,
list_Cons(SharedArg,
term_ArgumentList(SharedDuplicate)));
term_RplacSupertermList(SharedArg,
list_Cons(SharedDuplicate,
term_SupertermList(SharedArg)));
}
term_RplacArgumentList(SharedDuplicate,
list_NReverse(term_ArgumentList(SharedDuplicate)));
/* Now a newly generated term can be inserted into the 'SharedIndex' ! */
st_EntryCreate(sharing_Index(SharedIndex),
SharedDuplicate,
SharedDuplicate,
cont_LeftContext());
}
sharing_RememberSharedTermCopy(InsTerm,
SharedDuplicate);
}
}
return(sharing_SharedTermCopy(InsTerm));
}
/**************************************************************/
/* ********************************************************** */
/* * * */
/* * 2) FUNCTIONS FOR DELETION * */
/* * * */
/* ********************************************************** */
/**************************************************************/
void sharing_Delete(POINTER Data, TERM Atom, SHARED_INDEX SharedIndex)
/**********************************************************
INPUT: A data element, its atom and an index.
RETURNS: Nothing.
MEMORY: Destructive, deletes 'Atom' from Sharing and frees
memory that's no more needed.
***********************************************************/
{
#ifdef CHECK
if (!term_IsTerm(Atom)){
misc_StartErrorReport();
misc_ErrorReport("\n In sharing_Delete: Illegal input.\n");
misc_FinishErrorReport();
}
#endif
term_RplacSupertermList(Atom,
list_PointerDeleteElement(term_SupertermList(Atom), Data));
if (sharing_IsNoMoreUsed(Atom))
sharing_DeleteFromSharing(Atom, SharedIndex);
}
static void sharing_DeleteFromSharing(TERM Term, SHARED_INDEX SharedIndex)
/**********************************************************
INPUT: A term and a SharedIndex
RETURNS: Nothing
MEMORY: 'Term' is removed from the Sharing, only correct
if 'Term' is unshared in sharing (real subterms
may be shared, off course).
***********************************************************/
{
BOOL IsIndexed;
#ifdef CHECK
if (!term_IsTerm(Term)) {
misc_StartErrorReport();
misc_ErrorReport("\n In sharing_DeleteFromSharing: Illegal input.\n");
misc_FinishErrorReport();
}
#endif
IsIndexed = st_EntryDelete(sharing_Index(SharedIndex), Term, Term, cont_LeftContext());
#ifdef CHECK
if (!IsIndexed) {
misc_StartErrorReport();
misc_ErrorReport("\n In sharing_DeleteFromSharing: Input not indexed.\n");
misc_FinishErrorReport();
}
#endif
if (term_IsComplex(Term)){
LIST Args;
Args = term_ArgumentList(Term);
while (!list_Empty(Args)){
TERM NextArg;
LIST Help;
/* This block frees the terms arglists memory. */
Help = Args;
NextArg = (TERM) list_First(Args);
Args = list_Cdr(Args);
list_Free(Help);
term_RplacSupertermList(NextArg,
list_PointerDeleteOneElement(term_SupertermList(NextArg), Term));
if (sharing_IsNoMoreUsed(NextArg))
sharing_DeleteFromSharing(NextArg, SharedIndex);
}
} else if (term_IsConstant(Term)) {
sharing_SetConsttableEntry(SharedIndex, sharing_ConstantIndex(Term), NULL);
} else {
sharing_SetVartableEntry(SharedIndex, sharing_VariableIndex(Term), NULL);
}
list_Delete(term_SupertermList(Term));
term_Free(Term);
}
/**************************************************************/
/* Functions to access unshared data via the shared term. */
/**************************************************************/
LIST sharing_GetDataList(TERM Term, SHARED_INDEX SharedIndex)
/**********************************************************
INPUT: A shared term and the shared index it belongs to.
RETURNS: The list of data connected to all superterms of Term,
e.g. the list of all literals containing Term.
EFFECT: Allocates memory for the returned list.
CAUTION: Works recursive!
**********************************************************/
{
LIST Result = list_Nil();
#ifdef CHECK
if (!term_IsTerm(Term) || (SharedIndex == NULL)){
misc_StartErrorReport();
misc_ErrorReport("\n In sharing_GetDataList: Illegal input.\n");
misc_FinishErrorReport();
}
/* What if the term doesn't belong to this shared index ?????? */
#endif
if (term_StampOverflow(sharing_StampID(SharedIndex)))
sharing_ResetAllTermStamps(SharedIndex);
term_StartStamp();
Result = sharing_InternGetDataList(Term);
term_StopStamp();
return Result;
}
static LIST sharing_InternGetDataList(TERM Term)
/**********************************************************
INPUT: A shared term.
RETURNS: The list of data connected to all superterms of Term
EFFECT: Allocates memory for the returned list.
********************************************************/
{
if (term_IsAtom(Term))
/* We are at top level of the superterm "tree", */
/* so the recursion stops here */
return list_Copy(term_AtomsLiterals(Term));
else{
LIST SuperList;
LIST DataList = list_Nil();
for (SuperList = term_SupertermList(Term); !list_Empty(SuperList);
SuperList = list_Cdr(SuperList)) {
TERM superterm = (TERM) list_Car(SuperList);
if (!term_AlreadyVisited(superterm)) {
DataList = list_Nconc(sharing_InternGetDataList(superterm), DataList);
term_SetTermStamp(superterm);
}
}
return DataList;
}
}
void sharing_StartDataIterator(TERM Term, SHARED_INDEX SharedIndex)
/**********************************************************
INPUT: A shared term and the shared index it belongs to.
RETURNS: Nothing.
EFFECT: Prepares the data iterator for Term.
After this call you can access single data items
for Term with function sharing_GetNextData.
********************************************************/
{
#ifdef CHECK
if (!term_IsTerm(Term) || (SharedIndex == NULL)){
misc_StartErrorReport();
misc_ErrorReport("\n In sharing_StartDataIterator: Illegal input.\n");
misc_FinishErrorReport();
}
if (sharing_DATABLOCKED) {
misc_StartErrorReport();
misc_ErrorReport("\n In sharing_StartDataIterator: Data iterator already used.\n");
misc_FinishErrorReport();
}
sharing_DATABLOCKED = TRUE;
#endif
if (term_StampOverflow(sharing_StampID(SharedIndex)))
sharing_ResetAllTermStamps(SharedIndex);
term_StartStamp();
/* Init stack */
sharing_STACKPOINTER = sharing_STACK; /* Pop all items */
while (!term_IsAtom(Term)) {
/* Push superterm list on stack */
*(sharing_STACKPOINTER++) = term_SupertermList(Term);
Term = list_Car(term_SupertermList(Term));
}
sharing_DATALIST = term_AtomsLiterals(Term);
}
POINTER sharing_GetNextData(void)
/**********************************************************
INPUT: None
RETURNS: A single data item connected to the term specified
in the previous call of sharing_StartDataIterator.
NULL is returned, if all data items were accessed before.
EFFECT: In contrast to function sharing_GetDataList
no memory is allocated.
********************************************************/
{
POINTER Result = NULL;
LIST superlist;
#ifdef CHECK
if (!sharing_DATABLOCKED) {
misc_StartErrorReport();
misc_ErrorReport("\n In sharing_GetNextData: Iterator wasn't started.\n");
misc_FinishErrorReport();
}
#endif
if (!list_Empty(sharing_DATALIST)) {
Result = list_Car(sharing_DATALIST);
sharing_DATALIST = list_Cdr(sharing_DATALIST);
} else {
superlist = NULL;
while ((sharing_STACKPOINTER > sharing_STACK) && /* stack not empty */
list_Empty(superlist)) {
/* change between backtracking and expansion */
do { /* Backtracking */
superlist = *(--sharing_STACKPOINTER); /* PopResult */
term_SetTermStamp(list_Car(superlist));
do
superlist = list_Cdr(superlist);
while (!list_Empty(superlist) &&
term_AlreadyVisited(list_Car(superlist)));
} while ((sharing_STACKPOINTER>sharing_STACK) && list_Empty(superlist));
while (!list_Empty(superlist) &&
!term_IsAtom(list_Car(superlist))) { /* Expansion */
*(sharing_STACKPOINTER++) = superlist;
superlist = term_SupertermList(list_Car(superlist));
/* Search next unvisited term */
while (!list_Empty(superlist) &&
term_AlreadyVisited(list_Car(superlist)))
superlist = list_Cdr(superlist);
}
}
if (!list_Empty(superlist)) {
*(sharing_STACKPOINTER++) = superlist;
sharing_DATALIST = term_AtomsLiterals(list_Car(superlist));
Result = list_Car(sharing_DATALIST);
sharing_DATALIST = list_Cdr(sharing_DATALIST);
}
} /* else */
return Result;
}
void sharing_StopDataIterator(void)
/**********************************************************
INPUT: None.
RETURNS: Nothing.
EFFECT: The data iterator is stopped for the term you specified
in the corresponding call of sharing_StartDataIterator.
********************************************************/
{
#ifdef CHECK
if (!sharing_DATABLOCKED) {
misc_StartErrorReport();
misc_ErrorReport("\n In sharing_StopDataIterator: Iterator wasn't started.\n");
misc_FinishErrorReport();
}
sharing_DATABLOCKED = FALSE;
#endif
sharing_DATALIST = list_Nil();
term_StopStamp();
}
LIST sharing_NAtomDataList(TERM Atom)
/**********************************************************
INPUT: A shared term.
RETURNS: A List of data connected with 'Term' or superterms.
EFFECT: No memory Allocation
CAUTION: THE RETURNED LIST MUST NOT CHANGE
**********************************************************/
{
#ifdef CHECK
if (!term_IsTerm(Atom) || !term_IsAtom(Atom)){
misc_StartErrorReport();
misc_ErrorReport("\n In sharing_NAtomDataList: Illegal input.\n");
misc_FinishErrorReport();
}
#endif
return(term_AtomsLiterals(Atom));
}
LIST sharing_GetAllSuperTerms(SHARED_INDEX Index)
/**********************************************************
INPUT: A shared Index.
RETURNS: A List of all Data in the shared Index.
EFFECT: The term stamp is used.
**********************************************************/
{
int i;
LIST Result = list_Nil();
TERM term;
if (term_StampOverflow(sharing_StampID(Index)))
sharing_ResetAllTermStamps(Index);
term_StartStamp();
for (i = 0; i < symbol_MaxVars(); i++) {
term = sharing_VartableEntry(Index,i);
if (term != NULL)
Result = list_Nconc(sharing_InternGetDataList(term), Result);
}
for (i = 0; i < symbol_MaxConsts(); i++) {
term = sharing_ConsttableEntry(Index,i);
if (term != NULL)
Result = list_Nconc(sharing_InternGetDataList(term), Result);
}
term_StopStamp();
return Result;
}
void sharing_ResetAllTermStamps(SHARED_INDEX SharedIndex)
/**********************************************************
INPUT: A shared index.
RETURNS: Nothing.
EFFECT: The stamps of all terms in the shared index are reset.
**********************************************************/
{
TERM term;
int i;
/* Reset stamp for all variables and their superterms */
for (i = 0; i < symbol_MaxVars(); i++) {
term = sharing_VartableEntry(SharedIndex, i);
if (term != NULL)
sharing_ResetTermStamp(term);
}
/* Reset stamp for all constants and their superterms */
for (i = 0; i < symbol_MaxConsts(); i++){
term = sharing_ConsttableEntry(SharedIndex, i);
if (term != NULL)
sharing_ResetTermStamp(term);
}
}
static void sharing_ResetTermStamp(TERM Term)
/**********************************************************
INPUT: A term.
RETURNS: Nothing.
EFFECT: The stamps of Term and all its superterms are reset.
**********************************************************/
{
LIST SuperList;
TERM SuperTerm;
if (!term_IsAtom(Term)) {
for (SuperList = term_SupertermList(Term); !list_Empty(SuperList);
SuperList = list_Cdr(SuperList)) {
SuperTerm = (TERM) list_Car(SuperList);
if (!term_StampAlreadyReset(SuperTerm)) {
sharing_ResetTermStamp(SuperTerm);
term_ResetTermStamp(SuperTerm);
}
}
}
}
NAT sharing_GetNumberOfOccurances(TERM Term)
/**************************************************************
INPUT: A shared (!) term.
RETURNS: How many literals contain <Term>.
Note that literals containing <Term> <n> times are counted
<n> times.
***************************************************************/
{
if (term_IsAtom(Term))
/* Stop recursion */
return list_Length(term_AtomsLiterals(Term));
else {
LIST Scan;
NAT Result;
Result = 0;
for (Scan = term_SupertermList(Term); !list_Empty(Scan); Scan = list_Cdr(Scan))
Result += sharing_GetNumberOfOccurances(list_Car(Scan));
return Result;
}
}
NAT sharing_GetNumberOfInstances(TERM Term, SHARED_INDEX Index)
/**************************************************************
INPUT: A (!) shared term and a shared index. The term has to be
part of the index.
RETURNS: How many literals within the index contain an instance of <Term>.
Note that literals containing <n> instances of <Term>
are counted <n> times.
***************************************************************/
{
NAT Result;
TERM Instance;
Result = 0;
Instance = st_ExistInstance(cont_LeftContext(), sharing_Index(Index), Term);
while (Instance != NULL) {
Result += sharing_GetNumberOfOccurances(Instance);
Instance = st_NextCandidate();
}
return Result;
}
/**************************************************************/
/* Output functions for the sharing structure. */
/**************************************************************/
void sharing_PrintVartable(SHARED_INDEX SharedIndex)
/**********************************************************
INPUT: A shared index.
RETURNS: Nothing.
EFFECT: Prints the Vartable entries to stdout
**********************************************************/
{
int i;
for (i = 0; i < symbol_MaxVars(); i++){
if (sharing_VartableEntry(SharedIndex, i) != NULL){
printf("\n X%d : ", i);
term_Print(sharing_VartableEntry(SharedIndex, i));
}
}
}
void sharing_PrintConsttable(SHARED_INDEX SharedIndex)
/**********************************************************
INPUT: A shared index.
RETURNS: Nothing.
EFFECT: Prints the Consttable entries to stdout
**********************************************************/
{
int i;
for (i = 0; i < symbol_MaxConsts(); i++){
if (sharing_ConsttableEntry(SharedIndex, i) != NULL){
printf("\n c%d : ", i);
term_Print(sharing_ConsttableEntry(SharedIndex, i));
}
}
}
void sharing_PrintSharingConstterms1(SHARED_INDEX SharedIndex)
/**********************************************************
INPUT: A shared index.
RETURNS: Nothing.
EFFECT: Prints all terms from the Consttable and their
direct superterms to stdout
**********************************************************/
{
TERM term;
int i;
for (i = 0; i < symbol_MaxConsts(); i++) {
term = sharing_ConsttableEntry(SharedIndex, i);
if (term != NULL){
printf("\n c%d : ", i);
term_Print(term);
puts(" has the direct superterms : ");
term_TermListPrint(term_SupertermList(term));
}
}
}
void sharing_PrintSharingVarterms1(SHARED_INDEX SharedIndex)
/**********************************************************
INPUT: A shared index.
RETURNS: Nothing.
EFFECT: Prints all terms from the Vartable and their
superterms to stdout
**********************************************************/
{
TERM term;
int i;
for (i = 0; i < symbol_MaxVars(); i++) {
term = sharing_VartableEntry(SharedIndex, i);
if (term != NULL){
printf("\n x%d : ", i);
term_Print(term);
puts(" has the direct superterms : ");
term_TermListPrint(term_SupertermList(term));
}
}
}
static void sharing_PrintWithSuperterms(TERM Term)
/**********************************************************
INPUT: A Term
RETURNS: Nothing.
EFFECT: Prints all Superterms of 'Term' from the sharing
to stdout.
**********************************************************/
{
LIST HelpList;
#ifdef CHECK
if (!term_IsTerm(Term)) {
misc_StartErrorReport();
misc_ErrorReport("\n In sharing_PrintWithSuperterms: Illegal input.\n");
misc_FinishErrorReport();
}
#endif
if (term_IsAtom(Term)) {
term_Print(Term);
putchar('\n');
}
else{
term_Print(Term);
HelpList = term_SupertermList(Term);
if (!list_Empty(HelpList)){
putchar('[');
term_TermListPrint(HelpList);
puts("]");
while (!list_Empty(HelpList)){
sharing_PrintWithSuperterms(list_Car(HelpList));
HelpList = list_Cdr(HelpList);
}
}
}
}
void sharing_PrintSharing(SHARED_INDEX SharedIndex)
/**********************************************************
INPUT: A shared Index.
EFFECT: Prints the sharing to standard.out.
**********************************************************/
{
int i;
for (i = 0; i < symbol_MaxConsts(); i++){
if (sharing_ConsttableEntry(SharedIndex, i) != NULL){
sharing_PrintWithSuperterms(sharing_ConsttableEntry(SharedIndex, i));
puts("\n");
}
}
puts("------------------------");
for (i = 0; i < symbol_MaxVars(); i++){
if (sharing_VartableEntry(SharedIndex, i) != NULL){
sharing_PrintWithSuperterms(sharing_VartableEntry(SharedIndex, i));
puts("\n");
}
}
}
void sharing_PrintSameLevelTerms(TERM Term)
/**********************************************************
INPUT: A term.
RETURNS: Nothing.
EFFECT: Prints all terms, that share any of 'Term's args to
standard.out.
**********************************************************/
{
LIST HelpList;
HelpList = term_ArgumentList(Term);
while (!list_Empty(HelpList)){
if (!list_Empty(term_SupertermList(list_First(HelpList))))
term_TermListPrint(term_SupertermList(list_First(HelpList)));
HelpList = list_Cdr(HelpList);
}
}
void sharing_PrintStack(void)
/**********************************************************
INPUT: None.
RETURNS: Nothing.
EFFECT: Prints the internal sharing stack for iterative
queries. Only for debugging purposes.
**********************************************************/
{
TERM term;
LIST* ptr = sharing_STACKPOINTER;
while (ptr > sharing_STACK) {
ptr--;
term = list_Car(*ptr);
term_Print(term);
putchar('\n');
}
}
|