Open
Graph Drawing
Framework

 v.2012.05
 

basic.h
Go to the documentation of this file.
00001 /*
00002  * $Revision: 2320 $
00003  * 
00004  * last checkin:
00005  *   $Author: gutwenger $ 
00006  *   $Date: 2012-05-09 14:37:44 +0200 (Wed, 09 May 2012) $ 
00007  ***************************************************************/
00008  
00043 #ifdef _MSC_VER
00044 #pragma once
00045 #endif
00046 
00047 #ifndef OGDF_BASIC_H
00048 #define OGDF_BASIC_H
00049 
00050 
00073 //---------------------------------------------------------
00074 // assertions
00075 //---------------------------------------------------------
00076 
00077 #ifdef OGDF_DEBUG
00078 #include <assert.h>
00079 #define OGDF_ASSERT(expr) assert(expr);
00080 #define OGDF_ASSERT_IF(minLevel,expr) \
00081     if (int(ogdf::debugLevel) >= int(minLevel)) assert(expr); else ;
00082 #define OGDF_SET_DEBUG_LEVEL(level) ogdf::debugLevel = level;
00083 
00084 #else
00085 #define OGDF_ASSERT(expr)
00086 #define OGDF_ASSERT_IF(minLevel,expr)
00087 #define OGDF_SET_DEBUG_LEVEL(level)
00088 #endif
00089 
00090 
00091 //---------------------------------------------------------
00092 // macros for optimization
00093 //---------------------------------------------------------
00094 
00095 // Visual C++ compiler
00096 #ifdef _MSC_VER
00097 
00098 #define OGDF_LIKELY(x)    (x)
00099 #define OGDF_UNLIKELY(x)  (x)
00100 
00101 #ifdef OGDF_DEBUG
00102 #define OGDF_NODEFAULT    default: assert(0);
00103 #else
00104 #define OGDF_NODEFAULT    default: __assume(0);
00105 #endif
00106 
00107 #define OGDF_DECL_ALIGN(b) __declspec(align(b))
00108 #define OGDF_DECL_THREAD __declspec(thread)
00109 
00110 
00111 // GNU gcc compiler (also Intel compiler)
00112 #elif defined(__GNUC__)
00113 
00114 //#define __STDC_LIMIT_MACROS
00115 //#include <stdint.h>
00116 
00117 #define OGDF_LIKELY(x)    __builtin_expect((x),1)
00118 #define OGDF_UNLIKELY(x)  __builtin_expect((x),0)
00119 #define OGDF_NODEFAULT    default: ;
00120 
00121 #define OGDF_DECL_ALIGN(b) __attribute__ ((aligned(b)))
00122 #define OGDF_DECL_THREAD __thread
00123 
00124 
00125 // other compiler
00126 #else
00127 #define OGDF_LIKELY(x)    (x)
00128 #define OGDF_UNLIKELY(x)  (x)
00129 #define OGDF_NODEFAULT
00130 
00131 #define OGDF_DECL_ALIGN(b)
00132 #endif
00133 
00134 #ifndef __SIZEOF_POINTER__
00135 #ifdef _M_X64
00136 #define __SIZEOF_POINTER__ 8
00137 #else
00138 #define __SIZEOF_POINTER__ 4
00139 #endif
00140 #endif
00141 
00142 
00143 #if defined(__CYGWIN__) || defined(__APPLE__) || defined(__sparc__)
00144 #define OGDF_NO_COMPILER_TLS
00145 #elif defined(__GNUC__)
00146 #if __GNUC__ < 4
00147 #define OGDF_NO_COMPILER_TLS
00148 #endif
00149 #endif
00150 
00151 
00152 //---------------------------------------------------------
00153 // detection of the system
00154 //---------------------------------------------------------
00155 
00156 #if defined(unix) || defined(__unix__) || defined(__unix) || defined(_AIX) || defined(__APPLE__)
00157 #define OGDF_SYSTEM_UNIX
00158 #endif
00159 
00160 #if defined(__WIN32__) || defined(_WIN32) || defined(__NT__)
00161 #define OGDF_SYSTEM_WINDOWS
00162 #endif
00163 
00164 // Note: Apple OS X machines will be both OGDF_SYSTEM_UNIX and OGDF_SYSTEM_OSX
00165 #if defined(__APPLE__)
00166 #define OGDF_SYSTEM_OSX
00167 #endif
00168 
00169 
00170 #if defined(USE_COIN) || defined(OGDF_OWN_LPSOLVER)
00171 #define OGDF_LP_SOLVER
00172 #endif
00173 
00174 #if defined(USE_COIN) && !defined(COIN_OSI_CPX) && !defined(COIN_OSI_SYM) && !defined(COIN_OSI_CLP)
00175 #error "Compiler-flag USE_COIN requires an additional COIN_OSI_xxx-flag to choose the LP solver backend."
00176 #endif
00177 
00178 
00179 //---------------------------------------------------------
00180 // macros for compiling OGDF as DLL
00181 //---------------------------------------------------------
00182 
00183 #ifdef OGDF_DLL
00184 
00185 #ifdef OGDF_INSTALL
00186 #define OGDF_EXPORT __declspec(dllexport)
00187 
00188 #else
00189 #define OGDF_EXPORT __declspec(dllimport)
00190 #endif
00191 
00192 #else
00193 #define OGDF_EXPORT
00194 
00195 #endif
00196 
00197 
00198 //---------------------------------------------------------
00199 // define data types with known size
00200 //---------------------------------------------------------
00201 
00202 #ifdef _MSC_VER
00203 
00204 typedef unsigned __int8  __uint8;
00205 typedef unsigned __int16 __uint16;
00206 typedef unsigned __int32 __uint32;
00207 typedef unsigned __int64 __uint64;
00208 
00209 #else
00210 
00211 typedef signed char        __int8;
00212 typedef short              __int16;
00213 typedef int                __int32;
00214 typedef long long          __int64;
00215 typedef unsigned char      __uint8;
00216 typedef unsigned short     __uint16;
00217 typedef unsigned int       __uint32;
00218 typedef unsigned long long __uint64;
00219 #endif
00220 
00221 
00222 //---------------------------------------------------------
00223 // common includes
00224 //---------------------------------------------------------
00225 
00226 #include <iostream>
00227 #include <fstream>
00228 
00229 using std::ios;
00230 using std::istream;
00231 using std::ifstream;
00232 using std::ostream;
00233 using std::ofstream;
00234 using std::cin;
00235 using std::cout;
00236 using std::cerr;
00237 using std::endl;
00238 using std::flush;
00239 using std::swap;
00240 
00241 #include <stdio.h>
00242 #include <stdarg.h>
00243 #include <time.h>
00244 #include <string.h>
00245 #include <math.h>
00246 
00247 
00248 #ifdef OGDF_SYSTEM_UNIX
00249 #include <stdint.h>
00250 #endif
00251 // make sure that SIZE_MAX gets defined
00252 #ifndef SIZE_MAX
00253 #define SIZE_MAX ((size_t)-1)
00254 #endif
00255 
00256 
00257 #include <ogdf/basic/exceptions.h>
00258 #include <ogdf/basic/memory.h>
00259 #include <ogdf/basic/comparer.h>
00260     
00261 
00262 
00263 //---------------------------------------------------------
00264 // compiler adaptions
00265 //---------------------------------------------------------
00266 
00267 #ifdef _MSC_VER
00268 
00269 // disable useless warnings
00270 #pragma warning(disable:4250)
00271 #pragma warning(disable:4290)
00272 #pragma warning(disable:4291)
00273 #pragma warning(disable:4355)
00274 
00275 // missing dll-interface
00276 #pragma warning(disable:4251)
00277 #pragma warning(disable:4275)
00278 
00279 #endif
00280 
00281 
00283 namespace ogdf {
00284 
00285 #ifndef OGDF_DLL
00286 
00291 class Initialization {
00292     static int s_count;
00293 
00294 public:
00295     Initialization();
00296     ~Initialization();
00297 };
00298 
00299 static Initialization s_ogdfInitializer;
00300 
00301 #endif
00302 
00303 
00304 //---------------------------------------------------------
00305 // global basic functions
00306 //---------------------------------------------------------
00307 
00308     // forward declarations
00309     template<class E> class List;
00310     class OGDF_EXPORT String;
00311 
00312 
00313     enum Direction { before, after };
00314 
00315 #ifndef OGDF_NOMINMAX
00316 #ifndef min
00317 
00318     template<class T>
00319     inline T min(const T& x, const T& y) { return (x<y) ? x : y; }
00320 #endif
00321 
00322 #ifndef max
00323 
00324     template<class T>
00325     inline T max(const T& x, const T& y) { return (x>y) ? x : y; }
00326 #endif
00327 #endif
00328 
00330     inline int randomNumber(int low, int high) {
00331 #if RAND_MAX == 32767
00332         // We get only 15 random bits on some systems (Windows, Solaris)!
00333         int r1 = (rand() & ((1 << 16) - 1));
00334         int r2 = (rand() & ((1 << 16) - 1));
00335         int r = (r1 << 15) | r2;
00336 #else
00337         int r = rand();
00338 #endif
00339         return low + (r % (high-low+1));
00340     }
00341 
00343     inline double randomDouble(double low, double high) {
00344         double val = low +(rand()*(high-low))/RAND_MAX;
00345         OGDF_ASSERT(val >= low && val <= high);
00346         return val;
00347     }
00348 
00351     inline double randomDoubleNormal(double m, double sd)
00352     {
00353         double x1, x2, y1, w, rndVal;
00354     
00355         do {
00356             rndVal = randomDouble(0,1);
00357             x1 = 2.0 * rndVal - 1.0;
00358             rndVal = randomDouble(0,1);
00359             x2 = 2.0 * rndVal - 1.0;
00360             w = x1*x1 + x2*x2;
00361         } while (w >= 1.0);
00362 
00363         w = sqrt((-2.0 * log(w))/w) ;
00364         y1 = x1*w;
00365 
00366         return(m + y1*sd);
00367     }
00368 
00369 
00370 
00373     OGDF_EXPORT double usedTime(double& T);
00374 
00377     template<class E>inline bool doDestruction(const E *) { return true; }
00378     
00379     // specializations
00380     template<>inline bool doDestruction(const char *) { return false; }
00381     template<>inline bool doDestruction<int>(const int *) { return false; }
00382     template<>inline bool doDestruction<double>(const double *) { return false; }
00383 
00384 
00385     //---------------------------------------------------------
00386     // handling files and directories
00387     //---------------------------------------------------------
00388 
00390     enum FileType {
00391         ftEntry,     
00392         ftFile,      
00393         ftDirectory  
00394     };
00395 
00397     OGDF_EXPORT bool isFile(const char *fileName);
00398 
00400     OGDF_EXPORT bool isDirectory(const char *fileName);
00401 
00403     OGDF_EXPORT bool changeDir(const char *dirName);
00404     
00406 
00410     OGDF_EXPORT void getFiles(const char *dirName,
00411         List<String> &files,
00412         const char *pattern = "*");
00413 
00415 
00419     OGDF_EXPORT void getFilesAppend(const char *dirName,
00420         List<String> &files,
00421         const char *pattern = "*");
00422 
00423 
00425 
00429     OGDF_EXPORT void getSubdirs(const char *dirName,
00430         List<String> &subdirs,
00431         const char *pattern = "*");
00432 
00434 
00438     OGDF_EXPORT void getSubdirsAppend(const char *dirName,
00439         List<String> &subdirs,
00440         const char *pattern = "*");
00441 
00442 
00444 
00449     OGDF_EXPORT void getEntries(const char *dirName,
00450         List<String> &entries,
00451         const char *pattern = "*");
00452 
00454 
00459     OGDF_EXPORT void getEntriesAppend(const char *dirName,
00460         List<String> &entries,
00461         const char *pattern = "*");
00462 
00463 
00465 
00469     OGDF_EXPORT void getEntries(const char *dirName,
00470         FileType t,
00471         List<String> &entries,
00472         const char *pattern = "*");
00473 
00475 
00479     OGDF_EXPORT void getEntriesAppend(const char *dirName,
00480         FileType t,
00481         List<String> &entries,
00482         const char *pattern = "*");
00483 
00484     //---------------------------------------------------------
00485     // handling markup formatting
00486     //---------------------------------------------------------
00487     
00488     const char NEWLINE('\n');                   // newline character
00489     const char INDENTCHAR(' ');             // indent character
00490     const int INDENTSIZE(2);                    // indent size
00491 
00492 #ifdef OGDF_DEBUG
00493 
00501     enum DebugLevel {
00502         dlMinimal, dlExtendedChecking, dlConsistencyChecks, dlHeavyChecks
00503     };
00504     extern DebugLevel debugLevel;
00505 #endif
00506 
00507 
00509 
00514 template<class E> class BucketFunc
00515 {
00516 public:
00517     virtual ~BucketFunc() { }
00518 
00520     virtual int getBucket(const E &x) = 0;
00521 };
00522 
00523 
00524 
00525 #if _MSC_VER >= 1400
00526 
00527 inline int sprintf(char *buffer, size_t sizeOfBuffer, const char *format, ...)
00528 {
00529     va_list args;
00530     va_start(args, format);
00531 
00532     return vsprintf_s(buffer, sizeOfBuffer, format, args);
00533 }
00534 
00535 inline int vsprintf(char *buffer, size_t sizeInBytes, const char *format, va_list argptr)
00536 {
00537     return vsprintf_s(buffer, sizeInBytes, format, argptr);
00538 }
00539 
00540 inline int strcat(char *strDest, size_t sizeOfDest, const char *strSource)
00541 {
00542     return (int)strcat_s(strDest, sizeOfDest, strSource);
00543 }
00544 
00545 inline int strcpy(char *strDest, size_t sizeOfDest, const char *strSource)
00546 {
00547     return (int)strcpy_s(strDest, sizeOfDest, strSource);
00548 }
00549 
00550 inline int strncpy(char *strDest, size_t sizeOfDest, const char *strSource, size_t count)
00551 {
00552     return (int)strncpy_s(strDest, sizeOfDest, strSource, count);
00553 }
00554 
00555 inline char *strtok(char *strToken, const char *strDelimit)
00556 {
00557     //provide a persistent context pointer for strtok_s
00558     static 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