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

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: RefHash2KeysTableOf.hpp,v $
  58.  * Revision 1.3  2001/06/04 13:45:03  tng
  59.  * The "hash" argument clashes with STL hash.  Fixed by Pei Yong Zhang.
  60.  *
  61.  * Revision 1.2  2001/05/11 13:26:28  tng
  62.  * Copyright update.
  63.  *
  64.  * Revision 1.1  2001/02/27 18:24:01  tng
  65.  * Schema: Add utility RefHash2KeysTableOf.
  66.  *
  67.  */
  68. #if !defined(REFHASH2KEYSTABLEOF_HPP)
  69. #define REFHASH2KEYSTABLEOF_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 similar to RefHashTableOf with an additional integer as key2
  82. //
  83. //  Forward declare the enumerator so he can be our friend. Can you say
  84. //  friend? Sure...
  85. //
  86. template <class TVal> class RefHash2KeysTableOfEnumerator;
  87. template <class TVal> struct RefHash2KeysTableBucketElem;
  88. //
  89. //  This should really be a nested class, but some of the compilers we
  90. //  have to support cannot deal with that!
  91. //
  92. template <class TVal> struct RefHash2KeysTableBucketElem
  93. {
  94.     RefHash2KeysTableBucketElem(void* key1, int key2, TVal* const value, RefHash2KeysTableBucketElem<TVal>* next)
  95. : fData(value), fNext(next), fKey1(key1), fKey2(key2)
  96.         {
  97.         }
  98.     TVal*                           fData;
  99.     RefHash2KeysTableBucketElem<TVal>*   fNext;
  100. void* fKey1;
  101. int fKey2;
  102. };
  103. template <class TVal> class RefHash2KeysTableOf
  104. {
  105. public:
  106.     // -----------------------------------------------------------------------
  107.     //  Constructors and Destructor
  108.     // -----------------------------------------------------------------------
  109. // backwards compatability - default hasher is HashXMLCh
  110.     RefHash2KeysTableOf(const unsigned int modulus);
  111. // backwards compatability - default hasher is HashXMLCh
  112.     RefHash2KeysTableOf(const unsigned int modulus, const bool adoptElems);
  113. // if a hash function is passed in, it will be deleted when the hashtable is deleted.
  114. // use a new instance of the hasher class for each hashtable, otherwise one hashtable
  115. // may delete the hasher of a different hashtable if both use the same hasher.
  116.     RefHash2KeysTableOf(const unsigned int modulus, const bool adoptElems, HashBase* hashBase);
  117.     ~RefHash2KeysTableOf();
  118.     // -----------------------------------------------------------------------
  119.     //  Element management
  120.     // -----------------------------------------------------------------------
  121.     bool isEmpty() const;
  122.     bool containsKey(const void* const key1, const int key2) const;
  123.     void removeKey(const void* const key1, const int key2);
  124.     void removeAll();
  125.     // -----------------------------------------------------------------------
  126.     //  Getters
  127.     // -----------------------------------------------------------------------
  128.     TVal* get(const void* const key1, const int key2);
  129.     const TVal* get(const void* const key1, const int key2) const;
  130.     // -----------------------------------------------------------------------
  131.     //  Putters
  132.     // -----------------------------------------------------------------------
  133. void put(void* key1, int key2, TVal* const valueToAdopt);
  134. private :
  135.     // -----------------------------------------------------------------------
  136.     //  Declare our friends
  137.     // -----------------------------------------------------------------------
  138.     friend class RefHash2KeysTableOfEnumerator<TVal>;
  139. private:
  140.     // -----------------------------------------------------------------------
  141.     //  Private methods
  142.     // -----------------------------------------------------------------------
  143.     RefHash2KeysTableBucketElem<TVal>* findBucketElem(const void* const key1, const int key2, unsigned int& hashVal);
  144.     const RefHash2KeysTableBucketElem<TVal>* findBucketElem(const void* const key1, const int key2, unsigned int& hashVal) const;
  145.     void removeBucketElem(const void* const key1, const int key2, unsigned int& hashVal);
  146. void initialize(const unsigned int modulus);
  147.     // -----------------------------------------------------------------------
  148.     //  Data members
  149.     //
  150.     //  fAdoptedElems
  151.     //      Indicates whether the values added are adopted or just referenced.
  152.     //      If adopted, then they are deleted when they are removed from the
  153.     //      hash table.
  154.     //
  155.     //  fBucketList
  156.     //      This is the array that contains the heads of all of the list
  157.     //      buckets, one for each possible hash value.
  158.     //
  159.     //  fHashModulus
  160.     //      The modulus used for this hash table, to hash the keys. This is
  161.     //      also the number of elements in the bucket list.
  162. //
  163. //  fHash
  164. //      The hasher for the key1 data type.
  165.     // -----------------------------------------------------------------------
  166.     bool                                fAdoptedElems;
  167.     RefHash2KeysTableBucketElem<TVal>**      fBucketList;
  168.     unsigned int                        fHashModulus;
  169. HashBase* fHash;
  170. };
  171. //
  172. //  An enumerator for a value array. It derives from the basic enumerator
  173. //  class, so that value vectors can be generically enumerated.
  174. //
  175. template <class TVal> class RefHash2KeysTableOfEnumerator : public XMLEnumerator<TVal>
  176. {
  177. public :
  178.     // -----------------------------------------------------------------------
  179.     //  Constructors and Destructor
  180.     // -----------------------------------------------------------------------
  181.     RefHash2KeysTableOfEnumerator(RefHash2KeysTableOf<TVal>* const toEnum, const bool adopt = false);
  182.     ~RefHash2KeysTableOfEnumerator();
  183.     // -----------------------------------------------------------------------
  184.     //  Enum interface
  185.     // -----------------------------------------------------------------------
  186.     bool hasMoreElements() const;
  187.     TVal& nextElement();
  188.     void Reset();
  189. private :
  190.     // -----------------------------------------------------------------------
  191.     //  Private methods
  192.     // -----------------------------------------------------------------------
  193.     void findNext();
  194.     // -----------------------------------------------------------------------
  195.     //  Data Members
  196.     //
  197.     //  fAdopted
  198.     //      Indicates whether we have adopted the passed vector. If so then
  199.     //      we delete the vector when we are destroyed.
  200.     //
  201.     //  fCurElem
  202.     //      This is the current bucket bucket element that we are on.
  203.     //
  204.     //  fCurHash
  205.     //      The is the current hash buck that we are working on. Once we hit
  206.     //      the end of the bucket that fCurElem is in, then we have to start
  207.     //      working this one up to the next non-empty bucket.
  208.     //
  209.     //  fToEnum
  210.     //      The value array being enumerated.
  211.     // -----------------------------------------------------------------------
  212.     bool                                  fAdopted;
  213.     RefHash2KeysTableBucketElem<TVal>*         fCurElem;
  214.     unsigned int                          fCurHash;
  215.     RefHash2KeysTableOf<TVal>*                 fToEnum;
  216. };
  217. #if !defined(XERCES_TMPLSINC)
  218. #include <util/RefHash2KeysTableOf.c>
  219. #endif
  220. #endif