http://xml.apache.org/http://www.apache.org/http://www.w3.org/

Home

Readme
Release Info

Installation
Download
Build

FAQs
Samples
API Docs

DOM C++ Binding
Programming
Migration Guide

Feedback
Bug-Reporting
PDF Document

CVS Repository
Mail Archive

API Docs for SAX and DOM
 

Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

ValueHashTableOf.hpp

Go to the documentation of this file.
00001 /*
00002  * The Apache Software License, Version 1.1
00003  *
00004  * Copyright (c) 2002 The Apache Software Foundation.  All rights
00005  * reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions
00009  * are met:
00010  *
00011  * 1. Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  *
00014  * 2. Redistributions in binary form must reproduce the above copyright
00015  *    notice, this list of conditions and the following disclaimer in
00016  *    the documentation and/or other materials provided with the
00017  *    distribution.
00018  *
00019  * 3. The end-user documentation included with the redistribution,
00020  *    if any, must include the following acknowledgment:
00021  *       "This product includes software developed by the
00022  *        Apache Software Foundation (http://www.apache.org/)."
00023  *    Alternately, this acknowledgment may appear in the software itself,
00024  *    if and wherever such third-party acknowledgments normally appear.
00025  *
00026  * 4. The names "Xerces" and "Apache Software Foundation" must
00027  *    not be used to endorse or promote products derived from this
00028  *    software without prior written permission. For written
00029  *    permission, please contact apache\@apache.org.
00030  *
00031  * 5. Products derived from this software may not be called "Apache",
00032  *    nor may "Apache" appear in their name, without prior written
00033  *    permission of the Apache Software Foundation.
00034  *
00035  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
00036  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00037  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00038  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
00039  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00040  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00041  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
00042  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00043  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00044  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00045  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00046  * SUCH DAMAGE.
00047  * ====================================================================
00048  *
00049  * This software consists of voluntary contributions made by many
00050  * individuals on behalf of the Apache Software Foundation, and was
00051  * originally based on software copyright (c) 1999, International
00052  * Business Machines, Inc., http://www.ibm.com .  For more information
00053  * on the Apache Software Foundation, please see
00054  * <http://www.apache.org/>.
00055  */
00056 
00057 /*
00058  * $Id: ValueHashTableOf.hpp,v 1.4 2002/06/12 17:15:12 tng Exp $
00059  */
00060 
00061 
00062 #if !defined(VALUEHASHTABLEOF_HPP)
00063 #define VALUEHASHTABLEOF_HPP
00064 
00065 
00066 #include <xercesc/util/XercesDefs.hpp>
00067 #include <xercesc/util/HashBase.hpp>
00068 #include <xercesc/util/IllegalArgumentException.hpp>
00069 #include <xercesc/util/NoSuchElementException.hpp>
00070 #include <xercesc/util/RuntimeException.hpp>
00071 #include <xercesc/util/XMLExceptMsgs.hpp>
00072 #include <xercesc/util/XMLEnumerator.hpp>
00073 #include <xercesc/util/XMLString.hpp>
00074 #include <xercesc/util/HashBase.hpp>
00075 #include <xercesc/util/HashXMLCh.hpp>
00076 
00077 
00078 //
00079 //  Forward declare the enumerator so he can be our friend. Can you say
00080 //  friend? Sure...
00081 //
00082 template <class TVal> class ValueHashTableOfEnumerator;
00083 template <class TVal> struct ValueHashTableBucketElem;
00084 
00085 
00086 //
00087 //  This should really be a nested class, but some of the compilers we
00088 //  have to support cannot deal with that!
00089 //
00090 template <class TVal> struct ValueHashTableBucketElem
00091 {
00092     ValueHashTableBucketElem(void* key, const TVal& value, ValueHashTableBucketElem<TVal>* next)
00093         : fData(value), fNext(next), fKey(key)
00094         {
00095         }
00096 
00097     TVal                           fData;
00098     ValueHashTableBucketElem<TVal>* fNext;
00099     void*                          fKey;
00100 };
00101 
00102 
00103 template <class TVal> class ValueHashTableOf
00104 {
00105 public:
00106     // -----------------------------------------------------------------------
00107     //  Constructors and Destructor
00108     // -----------------------------------------------------------------------
00109     // backwards compatability - default hasher is HashXMLCh
00110     ValueHashTableOf(const unsigned int modulus);
00111     // if a hash function is passed in, it will be deleted when the hashtable is deleted.
00112     // use a new instance of the hasher class for each hashtable, otherwise one hashtable
00113     // may delete the hasher of a different hashtable if both use the same hasher.
00114     ValueHashTableOf(const unsigned int modulus, HashBase* hashBase);
00115     ~ValueHashTableOf();
00116 
00117 
00118     // -----------------------------------------------------------------------
00119     //  Element management
00120     // -----------------------------------------------------------------------
00121     bool isEmpty() const;
00122     bool containsKey(const void* const key) const;
00123     void removeKey(const void* const key);
00124     void removeAll();
00125 
00126 
00127     // -----------------------------------------------------------------------
00128     //  Getters
00129     // -----------------------------------------------------------------------
00130     TVal& get(const void* const key);
00131     const TVal& get(const void* const key) const;
00132 
00133 
00134     // -----------------------------------------------------------------------
00135     //  Putters
00136     // -----------------------------------------------------------------------
00137     void put(void* key, const TVal& valueToAdopt);
00138 
00139 
00140 private :
00141     // -----------------------------------------------------------------------
00142     //  Declare our friends
00143     // -----------------------------------------------------------------------
00144     friend class ValueHashTableOfEnumerator<TVal>;
00145 
00146 private:
00147 
00148     // -----------------------------------------------------------------------
00149     //  Private methods
00150     // -----------------------------------------------------------------------
00151     ValueHashTableBucketElem<TVal>* findBucketElem(const void* const key, unsigned int& hashVal);
00152     const ValueHashTableBucketElem<TVal>* findBucketElem(const void* const key, unsigned int& hashVal) const;
00153     void removeBucketElem(const void* const key, unsigned int& hashVal);
00154     void initialize(const unsigned int modulus);
00155 
00156 
00157     // -----------------------------------------------------------------------
00158     //  Data members
00159     //
00160     //  fBucketList
00161     //      This is the array that contains the heads of all of the list
00162     //      buckets, one for each possible hash value.
00163     //
00164     //  fHashModulus
00165     //      The modulus used for this hash table, to hash the keys. This is
00166     //      also the number of elements in the bucket list.
00167     //
00168     //  fHash
00169     //      The hasher for the key data type.
00170     // -----------------------------------------------------------------------
00171     ValueHashTableBucketElem<TVal>** fBucketList;
00172     unsigned int                    fHashModulus;
00173     HashBase*                       fHash;
00174 };
00175 
00176 
00177 
00178 //
00179 //  An enumerator for a value array. It derives from the basic enumerator
00180 //  class, so that value vectors can be generically enumerated.
00181 //
00182 template <class TVal> class ValueHashTableOfEnumerator : public XMLEnumerator<TVal>
00183 {
00184 public :
00185     // -----------------------------------------------------------------------
00186     //  Constructors and Destructor
00187     // -----------------------------------------------------------------------
00188     ValueHashTableOfEnumerator(ValueHashTableOf<TVal>* const toEnum, const bool adopt = false);
00189     ~ValueHashTableOfEnumerator();
00190 
00191 
00192     // -----------------------------------------------------------------------
00193     //  Enum interface
00194     // -----------------------------------------------------------------------
00195     bool hasMoreElements() const;
00196     TVal& nextElement();
00197     void Reset();
00198 
00199 
00200 private :
00201     // -----------------------------------------------------------------------
00202     //  Private methods
00203     // -----------------------------------------------------------------------
00204     void findNext();
00205 
00206 
00207     // -----------------------------------------------------------------------
00208     //  Data Members
00209     //
00210     //  fAdopted
00211     //      Indicates whether we have adopted the passed vector. If so then
00212     //      we delete the vector when we are destroyed.
00213     //
00214     //  fCurElem
00215     //      This is the current bucket bucket element that we are on.
00216     //
00217     //  fCurHash
00218     //      The is the current hash buck that we are working on. Once we hit
00219     //      the end of the bucket that fCurElem is in, then we have to start
00220     //      working this one up to the next non-empty bucket.
00221     //
00222     //  fToEnum
00223     //      The value array being enumerated.
00224     // -----------------------------------------------------------------------
00225     bool                            fAdopted;
00226     ValueHashTableBucketElem<TVal>* fCurElem;
00227     unsigned int                    fCurHash;
00228     ValueHashTableOf<TVal>*         fToEnum;
00229 };
00230 
00231 #if !defined(XERCES_TMPLSINC)
00232 #include <xercesc/util/ValueHashTableOf.c>
00233 #endif
00234 
00235 #endif


Copyright © 2000 The Apache Software Foundation. All Rights Reserved.