Disable clipboardsharing in view only mode.
[kdenetwork.git] / kget / core / bitset.h
blob1704fd2c939d066820212c10145862b6c58f5b54
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
23 #include <kget_export.h>
25 /**
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;
35 quint8* data;
36 quint32 num_on;
37 public:
38 /**
39 * Constructor.
40 * @param num_bits The number of bits
42 BitSet(quint32 num_bits = 8);
44 /**
45 * Manually set data.
46 * @param data The data
47 * @param num_bits The number of bits
49 BitSet(const quint8* data,quint32 num_bits);
51 /**
52 * Copy constructor.
53 * @param bs BitSet to copy
54 * @return
56 BitSet(const BitSet & bs);
57 virtual ~BitSet();
59 /// See if the BitSet is null
60 bool isNull() const {return num_bits == 0;}
62 /**
63 * Get the value of a bit, false means 0, true 1.
64 * @param i Index of Bit
66 bool get(quint32 i) const;
68 /**
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
76 void setAll(bool on);
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;}
86 /**
87 * Set all bits to 0
89 void clear();
91 /**
92 * or this BitSet with another.
93 * @param other The other BitSet
95 void orBitSet(const BitSet & other);
97 /**
98 * Assignment operator.
99 * @param bs BitSet to copy
100 * @return *this
102 BitSet & operator = (const BitSet & bs);
104 /// Check if all bit are set to 1
105 bool allOn() const;
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);}
119 static BitSet null;
122 inline bool BitSet::get(quint32 i) const
124 if (i >= num_bits)
125 return false;
127 quint32 byte = i / 8;
128 quint32 bit = i % 8;
129 quint8 b = data[byte] & (0x01 << (7 - bit));
130 return b != 0x00;
133 inline void BitSet::set(quint32 i,bool on)
135 if (i >= num_bits)
136 return;
138 quint32 byte = i / 8;
139 quint32 bit = i % 8;
140 if (on && !get(i))
142 num_on++;
143 data[byte] |= (0x01 << (7 - bit));
145 else if (!on && get(i))
147 num_on--;
148 quint8 b = (0x01 << (7 - bit));
149 data[byte] &= (~b);
153 #endif