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 BitSet BitSet::null;
00025
00026 BitSet::BitSet(quint32 num_bits) : num_bits(num_bits),data(0)
00027 {
00028 num_bytes = (num_bits / 8) + ((num_bits % 8 > 0) ? 1 : 0);
00029 data = new quint8[num_bytes];
00030 std::fill(data,data+num_bytes,0x00);
00031 num_on = 0;
00032 }
00033
00034 BitSet::BitSet(const quint8* d,quint32 num_bits) : num_bits(num_bits),data(0)
00035 {
00036 num_bytes = (num_bits / 8) + ((num_bits % 8 > 0) ? 1 : 0);
00037 data = new quint8[num_bytes];
00038 memcpy(data,d,num_bytes);
00039 num_on = 0;
00040 quint32 i = 0;
00041 while (i < num_bits)
00042 {
00043 if (get(i))
00044 num_on++;
00045 i++;
00046 }
00047 }
00048
00049 BitSet::BitSet(const BitSet & bs) : num_bits(bs.num_bits),num_bytes(bs.num_bytes),data(0),num_on(bs.num_on)
00050 {
00051 data = new quint8[num_bytes];
00052 std::copy(bs.data,bs.data+num_bytes,data);
00053 }
00054
00055 BitSet::~BitSet()
00056 {
00057 delete [] data;
00058 }
00059
00060
00061
00062 BitSet & BitSet::operator = (const BitSet & bs)
00063 {
00064 if (data)
00065 delete [] data;
00066 num_bytes = bs.num_bytes;
00067 num_bits = bs.num_bits;
00068 data = new quint8[num_bytes];
00069 std::copy(bs.data,bs.data+num_bytes,data);
00070 num_on = bs.num_on;
00071 return *this;
00072 }
00073
00074 void BitSet::setAll(bool on)
00075 {
00076 std::fill(data,data+num_bytes,on ? 0xFF : 0x00);
00077 num_on = on ? num_bits : 0;
00078 }
00079
00080 void BitSet::clear()
00081 {
00082 setAll(false);
00083 }
00084
00085 void BitSet::orBitSet(const BitSet & other)
00086 {
00087 quint32 i = 0;
00088 while (i < num_bits)
00089 {
00090 bool val = get(i) || other.get(i);
00091 set(i,val);
00092 i++;
00093 }
00094 }
00095
00096 bool BitSet::allOn() const
00097 {
00098 return num_on == num_bits;
00099 }
00100
00101 bool BitSet::operator == (const BitSet & bs)
00102 {
00103 if (this->getNumBits() != bs.getNumBits())
00104 return false;
00105
00106 return memcmp(data,bs.data,num_bytes) == 0;
00107 }
00108
00109