RefHashTableOf.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) 1999-2000 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: RefHashTableOf.hpp,v $
  58.  * Revision 1.9  2001/06/04 13:45:04  tng
  59.  * The "hash" argument clashes with STL hash.  Fixed by Pei Yong Zhang.
  60.  *
  61.  * Revision 1.8  2000/07/07 22:16:51  jpolast
  62.  * remove old put(value) function.  use put(key,value) instead.
  63.  *
  64.  * Revision 1.7  2000/06/29 18:27:09  jpolast
  65.  * bug fix for passing hasher class references to constructor
  66.  *
  67.  * Revision 1.6  2000/06/27 22:11:12  jpolast
  68.  * added more general functionality to hashtables.
  69.  * able to specify which hasher to use.
  70.  * default: HashXMLCh [hashes XMLCh* strings]
  71.  *
  72.  * future todo: make hasher class references static so only
  73.  * one instance of a hasher is ever created.
  74.  *
  75.  * Revision 1.5  2000/03/02 19:54:44  roddey
  76.  * This checkin includes many changes done while waiting for the
  77.  * 1.1.0 code to be finished. I can't list them all here, but a list is
  78.  * available elsewhere.
  79.  *
  80.  * Revision 1.4  2000/02/24 20:05:25  abagchi
  81.  * Swat for removing Log from API docs
  82.  *
  83.  * Revision 1.3  2000/02/06 07:48:03  rahulj
  84.  * Year 2K copyright swat.
  85.  *
  86.  * Revision 1.2  1999/12/18 00:18:10  roddey
  87.  * More changes to support the new, completely orthagonal support for
  88.  * intrinsic encodings.
  89.  *
  90.  * Revision 1.1.1.1  1999/11/09 01:05:01  twl
  91.  * Initial checkin
  92.  *
  93.  * Revision 1.2  1999/11/08 20:45:12  rahul
  94.  * Swat for adding in Product name and CVS comment log variable.
  95.  *
  96.  */
  97. #if !defined(REFHASHTABLEOF_HPP)
  98. #define REFHASHTABLEOF_HPP
  99. #include <util/XercesDefs.hpp>
  100. #include <util/KeyValuePair.hpp>
  101. #include <util/HashBase.hpp>
  102. #include <util/IllegalArgumentException.hpp>
  103. #include <util/NoSuchElementException.hpp>
  104. #include <util/RuntimeException.hpp>
  105. #include <util/XMLExceptMsgs.hpp>
  106. #include <util/XMLEnumerator.hpp>
  107. #include <util/XMLString.hpp>
  108. #include <util/HashBase.hpp>
  109. #include <util/HashXMLCh.hpp>
  110. //
  111. //  Forward declare the enumerator so he can be our friend. Can you say
  112. //  friend? Sure...
  113. //
  114. template <class TVal> class RefHashTableOfEnumerator;
  115. template <class TVal> struct RefHashTableBucketElem;
  116. //
  117. //  This should really be a nested class, but some of the compilers we
  118. //  have to support cannot deal with that!
  119. //
  120. template <class TVal> struct RefHashTableBucketElem
  121. {
  122.     RefHashTableBucketElem(void* key, TVal* const value, RefHashTableBucketElem<TVal>* next) 
  123. : fData(value), fNext(next), fKey(key)
  124.         {
  125.         }
  126.     TVal*                           fData;
  127.     RefHashTableBucketElem<TVal>*   fNext;
  128. void* fKey;
  129. };
  130. template <class TVal> class RefHashTableOf
  131. {
  132. public:
  133.     // -----------------------------------------------------------------------
  134.     //  Constructors and Destructor
  135.     // -----------------------------------------------------------------------
  136. // backwards compatability - default hasher is HashXMLCh
  137.     RefHashTableOf(const unsigned int modulus);
  138. // backwards compatability - default hasher is HashXMLCh
  139.     RefHashTableOf(const unsigned int modulus, const bool adoptElems);
  140. // if a hash function is passed in, it will be deleted when the hashtable is deleted.
  141. // use a new instance of the hasher class for each hashtable, otherwise one hashtable
  142. // may delete the hasher of a different hashtable if both use the same hasher.
  143.     RefHashTableOf(const unsigned int modulus, const bool adoptElems, HashBase* hashBase);
  144.     ~RefHashTableOf();
  145.     // -----------------------------------------------------------------------
  146.     //  Element management
  147.     // -----------------------------------------------------------------------
  148.     bool isEmpty() const;
  149.     bool containsKey(const void* const key) const;
  150.     void removeKey(const void* const key);
  151.     void removeAll();
  152.     // -----------------------------------------------------------------------
  153.     //  Getters
  154.     // -----------------------------------------------------------------------
  155.     TVal* get(const void* const key);
  156.     const TVal* get(const void* const key) const;
  157.     // -----------------------------------------------------------------------
  158.     //  Putters
  159.     // -----------------------------------------------------------------------
  160. void put(void* key, TVal* const valueToAdopt);
  161. private :
  162.     // -----------------------------------------------------------------------
  163.     //  Declare our friends
  164.     // -----------------------------------------------------------------------
  165.     friend class RefHashTableOfEnumerator<TVal>;
  166. private:
  167.     // -----------------------------------------------------------------------
  168.     //  Private methods
  169.     // -----------------------------------------------------------------------
  170.     RefHashTableBucketElem<TVal>* findBucketElem(const void* const key, unsigned int& hashVal);
  171.     const RefHashTableBucketElem<TVal>* findBucketElem(const void* const key, unsigned int& hashVal) const;
  172.     void removeBucketElem(const void* const key, unsigned int& hashVal);
  173. void initialize(const unsigned int modulus);
  174.     // -----------------------------------------------------------------------
  175.     //  Data members
  176.     //
  177.     //  fAdoptedElems
  178.     //      Indicates whether the values added are adopted or just referenced.
  179.     //      If adopted, then they are deleted when they are removed from the
  180.     //      hash table.
  181.     //
  182.     //  fBucketList
  183.     //      This is the array that contains the heads of all of the list
  184.     //      buckets, one for each possible hash value.
  185.     //
  186.     //  fHashModulus
  187.     //      The modulus used for this hash table, to hash the keys. This is
  188.     //      also the number of elements in the bucket list.
  189. //
  190. //  fHash
  191. //      The hasher for the key data type.
  192.     // -----------------------------------------------------------------------
  193.     bool                                fAdoptedElems;
  194.     RefHashTableBucketElem<TVal>**      fBucketList;
  195.     unsigned int                        fHashModulus;
  196. HashBase* fHash;
  197. };
  198. //
  199. //  An enumerator for a value array. It derives from the basic enumerator
  200. //  class, so that value vectors can be generically enumerated.
  201. //
  202. template <class TVal> class RefHashTableOfEnumerator : public XMLEnumerator<TVal>
  203. {
  204. public :
  205.     // -----------------------------------------------------------------------
  206.     //  Constructors and Destructor
  207.     // -----------------------------------------------------------------------
  208.     RefHashTableOfEnumerator(RefHashTableOf<TVal>* const toEnum, const bool adopt = false);
  209.     ~RefHashTableOfEnumerator();
  210.     // -----------------------------------------------------------------------
  211.     //  Enum interface
  212.     // -----------------------------------------------------------------------
  213.     bool hasMoreElements() const;
  214.     TVal& nextElement();
  215.     void Reset();
  216. private :    
  217.     // -----------------------------------------------------------------------
  218.     //  Private methods
  219.     // -----------------------------------------------------------------------
  220.     void findNext();
  221.     // -----------------------------------------------------------------------
  222.     //  Data Members
  223.     //
  224.     //  fAdopted
  225.     //      Indicates whether we have adopted the passed vector. If so then
  226.     //      we delete the vector when we are destroyed.
  227.     //
  228.     //  fCurElem
  229.     //      This is the current bucket bucket element that we are on.
  230.     //
  231.     //  fCurHash
  232.     //      The is the current hash buck that we are working on. Once we hit
  233.     //      the end of the bucket that fCurElem is in, then we have to start
  234.     //      working this one up to the next non-empty bucket.
  235.     //
  236.     //  fToEnum
  237.     //      The value array being enumerated.
  238.     // -----------------------------------------------------------------------
  239.     bool                                  fAdopted;
  240.     RefHashTableBucketElem<TVal>*         fCurElem;
  241.     unsigned int                          fCurHash;
  242.     RefHashTableOf<TVal>*                 fToEnum;
  243. };
  244. #if !defined(XERCES_TMPLSINC)
  245. #include <util/RefHashTableOf.c>
  246. #endif
  247. #endif