RDKit
Open-source cheminformatics and machine learning.
DiscreteValueVect.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2004-2008 Greg Landrum and Rational Discovery LLC
3 //
4 // @@ All Rights Reserved @@
5 // This file is part of the RDKit.
6 // The contents are covered by the terms of the BSD license
7 // which is included in the file license.txt, found at the root
8 // of the RDKit source tree.
9 //
10 #ifndef __RD_DISCRETE_VALUE_VECT_20050124__
11 #define __RD_DISCRETE_VALUE_VECT_20050124__
12 
13 #include <boost/smart_ptr.hpp>
14 #include <string>
15 #include <cstring>
16 #include <boost/cstdint.hpp>
17 
18 namespace RDKit{
19  // we require 32bit unsigneds using the boost::uint32_t type:
20  const unsigned int BITS_PER_INT=32;
21 
22  //! a class for efficiently storing vectors of discrete values
24  public:
25  typedef boost::shared_array<boost::uint32_t> DATA_SPTR;
26 
27  //! used to define the possible range of the values
28  typedef enum {
35 
36  //! initialize with a particular type and size
37  DiscreteValueVect(DiscreteValueType valType, unsigned int length) : d_type(valType), d_length(length) {
38  d_bitsPerVal = (1 << static_cast<unsigned int>(valType));
39  d_valsPerInt = BITS_PER_INT/d_bitsPerVal;
40  d_numInts = (length + d_valsPerInt -1)/d_valsPerInt;
41  d_mask = ((1<<d_bitsPerVal) -1);
42  boost::uint32_t *data = new boost::uint32_t[d_numInts];
43  memset(static_cast<void *>(data),0,d_numInts*sizeof(boost::uint32_t));
44  d_data.reset(data);
45  }
46 
47  //! Copy constructor
49 
50  //! constructor from a pickle
51  DiscreteValueVect(const std::string pkl){
52  initFromText(pkl.c_str(),pkl.size());
53  };
54  //! constructor from a pickle
55  DiscreteValueVect(const char *pkl,const unsigned int len){
56  initFromText(pkl,len);
57  };
58 
60 
61  //! return the value at an index
62  unsigned int getVal(unsigned int i) const;
63 
64  //! support indexing using []
65  int operator[] (unsigned int idx) const { return getVal(idx); };
66 
67  //! set the value at an index
68  /*!
69  NOTE: it is an error to have val > the max value this
70  DiscreteValueVect can accomodate
71  */
72  void setVal(unsigned int i, unsigned int val);
73 
74  //! returns the sum of all the elements in the vect
75  unsigned int getTotalVal() const;
76 
77  //! returns the length
78  unsigned int getLength() const;
79  //! returns the length
80  unsigned int size() const { return getLength(); };
81 
82  //! return a pointer to our raw data storage
83  const boost::uint32_t *getData() const;
84 
85  //! return the number of bits used to store each value
86  unsigned int getNumBitsPerVal() const {
87  return d_bitsPerVal;
88  }
89 
90  //! return the type of value being stored
91  DiscreteValueType getValueType() const {
92  return d_type;
93  }
94 
95  //! returns the size of our storage
96  unsigned int getNumInts() const {
97  return d_numInts;
98  }
99 
100  //! support dvv3 = dvv1&dvv2
101  /*!
102 
103  operator& returns the minimum value for each element.
104  e.g.:
105  [0,1,2,0] & [0,1,1,1] -> [0,1,1,0]
106 
107  */
108  DiscreteValueVect operator& (const DiscreteValueVect &other) const;
109  //! support dvv3 = dvv1|dvv2
110  /*!
111 
112  operator& returns the maximum value for each element.
113  e.g.:
114  [0,1,2,0] | [0,1,1,1] -> [0,1,2,1]
115 
116  */
117  DiscreteValueVect operator| (const DiscreteValueVect &other) const;
118  //DiscreteValueVect operator^ (const DiscreteValueVect &other) const;
119  //DiscreteValueVect operator~ () const;
120 
121 
124 
125  //! returns a binary string representation (pickle)
126  std::string toString() const;
127 
128  private:
129  DiscreteValueType d_type;
130  unsigned int d_bitsPerVal;
131  unsigned int d_valsPerInt;
132  unsigned int d_numInts;
133  unsigned int d_length;
134  unsigned int d_mask;
135  DATA_SPTR d_data;
136 
137  void initFromText(const char *pkl,const unsigned int len);
138  };
139 
140  unsigned int computeL1Norm(const DiscreteValueVect &v1, const DiscreteValueVect &v2);
141 
143  const DiscreteValueVect& p2);
145  const DiscreteValueVect& p2);
146 
147 }
148 
149 
150 
151 #endif
unsigned int getVal(unsigned int i) const
return the value at an index
unsigned int getNumInts() const
returns the size of our storage
DiscreteValueType
used to define the possible range of the values
boost::shared_array< boost::uint32_t > DATA_SPTR
const boost::uint32_t * getData() const
return a pointer to our raw data storage
DiscreteValueVect operator|(const DiscreteValueVect &other) const
support dvv3 = dvv1|dvv2
DiscreteValueVect(DiscreteValueType valType, unsigned int length)
initialize with a particular type and size
unsigned int size() const
returns the length
DiscreteValueVect(const std::string pkl)
constructor from a pickle
std::string toString() const
returns a binary string representation (pickle)
void setVal(unsigned int i, unsigned int val)
set the value at an index
a class for efficiently storing vectors of discrete values
DiscreteValueVect & operator-=(const DiscreteValueVect &other)
DiscreteValueVect & operator+=(const DiscreteValueVect &other)
unsigned int getLength() const
returns the length
int operator[](unsigned int idx) const
support indexing using []
DiscreteValueVect(const char *pkl, const unsigned int len)
constructor from a pickle
Includes a bunch of functionality for handling Atom and Bond queries.
Definition: Atom.h:28
DiscreteValueVect operator-(const DiscreteValueVect &p1, const DiscreteValueVect &p2)
DiscreteValueVect operator+(const DiscreteValueVect &p1, const DiscreteValueVect &p2)
DiscreteValueVect operator&(const DiscreteValueVect &other) const
support dvv3 = dvv1&dvv2
unsigned int getNumBitsPerVal() const
return the number of bits used to store each value
const unsigned int BITS_PER_INT
unsigned int getTotalVal() const
returns the sum of all the elements in the vect
unsigned int computeL1Norm(const DiscreteValueVect &v1, const DiscreteValueVect &v2)
DiscreteValueType getValueType() const
return the type of value being stored