-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintbase.h
291 lines (260 loc) · 9.25 KB
/
intbase.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
#ifndef INTBASE_H
#define INTBASE_H
/*! @file intbase.h
* @brief A sequence base stored as an integer and utilities to work with it.
* This class *must* be subclassed.
*/
#include <algorithm>
#include <string>
#include <log4cxx/logger.h>
/*! @class intbase
* @brief a class to hold the knowledge about mapping sequence bases to integers and vice versa
*/
const std::string endmarker = ">";
typedef std::string base_t; //!< The type of a base
typedef std::vector<base_t> bases_t; //!< a vector of bases
typedef std::map<base_t, unsigned int> mapping_t; //!< for mapping bases to integers
// these are global to avoid a copy in every object created.
static bases_t realbases; //!< All legal bases plus end marker
static mapping_t realmap; //!< reverse mapping base to int
class intbase {
protected:
// !!! bases *must* be initiaized in a subclass.
bases_t &bases = realbases;
unsigned int alphabet_size = 0; //!< Number of bases we work with
unsigned int base_bitmask; //!< bitmask for a single base in an integer
unsigned int base_nbits; //!< Number of bits used to store a base
mapping_t &mapping = realmap; //!< character to base value mapping. Must match bases
//! The base value
unsigned int base_value;
// Everything depends on the length of bases
void set_consts() {
alphabet_size = bases.size()-1;
base_nbits = 0;
base_bitmask = 0;
unsigned int t = alphabet_size;
while (t >>= 1) {
base_bitmask |= 1 << base_nbits;
++base_nbits;
}
for (unsigned int i=0; i< bases.size(); ++i) {
mapping.insert(std::pair<base_t, unsigned int>(bases[i], i));
}
};
// logging
log4cxx::LoggerPtr logger = nullptr;
void init_logging(void) {
logger = log4cxx::Logger::getLogger("intbase");
};
public:
intbase() {
// set_consts must be called by subclass
init_logging();
base_value = begin(); // initialized to first legal value unless via a constructor with an initial value.
};
unsigned int get_nbits() const {
return base_nbits;
};
unsigned int get_bitmask() const {
return base_bitmask;
};
unsigned int get_alphabetsize() const {
if (alphabet_size == 0) {
LOG4CXX_FATAL(logger, "alphabet_size is 0!");
abort();
}
return alphabet_size;
}
unsigned int get_int() const {
return base_value;
};
base_t get_base() const {
return bases[base_value];
};
void set_base(unsigned int b) {
if (b < get_alphabetsize()) {
base_value = b;
} else {
LOG4CXX_FATAL(logger, "base " << b << " >= alphabet size " << get_alphabetsize());
abort();
}
};
void set_base(const base_t b) {
set_base(base_to_int(b));
};
unsigned int base_to_int(const base_t base_p) {
std::string b = base_p; // So we can make it uppercase
std::transform(b.begin(), b.end(), b.begin(), ::toupper); // ensure b is all uc
for (unsigned int i=0; i<get_alphabetsize(); ++i) {
if (b.compare(bases[i]) == 0)
return i;
}
log4cxx::LoggerPtr logger(log4cxx::Logger::getLogger("base"));
LOG4CXX_FATAL(logger, "base_to_int: unknown base '" << b << "'");
abort();
/*NOTREACHED*/
};
/**
* @brief Convert from an unsigned integer to the character it represents. The value
* >' is used to indicate the end value that is not a legal base, but is a legal value to allow loops to run.
*
* @param value The integer to convert to the corresponding character base
* @return char value of the base.
*/
base_t int_to_base(unsigned int value) {
if (value < get_alphabetsize()) {
return bases[value];
} else if (value == get_alphabetsize()) {
return endmarker;
} else { // fatal error
log4cxx::LoggerPtr logger(log4cxx::Logger::getLogger("base"));
LOG4CXX_FATAL(logger, "int_to_base: invalid base value: " << value << " (max " << alphabet_size << ")");
abort();
}
/*NOTREACHED*/
return 0;
};
intbase& operator=(const intbase &i) {
if (this == &i) // Same object?
return *this;
base_value = i.base_value;
return *this;
};
intbase& operator=(const unsigned int i) {
if (i >= alphabet_size) {
LOG4CXX_FATAL(logger, "i >= alphabet_size");
abort();
}
base_value = i;
return *this;
};
intbase& operator++(void) { // Allowed to equal base::end()
if (base_value < get_alphabetsize())
++base_value;
else if (base_value == get_alphabetsize()) {
// do nothing; we are at the end and cannot increment more
}
else { // base > get_alphabetsize(); we should never be here
LOG4CXX_FATAL(logger, "base ++ on too-large value!");
abort();
}
return *this;
};
intbase& operator--(void) {
if (base_value > 0)
--base_value;
else // base == alphabet_size-1
base_value = alphabet_size-1;
return *this;
};
bool operator<(const intbase& rhs) const {
return base_value < rhs.get_int();
};
bool operator<(const unsigned int rhs) const {
return base_value < rhs;
};
bool operator>(const intbase& rhs) const {
return base_value > rhs.get_int();
};
bool operator==(const intbase& rhs) const {
return base_value == rhs.get_int();
};
bool operator!=(const intbase& rhs) const {
return !(*this == rhs);
};
//! @todo need rest of relational operators
//! the first legal value
static unsigned int begin(void) {
return 0;
};
//! one past the last legal value
unsigned int end(void) {
return get_alphabetsize();
};
void print(std::string comment = "") {
std::cout << comment;
std::cout << this;
};
//! @brief Unit test for base class
//! subclasses need their own tests specific to the subclass
//! ibp must be a freshly-created instance
friend void test_intbase(intbase& ibp) {
if (ibp.alphabet_size == 0) {
LOG4CXX_FATAL(ibp.logger, "test_base must be called on a subclass!");
abort();
}
if (ibp.bases.size() != ibp.get_alphabetsize()+1) {
LOG4CXX_FATAL(ibp.logger, "number of bases " << ibp.bases.size() << " does not equal alphabet_size(" << ibp.get_alphabetsize() << ") + 1 !");
abort();
}
// base_to_int and int_to_base work
for (unsigned int i=0; i<ibp.get_alphabetsize(); ++i) {
LOG4CXX_TRACE(ibp.logger, "i: " << i << "; base: " << ibp.bases[i]);
if (ibp.int_to_base(i) != ibp.bases[i]) {
LOG4CXX_FATAL(ibp.logger, "int_to_base(i) != bases[i]");
abort();
}
if (ibp.base_to_int(ibp.bases[i]) != i) {
LOG4CXX_FATAL(ibp.logger, "base_to_int(bases[i]) != i");
abort();
}
base_t b = ibp.int_to_base(i);
unsigned int ui = ibp.base_to_int(b);
if (ui != i) {
LOG4CXX_FATAL(ibp.logger, "base_to_int(bases[i]) != i");
abort();
}
LOG4CXX_TRACE(ibp.logger, "i: " << std::dec << i << " converts to '" << b << "'.");
b = ibp.bases.at(i);
ui = ibp.base_to_int(b);
if (ui != i) {
LOG4CXX_FATAL(ibp.logger, "ui != i");
abort();
}
LOG4CXX_TRACE(ibp.logger, "base '" << b << "' converts to " << std::dec << ui << ".");
}
LOG4CXX_INFO(ibp.logger, "base_to_int and int_to_base work OK.");
LOG4CXX_DEBUG(ibp.logger, "Newly-created base: " << ibp);
// start at min value
if (ibp.get_int() != ibp.begin()) {
LOG4CXX_FATAL(ibp.logger, "ib.get_int() != begin()");
abort();
}
// ++ operator works
for (unsigned int i=1; i<ibp.end(); ++i) {
++ibp;
LOG4CXX_DEBUG(ibp.logger, "i: " << i << "; ib: " << ibp);
if (ibp.get_base() != ibp.int_to_base(i)) {
LOG4CXX_FATAL(ibp.logger, "ib.get_base() != int_to_base(i)");
abort();
}
}
LOG4CXX_DEBUG(ibp.logger, "After ++ loop, ib is: " << ibp);
// -- operator works
for (unsigned int i=ibp.alphabet_size; i>0; --i) {
LOG4CXX_DEBUG(ibp.logger, "i: " << i << "; ib: " << ibp);
if (ibp.get_base() != ibp.int_to_base(i-1)) {
LOG4CXX_FATAL(ibp.logger, "ib.get_base() != int_to_base(i-1)");
abort();
}
--ibp;
}
LOG4CXX_DEBUG(ibp.logger, "After -- loop, ib is: " << ibp);
};
friend std::ostream& operator<< (std::ostream &stream, intbase ib) {
stream << "{" << std::dec << ib.get_base() << " (" << ib.get_int() << ")}";
return stream;
};
};
namespace std
{
template <>
struct hash<intbase>
{
size_t operator()(const intbase& ib) const
{
return hash<unsigned int>()(ib.get_int());
}
};
}
#endif // INTBASE_H