1 | /*
|
---|
2 | * Copyright 1999-2001,2004 The Apache Software Foundation.
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | */
|
---|
16 |
|
---|
17 | /*
|
---|
18 | * $Log: CMStateSet.hpp,v $
|
---|
19 | * Revision 1.7 2004/09/08 13:56:51 peiyongz
|
---|
20 | * Apache License Version 2.0
|
---|
21 | *
|
---|
22 | * Revision 1.6 2003/12/17 00:18:38 cargilld
|
---|
23 | * Update to memory management so that the static memory manager (one used to call Initialize) is only for static data.
|
---|
24 | *
|
---|
25 | * Revision 1.5 2003/05/16 21:43:20 knoaman
|
---|
26 | * Memory manager implementation: Modify constructors to pass in the memory manager.
|
---|
27 | *
|
---|
28 | * Revision 1.4 2003/05/15 18:48:27 knoaman
|
---|
29 | * Partial implementation of the configurable memory manager.
|
---|
30 | *
|
---|
31 | * Revision 1.3 2002/11/04 14:54:58 tng
|
---|
32 | * C++ Namespace Support.
|
---|
33 | *
|
---|
34 | * Revision 1.2 2002/07/16 12:50:08 tng
|
---|
35 | * [Bug 10651] CMStateSet.hpp includes both memory.h and string.h.
|
---|
36 | *
|
---|
37 | * Revision 1.1.1.1 2002/02/01 22:22:38 peiyongz
|
---|
38 | * sane_include
|
---|
39 | *
|
---|
40 | * Revision 1.5 2001/08/16 21:51:43 peiyongz
|
---|
41 | * hashCode() added
|
---|
42 | *
|
---|
43 | * Revision 1.4 2001/05/11 13:27:17 tng
|
---|
44 | * Copyright update.
|
---|
45 | *
|
---|
46 | * Revision 1.3 2001/05/03 21:02:28 tng
|
---|
47 | * Schema: Add SubstitutionGroupComparator and update exception messages. By Pei Yong Zhang.
|
---|
48 | *
|
---|
49 | * Revision 1.2 2001/02/27 14:48:46 tng
|
---|
50 | * Schema: Add CMAny and ContentLeafNameTypeVector, by Pei Yong Zhang
|
---|
51 | *
|
---|
52 | * Revision 1.1 2001/02/16 14:17:29 tng
|
---|
53 | * Schema: Move the common Content Model files that are shared by DTD
|
---|
54 | * and schema from 'DTD' folder to 'common' folder. By Pei Yong Zhang.
|
---|
55 | *
|
---|
56 | * Revision 1.4 2000/03/02 19:55:37 roddey
|
---|
57 | * This checkin includes many changes done while waiting for the
|
---|
58 | * 1.1.0 code to be finished. I can't list them all here, but a list is
|
---|
59 | * available elsewhere.
|
---|
60 | *
|
---|
61 | * Revision 1.3 2000/02/24 20:16:48 abagchi
|
---|
62 | * Swat for removing Log from API docs
|
---|
63 | *
|
---|
64 | * Revision 1.2 2000/02/09 21:42:36 abagchi
|
---|
65 | * Copyright swat
|
---|
66 | *
|
---|
67 | * Revision 1.1.1.1 1999/11/09 01:03:06 twl
|
---|
68 | * Initial checkin
|
---|
69 | *
|
---|
70 | * Revision 1.3 1999/11/08 20:45:36 rahul
|
---|
71 | * Swat for adding in Product name and CVS comment log variable.
|
---|
72 | *
|
---|
73 | */
|
---|
74 |
|
---|
75 | // DESCRIPTION:
|
---|
76 | //
|
---|
77 | // This class is a specialized bitset class for the content model code of
|
---|
78 | // the validator. It assumes that its never called with two objects of
|
---|
79 | // different bit counts, and that bit sets smaller than 64 bits are far
|
---|
80 | // and away the most common. So it can be a lot more optimized than a general
|
---|
81 | // purpose utility bitset class
|
---|
82 | //
|
---|
83 |
|
---|
84 | #if !defined(CMSTATESET_HPP)
|
---|
85 | #define CMSTATESET_HPP
|
---|
86 |
|
---|
87 | #include <xercesc/util/ArrayIndexOutOfBoundsException.hpp>
|
---|
88 | #include <xercesc/util/RuntimeException.hpp>
|
---|
89 | #include <xercesc/util/PlatformUtils.hpp>
|
---|
90 | #include <xercesc/framework/MemoryManager.hpp>
|
---|
91 | #include <string.h>
|
---|
92 |
|
---|
93 | XERCES_CPP_NAMESPACE_BEGIN
|
---|
94 |
|
---|
95 | class CMStateSet : public XMemory
|
---|
96 | {
|
---|
97 | public :
|
---|
98 | // -----------------------------------------------------------------------
|
---|
99 | // Constructors and Destructor
|
---|
100 | // -----------------------------------------------------------------------
|
---|
101 | CMStateSet( const unsigned int bitCount
|
---|
102 | , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager) :
|
---|
103 |
|
---|
104 | fBitCount(bitCount)
|
---|
105 | , fByteArray(0)
|
---|
106 | , fMemoryManager(manager)
|
---|
107 | {
|
---|
108 | //
|
---|
109 | // See if we need to allocate the byte array or whether we can live
|
---|
110 | // within the 64 bit high performance scheme.
|
---|
111 | //
|
---|
112 | if (fBitCount > 64)
|
---|
113 | {
|
---|
114 | fByteCount = fBitCount / 8;
|
---|
115 | if (fBitCount % 8)
|
---|
116 | fByteCount++;
|
---|
117 | fByteArray = (XMLByte*) fMemoryManager->allocate(fByteCount*sizeof(XMLByte)); //new XMLByte[fByteCount];
|
---|
118 | }
|
---|
119 |
|
---|
120 | // Init all the bits to zero
|
---|
121 | zeroBits();
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 | /*
|
---|
126 | * This method with the 'for' statement (commented out) cannot be made inline
|
---|
127 | * because the antiquated CC (CFront) compiler under HPUX 10.20 does not allow
|
---|
128 | * the 'for' statement inside any inline method. Unfortunately,
|
---|
129 | * we have to support it. So instead, we use memcpy().
|
---|
130 | */
|
---|
131 |
|
---|
132 | CMStateSet(const CMStateSet& toCopy) :
|
---|
133 | fBitCount(toCopy.fBitCount)
|
---|
134 | , fByteArray(0)
|
---|
135 | , fMemoryManager(toCopy.fMemoryManager)
|
---|
136 | {
|
---|
137 | //
|
---|
138 | // See if we need to allocate the byte array or whether we can live
|
---|
139 | // within the 64 bit high performance scheme.
|
---|
140 | //
|
---|
141 | if (fBitCount > 64)
|
---|
142 | {
|
---|
143 | fByteCount = fBitCount / 8;
|
---|
144 | if (fBitCount % 8)
|
---|
145 | fByteCount++;
|
---|
146 | fByteArray = (XMLByte*) fMemoryManager->allocate(fByteCount*sizeof(XMLByte)); //new XMLByte[fByteCount];
|
---|
147 |
|
---|
148 | memcpy((void *) fByteArray,
|
---|
149 | (const void *) toCopy.fByteArray,
|
---|
150 | fByteCount * sizeof(XMLByte));
|
---|
151 |
|
---|
152 | // for (unsigned int index = 0; index < fByteCount; index++)
|
---|
153 | // fByteArray[index] = toCopy.fByteArray[index];
|
---|
154 | }
|
---|
155 | else
|
---|
156 | {
|
---|
157 | fBits1 = toCopy.fBits1;
|
---|
158 | fBits2 = toCopy.fBits2;
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | ~CMStateSet()
|
---|
163 | {
|
---|
164 | if (fByteArray)
|
---|
165 | fMemoryManager->deallocate(fByteArray); //delete [] fByteArray;
|
---|
166 | }
|
---|
167 |
|
---|
168 |
|
---|
169 | // -----------------------------------------------------------------------
|
---|
170 | // Set manipulation methods
|
---|
171 | // -----------------------------------------------------------------------
|
---|
172 | void operator&=(const CMStateSet& setToAnd)
|
---|
173 | {
|
---|
174 | if (fBitCount < 65)
|
---|
175 | {
|
---|
176 | fBits1 &= setToAnd.fBits1;
|
---|
177 | fBits2 &= setToAnd.fBits2;
|
---|
178 | }
|
---|
179 | else
|
---|
180 | {
|
---|
181 | for (unsigned int index = 0; index < fByteCount; index++)
|
---|
182 | fByteArray[index] &= setToAnd.fByteArray[index];
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | void operator|=(const CMStateSet& setToOr)
|
---|
187 | {
|
---|
188 | if (fBitCount < 65)
|
---|
189 | {
|
---|
190 | fBits1 |= setToOr.fBits1;
|
---|
191 | fBits2 |= setToOr.fBits2;
|
---|
192 | }
|
---|
193 | else
|
---|
194 | {
|
---|
195 | for (unsigned int index = 0; index < fByteCount; index++)
|
---|
196 | fByteArray[index] |= setToOr.fByteArray[index];
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 | bool operator==(const CMStateSet& setToCompare) const
|
---|
201 | {
|
---|
202 | if (fBitCount != setToCompare.fBitCount)
|
---|
203 | return false;
|
---|
204 |
|
---|
205 | if (fBitCount < 65)
|
---|
206 | {
|
---|
207 | return ((fBits1 == setToCompare.fBits1)
|
---|
208 | && (fBits2 == setToCompare.fBits2));
|
---|
209 | }
|
---|
210 |
|
---|
211 | for (unsigned int index = 0; index < fByteCount; index++)
|
---|
212 | {
|
---|
213 | if (fByteArray[index] != setToCompare.fByteArray[index])
|
---|
214 | return false;
|
---|
215 | }
|
---|
216 | return true;
|
---|
217 | }
|
---|
218 |
|
---|
219 | CMStateSet& operator=(const CMStateSet& srcSet)
|
---|
220 | {
|
---|
221 | if (this == &srcSet)
|
---|
222 | return *this;
|
---|
223 |
|
---|
224 | // They have to be the same size
|
---|
225 | if (fBitCount != srcSet.fBitCount)
|
---|
226 | ThrowXMLwithMemMgr(RuntimeException, XMLExcepts::Bitset_NotEqualSize, fMemoryManager);
|
---|
227 |
|
---|
228 | if (fBitCount < 65)
|
---|
229 | {
|
---|
230 | fBits1 = srcSet.fBits1;
|
---|
231 | fBits2 = srcSet.fBits2;
|
---|
232 | }
|
---|
233 | else
|
---|
234 | {
|
---|
235 | for (unsigned int index = 0; index < fByteCount; index++)
|
---|
236 | fByteArray[index] = srcSet.fByteArray[index];
|
---|
237 | }
|
---|
238 | return *this;
|
---|
239 | }
|
---|
240 |
|
---|
241 |
|
---|
242 | bool getBit(const unsigned int bitToGet) const
|
---|
243 | {
|
---|
244 | if (bitToGet >= fBitCount)
|
---|
245 | ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Bitset_BadIndex, fMemoryManager);
|
---|
246 |
|
---|
247 | if (fBitCount < 65)
|
---|
248 | {
|
---|
249 | unsigned int mask = (0x1UL << (bitToGet % 32));
|
---|
250 | if (bitToGet < 32)
|
---|
251 | return ((fBits1 & mask) != 0);
|
---|
252 | else
|
---|
253 | return ((fBits2 & mask) != 0);
|
---|
254 | }
|
---|
255 |
|
---|
256 | // Create the mask and byte values
|
---|
257 | const XMLByte mask1 = XMLByte(0x1 << (bitToGet % 8));
|
---|
258 | const unsigned int byteOfs = bitToGet >> 3;
|
---|
259 |
|
---|
260 | // And access the right bit and byte
|
---|
261 | return ((fByteArray[byteOfs] & mask1) != 0);
|
---|
262 | }
|
---|
263 |
|
---|
264 | bool isEmpty() const
|
---|
265 | {
|
---|
266 | if (fBitCount < 65)
|
---|
267 | return ((fBits1 == 0) && (fBits2 == 0));
|
---|
268 |
|
---|
269 | for (unsigned int index = 0; index < fByteCount; index++)
|
---|
270 | {
|
---|
271 | if (fByteArray[index] != 0)
|
---|
272 | return false;
|
---|
273 | }
|
---|
274 | return true;
|
---|
275 | }
|
---|
276 |
|
---|
277 | void setBit(const unsigned int bitToSet)
|
---|
278 | {
|
---|
279 | if (bitToSet >= fBitCount)
|
---|
280 | ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Bitset_BadIndex, fMemoryManager);
|
---|
281 |
|
---|
282 | if (fBitCount < 65)
|
---|
283 | {
|
---|
284 | const unsigned int mask = (0x1UL << (bitToSet % 32));
|
---|
285 | if (bitToSet < 32)
|
---|
286 | {
|
---|
287 | fBits1 &= ~mask;
|
---|
288 | fBits1 |= mask;
|
---|
289 | }
|
---|
290 | else
|
---|
291 | {
|
---|
292 | fBits2 &= ~mask;
|
---|
293 | fBits2 |= mask;
|
---|
294 | }
|
---|
295 | }
|
---|
296 | else
|
---|
297 | {
|
---|
298 | // Create the mask and byte values
|
---|
299 | const XMLByte mask1 = XMLByte(0x1 << (bitToSet % 8));
|
---|
300 | const unsigned int byteOfs = bitToSet >> 3;
|
---|
301 |
|
---|
302 | // And access the right bit and byte
|
---|
303 | fByteArray[byteOfs] &= ~mask1;
|
---|
304 | fByteArray[byteOfs] |= mask1;
|
---|
305 | }
|
---|
306 | }
|
---|
307 |
|
---|
308 | void zeroBits()
|
---|
309 | {
|
---|
310 | if (fBitCount < 65)
|
---|
311 | {
|
---|
312 | fBits1 = 0;
|
---|
313 | fBits2 = 0;
|
---|
314 | }
|
---|
315 | else
|
---|
316 | {
|
---|
317 | for (unsigned int index = 0; index < fByteCount; index++)
|
---|
318 | fByteArray[index] = 0;
|
---|
319 | }
|
---|
320 | }
|
---|
321 |
|
---|
322 | int hashCode() const
|
---|
323 | {
|
---|
324 | if (fBitCount < 65)
|
---|
325 | {
|
---|
326 | return fBits1+ fBits2 * 31;
|
---|
327 | }
|
---|
328 | else
|
---|
329 | {
|
---|
330 | int hash = 0;
|
---|
331 | for (int index = fByteCount - 1; index >= 0; index--)
|
---|
332 | hash = fByteArray[index] + hash * 31;
|
---|
333 | return hash;
|
---|
334 | }
|
---|
335 |
|
---|
336 | }
|
---|
337 |
|
---|
338 | private :
|
---|
339 | // -----------------------------------------------------------------------
|
---|
340 | // Unimplemented constructors and operators
|
---|
341 | // -----------------------------------------------------------------------
|
---|
342 | CMStateSet();
|
---|
343 |
|
---|
344 |
|
---|
345 | // -----------------------------------------------------------------------
|
---|
346 | // Private data members
|
---|
347 | //
|
---|
348 | // fBitCount
|
---|
349 | // The count of bits that the outside world wants to support,
|
---|
350 | // so its the max bit index plus one.
|
---|
351 | //
|
---|
352 | // fByteCount
|
---|
353 | // If the bit count is > 64, then we use the fByteArray member to
|
---|
354 | // store the bits, and this indicates its size in bytes. Otherwise
|
---|
355 | // its value is meaningless and unset.
|
---|
356 | //
|
---|
357 | // fBits1
|
---|
358 | // fBits2
|
---|
359 | // When the bit count is <= 64 (very common), these hold the bits.
|
---|
360 | // Otherwise, the fByteArray member holds htem.
|
---|
361 | //
|
---|
362 | // fByteArray
|
---|
363 | // The array of bytes used when the bit count is > 64. It is
|
---|
364 | // allocated as required.
|
---|
365 | // -----------------------------------------------------------------------
|
---|
366 | unsigned int fBitCount;
|
---|
367 | unsigned int fByteCount;
|
---|
368 | unsigned int fBits1;
|
---|
369 | unsigned int fBits2;
|
---|
370 | XMLByte* fByteArray;
|
---|
371 | MemoryManager* fMemoryManager;
|
---|
372 | };
|
---|
373 |
|
---|
374 | XERCES_CPP_NAMESPACE_END
|
---|
375 |
|
---|
376 | #endif
|
---|