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
|
/**
* @file
* @brief Bit array data type.
**/
#include "AppHdr.h"
#include "bitary.h"
bit_vector::bit_vector(unsigned long s)
: size(s)
{
nwords = static_cast<int>((size + LONGSIZE - 1) / LONGSIZE);
data = new unsigned long[nwords];
reset();
}
bit_vector::bit_vector(const bit_vector& other) : size(other.size)
{
nwords = static_cast<int>((size + LONGSIZE - 1) / LONGSIZE);
data = new unsigned long[nwords];
for (int w = 0; w < nwords; ++w)
data[w] = other.data[w];
}
bit_vector::~bit_vector()
{
delete[] data;
}
void bit_vector::reset()
{
for (int w = 0; w < nwords; ++w)
data[w] = 0;
}
bool bit_vector::get(unsigned long index) const
{
ASSERT(index < size);
int w = index / LONGSIZE;
int b = index % LONGSIZE;
return data[w] & (1UL << b);
}
void bit_vector::set(unsigned long index, bool value)
{
ASSERT(index < size);
int w = index / LONGSIZE;
int b = index % LONGSIZE;
if (value)
data[w] |= (1UL << b);
else
data[w] &= ~(1UL << b);
}
bit_vector& bit_vector::operator |= (const bit_vector& other)
{
ASSERT(size == other.size);
for (int w = 0; w < nwords; ++w)
data[w] |= other.data[w];
return *this;
}
bit_vector& bit_vector::operator &= (const bit_vector& other)
{
ASSERT(size == other.size);
for (int w = 0; w < nwords; ++w)
data[w] &= other.data[w];
return *this;
}
bit_vector bit_vector::operator & (const bit_vector& other) const
{
ASSERT(size == other.size);
bit_vector res = bit_vector(size);
for (int w = 0; w < nwords; ++w)
res.data[w] = data[w] & other.data[w];
return res;
}
|