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

xml/soap/webservice

开发平台:

C/C++

  1. /*
  2.  * The Apache Software License, Version 1.1
  3.  *
  4.  * Copyright (c) 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) 2001, 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: RefHash3KeysIdPool.hpp,v $
  58.  * Revision 1.3  2001/06/04 13:45:04  tng
  59.  * The "hash" argument clashes with STL hash.  Fixed by Pei Yong Zhang.
  60.  *
  61.  * Revision 1.2  2001/05/11 13:26:29  tng
  62.  * Copyright update.
  63.  *
  64.  * Revision 1.1  2001/03/21 21:56:12  tng
  65.  * Schema: Add Schema Grammar, Schema Validator, and split the DTDValidator into DTDValidator, DTDScanner, and DTDGrammar.
  66.  *
  67.  */
  68. #if !defined(REFHASH3KEYSIDPOOL_HPP)
  69. #define REFHASH3KEYSIDPOOL_HPP
  70. #include <util/XercesDefs.hpp>
  71. #include <util/KeyValuePair.hpp>
  72. #include <util/HashBase.hpp>
  73. #include <util/IllegalArgumentException.hpp>
  74. #include <util/NoSuchElementException.hpp>
  75. #include <util/RuntimeException.hpp>
  76. #include <util/XMLExceptMsgs.hpp>
  77. #include <util/XMLEnumerator.hpp>
  78. #include <util/XMLString.hpp>
  79. #include <util/HashBase.hpp>
  80. #include <util/HashXMLCh.hpp>
  81. // This hash table is a combination of RefHash2KeyTableOf (with an additional integer as key3)
  82. // and NameIdPool with an id as index
  83. //
  84. //  Forward declare the enumerator so he can be our friend. Can you say
  85. //  friend? Sure...
  86. //
  87. template <class TVal> class RefHash3KeysIdPoolEnumerator;
  88. template <class TVal> struct RefHash3KeysTableBucketElem;
  89. //
  90. //  This should really be a nested class, but some of the compilers we
  91. //  have to support cannot deal with that!
  92. //
  93. template <class TVal> struct RefHash3KeysTableBucketElem
  94. {
  95.     RefHash3KeysTableBucketElem(
  96.               void* key1
  97.               , int key2
  98.               , int key3
  99.               , TVal* const value
  100.               , RefHash3KeysTableBucketElem<TVal>* next) :
  101. fData(value)
  102.     , fNext(next)
  103.     , fKey1(key1)
  104.     , fKey2(key2)
  105.     , fKey3(key3)
  106.     {
  107.     }
  108.     TVal*  fData;
  109.     RefHash3KeysTableBucketElem<TVal>*   fNext;
  110.     void*  fKey1;
  111.     int    fKey2;
  112.     int    fKey3;
  113. };
  114. template <class TVal> class RefHash3KeysIdPool
  115. {
  116. public:
  117.     // -----------------------------------------------------------------------
  118.     //  Constructors and Destructor
  119.     // -----------------------------------------------------------------------
  120.     // backwards compatability - default hasher is HashXMLCh
  121.     RefHash3KeysIdPool
  122.     (
  123.         const unsigned int modulus
  124.       , const unsigned int    initSize = 128
  125.     );
  126.     // backwards compatability - default hasher is HashXMLCh
  127.     RefHash3KeysIdPool
  128.     (
  129.         const unsigned int modulus
  130.       , const bool adoptElems
  131.       , const unsigned int initSize = 128
  132.     );
  133.     // if a hash function is passed in, it will be deleted when the hashtable is deleted.
  134.     // use a new instance of the hasher class for each hashtable, otherwise one hashtable
  135.     // may delete the hasher of a different hashtable if both use the same hasher.
  136.     RefHash3KeysIdPool
  137.     (
  138.          const unsigned int modulus
  139.        , const bool adoptElems, HashBase* hashBase
  140.        , const unsigned int initSize = 128
  141.     );
  142.     ~RefHash3KeysIdPool();
  143.     // -----------------------------------------------------------------------
  144.     //  Element management
  145.     // -----------------------------------------------------------------------
  146.     bool isEmpty() const;
  147.     bool containsKey(const void* const key1, const int key2, const int key3) const;
  148.     void removeAll();
  149.     // -----------------------------------------------------------------------
  150.     //  Getters
  151.     // -----------------------------------------------------------------------
  152.     TVal* getByKey(const void* const key1, const int key2, const int key3);
  153.     const TVal* getByKey(const void* const key1, const int key2, const int key3) const;
  154.     TVal* getById(const unsigned elemId);
  155.     const TVal* getById(const unsigned elemId) const;
  156.     // -----------------------------------------------------------------------
  157.     //  Putters
  158.     // -----------------------------------------------------------------------
  159. unsigned int put(void* key1, int key2, int key3, TVal* const valueToAdopt);
  160. private :
  161.     // -----------------------------------------------------------------------
  162.     //  Declare our friends
  163.     // -----------------------------------------------------------------------
  164.     friend class RefHash3KeysIdPoolEnumerator<TVal>;
  165. private:
  166.     // -----------------------------------------------------------------------
  167.     //  Private methods
  168.     // -----------------------------------------------------------------------
  169.     RefHash3KeysTableBucketElem<TVal>* findBucketElem(const void* const key1, const int key2, const int key3, unsigned int& hashVal);
  170.     const RefHash3KeysTableBucketElem<TVal>* findBucketElem(const void* const key1, const int key2, const int key3, unsigned int& hashVal) const;
  171.     void initialize(const unsigned int modulus);
  172.     // -----------------------------------------------------------------------
  173.     //  Data members
  174.     //
  175.     //  fAdoptedElems
  176.     //      Indicates whether the values added are adopted or just referenced.
  177.     //      If adopted, then they are deleted when they are removed from the
  178.     //      hash table.
  179.     //
  180.     //  fBucketList
  181.     //      This is the array that contains the heads of all of the list
  182.     //      buckets, one for each possible hash value.
  183.     //
  184.     //  fHashModulus
  185.     //      The modulus used for this hash table, to hash the keys. This is
  186.     //      also the number of elements in the bucket list.
  187.     //
  188.     //  fHash
  189.     //      The hasher for the key1 data type.
  190.     //
  191.     //  fIdPtrs
  192.     //  fIdPtrsCount
  193.     //      This is the array of pointers to the bucket elements in order of
  194.     //      their assigned ids. So taking id N and referencing this array
  195.     //      gives you the element with that id. The count field indicates
  196.     //      the current size of this list. When fIdCounter+1 reaches this
  197.     //      value the list must be expanded.
  198.     //
  199.     //  fIdCounter
  200.     //      This is used to give out unique ids to added elements. It starts
  201.     //      at zero (which means empty), and is bumped up for each newly added
  202.     //      element. So the first element is 1, the next is 2, etc... This
  203.     //      means that this value is set to the top index of the fIdPtrs array.
  204.     // -----------------------------------------------------------------------
  205.     bool                                fAdoptedElems;
  206.     RefHash3KeysTableBucketElem<TVal>** fBucketList;
  207.     unsigned int                        fHashModulus;
  208.     HashBase*                       fHash;
  209.     TVal**                          fIdPtrs;
  210.     unsigned int                    fIdPtrsCount;
  211.     unsigned int                    fIdCounter;
  212. };
  213. //
  214. //  An enumerator for a value array. It derives from the basic enumerator
  215. //  class, so that value vectors can be generically enumerated.
  216. //
  217. template <class TVal> class RefHash3KeysIdPoolEnumerator : public XMLEnumerator<TVal>
  218. {
  219. public :
  220.     // -----------------------------------------------------------------------
  221.     //  Constructors and Destructor
  222.     // -----------------------------------------------------------------------
  223.     RefHash3KeysIdPoolEnumerator(RefHash3KeysIdPool<TVal>* const toEnum, const bool adopt = false);
  224.     ~RefHash3KeysIdPoolEnumerator();
  225.     // -----------------------------------------------------------------------
  226.     //  Enum interface
  227.     // -----------------------------------------------------------------------
  228.     bool hasMoreElements() const;
  229.     TVal& nextElement();
  230.     void Reset();
  231. private :
  232.     // -----------------------------------------------------------------------
  233.     //  Data Members
  234.     //  fAdoptedElems
  235.     //      Indicates whether the values added are adopted or just referenced.
  236.     //      If adopted, then they are deleted when they are removed from the
  237.     //      hash table
  238.     //
  239.     //  fCurIndex
  240.     //      This is the current index into the pool's id mapping array. This
  241.     //      is now we enumerate it.
  242.     //
  243.     //  fToEnum
  244.     //      The name id pool that is being enumerated.
  245.     // -----------------------------------------------------------------------
  246.     bool                       fAdoptedElems;
  247.     unsigned int               fCurIndex;
  248.     RefHash3KeysIdPool<TVal>*  fToEnum;
  249. };
  250. #if !defined(XERCES_TMPLSINC)
  251. #include <util/RefHash3KeysIdPool.c>
  252. #endif
  253. #endif