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
|
/*-----------------------------------------------------------------------
File : ccl_freqvectors.c
Author: Stephan Schulz
Contents
Algorithms for frequency count vectors.
Copyright 1998-2018 by the author.
This code is released under the GNU General Public Licence and
the GNU Lesser General Public License.
See the file COPYING in the main E directory for details..
Run "eprover -h" for contact information.
Changes
Createrd: Tue Jul 8 21:50:44 CEST 2003
-----------------------------------------------------------------------*/
#include "ccl_freqvectors.h"
/*---------------------------------------------------------------------*/
/* Global Variables */
/*---------------------------------------------------------------------*/
PERF_CTR_DEFINE(FreqVecTimer);
/*---------------------------------------------------------------------*/
/* Forward Declarations */
/*---------------------------------------------------------------------*/
/*---------------------------------------------------------------------*/
/* Internal Functions */
/*---------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
//
// Function: tuple_3_compare_23lex()
//
// Compare 2 tuple-2 cells lexicographically, with diff more
// significant than value, which is more significant than pos.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
int tuple3_compare_23lex(const void* tuple1, const void* tuple2)
{
const Tuple3Cell *t1 = (const Tuple3Cell *)tuple1;
const Tuple3Cell *t2 = (const Tuple3Cell *)tuple2;
if(t1->diff < t2->diff)
{
return -1;
}
if(t1->diff > t2->diff)
{
return 1;
}
if(t1->value < t2->value)
{
return -1;
}
if(t1->value > t2->value)
{
return 1;
}
if(t1->pos > t2->pos)
{
return -1;
}
if(t1->pos < t2->pos)
{
return 1;
}
return 0;
}
/*-----------------------------------------------------------------------
//
// Function: gather_feature_vec()
//
// Gather a feature from a full feature vector according to
// cspec.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
static void gather_feature_vec(FVCollect_p cspec, long* full_vec,
FreqVector_p vec, long findex)
{
long resindex = -1, base = 0, offset = 0, mod = 0;
if(findex < cspec->ass_vec_len)
{
resindex = cspec->assembly_vector[findex];
}
else
{
switch(findex%4)
{
case 0:
base = cspec->pos_count_base;
offset = cspec->pos_count_offset;
mod = cspec->pos_count_mod;
break;
case 1:
base = cspec->pos_depth_base;
offset = cspec->pos_depth_offset;
mod = cspec->pos_depth_mod;
break;
case 2:
base = cspec->neg_count_base;
offset = cspec->neg_count_offset;
mod = cspec->neg_count_mod;
break;
case 3:
base = cspec->neg_depth_base;
offset = cspec->neg_depth_offset;
mod = cspec->neg_depth_mod;
break;
default:
assert(false);
break;
}
if(mod)
{
resindex = base+(offset+(findex/4))%mod;
}
}
if(resindex != -1)
{
vec->array[resindex]+=full_vec[findex];
}
}
/*---------------------------------------------------------------------*/
/* Exported Functions */
/*---------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
//
// Function: PermVectorComputeInternal()
//
// Find a "good" permutation (and selection) vector for
// FVIndexing by:
// - Ordering features from lesser to higher informativity
// - Selecting the best max_len features
// - Optionally drop features that have no projected informational
// value.
//
// Global Variables: -
//
// Side Effects : Memory operations
//
/----------------------------------------------------------------------*/
PermVector_p PermVectorComputeInternal(FreqVector_p fmax, FreqVector_p fmin,
FreqVector_p fsum,
long max_len,
bool eliminate_uninformative)
{
Tuple3Cell *array;
long i, size, start=0, start1=0, diff;
PermVector_p handle;
assert(fsum->size == fmax->size);
assert(fsum->size == fmin->size);
array = SizeMalloc(fsum->size * sizeof(Tuple3Cell));
for(i=0; i<fsum->size; i++)
{
array[i].pos = i;
diff = fmax->array[i]-fmin->array[i];
array[i].diff = diff;
array[i].value = fsum->array[i];
}
qsort(array, fsum->size, sizeof(Tuple3Cell), tuple3_compare_23lex);
if(fsum->size > max_len)
{
start = fsum->size - max_len;
}
if(eliminate_uninformative)
{
for(i=0; i<fsum->size && !array[i].diff; i++)
{
/* Intentionally empty */
};
start1 = i;
}
start = MAX(start, start1);
if(start == fsum->size)
{
start--;
}
size = fsum->size - start;
handle = PermVectorAlloc(size);
for(i=0; i < size; i++)
{
handle->array[i] = array[i+start].pos;
}
SizeFree(array, fsum->size * sizeof(Tuple3Cell));
/* PermVectorPrint(GlobalOut, handle); */
return handle;
}
/*-----------------------------------------------------------------------
//
// Function: FreqVectorAlloc()
//
// Allocate a frequency vector that can hold up to sig_start
// non function symbol count features and sig_count function symbol
// counts (in both positive and negative variety).
//
// Global Variables: -
//
// Side Effects : Memory operations.
//
/----------------------------------------------------------------------*/
FreqVector_p FreqVectorAlloc(long size)
{
FreqVector_p handle = FreqVectorCellAlloc();
/* printf("Size: %ld\n", size); */
handle->size = size;
handle->array = SizeMalloc(sizeof(long)*handle->size);
FreqVectorInitialize(handle, 0);
handle->clause = NULL;
return handle;
}
/*-----------------------------------------------------------------------
//
// Function: FreqVectorFree()
//
// Free a frequency vector.
//
// Global Variables: -
//
// Side Effects : Memory operations
//
/----------------------------------------------------------------------*/
void FreqVectorFreeReal(FreqVector_p junk)
{
assert(junk);
if(junk->array)
{
SizeFree(junk->array, sizeof(long)*junk->size);
#ifndef NDEBUG
junk->array = NULL;
junk->clause = NULL;
#endif
}
FreqVectorCellFree(junk);
}
/*-----------------------------------------------------------------------
//
// Function: FreqVectorInitialize()
//
// Store value in all fields of vec.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
void FreqVectorInitialize(FreqVector_p vec, long value)
{
long i;
for(i=0; i<vec->size;i++)
{
vec->array[i] = value;
}
}
/*-----------------------------------------------------------------------
//
// Function: FreqVectorPrint()
//
// Print a frequency vector.
//
// Global Variables: -
//
// Side Effects : Output
//
/----------------------------------------------------------------------*/
void FreqVectorPrint(FILE* out, FreqVector_p vec)
{
long i;
assert(vec);
if(vec->clause)
{
fprintf(out, "# FV for: ");
ClausePrint(out, vec->clause, true);
fprintf(out, "\n");
}
else
{
fprintf(out, "# FV, no clause given.\n");
}
fprintf(out, "# FV(len=%ld):", vec->size);
for(i=0; i<vec->size; i++)
{
fprintf(out, " %ld", vec->array[i]);
}
fprintf(out, "\n");
}
/*-----------------------------------------------------------------------
//
// Function: VarFreqVectorAddVals()
//
// Add values for up to symbol type features to the freq vector.
//
// Global Variables: -
//
// Side Effects : Memory operations
//
/----------------------------------------------------------------------*/
void VarFreqVectorAddVals(FreqVector_p vec, long symbols, FVIndexType features,
Clause_p clause)
{
long *unused, *pfreqstart, *nfreqstart, *pdepthstart, *ndepthstart;
long unused_size = 0;
Eqn_p handle;
assert(clause);
assert((features == FVIACFeatures) ||
(features == FVISSFeatures) ||
(features == FVIAllFeatures));
assert(vec);
assert(vec->size == FVSize(symbols, features));
switch(features)
{
case FVIACFeatures:
vec->array[0] += clause->pos_lit_no;
vec->array[1] += clause->neg_lit_no;
unused_size = symbols+1;
unused = SizeMalloc(sizeof(long)*unused_size);
pdepthstart = ndepthstart = unused;
nfreqstart = &(vec->array[FV_CLAUSE_FEATURES]);
pfreqstart = &(vec->array[FV_CLAUSE_FEATURES+1*(symbols+1)]);
break;
case FVISSFeatures:
unused_size = symbols+1;
unused = SizeMalloc(sizeof(long)*unused_size);
pfreqstart = nfreqstart = unused;
ndepthstart = &(vec->array[0]);
pdepthstart = &(vec->array[0+1*(symbols+1)]);
break;
case FVIAllFeatures:
vec->array[0] += clause->pos_lit_no;
vec->array[1] += clause->neg_lit_no;
unused = NULL;
nfreqstart = &(vec->array[FV_CLAUSE_FEATURES]);
pfreqstart = &(vec->array[FV_CLAUSE_FEATURES+1*(symbols+1)]);
pdepthstart = &(vec->array[FV_CLAUSE_FEATURES+2*(symbols+1)]);
ndepthstart = &(vec->array[FV_CLAUSE_FEATURES+3*(symbols+1)]);
break;
default:
assert(features == FVINoFeatures);
assert(false);
return; /* Cheapest way to fix compiler warning */
}
if(unused)
{ /* Stiffle insure warnings - we don't use unused (duh!), but
insure does not know that */
long i;
for(i=0; i<unused_size; i++)
{
unused[i] = 0;
}
}
for(handle = clause->literals; handle; handle = handle->next)
{
if(EqnIsPositive(handle))
{
EqnAddSymbolFeaturesLimited(handle,
pfreqstart,
pdepthstart,
symbols);
}
else
{
EqnAddSymbolFeaturesLimited(handle,
nfreqstart,
ndepthstart,
symbols);
}
}
if(unused)
{
SizeFree(unused, sizeof(long)*unused_size);
}
}
/*-----------------------------------------------------------------------
//
// Function: VarFreqVectorCompute()
//
// Allocate and return a frequency vector for clause based on the
// other supplied parameters.
//
// Global Variables: -
//
// Side Effects : Memory allocation.
//
/----------------------------------------------------------------------*/
FreqVector_p VarFreqVectorCompute(Clause_p clause, FVCollect_p cspec)
{
long size;
FreqVector_p vec;
assert(clause);
assert((cspec->features == FVIACFeatures) ||
(cspec->features == FVISSFeatures) ||
(cspec->features == FVIAllFeatures) ||
(cspec->features == FVICollectFeatures));
if(cspec->features == FVICollectFeatures)
{
vec = FVCollectFreqVectorCompute(clause, cspec);
}
else
{
size = FVSize(cspec->max_symbols, cspec->features);
vec = FreqVectorAlloc(size);
vec->clause = clause;
FreqVectorInitialize(vec, 0);
VarFreqVectorAddVals(vec, cspec->max_symbols,
cspec->features, clause);
}
return vec;
}
/*-----------------------------------------------------------------------
//
// Function: OptimizedVarFreqVectorCompute()
//
// Compute an "optimized" frequency count vector, based on a given
// permutation vector. If no permutation vector is given, return a
// VarFreqVector.
//
// Global Variables: -
//
// Side Effects : Memory operations
//
/----------------------------------------------------------------------*/
FreqVector_p OptimizedVarFreqVectorCompute(Clause_p clause,
PermVector_p perm,
FVCollect_p cspec)
{
FreqVector_p vec, res;
assert((cspec->features == FVIACFeatures) ||
(cspec->features == FVISSFeatures) ||
(cspec->features == FVIAllFeatures) ||
(cspec->features == FVICollectFeatures));
PERF_CTR_ENTRY(FreqVecTimer);
/* printf("Symbols used: %ld\n", sig_symbols); */
vec = VarFreqVectorCompute(clause, cspec);
/* FreqVectorPrint(stderr, vec); */
if(perm)
{
long i;
res = FreqVectorAlloc(perm->size);
for(i=0; i<perm->size; i++)
{
assert(perm->array[i]>=0);
assert(perm->array[i]<vec->size);
res->array[i] = vec->array[perm->array[i]];
}
res->clause = clause;
FreqVectorFree(vec);
PERF_CTR_EXIT(FreqVecTimer);
return res;
}
PERF_CTR_EXIT(FreqVecTimer);
return vec;
}
/*-----------------------------------------------------------------------
//
// Function: FVCollectInit()
//
// Initialize an FVCollectCell.
//
// Global Variables: -
//
// Side Effects : Memory operations
//
/----------------------------------------------------------------------*/
void FVCollectInit(FVCollect_p handle,
FVIndexType features,
bool use_litcount,
long ass_vec_len,
long res_vec_len,
long pos_count_base,
long pos_count_offset,
long pos_count_mod,
long neg_count_base,
long neg_count_offset,
long neg_count_mod,
long pos_depth_base,
long pos_depth_offset,
long pos_depth_mod,
long neg_depth_base,
long neg_depth_offset,
long neg_depth_mod)
{
long i;
handle->features = features;
handle->use_litcount = use_litcount;
handle->ass_vec_len = ass_vec_len;
handle->res_vec_len = res_vec_len;
handle->assembly_vector = SizeMalloc(sizeof(long)*ass_vec_len);
for(i=0; i< ass_vec_len; i++)
{
handle->assembly_vector[i] = -1;
}
handle->pos_count_base = pos_count_base;
handle->pos_count_offset = pos_count_offset;
handle->pos_count_mod = pos_count_mod;
handle->neg_count_base = neg_count_base;
handle->neg_count_offset = neg_count_offset;
handle->neg_count_mod = neg_count_mod;
handle->pos_depth_base = pos_depth_base;
handle->pos_depth_offset = pos_depth_offset;
handle->pos_depth_mod = pos_depth_mod;
handle->neg_depth_base = neg_depth_base;
handle->neg_depth_offset = neg_depth_offset;
handle->neg_depth_mod = neg_depth_mod;
handle->max_symbols = FVINDEX_MAX_FEATURES_DEFAULT;
}
/*-----------------------------------------------------------------------
//
// Function: FVCollectAlloc()
//
// Allocate an initialized FVCollectCell.
//
// Global Variables: -
//
// Side Effects : Memory operations
//
/----------------------------------------------------------------------*/
FVCollect_p FVCollectAlloc(FVIndexType features,
bool use_litcount,
long ass_vec_len,
long res_vec_len,
long pos_count_base,
long pos_count_offset,
long pos_count_mod,
long neg_count_base,
long neg_count_offset,
long neg_count_mod,
long pos_depth_base,
long pos_depth_offset,
long pos_depth_mod,
long neg_depth_base,
long neg_depth_offset,
long neg_depth_mod)
{
FVCollect_p handle = FVCollectCellAlloc();
FVCollectInit(handle,
features,
use_litcount,
ass_vec_len,
res_vec_len,
pos_count_base,
pos_count_offset,
pos_count_mod,
neg_count_base,
neg_count_offset,
neg_count_mod,
pos_depth_base,
pos_depth_offset,
pos_depth_mod,
neg_depth_base,
neg_depth_offset,
neg_depth_mod);
return handle;
}
/*-----------------------------------------------------------------------
//
// Function: FVCollectFree()
//
// Free a FVCollectCell.
//
// Global Variables: -
//
// Side Effects : Memory operations
//
/----------------------------------------------------------------------*/
void FVCollectFree(FVCollect_p junk)
{
SizeFree(junk->assembly_vector,sizeof(long)*junk->ass_vec_len);
FVCollectCellFree(junk);
}
/*-----------------------------------------------------------------------
//
// Function: FVCollectFreqVectorCompute()
//
// Compute a Feature Vector for the clause based on cspec.
//
// Global Variables:
//
// Side Effects :
//
/----------------------------------------------------------------------*/
FreqVector_p FVCollectFreqVectorCompute(Clause_p clause, FVCollect_p cspec)
{
static size_t full_vec_len = 0;
static long* full_vec = NULL;
FreqVector_p vec = FreqVectorAlloc(cspec->res_vec_len);
vec->clause = clause;
FreqVectorInitialize(vec, 0);
if(!ClauseIsEmpty(clause))
{
PStack_p mod_stack = PStackAlloc();
long max_fun = clause->literals->bank->sig->f_count;
long findex;
if(cspec->use_litcount)
{
vec->array[0] = clause->pos_lit_no;
vec->array[1] = clause->neg_lit_no;
}
full_vec = RegMemProvide(full_vec, &full_vec_len, sizeof(long)*(max_fun+1)*4);
ClauseAddSymbolFeatures(clause, mod_stack, full_vec);
while(!PStackEmpty(mod_stack))
{
findex = PStackPopInt(mod_stack);
gather_feature_vec(cspec, full_vec, vec, findex);
full_vec[findex] = 0;
gather_feature_vec(cspec, full_vec, vec, findex+1);
full_vec[findex+1] = 0;
}
PStackFree(mod_stack);
}
return vec;
}
/*-----------------------------------------------------------------------
//
// Function: BillFeaturesCollectAlloc()
//
// Generate a CollectSpec as follows
// - positive literals
// - negative literals
// foreach relation symbol
// positive occurrences
// negative occurrences
// foreach function symbol
// positive occurrences
// negative occurrences
// positive maxdepth
// negative maxdepth
//
// Global Variables: -
//
// Side Effects : Memory operations
//
/----------------------------------------------------------------------*/
FVCollect_p BillFeaturesCollectAlloc(Sig_p sig, long len)
{
long p_no = SigCountSymbols(sig, true);
long f_no = SigCountSymbols(sig, false);
FunCode i, pos;
FVCollect_p cspec;
assert(len>2);
while((2+2*p_no+4*f_no) > len)
{
if(p_no > f_no)
{
p_no--;
}
else
{
f_no--;
}
}
cspec = FVCollectAlloc(FVICollectFeatures,
true,
(sig->f_count+1)*4+2,
len,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0);
pos = 2;
for(i=sig->internal_symbols+1; p_no; i++)
{
/* printf("p = %ld (%s)\n", i, SigFindName(sig,i)); */
if(!SigIsSpecial(sig, i) && SigIsPredicate(sig, i))
{
cspec->assembly_vector[4*i] = pos;
pos++;
cspec->assembly_vector[4*i+1] = pos;
pos++;
p_no--;
}
}
for(i=sig->internal_symbols+1; f_no; i++)
{
/* printf("f = %ld (%s)\n", i, SigFindName(sig,i)); */
if(!SigIsSpecial(sig, i) && SigIsFunction(sig,i))
{
cspec->assembly_vector[4*i] = pos;
pos++;
cspec->assembly_vector[4*i+1] = pos;
pos++;
cspec->assembly_vector[4*i+2] = pos;
pos++;
cspec->assembly_vector[4*i+3] = pos;
pos++;
f_no--;
}
}
return cspec;
}
/*-----------------------------------------------------------------------
//
// Function: BillPlusFeaturesCollectAlloc()
//
// Generate a CollectSpec as follows
// - positive literals
// - negative literals
// foreach relation symbol
// positive occurrences
// negative occurrences
// foreach function symbol
// positive occurrences
// negative occurrences
// positive maxdepth
// negative maxdepth
// All overflow counts
// All overflow depths
//
// Global Variables: -
//
// Side Effects : Memory operations
//
/----------------------------------------------------------------------*/
FVCollect_p BillPlusFeaturesCollectAlloc(Sig_p sig, long len)
{
long p_no = SigCountSymbols(sig, true);
long f_no = SigCountSymbols(sig, false);
FunCode i, pos;
FVCollect_p cspec;
assert(len>2);
while((6+2*p_no+4*f_no) > len)
{
if(p_no > f_no)
{
p_no--;
}
else
{
f_no--;
}
}
cspec = FVCollectAlloc(FVICollectFeatures,
true,
(sig->f_count+1)*4+2,
len,
len-4, 0, 1,
len-3, 0, 1,
len-2, 0, 1,
len-1, 0, 1);
pos = 2;
for(i=sig->internal_symbols+1; p_no; i++)
{
/* printf("p = %ld (%s)\n", i, SigFindName(sig,i)); */
if(!SigIsSpecial(sig, i) && SigIsPredicate(sig, i))
{
cspec->assembly_vector[4*i] = pos;
pos++;
cspec->assembly_vector[4*i+1] = pos;
pos++;
p_no--;
}
}
for(i=sig->internal_symbols+1; f_no; i++)
{
/* printf("f = %ld (%s)\n", i, SigFindName(sig,i)); */
if(!SigIsSpecial(sig, i) && SigIsFunction(sig,i))
{
cspec->assembly_vector[4*i] = pos;
pos++;
cspec->assembly_vector[4*i+1] = pos;
pos++;
cspec->assembly_vector[4*i+2] = pos;
pos++;
cspec->assembly_vector[4*i+3] = pos;
pos++;
f_no--;
}
}
return cspec;
}
/*-----------------------------------------------------------------------
//
// Function: FVPackClause()
//
// If index is an index, compute and return a StandardFreqVector for
// clause, otherwise pack clause into a dummy frequency vector cell
// and return than.
//
// Global Variables: -
//
// Side Effects : Memory operations
//
/----------------------------------------------------------------------*/
FVPackedClause_p FVPackClause(Clause_p clause, PermVector_p perm,
FVCollect_p cspec)
{
FVPackedClause_p res;
if(cspec && (cspec->features != FVINoFeatures))
{
return OptimizedVarFreqVectorCompute(clause, perm, cspec);
}
res = FreqVectorCellAlloc();
res->array = NULL;
res->clause = clause;
return res;
}
/*-----------------------------------------------------------------------
//
// Function: FVUnpackClause()
//
// Unpack a packed clause, i.e. return the clause and throw away the
// container.
//
// Global Variables: -
//
// Side Effects : Memory operations
//
/----------------------------------------------------------------------*/
Clause_p FVUnpackClause(FVPackedClause_p pack)
{
Clause_p res = pack->clause;
#ifndef NDEBUG
pack->clause = NULL;
#endif
FreqVectorFree(pack);
return res;
}
/*-----------------------------------------------------------------------
//
// Function: FVPackedClauseFreeReal()
//
// Fully free a packed clause.
//
// Global Variables:
//
// Side Effects :
//
/----------------------------------------------------------------------*/
void FVPackedClauseFreeReal(FVPackedClause_p pack)
{
if(pack->clause)
{
ClauseFree(pack->clause);
}
FreqVectorFree(pack);
}
/*-----------------------------------------------------------------------
//
// Function: FreqVectorAdd()
//
// Component-wise addition of both sources. Guaranteed to work if
// dest is a source (but not maximally efficient - who cares). Yes,
// it's worth mentioning it ;-)
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
void FreqVectorAdd(FreqVector_p dest, FreqVector_p s1, FreqVector_p s2)
{
long i;
assert(s1 && s2 && dest);
assert(s1->size == dest->size);
assert(s2->size == dest->size);
for(i=0; i<dest->size; i++)
{
dest->array[i] = s1->array[i]+s2->array[i];
}
}
/*-----------------------------------------------------------------------
//
// Function: FreqVectorMulAdd()
//
// Component-wise addition of both weighted sources. Guaranteed to
// work if dest is a source (but not maximally efficient - who
// cares). Yes, it's worth mentioning it ;-)
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
void FreqVectorMulAdd(FreqVector_p dest, FreqVector_p s1, long f1,
FreqVector_p s2, long f2)
{
long i;
assert(s1 && s2 && dest);
assert(s1->size == dest->size);
assert(s2->size == dest->size);
for(i=0; i<dest->size; i++)
{
dest->array[i] = f1*s1->array[i]+f2*s2->array[i];
}
}
/*-----------------------------------------------------------------------
//
// Function: FreqVectorMax()
//
// Compute componentwise max of vectors. See above.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
void FreqVectorMax(FreqVector_p dest, FreqVector_p s1, FreqVector_p s2)
{
long i;
assert(s1 && s2 && dest);
assert(s1->size == dest->size);
assert(s2->size == dest->size);
for(i=0; i<dest->size; i++)
{
dest->array[i] = MAX(s1->array[i],s2->array[i]);
}
}
/*-----------------------------------------------------------------------
//
// Function: FreqVectorMin()
//
// Compute componentwise min of vectors. See above.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
void FreqVectorMin(FreqVector_p dest, FreqVector_p s1, FreqVector_p s2)
{
long i;
assert(s1 && s2 && dest);
assert(s1->size == dest->size);
assert(s2->size == dest->size);
for(i=0; i<dest->size; i++)
{
dest->array[i] = MIN(s1->array[i],s2->array[i]);
}
}
/*---------------------------------------------------------------------*/
/* End of File */
/*---------------------------------------------------------------------*/
|