RDKit: DiscreteValueVect.h Source File

| RDKit Open-source cheminformatics and machine learning.

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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
23 class DiscreteValueVect {
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 {
29 ONEBITVALUE = 0,
30 TWOBITVALUE,
31 FOURBITVALUE,
32 EIGHTBITVALUE,
33 SIXTEENBITVALUE,
34 } DiscreteValueType;
35
36 //! initialize with a particular type and size
37 DiscreteValueVect(DiscreteValueType valType, unsigned int length)
38 : d_type(valType), d_length(length) {
39 d_bitsPerVal = (1 << static_cast<unsigned int>(valType));
40 d_valsPerInt = BITS_PER_INT / d_bitsPerVal;
41 d_numInts = (length + d_valsPerInt - 1) / d_valsPerInt;
42 d_mask = ((1 << d_bitsPerVal) - 1);
43 boost::uint32_t *data = new boost::uint32_t[d_numInts];
44 memset(static_cast<void *>(data), 0, d_numInts * sizeof(boost::uint32_t));
45 d_data.reset(data);
46 }
47
48 //! Copy constructor
49 DiscreteValueVect(const DiscreteValueVect &other);
50
51 //! constructor from a pickle
52 DiscreteValueVect(const std::string &pkl) {
53 initFromText(pkl.c_str(), pkl.size());
54 };
55 //! constructor from a pickle
56 DiscreteValueVect(const char *pkl, const unsigned int len) {
57 initFromText(pkl, len);
58 };
59
60 ~DiscreteValueVect() {}
61
62 //! return the value at an index
63 unsigned int getVal(unsigned int i) const;
64
65 //! support indexing using []
66 int operator[](unsigned int idx) const { return getVal(idx); };
67
68 //! set the value at an index
69 /*!
70 NOTE: it is an error to have val > the max value this
71 DiscreteValueVect can accomodate
72 */
73 void setVal(unsigned int i, unsigned int val);
74
75 //! returns the sum of all the elements in the vect
76 unsigned int getTotalVal() const;
77
78 //! returns the length
79 unsigned int getLength() const;
80 //! returns the length
81 unsigned int size() const { return getLength(); };
82
83 //! return a pointer to our raw data storage
84 const boost::uint32_t *getData() const;
85
86 //! return the number of bits used to store each value
87 unsigned int getNumBitsPerVal() const { return d_bitsPerVal; }
88
89 //! return the type of value being stored
90 DiscreteValueType getValueType() const { return d_type; }
91
92 //! returns the size of our storage
93 unsigned int getNumInts() const { return d_numInts; }
94
95 //! support dvv3 = dvv1&dvv2
96 /*!
97
98 operator& returns the minimum value for each element.
99 e.g.:
100 [0,1,2,0] & [0,1,1,1] -> [0,1,1,0]
101
102 */
103 DiscreteValueVect operator&(const DiscreteValueVect &other) const;
104 //! support dvv3 = dvv1|dvv2
105 /*!
106
107 operator& returns the maximum value for each element.
108 e.g.:
109 [0,1,2,0] | [0,1,1,1] -> [0,1,2,1]
110
111 */
112 DiscreteValueVect operator|(const DiscreteValueVect &other) const;
113 // DiscreteValueVect operator^ (const DiscreteValueVect &other) const;
114 // DiscreteValueVect operator~ () const;
115
116 DiscreteValueVect &operator+=(const DiscreteValueVect &other);
117 DiscreteValueVect &operator-=(const DiscreteValueVect &other);
118
119 //! returns a binary string representation (pickle)
120 std::string toString() const;
121
122 private:
123 DiscreteValueType d_type;
124 unsigned int d_bitsPerVal;
125 unsigned int d_valsPerInt;
126 unsigned int d_numInts;
127 unsigned int d_length;
128 unsigned int d_mask;
129 DATA_SPTR d_data;
130
131 void initFromText(const char *pkl, const unsigned int len);
132 };
133
134 unsigned int computeL1Norm(const DiscreteValueVect &v1,
135 const DiscreteValueVect &v2);
136
137 DiscreteValueVect operator+(const DiscreteValueVect &p1,
138 const DiscreteValueVect &p2);
139 DiscreteValueVect operator-(const DiscreteValueVect &p1,
140 const DiscreteValueVect &p2);
141 }
142
143 #endif
RDKit::DiscreteValueVect::getVal
unsigned int getVal(unsigned int i) const
return the value at an index
RDKit::DiscreteValueVect::getNumInts
unsigned int getNumInts() const
returns the size of our storage
Definition: DiscreteValueVect.h:93
RDKit::DiscreteValueVect::DiscreteValueType
DiscreteValueType
used to define the possible range of the values
Definition: DiscreteValueVect.h:28
RDKit::DiscreteValueVect::DATA_SPTR
boost::shared_array< boost::uint32_t > DATA_SPTR
Definition: DiscreteValueVect.h:25
RDKit::DiscreteValueVect::getData
const boost::uint32_t * getData() const
return a pointer to our raw data storage
RDKit::DiscreteValueVect::SIXTEENBITVALUE
Definition: DiscreteValueVect.h:33
RDKit::DiscreteValueVect::operator|
DiscreteValueVect operator|(const DiscreteValueVect &other) const
support dvv3 = dvv1|dvv2
RDKit::DiscreteValueVect::DiscreteValueVect
DiscreteValueVect(DiscreteValueType valType, unsigned int length)
initialize with a particular type and size
Definition: DiscreteValueVect.h:37
RDKit::DiscreteValueVect::FOURBITVALUE
Definition: DiscreteValueVect.h:31
RDKit::DiscreteValueVect::size
unsigned int size() const
returns the length
Definition: DiscreteValueVect.h:81
RDKit::DiscreteValueVect::toString
std::string toString() const
returns a binary string representation (pickle)
RDKit::DiscreteValueVect::EIGHTBITVALUE
Definition: DiscreteValueVect.h:32
RDKit::DiscreteValueVect::setVal
void setVal(unsigned int i, unsigned int val)
set the value at an index
RDKit::DiscreteValueVect::DiscreteValueVect
DiscreteValueVect(const std::string &pkl)
constructor from a pickle
Definition: DiscreteValueVect.h:52
RDKit::DiscreteValueVect
a class for efficiently storing vectors of discrete values
Definition: DiscreteValueVect.h:23
RDKit::DiscreteValueVect::operator-=
DiscreteValueVect & operator-=(const DiscreteValueVect &other)
RDKit::DiscreteValueVect::operator+=
DiscreteValueVect & operator+=(const DiscreteValueVect &other)
RDKit::DiscreteValueVect::getLength
unsigned int getLength() const
returns the length
RDKit::DiscreteValueVect::operator[]
int operator[](unsigned int idx) const
support indexing using []
Definition: DiscreteValueVect.h:66
RDKit::DiscreteValueVect::DiscreteValueVect
DiscreteValueVect(const char *pkl, const unsigned int len)
constructor from a pickle
Definition: DiscreteValueVect.h:56
RDKit::DiscreteValueVect::TWOBITVALUE
Definition: DiscreteValueVect.h:30
RDKit
Includes a bunch of functionality for handling Atom and Bond queries.
Definition: Atom.h:28
RDKit::operator-
DiscreteValueVect operator-(const DiscreteValueVect &p1, const DiscreteValueVect &p2)
RDKit::DiscreteValueVect::~DiscreteValueVect
~DiscreteValueVect()
Definition: DiscreteValueVect.h:60
RDKit::DiscreteValueVect::ONEBITVALUE
Definition: DiscreteValueVect.h:29
RDKit::operator+
DiscreteValueVect operator+(const DiscreteValueVect &p1, const DiscreteValueVect &p2)
RDKit::DiscreteValueVect::operator&
DiscreteValueVect operator&(const DiscreteValueVect &other) const
support dvv3 = dvv1&dvv2
RDKit::DiscreteValueVect::getNumBitsPerVal
unsigned int getNumBitsPerVal() const
return the number of bits used to store each value
Definition: DiscreteValueVect.h:87
RDKit::BITS_PER_INT
const unsigned int BITS_PER_INT
Definition: DiscreteValueVect.h:20
RDKit::DiscreteValueVect::getTotalVal
unsigned int getTotalVal() const
returns the sum of all the elements in the vect
RDKit::computeL1Norm
unsigned int computeL1Norm(const DiscreteValueVect &v1, const DiscreteValueVect &v2)
RDKit::DiscreteValueVect::getValueType
DiscreteValueType getValueType() const
return the type of value being stored
Definition: DiscreteValueVect.h:90