00001
00005
00006
00007 #include <stdio.h>
00008 #include <stdlib.h>
00009 #include "bitvecset.h"
00010
00011 BV_Set * makeSet(int *list, int size, int max);
00012
00013 BV_Set * makeEmptySet(int max)
00014 {
00015 int x;
00016
00017 return ( makeSet(&x, 0, max));
00018
00019 }
00020
00021
00022 BV_Set * makeSet(int *list, int size, int max)
00023 {
00024 BV_Set * s;
00025 int i;
00026
00027 s = (BV_Set *) malloc(sizeof(BV_Set));
00028 s->max = max;
00029 s->size = size;
00030 s->vector = (short *) malloc(sizeof(short)*(1+max));
00031
00032 for (i = 0; i<= max; i++)
00033 s->vector[i] = 0;
00034 for (i = 0; i< size; i++)
00035 if (list[i] <= max && list[i] >= 0)
00036 s->vector[ list[i]] = 1;
00037 else
00038 printf("***ERROR: makeSet received %d, when max was supposed to be %d",
00039 list[i], max);
00040
00041 return s;
00042 }
00043
00044 void destroySet(BV_Set* set)
00045 {
00046 free(set->vector);
00047 set->vector=0;
00048 free(set);
00049 }
00050
00051
00052 void bvset_insert(BV_Set * s, int value)
00053 {
00054 if (value > s->max || value < 0)
00055 printf("BV_Set error. inserting value %d in a set where max is %d\n",
00056 value, s->max);
00057 else {
00058 if (s->vector[value]) return;
00059 else {
00060 s->vector[value] = 1;
00061 s->size++;
00062 }
00063 }
00064 }
00065
00066
00067 int bvset_find(BV_Set * s, int value)
00068 {
00069 if (value > s->max || value < 0) {
00070 printf("BV_Set error. *find* on a value %d in a set where max is %d\n",
00071 value, s->max);
00072 return -1;
00073 }
00074 else return (s->vector[value]);
00075 }
00076
00077 int bvset_size(BV_Set * s)
00078 {
00079 return s->size;
00080 }
00081
00082 void bvset_enumerate(BV_Set * s, int **list, int *size)
00083 {
00084 int i, j;
00085
00086
00087
00088
00089
00090
00091
00092 *list = (int *) malloc(sizeof(int)*s->size);
00093 *size = s->size;
00094
00095 j = 0;
00096 for (i=0; i<=s->max; i++)
00097 if (s->vector[i]) (*list)[j++] = i;
00098
00099 if (j > s->size) {
00100 printf("Error, too many bits written %d %d\n",j,s->size);
00101 printf("set is: ");
00102 for (i=0; i<=s->max; i++)
00103 printf("%d ", s->vector[i]);
00104 printf("\n returning list: ");
00105 for (i=0; i< *size; i++)
00106 printf("%d ", (*list)[i]);
00107 }
00108 }
00109