www.pudn.com > Demo C.rar > set.h


/*  
 * set.h  from the Set of Integers example,  
 * Ch. 7, C: A Reference Manual 5/e 
 * Samuel P. Harbison III and Guy L. Steele Jr. 
 * 
 * A set package, suitable for sets of small integers in the range 0 to N-1,  
 * where N is the number of bits in an unsigned int type.  
 * Each integer is represented by a bit position; bit i is 1 if and only if  
 * i is in the set. The low-order bit is bit 0.  
 */ 
#include    /* defines CHAR_BIT */ 
 
/* Type SET is used to represent sets. */ 
typedef unsigned int SET; 
 
/* SET_BITS: Maximum bits per set. */ 
#define SET_BITS             (sizeof(SET)*CHAR_BIT) 
 
/* check(i): True if i can be a set element. */ 
#define check(i)		           (((unsigned) (i)) < SET_BITS) 
 
/* emptyset: A set with no elements. */ 
#define emptyset             ((SET) 0) 
 
/* add(s,i): Add a single integer to a set. */ 
#define add(set,i)		         ((set) | singleset (i)) 
 
/* singleset(i): Return a set with one element in it. */ 
#define singleset(i)         (((SET) 1) << (i)) 
 
/* intersect: Return intersection of two sets. */ 
#define intersect(set1,set2) ((set1) & (set2)) 
 
/* union: Return the union of two sets. */ 
#define union(set1,set2)     ((set1) | (set2)) 
 
/* setdiff: Return a set of those elements in set1 or set2, 
   but not both. */ 
#define setdiff(set1,set2)   ((set1) ^ (set2)) 
 
/* element: True if i is in set. */ 
#define element(i,set)       (singleset((i)) & (set)) 
 
/* forallelements:  Perform the following statement once for 
   every element of the set s, with the variable j set to 
   that element. To print all the elements in s, just write 
   int j; 
   forallelements(j, s) 
      printf("%d ", j); 
*/  
#define forallelements(j,s) \ 
	for ((j)=0; (j)