[go: up one dir, main page]

Menu

[e222c1]: / common / utils.h  Maximize  Restore  History

Download this file

465 lines (367 with data), 11.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#ifndef MYUTILS_HEADER
#define MYUTILS_HEADER
/***************************************************************************
* utils.h
*
* Sat Aug 24 23:54:29 2002
* Copyright 2002 Roman Dementiev
* dementiev@mpi-sb.mpg.de
****************************************************************************/
#ifdef STXXL_BOOST_CONFIG
#include <boost/config.hpp>
#endif
#include <iostream>
#include <algorithm>
#include <assert.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#ifdef BOOST_MSVC
#else
#include <sys/time.h>
#include <unistd.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <string>
#ifdef STXXL_BOOST_TIMESTAMP
#include <boost/date_time/posix_time/posix_time.hpp>
#endif
#ifdef STXXL_BOOST_FILESYSTEM
#include "boost/filesystem/operations.hpp"
#endif
#define __STXXL_BEGIN_NAMESPACE namespace stxxl {
#define __STXXL_END_NAMESPACE }
#include "log.h"
__STXXL_BEGIN_NAMESPACE
//#define assert(x)
#define __STXXL_STRING(x) #x
#define STXXL_MSG(x) \
{ std::cout << "[STXXL-MSG] "<< x << std::endl; std::cout.flush(); \
stxxl::logger::get_instance()->log_stream() << "[STXXL-MSG] "<< x << std::endl; stxxl::logger::get_instance()->log_stream().flush(); \
};
#define STXXL_ERRMSG(x) \
{ std::cerr << "[STXXL-ERRMSG] "<< x << std::endl; std::cerr.flush(); \
stxxl::logger::get_instance()->errlog_stream() << "[STXXL-ERRMSG] "<< x << std::endl; stxxl::logger::get_instance()->errlog_stream().flush(); \
};
#if STXXL_VERBOSE_LEVEL > 0
#define STXXL_VERBOSE1(x) \
{ std::cout << "[STXXL-VERBOSE1] "<<x << std::endl; std::cerr.flush(); \
stxxl::logger::get_instance()->log_stream() << "[STXXL-VERBOSE1] "<< x << std::endl; stxxl::logger::get_instance()->log_stream().flush(); \
};
#else
#define STXXL_VERBOSE1(x) ;
#endif
#define STXXL_VERBOSE(x) STXXL_VERBOSE1(x)
#if STXXL_VERBOSE_LEVEL > 1
#define STXXL_VERBOSE2(x) \
{ std::cout << "[STXXL-VERBOSE2] "<< x << std::endl; std::cerr.flush(); \
stxxl::logger::get_instance()->log_stream() << "[STXXL-VERBOSE2] "<< x << std::endl; stxxl::logger::get_instance()->log_stream().flush(); \
};
#else
#define STXXL_VERBOSE2(x) ;
#endif
#if STXXL_VERBOSE_LEVEL > 2
#define STXXL_VERBOSE3(x) \
{ std::cout << "[STXXL-VERBOSE3] "<< x << std::endl; std::cerr.flush(); \
stxxl::logger::get_instance()->log_stream() << "[STXXL-VERBOSE3] "<< x << std::endl; stxxl::logger::get_instance()->log_stream().flush(); \
};
#else
#define STXXL_VERBOSE3(x) ;
#endif
inline void
stxxl_perror (const char *errmsg, int errcode)
{
// STXXL_ERRMSG(errmsg << " error code " << errcode << " : " << strerror (errcode) );
exit (errcode);
}
#ifndef STXXL_DEBUG_ON
#define STXXL_DEBUG_ON
#endif
#ifdef STXXL_DEBUG_ON
#define stxxl_error(errmsg) { perror(errmsg); exit(errno); }
#ifdef BOOST_MSVC
#define STXXL_PRETTY_FUNCTION_NAME __FUNCTION__
#else
#define STXXL_PRETTY_FUNCTION_NAME __PRETTY_FUNCTION__
#endif
#define stxxl_function_error stxxl_error(STXXL_PRETTY_FUNCTION_NAME)
#define stxxl_nassert(expr) { int ass_res=expr; if(ass_res) { std::cerr << "Error in function: " << STXXL_PRETTY_FUNCTION_NAME << " "; stxxl_perror(__STXXL_STRING(expr),ass_res); }}
#define stxxl_ifcheck(expr) if((expr)<0) { std::cerr<<"Error in function "<<STXXL_PRETTY_FUNCTION_NAME<<" "; stxxl_error(__STXXL_STRING(expr));}
#define stxxl_ifcheck_win(expr) if((expr)==0) { std::cerr<<"Error in function "<<STXXL_PRETTY_FUNCTION_NAME<<" "; stxxl_error(__STXXL_STRING(expr));}
#define stxxl_ifcheck_i(expr,info) if((expr)<0) { std::cerr<<"Error in function "<<STXXL_PRETTY_FUNCTION_NAME<<" Info: "<< info<<" "; stxxl_error(__STXXL_STRING(expr)); }
#define stxxl_debug(expr) expr
#ifdef BOOST_MSVC
#define stxxl_win_lasterror_exit(errmsg) \
{ \
TCHAR szBuf[80]; \
LPVOID lpMsgBuf; \
DWORD dw = GetLastError(); \
FormatMessage( \
FORMAT_MESSAGE_ALLOCATE_BUFFER | \
FORMAT_MESSAGE_FROM_SYSTEM, \
NULL, \
dw, \
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), \
(LPTSTR) &lpMsgBuf, \
0, NULL ); \
STXXL_ERRMSG("Error in "<< errmsg <<", error code "<<dw<<": "<<((char*)lpMsgBuf)); \
LocalFree(lpMsgBuf); \
ExitProcess(dw); \
}
#endif
#else
#define stxxl_error(errmsg) ;
#define stxxl_function_error ;
#define stxxl_nassert(expr) expr;
#define stxxl_ifcheck(expr) expr; if(0) {}
#define stxxl_debug(expr) ;
#ifdef BOOST_MSVC
#define stxxl_win_lasterror_exit(errmsg) ;
#endif
#endif
// returns number of seconds from ...
inline double
stxxl_timestamp ()
{
#ifdef STXXL_BOOST_TIMESTAMP
boost::posix_time::ptime MyTime = boost::posix_time::microsec_clock::local_time();
boost::posix_time::time_duration Duration = MyTime.time_of_day();
double sec= double(Duration.hours())*3600. +
double(Duration.minutes())*60. +
double(Duration.seconds()) +
double(Duration.fractional_seconds())/(pow(10.,Duration.num_fractional_digits()));
return sec;
#else
struct timeval tp;
gettimeofday (&tp, NULL);
return double (tp.tv_sec) + tp.tv_usec / 1000000.;
#endif
}
inline
std::string
stxxl_tmpfilename (std::string dir, std::string prefix)
{
//stxxl_debug(cerr <<" TMP:"<< dir.c_str() <<":"<< prefix.c_str()<< endl);
int rnd;
char buffer[1024];
std::string result;
struct stat st;
do
{
rnd = rand ();
sprintf (buffer, "%u", rnd);
result = dir + prefix + buffer;
}
#ifdef STXXL_BOOST_FILESYSTEM
while(boost::filesystem::exists(result));
return result;
#else
while (!lstat (result.c_str (), &st));
if (errno != ENOENT)
stxxl_function_error
return result;
#endif
}
inline
std::vector <
std::string >
split (const std::string & str, const std::string & sep)
{
std::vector < std::string > result;
if (str.empty ())
return result;
std::string::size_type CurPos (0), LastPos (0);
while (1)
{
CurPos = str.find (sep, LastPos);
if (CurPos == std::string::npos)
break;
std::string sub =
str.substr (LastPos,
std::string::size_type (CurPos -
LastPos));
if (sub.size ())
result.push_back (sub);
LastPos = CurPos + sep.size ();
};
std::string sub = str.substr (LastPos);
if (sub.size ())
result.push_back (sub);
return result;
};
#define str2int(str) atoi(str.c_str())
inline
std::string
int2str (int i)
{
char buf[32];
sprintf (buf, "%d", i);
return std::string (buf);
}
#ifndef BOOST_MSVC
#define STXXL_MIN(a,b) ( std::min(a,b) )
#define STXXL_MAX(a,b) ( std::max(a,b) )
#else
#define STXXL_MIN(a,b) ( (std::min)(a,b) )
#define STXXL_MAX(a,b) ( (std::max)(a,b) )
#endif
#define STXXL_L2_SIZE (512*1024)
#define div_and_round_up(a,b) ( (a)/(b) + !(!((a)%(b))))
#define log2(x) (log(x)/log(2.))
//#define HAVE_BUILTIN_EXPECT
#ifdef HAVE_BUILTIN_EXPECT
#define LIKELY(c) __builtin_expect((c), 1)
#else
#define LIKELY(c) c
#endif
#ifdef HAVE_BUILTIN_EXPECT
#define UNLIKELY(c) __builtin_expect((c), 0)
#else
#define UNLIKELY(c) c
#endif
//#define COUNT_WAIT_TIME
#ifdef COUNT_WAIT_TIME
#define START_COUNT_WAIT_TIME double count_wait_begin = stxxl_timestamp();
#define END_COUNT_WAIT_TIME stxxl::wait_time_counter+= (stxxl_timestamp() - count_wait_begin);
#define reset_io_wait_time() stxxl::wait_time_counter = 0.0;
#define io_wait_time() (stxxl::wait_time_counter)
#else
#define START_COUNT_WAIT_TIME
#define END_COUNT_WAIT_TIME
inline void reset_io_wait_time()
{
};
inline double io_wait_time()
{
return -1.0;
};
#endif
#ifdef STXXL_BOOST_CONFIG
#ifdef BOOST_MSVC
typedef __int64 int64;
typedef unsigned __int64 uint64;
#else
typedef long long int int64;
typedef unsigned long long uint64;
#endif
#else
typedef long long int int64;
typedef unsigned long long uint64;
#endif
/* since STD library lacks following functions in opposite to STL */
template <class _Tp>
inline void swap(_Tp& __a, _Tp& __b)
{
_Tp __tmp = __a;
__a = __b;
__b = __tmp;
}
template <class _ForwardIter>
bool is_sorted(_ForwardIter __first, _ForwardIter __last)
{
if (__first == __last)
return true;
_ForwardIter __next = __first;
for (++__next; __next != __last; __first = __next, ++__next) {
if (*__next < *__first)
return false;
}
return true;
}
template <class _ForwardIter, class _StrictWeakOrdering>
bool is_sorted(_ForwardIter __first, _ForwardIter __last,
_StrictWeakOrdering __comp)
{
if (__first == __last)
return true;
_ForwardIter __next = __first;
for (++__next; __next != __last; __first = __next, ++__next) {
if (__comp(*__next, *__first))
return false;
}
return true;
}
template <class T>
void swap_1D_arrays(T * a,T * b, unsigned size)
{
for(unsigned i=0;i<size;++i)
std::swap(a[i],b[i]);
}
// designed for typed_block (to use with std::vector )
template <class T>
class new_alloc {
public:
// type definitions
typedef T value_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
// rebind allocator to type U
template <class U>
struct rebind {
typedef new_alloc<U> other;
};
// return address of values
pointer address (reference value) const {
return &value;
}
const_pointer address (const_reference value) const {
return &value;
}
new_alloc() throw() {
}
new_alloc(const new_alloc&) throw() {
}
template <class U>
new_alloc (const new_alloc<U>&) throw() {
}
~new_alloc() throw() {
}
// return maximum number of elements that can be allocated
size_type max_size () const throw() {
return (std::numeric_limits<std::size_t>::max)() / sizeof(T);
}
// allocate but don't initialize num elements of type T
pointer allocate (size_type num, const void* = 0) {
pointer ret = (pointer)(T::operator new(num*sizeof(T)));
return ret;
}
// initialize elements of allocated storage p with value value
void construct (pointer p, const T& value)
{
// initialize memory with placement new
new((void*)p)T(value);
}
// destroy elements of initialized storage p
void destroy (pointer p)
{
// destroy objects by calling their destructor
p->~T();
}
// deallocate storage p of deleted elements
void deallocate (pointer p, size_type num)
{
T::operator delete((void*)p);
}
};
// return that all specializations of this allocator are interchangeable
template <class T1, class T2>
bool operator== (const new_alloc<T1>&,
const new_alloc<T2>&) throw() {
return true;
}
template <class T1, class T2>
bool operator!= (const new_alloc<T1>&,
const new_alloc<T2>&) throw() {
return false;
}
__STXXL_END_NAMESPACE
#endif