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