CMStateSet.hpp
上传用户:huihehuasu
上传日期:2007-01-10
资源大小:6948k
文件大小:12k
源码类别:

xml/soap/webservice

开发平台:

C/C++

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