src/var_set/var_set.c File Reference

#include "util.h"
#include "var_set.h"
Include dependency graph for var_set.c:

Go to the source code of this file.

Functions

var_set_tvar_set_new (int size)
var_set_tvar_set_copy (var_set_t *set)
var_set_tvar_set_assign (var_set_t *result, var_set_t *set)
void var_set_free (var_set_t *set)
static void init_size_array (void)
int var_set_n_elts (var_set_t *set)
var_set_tvar_set_or (var_set_t *result, var_set_t *a, var_set_t *b)
var_set_tvar_set_and (var_set_t *result, var_set_t *a, var_set_t *b)
var_set_tvar_set_not (var_set_t *result, var_set_t *a)
int var_set_get_elt (var_set_t *set, int index)
void var_set_set_elt (var_set_t *set, int index)
void var_set_clear_elt (var_set_t *set, int index)
void var_set_clear (var_set_t *set)
int var_set_intersect (var_set_t *a, var_set_t *b)
int var_set_is_empty (var_set_t *a)
int var_set_is_full (var_set_t *a)
void var_set_print (FILE *fp, var_set_t *set)
int var_set_equal (var_set_t *a, var_set_t *b)
int var_set_cmp (char *obj1, char *obj2)
unsigned int var_set_hash (var_set_t *set)

Variables

static int size_array [256]

Function Documentation

static void init_size_array ( void   )  [static]

Definition at line 51 of file var_set.c.

00052 {
00053   int i;
00054   unsigned j;
00055   int count;
00056 
00057   for (i = 0; i < 256; i++) {
00058     count = 0;
00059     for (j = 0; j < VAR_SET_WORD_SIZE; j++) {
00060       count += VAR_SET_EXTRACT_BIT(i, j);
00061     }
00062     size_array[i] = count;
00063   }
00064 }

var_set_t* var_set_and ( var_set_t result,
var_set_t a,
var_set_t b 
)

Definition at line 94 of file var_set.c.

00095 {
00096   int i;
00097   assert(result->n_elts == a->n_elts);
00098   assert(result->n_elts == b->n_elts);
00099   for (i = 0; i < result->n_words; i++)
00100     result->data[i] = a->data[i] & b->data[i];
00101   return result;
00102 }

var_set_t* var_set_assign ( var_set_t result,
var_set_t set 
)

Definition at line 33 of file var_set.c.

00034 {
00035   int i;
00036 
00037   assert(result->n_elts == set->n_elts);
00038   for (i = 0; i < result->n_words; i++)
00039     result->data[i] = set->data[i];
00040   return result;
00041 }

void var_set_clear ( var_set_t set  ) 

Definition at line 139 of file var_set.c.

00140 {
00141   int i;
00142 
00143   for (i = 0; i < set->n_words; i++)
00144     set->data[i] = 0;
00145 }

void var_set_clear_elt ( var_set_t set,
int  index 
)

Definition at line 131 of file var_set.c.

00132 {
00133   unsigned int *value;
00134   assert(index >= 0 && index < set->n_elts);
00135   value = &(set->data[index / VAR_SET_WORD_SIZE]);
00136   *value = *value & ~(1 << (index % VAR_SET_WORD_SIZE));
00137 }

int var_set_cmp ( char *  obj1,
char *  obj2 
)

Definition at line 197 of file var_set.c.

00198 {
00199   int i;
00200   var_set_t *a = (var_set_t *) obj1;
00201   var_set_t *b = (var_set_t *) obj2;
00202 
00203   assert(a->n_elts == b->n_elts);
00204   for (i = 0; i < a->n_words; i++)
00205     if (a->data[i] != b->data[i]) return 1;
00206   return 0;
00207 }

var_set_t* var_set_copy ( var_set_t set  ) 

Definition at line 21 of file var_set.c.

00022 {
00023   int i;
00024   var_set_t *result = ALLOC(var_set_t, 1);
00025 
00026   *result = *set;
00027   result->data = ALLOC(unsigned int, result->n_words);
00028   for (i = 0; i < result->n_words; i++)
00029     result->data[i] = set->data[i];
00030   return result;
00031 }

int var_set_equal ( var_set_t a,
var_set_t b 
)

Definition at line 185 of file var_set.c.

00186 {
00187   int i;
00188 
00189   assert(a->n_elts == b->n_elts);
00190   for (i = 0; i < a->n_words; i++)
00191     if (a->data[i] != b->data[i]) return 0;
00192   return 1;
00193 }

void var_set_free ( var_set_t set  ) 

Definition at line 43 of file var_set.c.

00044 {
00045   FREE(set->data);
00046   FREE(set);
00047 }

int var_set_get_elt ( var_set_t set,
int  index 
)

Definition at line 117 of file var_set.c.

00118 {
00119   assert(index >= 0 && index < set->n_elts);
00120   return VAR_SET_EXTRACT_BIT(set->data[index / VAR_SET_WORD_SIZE], index % VAR_SET_WORD_SIZE);
00121 }

unsigned int var_set_hash ( var_set_t set  ) 

Definition at line 210 of file var_set.c.

00211 {
00212   int i;
00213   unsigned int result = 0;
00214 
00215   for (i = 0; i < set->n_words; i++)
00216     result += (unsigned int) set->data[i];
00217   return result;
00218 }

int var_set_intersect ( var_set_t a,
var_set_t b 
)

Definition at line 147 of file var_set.c.

00148 {
00149   int i;
00150   assert(a->n_elts == b->n_elts);
00151   for (i = 0; i < a->n_words; i++)
00152     if (a->data[i] & b->data[i]) return 1;
00153   return 0;
00154 }

int var_set_is_empty ( var_set_t a  ) 

Definition at line 156 of file var_set.c.

00157 {
00158   int i;
00159   for (i = 0; i < a->n_words; i++)
00160     if (a->data[i]) return 0;
00161   return 1;
00162 }

int var_set_is_full ( var_set_t a  ) 

Definition at line 164 of file var_set.c.

00165 {
00166   int i;
00167   unsigned value;
00168   for (i = 0; i < a->n_words - 1; i++)
00169     if (a->data[i] != VAR_SET_ALL_ONES) return 0;
00170   value = VAR_SET_ALL_ONES >> (a->n_words * VAR_SET_WORD_SIZE - a->n_elts);
00171   return (a->data[a->n_words - 1] == value);
00172 }

int var_set_n_elts ( var_set_t set  ) 

Definition at line 66 of file var_set.c.

00067 {
00068   register int i, j;
00069   register unsigned int value;
00070   int n_bytes = VAR_SET_WORD_SIZE / VAR_SET_BYTE_SIZE;
00071   int count = 0;
00072 
00073   if (size_array[1] == 0) init_size_array();
00074   for (i = 0; i < set->n_words; i++) {
00075     value = set->data[i];
00076     for (j = 0; j < n_bytes; j++) {
00077       count += size_array[value & 0xff];
00078       value >>= VAR_SET_BYTE_SIZE;
00079     }
00080   }
00081   return count;
00082 }

var_set_t* var_set_new ( int  size  ) 

Definition at line 10 of file var_set.c.

00011 {
00012   var_set_t *result = ALLOC(var_set_t, 1);
00013 
00014   result->n_elts = size;
00015   result->n_words = size / VAR_SET_WORD_SIZE + ((size % VAR_SET_WORD_SIZE == 0) ? 0 : 1);
00016   result->data = ALLOC(unsigned int, result->n_words);
00017   (void) var_set_clear(result);
00018   return result;
00019 }

var_set_t* var_set_not ( var_set_t result,
var_set_t a 
)

Definition at line 104 of file var_set.c.

00105 {
00106   int i;
00107   unsigned int mask;
00108 
00109   assert(result->n_elts == a->n_elts);
00110   for (i = 0; i < a->n_words; i++)
00111     result->data[i] = ~a->data[i];
00112   mask = (unsigned int) VAR_SET_ALL_ONES >> (a->n_words * VAR_SET_WORD_SIZE - a->n_elts);
00113   result->data[a->n_words - 1] &= mask;
00114   return result;
00115 }

var_set_t* var_set_or ( var_set_t result,
var_set_t a,
var_set_t b 
)

Definition at line 84 of file var_set.c.

00085 {
00086   int i;
00087   assert(result->n_elts == a->n_elts);
00088   assert(result->n_elts == b->n_elts);
00089   for (i = 0; i < result->n_words; i++)
00090     result->data[i] = a->data[i] | b->data[i];
00091   return result;
00092 }

void var_set_print ( FILE *  fp,
var_set_t set 
)

Definition at line 174 of file var_set.c.

00175 {
00176   int i;
00177   for (i = 0; i < set->n_elts; i++) {
00178     fprintf(fp, "%d ", var_set_get_elt(set, i));
00179   }
00180   fprintf(fp, "\n");
00181 }

void var_set_set_elt ( var_set_t set,
int  index 
)

Definition at line 123 of file var_set.c.

00124 {
00125   unsigned int *value;
00126   assert(index >= 0 && index < set->n_elts);
00127   value = &(set->data[index / VAR_SET_WORD_SIZE]);
00128   *value = *value | (1 << (index % VAR_SET_WORD_SIZE));
00129 }


Variable Documentation

int size_array[256] [static]

Definition at line 49 of file var_set.c.


Generated on Tue Jan 12 13:57:29 2010 for glu-2.2 by  doxygen 1.6.1