#include #include #include #include "ensemble.h" #define SCHAR 8 #define NCHAR 128 #define NMAX (SCHAR * NCHAR) static unsigned char masque[SCHAR]; static unsigned char ens[NCHAR]; static void init_masque(void); static void afficher_octet(char c); static void test_max(unsigned int i); void ensemble_initialiser() { unsigned int i; init_masque(); for (i = 0; i < NCHAR; i++) ens[i] = 0; } unsigned int ensemble_majorant() { return NMAX - 1; } void ensemble_ajouter(unsigned int i) { test_max(i); ens[i / SCHAR] |= masque[i % SCHAR]; } void ensemble_supprimer(unsigned int i) { test_max(i); ens[i / SCHAR] &= ~masque[i % SCHAR]; } int ensemble_present(unsigned int i) { test_max(i); return (ens[i / SCHAR] & masque[i % SCHAR])? 1 : 0; } int ensemble_max() { int i; for (i = NMAX - 1; i >= 0; i--) { if (ensemble_present(i)) break; } return i; } unsigned int ensemble_cardinal() { unsigned int n = 0; unsigned int i; for (i = 0; i < NCHAR; i++) { int c = ens[i]; while (c) { n += c & 1; c >>= 1; } } return n; } static void init_masque() { unsigned int i; for (i = 0; i < SCHAR; i++) masque[i] = 1 << i; } static void afficher_octet(char c) { unsigned int i; for (i = 7; i >= 0; i--) printf("%c", (c >> i) & 1 ? '1' : '0'); printf("\n"); } static void test_max(unsigned int i) { assert(i < NMAX); }