New feature for KGet: Webinterface.
[kdenetwork.git] / kget / core / bitset.cpp
blob826a77bd253da9d551c0e735caa301a14bbc5073
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 #include "bitset.h"
21 #include <algorithm>
22 #include <string.h>
24 BitSet BitSet::null;
26 BitSet::BitSet(quint32 num_bits) : num_bits(num_bits),data(0)
28 num_bytes = (num_bits / 8) + ((num_bits % 8 > 0) ? 1 : 0);
29 data = new quint8[num_bytes];
30 std::fill(data,data+num_bytes,0x00);
31 num_on = 0;
34 BitSet::BitSet(const quint8* d,quint32 num_bits) : num_bits(num_bits),data(0)
36 num_bytes = (num_bits / 8) + ((num_bits % 8 > 0) ? 1 : 0);
37 data = new quint8[num_bytes];
38 memcpy(data,d,num_bytes);
39 num_on = 0;
40 quint32 i = 0;
41 while (i < num_bits)
43 if (get(i))
44 num_on++;
45 i++;
49 BitSet::BitSet(const BitSet & bs) : num_bits(bs.num_bits),num_bytes(bs.num_bytes),data(0),num_on(bs.num_on)
51 data = new quint8[num_bytes];
52 std::copy(bs.data,bs.data+num_bytes,data);
55 BitSet::~BitSet()
57 delete [] data;
62 BitSet & BitSet::operator = (const BitSet & bs)
64 if (data)
65 delete [] data;
66 num_bytes = bs.num_bytes;
67 num_bits = bs.num_bits;
68 data = new quint8[num_bytes];
69 std::copy(bs.data,bs.data+num_bytes,data);
70 num_on = bs.num_on;
71 return *this;
74 void BitSet::setAll(bool on)
76 std::fill(data,data+num_bytes,on ? 0xFF : 0x00);
77 num_on = on ? num_bits : 0;
80 void BitSet::clear()
82 setAll(false);
85 void BitSet::orBitSet(const BitSet & other)
87 quint32 i = 0;
88 while (i < num_bits)
90 bool val = get(i) || other.get(i);
91 set(i,val);
92 i++;
96 bool BitSet::allOn() const
98 return num_on == num_bits;
101 bool BitSet::operator == (const BitSet & bs)
103 if (this->getNumBits() != bs.getNumBits())
104 return false;
106 return memcmp(data,bs.data,num_bytes) == 0;