• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdenetwork API Reference
  • KDE Home
  • Contact Us
 

kget

  • sources
  • kde-4.12
  • kdenetwork
  • kget
  • core
bitset.cpp
Go to the documentation of this file.
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>
23 
24 BitSet BitSet::null;
25 
26 BitSet::BitSet(quint32 num_bits) : num_bits(num_bits),data(0)
27 {
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;
32 }
33 
34 BitSet::BitSet(const quint8* d,quint32 num_bits) : num_bits(num_bits),data(0)
35 {
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)
42  {
43  if (get(i))
44  num_on++;
45  i++;
46  }
47 }
48 
49 BitSet::BitSet(const BitSet & bs) : num_bits(bs.num_bits),num_bytes(bs.num_bytes),data(0),num_on(bs.num_on)
50 {
51  data = new quint8[num_bytes];
52  std::copy(bs.data,bs.data+num_bytes,data);
53 }
54 
55 BitSet::~BitSet()
56 {
57  delete [] data;
58 }
59 
60 
61 
62 BitSet & BitSet::operator = (const BitSet & bs)
63 {
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;
72 }
73 
74 void BitSet::setAll(bool on)
75 {
76  std::fill(data,data+num_bytes,on ? 0xFF : 0x00);
77  num_on = on ? num_bits : 0;
78 }
79 
80 void BitSet::getContinuousRange(qint32 *start, qint32 *end, bool on)
81 {
82  *start = -1;
83  *end = -1;
84 
85  const bool nothingFound = on ? allOff() : allOn();
86  if (nothingFound) {
87  return;
88  }
89 
90  const bool everythingMatches = on ? allOn() : allOff();
91  if (everythingMatches) {
92  *start = 0;
93  *end = num_bits -1;
94  return;
95  }
96 
97  for (quint32 i = 0; i < num_bits; ++i) {
98  if (get(i) == on) {
99  if (*start == -1) {
100  *start = i;
101  }
102  *end = i;
103  } else {
104  if (*start != -1) {
105  return;
106  }
107  }
108  }
109 }
110 
111 void BitSet::clear()
112 {
113  setAll(false);
114 }
115 
116 void BitSet::orBitSet(const BitSet & other)
117 {
118  quint32 i = 0;
119  while (i < num_bits)
120  {
121  bool val = get(i) || other.get(i);
122  set(i,val);
123  i++;
124  }
125 }
126 
127 bool BitSet::allOn() const
128 {
129  return num_on == num_bits;
130 }
131 
132 bool BitSet::allOff() const
133 {
134  return !num_on;
135 }
136 
137 bool BitSet::operator == (const BitSet & bs)
138 {
139  if (this->getNumBits() != bs.getNumBits())
140  return false;
141 
142  return memcmp(data,bs.data,num_bytes) == 0;
143 }
144 
145 
BitSet::get
bool get(quint32 i) const
Get the value of a bit, false means 0, true 1.
Definition: bitset.h:139
BitSet::set
void set(quint32 i, bool on)
Set the value of a bit, false means 0, true 1.
Definition: bitset.h:150
BitSet::getContinuousRange
void getContinuousRange(qint32 *start, qint32 *end, bool on)
Finds a continous rang of bits that on/off.
Definition: bitset.cpp:80
BitSet::allOn
bool allOn() const
Check if all bit are set to 1.
Definition: bitset.cpp:127
BitSet::BitSet
BitSet(quint32 num_bits=8)
Constructor.
Definition: bitset.cpp:26
BitSet::setAll
void setAll(bool on)
Set all bits on or off.
Definition: bitset.cpp:74
BitSet::orBitSet
void orBitSet(const BitSet &other)
or this BitSet with another.
Definition: bitset.cpp:116
BitSet::clear
void clear()
Set all bits to 0.
Definition: bitset.cpp:111
BitSet
Simple implementation of a BitSet.
Definition: bitset.h:32
BitSet::null
static BitSet null
Definition: bitset.h:136
BitSet::allOff
bool allOff() const
Definition: bitset.cpp:132
bitset.h
BitSet::getNumBits
quint32 getNumBits() const
Definition: bitset.h:87
BitSet::operator==
bool operator==(const BitSet &bs)
Check for equality of bitsets.
Definition: bitset.cpp:137
BitSet::~BitSet
virtual ~BitSet()
Definition: bitset.cpp:55
BitSet::operator=
BitSet & operator=(const BitSet &bs)
Assignment operator.
Definition: bitset.cpp:62
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:53:17 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kget

Skip menu "kget"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdenetwork API Reference

Skip menu "kdenetwork API Reference"
  • kget
  • kopete
  •   kopete
  •   libkopete
  • krdc
  • krfb

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal