00001 #ifndef __LIBUTILITY_H__ 00002 #define __LIBUTILITY_H__ 00003 00004 #include <stdio.h> 00005 #include <stdlib.h> 00006 #include <math.h> 00007 #include <sys/time.h> 00008 #include "Promotion.h" 00009 00010 //========================================================================== 00011 //========================================================================== 00012 #define ERROR(s...) {UD("ERROR:" s); exit(1);} 00013 00014 //========================================================================== 00015 //========================================================================== 00016 #define WARNING(s...) {printf("WARNING: "); printf(s); printf("\n");} 00017 00018 //========================================================================== 00019 //========================================================================== 00020 #define WARNIF(b,s...) do {if(b){WARNING(s);}} while(0) 00021 00022 //========================================================================== 00023 //========================================================================== 00024 #define ASSERT(__b) {if (!(__b)) {UD("ASSERT FAILED: %s", #__b); abort();}} 00025 00026 //========================================================================== 00027 //========================================================================== 00028 #define ASSERTf(__b,s...) {if (!(__b)) {UD("ASSERT FAILED: %s", #__b); UD(s); abort();}} 00029 00030 //========================================================================== 00031 //========================================================================== 00032 #define HERE UD("HERE"); 00033 00034 //========================================================================== 00035 //========================================================================== 00036 // print 00037 #define P(_s...) {printf(_s);printf("\n");} 00038 00039 //========================================================================== 00040 //========================================================================== 00041 // print with no newline 00042 #define PX(_s...) {printf(_s);} 00043 00044 //========================================================================== 00045 //========================================================================== 00046 // indented print 00047 #define IP(_c,_s...) {IPX(_c,_s); printf("\n");} 00048 00049 //========================================================================== 00050 //========================================================================== 00051 // indented print with no newline 00052 #define IPX(_c,_s...) { \ 00053 for (int _n=0; _n<_c; _n++){ \ 00054 printf("\t"); \ 00055 }; \ 00056 printf(_s); \ 00057 } 00058 00059 //========================================================================== 00060 //========================================================================== 00061 #define Stringify2(x...) #x 00062 #define Stringify(x) Stringify2(x) 00063 00064 //========================================================================== 00065 //========================================================================== 00066 template<class T> 00067 inline bool IsIntegral(const T& t) 00068 { 00069 return t == int(t); 00070 } 00071 00072 /* 00073 //========================================================================== 00074 //========================================================================== 00075 template <class T> 00076 inline T sqr(const T& t){ 00077 return t*t; 00078 } 00079 */ 00080 00081 //============================================================================ 00082 //============================================================================ 00083 template <class PRECISION> 00084 RANGEPROMOTION(PRECISION) sqr(const PRECISION& r) 00085 { 00086 return r*r; 00087 } 00088 00089 //========================================================================== 00090 //========================================================================== 00091 template<class T> 00092 inline int sign(const T& t) 00093 { 00094 return t < 0 ? -1 : 1; 00095 } 00096 00097 //========================================================================== 00098 //========================================================================== 00099 template <class T1, class T2> 00100 inline const PROMOTION(T1,T2)& max(const T1& t1, const T2& t2) 00101 { 00102 return (t1>t2)?t1:t2; 00103 } 00104 00105 //========================================================================== 00106 //========================================================================== 00107 template <class T1, class T2> 00108 inline const PROMOTION(T1,T2)& min(const T1& t1, const T2& t2) 00109 { 00110 return (t1<t2)?t1:t2; 00111 } 00112 00113 //========================================================================== 00114 //========================================================================== 00115 template<class T> 00116 inline const T& Bound(const T& t, const T& tMin, const T& tMax) 00117 { 00118 return min(tMax, max(tMin, t)); 00119 } 00120 00121 //========================================================================== 00122 //========================================================================== 00123 template<class T> 00124 inline T Random(const T& tMin, const T& tMax) 00125 { 00126 T d = tMax - tMin; 00127 return T(Modulo(rand() % 1000000, d * 1000) / 1000 + tMin); 00128 } 00129 00130 //========================================================================== 00131 //========================================================================== 00132 template<class T> 00133 inline void Swap(T& t1, T& t2) 00134 { 00135 T tT = t1; 00136 t1 = t2; 00137 t2 = tT; 00138 } 00139 00140 //========================================================================== 00141 //========================================================================== 00142 template <class T1, class T2> 00143 PROMOTION(T1,T2) Modulo(T1 t, T2 tBase) 00144 { 00145 int cRem=int(t/tBase); 00146 return t-cRem*tBase; 00147 } 00148 00149 //========================================================================== 00150 //========================================================================== 00151 template <class T1, class T2> 00152 PROMOTION(T1,T2) AbsModulo(T1 t, T2 tBase) 00153 { 00154 PROMOTION(T1,T2) tMod=Modulo(t,tBase); 00155 return tMod<0?tMod+tBase:tMod; 00156 } 00157 00158 //========================================================================== 00159 //========================================================================== 00160 inline double timestamp() 00161 { 00162 struct timeval tv; 00163 gettimeofday(&tv, NULL); 00164 return tv.tv_sec + double(tv.tv_usec) / 1000000; 00165 } 00166 00167 //========================================================================== 00168 //========================================================================== 00169 #define DTIMEEXECUTION(_s,_code) { \ 00170 TIMEEXECUTION(_timer,_code); \ 00171 D(_s,_timer); \ 00172 } 00173 00174 //========================================================================== 00175 #define UDTIMEEXECUTION(_s,_code) { \ 00176 TIMEEXECUTION(_timer,_code); \ 00177 UD(_s,_timer); \ 00178 } 00179 00180 //========================================================================== 00181 //========================================================================== 00182 #define DTIMELOOPEXECUTION(_s,_c,_code) { \ 00183 TIMEEXECUTION(_timer,for (int _n=0; _n<_c; _n++){_code;}); \ 00184 D(_s,_timer); \ 00185 } 00186 00187 //========================================================================== 00188 //========================================================================== 00189 #define TICK(_timer) \ 00190 double _timer = timestamp(); 00191 00192 //========================================================================== 00193 //========================================================================== 00194 #define TOCK(_timer) \ 00195 (timestamp() - _timer ) 00196 00197 //========================================================================== 00198 //========================================================================== 00199 #define DTOCK(_s,_timer) \ 00200 D(_s,TOCK( _timer ) ) 00201 00202 //========================================================================== 00203 //========================================================================== 00204 #define UDTOCK(_s,_timer) \ 00205 UD(_s,TOCK( _timer ) ) 00206 00207 //========================================================================== 00208 //========================================================================== 00209 template<class T> 00210 void SafeDelete(T*& pt) 00211 { 00212 if (pt != NULL) 00213 { 00214 delete pt; 00215 } 00216 } 00217 //========================================================================== 00218 //========================================================================== 00219 template<class T> 00220 void SafeDeleteArray(T*& pt) 00221 { 00222 if (pt != NULL) 00223 { 00224 delete[] pt; 00225 } 00226 } 00227 00228 #undef DEBUG 00229 #define DEBUG 9 00230 #include "libDebug.h" 00231 00232 #endif //#ifndef __LIBUTILITY_H__
1.6.1