| 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 LH3_UTILS_H |
| 29 |
#define LH3_UTILS_H |
| 30 |
|
| 31 |
#include <stdint.h> |
| 32 |
#include <stdio.h> |
| 33 |
#include <zlib.h> |
| 34 |
|
| 35 |
#ifdef __GNUC__ |
| 36 |
// Tell GCC to validate printf format string and args |
| 37 |
#define ATTRIBUTE(list) __attribute__ (list) |
| 38 |
#else |
| 39 |
#define ATTRIBUTE(list) |
| 40 |
#endif |
| 41 |
|
| 42 |
#define err_fatal_simple(msg) _err_fatal_simple(__func__, msg) |
| 43 |
#define err_fatal_simple_core(msg) _err_fatal_simple_core(__func__, msg) |
| 44 |
|
| 45 |
#define xopen(fn, mode) err_xopen_core(__func__, fn, mode) |
| 46 |
#define xreopen(fn, mode, fp) err_xreopen_core(__func__, fn, mode, fp) |
| 47 |
#define xzopen(fn, mode) err_xzopen_core(__func__, fn, mode) |
| 48 |
|
| 49 |
#define xassert(cond, msg) if ((cond) == 0) _err_fatal_simple_core(__func__, msg) |
| 50 |
|
| 51 |
typedef struct { |
| 52 |
uint64_t x, y; |
| 53 |
} pair64_t; |
| 54 |
|
| 55 |
typedef struct { size_t n, m; uint64_t *a; } uint64_v; |
| 56 |
typedef struct { size_t n, m; pair64_t *a; } pair64_v; |
| 57 |
|
| 58 |
#ifdef __cplusplus |
| 59 |
extern "C" { |
| 60 |
#endif |
| 61 |
|
| 62 |
void err_fatal(const char *header, const char *fmt, ...) ATTRIBUTE((noreturn)); |
| 63 |
void err_fatal_core(const char *header, const char *fmt, ...) ATTRIBUTE((noreturn)); |
| 64 |
void _err_fatal_simple(const char *func, const char *msg) ATTRIBUTE((noreturn)); |
| 65 |
void _err_fatal_simple_core(const char *func, const char *msg) ATTRIBUTE((noreturn)); |
| 66 |
FILE *err_xopen_core(const char *func, const char *fn, const char *mode); |
| 67 |
FILE *err_xreopen_core(const char *func, const char *fn, const char *mode, FILE *fp); |
| 68 |
gzFile err_xzopen_core(const char *func, const char *fn, const char *mode); |
| 69 |
size_t err_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream); |
| 70 |
size_t err_fread_noeof(void *ptr, size_t size, size_t nmemb, FILE *stream); |
| 71 |
|
| 72 |
int err_gzread(gzFile file, void *ptr, unsigned int len); |
| 73 |
int err_fseek(FILE *stream, long offset, int whence); |
| 74 |
#define err_rewind(FP) err_fseek((FP), 0, SEEK_SET) |
| 75 |
long err_ftell(FILE *stream); |
| 76 |
int err_fprintf(FILE *stream, const char *format, ...) |
| 77 |
ATTRIBUTE((format(printf, 2, 3))); |
| 78 |
int err_printf(const char *format, ...) |
| 79 |
ATTRIBUTE((format(printf, 1, 2))); |
| 80 |
int err_fputc(int c, FILE *stream); |
| 81 |
#define err_putchar(C) err_fputc((C), stdout) |
| 82 |
int err_fputs(const char *s, FILE *stream); |
| 83 |
int err_puts(const char *s); |
| 84 |
int err_fflush(FILE *stream); |
| 85 |
int err_fclose(FILE *stream); |
| 86 |
int err_gzclose(gzFile file); |
| 87 |
|
| 88 |
double cputime(); |
| 89 |
double realtime(); |
| 90 |
|
| 91 |
void ks_introsort_64 (size_t n, uint64_t *a); |
| 92 |
void ks_introsort_128(size_t n, pair64_t *a); |
| 93 |
|
| 94 |
#ifdef __cplusplus |
| 95 |
} |
| 96 |
#endif |
| 97 |
|
| 98 |
static inline uint64_t hash_64(uint64_t key) |
| 99 |
{ |
| 100 |
key += ~(key << 32); |
| 101 |
key ^= (key >> 22); |
| 102 |
key += ~(key << 13); |
| 103 |
key ^= (key >> 8); |
| 104 |
key += (key << 3); |
| 105 |
key ^= (key >> 15); |
| 106 |
key += ~(key << 27); |
| 107 |
key ^= (key >> 31); |
| 108 |
return key; |
| 109 |
} |
| 110 |
|
| 111 |
#endif |