bitsets.h

Go to the documentation of this file.
00001 
00002 
00003 
00014 
00015 // Copyright (C) 1997-2010 by Pawel Pilarczyk.
00016 //
00017 // This file is part of the Homology Library.  This library is free software;
00018 // you can redistribute it and/or modify it under the terms of the GNU
00019 // General Public License as published by the Free Software Foundation;
00020 // either version 2 of the License, or (at your option) any later version.
00021 //
00022 // This library is distributed in the hope that it will be useful,
00023 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00024 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00025 // GNU General Public License for more details.
00026 //
00027 // You should have received a copy of the GNU General Public License along
00028 // with this software; see the file "license.txt".  If not, write to the
00029 // Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00030 // MA 02111-1307, USA.
00031 
00032 // Started on February 22, 2007. Last revision: June 11, 2010.
00033 
00034 
00035 #ifndef _CHOMP_STRUCT_BITSETS_H_
00036 #define _CHOMP_STRUCT_BITSETS_H_
00037 
00038 #include "chomp/system/config.h"
00039 
00040 #include <iostream>
00041 #include <cstdlib>
00042 
00043 namespace chomp {
00044 namespace homology {
00045 
00046 
00047 // --------------------------------------------------
00048 // ------------------- BitSets ---------------------
00049 // --------------------------------------------------
00050 
00058 class BitSets
00059 {
00060 public:
00064         BitSets (int_t _nsets, int_t _nelem);
00065 
00067         BitSets (const BitSets &s);
00068 
00070         BitSets &operator = (const BitSets &s);
00071 
00073         ~BitSets ();
00074 
00076         void add (int_t n, int_t e);
00077 
00079         void remove (int_t n, int_t e);
00080 
00082         bool check (int_t n, int_t e) const;
00083 
00085         void addset (int_t n, int_t m);
00086 
00089         int_t get (int_t n, int_t start = 0) const;
00090 
00091 private:
00093         int_t nsets;
00094         
00096         int_t nelem;
00097 
00099         int_t nbytes;
00100 
00102         unsigned char *bits;
00103 
00104 }; /* BitSets */
00105 
00106 // --------------------------------------------------
00107 
00108 inline BitSets::BitSets (int_t _nsets, int_t _nelem):
00109         nsets (_nsets), nelem (_nelem), nbytes ((_nelem + 7) >> 3), bits (0)
00110 {
00111         int_t size = nsets * nbytes;
00112         bits = new unsigned char [size];
00113         for (int_t i = 0; i < size; ++ i)
00114                 bits [i] = 0;
00115         return;
00116 } /* BitSets::BitSets */
00117 
00118 inline BitSets::BitSets (const BitSets &s):
00119         nsets (s. nsets), nelem (s. nelem), nbytes (s. nbytes), bits (0)
00120 {
00121         int_t size = nsets * nbytes;
00122         bits = new unsigned char [size];
00123         for (int_t i = 0; i < size; ++ i)
00124                 bits [i] = s. bits [i];
00125         return;
00126 } /* BitSets::BitSets */
00127 
00128 inline BitSets &BitSets::operator = (const BitSets &s)
00129 {
00130         delete [] bits;
00131         nsets = s. nsets;
00132         nelem = s. nelem;
00133         nbytes = s. nbytes;
00134         int_t size = nsets * nbytes;
00135         bits = new unsigned char [size];
00136         for (int_t i = 0; i < size; ++ i)
00137                 bits [i] = s. bits [i];
00138         return *this;
00139 } /* BitSets::operator = */
00140 
00141 inline BitSets::~BitSets ()
00142 {
00143         delete [] bits;
00144         return;
00145 } /* BitSets::~BitSets */
00146 
00147 inline void BitSets::add (int_t n, int_t e)
00148 {
00149 //      sbug << "Add " << e << " to " << n << ".\n";
00150         unsigned char *buf = bits + n * nbytes;
00151         buf [e >> 3] |= (unsigned char) (0x01 << (e & 0x07));
00152         return;
00153 } /* BitSets::add */
00154 
00155 inline void BitSets::remove (int_t n, int_t e)
00156 {
00157         unsigned char *buf = bits + n * nbytes;
00158         buf [e >> 3] &= (unsigned char) ~(0x01 << (e & 0x07));
00159         return;
00160 } /* BitSets::remove */
00161 
00162 inline bool BitSets::check (int_t n, int_t e) const
00163 {
00164         unsigned char *buf = bits + n * nbytes;
00165         return !!(buf [e >> 3] & (0x01 << (e & 0x07)));
00166 } /* BitSets::check */
00167 
00168 inline void BitSets::addset (int_t n, int_t m)
00169 {
00170         unsigned char *nbuf = bits + n * nbytes;
00171         unsigned char *mbuf = bits + m * nbytes;
00172         for (int_t i = 0; i < nbytes; ++ i)
00173                 *(nbuf ++) |= *(mbuf ++);
00174         return;
00175 } /* BitSets::addset */
00176 
00177 inline int_t BitSets::get (int_t n, int_t start) const
00178 {
00179         if (start >= nelem)
00180                 return -1;
00181         unsigned char *buf = bits + n * nbytes;
00182         int_t offset = start >> 3;
00183         int bit = start & 0x07;
00184         if (buf [offset] & (0xFF << bit))
00185         {
00186                 for (; bit < 8; ++ bit)
00187                 {
00188                         if (buf [offset] & (0x01 << bit))
00189                                 return (offset << 3) + bit;
00190                 }
00191                 throw "Wrong bit number in BitSets.";
00192         }
00193         for (++ offset; offset < nbytes; ++ offset)
00194         {
00195                 if (!buf [offset])
00196                         continue;
00197                 for (int bit = 0; bit < 8; ++ bit)
00198                 {
00199                         if (buf [offset] & (0x01 << bit))
00200                                 return (offset << 3) + bit;
00201                 }
00202                 throw "False bit in BitSets.";
00203         }
00204         return -1;
00205 } /* BitSets::get */
00206 
00207 
00208 } // namespace homology
00209 } // namespace chomp
00210 
00211 #endif // _CHOMP_STRUCT_BITSETS_H_
00212 
00214