• 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 namespace bt
00025 {
00026     BitSet BitSet::null;
00027 
00028     BitSet::BitSet(Uint32 num_bits) : num_bits(num_bits),data(0)
00029     {
00030         num_bytes = (num_bits / 8) + ((num_bits % 8 > 0) ? 1 : 0);
00031         data = new Uint8[num_bytes];
00032         std::fill(data,data+num_bytes,0x00);
00033         num_on = 0;
00034     }
00035 
00036     BitSet::BitSet(const Uint8* d,Uint32 num_bits)  : num_bits(num_bits),data(0)
00037     {
00038         num_bytes = (num_bits / 8) + ((num_bits % 8 > 0) ? 1 : 0);
00039         data = new Uint8[num_bytes];
00040         memcpy(data,d,num_bytes);
00041         num_on = 0;
00042         Uint32 i = 0;
00043         while (i < num_bits)
00044         {
00045             if (get(i))
00046                 num_on++;
00047             i++;
00048         }
00049     }
00050     
00051     BitSet::BitSet(const BitSet & bs) : num_bits(bs.num_bits),num_bytes(bs.num_bytes),data(0),num_on(bs.num_on)
00052     {
00053         data = new Uint8[num_bytes];
00054         std::copy(bs.data,bs.data+num_bytes,data);
00055     }
00056             
00057     BitSet::~BitSet()
00058     {
00059         delete [] data;
00060     }
00061 
00062     
00063 
00064     BitSet & BitSet::operator = (const BitSet & bs)
00065     {
00066         if (data)
00067             delete [] data;
00068         num_bytes = bs.num_bytes;
00069         num_bits = bs.num_bits;
00070         data = new Uint8[num_bytes];
00071         std::copy(bs.data,bs.data+num_bytes,data);
00072         num_on = bs.num_on;
00073         return *this;
00074     }
00075     
00076     void BitSet::setAll(bool on)
00077     {
00078         std::fill(data,data+num_bytes,on ? 0xFF : 0x00);
00079         num_on = on ? num_bits : 0;
00080     }
00081 
00082     void BitSet::clear()
00083     {
00084         setAll(false);
00085     }
00086 
00087     void BitSet::orBitSet(const BitSet & other)
00088     {
00089         Uint32 i = 0;
00090         while (i < num_bits)
00091         {
00092             bool val = get(i) || other.get(i);
00093             set(i,val);
00094             i++;
00095         }
00096     }
00097 
00098     bool BitSet::allOn() const
00099     {
00100         return num_on == num_bits;
00101     }
00102 
00103     bool BitSet::operator == (const BitSet & bs)
00104     {
00105         if (this->getNumBits() != bs.getNumBits())
00106             return false;
00107 
00108         return memcmp(data,bs.data,num_bytes) == 0;
00109     }
00110 }
00111 

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