11# include <gsl/gsl_rng.h>
13static inline int Dbl_near(
double x,
double y);
14int LDbl_near(
long double x,
long double y);
15static inline int Dbl_equals_allowNonfinite(
double x,
double y);
16int compareInts(
const void *void_x,
const void *void_y);
20void dostacktrace(
const char *file,
int line, FILE * ofp);
26void eprintf(
const char *fmt, ...);
27FILE *
efopen(
const char *restrict name,
const char *restrict mode);
28void printBranchTab(
double tab[3][3], FILE * fp);
30void tellopt(
const char *opt,
const char *description);
31unsigned Dbl_first_geq(
double val,
unsigned len,
double v[len]);
35int strchrcnt(
const char *s,
int c);
36void *
memdup(
const void *p,
size_t n);
37double KLdiverg(
int n,
const double o[n],
const double e[n]);
38double sumDbl(
int n,
const double x[n]);
39double reflect(
double x,
double lo,
double hi);
42unsigned long strhash(
const char *ss) __attribute__((no_sanitize(
"integer")));
44unsigned long strhash(
const char *ss);
48char *stripInternalWhiteSpace(
char *buff);
50int tokenize(
int dim,
char *token[dim],
char *s,
const char *delim);
54void hdr(
const char *msg);
55char *
strcenter(
const char *text,
unsigned width,
56 char *buff,
size_t buffsize);
57int readline(
int dim,
char buff[dim], FILE *fp);
60int strnncopy(
size_t n,
char dst[n],
size_t m,
const char src[m]);
61void collapse_whitespace(
char * buff);
62void stutter(
char c,
int n, FILE * fp);
63static inline double survival(
double t,
double twoN);
65# define ERR(code, msg) do{ \
67 strerror_r((code), buff, sizeof(buff)); \
68 fprintf(stderr,"%s:%s:%d: %s: %d (%s)\n", \
69 __FILE__,__func__,__LINE__, \
70 (msg), (code), buff); \
74# define DIE(msg) do{ \
75 fprintf(stderr,"%s:%s:%d: %s\n", \
76 __FILE__,__func__,__LINE__, (msg)); \
80# define CHECKMEM(x) do { \
82 fprintf(stderr, "%s:%s:%d: NULL pointer\n", \
83 __FILE__,__func__,__LINE__); \
88# define REQUIRE(x, file, line) do { \
90 dostacktrace(__FILE__,__LINE__,stderr); \
91 fprintf(stderr,"ERR@%s:%d->%s:%d: Sanity check FAIL\n", \
92 (file), (line), __FILE__,__LINE__); \
100#define SHIFT_PTR(PTR,OSET,SIGN) do{ \
101 if((PTR) != NULL) { \
103 (PTR) = (void *) (((size_t) (PTR)) + ((size_t) (OSET))); \
105 (PTR) = (void *) (((size_t) (PTR)) - ((size_t) (OSET))); \
111static inline int Dbl_near(
double x,
double y) {
112 double tol = fmax(fabs(x), fabs(y)) * 8.0 * DBL_EPSILON;
113 tol = fmax(tol, DBL_EPSILON);
114 return fabs(x - y) <= tol;
120static inline int Dbl_equals_allowNonfinite(
double x,
double y) {
124 int xtype = fpclassify(x);
125 int ytype = fpclassify(y);
127 if(xtype==FP_NAN && ytype==FP_NAN)
129 if(xtype==FP_INFINITE && ytype==FP_INFINITE) {
139static inline double survival(
double t,
double twoN) {
142 return exp(-t / twoN);
void * memdup(const void *p, size_t n)
duplicate memory block
Definition: misc.c:314
double KLdiverg(int n, const double o[n], const double e[n])
Return Kullback-Leibler divergence of o relative to e.
Definition: misc.c:377
void strReplaceChr(char *s, int a, int b)
In string s, replace instances of character a with character b.
Definition: misc.c:548
void eprintf(const char *fmt,...)
eprintf: print error message and exit.
Definition: misc.c:101
int removeZeroes(int dim, unsigned x[dim])
Remove zeroes from an array of unsigned ints by sliding positive entries to the left.
Definition: misc.c:652
int compareInts(const void *void_x, const void *void_y)
Compare two ints.
Definition: misc.c:352
double perturb_ratio(double x, gsl_rng *rng)
Uniform perturbation on log scale.
Definition: misc.c:153
double perturb_ratio_w(double x, double w, gsl_rng *rng)
Uniform perturbation on log scale.
Definition: misc.c:141
double reflect(double x, double lo, double hi)
Fold x back and forth across the boundaries "lo" and "hi" to obtain a value y such that lo <= y <= hi...
Definition: misc.c:408
FILE * efopen(const char *restrict name, const char *restrict mode)
Open file.
Definition: misc.c:119
char * stripWhiteSpace(char *buff)
Remove white space from beginning and end of a string.
Definition: misc.c:474
char * strlowercase(char *s)
Convert NULL-terminated string to lower case.
Definition: misc.c:428
int strCountSetChunks(const char *str, const char *sep)
In string str, count the number of contiguous chunks of characters belonging to set.
Definition: misc.c:296
long long_last_leq(long val, long *v, long len)
Vector v must be sorted in ascending order before this function is called.
Definition: misc.c:257
char * strcenter(const char *text, unsigned width, char *buff, size_t buffsize)
Center string "text" in a field of width "width".
Definition: misc.c:625
int getNumCores(void)
An almost platform-independent function that returns the number of CPU cores on the current machine.
Definition: misc.c:69
void dostacktrace(const char *file, int line, FILE *ofp)
Print call stack.
Definition: misc.c:40
long long_first_geq(long val, long *v, long len)
Vector v must be sorted in ascending order before this function is called.
Definition: misc.c:223
int compareDoubles(const void *void_x, const void *void_y)
Compare two doubles.
Definition: misc.c:369
void tellopt(const char *opt, const char *description)
Describe an option.
Definition: misc.c:56
long double perturb_interval(long double x, long double lo, long double hi, gsl_rng *rng)
Uniform perturbation within interval.
Definition: misc.c:160
double sumDbl(int n, const double x[n])
Sum an array of doubles.
Definition: misc.c:393
double parseDbl(char *token)
Parse token as a double.
Definition: misc.c:561
void unitTstResult(const char *facility, const char *result)
Print result of a unit test.
Definition: misc.c:288
int compareLongs(const void *void_x, const void *void_y)
Compare two long ints.
Definition: misc.c:335
int readline(int dim, char buff[dim], FILE *fp)
Read a line of input into buff.
Definition: misc.c:679
char * strltrunc(char *s, int n)
Truncate string to n characters (plus terminating '\0') by removing characters from the left.
Definition: misc.c:587
int legalName(const char *name)
Return nonzero if name is legal; zero otherwise.
Definition: misc.c:714
unsigned long strhash(const char *ss)
Hash a character string using algorithm of Daniel J. Boorstein.
Definition: misc.c:440
char * nextWhitesepToken(char **str)
Return tokens separated by 1 or more spaces and/or tabs.
Definition: misc.c:519
void stutter(char c, int n, FILE *fp)
Print character c n times on output fp.
Definition: misc.c:798
int tokenize(int dim, char *token[dim], char *s, const char *delim)
Separate string s into tokens, separated by any character in string delim.
Definition: misc.c:532
const char * mybasename(const char *pathname)
Strip leading pathname components; return a pointer to the filename.
Definition: misc.c:703
int stripchr(char *s, int c)
Remove character c from string s. Return length of string.
Definition: misc.c:455
unsigned Dbl_first_geq(double val, unsigned len, double v[len])
Vector v must be sorted in ascending order before this function is called.
Definition: misc.c:189
int LDbl_near(long double x, long double y)
Return 1 if the relative difference between x and y is less than or equal to 8*DBL_EPSILON.
Definition: misc.c:791