• Main Page
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

bitvect.h

Go to the documentation of this file.
00001 /// @addtogroup unifexp
00002 /// @{
00003 
00004 /////////////////////////////////////////////////////////////////////////////
00005 ///
00006 /// @file bitvect.h
00007 ///
00008 /// This file contains the definition of a simple bit vector class.
00009 ///
00010 /// @author Pawel Pilarczyk
00011 ///
00012 /////////////////////////////////////////////////////////////////////////////
00013 
00014 // Copyright (C) 2007 by Pawel Pilarczyk.
00015 //
00016 // This file is part of my research program package.  This is free software;
00017 // you can redistribute it and/or modify it under the terms of the GNU
00018 // General Public License as published by the Free Software Foundation;
00019 // either version 2 of the License, or (at your option) any later version.
00020 //
00021 // This software is distributed in the hope that it will be useful,
00022 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00023 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00024 // GNU General Public License for more details.
00025 //
00026 // You should have received a copy of the GNU General Public License along
00027 // with this software; see the file "license.txt".  If not, write to the
00028 // Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00029 // MA 02111-1307, USA.
00030 
00031 // Started on March 8, 2007. Last revision: August 22, 2007.
00032 
00033 #ifndef _bitvect_h_
00034 #define _bitvect_h_
00035 
00036 namespace unifexp {
00037 
00038 
00039 // --------------------------------------------------
00040 // ------------------- bit vector -------------------
00041 // --------------------------------------------------
00042 
00043 /// This class defines a bit vector of fixed length.
00044 /// The bits are initially zero, and can be marked and unmarked.
00045 /// Additionally, convenient functions are available to search
00046 /// for the first marked/unmarked bit in the vector.
00047 class bitVector
00048 {
00049 public:
00050         /// The constructor of a bit vector of the given size.
00051         bitVector (int length);
00052 
00053         /// The destructor.
00054         ~bitVector ();
00055 
00056         /// Marks the given bit.
00057         void mark (int bit);
00058 
00059         /// Unmarks the given bit.
00060         void unmark (int bit);
00061 
00062         /// Finds the first marked bit starting at the given position.
00063         /// If not found, returns 'length'.
00064         int findMarked (int start, int length) const;
00065 
00066         /// Finds the first unmarked bit starting at the given position.
00067         /// If not found, returns 'length'.
00068         int findUnmarked (int start, int length) const;
00069 
00070 private:
00071         /// The copy constructor is not allowed.
00072         bitVector (const bitVector &b);
00073 
00074         /// The assignment operator is not allowed.
00075         bitVector &operator = (const bitVector &b);
00076 
00077         /// The array of bytes that represents the bitvector.
00078         unsigned char *buf;
00079 
00080         /// Temporarily: The actual size of the buffer.
00081         int bufSize;
00082 
00083 }; /* class bitVector */
00084 
00085 // --------------------------------------------------
00086 
00087 inline bitVector::bitVector (int length)
00088 {
00089         int size = (length + 7) >> 3;
00090         if (!size)
00091                 size = 1;
00092         buf = new unsigned char [size];
00093         for (int i = 0; i < size; ++ i)
00094                 buf [i] = 0;
00095         bufSize = size;
00096         return;
00097 } /* bitVector::bitVector */
00098 
00099 inline bitVector::~bitVector ()
00100 {
00101         delete [] buf;
00102         return;
00103 } /* bitVector::~bitVector */
00104 
00105 inline bitVector::bitVector (const bitVector &)
00106 {
00107         throw "Copy constructor not implemented for bit vectors.";
00108         return;
00109 } /* bitVector::bitVector */
00110 
00111 inline bitVector &bitVector::operator = (const bitVector &)
00112 {
00113         throw "Assignment operator not implemented for bit vectors.";
00114         return *this;
00115 } /* bitVector::operator = */
00116 
00117 inline void bitVector::mark (int bit)
00118 {
00119         int offset = bit >> 3;
00120         if (offset >= bufSize)
00121                 throw "Wrong bit in a bit vector marked.";
00122         buf [offset] |= 1 << (bit & 7);
00123         return;
00124 } /* bitVector::mark */
00125 
00126 inline void bitVector::unmark (int bit)
00127 {
00128         int offset = bit >> 3;
00129         if (offset >= bufSize)
00130                 throw "Wrong bit in a bit vector marked.";
00131         buf [offset] &= ~(1 << (bit & 7));
00132         return;
00133 } /* bitVector::unmark */
00134 
00135 inline int bitVector::findMarked (int first, int length) const
00136 {
00137         if (first >= length)
00138                 return length;
00139         int offset = first >> 3;
00140         int maxoffset = (length + 7) >> 3;
00141         if (offset >= bufSize)
00142                 throw "Wrong first marked bit in a bit vector to search.";
00143         if (maxoffset > bufSize)
00144                 throw "Wrong length of a bit vector to search for marked.";
00145         if (buf [offset])
00146         {
00147                 for (int i = first & 7; i < 8; ++ i)
00148                 {
00149                         if (buf [offset] & (1 << i))
00150                                 return ((offset << 3) + i);
00151                 }
00152         }
00153         ++ offset;
00154         for (;offset < maxoffset; ++ offset)
00155         {
00156                 if (!buf [offset])
00157                         continue;
00158                 for (int i = 0; i < 8; ++ i)
00159                 {
00160                         if (buf [offset] & (1 << i))
00161                                 return ((offset << 3) + i);
00162                 }
00163         }
00164         return length;
00165 } /* bitVector::findMarked */
00166 
00167 inline int bitVector::findUnmarked (int first, int length) const
00168 {
00169 //      if (first >= length)
00170 //              return length;
00171         int offset = first >> 3;
00172         int maxoffset = (length + 7) >> 3;
00173         if (offset >= bufSize)
00174                 throw "Wrong first unmarked bit in a bit vector to search.";
00175         if (maxoffset > bufSize)
00176                 throw "Wrong length of a bit vector to search for unmarked.";
00177         if (buf [offset] != 0xFFu)
00178         {
00179                 for (int i = first & 7; i < 8; ++ i)
00180                 {
00181                         if (!(buf [offset] & (1 << i)))
00182                                 return ((offset << 3) + i);
00183                 }
00184         }
00185         ++ offset;
00186         for (;offset < maxoffset; ++ offset)
00187         {
00188                 if (buf [offset] == 0xFFu)
00189                         continue;
00190                 for (int i = 0; i < 8; ++ i)
00191                 {
00192                         if (!(buf [offset] & (1 << i)))
00193                                 return ((offset << 3) + i);
00194                 }
00195         }
00196         return length;
00197 } /* bitVector::findUnmarked */
00198 
00199 
00200 } // namespace unifexp
00201 
00202 #endif // _bitvect_h_
00203 
00204 /// @}
00205 

Generated on Sun Feb 3 2013 12:40:31 for The Uniform Expansion Software by  doxygen 1.7.2