forked from MaZderMind/osm-history-splitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrowing_bitset.hpp
69 lines (50 loc) · 1.63 KB
/
growing_bitset.hpp
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
#ifndef GROWING_BITSET_HPP
#define GROWING_BITSET_HPP
#include <memory>
#include <vector>
#include <osmium/osm/types.hpp>
class growing_bitset {
static const size_t segment_size = 50*1024*1024;
typedef std::vector<bool> bitvec_type;
typedef bitvec_type* bitvec_ptr_type;
std::vector<std::unique_ptr<bitvec_type>> bitmap;
static size_t segment(const osmium::object_id_type pos) {
return pos / static_cast<osmium::object_id_type>(segment_size);
}
static size_t segmented_pos(const osmium::object_id_type pos) {
return pos % static_cast<osmium::object_id_type>(segment_size);
}
bitvec_ptr_type find_segment(size_t segment) {
if (segment >= bitmap.size()) {
bitmap.resize(segment+1);
}
auto ptr = bitmap[segment].get();
if (!ptr) {
bitmap[segment].reset(new bitvec_type(segment_size));
return bitmap[segment].get();
}
return ptr;
}
bitvec_ptr_type find_segment(size_t segment) const {
if (segment >= bitmap.size()) {
return nullptr;
}
return bitmap[segment].get();
}
public:
void set(const osmium::object_id_type pos) {
bitvec_ptr_type bitvec = find_segment(segment(pos));
bitvec->at(segmented_pos(pos)) = true;
}
bool get(const osmium::object_id_type pos) const {
bitvec_ptr_type bitvec = find_segment(segment(pos));
if (!bitvec) return false;
return bitvec->at(segmented_pos(pos));
}
void clear() {
for (auto& ptr : bitmap) {
ptr->clear();
}
}
}; // class growing_bitset
#endif