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 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
|
#ifndef BIT_ARRAY_H
#define BIT_ARRAY_H
#include <vector>
#include <climits>
#include <stdexcept>
#include <string>
#include <math.h>
#include <iostream>
#include <utility>
using std::vector;
using std::string;
using std::cout;
using std::endl;
using std::ostream;
using std::istream;
using std::logic_error;
template<class IType = size_t>
class BitArray
{
private:
vector<IType> Bit_Vector;
size_t _count;
enum { BITS_PER_WORD = CHAR_BIT* sizeof(IType) };
bool test(size_t pos) const
{
return Bit_Vector[pos / BITS_PER_WORD] & (1u << (pos%BITS_PER_WORD));
}
void reset(size_t pos)
{
Bit_Vector[pos / BITS_PER_WORD] &= ~(1u << (pos%BITS_PER_WORD));
}
void set(size_t pos)
{
Bit_Vector[pos / BITS_PER_WORD] |= (1u << (pos % BITS_PER_WORD));
}
void copy(size_t pos, bool value)
{
if (value)
set(pos);
else
reset(pos);
}
bool readBit(size_t pos)const
{
size_t block = pos / BITS_PER_WORD;
size_t offset = pos % BITS_PER_WORD;
if (pos >= _count)
throw logic_error("Index given is out of range.");
else
return Bit_Vector[block] & (1u << offset);
}
bool assignBit(size_t pos, bool value)
{
size_t block = pos / BITS_PER_WORD;
size_t offset = pos % BITS_PER_WORD;
if (value)
Bit_Vector[block] |= (1u << offset);
else
Bit_Vector[block] &= ~(1u << offset);
}
public:
// Object Management
explicit BitArray(size_t bitCount = 0)
{
_count = bitCount;
if (bitCount)
Bit_Vector.resize((bitCount - 1) / BITS_PER_WORD + 1);
};
explicit BitArray(const string& bit_string)
{
_count = bit_string.length();
if (_count)
Bit_Vector.resize((_count - 1) / BITS_PER_WORD + 1);
for (size_t i = 0; i < _count; ++i)
{
if (bit_string[i] == '1')
{
set(i);
}
else if (bit_string[i] == '0')
{
reset(i);
}
else
throw runtime_error("0 or 1 not found");
}
};
explicit BitArray(const BitArray& b) = default; // Copy constructor
BitArray& operator=(const BitArray& b) = default; // Copy assignment
explicit BitArray(BitArray&& b) noexcept : Bit_Vector(b.Bit_Vector) // Move assignment
{
cout << "move constructor" << endl;
_count = b._count;
//Bit_Vector = b.Bit_Vector;
b._count = 0;
b.Bit_Vector.clear();
};
BitArray& operator=(BitArray&& b) noexcept
{
cout << "move assignment" << endl;
swap(_count, b._count);
swap(Bit_Vector, b.Bit_Vector);
return *this;
};
size_t capacity() const
{
return (BITS_PER_WORD * Bit_Vector.size());
};
// Append a bit
BitArray& operator+=(bool bit)
{
//2 options space/no space
if (capacity() == _count)
Bit_Vector.resize(Bit_Vector.size() + 1);
_count++;
//put bit in last spot
copy(_count - 1, bit);
return *this;
}
//** Append a BitArray
BitArray& operator+=(const BitArray& b)
{
// Add BitArray obj starting from the last spot of the current BitArray
if (capacity() == _count)
Bit_Vector.resize(Bit_Vector.size() + Bit_Vector.size());
//Append BitArray obj to the end
for (int i = 0; i < _count; i++)
{
copy(_count-1, b[i]);
_count++;
}
return *this;
}
//** Remove “nbits” bits at a position
void erase(size_t pos, size_t nbits = 1)
{
????Bit_Vector[pos/BITS_PER_WORD] = nbits/BITS_PER_WORD;
}
//** Insert a bit at a position (slide "right")
void insert(size_t pos, bool value)
{
}
//** Insert an entire BitArray object
void insert(size_t pos, const BitArray& bitArray)
{
}
// Discard unused, trailing vector cells
void shrink_to_fit()
{
if (!Bit_Vector[Bit_Vector.size() - 1]) //ask if it's a 0
{
Bit_Vector.resize((_count - 1) / BITS_PER_WORD + 1);
}
}
// Bitwise ops
class bitproxy
{
BitArray* b;
int pos;
public:
bitproxy(BitArray* bs, int p) : b(bs) {
pos = p;
}
bitproxy& operator=(bool bit) {
if (bit)
b->Bit_Vector[pos/BITS_PER_WORD] |= (1u << (pos%BITS_PER_WORD));
else
b->Bit_Vector[pos / BITS_PER_WORD] &= ~(1u << (pos%BITS_PER_WORD));
return *this;
}
operator bool() const {
return b->Bit_Vector[pos / BITS_PER_WORD] & (1u << (pos%BITS_PER_WORD));
}
};
friend class bitproxy;
bitproxy operator[](size_t pos)
{
if (pos >= _count)
throw logic_error("Invalid Index.");
return bitproxy(this, pos);
}
bool operator[](size_t pos) const
{
if (pos>=_count)
throw logic_error("Invalid Index.");
else
return test(pos);
}
// Toggles all bits
void toggle(size_t pos)
{
if (test(pos))
reset(pos);
else
set(pos);
}
void toggle() // toggles all bits
{
for (size_t i = 0; i < _count; ++i)
{
if (test(i))
reset(i);
else
set(i);
}
}
BitArray operator~() const
{
BitArray<> bitArray(*this);
bitArray.toggle();
return bitArray; // //ERROR :C2440 'return': cannot convert from 'BitArray<size_t>' to 'BitArray<size_t>'
}
// Shift operators…
BitArray operator<<(unsigned int sLeft) const
{
BitArray<> bitArray(*this);
return bitArray <<= sLeft; //ERROR :C2440 'return': cannot convert from 'BitArray<size_t>' to 'BitArray<size_t>'
}
BitArray operator>>(unsigned int sRight) const
{
BitArray<> bitArray(*this);
return bitArray >>= sRight; //ERROR :C2440 'return': cannot convert from 'BitArray<size_t>' to 'BitArray<size_t>'
}
BitArray& operator<<=(unsigned int n)
{
BitArray<> temp1 = slice(n, _count - n); //ERROR :C2440 'initializing': cannot convert from 'BitArray<size_t>' to 'BitArray<size_t>'
BitArray<> temp2(n);
temp1 += temp2;
*this = temp1;
return *this;
}
BitArray& operator>>=(unsigned int n)
{
BitArray<> temp1(n);
BitArray<> temp2 = slice(n, _count - n); //Error C2440 'initializing': cannot convert from 'BitArray<size_t>' to 'BitArray<size_t>'
temp1 += temp2;
*this = temp1;
return *this;
}
BitArray slice(size_t pos, size_t count) const
{
string bitstr = (*this).to_string();
BitArray b{ bitstr.substr(pos, count) };
return b; //Error C2440 'initializing': cannot convert from 'BitArray<size_t>' to 'BitArray<size_t>'
}
bool operator==(const BitArray& checkBits) const
{
return !((*this) < checkBits || checkBits > (*this));
}
bool operator!=(const BitArray& checkBits) const
{
return ((*this) < checkBits || checkBits > (*this));
}
bool operator<(const BitArray& checkBits) const
{
if (strcmp((*this).to_string().c_str(), checkBits.to_string().c_str()) < 0)
return true;
else
return false;
}
bool operator<=(const BitArray& checkBits) const
{
if (strcmp((*this).to_string().c_str(), checkBits.to_string().c_str()) <= 0)
return true;
else
return false;
}
bool operator>(const BitArray& checkBits) const
{
if (strcmp((*this).to_string().c_str(), checkBits.to_string().c_str()) > 0)
return true;
else
return false;
}
bool operator>=(const BitArray& checkBits) const
{
if (strcmp((*this).to_string().c_str(), checkBits.to_string().c_str()) >= 0)
return true;
else
return false;
}
size_t size() const
{
return _count;
}
size_t count() const
{
size_t one_bits = 0;
for (size_t i = 0; i < _count; ++i)
{
if (test(i))
one_bits++;
}
return one_bits;
}
bool any() const
{
//for loop through vector
for (size_t i = 0; i < Bit_Vector.size(); i++)
{
if (Bit_Vector[i])
return true;
}
return false;
}
friend ostream& operator<<(ostream& ostrm, const BitArray& _bitarray)
{
ostrm << _bitarray.to_string();
return ostrm;
}
friend istream& operator>>(istream& istrm, BitArray& _bitarray)
{
string bitStr = "";
char charChk;
char charNext = 0;
while (istrm >>charChk)
{
if (!isdigit(charChk))
{
charNext = -1;
charNext = istrm.peek();
istrm.unget();
istrm.setstate(std::ios::failbit);
break;
}
else
{
bitStr.push_back(charChk);
}
}
if (charNext == -1)
return istrm;
istrm.clear();
BitArray<> ba(bitStr);
_bitarray = ba;
return istrm;
}
string to_string() const
{
string charStr = "";
for (size_t i = 0; i < _count; i++)
{
if(test(i))
charStr += '1'; //adding a 1
else
charStr += '0'; //adding a 0
}
return charStr;
}
};
|