-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitset.cpp
More file actions
144 lines (116 loc) · 3.16 KB
/
Bitset.cpp
File metadata and controls
144 lines (116 loc) · 3.16 KB
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
#include "Bitset.h"
#include <cstdlib>
#include <string>
#include <cstring>
#include "Utils.h"
Bitset::Bitset()
: m_isInitialised(false)
{
}
Bitset::~Bitset()
{
if (m_isInitialised) {
delete[] m_mask;
}
}
size_t Bitset::maskSize() const
{
return indexPageCount() * m_globConf->pageSize();
}
size_t Bitset::indexPageCount() const
{
return Utils::roundUpDiv(
m_globConf->pageCount(),
8 * m_globConf->pageSize()
);
}
void Bitset::initialize(
GlobalConfiguration *_globConf,
const std::vector<size_t> &preallocatedPagesNum,
size_t _indexStartingPage
)
{
if (m_isInitialised) {
throw std::string("Bitset is already intitialised");
}
m_isInitialised = true;
m_globConf = _globConf;
if (!m_globConf) {
throw std::string("globConf can't be null");
}
m_indexStartingPage = _indexStartingPage;
m_mask = new char[maskSize()];
memset(m_mask, 0, maskSize());
for (const size_t &i : preallocatedPagesNum) {
set(i, true);
}
for (size_t i = 0; i < indexPageCount(); i++) {
set(i + m_indexStartingPage, true);
}
}
bool Bitset::get(const size_t &pos) const
{
if (!m_isInitialised) {
throw std::string("Bitset isn't initialized");
}
if (pos < 0 || pos >= m_globConf->pageCount()) {
throw std::string("Invalid bitset position");
}
return (m_mask[pos / 8] >> (pos % 8)) & 1;
}
void Bitset::set(const size_t &pos, bool value)
{
if (!m_isInitialised) {
throw std::string("Bitset isn't initialised");
}
if (pos < 0 || pos >= m_globConf->pageCount()) {
throw std::string("Invalid bitset position");
}
if (value) {
m_mask[pos / 8] |= (1 << (pos % 8));
} else {
m_mask[pos / 8] &= ~(1 << (pos % 8));
}
}
size_t Bitset::freePageNumber() const
{
if (!m_isInitialised) {
throw std::string("Bitset isn't initialised");
}
for (size_t i = 0; i < m_globConf->pageCount(); i++) {
if (!get(i)) {
return i;
}
}
throw std::string("There is no free pages");
}
void Bitset::read(GlobalConfiguration *_globConf, Page &headerPage, PageReadWriter &rw)
{
if (m_isInitialised) {
throw std::string("Bitset is initialised");
}
m_isInitialised = true;
m_globConf = _globConf;
if (!m_globConf) {
throw std::string("globConf can't be null");
}
headerPage.read(&m_indexStartingPage, sizeof(m_indexStartingPage));
m_mask = new char[maskSize()];
for (size_t i = 0; i < indexPageCount(); i++) {
Page curPage(i + m_indexStartingPage, m_globConf->pageSize());
rw.read(curPage);
curPage.read(m_mask + i * m_globConf->pageSize(), m_globConf->pageSize());
}
}
void Bitset::write(Page &headerPage, PageReadWriter &rw) const
{
if (!m_isInitialised) {
throw std::string("Bitset isn't initialised");
}
headerPage.write(&m_indexStartingPage, sizeof(m_indexStartingPage));
for (size_t i = 0; i < indexPageCount(); i++) {
Page curPage(i + m_indexStartingPage, m_globConf->pageSize());
curPage.write(m_mask + i * m_globConf->pageSize(), m_globConf->pageSize());
rw.write(curPage);
}
}