MixedIntVars.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 // MixedIntVars.h
00012 //
00027 #ifndef __MixedIntVars_h
00028 #define __MixedIntVars_h
00029 
00030 #ifdef __GNUC__
00031 #pragma interface
00032 #endif
00033 
00034 #ifdef NON_ANSI
00035 #include <iostream.h>
00036 #else
00037 #include <iostream>
00038 using namespace std;
00039 #endif
00040 
00041 #include "BitArray.h"
00042 #include "IntVector.h"
00043 #include "DoubleVector.h"
00044 
00051 class UTILIB_API MixedIntVarsRep {
00052  
00053   friend class MixedIntVars;
00054 
00055 protected:
00056  
00058   MixedIntVarsRep(int nBinVars=0, int nIntVars=0, int nDblVars=0) 
00059         : ref(1), 
00060         own(AssumeOwnership),
00061         NumBinaryVars(nBinVars), 
00062         NumIntegerVars(nIntVars),
00063         NumVars(nBinVars+nIntVars+nDblVars),
00064         bitVars(nBinVars), gintVars(nIntVars), doubleVars(nDblVars)
00065         {}
00066 
00068   int ref;
00070   EnumDataOwned own;
00071 
00073   int NumBinaryVars;
00075   int NumIntegerVars;
00077   int NumVars;
00078 
00080   BitArray bitVars;
00082   IntVector gintVars;
00084   DoubleVector doubleVars;
00085 
00086 };
00087 
00088 
00089 
00090 class UTILIB_API MixedIntVars 
00091 {
00092 public:
00093 
00095   MixedIntVars(const int numBits=0, const int numGenInts=0, 
00096                         const int numDoubles=0); 
00098   virtual ~MixedIntVars()
00099                 {free();}
00100 
00102   void resize(const int numBits, const int numGenInts, const int numDoubles);
00104   int nrefs() const
00105         {return (a ? a->ref : 0);}
00106 
00108   int numIntegerVars() const
00109                 { return (a ? a->NumIntegerVars : 0); }
00111   int numBinaryVars() const
00112                 { return (a ? a->NumBinaryVars : 0); }
00114   int numGeneralIntVars() const
00115                 { return (a ? a->NumIntegerVars - a->NumBinaryVars : 0); }
00117   int numDoubles() const
00118                 { return (a ? a->NumVars - a->NumIntegerVars : 0); }
00120   int numAllVars() const
00121                 { return (a ? a->NumVars : 0); }
00122 
00124   void setBit(const int ndx, int val);
00126   void setGeneralInt(const int ndx, int val);
00132   void setInt(const int ndx, int val);
00134   void setDouble(const int ndx, double val);
00140   void set(const int ndx, double val);
00141 
00143   int getBit(const int ndx);
00145   int getGeneralInt(const int ndx);
00151   int getInt(const int ndx);
00153   double getDouble(const int ndx);
00159   double get(const int ndx);
00160 
00162   MixedIntVars& operator=(const MixedIntVars& array);
00164   MixedIntVars& operator&=(const MixedIntVars& array);
00166   MixedIntVars& operator<<(const MixedIntVars& array);
00168   MixedIntVars& operator=(const char val);
00170   MixedIntVars& operator=(const int val);
00172   MixedIntVars& operator=(const double val);
00173 
00175   int operator==(const MixedIntVars& array) const;
00182   int compare(const MixedIntVars& array) const;
00183 
00185   int write(ostream& os) const;
00187   int read(istream& is);
00189   int write(PackBuffer& os) const;
00191   int read(UnPackBuffer& is);
00192 
00193 protected:
00194 
00196   MixedIntVarsRep* a;
00197 
00199   void construct(const int nbits, const int nints, const int ndlbs);
00201   void free();
00202 
00203 };
00204 
00205 
00206 inline UTILIB_API PackBuffer& operator<< (PackBuffer& buff, 
00207                                                 const MixedIntVars& vars)
00208         { vars.write(buff); return buff; }
00209 
00210 inline UTILIB_API UnPackBuffer& operator>> (UnPackBuffer& buff, 
00211                                                 MixedIntVars& vars)
00212         { vars.read(buff); return buff;}   
00213 
00214 inline UTILIB_API ostream& operator<< (ostream& buff, const MixedIntVars& vars)
00215         { vars.write(buff); return buff; }
00216 
00217 inline UTILIB_API istream& operator>> (istream& buff, MixedIntVars& vars)
00218         { vars.read(buff); return buff;}   
00219 #endif