src/misc/espresso/set.c File Reference

#include "espresso.h"
Include dependency graph for set.c:

Go to the source code of this file.

Defines

#define largest_string   120

Functions

static int intcpy (unsigned int *d, unsigned int *s, long n)
int bit_index (unsigned int a)
int set_ord (pset a)
int set_dist (pset a, pset b)
pset set_clear (pset r, int size)
pset set_fill (pset r, int size)
pset set_copy (pset r, pset a)
pset set_and (pset r, pset a, pset b)
pset set_or (pset r, pset a, pset b)
pset set_diff (pset r, pset a, pset b)
pset set_xor (pset r, pset a, pset b)
pset set_merge (pset r, pset a, pset b, pset mask)
bool set_andp (pset r, pset a, pset b)
bool set_orp (pset r, pset a, pset b)
bool setp_empty (pset a)
bool setp_full (pset a, int size)
bool setp_equal (pset a, pset b)
bool setp_disjoint (pset a, pset b)
bool setp_implies (pset a, pset b)
pset sf_or (pset_family A)
pset sf_and (pset_family A)
pset_family sf_active (pset_family A)
pset_family sf_inactive (pset_family A)
pset_family sf_copy (pset_family R, pset_family A)
pset_family sf_join (pset_family A, pset_family B)
pset_family sf_append (pset_family A, pset_family B)
pset_family sf_new (int num, int size)
pset_family sf_save (pset_family A)
void sf_free (pset_family A)
void sf_cleanup ()
pset_family sf_addset (pset_family A, pset s)
void sf_delset (pset_family A, int i)
void sf_print (pset_family A)
void sf_bm_print (pset_family A)
void sf_write (FILE *fp, pset_family A)
pset_family sf_read (FILE *fp)
void set_write (FILE *fp, pset a)
pset_family sf_bm_read (FILE *fp)
char * ps1 (pset a)
char * pbv1 (pset s, int n)
void set_adjcnt (pset a, int *count, int weight)
int * sf_count (pset_family A)
int * sf_count_restricted (pset_family A, pset r)
pset_family sf_delc (pset_family A, int first, int last)
pset_family sf_addcol (pset_family A, int firstcol, int n)
pset_family sf_delcol (pset_family A, int firstcol, int n)
pset_family sf_copy_col (pset_family dst, int dstcol, pset_family src, int srccol)
pset_family sf_compress (pset_family A, pset c)
pset_family sf_transpose (pset_family A)
pset_family sf_permute (pset_family A, int *permute, int npermute)

Variables

static pset_family set_family_garbage = NULL
static char s1 [largest_string]

Define Documentation

#define largest_string   120

Definition at line 510 of file set.c.


Function Documentation

int bit_index ( unsigned int  a  ) 

Definition at line 31 of file set.c.

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 }

static int intcpy ( unsigned int *  d,
unsigned int *  s,
long  n 
) [static]

Definition at line 19 of file set.c.

00022 {
00023     register int i;
00024     for(i = 0; i < n; i++) {
00025         *d++ = *s++;
00026     }
00027 }

char* pbv1 ( pset  s,
int  n 
)

Definition at line 541 of file set.c.

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 }

char* ps1 ( pset  a  ) 

Definition at line 512 of file set.c.

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             /* Generate digits (reverse order) */
00526             l = 0; do temp[l++] = num % 10 + '0'; while ((num /= 10) > 0);
00527             /* Copy them back in correct order */
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 }

void set_adjcnt ( pset  a,
int *  count,
int  weight 
)

Definition at line 555 of file set.c.

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 }

pset set_and ( pset  r,
pset  a,
pset  b 
)

Definition at line 101 of file set.c.

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 }

bool set_andp ( pset  r,
pset  a,
pset  b 
)

Definition at line 150 of file set.c.

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 }

pset set_clear ( pset  r,
int  size 
)

Definition at line 68 of file set.c.

00071 {
00072     register int i = LOOPINIT(size);
00073     *r = i; do r[i] = 0; while (--i > 0);
00074     return r;
00075 }

pset set_copy ( pset  r,
pset  a 
)

Definition at line 92 of file set.c.

00094 {
00095     register int i = LOOPCOPY(a);
00096     do r[i] = a[i]; while (--i >= 0);
00097     return r;
00098 }

pset set_diff ( pset  r,
pset  a,
pset  b 
)

Definition at line 119 of file set.c.

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 }

int set_dist ( pset  a,
pset  b 
)

Definition at line 56 of file set.c.

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 }

pset set_fill ( pset  r,
int  size 
)

Definition at line 78 of file set.c.

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 }

pset set_merge ( pset  r,
pset  a,
pset  b,
pset  mask 
)

Definition at line 141 of file set.c.

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 }

pset set_or ( pset  r,
pset  a,
pset  b 
)

Definition at line 110 of file set.c.

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 }

int set_ord ( pset  a  ) 

Definition at line 44 of file set.c.

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 }

bool set_orp ( pset  r,
pset  a,
pset  b 
)

Definition at line 160 of file set.c.

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 }

void set_write ( FILE *  fp,
pset  a 
)

Definition at line 461 of file set.c.

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 }

pset set_xor ( pset  r,
pset  a,
pset  b 
)

Definition at line 128 of file set.c.

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 }

bool setp_disjoint ( pset  a,
pset  b 
)

Definition at line 205 of file set.c.

00207 {
00208     register int i = LOOP(a);
00209     do if (a[i] & b[i]) return FALSE; while (--i > 0);
00210     return TRUE;
00211 }

bool setp_empty ( pset  a  ) 

Definition at line 170 of file set.c.

00172 {
00173     register int i = LOOP(a);
00174     do if (a[i]) return FALSE; while (--i > 0);
00175     return TRUE;
00176 }

bool setp_equal ( pset  a,
pset  b 
)

Definition at line 196 of file set.c.

00198 {
00199     register int i = LOOP(a);
00200     do if (a[i] != b[i]) return FALSE; while (--i > 0);
00201     return TRUE;
00202 }

bool setp_full ( pset  a,
int  size 
)

Definition at line 179 of file set.c.

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 }

bool setp_implies ( pset  a,
pset  b 
)

Definition at line 214 of file set.c.

00216 {
00217     register int i = LOOP(a);
00218     do if (a[i] & ~b[i]) return FALSE; while (--i > 0);
00219     return TRUE;
00220 }

pset_family sf_active ( pset_family  A  ) 

Definition at line 247 of file set.c.

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 }

pset_family sf_addcol ( pset_family  A,
int  firstcol,
int  n 
)

Definition at line 648 of file set.c.

00651 {
00652     int maxsize;
00653 
00654     /* Check if adding columns at the end ... */
00655     if (firstcol == A->sf_size) {
00656         /* If so, check if there is already enough room */
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 }

pset_family sf_addset ( pset_family  A,
pset  s 
)

Definition at line 383 of file set.c.

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 }

pset sf_and ( pset_family  A  ) 

Definition at line 235 of file set.c.

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 }

pset_family sf_append ( pset_family  A,
pset_family  B 
)

Definition at line 314 of file set.c.

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 }

void sf_bm_print ( pset_family  A  ) 

Definition at line 416 of file set.c.

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 }

pset_family sf_bm_read ( FILE *  fp  ) 

Definition at line 477 of file set.c.

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 }

void sf_cleanup (  ) 

Definition at line 371 of file set.c.

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 }

pset_family sf_compress ( pset_family  A,
pset  c 
)

Definition at line 735 of file set.c.

00738 {
00739     register pset p;
00740     register int i, bcol;
00741     pset_family B;
00742 
00743     /* create a clean set family for the result */
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     /* copy each column of A which has a 1 in c */
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 }

pset_family sf_copy ( pset_family  R,
pset_family  A 
)

Definition at line 281 of file set.c.

00283 {
00284     R->sf_size = A->sf_size;
00285     R->wsize = A->wsize;
00286 /*R->capacity = A->count;*/
00287 /*R->data = REALLOC(unsigned int, R->data, (long) R->capacity * R->wsize);*/
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 }

pset_family sf_copy_col ( pset_family  dst,
int  dstcol,
pset_family  src,
int  srccol 
)

Definition at line 703 of file set.c.

00706 {
00707     register pset last, p, pdest;
00708     register int word_test, word_set;
00709     unsigned int bit_set, bit_test;
00710 
00711     /* CHEAT! form these constants outside the loop */
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  *  equivalent code for this is ...
00723  *      if (is_in_set(p, srccol)) set_insert(pdest, destcol);
00724  */
00725         pdest += dst->wsize;
00726     }
00727     return dst;
00728 }

int* sf_count ( pset_family  A  ) 

Definition at line 574 of file set.c.

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 }

int* sf_count_restricted ( pset_family  A,
pset  r 
)

Definition at line 603 of file set.c.

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     /* Loop for each set */
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 }

pset_family sf_delc ( pset_family  A,
int  first,
int  last 
)

Definition at line 636 of file set.c.

00639 {
00640     return sf_delcol(A, first, last-first + 1);
00641 }

pset_family sf_delcol ( pset_family  A,
int  firstcol,
int  n 
)

Definition at line 676 of file set.c.

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 }

void sf_delset ( pset_family  A,
int  i 
)

Definition at line 399 of file set.c.

00402 {   (void) set_copy(GETSET(A,i), GETSET(A, --A->count));}

void sf_free ( pset_family  A  ) 

Definition at line 361 of file set.c.

00363 {
00364     FREE(A->data);
00365     A->next = set_family_garbage;
00366     set_family_garbage = A;
00367 }

pset_family sf_inactive ( pset_family  A  ) 

Definition at line 260 of file set.c.

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 }

pset_family sf_join ( pset_family  A,
pset_family  B 
)

Definition at line 296 of file set.c.

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 }

pset_family sf_new ( int  num,
int  size 
)

Definition at line 332 of file set.c.

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 }

pset sf_or ( pset_family  A  ) 

Definition at line 223 of file set.c.

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 }

pset_family sf_permute ( pset_family  A,
int *  permute,
int  npermute 
)

Definition at line 798 of file set.c.

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 }

void sf_print ( pset_family  A  ) 

Definition at line 405 of file set.c.

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 }

pset_family sf_read ( FILE *  fp  ) 

Definition at line 441 of file set.c.

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 }

pset_family sf_save ( pset_family  A  ) 

Definition at line 353 of file set.c.

00355 {
00356     return sf_copy(sf_new(A->count, A->sf_size), A);
00357 }

pset_family sf_transpose ( pset_family  A  ) 

Definition at line 768 of file set.c.

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 }

void sf_write ( FILE *  fp,
pset_family  A 
)

Definition at line 428 of file set.c.

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 }


Variable Documentation

char s1[largest_string] [static]

Definition at line 511 of file set.c.

Definition at line 17 of file set.c.


Generated on Tue Jan 5 12:19:12 2010 for abc70930 by  doxygen 1.6.1