DiscreteValueVect.h

Go to the documentation of this file.
00001 //
00002 //  Copyright (C) 2004-2008 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 #include <cstring>
00012 #include <boost/cstdint.hpp>
00013 
00014 namespace RDKit{
00015   // we require 32bit unsigneds using the boost::uint32_t type:
00016   const unsigned int BITS_PER_INT=32;
00017 
00018   //! a class for efficiently storing vectors of discrete values
00019   class DiscreteValueVect {
00020   public:
00021     typedef boost::shared_array<boost::uint32_t> DATA_SPTR;
00022   
00023     //! used to define the possible range of the values
00024     typedef enum {
00025       ONEBITVALUE=0,
00026       TWOBITVALUE,
00027       FOURBITVALUE,
00028       EIGHTBITVALUE,
00029       SIXTEENBITVALUE,
00030     } DiscreteValueType;
00031 
00032     //! initialize with a particular type and size
00033     DiscreteValueVect(DiscreteValueType valType, unsigned int length) : d_type(valType), d_length(length) {
00034       d_bitsPerVal = (1 << static_cast<unsigned int>(valType));
00035       d_valsPerInt = BITS_PER_INT/d_bitsPerVal;
00036       d_numInts = (length + d_valsPerInt -1)/d_valsPerInt;
00037       d_mask = ((1<<d_bitsPerVal) -1);
00038       boost::uint32_t *data = new boost::uint32_t[d_numInts];
00039       memset(static_cast<void *>(data),0,d_numInts*sizeof(boost::uint32_t));
00040       d_data.reset(data);
00041     }
00042 
00043     //! Copy constructor
00044     DiscreteValueVect(const DiscreteValueVect& other);
00045 
00046     //! constructor from a pickle
00047     DiscreteValueVect(const std::string pkl){
00048       initFromText(pkl.c_str(),pkl.size());
00049     };
00050     //! constructor from a pickle
00051     DiscreteValueVect(const char *pkl,const unsigned int len){
00052       initFromText(pkl,len);
00053     };
00054 
00055     ~DiscreteValueVect() {}
00056 
00057     //! return the value at an index
00058     unsigned int getVal(unsigned int i) const;
00059     //! set the value at an index
00060     /*!
00061       NOTE: it is an error to have val > the max value this
00062       DiscreteValueVect can accomodate 
00063     */
00064     void setVal(unsigned int i, unsigned int val);
00065 
00066     //! returns the sum of all the elements in the vect
00067     unsigned int getTotalVal() const;
00068 
00069     //! returns the length
00070     unsigned int getLength() const;
00071 
00072     //! return a pointer to our raw data storage
00073     const boost::uint32_t *getData() const;
00074 
00075     //! return the number of bits used to store each value
00076     unsigned int getNumBitsPerVal() const {
00077       return d_bitsPerVal;
00078     }
00079 
00080     //! return the type of value being stored
00081     DiscreteValueType getValueType() const {
00082       return d_type;
00083     }
00084 
00085     //! returns the size of our storage
00086     unsigned int getNumInts() const {
00087       return d_numInts;
00088     }
00089 
00090     //! support dvv3 = dvv1&dvv2
00091     /*!
00092 
00093        operator& returns the minimum value for each element.
00094        e.g.:
00095          [0,1,2,0] & [0,1,1,1] -> [0,1,1,0]
00096 
00097     */
00098     DiscreteValueVect operator& (const DiscreteValueVect &other) const;
00099     //! support dvv3 = dvv1|dvv2
00100     /*!
00101 
00102        operator& returns the maximum value for each element.
00103        e.g.:
00104          [0,1,2,0] | [0,1,1,1] -> [0,1,2,1]
00105 
00106     */
00107     DiscreteValueVect operator| (const DiscreteValueVect &other) const;
00108     //DiscreteValueVect operator^ (const DiscreteValueVect &other) const;
00109     //DiscreteValueVect operator~ () const;
00110 
00111 
00112     DiscreteValueVect& operator+=(const DiscreteValueVect &other);
00113     DiscreteValueVect& operator-=(const DiscreteValueVect &other);
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 Fri Apr 3 06:03:01 2009 for RDCode by  doxygen 1.5.6