RDKit
Open-source cheminformatics and machine learning.
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1//
2// Copyright (C) 2002-2026 Greg Landrum and other RDKit contributors
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//
11#include <RDGeneral/export.h>
12#ifndef RD_UTILS_H
13#define RD_UTILS_H
14
15#include <cstdlib>
16
17#include "types.h"
18#include <RDGeneral/Invariant.h>
20#include <boost/random.hpp>
22
23#ifdef _WIN32
24inline int setenv(const char *name, const char *value, int) {
25 return _putenv_s(name, value);
26}
27#endif
28
29namespace RDKit {
30
32 1000; //!< the number of primes available and stored
34
35const int FILE_MAXLINE =
36 256; //!< an assumed maximum length for lines read from files
37
38//! \brief compute the product of the set of primes corresponding to the
39//! values in an INT_VECT
41
42//! floating point comparison with a tolerance
43RDKIT_RDGENERAL_EXPORT bool feq(double v1, double v2, double tol = 1e-4);
44
45typedef boost::minstd_rand rng_type;
46typedef boost::uniform_int<> uniform_int;
47typedef boost::uniform_real<> uniform_double;
48typedef boost::variate_generator<rng_type &, uniform_int> int_source_type;
49typedef boost::variate_generator<rng_type &, uniform_double> double_source_type;
50
51//! Optionally seed and return a reference to the global (Boost) random
52/// generator
54
55//! Return a random double value between 0.0 and 1.0
56//! Optionally seed the random number generator
58
59//! return a reference to the global (Boost) random source
61
62template <class T>
63unsigned int countSwapsToInterconvert(const T &ref, T probe) {
64 PRECONDITION(ref.size() == probe.size(), "size mismatch");
65 typename T::const_iterator refIt = ref.begin();
66 typename T::iterator probeIt = probe.begin();
67 typename T::iterator probeIt2;
68
69 unsigned int nSwaps = 0;
70 while (refIt != ref.end()) {
71 if ((*probeIt) != (*refIt)) {
72 bool foundIt = false;
73 probeIt2 = probeIt;
74 while (probeIt2 != probe.end() && (*probeIt2) != (*refIt)) {
75 ++probeIt2;
76 }
77 if (probeIt2 != probe.end()) {
78 foundIt = true;
79 }
80 CHECK_INVARIANT(foundIt, "could not find probe element");
81
82 std::swap(*probeIt, *probeIt2);
83 nSwaps++;
84 }
85 ++probeIt;
86 ++refIt;
87 }
88 return nSwaps;
89}
90
91RDKIT_RDGENERAL_EXPORT std::string augmentTagName(const std::string &tag);
92
93inline bool getValFromEnvironment(const char *var, bool defVal) {
94 auto evar = std::getenv(var);
95 if (evar != nullptr) {
96 if (!strcmp(evar, "0")) {
97 return false;
98 } else {
99 return true;
100 }
101 }
102 return defVal;
103}
104
105} // namespace RDKit
106
107// contribution from dkoes
108template <unsigned n>
109inline double int_pow(double x) {
110 double half = int_pow<n / 2>(x);
111 if (n % 2 == 0) { // even
112 return half * half;
113 } else {
114 return half * half * x;
115 }
116}
117
118template <>
119inline double int_pow<0>(double) {
120 return 1;
121}
122
123template <>
124inline double int_pow<1>(double x) {
125 return x; // this does a series of muls
126}
127
128#endif
#define CHECK_INVARIANT(expr, mess)
Definition Invariant.h:100
#define PRECONDITION(expr, mess)
Definition Invariant.h:108
#define RDKIT_RDGENERAL_EXPORT
Definition export.h:561
Std stuff.
std::vector< int > INT_VECT
Definition types.h:223
boost::uniform_int uniform_int
Definition utils.h:46
RDKIT_RDGENERAL_EXPORT bool feq(double v1, double v2, double tol=1e-4)
floating point comparison with a tolerance
boost::minstd_rand rng_type
Definition utils.h:45
boost::variate_generator< rng_type &, uniform_double > double_source_type
Definition utils.h:49
RDKIT_RDGENERAL_EXPORT double computeIntVectPrimesProduct(const INT_VECT &ring)
compute the product of the set of primes corresponding to the values in an INT_VECT
RDKIT_RDGENERAL_EXPORT double_source_type & getDoubleRandomSource()
return a reference to the global (Boost) random source
boost::variate_generator< rng_type &, uniform_int > int_source_type
Definition utils.h:48
RDKIT_RDGENERAL_EXPORT std::string augmentTagName(const std::string &tag)
RDKIT_RDGENERAL_EXPORT int firstThousandPrimes[NUM_PRIMES_AVAIL]
bool getValFromEnvironment(const char *var, bool defVal)
Definition utils.h:93
const int NUM_PRIMES_AVAIL
the number of primes available and stored
Definition utils.h:31
RDKIT_RDGENERAL_EXPORT rng_type & getRandomGenerator(int seed=-1)
unsigned int countSwapsToInterconvert(const T &ref, T probe)
Definition utils.h:63
boost::uniform_real uniform_double
Definition utils.h:47
const int FILE_MAXLINE
an assumed maximum length for lines read from files
Definition utils.h:35
RDKIT_RDGENERAL_EXPORT double getRandomVal(int seed=-1)
double int_pow(double x)
Definition utils.h:109
double int_pow< 0 >(double)
Definition utils.h:119
double int_pow< 1 >(double x)
Definition utils.h:124