| 1 |
/* The MIT License |
| 2 |
|
| 3 |
Copyright (c) 2008 Genome Research Ltd (GRL). |
| 4 |
|
| 5 |
Permission is hereby granted, free of charge, to any person obtaining |
| 6 |
a copy of this software and associated documentation files (the |
| 7 |
"Software"), to deal in the Software without restriction, including |
| 8 |
without limitation the rights to use, copy, modify, merge, publish, |
| 9 |
distribute, sublicense, and/or sell copies of the Software, and to |
| 10 |
permit persons to whom the Software is furnished to do so, subject to |
| 11 |
the following conditions: |
| 12 |
|
| 13 |
The above copyright notice and this permission notice shall be |
| 14 |
included in all copies or substantial portions of the Software. |
| 15 |
|
| 16 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 20 |
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 21 |
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 |
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 |
SOFTWARE. |
| 24 |
*/ |
| 25 |
|
| 26 |
/* Contact: Heng Li <[email protected]> */ |
| 27 |
|
| 28 |
#ifndef UTILS_H |
| 29 |
#define UTILS_H |
| 30 |
|
| 31 |
#define cputime abpoa_cputime |
| 32 |
#define realtime abpoa_realtime |
| 33 |
#define peakrss abpoa_peakrss |
| 34 |
#define usr_sys_cputime abpoa_usr_sys_cputime |
| 35 |
#define print_format_time abpoa_print_format_time |
| 36 |
#define get_cur_time abpoa_get_cur_time |
| 37 |
|
| 38 |
#include <stdint.h> |
| 39 |
#include <stdio.h> |
| 40 |
#include <limits.h> |
| 41 |
#include <zlib.h> |
| 42 |
#include <sys/resource.h> |
| 43 |
#include <sys/time.h> |
| 44 |
|
| 45 |
#ifndef kroundup32 |
| 46 |
#define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x)) |
| 47 |
#endif |
| 48 |
|
| 49 |
#ifndef kroundup64 |
| 50 |
#define kroundup64(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, (x)|=(x)>>32, ++(x)) |
| 51 |
#endif |
| 52 |
|
| 53 |
#ifdef __GNUC__ |
| 54 |
// Tell GCC to validate printf format string and args |
| 55 |
#define ATTRIBUTE(list) __attribute__ (list) |
| 56 |
#else |
| 57 |
#define ATTRIBUTE(list) |
| 58 |
#endif |
| 59 |
|
| 60 |
#define err_fatal_simple(msg) _err_fatal_simple(__func__, msg) |
| 61 |
#define err_fatal_simple_core(msg) _err_fatal_simple_core(__func__, msg) |
| 62 |
|
| 63 |
#define xopen(fn, mode) err_xopen_core(__func__, fn, mode) |
| 64 |
#define xreopen(fn, mode, fp) err_xreopen_core(__func__, fn, mode, fp) |
| 65 |
#define xzopen(fn, mode) err_xzopen_core(__func__, fn, mode) |
| 66 |
|
| 67 |
#define xassert(cond, msg) if ((cond) == 0) _err_fatal_simple_core(__func__, msg) |
| 68 |
|
| 69 |
#define _err_simple_func_printf(msg) err_func_printf(__func__, msg) |
| 70 |
|
| 71 |
typedef struct { |
| 72 |
uint64_t x, y; |
| 73 |
} pair64_t; |
| 74 |
|
| 75 |
typedef struct { size_t n, m; uint64_t *a; } uint64_v; |
| 76 |
typedef struct { size_t n, m; pair64_t *a; } pair64_v; |
| 77 |
|
| 78 |
#ifdef __cplusplus |
| 79 |
extern "C" { |
| 80 |
#endif |
| 81 |
|
| 82 |
void err_fatal(const char *header, const char *fmt, ...) ATTRIBUTE((noreturn)); |
| 83 |
void err_fatal_core(const char *header, const char *fmt, ...) ATTRIBUTE((noreturn)); |
| 84 |
void _err_fatal_simple(const char *func, const char *msg) ATTRIBUTE((noreturn)); |
| 85 |
void _err_fatal_simple_core(const char *func, const char *msg) ATTRIBUTE((noreturn)); |
| 86 |
FILE *err_xopen_core(const char *func, const char *fn, const char *mode); |
| 87 |
FILE *err_xreopen_core(const char *func, const char *fn, const char *mode, FILE *fp); |
| 88 |
gzFile err_xzopen_core(const char *func, const char *fn, const char *mode); |
| 89 |
size_t err_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream); |
| 90 |
size_t err_fread_noeof(void *ptr, size_t size, size_t nmemb, FILE *stream); |
| 91 |
|
| 92 |
int err_gzread(gzFile file, void *ptr, unsigned int len); |
| 93 |
int err_fseek(FILE *stream, long offset, int whence); |
| 94 |
#define err_rewind(FP) err_fseek((FP), 0, SEEK_SET) |
| 95 |
long err_ftell(FILE *stream); |
| 96 |
int err_fprintf(FILE *stream, const char *format, ...) |
| 97 |
ATTRIBUTE((format(printf, 2, 3))); |
| 98 |
int err_printf(const char *format, ...) |
| 99 |
ATTRIBUTE((format(printf, 1, 2))); |
| 100 |
int err_func_printf(const char *func, const char *format, ...) |
| 101 |
ATTRIBUTE((format(printf, 2, 3))); |
| 102 |
int stdout_printf(const char *format, ...) |
| 103 |
ATTRIBUTE((format(printf, 1, 2))); |
| 104 |
int err_fputc(int c, FILE *stream); |
| 105 |
#define err_putchar(C) err_fputc((C), stdout) |
| 106 |
int err_fputs(const char *s, FILE *stream); |
| 107 |
int err_puts(const char *s); |
| 108 |
void err_fgets(char *buff, size_t s, FILE *fp); |
| 109 |
int err_fflush(FILE *stream); |
| 110 |
int err_fclose(FILE *stream); |
| 111 |
int err_gzclose(gzFile file); |
| 112 |
|
| 113 |
#define _err_malloc(s) err_malloc(__func__, s) |
| 114 |
#define _err_calloc(n, s) err_calloc(__func__, n, s) |
| 115 |
#define _err_realloc(p, s) err_realloc(__func__, p, s) |
| 116 |
void *err_malloc(const char* func, size_t s); |
| 117 |
void *err_calloc(const char* func, size_t n, size_t s); |
| 118 |
void *err_realloc(const char* func, void *p, size_t s); |
| 119 |
|
| 120 |
void usr_sys_cputime(double *usr_t, double *sys_t); |
| 121 |
double cputime(); |
| 122 |
double realtime(); |
| 123 |
long peakrss(void); |
| 124 |
void print_format_time(FILE *out); |
| 125 |
int err_func_format_printf(const char *func, const char *format, ...); |
| 126 |
|
| 127 |
void ks_introsort_64 (size_t n, uint64_t *a); |
| 128 |
void ks_introsort_128(size_t n, pair64_t *a); |
| 129 |
|
| 130 |
|
| 131 |
#ifdef __cplusplus |
| 132 |
} |
| 133 |
#endif |
| 134 |
|
| 135 |
#define _uni_realloc(p, n, m, type) { \ |
| 136 |
if (m <= 0) { \ |
| 137 |
m = 1; \ |
| 138 |
m = MAX_OF_TWO(n, m); \ |
| 139 |
p = (type*)_err_malloc((m) * sizeof(type)); \ |
| 140 |
} else if (n >= m) { \ |
| 141 |
m = n + 1; kroundup32(m); \ |
| 142 |
p = (type*)_err_realloc(p, (m) * sizeof(type)); \ |
| 143 |
} \ |
| 144 |
} |
| 145 |
|
| 146 |
#define _realloc(p, m, type) {(m) <<= 1; p = (type*)_err_realloc(p, (m) * sizeof(type));} |
| 147 |
|
| 148 |
#define _sim_insert_abpoa_utils(v, p, n, m, type) { \ |
| 149 |
if (n == m) { \ |
| 150 |
_realloc(p, m, type) \ |
| 151 |
} \ |
| 152 |
p[n++] = v; \ |
| 153 |
} |
| 154 |
|
| 155 |
#define _insert_abpoa_utils(v, p, n, m, type) { \ |
| 156 |
int _i, _flag=0; \ |
| 157 |
for (_i = 0; _i < n; ++_i) { \ |
| 158 |
if (p[_i] == v) { \ |
| 159 |
_flag = 1; \ |
| 160 |
break; \ |
| 161 |
} \ |
| 162 |
} \ |
| 163 |
if (_flag == 0) { \ |
| 164 |
if (n == m) { \ |
| 165 |
_realloc(p, m, type) \ |
| 166 |
} \ |
| 167 |
p[n++] = v; \ |
| 168 |
} \ |
| 169 |
} |
| 170 |
|
| 171 |
#define _bin_insert_abpoa_utils_idx(v, p, n, m, type, flag, k_i) { \ |
| 172 |
flag=0, k_i=-1; \ |
| 173 |
int _left=0,_right=n-1,_mid; \ |
| 174 |
type _mid_v, _tmp_v; \ |
| 175 |
if (_right == -1) k_i = 0; \ |
| 176 |
else { \ |
| 177 |
while (_left <= _right) { \ |
| 178 |
_mid = (_left+_right) >> 1; \ |
| 179 |
_mid_v = p[_mid]; \ |
| 180 |
if (_mid_v == v) { \ |
| 181 |
k_i = _mid; \ |
| 182 |
flag = 1; break; \ |
| 183 |
} else if (_mid_v > v) { \ |
| 184 |
if (_mid != 0) { \ |
| 185 |
_tmp_v = p[_mid-1]; \ |
| 186 |
} \ |
| 187 |
if (_mid == 0 || v > _tmp_v) { \ |
| 188 |
k_i = _mid; \ |
| 189 |
break; \ |
| 190 |
} \ |
| 191 |
else _right = _mid-1; \ |
| 192 |
} else _left = _mid+1; \ |
| 193 |
} \ |
| 194 |
} \ |
| 195 |
if (k_i == -1) k_i = n; \ |
| 196 |
} |
| 197 |
|
| 198 |
#define _bin_insert_abpoa_utils(v, p, n, m, type) { \ |
| 199 |
int _k_i, _flag; \ |
| 200 |
_bin_insert_abpoa_utils_idx(v, p, n, m, type, _flag, _k_i) \ |
| 201 |
if (_flag == 0) { \ |
| 202 |
if (n == m) { \ |
| 203 |
_realloc(p, m, type) \ |
| 204 |
} \ |
| 205 |
if (_k_i <= n-1) \ |
| 206 |
memmove(p+_k_i+1, p+_k_i, (n-_k_i)*sizeof(type)); \ |
| 207 |
(p)[_k_i] = v; \ |
| 208 |
(n)++; \ |
| 209 |
} \ |
| 210 |
} |
| 211 |
|
| 212 |
#define _bin_search(v, p, n, type, hit, i) { \ |
| 213 |
int _left =0,_right=n-1,_mid; \ |
| 214 |
type _mid_v; \ |
| 215 |
hit = 0; \ |
| 216 |
if (_right == -1) hit=0; \ |
| 217 |
else { \ |
| 218 |
while (_left <= _right) { \ |
| 219 |
_mid = (_left+_right) >> 1; \ |
| 220 |
_mid_v = p[_mid]; \ |
| 221 |
if (_mid_v == v) { \ |
| 222 |
i = _mid; \ |
| 223 |
hit = 1; \ |
| 224 |
break; \ |
| 225 |
} else if (_mid_v > v) { \ |
| 226 |
_right = _mid-1; \ |
| 227 |
} else { \ |
| 228 |
_left = _mid+1; \ |
| 229 |
} \ |
| 230 |
} \ |
| 231 |
} \ |
| 232 |
} |
| 233 |
|
| 234 |
#define MIN_OF_TWO(a, b) ((a) < (b) ? (a) : (b)) |
| 235 |
#define MAX_OF_TWO(a, b) ((a) > (b) ? (a) : (b)) |
| 236 |
#define MIN_OF_THREE(a, b, c) ((a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c))) |
| 237 |
#define MAX_OF_THREE(a, b, c) ((a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c))) |
| 238 |
#define AVG_OF_TWO(a, b) (((a)&(b)) + (((a)^(b)) >> 1)) |
| 239 |
|
| 240 |
static inline uint64_t hash_64(uint64_t key) |
| 241 |
{ |
| 242 |
key += ~(key << 32); |
| 243 |
key ^= (key >> 22); |
| 244 |
key += ~(key << 13); |
| 245 |
key ^= (key >> 8); |
| 246 |
key += (key << 3); |
| 247 |
key ^= (key >> 15); |
| 248 |
key += ~(key << 27); |
| 249 |
key ^= (key >> 31); |
| 250 |
return key; |
| 251 |
} |
| 252 |
#ifndef _PRINT_FORMAT_H_ |
| 253 |
#define _PRINT_FORMAT_H_ |
| 254 |
|
| 255 |
#define NONE "\e[0m" //remove color/font |
| 256 |
#define BLACK "\e[0;30m" // black |
| 257 |
#define B_BLACK "\e[1;30m" // bold black |
| 258 |
#define RED "\e[0;31m" // read |
| 259 |
#define B_RED "\e[1;31m" // bold red |
| 260 |
#define GREEN "\e[0;32m" // green |
| 261 |
#define B_GREEN "\e[1;32m" // bold gren |
| 262 |
#define BROWN "\e[0;33m" // brown |
| 263 |
#define YELLOW "\e[1;33m" // yellow |
| 264 |
#define BLUE "\e[0;34m" // blue |
| 265 |
#define B_BLUE "\e[1;34m" // bold blue |
| 266 |
#define PURPLE "\e[0;35m" // purple |
| 267 |
#define B_PURPLE "\e[1;35m" // bold purple |
| 268 |
#define CYAN "\e[0;36m" // cyan |
| 269 |
#define B_CYAN "\e[1;36m" // bold cyan |
| 270 |
#define GRAY "\e[0;37m" // gray |
| 271 |
#define WHITE "\e[1;37m" // white, bold |
| 272 |
#define BOLD "\e[1m" // bold |
| 273 |
#define UNDERLINE "\e[4m" // underline |
| 274 |
#define BLINK "\e[5m" // blink |
| 275 |
#define REVERSE "\e[7m" // reverse background and foreground |
| 276 |
#define HIDE "\e[8m" // hide |
| 277 |
#define STRIKE "\e[9m" // strikethrough |
| 278 |
#define CLEAR "\e[2J" // clear |
| 279 |
#define CLRLINE "\r\e[K" // clear line |
| 280 |
|
| 281 |
// from https://blog.csdn.net/MoDa_Li/java/article/details/82156888 |
| 282 |
|
| 283 |
#endif |
| 284 |
|
| 285 |
#endif |