1 | /* |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
---|
3 | * contributor license agreements. See the NOTICE file distributed with |
---|
4 | * this work for additional information regarding copyright ownership. |
---|
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
---|
6 | * (the "License"); you may not use this file except in compliance with |
---|
7 | * the License. You may obtain a copy of the License at |
---|
8 | * |
---|
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
10 | * |
---|
11 | * Unless required by applicable law or agreed to in writing, software |
---|
12 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
14 | * See the License for the specific language governing permissions and |
---|
15 | * limitations under the License. |
---|
16 | */ |
---|
17 | |
---|
18 | /* |
---|
19 | * $Id: CMStateSet.hpp 568078 2007-08-21 11:43:25Z amassari $ |
---|
20 | */ |
---|
21 | |
---|
22 | // DESCRIPTION: |
---|
23 | // |
---|
24 | // This class is a specialized bitset class for the content model code of |
---|
25 | // the validator. It assumes that its never called with two objects of |
---|
26 | // different bit counts, and that bit sets smaller than 64 bits are far |
---|
27 | // and away the most common. So it can be a lot more optimized than a general |
---|
28 | // purpose utility bitset class |
---|
29 | // |
---|
30 | |
---|
31 | #if !defined(CMSTATESET_HPP) |
---|
32 | #define CMSTATESET_HPP |
---|
33 | |
---|
34 | #include <xercesc/util/ArrayIndexOutOfBoundsException.hpp> |
---|
35 | #include <xercesc/util/RuntimeException.hpp> |
---|
36 | #include <xercesc/util/PlatformUtils.hpp> |
---|
37 | #include <xercesc/framework/MemoryManager.hpp> |
---|
38 | #include <string.h> |
---|
39 | |
---|
40 | XERCES_CPP_NAMESPACE_BEGIN |
---|
41 | |
---|
42 | class CMStateSet : public XMemory |
---|
43 | { |
---|
44 | public : |
---|
45 | // ----------------------------------------------------------------------- |
---|
46 | // Constructors and Destructor |
---|
47 | // ----------------------------------------------------------------------- |
---|
48 | CMStateSet( const unsigned int bitCount |
---|
49 | , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager) : |
---|
50 | |
---|
51 | fBitCount(bitCount) |
---|
52 | , fByteArray(0) |
---|
53 | , fMemoryManager(manager) |
---|
54 | { |
---|
55 | // |
---|
56 | // See if we need to allocate the byte array or whether we can live |
---|
57 | // within the 64 bit high performance scheme. |
---|
58 | // |
---|
59 | if (fBitCount > 64) |
---|
60 | { |
---|
61 | fByteCount = fBitCount / 8; |
---|
62 | if (fBitCount % 8) |
---|
63 | fByteCount++; |
---|
64 | fByteArray = (XMLByte*) fMemoryManager->allocate(fByteCount*sizeof(XMLByte)); //new XMLByte[fByteCount]; |
---|
65 | } |
---|
66 | |
---|
67 | // Init all the bits to zero |
---|
68 | zeroBits(); |
---|
69 | } |
---|
70 | |
---|
71 | |
---|
72 | /* |
---|
73 | * This method with the 'for' statement (commented out) cannot be made inline |
---|
74 | * because the antiquated CC (CFront) compiler under HPUX 10.20 does not allow |
---|
75 | * the 'for' statement inside any inline method. Unfortunately, |
---|
76 | * we have to support it. So instead, we use memcpy(). |
---|
77 | */ |
---|
78 | |
---|
79 | CMStateSet(const CMStateSet& toCopy) : |
---|
80 | XMemory(toCopy) |
---|
81 | , fBitCount(toCopy.fBitCount) |
---|
82 | , fByteArray(0) |
---|
83 | , fMemoryManager(toCopy.fMemoryManager) |
---|
84 | { |
---|
85 | // |
---|
86 | // See if we need to allocate the byte array or whether we can live |
---|
87 | // within the 64 bit high performance scheme. |
---|
88 | // |
---|
89 | if (fBitCount > 64) |
---|
90 | { |
---|
91 | fByteCount = fBitCount / 8; |
---|
92 | if (fBitCount % 8) |
---|
93 | fByteCount++; |
---|
94 | fByteArray = (XMLByte*) fMemoryManager->allocate(fByteCount*sizeof(XMLByte)); //new XMLByte[fByteCount]; |
---|
95 | |
---|
96 | memcpy((void *) fByteArray, |
---|
97 | (const void *) toCopy.fByteArray, |
---|
98 | fByteCount * sizeof(XMLByte)); |
---|
99 | |
---|
100 | // for (unsigned int index = 0; index < fByteCount; index++) |
---|
101 | // fByteArray[index] = toCopy.fByteArray[index]; |
---|
102 | } |
---|
103 | else |
---|
104 | { |
---|
105 | fBits1 = toCopy.fBits1; |
---|
106 | fBits2 = toCopy.fBits2; |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | ~CMStateSet() |
---|
111 | { |
---|
112 | if (fByteArray) |
---|
113 | fMemoryManager->deallocate(fByteArray); //delete [] fByteArray; |
---|
114 | } |
---|
115 | |
---|
116 | |
---|
117 | // ----------------------------------------------------------------------- |
---|
118 | // Set manipulation methods |
---|
119 | // ----------------------------------------------------------------------- |
---|
120 | void operator&=(const CMStateSet& setToAnd) |
---|
121 | { |
---|
122 | if (fBitCount < 65) |
---|
123 | { |
---|
124 | fBits1 &= setToAnd.fBits1; |
---|
125 | fBits2 &= setToAnd.fBits2; |
---|
126 | } |
---|
127 | else |
---|
128 | { |
---|
129 | for (unsigned int index = 0; index < fByteCount; index++) |
---|
130 | fByteArray[index] &= setToAnd.fByteArray[index]; |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | void operator|=(const CMStateSet& setToOr) |
---|
135 | { |
---|
136 | if (fBitCount < 65) |
---|
137 | { |
---|
138 | fBits1 |= setToOr.fBits1; |
---|
139 | fBits2 |= setToOr.fBits2; |
---|
140 | } |
---|
141 | else |
---|
142 | { |
---|
143 | for (unsigned int index = 0; index < fByteCount; index++) |
---|
144 | fByteArray[index] |= setToOr.fByteArray[index]; |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | bool operator==(const CMStateSet& setToCompare) const |
---|
149 | { |
---|
150 | if (fBitCount != setToCompare.fBitCount) |
---|
151 | return false; |
---|
152 | |
---|
153 | if (fBitCount < 65) |
---|
154 | { |
---|
155 | return ((fBits1 == setToCompare.fBits1) |
---|
156 | && (fBits2 == setToCompare.fBits2)); |
---|
157 | } |
---|
158 | |
---|
159 | for (unsigned int index = 0; index < fByteCount; index++) |
---|
160 | { |
---|
161 | if (fByteArray[index] != setToCompare.fByteArray[index]) |
---|
162 | return false; |
---|
163 | } |
---|
164 | return true; |
---|
165 | } |
---|
166 | |
---|
167 | CMStateSet& operator=(const CMStateSet& srcSet) |
---|
168 | { |
---|
169 | if (this == &srcSet) |
---|
170 | return *this; |
---|
171 | |
---|
172 | // They have to be the same size |
---|
173 | if (fBitCount != srcSet.fBitCount) |
---|
174 | ThrowXMLwithMemMgr(RuntimeException, XMLExcepts::Bitset_NotEqualSize, fMemoryManager); |
---|
175 | |
---|
176 | if (fBitCount < 65) |
---|
177 | { |
---|
178 | fBits1 = srcSet.fBits1; |
---|
179 | fBits2 = srcSet.fBits2; |
---|
180 | } |
---|
181 | else |
---|
182 | { |
---|
183 | for (unsigned int index = 0; index < fByteCount; index++) |
---|
184 | fByteArray[index] = srcSet.fByteArray[index]; |
---|
185 | } |
---|
186 | return *this; |
---|
187 | } |
---|
188 | |
---|
189 | |
---|
190 | bool getBit(const unsigned int bitToGet) const |
---|
191 | { |
---|
192 | if (bitToGet >= fBitCount) |
---|
193 | ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Bitset_BadIndex, fMemoryManager); |
---|
194 | |
---|
195 | if (fBitCount < 65) |
---|
196 | { |
---|
197 | unsigned int mask = (0x1UL << (bitToGet % 32)); |
---|
198 | if (bitToGet < 32) |
---|
199 | return ((fBits1 & mask) != 0); |
---|
200 | else |
---|
201 | return ((fBits2 & mask) != 0); |
---|
202 | } |
---|
203 | |
---|
204 | // Create the mask and byte values |
---|
205 | const XMLByte mask1 = XMLByte(0x1 << (bitToGet % 8)); |
---|
206 | const unsigned int byteOfs = bitToGet >> 3; |
---|
207 | |
---|
208 | // And access the right bit and byte |
---|
209 | return ((fByteArray[byteOfs] & mask1) != 0); |
---|
210 | } |
---|
211 | |
---|
212 | bool isEmpty() const |
---|
213 | { |
---|
214 | if (fBitCount < 65) |
---|
215 | return ((fBits1 == 0) && (fBits2 == 0)); |
---|
216 | |
---|
217 | for (unsigned int index = 0; index < fByteCount; index++) |
---|
218 | { |
---|
219 | if (fByteArray[index] != 0) |
---|
220 | return false; |
---|
221 | } |
---|
222 | return true; |
---|
223 | } |
---|
224 | |
---|
225 | void setBit(const unsigned int bitToSet) |
---|
226 | { |
---|
227 | if (bitToSet >= fBitCount) |
---|
228 | ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Bitset_BadIndex, fMemoryManager); |
---|
229 | |
---|
230 | if (fBitCount < 65) |
---|
231 | { |
---|
232 | const unsigned int mask = (0x1UL << (bitToSet % 32)); |
---|
233 | if (bitToSet < 32) |
---|
234 | { |
---|
235 | fBits1 &= ~mask; |
---|
236 | fBits1 |= mask; |
---|
237 | } |
---|
238 | else |
---|
239 | { |
---|
240 | fBits2 &= ~mask; |
---|
241 | fBits2 |= mask; |
---|
242 | } |
---|
243 | } |
---|
244 | else |
---|
245 | { |
---|
246 | // Create the mask and byte values |
---|
247 | const XMLByte mask1 = XMLByte(0x1 << (bitToSet % 8)); |
---|
248 | const unsigned int byteOfs = bitToSet >> 3; |
---|
249 | |
---|
250 | // And access the right bit and byte |
---|
251 | fByteArray[byteOfs] &= ~mask1; |
---|
252 | fByteArray[byteOfs] |= mask1; |
---|
253 | } |
---|
254 | } |
---|
255 | |
---|
256 | void zeroBits() |
---|
257 | { |
---|
258 | if (fBitCount < 65) |
---|
259 | { |
---|
260 | fBits1 = 0; |
---|
261 | fBits2 = 0; |
---|
262 | } |
---|
263 | else |
---|
264 | { |
---|
265 | for (unsigned int index = 0; index < fByteCount; index++) |
---|
266 | fByteArray[index] = 0; |
---|
267 | } |
---|
268 | } |
---|
269 | |
---|
270 | int hashCode() const |
---|
271 | { |
---|
272 | if (fBitCount < 65) |
---|
273 | { |
---|
274 | return fBits1+ fBits2 * 31; |
---|
275 | } |
---|
276 | else |
---|
277 | { |
---|
278 | int hash = 0; |
---|
279 | for (int index = fByteCount - 1; index >= 0; index--) |
---|
280 | hash = fByteArray[index] + hash * 31; |
---|
281 | return hash; |
---|
282 | } |
---|
283 | |
---|
284 | } |
---|
285 | |
---|
286 | private : |
---|
287 | // ----------------------------------------------------------------------- |
---|
288 | // Unimplemented constructors and operators |
---|
289 | // ----------------------------------------------------------------------- |
---|
290 | CMStateSet(); |
---|
291 | |
---|
292 | |
---|
293 | // ----------------------------------------------------------------------- |
---|
294 | // Private data members |
---|
295 | // |
---|
296 | // fBitCount |
---|
297 | // The count of bits that the outside world wants to support, |
---|
298 | // so its the max bit index plus one. |
---|
299 | // |
---|
300 | // fByteCount |
---|
301 | // If the bit count is > 64, then we use the fByteArray member to |
---|
302 | // store the bits, and this indicates its size in bytes. Otherwise |
---|
303 | // its value is meaningless and unset. |
---|
304 | // |
---|
305 | // fBits1 |
---|
306 | // fBits2 |
---|
307 | // When the bit count is <= 64 (very common), these hold the bits. |
---|
308 | // Otherwise, the fByteArray member holds htem. |
---|
309 | // |
---|
310 | // fByteArray |
---|
311 | // The array of bytes used when the bit count is > 64. It is |
---|
312 | // allocated as required. |
---|
313 | // ----------------------------------------------------------------------- |
---|
314 | unsigned int fBitCount; |
---|
315 | unsigned int fByteCount; |
---|
316 | unsigned int fBits1; |
---|
317 | unsigned int fBits2; |
---|
318 | XMLByte* fByteArray; |
---|
319 | MemoryManager* fMemoryManager; |
---|
320 | }; |
---|
321 | |
---|
322 | XERCES_CPP_NAMESPACE_END |
---|
323 | |
---|
324 | #endif |
---|