BitArrayBase.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 // BitArrayBase.h
00012 //
00041 #ifndef __BitArrayBase_h
00042 #define __BitArrayBase_h
00043 
00044 #ifdef __GNUC__
00045 #pragma interface
00046 #endif
00047 
00048 #ifdef NON_ANSI
00049 #include <iostream.h>
00050 #else
00051 #include <iostream>
00052 using namespace std;
00053 #endif
00054 #ifndef ANSI_HDRS
00055 #include <math.h>
00056 #else
00057 #include <cmath>
00058 #endif
00059 
00060 
00061 #include "_generic.h"
00062 #include "PackBuf.h"
00063 #include "ClassRef.h"
00064 
00065 #ifndef SWIG
00066 // I'm not sure that this would work if BYTESIZE != 8
00067 #define BYTESIZE 8      /* bits per char */
00068 #define BIT(X,i)        (((X) & (char)(1 << ((i) & (BYTESIZE - 1)))) != 0)
00069 #define SET_BIT(X,i)    ((X) |= (char)(1 << ((i) & (BYTESIZE - 1))))
00070 #define RESET_BIT(X,i)  ((X) &= ~(char)(1 << ((i) & (BYTESIZE - 1))))
00071 
00083 #if !defined(BitArraySanityChecking)
00084 #define BitArraySanityChecking  1
00085 #endif
00086 
00087 
00088 class UTILIB_API BitArrayBase;
00089 
00095 class UTILIB_API BitArrayRef : public ArrayRef<char,BitArrayBase> {
00096 
00097 public:
00098 
00100   BitArrayRef() : ArrayRef<char,BitArrayBase>() {array_len=0;}
00101 
00103   virtual void copy_data(BitArrayBase* array);
00105   virtual void reset_data(BitArrayBase* array);
00106 
00108   size_type array_len;
00110   void set_data(char* Data_, size_type Len_, size_type array_len_,
00111                                                         EnumDataOwned own_)
00112         {
00113         if (Len_ > 0)
00114            array_len = array_len_;
00115         else
00116            array_len = 0;
00117         ArrayRef<char,BitArrayBase>::set_data(Data_,Len_, own_);
00118         }
00119 
00120 };
00121 #endif //SWIG
00122 
00123 
00124 #ifdef SWIG
00125 class BitArrayBase {
00126 #else
00127 class UTILIB_API BitArrayBase {
00128 #endif
00129 
00130   friend class ArrayRef<char,BitArrayBase>;
00131   friend class BitArrayRef;
00132 
00133 public:
00134 
00136   BitArrayBase()
00137         {construct(0,0,0);}
00148 #ifndef NON_ANSI
00149   explicit
00150 #endif
00151   BitArrayBase(const size_type len, const size_type nbytes=0, char* d=(char*)0, 
00152                          const EnumDataOwned o=DataNotOwned)
00153         {construct(len,nbytes,d,o);}
00155   BitArrayBase(const BitArrayBase& array)
00156         {construct(array.array_len,array.Len, array.Data, AcquireOwnership);}
00158   virtual ~BitArrayBase()
00159         {free();}
00160 
00162   int shared_mem() const
00163         {return (shared != NULL);}
00165   int resize(const size_type num);
00167   size_type size() const
00168         {return array_len;}
00170   int len() const
00171         {return (int)size();}
00173   char* data() const
00174         {return Data;}
00176   int nrefs() const
00177         {if (shared) return shared->ref; else return 0;}
00178 
00180   BitArrayBase& operator=(const BitArrayBase& array);
00182   BitArrayBase& operator&=(const BitArrayBase& array);
00184   BitArrayBase& operator&=(BitArrayBase& array);
00186   BitArrayBase& operator=(const int value);
00188   BitArrayBase& operator<<(const BitArrayBase& vec);
00189 
00191   virtual void put(const size_type ndx, const int val) = 0;
00192 
00194   int operator==(const BitArrayBase& array) const;
00196   int operator!=(const BitArrayBase& array) const;
00204   int compare(const BitArrayBase& array) const;
00205 
00207   int operator>(const BitArrayBase&) const;
00209   int operator>=(const BitArrayBase&) const;
00210 
00216   int write(ostream& output) const;
00218   virtual int read(istream& input) = 0;
00220   int write(PackBuffer& output) const;
00222   int read(UnPackBuffer& input);        
00223 
00224 protected:
00225 
00227   virtual int operator[](const size_type ndx) const = 0;
00229   char* Data;
00231   size_type Len;
00233   size_type array_len;
00235   BitArrayRef* shared;
00236 
00238   void construct(const size_type len, const size_type nbytes, 
00239                 char *d, const EnumDataOwned o=DataNotOwned);
00241   void free();
00243   void set_shared_data(char *data, const size_type len_, 
00244                 const size_type array_len_, const EnumDataOwned o=DataNotOwned);
00246   virtual int element_size() const = 0;
00248   void initialize(const size_type curr_array_len, 
00249                         const size_type new_array_len);
00250 
00251 };
00252  
00253 
00254 #ifndef SWIG
00255 inline void BitArrayRef::copy_data(BitArrayBase* array)
00256 {
00257 array->array_len = array_len;
00258 ArrayRef<char,BitArrayBase>::copy_data(array);
00259 }
00260 
00261 inline void BitArrayRef::reset_data(BitArrayBase* array)
00262 {
00263 array->array_len = 0;
00264 ArrayRef<char,BitArrayBase>::copy_data(array);
00265 }
00266 
00267 inline UTILIB_API ostream& operator<<(ostream& output, const BitArrayBase& array)
00268 { array.write(output); return(output); }
00269  
00270 inline UTILIB_API istream& operator>>(istream& input, BitArrayBase& array)
00271 { array.read(input); return(input); }
00272  
00273 inline UTILIB_API PackBuffer& operator<<(PackBuffer& output, const BitArrayBase& array)
00274 { array.write(output); return(output); }
00275  
00276 inline UTILIB_API UnPackBuffer& operator>>(UnPackBuffer& input, BitArrayBase& array)
00277 { array.read(input); return(input); }
00278 #endif
00279 
00280 
00281 #endif