Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

basic.h
Go to the documentation of this file.
1 
32 #pragma once
33 
35 
38 
41 #define OGDF_ASSERT(expr)
42 #define OGDF_ASSERT_IF(minLevel,expr)
45 #define OGDF_SET_DEBUG_LEVEL(level)
48 
49 #ifdef OGDF_DEBUG
50 # undef OGDF_ASSERT
51 # ifndef OGDF_USE_ASSERT_EXCEPTIONS
52 # include <cassert>
53 # define OGDF_ASSERT(expr) assert(expr)
54 # else
55 # include <stdexcept>
56 # include <sstream>
57 
58 namespace ogdf {
63 class AssertionFailed : public std::runtime_error {
64  using std::runtime_error::runtime_error;
65 };
66 }
67 
68 # define OGDF_ASSERT(expr) do { \
69  if (!(expr)) { \
70  std::stringstream ogdf_assert_ss; \
71  ogdf_assert_ss \
72  << "OGDF assertion `" #expr "' failed at " __FILE__ ":" \
73  << __LINE__ \
74  << "(" << OGDF_FUNCTION_NAME << ")"; \
75  ogdf::get_stacktrace(ogdf_assert_ss); \
76  throw ogdf::AssertionFailed(ogdf_assert_ss.str()); \
77  } } while (false)
78 # endif
79 # undef OGDF_ASSERT_IF
80 # define OGDF_ASSERT_IF(minLevel,expr) do { \
81  if (int(ogdf::debugLevel) >= int(minLevel)) { \
82  OGDF_ASSERT(expr); \
83  } } while (false)
84 # undef OGDF_SET_DEBUG_LEVEL
85 # define OGDF_SET_DEBUG_LEVEL(level) ogdf::debugLevel = level
86 #endif
87 
89 
90 #include <cstdint>
91 #include <cmath>
92 #include <ctime>
93 #include <fstream>
94 #include <algorithm>
95 #include <limits>
96 
98 namespace ogdf {
99 
100 using std::ifstream; // from <fstream>
101 using std::ofstream; // from <fstream>
102 using std::min; // from <algorithm>
103 using std::max; // from <algorithm>
104 using std::numeric_limits; // from <limits>
105 
106 
112 public:
113  Initialization();
114  ~Initialization();
115 };
116 
117 // OGDF_INSTALL is defined only when compiling a dynamic library.
118 // Whenever this library is used we can safely assume that this header
119 // file is included beforehand.
120 // Thus, there will be a static initialization object in the program that uses the library.
121 #ifndef OGDF_INSTALL
122 // This has to be in the header file. Being in the cpp file does not
123 // guarantee that it is constructed (with all linkers).
125 #endif
126 
127 #ifdef OGDF_USE_ASSERT_EXCEPTIONS
128 extern void get_stacktrace(std::ostream &);
130 #endif
131 
132 enum class Direction { before, after };
133 
136 
138 
142 OGDF_EXPORT long unsigned int randomSeed();
143 
145 OGDF_EXPORT void setSeed(int val);
146 
148 
152 OGDF_EXPORT int randomNumber(int low, int high);
153 
155 
159 OGDF_EXPORT double randomDouble(double low, double high);
160 
163 inline double randomDoubleNormal(double m, double sd)
164 {
165  double x1, y1, w;
166 
167  do {
168  double rndVal = randomDouble(0,1);
169  x1 = 2.0 * rndVal - 1.0;
170  rndVal = randomDouble(0,1);
171  double x2 = 2.0 * rndVal - 1.0;
172  w = x1*x1 + x2*x2;
173  } while (w >= 1.0);
174 
175  w = sqrt((-2.0 * log(w))/w) ;
176  y1 = x1*w;
177 
178  return m + y1 * sd;
179 }
180 
182 
184 
187 OGDF_EXPORT double usedTime(double& T);
188 
190 OGDF_EXPORT void removeTrailingWhitespace(string &str);
191 
193 OGDF_EXPORT bool equalIgnoreCase(const string &str1, const string &str2);
194 
196 OGDF_EXPORT bool prefixIgnoreCase(const string &prefix, const string &str);
197 
200 
202 
211 template< typename CONTAINER, typename T>
212 int searchPos(const CONTAINER &C, const T &x)
213 {
214  int pos = 0;
215  for (const T &y : C) {
216  if (x == y)
217  return pos;
218  ++pos;
219  }
220 
221  return -1;
222 }
223 
225 
226 
227 #ifdef OGDF_DEBUG
228 
236 enum class DebugLevel {
237  Minimal,
238  ExtendedChecking,
239  ConsistencyChecks,
240  HeavyChecks
241 };
242 extern DebugLevel debugLevel;
243 
245 bool debugLevelIsAtLeast(const DebugLevel& dl);
246 #endif
247 
248 
250 
255 template<class E> class BucketFunc
256 {
257 public:
258  virtual ~BucketFunc() { }
259 
261  virtual int getBucket(const E &x) = 0;
262 };
263 
264 using std::stoi;
265 using std::stoll;
266 using std::stoul;
267 using std::stoull;
268 
269 using std::stof;
270 using std::stod;
271 using std::stold;
272 
273 using std::to_string;
274 
275 } // end namespace ogdf
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:163
int randomNumber(int low, int high)
Returns random integer between low and high (including).
Basic configuration file.
static Initialization s_ogdfInitializer
Definition: basic.h:124
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:132
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:111
Abstract base class for bucket functions.
Definition: basic.h:255
virtual ~BucketFunc()
Definition: basic.h:258
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:212