kget
bitset.h
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 #ifndef BTBITSET_H
00021 #define BTBITSET_H
00022
00023 #include <btcore_export.h>
00024 #include "constants.h"
00025
00026 namespace bt
00027 {
00028
00036 class BTCORE_EXPORT BitSet
00037 {
00038 Uint32 num_bits,num_bytes;
00039 Uint8* data;
00040 Uint32 num_on;
00041 public:
00046 BitSet(Uint32 num_bits = 8);
00047
00053 BitSet(const Uint8* data,Uint32 num_bits);
00054
00060 BitSet(const BitSet & bs);
00061 virtual ~BitSet();
00062
00064 bool isNull() const {return num_bits == 0;}
00065
00070 bool get(Uint32 i) const;
00071
00077 void set(Uint32 i,bool on);
00078
00080 void setAll(bool on);
00081
00082 Uint32 getNumBytes() const {return num_bytes;}
00083 Uint32 getNumBits() const {return num_bits;}
00084 const Uint8* getData() const {return data;}
00085 Uint8* getData() {return data;}
00086
00088 Uint32 numOnBits() const {return num_on;}
00089
00093 void clear();
00094
00099 void orBitSet(const BitSet & other);
00100
00106 BitSet & operator = (const BitSet & bs);
00107
00109 bool allOn() const;
00110
00116 bool operator == (const BitSet & bs);
00117
00121 bool operator != (const BitSet & bs) {return ! operator == (bs);}
00122
00123 static BitSet null;
00124 };
00125
00126 inline bool BitSet::get(Uint32 i) const
00127 {
00128 if (i >= num_bits)
00129 return false;
00130
00131 Uint32 byte = i / 8;
00132 Uint32 bit = i % 8;
00133 Uint8 b = data[byte] & (0x01 << (7 - bit));
00134 return b != 0x00;
00135 }
00136
00137 inline void BitSet::set(Uint32 i,bool on)
00138 {
00139 if (i >= num_bits)
00140 return;
00141
00142 Uint32 byte = i / 8;
00143 Uint32 bit = i % 8;
00144 if (on && !get(i))
00145 {
00146 num_on++;
00147 data[byte] |= (0x01 << (7 - bit));
00148 }
00149 else if (!on && get(i))
00150 {
00151 num_on--;
00152 Uint8 b = (0x01 << (7 - bit));
00153 data[byte] &= (~b);
00154 }
00155 }
00156 }
00157
00158 #endif