kget
bitset.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "bitset.h"
00021 #include <algorithm>
00022 #include <string.h>
00023
00024 namespace bt
00025 {
00026 BitSet BitSet::null;
00027
00028 BitSet::BitSet(Uint32 num_bits) : num_bits(num_bits),data(0)
00029 {
00030 num_bytes = (num_bits / 8) + ((num_bits % 8 > 0) ? 1 : 0);
00031 data = new Uint8[num_bytes];
00032 std::fill(data,data+num_bytes,0x00);
00033 num_on = 0;
00034 }
00035
00036 BitSet::BitSet(const Uint8* d,Uint32 num_bits) : num_bits(num_bits),data(0)
00037 {
00038 num_bytes = (num_bits / 8) + ((num_bits % 8 > 0) ? 1 : 0);
00039 data = new Uint8[num_bytes];
00040 memcpy(data,d,num_bytes);
00041 num_on = 0;
00042 Uint32 i = 0;
00043 while (i < num_bits)
00044 {
00045 if (get(i))
00046 num_on++;
00047 i++;
00048 }
00049 }
00050
00051 BitSet::BitSet(const BitSet & bs) : num_bits(bs.num_bits),num_bytes(bs.num_bytes),data(0),num_on(bs.num_on)
00052 {
00053 data = new Uint8[num_bytes];
00054 std::copy(bs.data,bs.data+num_bytes,data);
00055 }
00056
00057 BitSet::~BitSet()
00058 {
00059 delete [] data;
00060 }
00061
00062
00063
00064 BitSet & BitSet::operator = (const BitSet & bs)
00065 {
00066 if (data)
00067 delete [] data;
00068 num_bytes = bs.num_bytes;
00069 num_bits = bs.num_bits;
00070 data = new Uint8[num_bytes];
00071 std::copy(bs.data,bs.data+num_bytes,data);
00072 num_on = bs.num_on;
00073 return *this;
00074 }
00075
00076 void BitSet::setAll(bool on)
00077 {
00078 std::fill(data,data+num_bytes,on ? 0xFF : 0x00);
00079 num_on = on ? num_bits : 0;
00080 }
00081
00082 void BitSet::clear()
00083 {
00084 setAll(false);
00085 }
00086
00087 void BitSet::orBitSet(const BitSet & other)
00088 {
00089 Uint32 i = 0;
00090 while (i < num_bits)
00091 {
00092 bool val = get(i) || other.get(i);
00093 set(i,val);
00094 i++;
00095 }
00096 }
00097
00098 bool BitSet::allOn() const
00099 {
00100 return num_on == num_bits;
00101 }
00102
00103 bool BitSet::operator == (const BitSet & bs)
00104 {
00105 if (this->getNumBits() != bs.getNumBits())
00106 return false;
00107
00108 return memcmp(data,bs.data,num_bytes) == 0;
00109 }
00110 }
00111