00001 /*
00002 * The Apache Software License, Version 1.1
00003 *
00004 * Copyright (c) 1999-2000 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 * $Log: RefHashTableOf.hpp,v $
00059 * Revision 1.5 2000/03/02 19:54:44 roddey
00060 * This checkin includes many changes done while waiting for the
00061 * 1.1.0 code to be finished. I can't list them all here, but a list is
00062 * available elsewhere.
00063 *
00064 * Revision 1.4 2000/02/24 20:05:25 abagchi
00065 * Swat for removing Log from API docs
00066 *
00067 * Revision 1.3 2000/02/06 07:48:03 rahulj
00068 * Year 2K copyright swat.
00069 *
00070 * Revision 1.2 1999/12/18 00:18:10 roddey
00071 * More changes to support the new, completely orthagonal support for
00072 * intrinsic encodings.
00073 *
00074 * Revision 1.1.1.1 1999/11/09 01:05:01 twl
00075 * Initial checkin
00076 *
00077 * Revision 1.2 1999/11/08 20:45:12 rahul
00078 * Swat for adding in Product name and CVS comment log variable.
00079 *
00080 */
00081
00082
00083 #if !defined(REFHASHTABLEOF_HPP)
00084 #define REFHASHTABLEOF_HPP
00085
00086
00087 #include <util/XercesDefs.hpp>
00088 #include <util/KeyValuePair.hpp>
00089 #include <util/IllegalArgumentException.hpp>
00090 #include <util/NoSuchElementException.hpp>
00091 #include <util/RuntimeException.hpp>
00092 #include <util/XMLExceptMsgs.hpp>
00093 #include <util/XMLEnumerator.hpp>
00094 #include <util/XMLString.hpp>
00095
00096
00097 //
00098 // Forward declare the enumerator so he can be our friend. Can you say
00099 // friend? Sure...
00100 //
00101 template <class TVal> class RefHashTableOfEnumerator;
00102 template <class TVal> struct RefHashTableBucketElem;
00103
00104
00105 //
00106 // This should really be a nested class, but some of the compilers we
00107 // have to support cannot deal with that!
00108 //
00109 template <class TVal> struct RefHashTableBucketElem
00110 {
00111RefHashTableBucketElem(TVal* const value, RefHashTableBucketElem<TVal>* next) :
00112
00113 fData(value)
00114 , fNext(next)
00115 {
00116 }
00117
00118 TVal* fData;
00119RefHashTableBucketElem<TVal>* fNext;
00120 };
00121
00122
00123 template <class TVal> class RefHashTableOf
00124 {
00125 public :
00126 // -----------------------------------------------------------------------
00127 // Constructors and Destructor
00128 // -----------------------------------------------------------------------
00129 RefHashTableOf
00130 (
00131 const unsigned int modulus
00132 , const bool adoptElems = true
00133 );
00134 ~RefHashTableOf();
00135
00136
00137 // -----------------------------------------------------------------------
00138 // Element management
00139 // -----------------------------------------------------------------------
00140 bool isEmpty() const;
00141 bool containsKey(const XMLCh* const key) const;
00142 void removeKey(const XMLCh* const key);
00143 void removeAll();
00144
00145
00146 // -----------------------------------------------------------------------
00147 // Getters
00148 // -----------------------------------------------------------------------
00149 TVal* get(const XMLCh* const key);
00150 const TVal* get(const XMLCh* const key) const;
00151
00152
00153 // -----------------------------------------------------------------------
00154 // Putters
00155 // -----------------------------------------------------------------------
00156 void put(TVal* const valueToAdopt);
00157
00158
00159 private :
00160 // -----------------------------------------------------------------------
00161 // Declare our friends
00162 // -----------------------------------------------------------------------
00163 friend class RefHashTableOfEnumerator<TVal>;
00164
00165 private:
00166
00167 // -----------------------------------------------------------------------
00168 // Private methods
00169 // -----------------------------------------------------------------------
00170 RefHashTableBucketElem<TVal>* findBucketElem
00171 (
00172 const XMLCh* const key,
00173 unsigned int& hashVal
00174 );
00175 const RefHashTableBucketElem<TVal>* findBucketElem
00176 (
00177 const XMLCh* const key
00178 , unsigned int& hashVal
00179 ) const;
00180 void removeBucketElem(const XMLCh* const key, unsigned int& hashVal);
00181
00182
00183 // -----------------------------------------------------------------------
00184 // Data members
00185 //
00186 // fAdoptedElems
00187 // Indicates whether the values added are adopted or just referenced.
00188 // If adopted, then they are deleted when they are removed from the
00189 // hash table.
00190 //
00191 // fBucketList
00192 // This is the array that contains the heads of all of the list
00193 // buckets, one for each possible hash value.
00194 //
00195 // fHashModulus
00196 // The modulus used for this hash table, to hash the keys. This is
00197 // also the number of elements in the bucket list.
00198 // -----------------------------------------------------------------------
00199 bool fAdoptedElems;
00200 RefHashTableBucketElem<TVal>** fBucketList;
00201 unsigned int fHashModulus;
00202 };
00203
00204
00205
00206 //
00207 // An enumerator for a value array. It derives from the basic enumerator
00208 // class, so that value vectors can be generically enumerated.
00209 //
00210 template <class TVal> class RefHashTableOfEnumerator : public XMLEnumerator<TVal>
00211 {
00212 public :
00213 // -----------------------------------------------------------------------
00214 // Constructors and Destructor
00215 // -----------------------------------------------------------------------
00216 RefHashTableOfEnumerator
00217 (
00218 RefHashTableOf<TVal>* const toEnum
00219 , const bool adopt = false
00220 );
00221 ~RefHashTableOfEnumerator();
00222
00223
00224 // -----------------------------------------------------------------------
00225 // Enum interface
00226 // -----------------------------------------------------------------------
00227 bool hasMoreElements() const;
00228 TVal& nextElement();
00229 void Reset();
00230
00231
00232 private :
00233 // -----------------------------------------------------------------------
00234 // Private methods
00235 // -----------------------------------------------------------------------
00236 void findNext();
00237
00238
00239 // -----------------------------------------------------------------------
00240 // Data Members
00241 //
00242 // fAdopted
00243 // Indicates whether we have adopted the passed vector. If so then
00244 // we delete the vector when we are destroyed.
00245 //
00246 // fCurElem
00247 // This is the current bucket bucket element that we are on.
00248 //
00249 // fCurHash
00250 // The is the current hash buck that we are working on. Once we hit
00251 // the end of the bucket that fCurElem is in, then we have to start
00252 // working this one up to the next non-empty bucket.
00253 //
00254 // fToEnum
00255 // The value array being enumerated.
00256 // -----------------------------------------------------------------------
00257 bool fAdopted;
00258 RefHashTableBucketElem<TVal>* fCurElem;
00259 unsigned int fCurHash;
00260 RefHashTableOf<TVal>* fToEnum;
00261 };
00262
00263 #if !defined(XERCES_TMPLSINC)
00264 #include <util/RefHashTableOf.c>
00265 #endif
00266
00267 #endif