Open
Graph Drawing
Framework

 v.2010.10
 

basic.h

Go to the documentation of this file.
00001 /*
00002  * $Revision: 2047 $
00003  * 
00004  * last checkin:
00005  *   $Author: klein $ 
00006  *   $Date: 2010-10-13 17:12:21 +0200 (Wed, 13 Oct 2010) $ 
00007  ***************************************************************/
00008  
00053 #ifdef _MSC_VER
00054 #pragma once
00055 #endif
00056 
00057 #ifndef OGDF_BASIC_H
00058 #define OGDF_BASIC_H
00059 
00060 
00072 //---------------------------------------------------------
00073 // assertions
00074 //---------------------------------------------------------
00075 
00076 #ifdef OGDF_DEBUG
00077 #include <assert.h>
00078 #define OGDF_ASSERT(expr) assert(expr);
00079 #define OGDF_ASSERT_IF(minLevel,expr) \
00080     if (int(ogdf::debugLevel) >= int(minLevel)) assert(expr); else ;
00081 #define OGDF_SET_DEBUG_LEVEL(level) ogdf::debugLevel = level;
00082 
00083 #else
00084 #define OGDF_ASSERT(expr)
00085 #define OGDF_ASSERT_IF(minLevel,expr)
00086 #define OGDF_SET_DEBUG_LEVEL(level)
00087 #endif
00088 
00089 
00090 //---------------------------------------------------------
00091 // macros for optimization
00092 //---------------------------------------------------------
00093 
00094 // Visual C++ compiler
00095 #ifdef _MSC_VER
00096 
00097 #define OGDF_LIKELY(x)    (x)
00098 #define OGDF_UNLIKELY(x)  (x)
00099 
00100 #ifdef OGDF_DEBUG
00101 #define OGDF_NODEFAULT    default: assert(0);
00102 #else
00103 #define OGDF_NODEFAULT    default: __assume(0);
00104 #endif
00105 
00106 #define OGDF_DECL_ALIGN(b) __declspec(align(b))
00107 #define OGDF_DECL_THREAD __declspec(thread)
00108 
00109 
00110 // GNU gcc compiler (also Intel compiler)
00111 #elif defined(__GNUC__)
00112 
00113 //#define __STDC_LIMIT_MACROS
00114 //#include <stdint.h>
00115 
00116 #define OGDF_LIKELY(x)    __builtin_expect((x),1)
00117 #define OGDF_UNLIKELY(x)  __builtin_expect((x),0)
00118 #define OGDF_NODEFAULT    default: ;
00119 
00120 #define OGDF_DECL_ALIGN(b) __attribute__ ((aligned(b)))
00121 #define OGDF_DECL_THREAD __thread
00122 
00123 
00124 // other compiler
00125 #else
00126 #define OGDF_LIKELY(x)    (x)
00127 #define OGDF_UNLIKELY(x)  (x)
00128 #define OGDF_NODEFAULT
00129 
00130 #define OGDF_DECL_ALIGN(b)
00131 #endif
00132 
00133 #ifndef __SIZEOF_POINTER__
00134 #ifdef _M_X64
00135 #define __SIZEOF_POINTER__ 8
00136 #else
00137 #define __SIZEOF_POINTER__ 4
00138 #endif
00139 #endif
00140 
00141 
00142 #if defined(__CYGWIN__) || defined(__APPLE__) || defined(__sparc__)
00143 #define OGDF_NO_COMPILER_TLS
00144 #elif defined(__GNUC__)
00145 #if __GNUC__ < 4
00146 #define OGDF_NO_COMPILER_TLS
00147 #endif
00148 #endif
00149 
00150 
00151 //---------------------------------------------------------
00152 // detection of the system
00153 //---------------------------------------------------------
00154 
00155 #if defined(unix) || defined(__unix__) || defined(__unix) || defined(_AIX) || defined(__APPLE__)
00156 #define OGDF_SYSTEM_UNIX
00157 #endif
00158 
00159 #if defined(__WIN32__) || defined(_WIN32) || defined(__NT__)
00160 #define OGDF_SYSTEM_WINDOWS
00161 #endif
00162 
00163 // Note: Apple OS X machines will be both OGDF_SYSTEM_UNIX and OGDF_SYSTEM_OSX
00164 #if defined(__APPLE__)
00165 #define OGDF_SYSTEM_OSX
00166 #endif
00167 
00168 
00169 #if defined(USE_COIN) || defined(OGDF_OWN_LPSOLVER)
00170 #define OGDF_LP_SOLVER
00171 #endif
00172 
00173 #if defined(USE_COIN) && !defined(COIN_OSI_CPX) && !defined(COIN_OSI_SYM) && !defined(COIN_OSI_CLP)
00174 #error "Compiler-flag USE_COIN requires an additional COIN_OSI_xxx-flag to choose the LP solver backend."
00175 #endif
00176 
00177 
00178 //---------------------------------------------------------
00179 // macros for compiling OGDF as DLL
00180 //---------------------------------------------------------
00181 
00182 #ifdef OGDF_DLL
00183 
00184 #ifdef OGDF_INSTALL
00185 #define OGDF_EXPORT __declspec(dllexport)
00186 
00187 #else
00188 #define OGDF_EXPORT __declspec(dllimport)
00189 #endif
00190 
00191 #else
00192 #define OGDF_EXPORT
00193 
00194 #endif
00195 
00196 
00197 //---------------------------------------------------------
00198 // define data types with known size
00199 //---------------------------------------------------------
00200 
00201 #ifdef _MSC_VER
00202 
00203 typedef unsigned __int8  __uint8;
00204 typedef unsigned __int16 __uint16;
00205 typedef unsigned __int32 __uint32;
00206 typedef unsigned __int64 __uint64;
00207 
00208 #else
00209 
00210 typedef signed char        __int8;
00211 typedef short              __int16;
00212 typedef int                __int32;
00213 typedef long long          __int64;
00214 typedef unsigned char      __uint8;
00215 typedef unsigned short     __uint16;
00216 typedef unsigned int       __uint32;
00217 typedef unsigned long long __uint64;
00218 #endif
00219 
00220 
00221 //---------------------------------------------------------
00222 // common includes
00223 //---------------------------------------------------------
00224 
00225 #include <iostream>
00226 #include <fstream>
00227 
00228 using std::ios;
00229 using std::istream;
00230 using std::ifstream;
00231 using std::ostream;
00232 using std::ofstream;
00233 using std::cin;
00234 using std::cout;
00235 using std::cerr;
00236 using std::endl;
00237 using std::flush;
00238 using std::swap;
00239 
00240 #include <stdio.h>
00241 #include <stdarg.h>
00242 #include <time.h>
00243 #include <string.h>
00244 #include <math.h>
00245 
00246 
00247 #ifdef OGDF_SYSTEM_UNIX
00248 #include <stdint.h>
00249 #endif
00250 // make sure that SIZE_MAX gets defined
00251 #ifndef SIZE_MAX
00252 #define SIZE_MAX ((size_t)-1)
00253 #endif
00254 
00255 
00256 #include <ogdf/basic/exceptions.h>
00257 #include <ogdf/basic/memory.h>
00258 #include <ogdf/basic/comparer.h>
00259     
00260 
00261 
00262 //---------------------------------------------------------
00263 // compiler adaptions
00264 //---------------------------------------------------------
00265 
00266 #ifdef _MSC_VER
00267 
00268 // disable useless warnings
00269 #pragma warning(disable:4250)
00270 #pragma warning(disable:4290)
00271 #pragma warning(disable:4291)
00272 #pragma warning(disable:4355)
00273 
00274 // missing dll-interface
00275 #pragma warning(disable:4251)
00276 #pragma warning(disable:4275)
00277 
00278 #endif
00279 
00280 
00282 namespace ogdf {
00283 
00284 #ifndef OGDF_DLL
00285 
00290 class Initialization {
00291     static int s_count;
00292 
00293 public:
00294     Initialization();
00295     ~Initialization();
00296 };
00297 
00298 static Initialization s_ogdfInitializer;
00299 
00300 #endif
00301 
00302 
00303 //---------------------------------------------------------
00304 // global basic functions
00305 //---------------------------------------------------------
00306     const double pi    = 3.1415926535897932384626433832795028841971;
00307     const double euler = 2.7182818284590452353602874713526624977572;
00308 
00309     // forward declarations
00310     template<class E> class List;
00311     class OGDF_EXPORT String;
00312 
00313 
00314     enum Direction { before, after };
00315 
00316 #ifndef OGDF_NOMINMAX
00317 #ifndef min
00318 
00319     template<class T>
00320     inline T min(const T& x, const T& y) { return (x<y) ? x : y; }
00321 #endif
00322 
00323 #ifndef max
00324 
00325     template<class T>
00326     inline T max(const T& x, const T& y) { return (x>y) ? x : y; }
00327 #endif
00328 #endif
00329 
00331     inline int randomNumber(int low, int high) {
00332 #if RAND_MAX == 32767
00333         // We get only 15 random bits on some systems (Windows, Solaris)!
00334         int r1 = (rand() & ((1 << 16) - 1));
00335         int r2 = (rand() & ((1 << 16) - 1));
00336         int r = (r1 << 15) | r2;
00337 #else
00338         int r = rand();
00339 #endif
00340         return low + (r % (high-low+1));
00341     }
00342 
00344     inline double randomDouble(double low, double high) {
00345         double val = low +(rand()*(high-low))/RAND_MAX;
00346         OGDF_ASSERT(val >= low && val <= high);
00347         return val;
00348     }
00349 
00352     inline double randomDoubleNormal(double m, double sd)
00353     {
00354         double x1, x2, y1, w, rndVal;
00355     
00356         do {
00357             rndVal = randomDouble(0,1);
00358             x1 = 2.0 * rndVal - 1.0;
00359             rndVal = randomDouble(0,1);
00360             x2 = 2.0 * rndVal - 1.0;
00361             w = x1*x1 + x2*x2;
00362         } while (w >= 1.0);
00363 
00364         w = sqrt((-2.0 * log(w))/w) ;
00365         y1 = x1*w;
00366 
00367         return(m + y1*sd);
00368     }
00369 
00370 
00371 
00374     OGDF_EXPORT double usedTime(double& T);
00375 
00378     template<class E>inline bool doDestruction(const E *) { return true; }
00379     
00380     // specializations
00381     template<>inline bool doDestruction(const char *) { return false; }
00382     template<>inline bool doDestruction<int>(const int *) { return false; }
00383     template<>inline bool doDestruction<double>(const double *) { return false; }
00384 
00385 
00386     //---------------------------------------------------------
00387     // handling files and directories
00388     //---------------------------------------------------------
00389 
00391     enum FileType {
00392         ftEntry,     
00393         ftFile,      
00394         ftDirectory  
00395     };
00396 
00398     OGDF_EXPORT bool isFile(const char *fileName);
00399 
00401     OGDF_EXPORT bool isDirectory(const char *fileName);
00402 
00404     OGDF_EXPORT void changeDir(const char *dirName);
00405     
00407 
00411     OGDF_EXPORT void getFiles(const char *dirName,
00412         List<String> &files,
00413         const char *pattern = "*");
00414 
00416 
00420     OGDF_EXPORT void getFilesAppend(const char *dirName,
00421         List<String> &files,
00422         const char *pattern = "*");
00423 
00424 
00426 
00430     OGDF_EXPORT void getSubdirs(const char *dirName,
00431         List<String> &subdirs,
00432         const char *pattern = "*");
00433 
00435 
00439     OGDF_EXPORT void getSubdirsAppend(const char *dirName,
00440         List<String> &subdirs,
00441         const char *pattern = "*");
00442 
00443 
00445 
00450     OGDF_EXPORT void getEntries(const char *dirName,
00451         List<String> &entries,
00452         const char *pattern = "*");
00453 
00455 
00460     OGDF_EXPORT void getEntriesAppend(const char *dirName,
00461         List<String> &entries,
00462         const char *pattern = "*");
00463 
00464 
00466 
00470     OGDF_EXPORT void getEntries(const char *dirName,
00471         FileType t,
00472         List<String> &entries,
00473         const char *pattern = "*");
00474 
00476 
00480     OGDF_EXPORT void getEntriesAppend(const char *dirName,
00481         FileType t,
00482         List<String> &entries,
00483         const char *pattern = "*");
00484 
00485     //---------------------------------------------------------
00486     // handling markup formatting
00487     //---------------------------------------------------------
00488     
00489     const char NEWLINE('\n');                   // newline character
00490     const char INDENTCHAR(' ');             // indent character
00491     const int INDENTSIZE(2);                    // indent size
00492 
00493 #ifdef OGDF_DEBUG
00494 
00502     enum DebugLevel {
00503         dlMinimal, dlExtendedChecking, dlConsistencyChecks, dlHeavyChecks
00504     };
00505     extern DebugLevel debugLevel;
00506 #endif
00507 
00508 
00510 
00515 template<class E> class BucketFunc
00516 {
00517 public:
00518     virtual ~BucketFunc() { }
00519 
00521     virtual int getBucket(const E &x) = 0;
00522 };
00523 
00524 
00525 
00526 #if _MSC_VER >= 1400
00527 
00528 inline int sprintf(char *buffer, size_t sizeOfBuffer, const char *format, ...)
00529 {
00530     va_list args;
00531     va_start(args, format);
00532 
00533     return vsprintf_s(buffer, sizeOfBuffer, format, args);
00534 }
00535 
00536 inline int vsprintf(char *buffer, size_t sizeInBytes, const char *format, va_list argptr)
00537 {
00538     return vsprintf_s(buffer, sizeInBytes, format, argptr);
00539 }
00540 
00541 inline int strcat(char *strDest, size_t sizeOfDest, const char *strSource)
00542 {
00543     return (int)strcat_s(strDest, sizeOfDest, strSource);
00544 }
00545 
00546 inline int strcpy(char *strDest, size_t sizeOfDest, const char *strSource)
00547 {
00548     return (int)strcpy_s(strDest, sizeOfDest, strSource);
00549 }
00550 
00551 inline int strncpy(char *strDest, size_t sizeOfDest, const char *strSource, size_t count)
00552 {
00553     return (int)strncpy_s(strDest, sizeOfDest, strSource, count);
00554 }
00555 
00556 inline char *strtok(char *strToken, const char *strDelimit)
00557 {
00558     char *context;
00559     return strtok_s(strToken, strDelimit, &context);
00560 }
00561 
00562 #define scanf scanf_s
00563 #define fscanf fscanf_s
00564 #define sscanf sscanf_s
00565 
00566 inline FILE *fopen(const char *filename, const char *mode)
00567 {
00568     FILE *stream;
00569     if(fopen_s(&stream, filename, mode)) stream = 0;
00570     return stream;
00571 }
00572 
00573 inline int localtime(struct tm *ptm, const time_t *timer)
00574 {
00575     return (int)localtime_s(ptm,timer);
00576 }
00577 
00578 #else ///////////////////////////////////////////////////////////////////////////////
00579 
00580 inline int sprintf(char *buffer, size_t, const char *format, ...)
00581 {
00582     va_list args;
00583     va_start(args, format);
00584 
00585     return ::vsprintf(buffer, format, args);
00586 }
00587 
00588 
00589 inline int vsprintf(char *buffer, size_t, const char *format, va_list argptr)
00590 {
00591     return ::vsprintf(buffer, format, argptr);
00592 }
00593 
00594 
00595 inline int strcat(char *strDest, size_t, const char *strSource)
00596 {
00597 	::strcat(strDest, strSource);
00598     return 0;
00599 }
00600 
00601 inline int strcpy(char *strDest, size_t, const char *strSource)
00602 {
00603 	::strcpy(strDest, strSource);
00604     return 0;
00605 }
00606 
00607 inline int strncpy(char *strDest, size_t, const char *strSource, size_t count)
00608 {
00609 	::strncpy(strDest, strSource, count);
00610     return 0;
00611 }
00612 
00613 inline int localtime(struct tm *ptm, const time_t *timer)
00614 {
00615     struct tm *newtime = ::localtime(timer);
00616     if(newtime) {
00617         *ptm = *newtime;
00618         return 0;
00619     }
00620     return 1; // indicates error
00621 }
00622 
00623 #endif
00624 
00625 } // end namespace ogdf
00626 
00627 
00628 #endif