#include<stdio.h>
#include<stdlib.h>

/* burton rosenberg
   11 jan 1998

   from a list of acceptible ranges, make
   two conversion tables and pass characters through them.

   the forwqard tables relables the characters by their position
   in the range, or -1 if out-of-range, the reverse table is
   the inverse, except -1 is an error.
*/

int maketable( int * t, int * p, int n ) {

/* t is the table to be built, must be int [256], 
   p is an even-length array  where consecutive pairs are 
   integer ranges, n is the length of array p.
   returns the length of t
*/

   int i, ii, j = 0 ;

   for ( i=0; i<256; i++ ) t[i] = -1 ;

   for ( i=0; i<n; i+=2 ) {
      if ( p[i]<0 || p[i]>=256 || p[i+1]<0 || p[i+1]>=256
         || p[i]>p[i+1] ) continue ;
      for ( ii=p[i]; ii<=p[i+1]; ii++ )
         t[ii] = j++ ;
   }
   return j ; 

}

int invtable( int * invt, int * t ) {

/* invert table t into table invt, both tables assumed to
    be of length 256.
*/

   int i ;
   int j = 0 ;
   for ( i=0; i<256; i++ ) invt[i] = -1 ;
   for ( i=0; i<256; i++ )
      if ( t[i]>=0 && t[i]<256 )  {
         invt[t[i]] = i ;
         j++ ;
      }
   return j ;
}

void printtable(int * t, int n ) {

   int i ;
   for ( i=0; i<n; i++ ) 
      printf("%3d %3d\n", i, t[i] ) ;

}


int main( int argc, char * argv[] ) {

   int ft[256] ;
   int rt[256] ;
   int p[] = { (int)'a', (int)'z' } ;
   maketable( ft, p, 2 ) ;
   printtable( ft, 256 ) ;

}







