-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBitmap.cpp
153 lines (130 loc) · 4.69 KB
/
Bitmap.cpp
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
//
// Bitmap.cpp
// BitmapIndex
//
// Created on 09.02.13.
// Copyright (c) 2013 Christoph Schaefer. All rights reserved.
//
#include "Bitmap.h"
#include "math.h"
#include <fstream>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
Bitmap::Bitmap(wchar_t c) :
_character(c),
_rowCount(0),
_bitVec(std::make_shared<std::vector<uint64_t>>()),
_bitArray(NULL),
_bitmapFileDescriptor(-1),
_totalPopCount(0)
{
};
Bitmap::Bitmap(wchar_t c, const uint64_t& size) :
_character(c),
_rowCount(size),
_bitVec(),
_bitArray(new uint64_t[size/64 + 1]),
_bitmapFileDescriptor(-1),
_totalPopCount(0)
{
};
Bitmap::~Bitmap() {
if(_bitArray) {
if(_bitmapFileDescriptor != -1) {
close(_bitmapFileDescriptor);
}
else {
delete[] _bitArray;
}
}
};
void Bitmap::addRow(const wchar_t& c){
uint16_t currentOffset = _rowCount % 64;
if (currentOffset == 0) {
_bitVec->push_back(uint64_t(0));
}
if (c != _character) {
// this bitmap stands for an other character
// the correponding bit remains at 0
}
else {
_totalPopCount++;
setBitAtPosition(_bitVec->at(_rowCount/64), currentOffset);
}
_rowCount++;
};
void Bitmap::setBitAtPosition(uint64_t& number, const uint16_t& position) {
number |= uint64_t(1) << position;
};
UInt32VecPtr Bitmap::getTargetRows(const RangePair& range){
uint64_t blockNumber = range.first / 64; // 64 rows in one block, one block is a uint64
uint64_t* rowIter = _bitArray + blockNumber;
uint64_t* rowIterEnd = _bitArray + (range.second / 64);
UInt32VecPtr result = std::make_shared<std::vector<uint32_t>>();
uint16_t resultSize = 0;
//TODO flip bitorder
for ( ; rowIter <= rowIterEnd; ++rowIter, ++blockNumber) {
uint64_t bits = *rowIter;
// http://stackoverflow.com/questions/14086854/get-an-array-of-the-bit-positions-within-a-64-bit-integer
for (; bits != 0; resultSize++, bits &= (bits - 1L))
{
uint64_t row = blockNumber * 64 + log2(bits & ~(bits-1L));
if(row >= range.first && row < range.second) {
result->push_back(row);
if(resultSize == MAX_RESULT_SIZE) {
return result;
}
}
}
}
return result;
};
void Bitmap::AND(const BitmapPtr& bitmap, const RangePair& range){
uint64_t* rhsIter = bitmap->_bitArray + range.first/64;
uint64_t* resultIter = _bitArray + range.first/64;
uint64_t* resultIterEnd = _bitArray + (range.second/64) + 1;
for(; resultIter != resultIterEnd; ++resultIter, ++rhsIter){
*resultIter &= *rhsIter;
//std::cout << "resultIter: " << *resultIter << ", rhsIter: " << bitmap->_character << *rhsIter << std::endl;
}
}
void Bitmap::AND(const BitmapPtr& lhsBitmap, const BitmapPtr& rhsBitmap, const RangePair& range) {
uint64_t* lhsIter = lhsBitmap->_bitArray + range.first/64;
uint64_t* rhsIter = rhsBitmap->_bitArray + range.first/64;
uint64_t* resultIter = _bitArray + range.first/64;
uint64_t* resultIterEnd =_bitArray + (range.second/64) + 1;
for(; resultIter != resultIterEnd; ++lhsIter, ++rhsIter, ++resultIter){
*resultIter = *lhsIter & *rhsIter;
//std::cout << "resultIter: " << *resultIter << ", lhs: " << lhsBitmap->_character << *lhsIter << ", rhs: " << rhsBitmap->_character << *rhsIter << std::endl;
}
}
void Bitmap::writeToDisc(const std::string& path) {
if(_totalPopCount) {
std::ofstream ofs;
// hash the _character because some chars like '/' are not allowed in filenames
ofs.open(path + "_" + std::to_string(static_cast<uint32_t>(_character)), std::ios::out | std::ios::binary);
uint64_t vectorSize = _bitVec->size();
ofs.write(reinterpret_cast<char*>(&vectorSize), sizeof(uint64_t));
ofs.write(reinterpret_cast<char*>(&_bitVec->at(0)), vectorSize * sizeof(uint64_t));
ofs.close();
}
}
void Bitmap::readFromDisc(const std::string& path){
_bitmapFileDescriptor = open((path + "_" + std::to_string(static_cast<uint32_t>(_character))).c_str(), O_RDONLY);
if(_bitmapFileDescriptor > 0) {
uint64_t vectorSize;
read(_bitmapFileDescriptor, &vectorSize, sizeof(uint64_t));
_rowCount = vectorSize * 64;
size_t fileSizeInBytes = vectorSize * sizeof(uint64_t) + sizeof(uint64_t);
_bitArray = reinterpret_cast<uint64_t*>(mmap(0, fileSizeInBytes, PROT_READ, MAP_SHARED, _bitmapFileDescriptor, 0));
if(_bitArray == MAP_FAILED) {
std::cout << "ERROR mmapping file " << path << "_" << static_cast<uint32_t>(_character) << std::endl;
exit(1);
}
_bitArray++; // jump over the "header" which contains the file size
}
else {
_rowCount = 0;
}
}