-
Notifications
You must be signed in to change notification settings - Fork 10
/
bitmask.h
539 lines (421 loc) · 13.2 KB
/
bitmask.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
// ThermonucleotideBLAST
//
// Copyright (c) 2007, Los Alamos National Security, LLC
// All rights reserved.
//
// Copyright 2007. Los Alamos National Security, LLC. This software was produced under U.S. Government
// contract DE-AC52-06NA25396 for Los Alamos National Laboratory (LANL), which is operated by Los Alamos
// National Security, LLC for the U.S. Department of Energy. The U.S. Government has rights to use,
// reproduce, and distribute this software. NEITHER THE GOVERNMENT NOR LOS ALAMOS NATIONAL SECURITY,
// LLC MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE.
// If software is modified to produce derivative works, such modified software should be clearly marked,
// so as not to confuse it with the version available from LANL.
//
// Additionally, redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this list of conditions
// and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
// and the following disclaimer in the documentation and/or other materials provided with the distribution.
// * Neither the name of Los Alamos National Security, LLC, Los Alamos National Laboratory, LANL,
// the U.S. Government, nor the names of its contributors may be used to endorse or promote products
// derived from this software without specific prior written permission.
//
//
// THIS SOFTWARE IS PROVIDED BY LOS ALAMOS NATIONAL SECURITY, LLC AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LOS ALAMOS NATIONAL SECURITY, LLC
// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// bitmask v. 0.1
// J.D. Gans 9/14/05
//
// A class, very similar to bitset, that provides:
// 1) dynamic sizing
// 2) Comperable speed to bitset
// 3) The serialization funtions:
// mpi_pack/mpi_unpack, read/write
//
// 12/14/05 Fixed bug in operator=(). Bits were not properly set.
// 12/19/05 Added mpi_pack and mpi_pack member functions
// 01/10/06 Added Set operations and helper functions:
// operator=(const set<unsigned int> &m_set);
// 02/01/06 Fixed bug in clear() -- bits was not zero to zero.
// Added addition converstions between bitmask and std::set<unsigned int>
// 08/29/06 Added read() and write() for binary I/O
// 09/06/06 Added operator=(const __bitelem &m_bit). This operator is required
// for chaining bit copies; i.e. A[0] = B[0] = 1.
// 01/09/07 Added a toggle() member function to __bitelem to allow efficient bit
// flipping.
#ifndef __BIT_MASK
#define __BIT_MASK
#include <string.h> // For memset
#include <set>
#include <iostream>
#include <fstream>
#include <string>
class __bitelem{
private:
unsigned char *byte;
unsigned char offset;
public:
inline __bitelem(unsigned char *m_byte, const unsigned char &m_offset)
{
byte = m_byte;
offset = m_offset;
};
// Wow -- performance is dramatically improved by an explicit
// destructor.
~__bitelem()
{
// Do nothing!
};
inline __bitelem& operator=(const bool &m_bool)
{
*byte = (m_bool) ? ( *byte | (1 << offset) ) : ( *byte & ~(1 << offset) );
return *this;
};
inline __bitelem& operator=(const __bitelem &m_bit)
{
*byte = bool(m_bit) ? ( *byte | (1 << offset) ) : ( *byte & ~(1 << offset) );
return *this;
};
inline operator bool() const
{
return ( (*byte >> offset) & 1 );
};
inline void toggle()
{
// Similar code to operator=(const __bitelem &m_bit) but:
// 1) explicity inlines the the bool() and
// 2) swaps the arguments to ? (i.e. bool ? A : B --> bool ? B : A)
*byte = ( (*byte >> offset) & 1 ) ?
( *byte & ~(1 << offset) ) : ( *byte | (1 << offset) );
};
};
class bitmask {
private:
enum {BITS_PER_BYTE = 8};
unsigned char* mask;
size_t len;
size_t bits;
// Make sure that the left-over bits are always zero
// (this will allow for efficient BYTE-wise comparisons)
inline void zero_remainder()
{
// Set any unused bits to zero
const unsigned char last_bit = (unsigned char)(bits%BITS_PER_BYTE);
const unsigned char remainder_mask[] = {
0xFF, // 11111111
0x01, // 00000001,
0x03, // 00000011
0x07, // 00000111
0x0F, // 00001111
0x1F, // 00011111
0x3F, // 00111111
0x7F // 01111111
};
if(last_bit != 0){
// len == 0 only if last_bit == 0
mask[len - 1] &= remainder_mask[last_bit];
}
};
public:
bitmask()
{
mask = NULL;
len = 0;
bits = 0;
};
bitmask(const unsigned int &m_num_bits, const bool &m_bool = false)
{
bits = m_num_bits;
if(bits == 0){
mask = NULL;
len = 0;
return;
}
len = (bits/BITS_PER_BYTE) +
(bits%BITS_PER_BYTE != 0);
mask = new unsigned char [len];
if(mask == NULL){
throw "bitmask: Unable to allocate memory!";
}
// Flip bits to either all 1's or all 0's
memset(mask, m_bool ? 0xff : 0x00, len);
// Set all remainder bits (if any) to zero
zero_remainder();
};
bitmask(const bitmask &m_mask)
{
if(m_mask.mask == NULL){
mask = NULL;
len = 0;
bits = 0;
return;
}
len = m_mask.len;
bits = m_mask.bits;
mask = new unsigned char [len];
if(mask == NULL){
throw "bitmask: Unable to allocate memory!";
}
// Copy the memory
memcpy(mask, m_mask.mask, len);
};
~bitmask()
{
if(mask){
delete [] mask;
mask = NULL;
}
};
// No bounds checking for efficient access
inline __bitelem operator[](const unsigned int &m_index)
{
const unsigned int core = m_index/BITS_PER_BYTE;
const unsigned char remainder = m_index%BITS_PER_BYTE;
return __bitelem(mask + core, remainder);
};
inline bool operator[](const unsigned int &m_index) const
{
const unsigned int core = m_index/BITS_PER_BYTE;
const unsigned char remainder = m_index%BITS_PER_BYTE;
return ( (mask[core] >> remainder) & 1 );
};
// Check the bounds
inline __bitelem at(const unsigned int &m_index)
{
if(m_index >= bits){
throw "bitmask:at: index out of bounds!";
}
const unsigned int core = m_index/BITS_PER_BYTE;
const unsigned char remainder = m_index%BITS_PER_BYTE;
return __bitelem(mask + core, remainder);
};
inline bool at(const unsigned int &m_index) const
{
if(m_index >= bits){
throw "bitmask:at: index out of bounds!";
}
const unsigned int core = m_index/BITS_PER_BYTE;
const unsigned char remainder = m_index%BITS_PER_BYTE;
return ( (mask[core] >> remainder) & 1 );
};
// Reset -> 0
inline void reset()
{
memset(mask, 0x00, len);
// We don't need to zero the remainder since all bits
// are already 0 .
// zero_remainder();
};
// Overwrite this bitmask with the contents of the m_set. Note that
// m_set is assumed to be a zero based set! Sets whose elements are
// not clustered around zero may result in a bit set that can be very large
// (i.e inefficient)!
void reset(const unsigned int &m_num_bits, const std::set<unsigned int> &m_set);
inline void clear()
{
if(mask){
delete [] mask;
mask = NULL;
}
len = 0;
bits = 0;
};
inline size_t size() const
{
return bits;
};
// Similar to std::includes()
bool includes(const bitmask &m_set) const;
bool operator==(const bitmask &m_mask) const;
bool operator==(const std::set<unsigned int> &m_mask) const;
bool operator!=(const bitmask &m_mask) const;
bool operator<(const bitmask &m_mask) const;
bool operator>(const bitmask &m_mask) const;
// Set difference
bitmask operator-(const bitmask &m_mask) const;
// Set union
bitmask operator+(const bitmask &m_mask) const;
// Make (*this) the union of (*this) and m_set
bitmask& operator += (const bitmask &m_set);
// Set intersection
bitmask operator&(const bitmask &m_mask) const;
// Make (*this) the intersection of (*this) and m_set
bitmask& operator &= (const bitmask &m_set);
// True if all bits are zero, false otherwise
inline bool empty() const
{
unsigned char *ptr = mask;
unsigned char offset = 0;
for(unsigned int i = 0;i < bits;i++){
// If this bit is 1 (i.e. true) return false
// right away.
if( ( (*ptr >> offset) & 1) == 1 ){
return false;
}
offset++;
if(offset == BITS_PER_BYTE){
ptr++;
offset = 0;
}
}
return true;
};
// Count the number of bits == 1
inline unsigned int count() const
{
unsigned int tmp = 0;
unsigned char *ptr = mask;
unsigned char offset = 0;
for(unsigned int i = 0;i < bits;i++){
tmp += ( ( (*ptr >> offset) & 1) == 1 );
offset++;
if(offset == BITS_PER_BYTE){
ptr++;
offset = 0;
}
}
return tmp;
};
inline void resize(const unsigned int &m_num_bits, const bool &m_bool)
{
// Deallocate existing memory
if(mask){
delete [] mask;
}
bits = m_num_bits;
if(bits == 0){
mask = NULL;
len = 0;
return;
}
len = (bits/BITS_PER_BYTE) +
(bits%BITS_PER_BYTE != 0);
mask = new unsigned char [len];
if(mask == NULL){
throw "bitmask: Unable to allocate memory!";
}
// Flip bits to either all 1's or all 0's
memset(mask, m_bool ? 0xff : 0x00, len);
// Set all remainder bits (if any) to zero
zero_remainder();
};
inline bitmask& operator=(const bitmask &m_mask)
{
if(mask){
delete [] mask;
}
if(m_mask.mask == NULL){
mask = NULL;
len = 0;
bits = 0;
return *this;
}
len = m_mask.len;
bits = m_mask.bits;
mask = new unsigned char [len];
if(mask == NULL){
throw "bitmask: Unable to allocate memory!";
}
// Copy the memory
memcpy(mask, m_mask.mask, len);
return *this;
};
inline bitmask& operator=(const int &m_int)
{
// Flip bits to either all 0's or all 1's
memset(mask, (m_int == 0) ? 0x00 : 0xff, len);
// Set all remainder bits (if any) to zero
zero_remainder();
return *this;
};
inline bitmask& operator=(const bool &m_bool)
{
// Flip bits to either all 1's or all 0's
memset(mask, m_bool ? 0xff : 0x00, len);
// Set all remainder bits (if any) to zero
zero_remainder();
return *this;
};
bitmask& operator=(const std::string &m_mask);
bitmask& operator=(const char* m_mask);
inline size_t mpi_size() const
{
return sizeof(unsigned int) + // bits
sizeof(unsigned int) + // len
len;
};
inline unsigned char *mpi_pack(unsigned char *m_ptr) const
{
// The number of bits
memcpy(m_ptr, &bits, sizeof(unsigned int) );
m_ptr += sizeof(unsigned int);
// The length of the bit array (in bytes)
memcpy(m_ptr, &len, sizeof(unsigned int) );
m_ptr += sizeof(unsigned int);
// The bit array
memcpy(m_ptr, mask, len);
m_ptr += len;
return m_ptr;
};
inline unsigned char *mpi_unpack(unsigned char *m_ptr)
{
// Clean up any existing memory
if(mask){
delete [] mask;
mask = NULL;
}
// The number of bits
memcpy(&bits, m_ptr, sizeof(unsigned int) );
m_ptr += sizeof(unsigned int);
// The length of the bit array (in bytes)
memcpy(&len, m_ptr, sizeof(unsigned int) );
m_ptr += sizeof(unsigned int);
mask = new unsigned char [len];
if(mask == NULL){
throw "bitmask:mpi_unpack: Unable to allocate memory!";
}
// Copy the bit array
memcpy(mask, m_ptr, len);
m_ptr += len*sizeof(unsigned char);
return m_ptr;
};
void write(std::ofstream &m_fout) const
{
// The number of bits
m_fout.write( (const char*)&bits, sizeof(bits) );
// The length of the bit array (in bytes)
m_fout.write( (const char*)&len, sizeof(len) );
// The bit array
m_fout.write( (const char*)mask, len );
};
void read(std::ifstream &m_fin)
{
// Clean up any existing memory
if(mask){
delete [] mask;
mask = NULL;
}
// The number of bits
m_fin.read( (char*)&bits, sizeof(bits) );
// The length of the bit array (in bytes)
m_fin.read( (char*)&len, sizeof(len) );
mask = new unsigned char [len];
if(mask == NULL){
throw "bitmask:read: Unable to allocate memory!";
}
// The bit array
m_fin.read( (char*)mask, len );
}
// Convert this bitmask into a std::set<unsigned int>
operator std::set<unsigned int>() const;
};
std::ostream& operator<<(std::ostream &s, const bitmask &m_set);
#endif // __BIT_MASK