Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

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 <sstream>
57 # include <stdexcept>
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) \
70  do { \
71  if (!(expr)) { \
72  std::stringstream ogdf_assert_ss; \
73  ogdf_assert_ss << "OGDF assertion `" #expr "' failed at " __FILE__ ":" \
74  << __LINE__ << "(" << OGDF_FUNCTION_NAME << ")"; \
75  ogdf::get_stacktrace(ogdf_assert_ss); \
76  OGDF_DISABLE_WARNING_PUSH \
77  OGDF_DISABLE_WARNING_THROW_TERMINATE \
78  throw ogdf::AssertionFailed(ogdf_assert_ss.str()); \
79  OGDF_DISABLE_WARNING_POP \
80  } \
81  } while (false)
82 # endif
83 #endif
84 
87 #define OGDF_IF_DBG(x)
88 #ifdef OGDF_DEBUG
89 # undef OGDF_IF_DBG
90 # define OGDF_IF_DBG(x) x
91 #endif
92 
94 
95 // g++ 4.8/4.9 does not have is_trivially_copyable,
96 // but clang 3.5 (which is also __GNUC__ < 5) has it.
97 #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 5
98 # define OGDF_TRIVIALLY_COPYABLE std::has_trivial_copy_assign
99 #else
100 # define OGDF_TRIVIALLY_COPYABLE std::is_trivially_copyable
101 #endif
102 
105 #include <algorithm>
107 #include <cmath>
108 #include <cstdint>
109 #include <ctime>
110 #include <fstream>
111 #include <limits>
112 
114 namespace ogdf {
115 
117 OGDF_EXPORT extern bool debugMode;
118 
119 // generally used <algorithm> members
120 using std::max;
121 using std::min;
122 
128 public:
129  Initialization();
130  ~Initialization();
131 };
132 
133 // OGDF_INSTALL is defined only when compiling a dynamic library.
134 // Whenever this library is used we can safely assume that this header
135 // file is included beforehand.
136 // Thus, there will be a static initialization object in the program that uses the library.
137 #ifndef OGDF_INSTALL
138 // This has to be in the header file. Being in the cpp file does not
139 // guarantee that it is constructed (with all linkers).
141 #endif
142 
143 #ifdef OGDF_USE_ASSERT_EXCEPTIONS
144 OGDF_EXPORT extern void get_stacktrace(std::ostream& stream);
146 #endif
147 
148 enum class Direction { before, after };
149 
152 
154 
158 OGDF_EXPORT long unsigned int randomSeed();
159 
161 OGDF_EXPORT void setSeed(int val);
162 
164 
168 OGDF_EXPORT int randomNumber(int low, int high);
169 
171 
175 OGDF_EXPORT double randomDouble(double low, double high);
176 
179 inline double randomDoubleNormal(double m, double sd) {
180  double x1, y1, w;
181 
182  do {
183  double rndVal = randomDouble(0, 1);
184  x1 = 2.0 * rndVal - 1.0;
185  rndVal = randomDouble(0, 1);
186  double x2 = 2.0 * rndVal - 1.0;
187  w = x1 * x1 + x2 * x2;
188  } while (w >= 1.0);
189 
190  w = sqrt((-2.0 * log(w)) / w);
191  y1 = x1 * w;
192 
193  return m + y1 * sd;
194 }
195 
197 
201 OGDF_EXPORT double randomDoubleExponential(double beta);
202 
204 
206 
209 OGDF_EXPORT double usedTime(double& T);
210 
212 OGDF_EXPORT void removeTrailingWhitespace(string& str);
213 
215 OGDF_EXPORT bool equalIgnoreCase(const string& str1, const string& str2);
216 
218 OGDF_EXPORT bool prefixIgnoreCase(const string& prefix, const string& str);
219 
222 
224 
233 template<typename CONTAINER, typename T>
234 int searchPos(const CONTAINER& C, const T& x) {
235  int pos = 0;
236  for (const T& y : C) {
237  if (x == y) {
238  return pos;
239  }
240  ++pos;
241  }
242 
243  return -1;
244 }
245 
247 
249 
254 template<class E>
255 class BucketFunc {
256 public:
257  virtual ~BucketFunc() { }
258 
260  virtual int getBucket(const E& x) = 0;
261 };
262 
263 }
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:127
ogdf::Direction
Direction
Definition: basic.h:148
ogdf::BucketFunc::~BucketFunc
virtual ~BucketFunc()
Definition: basic.h:257
ogdf::BucketFunc
Abstract base class for bucket functions.
Definition: basic.h:255
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:234
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:140
OGDF_DISABLE_WARNING_DEPRECATED
#define OGDF_DISABLE_WARNING_DEPRECATED
Disable deprecation warnings.
Definition: config.h:159
OGDF_DISABLE_WARNING_POP
#define OGDF_DISABLE_WARNING_POP
End the current warning configuration context (i.e. do pragma diagnostic/warning pop)
Definition: config.h:143
ogdf::Direction::after
@ after
ogdf::randomDoubleExponential
double randomDoubleExponential(double beta)
Returns a random double value from the exponential distribution.
ogdf::MeasureEnum::log
@ log
ogdf::equalIgnoreCase
bool equalIgnoreCase(const string &str1, const string &str2)
Compares the two strings str1 and str2, ignoring the case of characters.
OGDF_DISABLE_WARNING_PUSH
#define OGDF_DISABLE_WARNING_PUSH
Start a new warning configuration context (i.e. do pragma diagnostic/warning push)
Definition: config.h:139
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:179
OGDF_EXPORT
#define OGDF_EXPORT
Specifies that a function or class is exported by the OGDF DLL.
Definition: config.h:101
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).