Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

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 #include <cstdint>
85 #include <cmath>
86 #include <ctime>
87 #include <fstream>
88 #include <algorithm>
89 #include <limits>
90 
92 namespace ogdf {
93 
95 OGDF_EXPORT extern bool debugMode;
96 
97 // generally used <algorithm> members
98 using std::min;
99 using std::max;
100 
106 public:
107  Initialization();
108  ~Initialization();
109 };
110 
111 // OGDF_INSTALL is defined only when compiling a dynamic library.
112 // Whenever this library is used we can safely assume that this header
113 // file is included beforehand.
114 // Thus, there will be a static initialization object in the program that uses the library.
115 #ifndef OGDF_INSTALL
116 // This has to be in the header file. Being in the cpp file does not
117 // guarantee that it is constructed (with all linkers).
119 #endif
120 
121 #ifdef OGDF_USE_ASSERT_EXCEPTIONS
122 extern void get_stacktrace(std::ostream &);
124 #endif
125 
126 enum class Direction { before, after };
127 
130 
132 
136 OGDF_EXPORT long unsigned int randomSeed();
137 
139 OGDF_EXPORT void setSeed(int val);
140 
142 
146 OGDF_EXPORT int randomNumber(int low, int high);
147 
149 
153 OGDF_EXPORT double randomDouble(double low, double high);
154 
157 inline double randomDoubleNormal(double m, double sd)
158 {
159  double x1, y1, w;
160 
161  do {
162  double rndVal = randomDouble(0,1);
163  x1 = 2.0 * rndVal - 1.0;
164  rndVal = randomDouble(0,1);
165  double x2 = 2.0 * rndVal - 1.0;
166  w = x1*x1 + x2*x2;
167  } while (w >= 1.0);
168 
169  w = sqrt((-2.0 * log(w))/w) ;
170  y1 = x1*w;
171 
172  return m + y1 * sd;
173 }
174 
176 
178 
181 OGDF_EXPORT double usedTime(double& T);
182 
184 OGDF_EXPORT void removeTrailingWhitespace(string &str);
185 
187 OGDF_EXPORT bool equalIgnoreCase(const string &str1, const string &str2);
188 
190 OGDF_EXPORT bool prefixIgnoreCase(const string &prefix, const string &str);
191 
194 
196 
205 template< typename CONTAINER, typename T>
206 int searchPos(const CONTAINER &C, const T &x)
207 {
208  int pos = 0;
209  for (const T &y : C) {
210  if (x == y)
211  return pos;
212  ++pos;
213  }
214 
215  return -1;
216 }
217 
219 
221 
226 template<class E> class BucketFunc
227 {
228 public:
229  virtual ~BucketFunc() { }
230 
232  virtual int getBucket(const E &x) = 0;
233 };
234 
235 }
bool equalIgnoreCase(const string &str1, const string &str2)
Compares the two strings str1 and str2, ignoring the case of characters.
#define OGDF_EXPORT
Specifies that a function or class is exported by the OGDF DLL.
Definition: config.h:99
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:157
int randomNumber(int low, int high)
Returns random integer between low and high (including).
Basic configuration file.
static Initialization s_ogdfInitializer
Definition: basic.h:118
bool debugMode
Set to true iff debug mode is used during compilation of the OGDF.
long unsigned int randomSeed()
Returns a random value suitable as initial seed for a random number engine.
void removeTrailingWhitespace(string &str)
Removes trailing space, horizontal and vertical tab, feed, newline, and carriage return from str...
double randomDouble(double low, double high)
Returns a random double value from the interval [low, high).
Direction
Definition: basic.h:126
double usedTime(double &T)
Returns used CPU time from T to current time and assigns current time to T.
The class Initialization is used for initializing global variables.
Definition: basic.h:105
Abstract base class for bucket functions.
Definition: basic.h:226
virtual ~BucketFunc()
Definition: basic.h:229
The namespace for all OGDF objects.
bool prefixIgnoreCase(const string &prefix, const string &str)
Tests if prefix is a prefix of str, ignoring the case of characters.
void setSeed(int val)
Sets the seed for functions like randomSeed(), randomNumber(), randomDouble().
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:206