1 /***************************************************************************
2 * Copyright (C) 2005 by Joris Guisson *
3 * joris.guisson@gmail.com *
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. *
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. *
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 ***************************************************************************/
23 #include <kget_export.h>
26 * @author Joris Guisson
27 * @brief Simple implementation of a BitSet
29 * Simple implementation of a BitSet, can only turn on and off bits.
30 * BitSet's are used to indicate which chunks we have or not.
32 class KGET_EXPORT BitSet
34 quint32 num_bits
,num_bytes
;
40 * @param num_bits The number of bits
42 BitSet(quint32 num_bits
= 8);
46 * @param data The data
47 * @param num_bits The number of bits
49 BitSet(const quint8
* data
,quint32 num_bits
);
53 * @param bs BitSet to copy
56 BitSet(const BitSet
& bs
);
59 /// See if the BitSet is null
60 bool isNull() const {return num_bits
== 0;}
63 * Get the value of a bit, false means 0, true 1.
64 * @param i Index of Bit
66 bool get(quint32 i
) const;
69 * Set the value of a bit, false means 0, true 1.
70 * @param i Index of Bit
71 * @param on False means 0, true 1
73 void set(quint32 i
,bool on
);
75 /// Set all bits on or off
78 quint32
getNumBytes() const {return num_bytes
;}
79 quint32
getNumBits() const {return num_bits
;}
80 const quint8
* getData() const {return data
;}
81 quint8
* getData() {return data
;}
83 /// Get the number of on bits
84 quint32
numOnBits() const {return num_on
;}
92 * or this BitSet with another.
93 * @param other The other BitSet
95 void orBitSet(const BitSet
& other
);
98 * Assignment operator.
99 * @param bs BitSet to copy
102 BitSet
& operator = (const BitSet
& bs
);
104 /// Check if all bit are set to 1
108 * Check for equality of bitsets
109 * @param bs BitSet to compare
110 * @return true if equal
112 bool operator == (const BitSet
& bs
);
115 * Opposite of operator ==
117 bool operator != (const BitSet
& bs
) {return ! operator == (bs
);}
122 inline bool BitSet::get(quint32 i
) const
127 quint32 byte
= i
/ 8;
129 quint8 b
= data
[byte
] & (0x01 << (7 - bit
));
133 inline void BitSet::set(quint32 i
,bool on
)
138 quint32 byte
= i
/ 8;
143 data
[byte
] |= (0x01 << (7 - bit
));
145 else if (!on
&& get(i
))
148 quint8 b
= (0x01 << (7 - bit
));