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

kget

bitset.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005 by Joris Guisson                                   *
00003  *   joris.guisson@gmail.com                                               *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
00019  ***************************************************************************/
00020 #include "bitset.h"
00021 #include <algorithm>
00022 #include <string.h>
00023 
00024 BitSet BitSet::null;
00025 
00026 BitSet::BitSet(quint32 num_bits) : num_bits(num_bits),data(0)
00027 {
00028     num_bytes = (num_bits / 8) + ((num_bits % 8 > 0) ? 1 : 0);
00029     data = new quint8[num_bytes];
00030     std::fill(data,data+num_bytes,0x00);
00031     num_on = 0;
00032 }
00033 
00034 BitSet::BitSet(const quint8* d,quint32 num_bits)  : num_bits(num_bits),data(0)
00035 {
00036     num_bytes = (num_bits / 8) + ((num_bits % 8 > 0) ? 1 : 0);
00037     data = new quint8[num_bytes];
00038     memcpy(data,d,num_bytes);
00039     num_on = 0;
00040     quint32 i = 0;
00041     while (i < num_bits)
00042     {
00043         if (get(i))
00044             num_on++;
00045         i++;
00046     }
00047 }
00048 
00049 BitSet::BitSet(const BitSet & bs) : num_bits(bs.num_bits),num_bytes(bs.num_bytes),data(0),num_on(bs.num_on)
00050 {
00051     data = new quint8[num_bytes];
00052     std::copy(bs.data,bs.data+num_bytes,data);
00053 }
00054         
00055 BitSet::~BitSet()
00056 {
00057     delete [] data;
00058 }
00059 
00060 
00061 
00062 BitSet & BitSet::operator = (const BitSet & bs)
00063 {
00064     if (data)
00065         delete [] data;
00066     num_bytes = bs.num_bytes;
00067     num_bits = bs.num_bits;
00068     data = new quint8[num_bytes];
00069     std::copy(bs.data,bs.data+num_bytes,data);
00070     num_on = bs.num_on;
00071     return *this;
00072 }
00073 
00074 void BitSet::setAll(bool on)
00075 {
00076     std::fill(data,data+num_bytes,on ? 0xFF : 0x00);
00077     num_on = on ? num_bits : 0;
00078 }
00079 
00080 void BitSet::clear()
00081 {
00082     setAll(false);
00083 }
00084 
00085 void BitSet::orBitSet(const BitSet & other)
00086 {
00087     quint32 i = 0;
00088     while (i < num_bits)
00089     {
00090         bool val = get(i) || other.get(i);
00091         set(i,val);
00092         i++;
00093     }
00094 }
00095 
00096 bool BitSet::allOn() const
00097 {
00098     return num_on == num_bits;
00099 }
00100 
00101 bool BitSet::operator == (const BitSet & bs)
00102 {
00103     if (this->getNumBits() != bs.getNumBits())
00104         return false;
00105 
00106     return memcmp(data,bs.data,num_bytes) == 0;
00107 }
00108 
00109 

kget

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

kdenetwork

Skip menu "kdenetwork"
  • kget
  • kopete
  •   kopete
  •   libkopete
  •       libpapillon
  • krfb
Generated for kdenetwork by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal