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 
53 
54  //! constructor from a pickle
55  DiscreteValueVect(const std::string &pkl) {
56  initFromText(pkl.c_str(), static_cast<unsigned int>(pkl.size()));
57  }
58  //! constructor from a pickle
59  DiscreteValueVect(const char *pkl, const unsigned int len) {
60  initFromText(pkl, len);
61  }
62 
63  ~DiscreteValueVect() = default;
64 
65  //! return the value at an index
66  unsigned int getVal(unsigned int i) const;
67 
68  //! support indexing using []
69  int operator[](unsigned int idx) const { return getVal(idx); }
70 
71  //! set the value at an index
72  /*!
73  NOTE: it is an error to have val > the max value this
74  DiscreteValueVect can accommodate
75  */
76  void setVal(unsigned int i, unsigned int val);
77 
78  //! returns the sum of all the elements in the vect
79  unsigned int getTotalVal() const;
80 
81  //! returns the length
82  unsigned int getLength() const;
83  //! returns the length
84  unsigned int size() const { return getLength(); }
85 
86  //! return a pointer to our raw data storage
87  const std::uint32_t *getData() const;
88 
89  //! return the number of bits used to store each value
90  unsigned int getNumBitsPerVal() const { return d_bitsPerVal; }
91 
92  //! return the type of value being stored
93  DiscreteValueType getValueType() const { return d_type; }
94 
95  //! returns the size of our storage
96  unsigned int getNumInts() const { return d_numInts; }
97 
98  //! support dvv3 = dvv1&dvv2
99  /*!
100 
101  operator& returns the minimum value for each element.
102  e.g.:
103  [0,1,2,0] & [0,1,1,1] -> [0,1,1,0]
104 
105  */
107  //! support dvv3 = dvv1|dvv2
108  /*!
109 
110  operator& returns the maximum value for each element.
111  e.g.:
112  [0,1,2,0] | [0,1,1,1] -> [0,1,2,1]
113 
114  */
116  // DiscreteValueVect operator^ (const DiscreteValueVect &other) const;
117  // DiscreteValueVect operator~ () const;
118 
121 
122  //! returns a binary string representation (pickle)
123  std::string toString() const;
124 
125  private:
126  DiscreteValueType d_type;
127  unsigned int d_bitsPerVal;
128  unsigned int d_valsPerInt;
129  unsigned int d_numInts;
130  unsigned int d_length;
131  unsigned int d_mask;
132  DATA_SPTR d_data;
133 
134  void initFromText(const char *pkl, const unsigned int len);
135 };
136 
138  const DiscreteValueVect &v1, const DiscreteValueVect &v2);
139 
144 } // namespace RDKit
145 
146 #endif
a class for efficiently storing vectors of discrete values
unsigned int size() const
returns the length
DiscreteValueVect(const std::string &pkl)
constructor from a pickle
DiscreteValueVect(const DiscreteValueVect &other)
Copy constructor.
DiscreteValueVect & operator-=(const DiscreteValueVect &other)
std::string toString() const
returns a binary string representation (pickle)
unsigned int getVal(unsigned int i) const
return the value at an index
unsigned int getLength() const
returns the length
DiscreteValueType
used to define the possible range of the values
DiscreteValueVect & operator+=(const DiscreteValueVect &other)
unsigned int getTotalVal() const
returns the sum of all the elements in the vect
DiscreteValueVect operator|(const DiscreteValueVect &other) const
support dvv3 = dvv1|dvv2
DiscreteValueVect(DiscreteValueType valType, unsigned int length)
initialize with a particular type and size
DiscreteValueVect operator&(const DiscreteValueVect &other) const
support dvv3 = dvv1&dvv2
boost::shared_array< std::uint32_t > DATA_SPTR
DiscreteValueType getValueType() const
return the type of value being stored
unsigned int getNumInts() const
returns the size of our storage
int operator[](unsigned int idx) const
support indexing using []
unsigned int getNumBitsPerVal() const
return the number of bits used to store each value
DiscreteValueVect(const char *pkl, const unsigned int len)
constructor from a pickle
DiscreteValueVect & operator=(const DiscreteValueVect &other)
void setVal(unsigned int i, unsigned int val)
set the value at an index
const std::uint32_t * getData() const
return a pointer to our raw data storage
#define RDKIT_DATASTRUCTS_EXPORT
Definition: export.h:81
Std stuff.
Definition: Abbreviations.h:19
RDKIT_DATASTRUCTS_EXPORT unsigned int computeL1Norm(const DiscreteValueVect &v1, const DiscreteValueVect &v2)
RDKIT_DATASTRUCTS_EXPORT DiscreteValueVect operator-(const DiscreteValueVect &p1, const DiscreteValueVect &p2)
RDKIT_DATASTRUCTS_EXPORT DiscreteValueVect operator+(const DiscreteValueVect &p1, const DiscreteValueVect &p2)
const unsigned int BITS_PER_INT