BitArray.h

00001 /*  _________________________________________________________________________
00002  *
00003  *  UTILIB: A utility library for developing portable C++ codes.
00004  *  Copyright (c) 2001, Sandia National Laboratories.
00005  *  This software is distributed under the GNU Lesser General Public License.
00006  *  For more information, see the README file in the top UTILIB directory.
00007  *  _________________________________________________________________________
00008  */
00009 
00010 //
00011 // BitArray.h
00012 //
00024 #ifndef __BitArray_h
00025 #define __BitArray_h
00026 
00027 #ifdef __GNUC__
00028 #pragma interface
00029 #endif
00030 
00031 #ifdef NON_ANSI
00032 #include <iostream.h>
00033 #else
00034 #include <iostream>
00035 using namespace std;
00036 #endif
00037 #ifndef ANSI_HDRS
00038 #include <math.h>
00039 #else
00040 #include <cmath>
00041 #endif
00042 #include "BitArrayBase.h"
00043 
00044 #ifndef SWIG
00045 // I'm not sure that this would work if BYTESIZE != 8
00046 #define BYTESIZE 8      /* bits per char */
00047 #define BIT(X,i)        (((X) & (char)(1 << ((i) & (BYTESIZE - 1)))) != 0)
00048 #define SET_BIT(X,i)    ((X) |= (char)(1 << ((i) & (BYTESIZE - 1))))
00049 #define RESET_BIT(X,i)  ((X) &= ~(char)(1 << ((i) & (BYTESIZE - 1))))
00050 #endif
00051 
00063 #if !defined(BitArraySanityChecking)
00064 #define BitArraySanityChecking  1
00065 #endif
00066 
00067 
00068 #ifdef SWIG
00069 class BitArray : public BitArrayBase {
00070 #else
00071 class UTILIB_API BitArray : public BitArrayBase {
00072 #endif
00073 
00074 public:
00075 
00077   BitArray() : BitArrayBase() {}
00079   BitArray(const size_type len, const size_type nbytes=0, char* d=(char*)0, 
00080                          const EnumDataOwned o=DataNotOwned)
00081                 {construct(len,
00082                         (nbytes==0 ?  (len+BYTESIZE-1)/(BYTESIZE/element_size()) 
00083                                    : nbytes),
00084                         d,o); }
00086   BitArray(const BitArray& array)
00087                 : BitArrayBase(array) {}
00088 
00090   int operator()(const size_type ndx) const;
00092   void put(const size_type ndx, const int val);
00094   void set(const size_type ndx);
00096   void set();
00098   void reset(const size_type ndx);
00100   void reset();
00102   void flip(const size_type ndx);
00104   void flip();
00105 
00106 
00108   size_type nbits() const;
00109 
00111   int read(istream& input);
00112 
00117   int shared_one(BitArray& other) const;
00118 
00119 protected:
00120 
00122   int operator[](const size_type ndx) const;
00124   int element_size() const
00125                 {return 1;}
00126 
00127 };
00128  
00129 //
00130 // Related functions that might be useful
00131 //
00133 int bitwise_or(BitArray& a1, BitArray& a2, BitArray& result);
00135 int bitwise_xor(BitArray& a1, BitArray& a2, BitArray& result);
00137 int bitwise_and(BitArray& a1, BitArray& a2, BitArray& result);
00138 
00139 
00140 inline int BitArray::operator()(const size_type idx) const
00141 {
00142 #if (BitArraySanityChecking==1)
00143 if (idx >= array_len)
00144    ErrAbort(errmsg("BitArray<T>::operator() : iterator out of range. idx=%d len=%d",idx,array_len));
00145 #endif
00146 
00147 int ans;
00148 ans = (int) BIT( Data[idx/BYTESIZE] , idx );
00149 return ans;
00150 }
00151  
00152 inline int BitArray::operator[](const size_type idx) const
00153 {
00154 #if (BitArraySanityChecking==1)
00155 if (idx >= array_len)
00156    ErrAbort(errmsg("BitArray<T>::operator[] : iterator out of range. idx=%d len=%d",idx,array_len));
00157 #endif
00158 
00159 int ans;
00160 ans = (int) BIT( Data[idx/BYTESIZE] , idx );
00161 return ans;
00162 }
00163  
00164 #endif