DiscreteValueVect.h

Go to the documentation of this file.
00001 //
00002 //  Copyright (C) 2004-2007 Greg Landrum and Rational Discovery LLC
00003 //
00004 //  @@ All Rights Reserved @@
00005 //
00006 #ifndef __RD_DISCRETE_VALUE_VECT_20050124__
00007 #define __RD_DISCRETE_VALUE_VECT_20050124__
00008 
00009 #include <boost/smart_ptr.hpp>
00010 #include <string>
00011 
00012 namespace RDKit{
00013   // we are making an assumption here that an unsigned int is 32 bits long
00014   const unsigned int BITS_PER_INT=32;
00015 
00016   //! a class for efficiently storing vectors of discrete values
00017   class DiscreteValueVect {
00018   public:
00019     typedef boost::shared_array<unsigned int> DATA_SPTR;
00020   
00021     //! used to define the possible range of the values
00022     typedef enum {
00023       ONEBITVALUE=0,
00024       TWOBITVALUE,
00025       FOURBITVALUE,
00026       EIGHTBITVALUE,
00027       SIXTEENBITVALUE,
00028     } DiscreteValueType;
00029 
00030     //! initialize with a particular type and size
00031     DiscreteValueVect(DiscreteValueType valType, unsigned int length) : d_type(valType), d_length(length) {
00032       d_bitsPerVal = (1 << static_cast<unsigned int>(valType));
00033       d_valsPerInt = BITS_PER_INT/d_bitsPerVal;
00034       d_numInts = (length + d_valsPerInt -1)/d_valsPerInt;
00035       d_mask = ((1<<d_bitsPerVal) -1);
00036       unsigned int *data = new unsigned int[d_numInts];
00037       memset(static_cast<void *>(data),0,d_numInts*sizeof(unsigned int));
00038       d_data.reset(data);
00039     }
00040 
00041     //! Copy constructor
00042     DiscreteValueVect(const DiscreteValueVect& other);
00043 
00044     //! constructor from a pickle
00045     DiscreteValueVect(const std::string pkl){
00046       initFromText(pkl.c_str(),pkl.size());
00047     };
00048     //! constructor from a pickle
00049     DiscreteValueVect(const char *pkl,const unsigned int len){
00050       initFromText(pkl,len);
00051     };
00052 
00053     ~DiscreteValueVect() {}
00054 
00055     //! return the value at an index
00056     unsigned int getVal(unsigned int i) const;
00057     //! set the value at an index
00058     /*!
00059       NOTE: it is an error to have val > the max value this
00060       DiscreteValueVect can accomodate 
00061     */
00062     void setVal(unsigned int i, unsigned int val);
00063 
00064     //! returns the sum of all the elements in the vect
00065     unsigned int getTotalVal() const;
00066 
00067     //! returns the length
00068     unsigned int getLength() const;
00069 
00070     //! return a pointer to our raw data storage
00071     const unsigned int *getData() const;
00072 
00073     //! return the number of bits used to store each value
00074     unsigned int getNumBitsPerVal() const {
00075       return d_bitsPerVal;
00076     }
00077 
00078     //! return the type of value being stored
00079     DiscreteValueType getValueType() const {
00080       return d_type;
00081     }
00082 
00083     //! returns the size of our storage
00084     unsigned int getNumInts() const {
00085       return d_numInts;
00086     }
00087 
00088     //! support dvv3 = dvv1&dvv2
00089     /*!
00090 
00091        operator& returns the minimum value for each element.
00092        e.g.:
00093          [0,1,2,0] & [0,1,1,1] -> [0,1,1,0]
00094 
00095     */
00096     DiscreteValueVect operator& (const DiscreteValueVect &other) const;
00097     //! support dvv3 = dvv1|dvv2
00098     /*!
00099 
00100        operator& returns the maximum value for each element.
00101        e.g.:
00102          [0,1,2,0] | [0,1,1,1] -> [0,1,2,1]
00103 
00104     */
00105     DiscreteValueVect operator| (const DiscreteValueVect &other) const;
00106     //DiscreteValueVect operator^ (const DiscreteValueVect &other) const;
00107     //DiscreteValueVect operator~ () const;
00108 
00109 
00110     DiscreteValueVect& operator+=(const DiscreteValueVect &other);
00111     DiscreteValueVect& operator-=(const DiscreteValueVect &other);
00112 
00113 
00114 
00115     //! returns a binary string representation (pickle)
00116     std::string toString() const;
00117   private:
00118     DiscreteValueType d_type;
00119     unsigned int d_bitsPerVal;
00120     unsigned int d_valsPerInt;
00121     unsigned int d_numInts;
00122     unsigned int d_length;
00123     unsigned int d_mask;
00124     DATA_SPTR d_data;
00125 
00126     void initFromText(const char *pkl,const unsigned int len);
00127   };
00128 
00129   unsigned int computeL1Norm(const DiscreteValueVect &v1, const DiscreteValueVect &v2);
00130 
00131   DiscreteValueVect operator+ (const DiscreteValueVect& p1,
00132                                const DiscreteValueVect& p2);
00133   DiscreteValueVect operator- (const DiscreteValueVect& p1,
00134                                const DiscreteValueVect& p2);
00135 
00136 } 
00137 
00138 
00139 
00140 #endif

Generated on Sat May 24 08:36:32 2008 for RDCode by  doxygen 1.5.3