File: RealValueVect.h

package info (click to toggle)
rdkit 202503.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 220,160 kB
  • sloc: cpp: 399,240; python: 77,453; ansic: 25,517; java: 8,173; javascript: 4,005; sql: 2,389; yacc: 1,565; lex: 1,263; cs: 1,081; makefile: 580; xml: 229; fortran: 183; sh: 105
file content (137 lines) | stat: -rw-r--r-- 4,117 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//
//  Copyright (c) 2014-2024, Novartis Institutes for BioMedical Research and
//  other RDKit contributors
//
//   @@ All Rights Reserved @@
//  This file is part of the RDKit.
//  The contents are covered by the terms of the BSD license
//  which is included in the file license.txt, found at the root
//  of the RDKit source tree.
//
#include <RDGeneral/export.h>
#ifndef RD_REAL_VALUE_VECT_20140407
#define RD_REAL_VALUE_VECT_20140407

#include <boost/smart_ptr.hpp>
#include <string>
#include <cstring>

namespace RDGeom {
class UniformRealValueGrid3D;
}
namespace RDKit {

//! a class for efficiently storing vectors of double values
//! Has additional features compared to std::vector<double>:
//! construct from and write to pickle
class RDKIT_DATASTRUCTS_EXPORT RealValueVect {
 public:
  RealValueVect() = default;
  //! initialize with a particular size
  RealValueVect(unsigned int length) : d_length(length) {
    d_data.resize(d_length, 0.0);
  }

  //! initialize with a particular size and initial value
  RealValueVect(unsigned int length, double val) : d_length(length) {
    d_data.resize(d_length, val);
  }

  //! constructor from a pickle
  RealValueVect(const std::string &pkl) {
    initFromText(pkl.c_str(), pkl.size());
  };

  //! constructor from a pickle
  RealValueVect(const char *pkl, unsigned int len) { initFromText(pkl, len); };

  //! return the value at an index
  double getVal(unsigned int i) const;

  //! support indexing using []
  double operator[](unsigned int idx) const { return getVal(idx); };

  //! set the value at an index
  void setVal(unsigned int i, double val);

  //! returns the sum of all the elements in the vect
  double getTotalVal() const;

  //! returns the length
  unsigned int getLength() const { return d_length; };

  //! returns the length
  unsigned int size() const { return getLength(); };

  //! compares 2 vectors and returns false if different
  bool compareVectors(const RealValueVect &other) const;

  //! in-place operator&
  RealValueVect &operator&=(const RealValueVect &other);

  //! in-place operator|
  RealValueVect &operator|=(const RealValueVect &other);

  //! in-place operator+
  RealValueVect &operator+=(const RealValueVect &other);

  //! in-place operator-
  RealValueVect &operator-=(const RealValueVect &other);

  //! returns a binary string representation (pickle)
  std::string toString() const;

  void setLength(unsigned int sz) {
    d_length = sz;
    d_data.resize(sz);
  }
  void setToVal(double val) { std::fill(d_data.begin(), d_data.end(), val); }

  const std::vector<double> &getData() const { return d_data; }
  std::vector<double> &getData() { return d_data; }

 private:
  void initFromText(const char *pkl, const unsigned int len);
  unsigned int d_length = 0;
  std::vector<double> d_data;

  template <typename O>
  RealValueVect &applyBinaryOp(const RealValueVect &other, O op);
};  // end of declaration of class RealValueVect

//! returns L1 Norm of vectors
RDKIT_DATASTRUCTS_EXPORT double computeL1Norm(const RealValueVect &v1,
                                              const RealValueVect &v2);

//! returns the sum of vectors
RDKIT_DATASTRUCTS_EXPORT RealValueVect operator+(const RealValueVect &p1,
                                                 const RealValueVect &p2);

//! returns the difference of vectors
RDKIT_DATASTRUCTS_EXPORT RealValueVect operator-(const RealValueVect &p1,
                                                 const RealValueVect &p2);

//! support rvv3 = rvv1|rvv2
/*!

   operator| returns the maximum value for each element.
   e.g.:
     [0,1,2,0] | [0,1,1,1] -> [0,1,2,1]

 */
RDKIT_DATASTRUCTS_EXPORT RealValueVect operator|(const RealValueVect &p1,
                                                 const RealValueVect &p2);

//! support rvv3 = rvv1&rvv2
/*!
   operator& returns the minimum value for each element.
   e.g.:
     [0,1,2,0] & [0,1,1,1] -> [0,1,1,0]

 */
RDKIT_DATASTRUCTS_EXPORT RealValueVect operator&(const RealValueVect &p1,
                                                 const RealValueVect &p2);

}  // end of namespace RDKit

#endif