Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

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)
53 #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 
70 namespace ogdf {
75 class 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 
116 namespace ogdf {
117 
119 OGDF_EXPORT extern bool debugMode;
120 
121 // generally used <algorithm> members
122 using std::max;
123 using std::min;
124 
130 public:
131  Initialization();
132  ~Initialization();
133 };
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
146 OGDF_EXPORT extern void get_stacktrace(std::ostream& stream);
148 #endif
149 
150 enum class Direction { before, after };
151 
154 
156 
160 OGDF_EXPORT long unsigned int randomSeed();
161 
163 OGDF_EXPORT void setSeed(int val);
164 
166 
170 OGDF_EXPORT int randomNumber(int low, int high);
171 
173 
177 OGDF_EXPORT double randomDouble(double low, double high);
178 
181 inline 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 
203 OGDF_EXPORT double randomDoubleExponential(double beta);
204 
206 
208 
211 OGDF_EXPORT double usedTime(double& T);
212 
214 OGDF_EXPORT void removeTrailingWhitespace(string& str);
215 
217 OGDF_EXPORT bool equalIgnoreCase(const string& str1, const string& str2);
218 
220 OGDF_EXPORT bool prefixIgnoreCase(const string& prefix, const string& str);
221 
224 
226 
235 template<typename CONTAINER, typename T>
236 int 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 
256 template<class E>
257 class BucketFunc {
258 public:
259  virtual ~BucketFunc() { }
260 
262  virtual int getBucket(const E& x) = 0;
263 };
264 
265 }
ogdf
The namespace for all OGDF objects.
Definition: multilevelmixer.cpp:39
ogdf::Initialization
The class Initialization is used for initializing global variables.
Definition: basic.h:129
ogdf::Direction
Direction
Definition: basic.h:150
ogdf::BucketFunc::~BucketFunc
virtual ~BucketFunc()
Definition: basic.h:259
ogdf::BucketFunc
Abstract base class for bucket functions.
Definition: basic.h:257
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:236
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:142
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.
config_autogen.h
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:181
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).