Open
Graph Drawing
Framework

 v. 2022.02 (Dogwood)
 

basic.h
Go to the documentation of this file.
1 
32 #pragma once
33 
35 
38 
41 #define OGDF_ASSERT(expr)
42 #define OGDF_HEAVY_ASSERT(expr)
45 
46 #ifdef OGDF_DEBUG
47 # ifdef OGDF_HEAVY_DEBUG
48 # undef OGDF_HEAVY_ASSERT
49 # define OGDF_HEAVY_ASSERT(expr) OGDF_ASSERT(expr)
50 # endif
51 # undef OGDF_ASSERT
52 # ifndef OGDF_USE_ASSERT_EXCEPTIONS
53 # include <cassert>
54 # define OGDF_ASSERT(expr) assert(expr)
55 # else
56 # include <stdexcept>
57 # include <sstream>
58 
59 namespace ogdf {
64 class AssertionFailed : public std::runtime_error {
65  using std::runtime_error::runtime_error;
66 };
67 }
68 
69 # define OGDF_ASSERT(expr) do { \
70  if (!(expr)) { \
71  std::stringstream ogdf_assert_ss; \
72  ogdf_assert_ss \
73  << "OGDF assertion `" #expr "' failed at " __FILE__ ":" \
74  << __LINE__ \
75  << "(" << OGDF_FUNCTION_NAME << ")"; \
76  ogdf::get_stacktrace(ogdf_assert_ss); \
77  throw ogdf::AssertionFailed(ogdf_assert_ss.str()); \
78  } } while (false)
79 # endif
80 #endif
81 
83 
84 // g++ 4.8/4.9 does not have is_trivially_copyable,
85 // but clang 3.5 (which is also __GNUC__ < 5) has it.
86 #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 5
87 #define OGDF_TRIVIALLY_COPYABLE std::has_trivial_copy_assign
88 #else
89 #define OGDF_TRIVIALLY_COPYABLE std::is_trivially_copyable
90 #endif
91 
92 #include <cstdint>
93 #include <cmath>
94 #include <ctime>
95 #include <fstream>
96 #include <algorithm>
97 #include <limits>
98 
100 namespace ogdf {
101 
103 OGDF_EXPORT extern bool debugMode;
104 
105 // generally used <algorithm> members
106 using std::min;
107 using std::max;
108 
114 public:
115  Initialization();
116  ~Initialization();
117 };
118 
119 // OGDF_INSTALL is defined only when compiling a dynamic library.
120 // Whenever this library is used we can safely assume that this header
121 // file is included beforehand.
122 // Thus, there will be a static initialization object in the program that uses the library.
123 #ifndef OGDF_INSTALL
124 // This has to be in the header file. Being in the cpp file does not
125 // guarantee that it is constructed (with all linkers).
127 #endif
128 
129 #ifdef OGDF_USE_ASSERT_EXCEPTIONS
130 extern void get_stacktrace(std::ostream &);
132 #endif
133 
134 enum class Direction { before, after };
135 
138 
140 
144 OGDF_EXPORT long unsigned int randomSeed();
145 
147 OGDF_EXPORT void setSeed(int val);
148 
150 
154 OGDF_EXPORT int randomNumber(int low, int high);
155 
157 
161 OGDF_EXPORT double randomDouble(double low, double high);
162 
165 inline double randomDoubleNormal(double m, double sd)
166 {
167  double x1, y1, w;
168 
169  do {
170  double rndVal = randomDouble(0,1);
171  x1 = 2.0 * rndVal - 1.0;
172  rndVal = randomDouble(0,1);
173  double x2 = 2.0 * rndVal - 1.0;
174  w = x1*x1 + x2*x2;
175  } while (w >= 1.0);
176 
177  w = sqrt((-2.0 * log(w))/w) ;
178  y1 = x1*w;
179 
180  return m + y1 * sd;
181 }
182 
184 
188 OGDF_EXPORT double randomDoubleExponential(double beta);
189 
191 
193 
196 OGDF_EXPORT double usedTime(double& T);
197 
199 OGDF_EXPORT void removeTrailingWhitespace(string &str);
200 
202 OGDF_EXPORT bool equalIgnoreCase(const string &str1, const string &str2);
203 
205 OGDF_EXPORT bool prefixIgnoreCase(const string &prefix, const string &str);
206 
209 
211 
220 template< typename CONTAINER, typename T>
221 int searchPos(const CONTAINER &C, const T &x)
222 {
223  int pos = 0;
224  for (const T &y : C) {
225  if (x == y)
226  return pos;
227  ++pos;
228  }
229 
230  return -1;
231 }
232 
234 
236 
241 template<class E> class BucketFunc
242 {
243 public:
244  virtual ~BucketFunc() { }
245 
247  virtual int getBucket(const E &x) = 0;
248 };
249 
250 }
ogdf
The namespace for all OGDF objects.
Definition: AugmentationModule.h:36
ogdf::Initialization
The class Initialization is used for initializing global variables.
Definition: basic.h:113
ogdf::Direction
Direction
Definition: basic.h:134
ogdf::BucketFunc::~BucketFunc
virtual ~BucketFunc()
Definition: basic.h:244
ogdf::BucketFunc
Abstract base class for bucket functions.
Definition: basic.h:241
ogdf::searchPos
int searchPos(const CONTAINER &C, const T &x)
Searches for the position of x in container C; returns -1 if not found.
Definition: basic.h:221
ogdf::Direction::before
@ before
ogdf::debugMode
bool debugMode
Set to true iff debug mode is used during compilation of the OGDF.
ogdf::s_ogdfInitializer
static Initialization s_ogdfInitializer
Definition: basic.h:126
ogdf::Direction::after
@ after
ogdf::randomDoubleExponential
double randomDoubleExponential(double beta)
Returns a random double value from the exponential distribution.
ogdf::equalIgnoreCase
bool equalIgnoreCase(const string &str1, const string &str2)
Compares the two strings str1 and str2, ignoring the case of characters.
ogdf::setSeed
void setSeed(int val)
Sets the seed for functions like randomSeed(), randomNumber(), randomDouble().
ogdf::randomSeed
long unsigned int randomSeed()
Returns a random value suitable as initial seed for a random number engine.
ogdf::randomDoubleNormal
double randomDoubleNormal(double m, double sd)
Returns a random double value from the normal distribution with mean m and standard deviation sd.
Definition: basic.h:165
OGDF_EXPORT
#define OGDF_EXPORT
Specifies that a function or class is exported by the OGDF DLL.
Definition: config.h:99
config.h
Basic configuration file.
ogdf::prefixIgnoreCase
bool prefixIgnoreCase(const string &prefix, const string &str)
Tests if prefix is a prefix of str, ignoring the case of characters.
ogdf::removeTrailingWhitespace
void removeTrailingWhitespace(string &str)
Removes trailing space, horizontal and vertical tab, feed, newline, and carriage return from str.
ogdf::randomNumber
int randomNumber(int low, int high)
Returns random integer between low and high (including).
ogdf::usedTime
double usedTime(double &T)
Returns used CPU time from T to current time and assigns current time to T.
ogdf::BucketFunc::getBucket
virtual int getBucket(const E &x)=0
Returns the bucket of x.
ogdf::randomDouble
double randomDouble(double low, double high)
Returns a random double value from the interval [low, high).