libktorrent  2.1.1
bitset.h
1 /***************************************************************************
2  * Copyright (C) 2005 by Joris Guisson *
3  * joris.guisson@gmail.com *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19  ***************************************************************************/
20 #ifndef BTBITSET_H
21 #define BTBITSET_H
22 
23 #include <ktorrent_export.h>
24 #include "constants.h"
25 
26 namespace bt
27 {
28 
36  class KTORRENT_EXPORT BitSet
37  {
38  Uint32 num_bits,num_bytes;
39  Uint8* data;
40  Uint32 num_on;
41  public:
46  BitSet(Uint32 num_bits = 8);
47 
53  BitSet(const Uint8* data,Uint32 num_bits);
54 
60  BitSet(const BitSet & bs);
61  virtual ~BitSet();
62 
64  bool isNull() const {return num_bits == 0;}
65 
70  bool get(Uint32 i) const;
71 
77  void set(Uint32 i,bool on);
78 
80  void setAll(bool on);
81 
82  Uint32 getNumBytes() const {return num_bytes;}
83  Uint32 getNumBits() const {return num_bits;}
84  const Uint8* getData() const {return data;}
85  Uint8* getData() {return data;}
86 
88  Uint32 numOnBits() const {return num_on;}
89 
93  void clear();
94 
98  void invert();
99 
104  void orBitSet(const BitSet & other);
105 
110  void andBitSet(const BitSet & other);
111 
116  bool includesBitSet(const BitSet & other);
117 
123  BitSet & operator = (const BitSet & bs);
124 
130  BitSet & operator -= (const BitSet & bs);
131 
137  BitSet operator - (const BitSet & bs) const;
138 
140  bool allOn() const;
141 
147  bool operator == (const BitSet & bs) const;
148 
152  bool operator != (const BitSet & bs) const {return ! operator == (bs);}
153 
157  void updateNumOnBits();
158 
159  static BitSet null;
160  };
161 
162  inline bool BitSet::get(Uint32 i) const
163  {
164  if (i >= num_bits)
165  return false;
166 
167  Uint32 byte = i / 8;
168  Uint32 bit = i % 8;
169  Uint8 b = data[byte] & (0x01 << (7 - bit));
170  return b != 0x00;
171  }
172 
173  inline void BitSet::set(Uint32 i,bool on)
174  {
175  if (i >= num_bits)
176  return;
177 
178  Uint32 byte = i / 8;
179  Uint32 bit = i % 8;
180  bool wasOn = get(i);
181  if (on && !wasOn)
182  {
183  num_on++;
184  data[byte] |= (0x01 << (7 - bit));
185  }
186  else if (!on && wasOn)
187  {
188  num_on--;
189  Uint8 b = (0x01 << (7 - bit));
190  data[byte] &= (~b);
191  }
192  }
193 }
194 
195 #endif
bt::BitSet
Simple implementation of a BitSet.
Definition: bitset.h:55
bt::BitSet::get
bool get(Uint32 i) const
Definition: bitset.h:180
bt::BitSet::set
void set(Uint32 i, bool on)
Definition: bitset.h:191