Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
basic.h
Go to the documentation of this file.
1
32#pragma once
33
34#include <ogdf/basic/internal/config.h> // IWYU pragma: export
35#include <ogdf/basic/internal/config_autogen.h> // IWYU pragma: export
36
39#include <algorithm>
40
42
43#include <cmath>
44#include <fstream>
45#include <string>
46
49
52#define OGDF_ASSERT(expr)
55#define OGDF_HEAVY_ASSERT(expr)
56
57#ifdef OGDF_DEBUG
58# ifdef OGDF_HEAVY_DEBUG
59# undef OGDF_HEAVY_ASSERT
60# define OGDF_HEAVY_ASSERT(expr) OGDF_ASSERT(expr)
61# endif
62# undef OGDF_ASSERT
63# ifndef OGDF_USE_ASSERT_EXCEPTIONS
64# include <cassert>
65
66# define OGDF_ASSERT(expr) assert(expr)
67# else
68# include <stdexcept>
69
70namespace ogdf {
75class OGDF_EXPORT AssertionFailed : public std::runtime_error {
76 using std::runtime_error::runtime_error;
77};
78}
79
80# define OGDF_ASSERT(expr) \
81 do { \
82 if (!(expr)) { \
83 std::stringstream ogdf_assert_ss; \
84 ogdf_assert_ss << "OGDF assertion `" #expr "' failed at " __FILE__ ":" \
85 << __LINE__ << "(" << OGDF_FUNCTION_NAME << ")"; \
86 ogdf::get_stacktrace(ogdf_assert_ss); \
87 OGDF_DISABLE_WARNING_PUSH \
88 OGDF_DISABLE_WARNING_THROW_TERMINATE \
89 throw ogdf::AssertionFailed(ogdf_assert_ss.str()); \
90 OGDF_DISABLE_WARNING_POP \
91 } \
92 } while (false)
93# endif
94#endif
95
98#define OGDF_IF_DBG(x)
99#ifdef OGDF_DEBUG
100# undef OGDF_IF_DBG
101# define OGDF_IF_DBG(x) x
102#endif
103
105
106// g++ 4.8/4.9 does not have is_trivially_copyable,
107// but clang 3.5 (which is also __GNUC__ < 5) has it.
108#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 5
109# define OGDF_TRIVIALLY_COPYABLE std::has_trivial_copy_assign
110#else
111# define OGDF_TRIVIALLY_COPYABLE std::is_trivially_copyable
112#endif
113
114
116namespace ogdf {
117
119OGDF_EXPORT extern bool debugMode;
120
121// generally used <algorithm> members
122using std::max;
123using std::min;
124
134
135// OGDF_INSTALL is defined only when compiling a dynamic library.
136// Whenever this library is used we can safely assume that this header
137// file is included beforehand.
138// Thus, there will be a static initialization object in the program that uses the library.
139#ifndef OGDF_INSTALL
140// This has to be in the header file. Being in the cpp file does not
141// guarantee that it is constructed (with all linkers).
143#endif
144
145#ifdef OGDF_USE_ASSERT_EXCEPTIONS
147OGDF_EXPORT extern void get_stacktrace(std::ostream& stream);
148#endif
149
150enum class Direction { before, after };
151
154
156
160OGDF_EXPORT long unsigned int randomSeed();
161
163OGDF_EXPORT void setSeed(int val);
164
166
170OGDF_EXPORT int randomNumber(int low, int high);
171
173
177OGDF_EXPORT double randomDouble(double low, double high);
178
181inline double randomDoubleNormal(double m, double sd) {
182 double x1, y1, w;
183
184 do {
185 double rndVal = randomDouble(0, 1);
186 x1 = 2.0 * rndVal - 1.0;
187 rndVal = randomDouble(0, 1);
188 double x2 = 2.0 * rndVal - 1.0;
189 w = x1 * x1 + x2 * x2;
190 } while (w >= 1.0);
191
192 w = sqrt((-2.0 * log(w)) / w);
193 y1 = x1 * w;
194
195 return m + y1 * sd;
196}
197
199
204
206
208
211OGDF_EXPORT double usedTime(double& T);
212
215
217OGDF_EXPORT bool equalIgnoreCase(const string& str1, const string& str2);
218
220OGDF_EXPORT bool prefixIgnoreCase(const string& prefix, const string& str);
221
224
226
235template<typename CONTAINER, typename T>
236int searchPos(const CONTAINER& C, const T& x) {
237 int pos = 0;
238 for (const T& y : C) {
239 if (x == y) {
240 return pos;
241 }
242 ++pos;
243 }
244
245 return -1;
246}
247
249
251
256template<class E>
258public:
259 virtual ~BucketFunc() { }
260
262 virtual int getBucket(const E& x) = 0;
263};
264
265}
Abstract base class for bucket functions.
Definition basic.h:257
virtual ~BucketFunc()
Definition basic.h:259
virtual int getBucket(const E &x)=0
Returns the bucket of x.
The class Initialization is used for initializing global variables.
Definition basic.h:129
Basic configuration file.
#define OGDF_EXPORT
Specifies that a function or class is exported by the OGDF dynamic library (shared object / DLL),...
Definition config.h:117
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:236
double usedTime(double &T)
Returns used CPU time from T to current time and assigns current time to T.
#define OGDF_DISABLE_WARNING_POP
End the current warning configuration context (i.e. do pragma diagnostic/warning pop)
Definition config.h:229
#define OGDF_DISABLE_WARNING_PUSH
Start a new warning configuration context (i.e. do pragma diagnostic/warning push)
Definition config.h:225
#define OGDF_DISABLE_WARNING_DEPRECATED
Disable deprecation warnings.
Definition config.h:245
int randomNumber(int low, int high)
Returns random integer between low and high (including).
double randomDoubleExponential(double beta)
Returns a random double value from the exponential distribution.
double randomDouble(double low, double high)
Returns a random double value from the interval [low, high).
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:181
void setSeed(int val)
Sets the seed for functions like randomSeed(), randomNumber(), randomDouble().
long unsigned int randomSeed()
Returns a random value suitable as initial seed for a random number engine.
The namespace for all OGDF objects.
bool equalIgnoreCase(const string &str1, const string &str2)
Compares the two strings str1 and str2, ignoring the case of characters.
bool prefixIgnoreCase(const string &prefix, const string &str)
Tests if prefix is a prefix of str, ignoring the case of characters.
void removeTrailingWhitespace(string &str)
Removes trailing space, horizontal and vertical tab, feed, newline, and carriage return from str.
Direction
Definition basic.h:150
static Initialization s_ogdfInitializer
Definition basic.h:142
bool debugMode
Set to true iff debug mode is used during compilation of the OGDF.