-
Notifications
You must be signed in to change notification settings - Fork 67
/
Util.h
183 lines (159 loc) · 4.04 KB
/
Util.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
#pragma once
#include <map>
#include "stdafx.h"
#include "Compat.h"
#include "GoodRandom.h"
#include "Tables.h"
//
// General utilities.
//
namespace util {
#define BEGEND(container) (container).begin(), (container).end()
//
// Sample from the Bernoulli distribution corresponding to 'probability'.
//
inline bool sampleBernoulli(double probability, _uint64 resolution=0xffffffff)
{
_ASSERT(0 <= probability && probability <= 1);
return GoodFastRandom(resolution) / (double)resolution < probability;
}
inline double ratio(double a, double b=1)
{
return a / (a + b);
}
inline void die(const std::string &epitaph, int code=1)
{
fputs((epitaph + '\n').c_str(), stderr);
exit(code);
}
const int MAXLINE = 1024;
inline bool readLine(char *line, FILE *file)
{
return fgets(line, MAXLINE, file) && strchr(line, '\n');
}
inline void dieAtLine(const std::string &epitaph, const std::string &line)
{
die(epitaph + "\n line: " + line);
}
//
// Read a line, returning whether the read was successful or EOF occurred.
//
// Die if an error occurred.
//
inline bool readLineOrDie(char *line, FILE *file)
{
if (!readLine(line, file)) {
if (feof(file))
return false;
dieAtLine("Error reading line.", line);
}
return true;
}
template <class Key, class T>
const T &getOrElse(const std::map<Key, T> &m, typename std::map<Key, T>::const_iterator p,
const T &d=T())
{
return p == m.end() ? d : p->second;
}
//
// Scala map method: lookup with default if not found.
//
template <class Key, class T>
const T &getOrElse(const std::map<Key, T> &m, const Key &k, const T &d=T())
{
return getOrElse(m, m.find(k), d);
}
template <class RandIter, class T>
size_t findIndex(RandIter first, RandIter last, const T &value)
{
return std::find(first, last, value) - first;
}
//
// Analogue of Scala's 'mkString' method.
//
template <class InIter>
std::string joinWithSep(InIter first, InIter last, char sep)
{
std::string joined;
if (first == last)
return joined;
joined += *first++;
while (first != last)
(joined += sep) += *first++;
return joined;
}
template <class T>
void addIfAbsent(std::vector<T> *v, const T &e)
{
if (!std::count(v->begin(), v->end(), e))
v->push_back(e);
}
inline FILE *fopenOrDie(const char *fileName, const char *mode)
{
FILE *file = fopen(fileName, mode);
if (!file)
die("Unable to open file '%s'.");
return file;
}
inline void fcloseOrDie(FILE *file)
{
if (ferror(file))
die("Error in file about to be closed.");
fclose(file);
}
inline bool startsWith(const char *s, const char *prefix)
{
return strstr(s, prefix) == s;
}
inline const char *strstrAfter(const char *s, const char *t)
{
const char *tins = strstr(s, t);
return tins ? tins + strlen(t) : NULL;
}
inline void
toComplement(
char* rc,
const char* bases = NULL,
int length = -1,
bool toLower = false)
{
if (length < 0) {
length = (int) strlen(bases != NULL ? bases : rc);
if (bases != NULL) {
rc[length] = '\0';
}
}
if (bases != NULL) {
if (! toLower) {
for (int i = 0; i < length; i++) {
rc[i] = COMPLEMENT[bases[length - i - 1]];
}
} else {
for (int i = 0; i < length; i++) {
rc[i] = tolower(COMPLEMENT[bases[length - i - 1]]);
}
}
} else {
// reverse complement in place
for (int i = 0; i < length / 2; i++) {
char t = COMPLEMENT[rc[i]];
rc[i] = toLower ? tolower(COMPLEMENT[rc[length - i - 1]]) : COMPLEMENT[rc[length - i - 1]];
rc[length - i - 1] = toLower ? tolower(t) : t;
}
if (length % 2 == 1) {
rc[length / 2] = toLower ? tolower(COMPLEMENT[rc[length / 2]]) : COMPLEMENT[rc[length / 2]];
}
}
}
inline unsigned
log10bucket(
unsigned n)
{
unsigned factor = 1;
while (n >= 10) {
n /= 10;
factor *= 10;
}
return n * factor;
}
} // namespace util