AMROC Main     Blockstructured Adaptive Mesh Refinement in object-oriented C++


Main Page   Class Hierarchy   Compound List   File List  

BitVec.C

Go to the documentation of this file.
00001 
00006 #include "BitVec.h"
00007 
00008 /*
00009 *************************************************************************
00010 *                                                                       *
00011 * BitVec::BitVec(const int len)                                         *
00012 *                                                                       *
00013 * Constructor BitVec creates a bit vector of length "len" as an         *
00014 * array of BitVecType. The bit vector is initialized to 0.              *
00015 *                                                                       *
00016 *************************************************************************
00017 */
00018 
00019 BitVec::BitVec(const int len) 
00020   {
00021 
00022    slot_width = (sizeof(BitVecType)*ByteWidth);
00023 
00024    slots = (len%slot_width == 0) ?
00025            len/slot_width : len/slot_width + 1;
00026 
00027 #ifdef DEBUG_PRINT
00028    assert(slots <= MaxBitVecSlots);
00029 #endif
00030 
00031    length = len;
00032 
00033    for(register int i=0;i<slots;i++)
00034      bvec[i] = 0;
00035 
00036   }
00037 
00038 /*
00039 *************************************************************************
00040 *                                                                       *
00041 * BitVec const &BitVec::operator= (BitVec const &other);                *
00042 *                                                                       *
00043 * Overload the assignment operator for BitVec                           * 
00044 *                                                                       *
00045 *************************************************************************
00046 */
00047 
00048 BitVec const &BitVec::operator= (BitVec const &other)
00049   {
00050    if(this != &other) 
00051      {
00052       slot_width = other.slot_width;
00053       slots = other.slots;
00054       length = other.length;
00055 
00056       for(register int i=0;i<slots;i++)
00057         bvec[i] = other.bvec[i];
00058      }
00059    return(*this);
00060   }
00061 
00062 /*
00063 *************************************************************************
00064 *                                                                       *
00065 * int BitVec::operator== (BitVec const &other);                         *
00066 *                                                                       *
00067 * Overload the equality operator for BitVec                             * 
00068 *                                                                       *
00069 *************************************************************************
00070 */
00071 
00072 int BitVec::operator== (BitVec const &other) const
00073   {
00074 #ifdef DEBUG_PRINT
00075    assert(length == other.length);
00076 #endif
00077  
00078    if(this == &other) return(1);
00079 
00080    for(register int i=0;i<slots;i++)
00081      if((int)bvec[i] != (int)other.bvec[i])
00082        return 0;
00083 
00084    return(1);
00085   }
00086 
00087 /*
00088 *************************************************************************
00089 *                                                                       *
00090 * int BitVec::operator> (BitVec const &other);                          *
00091 *                                                                       *
00092 * Overload the greater than operator for BitVec                         * 
00093 *                                                                       *
00094 *************************************************************************
00095 */
00096 
00097 int BitVec::operator> (BitVec const &other) const
00098   {
00099 #ifdef DEBUG_PRINT
00100    assert(length == other.length);
00101 #endif
00102 
00103    if(this == &other) 
00104      return(0);
00105 
00106    for(register int i=0;i<slots;i++) {
00107      if(bvec[i] > other.bvec[i])
00108        return 1;
00109      else if(bvec[i] < other.bvec[i])
00110        return 0;
00111    }
00112 
00113    return(0);
00114   }
00115 
00116 /*
00117 *************************************************************************
00118 *                                                                       *
00119 * int BitVec::operator< (BitVec const &other);                          *
00120 *                                                                       *
00121 * Overload the less than operator for BitVec                            * 
00122 *                                                                       *
00123 *************************************************************************
00124 */
00125 
00126 int BitVec::operator< (BitVec const &other) const
00127   {
00128 #ifdef DEBUG_PRINT
00129    assert(length == other.length);
00130 #endif
00131 
00132    if(this == &other) 
00133      return(0);
00134 
00135    for(register int i=0;i<slots;i++) {
00136      if(bvec[i] < other.bvec[i])
00137        return 1;
00138      else if(bvec[i] > other.bvec[i])
00139        return 0;
00140    }
00141 
00142    return(0);
00143   }
00144 
00145 /*
00146 *************************************************************************
00147 *                                                                       *
00148 * int BitVec::operator!= (BitVec const &other);                         *
00149 *                                                                       *
00150 * Overload the != operator for BitVec                                   * 
00151 *                                                                       *
00152 *************************************************************************
00153 */
00154 
00155 int BitVec::operator!= (BitVec const &other) const
00156   {
00157 #ifdef DEBUG_PRINT
00158    assert(length == other.length);
00159 #endif
00160 
00161    if(this == &other) 
00162      return(0);
00163 
00164    for(register int i=0;i<slots;i++)
00165      if((int)bvec[i] != (int)other.bvec[i])
00166        return 1;
00167 
00168    return(0);
00169   }
00170 
00171 /*
00172 *************************************************************************
00173 *                                                                       *
00174 * int BitVec::operator>= (BitVec const &other);                         *
00175 *                                                                       *
00176 * Overload the >= operator for BitVec                                   *
00177 *                                                                       *
00178 *************************************************************************
00179 */
00180 
00181 int BitVec::operator>= (BitVec const &other) const
00182   {
00183 #ifdef DEBUG_PRINT
00184    assert(length == other.length);
00185 #endif
00186 
00187    if(this == &other) 
00188      return(1);
00189 
00190    for(register int i=0;i<slots;i++) {
00191      if(bvec[i] > other.bvec[i])
00192        return 1;
00193      else if(bvec[i] < other.bvec[i])
00194        return 0;
00195    }
00196 
00197    return(1);
00198   }
00199 
00200 /*
00201 *************************************************************************
00202 *                                                                       *
00203 * int BitVec::operator<= (BitVec const &other);                         *
00204 *                                                                       *
00205 * Overload the <= operator for BitVec                                   *
00206 *                                                                       *
00207 *************************************************************************
00208 */
00209 
00210 int BitVec::operator<= (BitVec const &other) const
00211   {
00212 #ifdef DEBUG_PRINT
00213    assert(length == other.length);
00214 #endif
00215 
00216    if(this == &other) 
00217      return(1);
00218 
00219    for(register int i=0;i<slots;i++) {
00220      if(bvec[i] < other.bvec[i])
00221        return 1;
00222      else if(bvec[i] > other.bvec[i])
00223        return 0;
00224    }
00225 
00226    return(1);
00227   }
00228 
00229 /*
00230 *************************************************************************
00231 *                                                                       *
00232 * void BitVec::IsolateBit(const int loc, const int num) const           *
00233 *                                                                       *
00234 * Member function IsolateBit isolates the bit at location "loc" of      *
00235 * bit vector and returns it as the LSB of an int.                       *
00236 * Remember that the most significant bit is numbered 0.                 *
00237 *                                                                       *
00238 *************************************************************************
00239 */
00240 
00241 int BitVec::IsolateBit(const int loc, const int num) const
00242  {
00243 #ifdef DEBUG_PRINT
00244    assert(loc >= 0 && loc+num-1 < length);
00245 #endif
00246 
00247    register int n;
00248    if((n = (slot_width - loc%slot_width)) < num) {
00249      register int tmp = (~((~(int)0)<<n)) & 
00250        bvec[GetSlot(loc)];
00251                     /* IsolateBit(loc,n); */
00252      tmp = (tmp<<(num-n)) | 
00253        ((~((~(int)0)<<num-n)) & 
00254         (bvec[GetSlot(loc+n)]>>(slot_width-(num-n))));
00255                     /* IsolateBit(loc+n,num-n); */
00256      return tmp;
00257    }
00258    else {
00259       return ( (~((~(int)0)<<num)) & 
00260                (bvec[GetSlot(loc)]>>(n-num)) );
00261    }
00262  }
00263 
00264 /*
00265 *************************************************************************
00266 *                                                                       *
00267 * void BitVec::SwapBit(const int loc1, const int loc2)                  *
00268 *                                                                       *
00269 * Member function SwapBit swaps the bits at locations "loc1" and "loc2".*
00270 *                                                                       *
00271 * Remember that the most significant bit is numbered 0.                 *
00272 *                                                                       *
00273 *************************************************************************
00274 */
00275 
00276 void BitVec::SwapBit(const int loc1, const int loc2)
00277  {
00278 #ifdef DEBUG_PRINT
00279    assert(loc1 >= 0 && loc2 < length &&
00280           loc2 >= 0 && loc2 < length);
00281 #endif
00282 
00283    if (loc1 == loc2) return;
00284         
00285    register int tmp = IsolateBit(loc1);
00286 
00287    if (TestBit(loc2))
00288      SetBit(loc1);
00289    else
00290      UnsetBit(loc1);
00291 
00292    if (tmp)
00293      SetBit(loc2);
00294    else
00295      UnsetBit(loc2);
00296  }


Quickstart     Users Guide     Programmers Reference     Installation      Examples     Download



AMROC Main      Home      Contact
last update: 06/01/04