home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_10_07 / 1007022a < prev    next >
Text File  |  1992-05-07  |  4KB  |  146 lines

  1.  
  2.  
  3. // 2D - bit packed array
  4. // fundamental class definitions
  5.  
  6. #include <stdlib.h>
  7. #include <iostream.h>
  8.  
  9. enum logical { FALSE, TRUE };
  10.  
  11. typedef unsigned char BYTE;
  12.  
  13. template < int XDIM, int YDIM, int NUM_BITS >
  14. class Bit_2D_Array {
  15.     private:
  16.         // You can't initialize NUM_BYTES here, but you can dimension
  17.         // data, so NUM_BYTES = sizeof(data);
  18.         int NUM_BYTES;
  19.  
  20.         BYTE data[( XDIM * YDIM * NUM_BITS + 7 ) / 8];
  21.  
  22.         class BIT_TMP {
  23.             public:
  24.                 BYTE * byte_ptr;
  25.                 BYTE   shift_bits;
  26.                 BYTE   value;
  27.  
  28.                 BYTE MASK[8];     // All but bits of interest CLEARED
  29.                 BYTE INV_MASK[8]; // All but bits of interest SET
  30.  
  31.                 operator int() {
  32.                     return( value );
  33.                 }
  34.  
  35.                 BIT_TMP() {
  36.                     double num_sub_fields;
  37.                     BYTE tmp_mask;
  38.                     int i;
  39.  
  40.                     // Needed for Cfront3.0
  41.                     cout << " BIT_TMP" << endl;
  42.  
  43.                     num_sub_fields = 8.0 / NUM_BITS;
  44.                     if ( num_sub_fields * NUM_BITS != 8 ) {
  45.                         cerr << "NUM_BITS must divide evenly into 8" << endl;
  46.                         exit(1);
  47.                     }
  48.                     tmp_mask = 1;
  49.                     for( i=1; i<NUM_BITS; i++) {
  50.                         tmp_mask <<= 1;
  51.                         tmp_mask |= 1;
  52.                     }
  53.                     for( i=0; i<num_sub_fields; i++ ) {
  54.                         MASK[i] = tmp_mask << (i*NUM_BITS);
  55.                         INV_MASK[i] = 255 ^ MASK[i];
  56.                     }
  57.                 }
  58.  
  59.                 void operator=( class BIT_TMP input ) {
  60.  
  61.                     input.value &= MASK[0]; // Mask off any extraneous bits 
  62.                     *(byte_ptr) &= INV_MASK[ shift_bits / NUM_BITS ];
  63.                     *(byte_ptr) |= ( input.value << shift_bits );
  64.                     value = input.value; 
  65.                 }
  66.  
  67.                 void operator=( BYTE input_value ) {
  68.  
  69.                     input_value &= MASK[0]; // Mask off any extraneous bits 
  70.                     *(byte_ptr) &= INV_MASK[ shift_bits / NUM_BITS ];
  71.                     *(byte_ptr) |= ( input_value << shift_bits );
  72.                     value = input_value; 
  73.                 }
  74.  
  75.                 logical operator==( class BIT_TMP input ) {
  76.                     return( logical( value == input.value) );
  77.                 }
  78.         };
  79.  
  80.         class BIT_TMP local_rec;  
  81.  
  82.     public:
  83.         Bit_2D_Array();
  84.  
  85.         // This should be an overloaded ostream but Cfront3.0 crashes
  86.         void dump() {
  87.             int i, j;
  88.  
  89.         cout.width(3);
  90.         cout << endl ;
  91.         for( j=0; j<YDIM; j++) {
  92.         cout << j << " ";
  93.         }
  94.         cout << endl;
  95.         cout << "  ";
  96.         for( j=0; j<YDIM; j++) {
  97.         cout << "----";
  98.             }
  99.             cout << endl;
  100.             for( i=0; i<XDIM; i++) {
  101.                 cout << i << "|";
  102.                 for( j=0; j<YDIM; j++) {
  103.                     cout << (*this)(i,j) << " ";
  104.                 }
  105.                 cout << endl;
  106.             }
  107.         }
  108.  
  109.         class BIT_TMP operator()( int i, int j ) {
  110.             BYTE bit_num, byte_num, sub_bits;
  111.  
  112.             bit_num  = NUM_BITS * (XDIM*i + j);
  113.             byte_num = bit_num / 8;
  114.             sub_bits = bit_num % 8;
  115.  
  116.             local_rec.byte_ptr = &data[byte_num];
  117.             local_rec.shift_bits = sub_bits;
  118.             local_rec.value = 
  119.         local_rec.MASK[0] & ( data[byte_num] >> sub_bits );
  120.         cout << " subscript for " << i << ',' << j << endl ;
  121.             return( local_rec );
  122.         };
  123.  
  124.         logical operator==( Bit_2D_Array< XDIM, YDIM, NUM_BITS > RHS ) {
  125.             int i;
  126.  
  127.             for( i=0; i< NUM_BYTES; i++) {
  128.                 if ( data[i] != RHS.data[i] ) {
  129.                     return( FALSE );
  130.                 }
  131.             }
  132.             return( TRUE );
  133.         };
  134.  
  135.         void operator=( int RHS ) {
  136.             int i, j;
  137.  
  138.             for( i=0; i< XDIM; i++) {
  139.                 for( j=0; j< YDIM; j++) {
  140.                     (*this)(i,j) = RHS;
  141.                 }
  142.             }
  143.         };
  144. };
  145.  
  146.