00001
00002
00003
00004 #ifndef IBIS_UTIL_H
00005 #define IBIS_UTIL_H
00006
00007
00008
00009
00010 #if defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG)
00011 #define _CRTDBG_MAP_ALLOC
00012 #include <stdlib.h>
00013 #include <crtdbg.h>
00014 #else
00015 #include <stdlib.h>
00016 #endif
00017 #include "const.h"
00018
00019 #include <cctype>
00020 #include <cstring>
00021 #include <stdio.h>
00022
00023 #include <sys/stat.h>
00024
00025
00026 #include <fcntl.h>
00027
00028 #include <map>
00029 #include <string>
00030 #include <limits>
00031 #include <sstream>
00032
00033 #include <float.h>
00034 #include <math.h>
00035 #if !defined(unix) && defined(_WIN32)
00036 #include <windows.h>
00037 #include <io.h>
00038 #include <fcntl.h>
00039 int truncate(const char*, uint32_t);
00040 #include <direct.h>
00041 #include <sys/stat.h>
00042 #define rmdir _rmdir
00043
00044 #else
00045 #include <unistd.h>
00046 #endif
00047
00048
00049 #ifndef FASTBIT_MIN_MAP_SIZE
00050 #define FASTBIT_MIN_MAP_SIZE 1048576
00051 #endif
00052
00053 #if ! (defined(HAVE_MMAP) || defined(_MSC_VER))
00054 # if defined(_XOPEN_SOURCE)
00055 # define HAVE_MMAP _XOPEN_SOURCE - 0 >= 500
00056 # elif defined(_POSIX_C_SOURCE)
00057 # define HAVE_MMAP _POSIX_C_SOURCE - 0 >= 0
00058 # else
00059 # define HAVE_MMAP defined(unix)||defined(linux)||defined(__APPLE__)||defined(__CYGWIN__)
00060 # endif
00061 #endif
00062
00063 #if (HAVE_MMAP+0>0) || (defined(_WIN32) && defined(_MSC_VER))
00064 #define HAVE_FILE_MAP 1
00065 #endif
00066
00067 #ifndef DBL_EPSILON
00068 #define DBL_EPSILON 2.2204460492503131e-16
00069 #else
00070
00071 #undef DBL_EPSILON
00072 #define DBL_EPSILON 2.2204460492503131e-16
00073 #endif
00074
00075 #ifndef FASTBIT_FLOAT_NULL
00076 #define FASTBIT_FLOAT_NULL std::numeric_limits<float>::quiet_NaN()
00077 #endif
00078 #ifndef FASTBIT_DOUBLE_NULL
00079 #define FASTBIT_DOUBLE_NULL std::numeric_limits<double>::quiet_NaN()
00080 #endif
00081
00083 #if !defined(HAVE_GCC_ATOMIC32) && defined(WITHOUT_FASTBIT_CONFIG_H)
00084 #if __GNUC__+0 >= 4 && defined(LINUX)
00085 #define HAVE_GCC_ATOMIC32 2
00086 #endif
00087 #endif
00088 #if !defined(HAVE_GCC_ATOMIC64) && defined(WITHOUT_FASTBIT_CONFIG_H)
00089 #if defined(__IA64__) || defined(__x86_64__) || defined(__ppc64__)
00090 #if __GNUC__+0 >= 4 && defined(LINUX)
00091 #define HAVE_GCC_ATOMIC64 2
00092 #endif
00093 #endif
00094 #endif
00095
00097 #if defined(_MSC_VER) && defined(_WIN32)
00098 #ifndef HAVE_WIN_ATOMIC32
00099 #if defined(NTDDI_VERSION) && defined(NTDDI_WIN2K)
00100 #if NTDDI_VERSION >= NTDDI_WIN2K
00101 #define HAVE_WIN_ATOMIC32
00102 #endif
00103 #elif defined(WINVER)
00104 #if WINVER >= 0x0500
00105 #define HAVE_WIN_ATOMIC32
00106 #endif
00107 #endif
00108 #endif
00109 #ifndef HAVE_WIN_ATOMIC64
00110 #if defined(NTDDI_VERSION) && defined(NTDDI_WINVISTA)
00111 #if NTDDI_VERSION >= NTDDI_WINVISTA
00112 #define HAVE_WIN_ATOMIC64
00113 #endif
00114 #elif defined(WINVER)
00115 #if WINVER >= 0x0600
00116 #define HAVE_WIN_ATOMIC64
00117 #endif
00118 #endif
00119 #endif
00120 #endif
00121
00122
00123 #if defined(_MSC_VER) && defined(_WIN32)
00124 #define UnixOpen ::_open
00125 #define UnixClose ::_close
00126 #define UnixRead ::_read
00127 #define UnixWrite ::_write
00128 #define UnixSeek ::_lseek
00129 #define UnixFlush ::_commit
00130 #define UnixSnprintf ::_snprintf
00131 #define UnixStat ::_stat
00132 #define UnixFStat ::_fstat
00133 #define Stat_T struct _stat
00134 #else
00135 #define UnixOpen ::open
00136 #define UnixClose ::close
00137 #define UnixRead ::read
00138 #define UnixWrite ::write
00139 #define UnixSeek ::lseek
00140 #define UnixFlush ::fsync
00141 #define UnixSnprintf ::snprintf
00142 #define UnixStat ::stat
00143 #define UnixFStat ::fstat
00144 #define Stat_T struct stat
00145 #endif
00146
00147
00148 #if defined(O_RDONLY)
00149 #if defined(O_LARGEFILE)
00150 #if defined(O_BINARY)
00151 #define OPEN_READONLY O_RDONLY | O_BINARY | O_LARGEFILE
00152 #else
00153 #define OPEN_READONLY O_RDONLY | O_LARGEFILE
00154 #endif
00155 #elif defined(O_BINARY)
00156 #define OPEN_READONLY O_RDONLY | O_BINARY
00157 #else
00158 #define OPEN_READONLY O_RDONLY
00159 #endif
00160 #elif defined(_O_RDONLY)
00161 #if defined(_O_LARGEFILE)
00162 #define OPEN_READONLY _O_RDONLY | _O_LARGEFILE | _O_BINARY
00163 #else
00164 #define OPEN_READONLY _O_RDONLY | _O_BINARY
00165 #endif
00166 #endif
00167
00168 #if defined(O_WRONLY)
00169 #if defined(O_LARGEFILE)
00170 #if defined(O_BINARY)
00171 #define OPEN_WRITENEW O_WRONLY | O_BINARY | O_CREAT | O_TRUNC | O_LARGEFILE
00172 #else
00173 #define OPEN_WRITENEW O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE
00174 #endif
00175 #elif defined(O_BINARY)
00176 #define OPEN_WRITENEW O_WRONLY | O_BINARY | O_CREAT | O_TRUNC
00177 #else
00178 #define OPEN_WRITENEW O_WRONLY | O_CREAT | O_TRUNC
00179 #endif
00180 #elif defined(_O_WRONLY)
00181 #if defined(_O_LARGEFILE)
00182 #define OPEN_WRITENEW _O_WRONLY|_O_CREAT|_O_TRUNC|_O_LARGEFILE|_O_BINARY
00183 #else
00184 #define OPEN_WRITENEW _O_WRONLY|_O_CREAT|_O_TRUNC|_O_BINARY
00185 #endif
00186 #endif
00187
00188 #if defined(O_WRONLY)
00189 #if defined(O_LARGEFILE)
00190 #if defined(O_BINARY)
00191 #define OPEN_WRITEADD O_WRONLY | O_BINARY | O_CREAT | O_LARGEFILE
00192 #else
00193 #define OPEN_WRITEADD O_WRONLY | O_CREAT| O_LARGEFILE
00194 #endif
00195 #elif defined(O_BINARY)
00196 #define OPEN_WRITEADD O_WRONLY | O_BINARY | O_CREAT
00197 #else
00198 #define OPEN_WRITEADD O_WRONLY | O_CREAT
00199 #endif
00200 #elif defined(_O_WRONLY)
00201 #if defined(_O_LARGEFILE)
00202 #define OPEN_WRITEADD _O_WRONLY | _O_CREAT | _O_LARGEFILE | _O_BINARY
00203 #else
00204 #define OPEN_WRITEADD _O_WRONLY | _O_CREAT | _O_BINARY
00205 #endif
00206 #endif
00207
00208 #if defined(O_RDWR)
00209 #if defined(O_LARGEFILE)
00210 #if defined(O_BINARY)
00211 #define OPEN_READWRITE O_RDWR | O_BINARY | O_CREAT | O_LARGEFILE
00212 #else
00213 #define OPEN_READWRITE O_RDWR | O_CREAT | O_LARGEFILE
00214 #endif
00215 #elif defined(O_BINARY)
00216 #define OPEN_READWRITE O_RDWR | O_BINARY | O_CREAT
00217 #else
00218 #define OPEN_READWRITE O_RDWR | O_CREAT
00219 #endif
00220 #elif defined(_O_RDWR)
00221 #if defined(_O_LARGEFILE)
00222 #define OPEN_READWRITE _O_RDWR | _O_CREAT | _O_LARGEFILE | _O_BINARY
00223 #else
00224 #define OPEN_READWRITE _O_RDWR | _O_CREAT | _O_BINARY
00225 #endif
00226 #endif
00227
00228 #if defined(O_WRONLY)
00229 #if defined(O_LARGEFILE)
00230 #if defined(O_BINARY)
00231 #define OPEN_APPENDONLY O_WRONLY | O_BINARY | O_CREAT | O_APPEND | O_LARGEFILE
00232 #else
00233 #define OPEN_APPENDONLY O_WRONLY | O_CREAT | O_APPEND | O_LARGEFILE
00234 #endif
00235 #elif defined(O_BINARY)
00236 #define OPEN_APPENDONLY O_WRONLY | O_BINARY | O_CREAT | O_APPEND
00237 #else
00238 #define OPEN_APPENDONLY O_WRONLY | O_CREAT | O_APPEND
00239 #endif
00240 #elif defined(_O_WRONLY)
00241 #if defined(_O_LARGEFILE)
00242 #define OPEN_APPENDONLY _O_WRONLY | _O_CREAT | _O_APPEND | _O_LARGEFILE | _O_BINARY
00243 #else
00244 #define OPEN_APPENDONLY _O_WRONLY | _O_CREAT | _O_APPEND | _O_BINARY
00245 #endif
00246 #endif
00247
00248 #if defined(_MSC_VER) && defined(_WIN32)
00249 #define OPEN_FILEMODE _S_IREAD | _S_IWRITE
00250 #elif defined(S_IRGRP) && defined(S_IWGRP) && defined(S_IROTH)
00251 #define OPEN_FILEMODE S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH
00252 #else
00253 #define OPEN_FILEMODE S_IRUSR | S_IWUSR
00254 #endif
00255
00256 #if defined(_WIN32) && defined(_MSC_VER)
00257
00258 #ifdef max
00259 #undef max
00260 #endif
00261 #ifdef min
00262 #undef min
00263 #endif
00264 #endif
00265
00266 #ifndef FASTBIT_CS_PATTERN_MATCH
00267
00268
00269
00270 #define FASTBIT_CS_PATTERN_MATCH 1
00271 #endif
00272
00273
00274 #define STRMATCH_META_CSH_ANY '*'
00275 #define STRMATCH_META_CSH_ONE '?'
00276 #define STRMATCH_META_SQL_ANY '%'
00277 #define STRMATCH_META_SQL_ONE '_'
00278 #define STRMATCH_META_ESCAPE '\\'
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289 #define LOGGER(v) \
00290 if (false == (v)) ; else ibis::util::logger(0)()
00291
00292
00293 #define IBIS_JOIN_MACRO2(X, Y) X##Y
00294 #define IBIS_JOIN_MACRO(X, Y) IBIS_JOIN_MACRO2(X, Y)
00295 #ifdef __GNUC__
00296 #define IBIS_GUARD_NAME IBIS_JOIN_MACRO(_guard, __LINE__) __attribute__( ( unused ) )
00297 #else
00298 #define IBIS_GUARD_NAME IBIS_JOIN_MACRO(_guard, __LINE__)
00299 #endif
00300 #define IBIS_BLOCK_GUARD \
00301 ibis::util::guard IBIS_GUARD_NAME = ibis::util::makeGuard
00302
00303 namespace std {
00304
00305 template <> struct less< char* > {
00306 bool operator()(const char*x, const char*y) const {
00307 return strcmp(x, y) < 0;
00308 }
00309 };
00310
00311
00312 template <> struct less< const char* > {
00313 bool operator()(const char* x, const char* y) const {
00314 return strcmp(x, y) < 0;
00315 }
00316 };
00317
00318 template <> struct less< ibis::rid_t > {
00319 bool operator()(const ibis::rid_t& x, const ibis::rid_t& y) const {
00320 return (x < y);
00321 }
00322 };
00323
00324 template <> struct less< const ibis::rid_t* > {
00325 bool operator()(const ibis::rid_t* x, const ibis::rid_t* y) const {
00326 return (*x < *y);
00327 }
00328 };
00329 }
00330
00331 namespace ibis {
00334 class resource;
00335 class bitvector;
00336 class column;
00337 class fileManager;
00338 class horometer;
00339 class index;
00340 class roster;
00341 class bitvector64;
00342
00343 class dictionary;
00344 class bundle;
00345 class colValues;
00346
00347 class fromClause;
00348 class whereClause;
00349 class selectClause;
00350
00351
00353 extern FASTBIT_CXX_DLLSPEC partList datasets;
00354
00355 typedef std::vector<colValues*> colList;
00356
00358 FASTBIT_CXX_DLLSPEC ibis::resource& gParameters();
00359
00362 class FASTBIT_CXX_DLLSPEC nameList {
00363 public:
00364 nameList() : cstr(0), buff(0) {};
00365 nameList(const char* str) : cstr(0), buff(0) {select(str);}
00366 ~nameList() {if (cstr) clear();}
00367
00368 bool empty() const {return cstr == 0;}
00369 const char* operator*() const {return cstr;};
00370 uint32_t size() const {return cvec.size();};
00371
00373 void select(const char* str);
00375 void add(const char* str);
00379 uint32_t find(const char* key) const;
00380
00381 const char* operator[](uint32_t i) const {return cvec[i];}
00382 typedef std::vector< const char* >::const_iterator const_iterator;
00383 const_iterator begin() const {return cvec.begin();}
00384 const_iterator end() const {return cvec.end();}
00385
00386 void clear()
00387 {cvec.clear(); delete [] cstr; delete [] buff; buff=0; cstr=0;}
00388
00389 private:
00390 typedef std::vector< const char * > compStore;
00391 char* cstr;
00392 char* buff;
00393 compStore cvec;
00394
00395 nameList(const nameList&);
00396 nameList& operator=(const nameList&);
00397 };
00398
00401 typedef std::map< const char*, part*, lessi > partAssoc;
00402
00404 class bad_alloc : public std::bad_alloc {
00405 public:
00409 bad_alloc(const char *m="unknown") throw() : mesg_(m) {};
00410 virtual ~bad_alloc() throw() {}
00411 virtual const char* what() const throw() {return mesg_;}
00412
00413 private:
00416 const char *mesg_;
00417 };
00418
00420 namespace util {
00422 extern const char* charTable;
00424 extern const short unsigned charIndex[];
00426 extern const char* delimiters;
00428 extern const int log2table[256];
00435 extern FASTBIT_CXX_DLLSPEC pthread_mutex_t envLock;
00436
00438 inline char* trim(char* str);
00440 char* strnewdup(const char* s);
00442 char* strnewdup(const char* s, const uint32_t n);
00444 inline void removeTail(char* str, char tail);
00445 char* getString(const char* buf);
00446 const char* getToken(char*& str, const char* tok_chrs);
00447 int readInt(int64_t& val, const char *&str,
00448 const char* del=ibis::util::delimiters);
00449 int readUInt(uint64_t& val, const char *&str,
00450 const char* del=ibis::util::delimiters);
00451 int readDouble(double& val, const char *&str,
00452 const char* del=ibis::util::delimiters);
00453 FASTBIT_CXX_DLLSPEC int readString(std::string& str, const char*& buf,
00454 const char *delim=0);
00455
00458 void removeDir(const char* name, bool leaveDir=false);
00460 int makeDir(const char* dir);
00463 FASTBIT_CXX_DLLSPEC off_t getFileSize(const char* name);
00466 int copy(const char* to, const char* from);
00467
00471 inline void setVerboseLevel(int v) {ibis::gVerbose=v;}
00473 FASTBIT_CXX_DLLSPEC const char* userName();
00475 uint32_t serialNumber();
00476 void uniformFraction(const long unsigned idx,
00477 long unsigned &denominator,
00478 long unsigned &numerator);
00479 inline double rand();
00480
00482 FASTBIT_CXX_DLLSPEC uint32_t checksum(const char* str, uint32_t sz);
00483 inline uint32_t checksum(uint32_t a, uint32_t b);
00484 std::string shortName(const std::string& longname);
00485 std::string randName(const std::string& longname);
00487
00489 void int2string(std::string &str, unsigned val);
00490 void int2string(std::string &str, unsigned v1, unsigned v2);
00491 void int2string(std::string &str, unsigned v1,
00492 unsigned v2, unsigned v3);
00493 void int2string(std::string &str, const std::vector<unsigned>& val);
00494 void encode64(uint64_t, std::string&);
00495 int decode64(uint64_t&, const std::string&);
00496 int decode16(uint64_t&, const char*);
00497 std::string groupby1000(uint64_t);
00499
00502 double incrDouble(const double&);
00503 double decrDouble(const double&);
00504 void eq2range(const double&, double&, double&);
00507 inline double coarsen(const double in, unsigned prec=2);
00510 double compactValue(double left, double right,
00511 double start=0.0);
00512
00515 double compactValue2(double left, double right,
00516 double start=0.0);
00517
00519 void setNaN(double& val);
00520 void setNaN(float& val);
00521
00525 template <typename Tin, typename Tout>
00526 void round_down(const Tin& inval, Tout& outval) {
00527 outval = ((double)std::numeric_limits<Tout>::min() > inval ?
00528 std::numeric_limits<Tout>::min() :
00529 (double)std::numeric_limits<Tout>::max() <= inval ?
00530 std::numeric_limits<Tout>::max() :
00531 static_cast<Tout>(inval));
00532 }
00536 template <typename Tin, typename Tout>
00537 void round_up(const Tin& inval, Tout& outval) {
00538 outval = ((double)std::numeric_limits<Tout>::min() >= inval ?
00539 std::numeric_limits<Tout>::min() :
00540 (double) std::numeric_limits<Tout>::max() < inval ?
00541 std::numeric_limits<Tout>::max() :
00542 static_cast<Tout>(inval) +
00543 ((inval-static_cast<Tin>(static_cast<Tout>(inval))) > 0));
00544 }
00546 template <typename Tin>
00547 void round_up(const Tin& inval, float&);
00549 template <typename Tin>
00550 void round_up(const Tin& inval, double& outval) {
00551 outval = static_cast<double>(inval);
00552 }
00553
00555 inline int log2(uint32_t x) {
00556 register uint32_t xx, xxx;
00557 return (xx = x >> 16)
00558 ? (xxx = xx >> 8) ? 24 + log2table[xxx] : 16 + log2table[xx]
00559 : (xxx = x >> 8) ? 8 + log2table[xxx] : log2table[x];
00560 }
00562 inline int log2(uint64_t x) {
00563 register uint32_t xx;
00564 return (xx = x >> 32)
00565 ? 32 + log2(xx)
00566 : log2(static_cast<uint32_t>(x));
00567 }
00569
00570 FASTBIT_CXX_DLLSPEC void
00571 logMessage(const char* event, const char* fmt, ...);
00572 FASTBIT_CXX_DLLSPEC FILE* getLogFile();
00573 int writeLogFileHeader(FILE *fptr, const char* fname);
00574 FASTBIT_CXX_DLLSPEC void closeLogFile();
00575 FASTBIT_CXX_DLLSPEC int setLogFileName(const char* filename);
00576 FASTBIT_CXX_DLLSPEC const char* getLogFileName();
00577
00581 FASTBIT_CXX_DLLSPEC bool strMatch(const char* str, const char* pat);
00582
00584 const ibis::bitvector64& outerProduct(const ibis::bitvector& a,
00585 const ibis::bitvector& b,
00586 ibis::bitvector64& c);
00589 const ibis::bitvector64& outerProductUpper(const ibis::bitvector& a,
00590 const ibis::bitvector& b,
00591 ibis::bitvector64& c);
00592
00594 long intersect(const std::vector<ibis::bitvector> &bits1,
00595 const std::vector<ibis::bitvector> &bits2,
00596 std::vector<ibis::bitvector> &res);
00598 long intersect(const std::vector<ibis::bitvector> &bits1,
00599 const std::vector<ibis::bitvector> &bits2,
00600 const std::vector<ibis::bitvector> &bits3,
00601 std::vector<ibis::bitvector> &res);
00602 void clear(ibis::array_t<ibis::bitvector*> &bv) throw();
00603 void clear(ibis::partList &pl) throw();
00604 void updateDatasets(void);
00605
00608 inline const char* getVersionString() {
00609 return FASTBIT_STRING;
00610 }
00617 inline int getVersionNumber() {
00618 #ifdef FASTBIT_IBIS_INT_VERSION
00619 return FASTBIT_IBIS_INT_VERSION;
00620 #else
00621 return 1020800;
00622 #endif
00623 }
00624
00626 void getLocalTime(char *str);
00628 void getGMTime(char *str);
00629 void secondsToString(const time_t, char *str);
00630
00631
00632 #if defined(WIN32) && ! defined(__CYGWIN__)
00633 char* getpass_r(const char *prompt, char *buffer, uint32_t buflen);
00634 char* getpass(const char* prompt);
00635 #else
00636 inline char *itoa(int value, char *str, int ) {
00637 sprintf(str,"%d",value);
00638 return str;
00639 }
00640 #endif
00641
00643 template <typename T> inline void
00644 clearVec(std::vector<T*> &v) {
00645 const size_t nv = v.size();
00646 for (size_t j = 0; j < nv; ++j)
00647 delete v[j];
00648 v.clear();
00649 }
00650
00659 class FASTBIT_CXX_DLLSPEC logger {
00660 public:
00662 logger(int blanks=0);
00664 ~logger();
00666 std::ostream& operator()(void) {return mybuffer;}
00667
00668 protected:
00670 std::ostringstream mybuffer;
00671
00672 private:
00673 logger(const logger&);
00674 logger& operator=(const logger&);
00675 };
00676
00679 class ioLock {
00680 public:
00681 ioLock() {
00682 #if defined(PTW32_STATIC_LIB)
00683 if (mutex == PTHREAD_MUTEX_INITIALIZER) {
00684 int ierr = pthread_mutex_init(&mutex, 0);
00685 if (ierr != 0)
00686 throw "ioLock failed to initialize the necessary mutex";
00687 }
00688 #endif
00689 if (0 != pthread_mutex_lock(&mutex))
00690 throw "ioLock failed to obtain a lock";
00691 }
00692 ~ioLock() {
00693 (void) pthread_mutex_unlock(&mutex);
00694 }
00695 private:
00696
00697 static pthread_mutex_t mutex;
00698
00699 ioLock(const ioLock&) {};
00700 ioLock& operator=(const ioLock&);
00701 };
00702
00704 class mutexLock {
00705 public:
00706 mutexLock(pthread_mutex_t* lk, const char* m)
00707 : mesg(m), lock(lk) {
00708 LOGGER(ibis::gVerbose > 10)
00709 << "util::mutexLock -- acquiring lock (" << lock
00710 << ") for " << mesg;
00711 if (0 != pthread_mutex_lock(lock)) {
00712 throw "mutexLock failed to obtain a lock";
00713 }
00714 }
00715 ~mutexLock() {
00716 LOGGER(ibis::gVerbose > 10)
00717 << "util::mutexLock -- releasing lock (" << lock
00718 << ") for " << mesg;
00719 (void) pthread_mutex_unlock(lock);
00720 }
00721
00722 private:
00723 const char *mesg;
00724 pthread_mutex_t *lock;
00725
00726 mutexLock() : mesg(0), lock(0) {};
00727 mutexLock(const mutexLock&);
00728 mutexLock& operator=(const mutexLock&);
00729 };
00730
00734 class quietLock {
00735 public:
00736 quietLock(pthread_mutex_t *lk) : lock(lk) {
00737 if (0 != pthread_mutex_lock(lock))
00738 throw "quietLock failed to obtain a mutex lock";
00739 }
00740 ~quietLock() {
00741 (void) pthread_mutex_unlock(lock);
00742 }
00743
00744 private:
00745 pthread_mutex_t *lock;
00746
00747 quietLock();
00748 quietLock(const quietLock&);
00749 quietLock& operator=(const quietLock&);
00750 };
00751
00753 class readLock {
00754 public:
00755 readLock(pthread_rwlock_t* lk, const char* m)
00756 : mesg(m), lock(lk) {
00757 if (0 != pthread_rwlock_rdlock(lock)) {
00758 throw "readLock failed to obtain a lock";
00759 }
00760 }
00761 ~readLock() {
00762 (void) pthread_rwlock_unlock(lock);
00763 }
00764
00765 private:
00766 const char *mesg;
00767 pthread_rwlock_t *lock;
00768
00769 readLock() : mesg(0), lock(0) {};
00770 readLock(const readLock&);
00771 readLock& operator=(const readLock&);
00772 };
00773
00775 class writeLock {
00776 public:
00777 writeLock(pthread_rwlock_t* lk, const char* m)
00778 : mesg(m), lock(lk) {
00779 if (0 != pthread_rwlock_wrlock(lock)) {
00780 throw "writeLock failed to obtain a lock";
00781 }
00782 }
00783 ~writeLock() {
00784 int ierr = pthread_rwlock_unlock(lock);
00785 if (ierr != 0) {
00786 throw "writeLock failed to release the lock";
00787 }
00788 }
00789
00790 private:
00791 const char *mesg;
00792 pthread_rwlock_t *lock;
00793
00794 writeLock() : mesg(0), lock(0) {};
00795 writeLock(const writeLock&);
00796 writeLock& operator=(const writeLock&);
00797 };
00798
00805 class FASTBIT_CXX_DLLSPEC counter {
00806 public:
00807 ~counter() {
00808 #if defined(HAVE_GCC_ATOMIC32)
00809 #elif defined(HAVE_WIN_ATOMIC32)
00810 #else
00811 (void)pthread_mutex_destroy(&lock_);
00812 #endif
00813 }
00814 counter() : count_(0) {
00815 #if defined(HAVE_GCC_ATOMIC32)
00816 #elif defined(HAVE_WIN_ATOMIC32)
00817 #else
00818 if (0 != pthread_mutex_init(&lock_, 0))
00819 throw ibis::bad_alloc
00820 ("util::counter failed to initialize mutex lock");
00821 #endif
00822 }
00823
00825 uint32_t operator()() {
00826 #if defined(HAVE_GCC_ATOMIC32)
00827 return __sync_fetch_and_add(&count_, 1);
00828 #elif defined(HAVE_WIN_ATOMIC32)
00829 return InterlockedIncrement((volatile long *)&count_)-1;
00830 #else
00831 ibis::util::quietLock lck(&lock_);
00832 uint32_t ret = count_;
00833 ++ count_;
00834 return ret;
00835 #endif
00836 }
00838 void reset() {
00839 #if defined(HAVE_GCC_ATOMIC32)
00840 (void) __sync_fetch_and_sub(&count_, count_);
00841 #elif defined(HAVE_WIN_ATOMIC32)
00842 (void) InterlockedExchange((volatile long *)&count_, 0);
00843 #else
00844 ibis::util::quietLock lck(&lock_);
00845 count_ = 0;
00846 #endif
00847 }
00849 uint32_t value() const {
00850 return count_;
00851 }
00852
00853 private:
00854 #if defined(HAVE_GCC_ATOMIC32)
00855 #elif defined(HAVE_WIN_ATOMIC32)
00856 #else
00857 mutable pthread_mutex_t lock_;
00858 #endif
00859 volatile uint32_t count_;
00860
00862 counter(const counter&);
00864 counter& operator=(const counter&);
00865 };
00866
00881 class FASTBIT_CXX_DLLSPEC sharedInt32 {
00882 public:
00883 sharedInt32() : val_(0) {
00884 #if defined(HAVE_GCC_ATOMIC32)
00885 #elif defined(HAVE_WIN_ATOMIC32)
00886 #else
00887 if (pthread_mutex_init(&mytex, 0) != 0)
00888 throw "pthread_mutex_init failed for sharedInt";
00889 #endif
00890 }
00891
00892 ~sharedInt32() {
00893 #if defined(HAVE_GCC_ATOMIC32)
00894 #elif defined(HAVE_WIN_ATOMIC32)
00895 #else
00896 (void)pthread_mutex_destroy(&mytex);
00897 #endif
00898 }
00899
00901 uint32_t operator()() const {return val_;}
00902
00904 uint32_t operator++() {
00905 #if defined(HAVE_GCC_ATOMIC32)
00906 return __sync_add_and_fetch(&val_, 1);
00907 #elif defined(HAVE_WIN_ATOMIC32)
00908 return InterlockedIncrement((volatile long *)&val_);
00909 #else
00910 ibis::util::quietLock lock(&mytex);
00911 ++ val_;
00912 return val_;
00913 #endif
00914 }
00915
00917 uint32_t operator--() {
00918 #if defined(HAVE_GCC_ATOMIC32)
00919 return __sync_sub_and_fetch(&val_, 1);
00920 #elif defined(HAVE_WIN_ATOMIC32)
00921 return InterlockedDecrement((volatile long *)&val_);
00922 #else
00923 ibis::util::quietLock lock(&mytex);
00924 -- val_;
00925 return val_;
00926 #endif
00927 }
00928
00930 void operator+=(const uint32_t rhs) {
00931 #if defined(HAVE_GCC_ATOMIC32)
00932 (void) __sync_add_and_fetch(&val_, rhs);
00933 #elif defined(HAVE_WIN_ATOMIC32)
00934 (void) InterlockedExchangeAdd((volatile long *)&val_, rhs);
00935 #else
00936 ibis::util::quietLock lock(&mytex);
00937 val_ += rhs;
00938 #endif
00939 }
00940
00942 void operator-=(const uint32_t rhs) {
00943 #if defined(HAVE_GCC_ATOMIC32)
00944 (void) __sync_sub_and_fetch(&val_, rhs);
00945 #elif defined(HAVE_WIN_ATOMIC32)
00946 (void) InterlockedExchangeAdd((volatile long *)&val_,
00947 -(long)rhs);
00948 #else
00949 ibis::util::quietLock lock(&mytex);
00950 val_ -= rhs;
00951 #endif
00952 }
00953
00955 void swap(sharedInt32 &rhs) {
00956 uint32_t tmp = rhs.val_;
00957 rhs.val_ = val_;
00958 val_ = tmp;
00959 }
00960
00961 private:
00962 uint32_t volatile val_;
00963 #if defined(HAVE_GCC_ATOMIC32)
00964 #elif defined(HAVE_WIN_ATOMIC32)
00965 #else
00966 pthread_mutex_t mytex;
00967 #endif
00968
00969 sharedInt32(const sharedInt32&);
00970 sharedInt32& operator=(const sharedInt32&);
00971 };
00972
00979 class sharedInt64 {
00980 public:
00981 sharedInt64() : val_(0) {
00982 #if defined(HAVE_GCC_ATOMIC64)
00983 #elif defined(HAVE_WIN_ATOMIC64)
00984 #else
00985 if (pthread_mutex_init(&mytex, 0) != 0)
00986 throw "pthread_mutex_init failed for sharedInt";
00987 #endif
00988 }
00989
00990 ~sharedInt64() {
00991 #if defined(HAVE_GCC_ATOMIC64)
00992 #elif defined(HAVE_WIN_ATOMIC64)
00993 #else
00994 (void)pthread_mutex_destroy(&mytex);
00995 #endif
00996 }
00997
00999 uint64_t operator()() const {return val_;}
01000
01002 uint64_t operator++() {
01003 #if defined(HAVE_GCC_ATOMIC64)
01004 return __sync_add_and_fetch(&val_, 1);
01005 #elif defined(HAVE_WIN_ATOMIC64)
01006 return InterlockedIncrement64((volatile LONGLONG *)&val_);
01007 #else
01008 ibis::util::quietLock lock(&mytex);
01009 ++ val_;
01010 return val_;
01011 #endif
01012 }
01013
01015 uint64_t operator--() {
01016 #if defined(HAVE_GCC_ATOMIC64)
01017 return __sync_sub_and_fetch(&val_, 1);
01018 #elif defined(HAVE_WIN_ATOMIC64)
01019 return InterlockedDecrement64((volatile LONGLONG *)&val_);
01020 #else
01021 ibis::util::quietLock lock(&mytex);
01022 -- val_;
01023 return val_;
01024 #endif
01025 }
01026
01028 void operator+=(const uint64_t rhs) {
01029 #if defined(HAVE_GCC_ATOMIC64)
01030 (void) __sync_add_and_fetch(&val_, rhs);
01031 #elif defined(HAVE_WIN_ATOMIC64)
01032 (void) InterlockedExchangeAdd64((volatile LONGLONG *)&val_,
01033 rhs);
01034 #else
01035 ibis::util::quietLock lock(&mytex);
01036 val_ += rhs;
01037 #endif
01038 }
01039
01041 void operator-=(const uint64_t rhs) {
01042 #if defined(HAVE_GCC_ATOMIC64)
01043 (void) __sync_sub_and_fetch(&val_, rhs);
01044 #elif defined(HAVE_WIN_ATOMIC64)
01045 (void) InterlockedExchangeAdd64((volatile LONGLONG *)&val_,
01046 -(long)rhs);
01047 #else
01048 ibis::util::quietLock lock(&mytex);
01049 val_ -= rhs;
01050 #endif
01051 }
01052
01054 void swap(sharedInt64 &rhs) {
01055 uint64_t tmp = rhs.val_;
01056 rhs.val_ = val_;
01057 val_ = tmp;
01058 }
01059
01060 private:
01061 uint64_t volatile val_;
01062 #if defined(HAVE_GCC_ATOMIC64)
01063 #elif defined(HAVE_WIN_ATOMIC64)
01064 #else
01065 pthread_mutex_t mytex;
01066 #endif
01067
01068 sharedInt64(const sharedInt64&);
01069 sharedInt64& operator=(const sharedInt64&);
01070 };
01071
01081 class timer {
01082 public:
01083 explicit timer(const char* msg, int lvl=1);
01084 ~timer();
01085
01086 private:
01087 ibis::horometer *chrono_;
01088 std::string mesg_;
01089
01090 timer();
01091 timer(const timer&);
01092 timer& operator=(const timer&);
01093 };
01094
01096 template <class T> class refHolder {
01097 public:
01098 refHolder(T& r) : ref_(r) {}
01099 operator T& () const {return ref_;}
01100
01101 private:
01102 T& ref_;
01103
01104 refHolder();
01105 };
01106
01108 template <class T>
01109 inline refHolder<T> ref(T& r) {return refHolder<T>(r);}
01110
01113 class guardBase {
01114 public:
01117 void dismiss() const {done_ = true;}
01118
01119 protected:
01120 mutable volatile bool done_;
01121
01123 ~guardBase() {};
01124 guardBase() : done_(false) {};
01125
01126
01127 guardBase(const guardBase& rhs) : done_(rhs.done_) {
01128 rhs.dismiss();
01129 }
01130
01133 template <typename T>
01134 static void cleanup(T& task) throw () {
01135 try {
01136 if (!task.done_)
01137 task.execute();
01138 }
01139 catch (const std::exception& e) {
01140 LOGGER(ibis::gVerbose > 1)
01141 << " ... caught a std::exception (" << e.what()
01142 << ") in util::gard";
01143 }
01144 catch (const char* s) {
01145 LOGGER(ibis::gVerbose > 1)
01146 << " ... caught a string exception (" << s
01147 << ") in util::guard";
01148 }
01149 catch (...) {
01150 LOGGER(ibis::gVerbose > 1)
01151 << " ... caught a unknown exception in util::guard";
01152 }
01153 task.done_ = true;
01154 }
01155 };
01156
01170 typedef const guardBase& guard;
01171
01174 template <typename F>
01175 class guardImpl0 : public guardBase {
01176 public:
01177 static guardImpl0<F> makeGuard(F f) {
01178 return guardImpl0<F>(f);
01179 }
01180
01182 ~guardImpl0() {cleanup(*this);}
01183
01184 protected:
01185 friend class guardBase;
01186 void execute() {fun_();}
01187
01189 explicit guardImpl0(F f) : fun_(f) {}
01190
01191 private:
01193 F fun_;
01194
01195 guardImpl0();
01196 guardImpl0& operator=(const guardImpl0&);
01197 };
01198
01199 template <typename F>
01200 inline guardImpl0<F> makeGuard(F f) {
01201 return guardImpl0<F>::makeGuard(f);
01202 }
01203
01206 template <typename F, typename A>
01207 class guardImpl1 : public guardBase {
01208 public:
01209 static guardImpl1<F, A> makeGuard(F f, A a) {
01210 return guardImpl1<F, A>(f, a);
01211 }
01212
01214 ~guardImpl1() {cleanup(*this);}
01215
01216 protected:
01217 friend class guardBase;
01218 void execute() {fun_(arg_);}
01219
01221 explicit guardImpl1(F f, A a) : fun_(f), arg_(a) {}
01222
01223 private:
01225 F fun_;
01227 A arg_;
01228
01229 guardImpl1();
01230 guardImpl1& operator=(const guardImpl1&);
01231 };
01232
01233 template <typename F, typename A>
01234 inline guardImpl1<F, A> makeGuard(F f, A a) {
01235 return guardImpl1<F, A>::makeGuard(f, a);
01236 }
01237
01240 template <typename F, typename A1, typename A2>
01241 class guardImpl2 : public guardBase {
01242 public:
01243 static guardImpl2<F, A1, A2> makeGuard(F f, A1 a1, A2 a2) {
01244 return guardImpl2<F, A1, A2>(f, a1, a2);
01245 }
01246
01248 ~guardImpl2() {cleanup(*this);}
01249
01250 protected:
01251 friend class guardBase;
01252 void execute() {fun_(arg1_, arg2_);}
01253
01255 explicit guardImpl2(F f, A1 a1, A2 a2)
01256 : fun_(f), arg1_(a1), arg2_(a2) {}
01257
01258 private:
01260 F fun_;
01262 A1 arg1_;
01264 A2 arg2_;
01265
01266 guardImpl2();
01267
01268 guardImpl2& operator=(const guardImpl2&);
01269 };
01270
01271 template <typename F, typename A1, typename A2>
01272 inline guardImpl2<F, A1, A2> makeGuard(F f, A1 a1, A2 a2) {
01273 return guardImpl2<F, A1, A2>::makeGuard(f, a1, a2);
01274 }
01275
01277 template <class C, typename F>
01278 class guardObj0 : public guardBase {
01279 public:
01280 static guardObj0<C, F> makeGuard(C& o, F f) {
01281 return guardObj0<C, F>(o, f);
01282 }
01283
01285 ~guardObj0() {cleanup(*this);}
01286
01287 protected:
01288 friend class guardBase;
01289 void execute() {(obj_.*fun_)();}
01290
01292 guardObj0(C& o, F f) : obj_(o), fun_(f) {}
01293
01294 private:
01295 C& obj_;
01296 F fun_;
01297
01298 guardObj0();
01299 guardObj0& operator=(const guardObj0&);
01300 };
01301
01302 template <class C, typename F>
01303 inline guardObj0<C, F> objectGuard(C o, F f) {
01304 return guardObj0<C, F>::makeGuard(o, f);
01305 }
01306 }
01307 }
01308
01309 #if defined(WIN32) && ! defined(__CYGWIN__)
01310 char* getpass(const char* prompt);
01311 #endif
01312
01318 inline double ibis::util::rand() {
01319
01320
01321 static uint32_t seed = 1;
01322 static const uint32_t alpha = 69069;
01323 static const double scale = ::pow(0.5, 32);
01324 seed = static_cast<uint32_t>(seed * alpha);
01325 return(scale * seed);
01326 }
01327
01329 inline uint32_t ibis::util::checksum(uint32_t a, uint32_t b) {
01330 uint32_t a0 = (a >> 16);
01331 uint32_t a1 = (a & 0xFFFF);
01332 uint32_t b0 = (b >> 16);
01333 uint32_t b1 = (b & 0xFFFF);
01334 return ((((a0<<2)+a1*3+(b0<<1)+b1) << 16) | ((a0+a1+b0+b1) & 0xFFFF));
01335 }
01336
01344 inline double ibis::util::incrDouble(const double& in) {
01345 #if defined(HAVE_NEXTAFTER)
01346 return nextafter(in, DBL_MAX);
01347 #elif defined(_MSC_VER) && defined(_WIN32)
01348 return _nextafter(in, DBL_MAX);
01349 #else
01350 double tmp = fabs(in) * DBL_EPSILON;
01351 if (tmp > 0.0) tmp += in;
01352 else tmp = in + DBL_MIN;
01353 return tmp;
01354 #endif
01355 }
01356
01359 inline double ibis::util::decrDouble(const double& in) {
01360 #if defined(HAVE_NEXTAFTER)
01361 return nextafter(in, -DBL_MAX);
01362 #elif defined(_MSC_VER) && defined(_WIN32)
01363 return _nextafter(in, -DBL_MAX);
01364 #else
01365 double tmp = fabs(in) * DBL_EPSILON;
01366 if (tmp > 0.0) tmp = in - tmp;
01367 else tmp = in - DBL_MIN;
01368 return tmp;
01369 #endif
01370 }
01371
01376 inline void ibis::util::eq2range(const double& in,
01377 double& left, double& right) {
01378 #if defined(HAVE_NEXTAFTER)
01379 right = nextafter(in, DBL_MAX);
01380 #elif defined(_MSC_VER) && defined(_WIN32)
01381 right = _nextafter(in, DBL_MAX);
01382 #else
01383 double tmp = fabs(in) * DBL_EPSILON;
01384 if (tmp > 0.0) {right = in + tmp;}
01385 else {right = in + DBL_MIN;}
01386 #endif
01387 left = in;
01388 }
01389
01393 template <typename Tin>
01394 inline void ibis::util::round_up(const Tin& inval, float& outval) {
01395
01396 outval = static_cast<float>(inval);
01397 if (static_cast<Tin>(outval) < inval) {
01398
01399
01400 #if defined(HAVE_NEXTAFTER)
01401 outval = nextafterf(static_cast<float>(inval), FLT_MAX);
01402 #else
01403 float tmp = fabsf(outval) * FLT_EPSILON;
01404 if (tmp > 0.0) outval += tmp;
01405 else outval += FLT_MIN;
01406 #endif
01407 }
01408 }
01409
01410
01411 inline void ibis::util::removeTail(char* str, char tail) {
01412 if (str != 0 && *str != 0) {
01413 char *tmp = str;
01414 while (*tmp != 0) ++ tmp;
01415 -- tmp;
01416 while (tmp > str && *tmp == tail) {
01417 *tmp = static_cast<char>(0);
01418 -- tmp;
01419 }
01420 }
01421 }
01422
01423
01424 inline char* ibis::util::trim(char* str) {
01425 char* head = 0;
01426 if (str == 0) return head;
01427 if (*str == 0) return head;
01428
01429 head = str;
01430 while (*head) {
01431 if (std::isspace(*head))
01432 ++ head;
01433 else
01434 break;
01435 }
01436 if (*head == 0)
01437 return head;
01438
01439 for (str = head; *str != 0; ++ str);
01440 -- str;
01441 while (str >= head && std::isspace(*str)) {
01442 *str = static_cast<char>(0);
01443 -- str;
01444 }
01445 return head;
01446 }
01447
01455 inline double ibis::util::coarsen(const double in, unsigned prec) {
01456 double ret;
01457 if (prec > 15) {
01458 ret = in;
01459 }
01460 else if (in == 0.0) {
01461 ret = in;
01462 }
01463 else {
01464 ret = fabs(in);
01465 if (ret < DBL_MIN) {
01466 ret = 0.0;
01467 }
01468 else if (ret < DBL_MAX) {
01469 ret = log10(ret);
01470 if (prec > 0)
01471 -- prec;
01472 const int ixp = static_cast<int>(floor(ret)) -
01473 static_cast<int>(prec);
01474 ret = floor(0.5 + pow(1e1, ret-ixp));
01475 if (ixp > 0)
01476 ret *= pow(1e1, ixp);
01477 else if (ixp < 0)
01478 ret /= pow(1e1, -ixp);
01479 if (in < 0.0)
01480 ret = -ret;
01481 }
01482 else {
01483 ret = in;
01484 }
01485 }
01486 return ret;
01487 }
01488
01490 inline std::ostream& operator<<(std::ostream& out, const ibis::rid_t& rid) {
01491 out << '(' << rid.num.run << ", " << rid.num.event << ')';
01492 return out;
01493 }
01494
01496 inline std::istream& operator>>(std::istream& is, ibis::rid_t& rid) {
01497 char c = 0;
01498 is >> c;
01499 if (c == '(') {
01500 is >> rid.num.run >> c;
01501 if (c == ',')
01502 is >> rid.num.event >> c;
01503 else
01504 rid.num.event = 0;
01505 if (c != ')')
01506 is.clear(std::ios::badbit);
01507 }
01508 else {
01509 is.putback(c);
01510 is >> rid.num.run >> c;
01511 if (c != ',')
01512 is.putback(c);
01513 is >> rid.num.event;
01514 }
01515 return is;
01516 }
01517 #endif // IBIS_UTIL_H