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
|
/****************************************************************/
/* Parallel Combinatorial BLAS Library (for Graph Computations) */
/* version 1.4 -------------------------------------------------*/
/* date: 1/17/2014 ---------------------------------------------*/
/* authors: Aydin Buluc (abuluc@lbl.gov), Adam Lugowski --------*/
/* this file contributed by Scott Beamer of UC Berkeley --------*/
/****************************************************************/
/*
Copyright (c) 2010-2014, The Regents of the University of California
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef BITMAP_H
#define BITMAP_H
#include <stdint.h>
#define WORD_OFFSET(n) (n/64)
#define BIT_OFFSET(n) (n & 0x3f)
namespace combblas {
class BitMap {
public:
// actually always rounds up
BitMap() { start = NULL; end = NULL;}; // default constructor
BitMap(uint64_t size) {
uint64_t num_longs = (size + 63) / 64;
// VS9: warning C4244: 'initializing' : conversion from 'uint64_t' to 'unsigned int', possible loss of data
start = new uint64_t[num_longs](); // zero initialized by default
end = start + num_longs;
}
~BitMap() {
delete[] start;
}
BitMap(const BitMap & rhs)
{
uint64_t num_longs = rhs.end - rhs.start;
// VS9: warning C4244: 'initializing' : conversion from 'uint64_t' to 'unsigned int', possible loss of data
start = new uint64_t[num_longs];
end = start + num_longs;
std::copy(rhs.start, rhs.end, start);
}
BitMap & operator= (const BitMap & rhs)
{
if(this != &rhs)
{
delete [] start;
uint64_t num_longs = rhs.end - rhs.start;
// VS9: warning C4244: 'initializing' : conversion from 'uint64_t' to 'unsigned int', possible loss of data
start = new uint64_t[num_longs];
end = start + num_longs;
std::copy(rhs.start, rhs.end, start);
}
return *this;
}
inline
void reset() {
for(uint64_t *it=start; it!=end; it++)
*it = 0;
}
inline
void set_bit(uint64_t pos) {
start[WORD_OFFSET(pos)] |= ( static_cast<uint64_t>(1l)<<BIT_OFFSET(pos));
}
inline
void reset_bit(uint64_t pos) {
start[WORD_OFFSET(pos)] &= ~( static_cast<uint64_t>(1l)<<BIT_OFFSET(pos));
}
inline
void set_bit_atomic(long pos) {
set_bit(pos);
// uint64_t old_val, new_val;
// uint64_t *loc = start + WORD_OFFSET(pos);
// do {
// old_val = *loc;
// new_val = old_val | ((uint64_t) 1l<<BIT_OFFSET(pos));
// } while(!__sync_bool_compare_and_swap(loc, old_val, new_val));
}
inline
bool get_bit(uint64_t pos) {
// VS9: warning C4334: '<<' : result of 32-bit shift implicitly converted to 64 bits (was 64-bit shift intended?)
if (start[WORD_OFFSET(pos)] & ( static_cast<uint64_t>(1l) <<BIT_OFFSET(pos)))
return true;
else
return false;
}
inline
long get_next_bit(uint64_t pos) {
uint64_t next = pos;
int bit_offset = BIT_OFFSET(pos);
uint64_t *it = start + WORD_OFFSET(pos);
uint64_t temp = (*it);
if (bit_offset != 63) {
temp = temp >> (bit_offset+1);
} else {
temp = 0;
}
if (!temp) {
next = (next & 0xffffffc0);
while (!temp) {
it++;
if (it >= end)
return -1;
temp = *it;
next += 64;
}
} else {
next++;
}
while(!(temp&1)) {
temp = temp >> 1;
next++;
}
return next;
}
inline
uint64_t* data() {
return start;
}
void copy_from(const BitMap* other) {
std::copy(other->start, other->end, start);
}
void print_ones() {
uint64_t max_size = (end-start)*64;
for (uint64_t i=0; i<max_size; i++)
if (get_bit(i))
std::cout << " " << i;
std::cout << std::endl;
}
private:
uint64_t *start;
uint64_t *end;
};
}
#endif // BITMAP_H
|