C-Breeze
C Compiler Infrastructure

[ Project home page]
Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

bits.h

Go to the documentation of this file.
00001 // $Id: bits.h,v 1.5 2003/08/07 23:14:07 pnav Exp $
00002 // ----------------------------------------------------------------------
00003 //
00004 //  C-Breeze
00005 //  C Compiler Framework
00006 // 
00007 //  Copyright (c) 2000 University of Texas at Austin
00008 // 
00009 //  Samuel Z. Guyer
00010 //  Daniel A. Jimenez
00011 //  Calvin Lin
00012 // 
00013 //  Permission is hereby granted, free of charge, to any person
00014 //  obtaining a copy of this software and associated documentation
00015 //  files (the "Software"), to deal in the Software without
00016 //  restriction, including without limitation the rights to use, copy,
00017 //  modify, merge, publish, distribute, sublicense, and/or sell copies
00018 //  of the Software, and to permit persons to whom the Software is
00019 //  furnished to do so, subject to the following conditions:
00020 //  
00021 //  The above copyright notice and this permission notice shall be
00022 //  included in all copies or substantial portions of the Software.
00023 //  
00024 //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025 //  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00026 //  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00027 //  NONINFRINGEMENT.  IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT
00028 //  AUSTIN BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
00029 //  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
00030 //  OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00031 //  THE SOFTWARE.
00032 //
00033 //  We acknowledge the C-to-C Translator from MIT Laboratory for
00034 //  Computer Science for inspiring parts of the C-Breeze design.
00035 //
00036 // ----------------------------------------------------------------------
00037 
00038 #ifndef CBZ_BITS_H
00039 #define CBZ_BITS_H
00040 
00041 #define BITS_PER_INT (8*sizeof(int))
00042 
00043 class Bits {
00044 private:
00045         unsigned int *ints;
00046         int     n, nints;
00047 
00048 public:
00049         void reset_all (void) {
00050                 for (int i=0; i<nints; i++) ints[i] = 0;
00051         }
00052 
00053         void set_all (void) {
00054           // TODO: should this be like reset_all()?
00055           for (int i=0; i < n; i++) set(i);
00056         }
00057 
00058         Bits (int nn) {
00059                 n = nn;
00060                 nints = nn / BITS_PER_INT;
00061                 if (nn % BITS_PER_INT) nints++;
00062                 ints = new unsigned int[nints];
00063                 reset_all ();
00064         }
00065 
00066         Bits (Bits & other) {
00067           n = other.n;
00068           nints = other.nints;
00069           ints = new unsigned int[nints];
00070           copy(&other);
00071         }
00072 
00073 
00074         ~Bits (void) {
00075           delete [] ints;
00076         }
00077 
00078         Bits & operator=(Bits & rhs) {
00079           if ( this != &rhs ) {
00080             delete [] ints;
00081             n = rhs.n;
00082             nints = rhs.nints;
00083             ints = new unsigned int[nints];
00084             copy(&rhs);
00085           }
00086           return *this;
00087         }
00088 
00089         int size(void) { return n; }
00090 
00091         void set (int pos, bool val) {
00092                 int i = pos / BITS_PER_INT;
00093                 int j = pos % BITS_PER_INT;
00094                 if (val) {
00095                         ints[i] |= (1<<j);
00096                 } else {
00097                         ints[i] &= ~(1<<j);
00098                 }
00099         }
00100 
00101         void set (int pos) {
00102                 set (pos, true);
00103         }
00104 
00105         void copy (Bits * b) {
00106                 for (int i=0; i<nints; i++) ints[i] = b->ints[i];
00107         }
00108 
00109         void reset (int pos) {
00110                 set (pos, false);
00111         }
00112 
00113         bool value (int pos) {
00114                 int i = pos / BITS_PER_INT;
00115                 int j = pos % BITS_PER_INT;
00116                 if (ints[i] & (1<<j)) 
00117                         return true;
00118                 return false;
00119         }
00120 
00121         void And (Bits *other) {
00122                 for (int i=0; i<nints; i++) 
00123                         ints[i] &= other->ints[i];
00124         }
00125 
00126         void Or (Bits *other) {
00127                 for (int i=0; i<nints; i++) 
00128                         ints[i] |= other->ints[i];
00129         }
00130 
00131         void Not (void) {
00132                 for (int i=0; i<nints; i++)
00133                         ints[i] = ~ints[i];
00134         }
00135 
00136         void Difference(Bits * other) {
00137           Bits comp(n);
00138           comp.copy(other);
00139           comp.Not();
00140           And(&comp);
00141         }
00142 
00143         friend bool operator==(Bits & a, Bits & b) {
00144           if ( a.nints != b.nints )
00145             return false;
00146           for ( int i = 0; i < a.nints; i++ )
00147             if ( a.ints[i] != b.ints[i] )
00148               return false;
00149           return true;
00150         }
00151 
00152         friend bool operator!=(Bits & a, Bits & b) {
00153           return ! (a == b);
00154         }
00155 
00156         Bits *clone (void) {
00157                 Bits *b = new Bits (n);
00158                 for (int i=0; i<nints; i++) b->ints[i] = ints[i];
00159                 return b;
00160         }
00161 
00162         friend ostream & operator<<(ostream & out, Bits & a) {
00163           out << "(" << a.size() << ")" << " ( ";
00164           for ( int i = 0; i < a.size(); i++ )
00165             out << a.value(i);
00166           out << " )";
00167           return out;
00168         }
00169 };
00170 
00171 #endif // CBZ_BITS_H

Generated on August 27, 2003
Back to the C-Breeze home page