#include "set.h" #include #include #include short set_check (set* s){ return (s != NULL); } short set_check2 (set* s){ return ( (s != NULL) && (s->n >= 0) && (s->t != NULL)); } set* set_alloc (int max_elt){ set* s = (set*)malloc (sizeof(set)); s->n = max_elt; s->t = calloc(max_elt+1, sizeof(set_type)); return s; } short set_eq (set* s1, set* s2){ int i; set* tmp1 = s1; set* tmp2 = s2; if (! (set_check(s1) && (set_check(s2)))){ fprintf(stderr, "ERROR: Testing equality of invalid or null set\n"); exit(1); } if (tmp2->n > tmp1->n){ tmp1 = s2; tmp2 = s1; } for (i=0; i<= tmp1->n; i++) if (tmp1->t[i] != tmp2->t[i]) return 0; for (; i <= tmp2->n; i++) if (tmp2->n != 0) return 0; return 1; } set* set_copy (set* s){ if (! set_check (s)){ fprintf(stderr, "ERROR: Copying invalid or null set\n"); exit(1); } set* copy = set_alloc(s->n); memcpy (copy->t, s->t, (s->n +1) * sizeof(set_type)); return copy; } short set_is_empty (set* s){ if (! set_check (s)){ fprintf(stderr, "WRN: Testing emptiness of invalid or null set\n"); return 1; } int i; for (i=0; i<= s->n; i++) if (s->t [i]) return 0; return 1; } short set_add_elt (set* s, int elt){ if (! set_check (s)){ fprintf(stderr, "ERROR: Adding element in invalid or null set\n"); return 0; } if (s->n < elt){ s-> t = realloc (s->t, (elt+1) * sizeof (set_type)); memset (s->t + (s->n +1), 0, elt - s->n); s->n = elt; } s->t[elt] = 1; return 1; } short set_rm_elt (set* s, int elt){ if (! (set_check (s) && (s->n >= elt))){ fprintf(stderr, "ERROR: removing element in invalid or null set\n"); return 0; } s->t [elt] = 0; return 1; } void set_empty (set* s){ if (! set_check (s)) fprintf(stderr, "ERROR: set_empty in invalid or null set\n"); memset(s->t, 0, (s->n +1) * sizeof (set_type)); } void set_free (set* s){ if (set_check (s)){ free(s->t); free(s); } } subset_list* subset_list_alloc (int set_size) { subset_list* l = malloc (sizeof(subset_list)); l->set_size = set_size; l->n = 0; l->t = NULL; /* l->t = malloc (set_size * sizeof(set*)); */ return l; } int subset_list_add (subset_list* l, set* s){ int i = 0; for (i = 0; i < l->n; i++) if (set_eq (l->t[i], s)) return i; if (l->n == i) l->t= realloc (l->t, (l->n + 1) * sizeof (set*)); l->t[i] = set_copy (s); l->n++; return i; } short subset_list_has (subset_list* l, set* s){ int i; for (i =0; i< l->n; i++) if(set_eq (l->t[i], s)) return 1; return 0; } void subset_list_free (subset_list* l){ int i; for (i=0; i < l->n; i++) set_free (l->t[i]); free (l->t); free (l); }