RDKit
Open-source cheminformatics and machine learning.
Toggle main menu visibility
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
>
19
#include <
RDGeneral/BoostStartInclude.h
>
20
#include <boost/random.hpp>
21
#include <
RDGeneral/BoostEndInclude.h
>
22
23
#ifdef _WIN32
24
inline
int
setenv(
const
char
*name,
const
char
*value,
int
) {
25
return
_putenv_s(name, value);
26
}
27
#endif
28
29
namespace
RDKit
{
30
31
const
int
NUM_PRIMES_AVAIL
=
32
1000;
//!< the number of primes available and stored
33
RDKIT_RDGENERAL_EXPORT
extern
int
firstThousandPrimes
[
NUM_PRIMES_AVAIL
];
34
35
const
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
40
RDKIT_RDGENERAL_EXPORT
double
computeIntVectPrimesProduct
(
const
INT_VECT
&ring);
41
42
//! floating point comparison with a tolerance
43
RDKIT_RDGENERAL_EXPORT
bool
feq
(
double
v1
,
double
v2
,
double
tol = 1e-4);
44
45
typedef
boost::minstd_rand
rng_type
;
46
typedef
boost::uniform_int<>
uniform_int
;
47
typedef
boost::uniform_real<>
uniform_double
;
48
typedef
boost::variate_generator<rng_type &, uniform_int>
int_source_type
;
49
typedef
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
53
RDKIT_RDGENERAL_EXPORT
rng_type
&
getRandomGenerator
(
int
seed = -1);
54
55
//! Return a random double value between 0.0 and 1.0
56
//! Optionally seed the random number generator
57
RDKIT_RDGENERAL_EXPORT
double
getRandomVal
(
int
seed = -1);
58
59
//! return a reference to the global (Boost) random source
60
RDKIT_RDGENERAL_EXPORT
double_source_type
&
getDoubleRandomSource
();
61
62
template
<
class
T>
63
unsigned
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
91
RDKIT_RDGENERAL_EXPORT
std::string
augmentTagName
(
const
std::string &tag);
92
93
inline
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
108
template
<
unsigned
n>
109
inline
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
118
template
<>
119
inline
double
int_pow<0>
(
double
) {
120
return
1;
121
}
122
123
template
<>
124
inline
double
int_pow<1>
(
double
x) {
125
return
x;
// this does a series of muls
126
}
127
128
#endif
BoostEndInclude.h
BoostStartInclude.h
Invariant.h
CHECK_INVARIANT
#define CHECK_INVARIANT(expr, mess)
Definition
Invariant.h:100
PRECONDITION
#define PRECONDITION(expr, mess)
Definition
Invariant.h:108
export.h
RDKIT_RDGENERAL_EXPORT
#define RDKIT_RDGENERAL_EXPORT
Definition
export.h:561
RDKit::v1
Definition
ReactionParser.h:91
RDKit::v2
Definition
ReactionParser.h:71
RDKit
Std stuff.
Definition
Abbreviations.h:19
RDKit::INT_VECT
std::vector< int > INT_VECT
Definition
types.h:223
RDKit::uniform_int
boost::uniform_int uniform_int
Definition
utils.h:46
RDKit::feq
RDKIT_RDGENERAL_EXPORT bool feq(double v1, double v2, double tol=1e-4)
floating point comparison with a tolerance
RDKit::rng_type
boost::minstd_rand rng_type
Definition
utils.h:45
RDKit::double_source_type
boost::variate_generator< rng_type &, uniform_double > double_source_type
Definition
utils.h:49
RDKit::computeIntVectPrimesProduct
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::getDoubleRandomSource
RDKIT_RDGENERAL_EXPORT double_source_type & getDoubleRandomSource()
return a reference to the global (Boost) random source
RDKit::int_source_type
boost::variate_generator< rng_type &, uniform_int > int_source_type
Definition
utils.h:48
RDKit::augmentTagName
RDKIT_RDGENERAL_EXPORT std::string augmentTagName(const std::string &tag)
RDKit::firstThousandPrimes
RDKIT_RDGENERAL_EXPORT int firstThousandPrimes[NUM_PRIMES_AVAIL]
RDKit::getValFromEnvironment
bool getValFromEnvironment(const char *var, bool defVal)
Definition
utils.h:93
RDKit::NUM_PRIMES_AVAIL
const int NUM_PRIMES_AVAIL
the number of primes available and stored
Definition
utils.h:31
RDKit::getRandomGenerator
RDKIT_RDGENERAL_EXPORT rng_type & getRandomGenerator(int seed=-1)
RDKit::countSwapsToInterconvert
unsigned int countSwapsToInterconvert(const T &ref, T probe)
Definition
utils.h:63
RDKit::uniform_double
boost::uniform_real uniform_double
Definition
utils.h:47
RDKit::FILE_MAXLINE
const int FILE_MAXLINE
an assumed maximum length for lines read from files
Definition
utils.h:35
RDKit::getRandomVal
RDKIT_RDGENERAL_EXPORT double getRandomVal(int seed=-1)
types.h
int_pow
double int_pow(double x)
Definition
utils.h:109
int_pow< 0 >
double int_pow< 0 >(double)
Definition
utils.h:119
int_pow< 1 >
double int_pow< 1 >(double x)
Definition
utils.h:124
RDGeneral
utils.h
Generated on
for RDKit by
1.17.0