-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmyutils.h
467 lines (371 loc) · 14 KB
/
myutils.h
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
465
466
467
#ifndef myutils_h
#define myutils_h
#ifdef _MSC_VER
#if !defined(MYUTILS_CPP)
#define _SCL_SECURE_NO_WARNINGS
#define _HAS_ITERATOR_DEBUGGING 0
#define _ITERATOR_DEBUG_LEVEL 0
#endif // !defined(IN_MYUTILS_CPP)
#endif
#define RCE_MALLOC 0
#define TRACK_ALLOC 0
#define ALLOC_TOTALS 0
#if defined(__x86_64__) || defined(_M_X64)
#define BITS 64
#else
#define BITS 32
#endif
#include <stdio.h>
#include <sys/types.h>
#include <string>
#include <string.h>
#include <memory.h>
#include <vector>
#include <math.h>
#include <stdarg.h>
#include <cstdlib>
#include <climits>
#include <float.h>
#include <omp.h>
#include <algorithm>
#include <inttypes.h>
#ifndef _MSC_VER
#define _stricmp strcasecmp
#endif
//#ifndef max
//#define max(x, y) ((x) >= (y) ? (x) : (y))
//#define min(x, y) ((x) <= (y) ? (x) : (y))
//#endif
using namespace std;
#ifdef _MSC_VER
#include <crtdbg.h>
#pragma warning(disable: 4996) // deprecated functions
#define _CRT_SECURE_NO_DEPRECATE 1
#endif
#if defined(_DEBUG) && !defined(DEBUG)
#define DEBUG 1
#endif
#if defined(DEBUG) && !defined(_DEBUG)
#define _DEBUG 1
#endif
#ifndef NDEBUG
#define DEBUG 1
#define _DEBUG 1
#endif
typedef unsigned char byte;
typedef unsigned short uint16;
typedef unsigned uint32;
typedef unsigned uint;
// typedefs for int64 and uint64
#if defined(_MSC_VER)
typedef __int64 int64;
typedef unsigned __int64 uint64;
#elif defined(__GNUC__)
typedef long int64;
typedef unsigned long uint64;
#else
#error "int64 typedefs"
#endif
#if BITS==32
typedef uint32 uintb;
#else
typedef uint64 uintb;
#endif
#ifndef UINT32_MAX
const uint32 UINT32_MAX = (~(uint32(0)));
#endif
#ifndef UINT64_MAX
const uint64 UINT64_MAX = (~(uint64(0)));
#endif
#ifndef SIZE_T_MAX
const size_t SIZE_T_MAX = (~(size_t(1)));
#endif
void myassertfail(const char *Exp, const char *File, unsigned Line);
#undef assert
#ifdef NDEBUG
#define assert(exp) ((void)0)
#define myassert(exp) ((void)0)
#else
#define assert(exp) (void)( (exp) || (myassertfail(#exp, __FILE__, __LINE__), 0) )
#define myassert(exp) (void)( (exp) || (myassertfail(#exp, __FILE__, __LINE__), 0) )
#endif
#define asserta(exp) (void)( (exp) || (myassertfail(#exp, __FILE__, __LINE__), 0) )
#define ureturn(x) return (x)
#if DEBUG && defined(_MSC_VER)
#if RCE_MALLOC
#define _chkmem() rce_chkmem()
#else
#define _chkmem() asserta(_CrtCheckMemory())
#endif
#endif
#if ALLOC_TOTALS
void LogAllocSummary();
#endif
#define NotUsed(v) ((void *) &v)
// pom=plus or minus, tof=true or false, yon=yes or no
static inline char pom(bool Plus) { return Plus ? '+' : '-'; }
static inline char tof(bool x) { return x ? 'T' : 'F'; }
static inline char yon(bool x) { return x ? 'Y' : 'N'; }
static inline const char *YesOrNo(bool x) { return x ? "Yes" : "No"; }
static inline const char *plurals(unsigned n) { return n == 1 ? "" : "s"; }
const char *GetPlatform();
unsigned GetElapsedSecs();
void mysleep(unsigned ms);
void mylistdir(const string &DirName, vector<string> &FileNames);
void Version(FILE *f);
extern const int VER;
#if RCE_MALLOC
void *rce_malloc(unsigned n, unsigned bytes, const char *FileName, int Line);
void rce_free(void *p, const char *FileName, int LineNr);
void rce_chkmem();
void rce_assertvalidptr_(void *p, const char *FileName, int LineNr);
#define rce_assertvalidptr(p) rce_assertvalidptr_(p, __FILE__, __LINE__)
void rce_dumpptr_(void *p, const char *FileName, int LineNr);
#define rce_dumpptr(p) rce_dumpptr_(p, __FILE__, __LINE__)
#define mymalloc(n, m) rce_malloc((n), (m), __FILE__, __LINE__)
#define myfree(p) rce_free(p, __FILE__, __LINE__)
#define myalloc(t, n) (t *) rce_malloc((n)*sizeof(t), __FILE__, __LINE__)
#else // RCE_MALLOC
void *mymalloc(unsigned n, unsigned bytes);
void myfree(void *p);
#define rce_chkmem() /* empty */
extern unsigned g_AllocLine;
extern const char *g_AllocFile;
#define myalloc(t, n) (t *) (g_AllocLine = __LINE__, g_AllocFile = __FILE__, (mymalloc((n), sizeof(t))))
void *mymalloc64(unsigned BytesPerObject, uint64 N);
#define myalloc64(t, n) (t *) mymalloc64(sizeof(t), (n));
#endif // RCE_MALLOC
#if TRACK_ALLOC
#undef myalloc
#undef myfree
void *myalloc_track(unsigned Bytes, const char *FileName, int LineNr);
void myfree_track(void *p, const char *FileName, int LineNr);
void myalloc_trace(bool On);
#define myalloc(t, n) (t *) myalloc_track(sizeof(t)*(n), __FILE__, __LINE__)
#define myfree(p) myfree_track(p, __FILE__, __LINE__)
#endif // TRACK_ALLOC
#define SIZE(c) unsigned((c).size())
#define RoundUp(Bytes, BlockSize) ((Bytes) + ((BlockSize) - (Bytes)%(BlockSize)))
bool myisatty(int fd);
#ifdef _MSC_VER
#define off_t __int64
#endif
// Stdio functions without "nr of bytes" arg.
FILE *OpenStdioFile(const string &FileName);
FILE *CreateStdioFile(const string &FileName);
void CloseStdioFile(FILE *f);
bool ReadLineStdioFile(FILE *f, string &Line);
void ReadTabbedLineStdioFile(FILE *f, vector<string> &Fields, unsigned FieldCount);
void FlushStdioFile(FILE *f);
bool StdioFileExists(const string &FileName);
void LogStdioFileState(FILE *f);
void RenameStdioFile(const string &FileNameFrom, const string &FileNameTo);
void MoveStdioFile(const string &FileName1, const string &FileName2);
void DeleteStdioFile(const string &FileName);
void WriteStdioFileStr(FILE *f, const char *s);
void Pr(FILE *f, const char *Format, ...);
void ParseFileName(const string &FileName, string &Path, string &Name);
// void ReadDir(const string &DirName, vector<string> &FileNames);
// Stdio functions with size args:
byte *ReadAllStdioFile32(FILE *f, uint32 &FileSize);
byte *ReadAllStdioFile64(FILE *f, uint64 &FileSize);
byte *ReadAllStdioFile(FILE *f, uint32 &FileSize);
byte *ReadAllStdioFile64(FILE *f, uint64 &FileSize);
byte *ReadAllStdioFile32(const string &FileName, uint32 &FileSize);
byte *ReadAllStdioFile64(const string &FileName, uint64 &FileSize);
bool ReadLineStdioFile(FILE *f, char *Line, uint32 Bytes);
bool ReadLineStdioFile64(FILE *f, char *Line, uint64 Bytes);
void SetStdioFilePos(FILE *f, uint32 Pos);
void SetStdioFilePos64(FILE *f, uint64 Pos);
uint32 GetStdioFilePos32(FILE *f);
uint64 GetStdioFilePos64(FILE *f);
uint32 GetStdioFileSize32(FILE *f);
uint64 GetStdioFileSize64(FILE *f);
#if BITS==32
#define uintB uint32
#define GetStdioFilePosB GetStdioFilePos32
#define GetStdioFileSizeB GetStdioFileSize32
#define SetStdioFilePosB SetStdioFilePos
#else
#define uintB uint64
#define GetStdioFilePosB GetStdioFilePos64
#define GetStdioFileSizeB GetStdioFileSize64
#define SetStdioFilePosB SetStdioFilePos64
#endif
uint32 ReadStdioFile_NoFail(FILE *f, void *Buffer, uint32 Bytes);
void ReadStdioFile(FILE *f, uint32 Pos, void *Buffer, uint32 Bytes);
void ReadStdioFile64(FILE *f, uint64 Pos, void *Buffer, uint64 Bytes);
void ReadStdioFile(FILE *f, void *Buffer, uint32 Bytes);
void ReadStdioFile64(FILE *f, void *Buffer, uint64 Bytes);
void WriteStdioFile(FILE *f, uint32 Pos, const void *Buffer, uint32 Bytes);
void WriteStdioFile64(FILE *f, uint64 Pos, const void *Buffer, uint64 Bytes);
void WriteStdioFile(FILE *f, const void *Buffer, uint32 Bytes);
void WriteStdioFile64(FILE *f, const void *Buffer, uint64 Bytes);
#define MAGIC(a, b, c, d) uint32(uint32(a)<<24 | uint32(b)<<16 | uint32(c)<<8 | (d))
void Pf(FILE *f, const char *szFormat, ...);
void Ps(string &Str, const char *szFormat, ...);
void Psa(string &Str, const char *szFormat, ...);
void Psasc(string &Str, const char *szFormat, ...);
void SetLogFileName(const string &FileName);
void Log(const char *szFormat, ...);
#if DEBUG
void Die_(const char *szFormat, ...);
void Warning_(const char *szFormat, ...);
typedef void (*PTR_PRINTFLIKE_FN)(const char *Format, ...);
static inline PTR_PRINTFLIKE_FN DiePtr(const char *FileName, unsigned LineNr)
{
fprintf(stderr, "\n\n%s(%u): ", FileName, LineNr);
Log("\n\n%s(%u): ", FileName, LineNr);
return Die_;
}
static inline PTR_PRINTFLIKE_FN WarningPtr(const char *FileName, unsigned LineNr)
{
fprintf(stderr, "\n\n%s(%u): ", FileName, LineNr);
Log("\n\n%s(%u): ", FileName, LineNr);
return Warning_;
}
#define Die (*DiePtr(__FILE__, __LINE__))
#define Warning (*WarningPtr(__FILE__, __LINE__))
#else
void Die_(const char *szFormat, ...);
void Warning_(const char *szFormat, ...);
#define Die Die_
#define Warning Warning_
#endif
typedef const char *(*FN_PROGRESS_CALLBACK)();
void SetPCB(FN_PROGRESS_CALLBACK PCB);
void ProgressCallback(unsigned i, unsigned N);
bool ProgressPrefix(bool On);
void ProgressStep(unsigned i, unsigned N, const char *Format, ...);
void ProgressStep64(uint64 i, uint64 N, const char *Msg);
void Progress(const char *szFormat, ...);
void Progress(const string &Str);
void ProgressLog(const char *szFormat, ...);
void ProgressLogPrefix(const char *Format, ...);
void ProgressFileInit(FILE *f, const char *Format = 0, ...);
void ProgressFileStep(const char *Format = 0, ...);
void ProgressFileDone(const char *Format = 0, ...);
void LogElapsedTimeAndRAM();
void LogProgramInfoAndCmdLine();
void Help();
inline unsigned ustrlen(const char *s) { return (unsigned) strlen(s); }
inline unsigned ustrlen(const string &s) { return SIZE(s); }
char *mystrsave(const char *s);
unsigned myipow(unsigned x, unsigned y);
uint64 myipow64(unsigned x, unsigned y);
static inline unsigned atou(const char *s) { return unsigned(atoi(s)); }
static inline unsigned atou(const string &s) { return unsigned(atoi(s.c_str())); }
unsigned GetThreadIndex();
double GetMemUseBytes();
double GetPeakMemUseBytes();
double GetPhysMemBytes();
double GetUsableMemBytes();
// https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
inline bool feq(double x, double y)
{
if (x == y)
return true;
double X = fabs(x);
double Y = fabs(y);
double Max = max(X, Y);
double Diff = fabs(X-Y);
return Diff < Max*0.01;
}
#define asserteq(x, y) assert(feq(x, y))
#define assertaeq(x, y) asserta(feq(x, y))
#define zero(a, n) memset((a), 0, (n)*sizeof(a[0]))
#define memset_zero(a, n) memset((a), 0, (n)*sizeof(a[0]))
void ResetRand(unsigned Seed);
unsigned randu32();
uint64 randu64();
void Split(const string &Str, vector<string> &Fields, char Sep = '\t');
void StripWhiteSpace(string &Str);
bool StartsWith(const string &s, const string &t);
bool StartsWith(const char *s, const char *t);
void ToUpper(const string &s, string &t);
void ToLower(const string &s, string &t);
void Reverse(string &s);
static inline double GetRatio(double x, double y) { if (y == 0) { asserta(x == 0); return 0; } return x/y; }
static inline double GetPct(double x, double y) { return 100.0*GetRatio(x, y); }
static inline double GetPct64(int64 x, int64 y) { return 100.0*GetRatio(double(x), double(y)); }
const char *PctToStr(double Pct);
double GetMemUseBytes();
void PrintCmdLine(FILE *f);
void PrintProgramInfo(FILE *f);
void PrintCopyright(FILE *f);
const char *MemBytesToStr(double Bytes);
static inline const char *MemBytesToStr(uint64 Bytes) { return MemBytesToStr((double) Bytes); }
static inline const char *MemBytesToStr(uint32 Bytes) { return MemBytesToStr((double) Bytes); }
double StrToMemBytes(const string &s);
double StrToFloat(const char *s, bool StarIsDblMax = false);
double StrToFloat(const string &s, bool StarIsDblMax = false);
bool IsValidFloatStr(const char *s);
bool IsValidFloatStr(const string &s);
const char *GetElapsedTimeStr(string &s);
const char *GetMaxRAMStr(string &s);
const char *BaseName(const char *PathName);
const char *IntToStr(uint64 i);
const char *IntToStr2(uint64 i);
const char *Int64ToStr(uint64 i);
const char *FloatToStr(double d);
const char *FloatToStr(uint64 i);
const char *IntFloatToStr(double d);
const char *SecsToStr(double Secs);
bool IsUintStr(const char *s);
unsigned StrToUint(const char *s, bool StarIsUnitMax = false);
unsigned StrToUint(const string &s, bool StarIsUnitMax = false);
uint64 StrToUint64(const char *s);
uint64 StrToUint64(const string &s);
bool EndsWith(const string &s, const string &t);
bool Replace(string &s, const string &a, const string &b);
double mylog2(double x);
double mylog10(double x);
void LogInt(unsigned i, unsigned w = UINT_MAX);
void Logu(unsigned u, unsigned w, unsigned prefixspaces = 2);
void Logf(float x, unsigned w, unsigned prefixspaces = 2);
const char *SecsToHHMMSS(int Secs);
unsigned GetCPUCoreCount();
void MyCmdLine(int argc, char **argv);
//void CmdLineErr(const char *Format, ...);
void GetCmdLine(string &s);
#define oc(optname) (opt_##optname)
#define os(optname) (string(opt_##optname))
#define FLAG_OPT(Name) extern bool opt_##Name; extern bool optset_##Name; extern bool optused_##Name;
#define UNS_OPT(Name, Default, Min, Max) extern unsigned opt_##Name; extern bool optset_##Name; extern bool optused_##Name;
#define FLT_OPT(Name, Default, Min, Max) extern double opt_##Name; extern bool optset_##Name; extern bool optused_##Name;
#define STR_OPT(Name) extern string opt_##Name; extern bool optset_##Name; extern bool optused_##Name;
#include "myopts.h"
#define sopt(Name) (optused_##Name = true, opt_##Name.c_str())
#define opt(Name) (optused_##Name = true, opt_##Name)
#define default_opt(Name, Value) if (!optset_##Name) { opt_##Name = Value; optset_##Name = true; }
#define set_opt(Name, Value) { opt_##Name = Value; optset_##Name = true; }
void CheckUsedOpts(bool LogAll);
extern FILE *g_fLog;
void SeqToFasta(FILE *f, const byte *Seq, unsigned L, const char *Label);
void SeqToFastq(FILE *f, const byte *Seq, unsigned L, const char *Qual, const char *Label);
void SeqToFastaRC(FILE *f, const byte *Seq, unsigned L, const char *Label);
void RevCompSeq(const byte *Seq, unsigned L, byte *RCSeq);
void LogAllocs();
unsigned GetRequestedThreadCount();
inline char mytoupper(char c) { return c & (~0x20); }
inline char mytoupper(byte c) { return c & (~0x20); }
inline bool myislower(byte c) { return (c & 0x20) != 0; }
inline bool myislower(char c) { return (c & 0x20) != 0; }
#define NO_TRACE 0
#define TMP_TRACE 2 // true but not 1, for grep_trace
#define REMOVEME 1
enum CMD
{
CMD_none,
#define A(x) CMD_##x,
#include "cmds.h"
};
#define A(x) + 1
static const unsigned g_CmdCount = 1 + // for CMD_none
#include "cmds.h"
;
const char *CmdToStr(CMD Cmd);
CMD StrToCmd(const char *Str);
CMD GetCmd();
extern CMD g_Cmd;
#endif // myutils_h