Contents

  1. set.h
  2. set.c

set.h 1/2

[
top][prev][next]
typedef unsigned long long  Set_t; // Type Ensemble  = uint64_t sans doute
typedef int                Elem_t; // elements from 0 to 2^sizeof(Set_t) -1
typedef int               boolean; // boolean pour les fonctions Is_*

/* "Impression d'ensemble" */
#define printout stdout            // file descriptor for Print_set()
void Print_set  (Set_t A);         // Imprime l'ensemble A sur "printout" 
void Println_set(Set_t A);         // Idem avec retour-charriot

/* Operation ensembliste */
Set_t Union_set(Set_t A, Set_t B); // returns : A Union B 
Set_t Inter_set(Set_t A, Set_t B); // returns : A Inter B
Set_t Diff_set (Set_t A, Set_t B); // returns : Difference A / B
Set_t Comp_set (Set_t A);          // returns : Complementaire de A

/* Construction d'un ensemble */
Set_t Empty_set    ();                   // returns : {} 
Set_t Singleton_set(Elem_t x);           // returns : {x}; ou {} si x est hors borne 
Set_t Add_elem_set (Elem_t x , Set_t A); // returns : {x} Union A, ou A si x hors borne

/* quelques tests */
boolean Is_empty_set   (Set_t A);           // test A == {}
boolean Is_elem_in_set (Elem_t x , Set_t A);// test x appartient a 
boolean Is_elem_out_of_range (Elem_t x);    // test si x est hors borne pour l'implementation
        // NB: le test Is_elem_out_of_range est deja integré dans 
        // les fonctions prenant un "Elem_t" en argument

set.c 2/2

[
top][prev][next]
#include "set.h"
#include <stdio.h>

Set_t Empty_set()                { return( 0 ); }
Set_t Union_set(Set_t A, Set_t B){ return( A | B ); }
Set_t Inter_set(Set_t A, Set_t B){ return( A & B ); }
Set_t Diff_set(Set_t A, Set_t B) { return( A & (~B) ); }
Set_t Comp_set(Set_t A)          { return( ~A ); }

boolean Is_elem_out_of_range (Elem_t x) {
  return( (x<0) || (x >= 8*sizeof(Set_t)) );
}

boolean Is_elem_in_set (Elem_t x , Set_t A) {
  return( (A & Singleton_set(x)) != 0 );
}

boolean Is_empty_set (Set_t A){
  return( A==0 );
}

Set_t Singleton_set(Elem_t x){
  if (Is_elem_out_of_range(x)) {
    fprintf(printout, "ERROR : out of range element (%d) ignored\n",x); 
    return(Empty_set());
  }
  else return((Set_t)1<<x);
}

Set_t Add_elem_set (Elem_t x, Set_t A){
  return(A|Singleton_set(x));
}

void Print_set(Set_t A){
  int i, first=0;
  fprintf(printout,"{ ");
  for(i=0; i<8*sizeof(Set_t); i++)
    if (Is_elem_in_set(i,A)) {
      if (first++) fprintf(printout,", %d",i);
      else fprintf(printout,"%d",i);
    }
  fprintf(printout," }");
}

void Println_set(Set_t A){
  Print_set(A);
  fprintf(printout,"\n");
}

void Test(){
  Set_t A,B;
  A=Add_elem_set(20, Empty_set()); A=Add_elem_set(222, A); Println_set(A);
  B=Singleton_set(21); B=Add_elem_set(22, B); Println_set(B);
  Println_set(Union_set(A,B));
  Println_set(Inter_set(Singleton_set(21),B));
  Println_set(Comp_set(B));
  Println_set(Inter_set(B,Comp_set(B)));
}

// int main(){ Test(); return(0); }

Generated by GNU enscript 1.6.4.
gererated as : enscript -E --color --language=html --toc --output=set.html set.h set.c