-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBits.cpp
293 lines (244 loc) · 6.18 KB
/
Bits.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
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
//
// Created by Development Environment on 4/29/19.
//
#include "Bits.h"
#include <fstream>
#include <string.h>
#include <cmath>
using namespace std;
Bitset::Bitset(std::string s_fileName, int size)
{
int temp = countChars(s_fileName);
if (temp > 0) {
ifstream file(s_fileName.c_str());
capacity = temp;
set = new char[capacity];
this->size = size;
bitsPerRow = static_cast<int>(sqrt(size));
if (file.is_open() && !file.bad()) {
char c;
int counter = 0;
while (file >> c && counter < capacity) {
set[counter] = c;
counter++;
} // end while
} // end if
file.close();
} else {
throw new invalid_argument("Size must be greater than 0.");
}
}
Bitset::Bitset(Bitset &other)
{
size = other.size;
capacity = other.capacity;
set = new char[capacity];
bitsPerRow = other.bitsPerRow;
int i;
for(i = 0; i < capacity; i++)
{
set[i] = other.set[i];
}
}
Bitset::~Bitset(void)
{
delete[] set;
}
int Bitset::operator()(int i, int j)
{
int _byte = (i * bitsPerRow) / __BYTEOFFSET;
int rowStart = (i * bitsPerRow) % __BYTEOFFSET;
int _bit = rowStart + j;
while (_bit > 7)
{
_byte++;
_bit -= 8;
} // end
if (testBit(_byte, _bit))
{
return 1;
} // end if
return 0;
}
int Bitset::operator[](int i)
{
int byte = i / __BYTEOFFSET;
int bit = i % __BYTEOFFSET;
if (testBit(byte, bit))
{
return 1;
} // end if
return 0;
} // end operator[]
void Bitset::setBit(int i, int j, bool b_val)
{
int _byte = (i * bitsPerRow) / __BYTEOFFSET;
int rowStart = (i * bitsPerRow) % __BYTEOFFSET;
int _bit = rowStart + j;
while (_bit > 7)
{
_byte++;
_bit -= 8;
} // end
setBit(_byte, _bit, (b_val ? 1 : 0));
}
bool Bitset::allSame()
{
int temp = (testBit(0, 0) ? 1 : 0);
for(int i = 0; i < size; i++)
{
if (this->operator[](i) != temp)
{
return false;
} // end if
} // end for
return true;
}
void Bitset::setBit(int i, int j, int i_val)
{
if (i >= capacity)
{
string error = "Not a valid byte. Received: " + i;
error += " Expected: 0-" + size;
throw new invalid_argument(error);
} // end if
char temp = set[i];
char* bit = &set[i];
if (i_val && !testBit(i,j))
{
switch (j)
{
case 0:
*(bit) |= __SETBIT0MASK;
break;
case 1:
*(bit) |= __SETBIT1MASK;
break;
case 2:
*(bit) |= __SETBIT2MASK;
break;
case 3:
*(bit) |= __SETBIT3MASK;
break;
case 4:
*(bit) |= __SETBIT4MASK;
break;
case 5:
*(bit) |= __SETBIT5MASK;
break;
case 6:
*(bit) |= __SETBIT6MASK;
break;
case 7:
*(bit) |= __SETBIT7MASK;
break;
default:
string error = "Not a valid bit. Received: " + j;
error += " Expected: 0-7";
throw new invalid_argument(error);
}
}
else if(!i_val && testBit(i,j))
{
switch (j)
{
case 0:
*(bit) &= __RESETBIT0MASK;
break;
case 1:
*(bit) &= __RESETBIT1MASK;
break;
case 2:
*(bit) &= __RESETBIT2MASK;
break;
case 3:
*(bit) &= __RESETBIT3MASK;
break;
case 4:
*(bit) &= __RESETBIT4MASK;
break;
case 5:
*(bit) &= __RESETBIT5MASK;
break;
case 6:
*(bit) &= __RESETBIT6MASK;
break;
case 7:
*(bit) &= __RESETBIT7MASK;
break;
default:
string error = "Not a valid bit. Received: " + j;
error += " Expected: 0-7";
throw new invalid_argument(error);
} // end switch
} // end else
} // end method setBit(int,int,int)
void Bitset::writeToFile(string s_fileName)
{
remove(s_fileName.c_str());
ofstream file(s_fileName.c_str(), ios::app | ios::binary);
if (file.is_open() && !file.bad())
{
for (int i = 0; i < capacity; i++)
{
char temp = set[i];
file << temp;
}
}
}
bool Bitset::testBit(int i, int j)
{
if (i >= capacity)
{
string error = "Not a valid byte. Received: " + i;
error += " Expected: 0-" + (capacity-1);
throw new invalid_argument(error);
} // end if
if ((i*__BYTEOFFSET + j >= size))
{
string error = "Not a valid bit. Received: " + (i*__BYTEOFFSET + j);
error += " Expected: 0-" + (size-1);
throw new invalid_argument(error);
} // end if
char temp = set[i];
switch (j)
{
case 0:
return temp & __SETBIT0MASK;
case 1:
return temp & __SETBIT1MASK;
case 2:
return temp & __SETBIT2MASK;
case 3:
return temp & __SETBIT3MASK;
case 4:
return temp & __SETBIT4MASK;
case 5:
return temp & __SETBIT5MASK;
case 6:
return temp & __SETBIT6MASK;
case 7:
return temp & __SETBIT7MASK;
default:
string error = "Not valid bit. Received: " + j;
error += " Expected: 0-7";
throw new invalid_argument(error);
}
}
int Bitset::countChars(string s_fileName)
{
ifstream file(s_fileName, ios::binary | ios::ate);
return static_cast<int>(file.tellg());
} // end method countChars
ostream& operator<<(ostream& stream, Bitset* set)
{
for (int i = 0; i < set->bitsPerRow; i++)
{
for (int j = 0; j < set->bitsPerRow; j++)
{
stream << (*set)(i, j) << " " ;
}
stream << endl;
}
return stream;
}