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