source: NonGTP/Xerces/xercesc/validators/common/CMStateSet.hpp @ 188

Revision 188, 13.5 KB checked in by mattausch, 19 years ago (diff)

added xercesc to support

Line 
1/*
2 * The Apache Software License, Version 1.1
3 *
4 * Copyright (c) 1999-2001 The Apache Software Foundation.  All rights
5 * reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in
16 *    the documentation and/or other materials provided with the
17 *    distribution.
18 *
19 * 3. The end-user documentation included with the redistribution,
20 *    if any, must include the following acknowledgment:
21 *       "This product includes software developed by the
22 *        Apache Software Foundation (http://www.apache.org/)."
23 *    Alternately, this acknowledgment may appear in the software itself,
24 *    if and wherever such third-party acknowledgments normally appear.
25 *
26 * 4. The names "Xerces" and "Apache Software Foundation" must
27 *    not be used to endorse or promote products derived from this
28 *    software without prior written permission. For written
29 *    permission, please contact apache\@apache.org.
30 *
31 * 5. Products derived from this software may not be called "Apache",
32 *    nor may "Apache" appear in their name, without prior written
33 *    permission of the Apache Software Foundation.
34 *
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 * SUCH DAMAGE.
47 * ====================================================================
48 *
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Software Foundation, and was
51 * originally based on software copyright (c) 1999, International
52 * Business Machines, Inc., http://www.ibm.com .  For more information
53 * on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57/*
58 * $Log: CMStateSet.hpp,v $
59 * Revision 1.6  2003/12/17 00:18:38  cargilld
60 * Update to memory management so that the static memory manager (one used to call Initialize) is only for static data.
61 *
62 * Revision 1.5  2003/05/16 21:43:20  knoaman
63 * Memory manager implementation: Modify constructors to pass in the memory manager.
64 *
65 * Revision 1.4  2003/05/15 18:48:27  knoaman
66 * Partial implementation of the configurable memory manager.
67 *
68 * Revision 1.3  2002/11/04 14:54:58  tng
69 * C++ Namespace Support.
70 *
71 * Revision 1.2  2002/07/16 12:50:08  tng
72 * [Bug 10651] CMStateSet.hpp includes both memory.h and string.h.
73 *
74 * Revision 1.1.1.1  2002/02/01 22:22:38  peiyongz
75 * sane_include
76 *
77 * Revision 1.5  2001/08/16 21:51:43  peiyongz
78 * hashCode() added
79 *
80 * Revision 1.4  2001/05/11 13:27:17  tng
81 * Copyright update.
82 *
83 * Revision 1.3  2001/05/03 21:02:28  tng
84 * Schema: Add SubstitutionGroupComparator and update exception messages.  By Pei Yong Zhang.
85 *
86 * Revision 1.2  2001/02/27 14:48:46  tng
87 * Schema: Add CMAny and ContentLeafNameTypeVector, by Pei Yong Zhang
88 *
89 * Revision 1.1  2001/02/16 14:17:29  tng
90 * Schema: Move the common Content Model files that are shared by DTD
91 * and schema from 'DTD' folder to 'common' folder.  By Pei Yong Zhang.
92 *
93 * Revision 1.4  2000/03/02 19:55:37  roddey
94 * This checkin includes many changes done while waiting for the
95 * 1.1.0 code to be finished. I can't list them all here, but a list is
96 * available elsewhere.
97 *
98 * Revision 1.3  2000/02/24 20:16:48  abagchi
99 * Swat for removing Log from API docs
100 *
101 * Revision 1.2  2000/02/09 21:42:36  abagchi
102 * Copyright swat
103 *
104 * Revision 1.1.1.1  1999/11/09 01:03:06  twl
105 * Initial checkin
106 *
107 * Revision 1.3  1999/11/08 20:45:36  rahul
108 * Swat for adding in Product name and CVS comment log variable.
109 *
110 */
111
112//  DESCRIPTION:
113//
114//  This class is a specialized bitset class for the content model code of
115//  the validator. It assumes that its never called with two objects of
116//  different bit counts, and that bit sets smaller than 64 bits are far
117//  and away the most common. So it can be a lot more optimized than a general
118//  purpose utility bitset class
119//
120
121#if !defined(CMSTATESET_HPP)
122#define CMSTATESET_HPP
123
124#include <xercesc/util/ArrayIndexOutOfBoundsException.hpp>
125#include <xercesc/util/RuntimeException.hpp>
126#include <xercesc/util/PlatformUtils.hpp>
127#include <xercesc/framework/MemoryManager.hpp>
128#include <string.h>
129
130XERCES_CPP_NAMESPACE_BEGIN
131
132class CMStateSet : public XMemory
133{
134public :
135    // -----------------------------------------------------------------------
136    //  Constructors and Destructor
137    // -----------------------------------------------------------------------
138    CMStateSet( const unsigned int bitCount
139              , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager) :
140
141        fBitCount(bitCount)
142        , fByteArray(0)
143        , fMemoryManager(manager)
144    {
145        //
146        //  See if we need to allocate the byte array or whether we can live
147        //  within the 64 bit high performance scheme.
148        //
149        if (fBitCount > 64)
150        {
151            fByteCount = fBitCount / 8;
152            if (fBitCount % 8)
153                fByteCount++;
154            fByteArray = (XMLByte*) fMemoryManager->allocate(fByteCount*sizeof(XMLByte)); //new XMLByte[fByteCount];
155        }
156
157        // Init all the bits to zero
158        zeroBits();
159    }
160
161
162    /*
163     * This method with the 'for' statement (commented out) cannot be made inline
164     * because the antiquated CC (CFront) compiler under HPUX 10.20 does not allow
165     * the 'for' statement inside any inline method. Unfortunately,
166     * we have to support it. So instead, we use memcpy().
167     */
168
169    CMStateSet(const CMStateSet& toCopy) :
170        fBitCount(toCopy.fBitCount)
171      , fByteArray(0)
172      , fMemoryManager(toCopy.fMemoryManager)
173    {
174        //
175        //  See if we need to allocate the byte array or whether we can live
176        //  within the 64 bit high performance scheme.
177        //
178        if (fBitCount > 64)
179        {
180            fByteCount = fBitCount / 8;
181            if (fBitCount % 8)
182                fByteCount++;
183            fByteArray = (XMLByte*) fMemoryManager->allocate(fByteCount*sizeof(XMLByte)); //new XMLByte[fByteCount];
184
185            memcpy((void *) fByteArray,
186                   (const void *) toCopy.fByteArray,
187                   fByteCount * sizeof(XMLByte));
188
189            // for (unsigned int index = 0; index < fByteCount; index++)
190            //     fByteArray[index] = toCopy.fByteArray[index];
191        }
192         else
193        {
194            fBits1 = toCopy.fBits1;
195            fBits2 = toCopy.fBits2;
196        }
197    }
198
199    ~CMStateSet()
200    {
201        if (fByteArray)
202            fMemoryManager->deallocate(fByteArray); //delete [] fByteArray;
203    }
204
205
206    // -----------------------------------------------------------------------
207    //  Set manipulation methods
208    // -----------------------------------------------------------------------
209    void operator&=(const CMStateSet& setToAnd)
210    {
211        if (fBitCount < 65)
212        {
213            fBits1 &= setToAnd.fBits1;
214            fBits2 &= setToAnd.fBits2;
215        }
216         else
217        {
218            for (unsigned int index = 0; index < fByteCount; index++)
219                fByteArray[index] &= setToAnd.fByteArray[index];
220        }
221    }
222
223    void operator|=(const CMStateSet& setToOr)
224    {
225        if (fBitCount < 65)
226        {
227            fBits1 |= setToOr.fBits1;
228            fBits2 |= setToOr.fBits2;
229        }
230         else
231        {
232            for (unsigned int index = 0; index < fByteCount; index++)
233                fByteArray[index] |= setToOr.fByteArray[index];
234        }
235    }
236
237    bool operator==(const CMStateSet& setToCompare) const
238    {
239        if (fBitCount != setToCompare.fBitCount)
240            return false;
241
242        if (fBitCount < 65)
243        {
244            return ((fBits1 == setToCompare.fBits1)
245            &&      (fBits2 == setToCompare.fBits2));
246        }
247
248        for (unsigned int index = 0; index < fByteCount; index++)
249        {
250            if (fByteArray[index] != setToCompare.fByteArray[index])
251                return false;
252        }
253        return true;
254    }
255
256    CMStateSet& operator=(const CMStateSet& srcSet)
257    {
258        if (this == &srcSet)
259            return *this;
260
261        // They have to be the same size
262        if (fBitCount != srcSet.fBitCount)
263            ThrowXMLwithMemMgr(RuntimeException, XMLExcepts::Bitset_NotEqualSize, fMemoryManager);
264
265        if (fBitCount < 65)
266        {
267            fBits1 = srcSet.fBits1;
268            fBits2 = srcSet.fBits2;
269        }
270         else
271        {
272            for (unsigned int index = 0; index < fByteCount; index++)
273                fByteArray[index] = srcSet.fByteArray[index];
274        }
275        return *this;
276    }
277
278
279    bool getBit(const unsigned int bitToGet) const
280    {
281        if (bitToGet >= fBitCount)
282            ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Bitset_BadIndex, fMemoryManager);
283
284        if (fBitCount < 65)
285        {
286            unsigned int mask = (0x1UL << (bitToGet % 32));
287            if (bitToGet < 32)
288                return ((fBits1 & mask) != 0);
289            else
290                return ((fBits2 & mask) != 0);
291        }
292
293        // Create the mask and byte values
294        const XMLByte mask1 = XMLByte(0x1 << (bitToGet % 8));
295        const unsigned int byteOfs = bitToGet >> 3;
296
297        // And access the right bit and byte
298        return ((fByteArray[byteOfs] & mask1) != 0);
299    }
300
301    bool isEmpty() const
302    {
303        if (fBitCount < 65)
304            return ((fBits1 == 0) && (fBits2 == 0));
305
306        for (unsigned int index = 0; index < fByteCount; index++)
307        {
308            if (fByteArray[index] != 0)
309                return false;
310        }
311        return true;
312    }
313
314    void setBit(const unsigned int bitToSet)
315    {
316        if (bitToSet >= fBitCount)
317            ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Bitset_BadIndex, fMemoryManager);
318
319        if (fBitCount < 65)
320        {
321            const unsigned int mask = (0x1UL << (bitToSet % 32));
322            if (bitToSet < 32)
323            {
324                fBits1 &= ~mask;
325                fBits1 |= mask;
326            }
327             else
328            {
329                fBits2 &= ~mask;
330                fBits2 |= mask;
331            }
332        }
333         else
334        {
335            // Create the mask and byte values
336            const XMLByte mask1 = XMLByte(0x1 << (bitToSet % 8));
337            const unsigned int byteOfs = bitToSet >> 3;
338
339            // And access the right bit and byte
340            fByteArray[byteOfs] &= ~mask1;
341            fByteArray[byteOfs] |= mask1;
342        }
343    }
344
345    void zeroBits()
346    {
347        if (fBitCount < 65)
348        {
349            fBits1 = 0;
350            fBits2 = 0;
351        }
352         else
353        {
354            for (unsigned int index = 0; index < fByteCount; index++)
355                fByteArray[index] = 0;
356        }
357    }
358
359    int hashCode() const
360    {
361        if (fBitCount < 65)
362        {
363            return fBits1+ fBits2 * 31;
364        }
365        else
366        {
367            int hash = 0;
368            for (int index = fByteCount - 1; index >= 0; index--)
369                hash = fByteArray[index] + hash * 31;
370            return hash;
371        }
372
373    }
374
375private :
376    // -----------------------------------------------------------------------
377    //  Unimplemented constructors and operators
378    // -----------------------------------------------------------------------
379    CMStateSet();
380
381
382    // -----------------------------------------------------------------------
383    //  Private data members
384    //
385    //  fBitCount
386    //      The count of bits that the outside world wants to support,
387    //      so its the max bit index plus one.
388    //
389    //  fByteCount
390    //      If the bit count is > 64, then we use the fByteArray member to
391    //      store the bits, and this indicates its size in bytes. Otherwise
392    //      its value is meaningless and unset.
393    //
394    //  fBits1
395    //  fBits2
396    //      When the bit count is <= 64 (very common), these hold the bits.
397    //      Otherwise, the fByteArray member holds htem.
398    //
399    //  fByteArray
400    //      The array of bytes used when the bit count is > 64. It is
401    //      allocated as required.
402    // -----------------------------------------------------------------------
403    unsigned int    fBitCount;
404    unsigned int    fByteCount;
405    unsigned int    fBits1;
406    unsigned int    fBits2;
407    XMLByte*        fByteArray;
408    MemoryManager*  fMemoryManager;
409};
410
411XERCES_CPP_NAMESPACE_END
412
413#endif
Note: See TracBrowser for help on using the repository browser.