00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "espresso.h"
00017 static pset_family set_family_garbage = NULL;
00018
00019 static int intcpy(d, s, n)
00020 register unsigned int *d, *s;
00021 register long n;
00022 {
00023 register int i;
00024 for(i = 0; i < n; i++) {
00025 *d++ = *s++;
00026 }
00027 }
00028
00029
00030
00031 int bit_index(a)
00032 register unsigned int a;
00033 {
00034 register int i;
00035 if (a == 0)
00036 return -1;
00037 for(i = 0; (a & 1) == 0; a >>= 1, i++)
00038 ;
00039 return i;
00040 }
00041
00042
00043
00044 int set_ord(a)
00045 register pset a;
00046 {
00047 register int i, sum = 0;
00048 register unsigned int val;
00049 for(i = LOOP(a); i > 0; i--)
00050 if ((val = a[i]) != 0)
00051 sum += count_ones(val);
00052 return sum;
00053 }
00054
00055
00056 int set_dist(a, b)
00057 register pset a, b;
00058 {
00059 register int i, sum = 0;
00060 register unsigned int val;
00061 for(i = LOOP(a); i > 0; i--)
00062 if ((val = a[i] & b[i]) != 0)
00063 sum += count_ones(val);
00064 return sum;
00065 }
00066
00067
00068 pset set_clear(r, size)
00069 register pset r;
00070 int size;
00071 {
00072 register int i = LOOPINIT(size);
00073 *r = i; do r[i] = 0; while (--i > 0);
00074 return r;
00075 }
00076
00077
00078 pset set_fill(r, size)
00079 register pset r;
00080 register int size;
00081 {
00082 register int i = LOOPINIT(size);
00083 *r = i;
00084 r[i] = ~ (unsigned) 0;
00085 r[i] >>= i * BPI - size;
00086 while (--i > 0)
00087 r[i] = ~ (unsigned) 0;
00088 return r;
00089 }
00090
00091
00092 pset set_copy(r, a)
00093 register pset r, a;
00094 {
00095 register int i = LOOPCOPY(a);
00096 do r[i] = a[i]; while (--i >= 0);
00097 return r;
00098 }
00099
00100
00101 pset set_and(r, a, b)
00102 register pset r, a, b;
00103 {
00104 register int i = LOOP(a);
00105 PUTLOOP(r,i); do r[i] = a[i] & b[i]; while (--i > 0);
00106 return r;
00107 }
00108
00109
00110 pset set_or(r, a, b)
00111 register pset r, a, b;
00112 {
00113 register int i = LOOP(a);
00114 PUTLOOP(r,i); do r[i] = a[i] | b[i]; while (--i > 0);
00115 return r;
00116 }
00117
00118
00119 pset set_diff(r, a, b)
00120 register pset r, a, b;
00121 {
00122 register int i = LOOP(a);
00123 PUTLOOP(r,i); do r[i] = a[i] & ~b[i]; while (--i > 0);
00124 return r;
00125 }
00126
00127
00128 pset set_xor(r, a, b)
00129 register pset r, a, b;
00130 {
00131 register int i = LOOP(a);
00132 #ifdef IBM_WATC
00133 PUTLOOP(r,i); do r[i] = (a[i]&~b[i]) | (~a[i]&b[i]); while (--i > 0);
00134 #else
00135 PUTLOOP(r,i); do r[i] = a[i] ^ b[i]; while (--i > 0);
00136 #endif
00137 return r;
00138 }
00139
00140
00141 pset set_merge(r, a, b, mask)
00142 register pset r, a, b, mask;
00143 {
00144 register int i = LOOP(a);
00145 PUTLOOP(r,i); do r[i] = (a[i]&mask[i]) | (b[i]&~mask[i]); while (--i > 0);
00146 return r;
00147 }
00148
00149
00150 bool set_andp(r, a, b)
00151 register pset r, a, b;
00152 {
00153 register int i = LOOP(a);
00154 register unsigned int x = 0;
00155 PUTLOOP(r,i); do {r[i] = a[i] & b[i]; x |= r[i];} while (--i > 0);
00156 return x != 0;
00157 }
00158
00159
00160 bool set_orp(r, a, b)
00161 register pset r, a, b;
00162 {
00163 register int i = LOOP(a);
00164 register unsigned int x = 0;
00165 PUTLOOP(r,i); do {r[i] = a[i] | b[i]; x |= r[i];} while (--i > 0);
00166 return x != 0;
00167 }
00168
00169
00170 bool setp_empty(a)
00171 register pset a;
00172 {
00173 register int i = LOOP(a);
00174 do if (a[i]) return FALSE; while (--i > 0);
00175 return TRUE;
00176 }
00177
00178
00179 bool setp_full(a, size)
00180 register pset a;
00181 register int size;
00182 {
00183 register int i = LOOP(a);
00184 register unsigned int test;
00185 test = ~ (unsigned) 0;
00186 test >>= i * BPI - size;
00187 if (a[i] != test)
00188 return FALSE;
00189 while (--i > 0)
00190 if (a[i] != (~(unsigned) 0))
00191 return FALSE;
00192 return TRUE;
00193 }
00194
00195
00196 bool setp_equal(a, b)
00197 register pset a, b;
00198 {
00199 register int i = LOOP(a);
00200 do if (a[i] != b[i]) return FALSE; while (--i > 0);
00201 return TRUE;
00202 }
00203
00204
00205 bool setp_disjoint(a, b)
00206 register pset a, b;
00207 {
00208 register int i = LOOP(a);
00209 do if (a[i] & b[i]) return FALSE; while (--i > 0);
00210 return TRUE;
00211 }
00212
00213
00214 bool setp_implies(a, b)
00215 register pset a, b;
00216 {
00217 register int i = LOOP(a);
00218 do if (a[i] & ~b[i]) return FALSE; while (--i > 0);
00219 return TRUE;
00220 }
00221
00222
00223 pset sf_or(A)
00224 pset_family A;
00225 {
00226 register pset or, last, p;
00227
00228 or = set_new(A->sf_size);
00229 foreach_set(A, last, p)
00230 INLINEset_or(or, or, p);
00231 return or;
00232 }
00233
00234
00235 pset sf_and(A)
00236 pset_family A;
00237 {
00238 register pset and, last, p;
00239
00240 and = set_fill(set_new(A->sf_size), A->sf_size);
00241 foreach_set(A, last, p)
00242 INLINEset_and(and, and, p);
00243 return and;
00244 }
00245
00246
00247 pset_family sf_active(A)
00248 pset_family A;
00249 {
00250 register pset p, last;
00251 foreach_set(A, last, p) {
00252 SET(p, ACTIVE);
00253 }
00254 A->active_count = A->count;
00255 return A;
00256 }
00257
00258
00259
00260 pset_family sf_inactive(A)
00261 pset_family A;
00262 {
00263 register pset p, last, pdest;
00264
00265 pdest = A->data;
00266 foreach_set(A, last, p) {
00267 if (TESTP(p, ACTIVE)) {
00268 if (pdest != p) {
00269 INLINEset_copy(pdest, p);
00270 }
00271 pdest += A->wsize;
00272 } else {
00273 A->count--;
00274 }
00275 }
00276 return A;
00277 }
00278
00279
00280
00281 pset_family sf_copy(R, A)
00282 pset_family R, A;
00283 {
00284 R->sf_size = A->sf_size;
00285 R->wsize = A->wsize;
00286
00287
00288 R->count = A->count;
00289 R->active_count = A->active_count;
00290 intcpy(R->data, A->data, (long) A->wsize * A->count);
00291 return R;
00292 }
00293
00294
00295
00296 pset_family sf_join(A, B)
00297 pset_family A, B;
00298 {
00299 pset_family R;
00300 long asize = A->count * A->wsize;
00301 long bsize = B->count * B->wsize;
00302
00303 if (A->sf_size != B->sf_size) fatal("sf_join: sf_size mismatch");
00304 R = sf_new(A->count + B->count, A->sf_size);
00305 R->count = A->count + B->count;
00306 R->active_count = A->active_count + B->active_count;
00307 intcpy(R->data, A->data, asize);
00308 intcpy(R->data + asize, B->data, bsize);
00309 return R;
00310 }
00311
00312
00313
00314 pset_family sf_append(A, B)
00315 pset_family A, B;
00316 {
00317 long asize = A->count * A->wsize;
00318 long bsize = B->count * B->wsize;
00319
00320 if (A->sf_size != B->sf_size) fatal("sf_append: sf_size mismatch");
00321 A->capacity = A->count + B->count;
00322 A->data = REALLOC(unsigned int, A->data, (long) A->capacity * A->wsize);
00323 intcpy(A->data + asize, B->data, bsize);
00324 A->count += B->count;
00325 A->active_count += B->active_count;
00326 sf_free(B);
00327 return A;
00328 }
00329
00330
00331
00332 pset_family sf_new(num, size)
00333 int num, size;
00334 {
00335 pset_family A;
00336 if (set_family_garbage == NULL) {
00337 A = ALLOC(set_family_t, 1);
00338 } else {
00339 A = set_family_garbage;
00340 set_family_garbage = A->next;
00341 }
00342 A->sf_size = size;
00343 A->wsize = SET_SIZE(size);
00344 A->capacity = num;
00345 A->data = ALLOC(unsigned int, (long) A->capacity * A->wsize);
00346 A->count = 0;
00347 A->active_count = 0;
00348 return A;
00349 }
00350
00351
00352
00353 pset_family sf_save(A)
00354 register pset_family A;
00355 {
00356 return sf_copy(sf_new(A->count, A->sf_size), A);
00357 }
00358
00359
00360
00361 void sf_free(A)
00362 pset_family A;
00363 {
00364 FREE(A->data);
00365 A->next = set_family_garbage;
00366 set_family_garbage = A;
00367 }
00368
00369
00370
00371 void sf_cleanup()
00372 {
00373 register pset_family p, pnext;
00374 for(p = set_family_garbage; p != (pset_family) NULL; p = pnext) {
00375 pnext = p->next;
00376 FREE(p);
00377 }
00378 set_family_garbage = (pset_family) NULL;
00379 }
00380
00381
00382
00383 pset_family sf_addset(A, s)
00384 pset_family A;
00385 pset s;
00386 {
00387 register pset p;
00388
00389 if (A->count >= A->capacity) {
00390 A->capacity = A->capacity + A->capacity/2 + 1;
00391 A->data = REALLOC(unsigned int, A->data, (long) A->capacity * A->wsize);
00392 }
00393 p = GETSET(A, A->count++);
00394 INLINEset_copy(p, s);
00395 return A;
00396 }
00397
00398
00399 void sf_delset(A, i)
00400 pset_family A;
00401 int i;
00402 { (void) set_copy(GETSET(A,i), GETSET(A, --A->count));}
00403
00404
00405 void sf_print(A)
00406 pset_family A;
00407 {
00408 char *ps1();
00409 register pset p;
00410 register int i;
00411 foreachi_set(A, i, p)
00412 printf("A[%d] = %s\n", i, ps1(p));
00413 }
00414
00415
00416 void sf_bm_print(A)
00417 pset_family A;
00418 {
00419 char *pbv1();
00420 register pset p;
00421 register int i;
00422 foreachi_set(A, i, p)
00423 printf("[%4d] %s\n", i, pbv1(p, A->sf_size));
00424 }
00425
00426
00427
00428 void sf_write(fp, A)
00429 FILE *fp;
00430 pset_family A;
00431 {
00432 register pset p, last;
00433 (void) fprintf(fp, "%d %d\n", A->count, A->sf_size);
00434 foreach_set(A, last, p)
00435 set_write(fp, p);
00436 (void) fflush(fp);
00437 }
00438
00439
00440
00441 pset_family sf_read(fp)
00442 FILE *fp;
00443 {
00444 int i, j;
00445 register pset p, last;
00446 pset_family A;
00447
00448 (void) fscanf(fp, "%d %d\n", &i, &j);
00449 A = sf_new(i, j);
00450 A->count = i;
00451 foreach_set(A, last, p) {
00452 (void) fscanf(fp, "%x", p);
00453 for(j = 1; j <= LOOP(p); j++)
00454 (void) fscanf(fp, "%x", p+j);
00455 }
00456 return A;
00457 }
00458
00459
00460
00461 void set_write(fp, a)
00462 register FILE *fp;
00463 register pset a;
00464 {
00465 register int n = LOOP(a), j;
00466
00467 for(j = 0; j <= n; j++) {
00468 (void) fprintf(fp, "%x ", a[j]);
00469 if ((j+1) % 8 == 0 && j != n)
00470 (void) fprintf(fp, "\n\t");
00471 }
00472 (void) fprintf(fp, "\n");
00473 }
00474
00475
00476
00477 pset_family sf_bm_read(fp)
00478 FILE *fp;
00479 {
00480 int i, j, rows, cols;
00481 register pset pdest;
00482 pset_family A;
00483
00484 (void) fscanf(fp, "%d %d\n", &rows, &cols);
00485 A = sf_new(rows, cols);
00486 for(i = 0; i < rows; i++) {
00487 pdest = GETSET(A, A->count++);
00488 (void) set_clear(pdest, A->sf_size);
00489 for(j = 0; j < cols; j++) {
00490 switch(getc(fp)) {
00491 case '0':
00492 break;
00493 case '1':
00494 set_insert(pdest, j);
00495 break;
00496 default:
00497 fatal("Error reading set family");
00498 }
00499 }
00500 if (getc(fp) != '\n') {
00501 fatal("Error reading set family (at end of line)");
00502 }
00503 }
00504 return A;
00505 }
00506
00507
00508
00509
00510 #define largest_string 120
00511 static char s1[largest_string];
00512 char *ps1(a)
00513 register pset a;
00514 {
00515 register int i, num, l, len = 0, n = NELEM(a);
00516 char temp[20];
00517 bool first = TRUE;
00518
00519 s1[len++] = '[';
00520 for(i = 0; i < n; i++)
00521 if (is_in_set(a,i)) {
00522 if (! first)
00523 s1[len++] = ',';
00524 first = FALSE; num = i;
00525
00526 l = 0; do temp[l++] = num % 10 + '0'; while ((num /= 10) > 0);
00527
00528 do s1[len++] = temp[--l]; while (l > 0);
00529 if (len > largest_string-15) {
00530 s1[len++] = '.'; s1[len++] = '.'; s1[len++] = '.';
00531 break;
00532 }
00533 }
00534
00535 s1[len++] = ']';
00536 s1[len++] = '\0';
00537 return s1;
00538 }
00539
00540
00541 char *pbv1(s, n)
00542 pset s;
00543 int n;
00544 {
00545 register int i;
00546 for(i = 0; i < n; i++)
00547 s1[i] = is_in_set(s,i) ? '1' : '0';
00548 s1[n] = '\0';
00549 return s1;
00550 }
00551
00552
00553
00554 void
00555 set_adjcnt(a, count, weight)
00556 register pset a;
00557 register int *count, weight;
00558 {
00559 register int i, base;
00560 register unsigned int val;
00561
00562 for(i = LOOP(a); i > 0; ) {
00563 for(val = a[i], base = --i << LOGBPI; val != 0; base++, val >>= 1) {
00564 if (val & 1) {
00565 count[base] += weight;
00566 }
00567 }
00568 }
00569 }
00570
00571
00572
00573
00574 int *sf_count(A)
00575 pset_family A;
00576 {
00577 register pset p, last;
00578 register int i, base, *count;
00579 register unsigned int val;
00580
00581 count = ALLOC(int, A->sf_size);
00582 for(i = A->sf_size - 1; i >= 0; i--) {
00583 count[i] = 0;
00584 }
00585
00586 foreach_set(A, last, p) {
00587 for(i = LOOP(p); i > 0; ) {
00588 for(val = p[i], base = --i << LOGBPI; val != 0; base++, val >>= 1) {
00589 if (val & 1) {
00590 count[base]++;
00591 }
00592 }
00593 }
00594 }
00595 return count;
00596 }
00597
00598
00599
00600
00601
00602
00603 int *sf_count_restricted(A, r)
00604 pset_family A;
00605 register pset r;
00606 {
00607 register pset p;
00608 register int i, base, *count;
00609 register unsigned int val;
00610 int weight;
00611 pset last;
00612
00613 count = ALLOC(int, A->sf_size);
00614 for(i = A->sf_size - 1; i >= 0; i--) {
00615 count[i] = 0;
00616 }
00617
00618
00619 foreach_set(A, last, p) {
00620 weight = 1024 / (set_ord(p) - 1);
00621 for(i = LOOP(p); i > 0; ) {
00622 for(val=p[i]&r[i], base= --i<<LOGBPI; val!=0; base++, val >>= 1) {
00623 if (val & 1) {
00624 count[base] += weight;
00625 }
00626 }
00627 }
00628 }
00629 return count;
00630 }
00631
00632
00633
00634
00635
00636 pset_family sf_delc(A, first, last)
00637 pset_family A;
00638 int first, last;
00639 {
00640 return sf_delcol(A, first, last-first + 1);
00641 }
00642
00643
00644
00645
00646
00647
00648 pset_family sf_addcol(A, firstcol, n)
00649 pset_family A;
00650 int firstcol, n;
00651 {
00652 int maxsize;
00653
00654
00655 if (firstcol == A->sf_size) {
00656
00657 maxsize = BPI * LOOPINIT(A->sf_size);
00658 if ((A->sf_size + n) <= maxsize) {
00659 A->sf_size += n;
00660 return A;
00661 }
00662 }
00663 return sf_delcol(A, firstcol, -n);
00664 }
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676 pset_family sf_delcol(A, firstcol, n)
00677 pset_family A;
00678 register int firstcol, n;
00679 {
00680 register pset p, last, pdest;
00681 register int i;
00682 pset_family B;
00683
00684 B = sf_new(A->count, A->sf_size - n);
00685 foreach_set(A, last, p) {
00686 pdest = GETSET(B, B->count++);
00687 INLINEset_clear(pdest, B->sf_size);
00688 for(i = 0; i < firstcol; i++)
00689 if (is_in_set(p, i))
00690 set_insert(pdest, i);
00691 for(i = n > 0 ? firstcol + n : firstcol; i < A->sf_size; i++)
00692 if (is_in_set(p, i))
00693 set_insert(pdest, i - n);
00694 }
00695 sf_free(A);
00696 return B;
00697 }
00698
00699
00700
00701
00702
00703 pset_family sf_copy_col(dst, dstcol, src, srccol)
00704 pset_family dst, src;
00705 int dstcol, srccol;
00706 {
00707 register pset last, p, pdest;
00708 register int word_test, word_set;
00709 unsigned int bit_set, bit_test;
00710
00711
00712 word_test = WHICH_WORD(srccol);
00713 bit_test = 1 << WHICH_BIT(srccol);
00714 word_set = WHICH_WORD(dstcol);
00715 bit_set = 1 << WHICH_BIT(dstcol);
00716
00717 pdest = dst->data;
00718 foreach_set(src, last, p) {
00719 if ((p[word_test] & bit_test) != 0)
00720 pdest[word_set] |= bit_set;
00721
00722
00723
00724
00725 pdest += dst->wsize;
00726 }
00727 return dst;
00728 }
00729
00730
00731
00732
00733
00734
00735 pset_family sf_compress(A, c)
00736 pset_family A;
00737 register pset c;
00738 {
00739 register pset p;
00740 register int i, bcol;
00741 pset_family B;
00742
00743
00744 B = sf_new(A->count, set_ord(c));
00745 for(i = 0; i < A->count; i++) {
00746 p = GETSET(B, B->count++);
00747 INLINEset_clear(p, B->sf_size);
00748 }
00749
00750
00751 bcol = 0;
00752 for(i = 0; i < A->sf_size; i++) {
00753 if (is_in_set(c, i)) {
00754 (void) sf_copy_col(B, bcol++, A, i);
00755 }
00756 }
00757 sf_free(A);
00758 return B;
00759 }
00760
00761
00762
00763
00764
00765
00766
00767
00768 pset_family sf_transpose(A)
00769 pset_family A;
00770 {
00771 pset_family B;
00772 register pset p;
00773 register int i, j;
00774
00775 B = sf_new(A->sf_size, A->count);
00776 B->count = A->sf_size;
00777 foreachi_set(B, i, p) {
00778 INLINEset_clear(p, B->sf_size);
00779 }
00780 foreachi_set(A, i, p) {
00781 for(j = 0; j < A->sf_size; j++) {
00782 if (is_in_set(p, j)) {
00783 set_insert(GETSET(B, j), i);
00784 }
00785 }
00786 }
00787 sf_free(A);
00788 return B;
00789 }
00790
00791
00792
00793
00794
00795
00796
00797
00798 pset_family sf_permute(A, permute, npermute)
00799 pset_family A;
00800 register int *permute, npermute;
00801 {
00802 pset_family B;
00803 register pset p, last, pdest;
00804 register int j;
00805
00806 B = sf_new(A->count, npermute);
00807 B->count = A->count;
00808 foreach_set(B, last, p)
00809 INLINEset_clear(p, npermute);
00810
00811 pdest = B->data;
00812 foreach_set(A, last, p) {
00813 for(j = 0; j < npermute; j++)
00814 if (is_in_set(p, permute[j]))
00815 set_insert(pdest, j);
00816 pdest += B->wsize;
00817 }
00818 sf_free(A);
00819 return B;
00820 }