Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifndef _bitvect_h_
00034 #define _bitvect_h_
00035
00036 namespace unifexp {
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 class bitVector
00048 {
00049 public:
00050
00051 bitVector (int length);
00052
00053
00054 ~bitVector ();
00055
00056
00057 void mark (int bit);
00058
00059
00060 void unmark (int bit);
00061
00062
00063
00064 int findMarked (int start, int length) const;
00065
00066
00067
00068 int findUnmarked (int start, int length) const;
00069
00070 private:
00071
00072 bitVector (const bitVector &b);
00073
00074
00075 bitVector &operator = (const bitVector &b);
00076
00077
00078 unsigned char *buf;
00079
00080
00081 int bufSize;
00082
00083 };
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 }
00098
00099 inline bitVector::~bitVector ()
00100 {
00101 delete [] buf;
00102 return;
00103 }
00104
00105 inline bitVector::bitVector (const bitVector &)
00106 {
00107 throw "Copy constructor not implemented for bit vectors.";
00108 return;
00109 }
00110
00111 inline bitVector &bitVector::operator = (const bitVector &)
00112 {
00113 throw "Assignment operator not implemented for bit vectors.";
00114 return *this;
00115 }
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 }
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 }
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 }
00166
00167 inline int bitVector::findUnmarked (int first, int length) const
00168 {
00169
00170
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 }
00198
00199
00200 }
00201
00202 #endif // _bitvect_h_
00203
00204
00205