00001 /* 00002 * The Apache Software License, Version 1.1 00003 * 00004 * Copyright (c) 2001 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) 2001, 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: RefHash2KeysTableOf.hpp,v $ 00059 * Revision 1.1 2002/05/11 21:08:50 bhavani 00060 * CR#CR062582# adding xercesc 1.7 file 00061 * 00062 * Revision 1.1.1.1 2002/02/01 22:22:12 peiyongz 00063 * sane_include 00064 * 00065 * Revision 1.4 2001/12/22 01:06:08 jasons 00066 * Made the destructors virtual for: 00067 * 00068 * * ~RefHash2KeysTableOfEnumerator 00069 * * ~RefHash3KeysIdPoolEnumerator 00070 * 00071 * This fixes bug #5514 00072 * 00073 * Revision 1.3 2001/06/04 13:45:03 tng 00074 * The "hash" argument clashes with STL hash. Fixed by Pei Yong Zhang. 00075 * 00076 * Revision 1.2 2001/05/11 13:26:28 tng 00077 * Copyright update. 00078 * 00079 * Revision 1.1 2001/02/27 18:24:01 tng 00080 * Schema: Add utility RefHash2KeysTableOf. 00081 * 00082 */ 00083 00084 00085 #if !defined(REFHASH2KEYSTABLEOF_HPP) 00086 #define REFHASH2KEYSTABLEOF_HPP 00087 00088 00089 #include <xercesc/util/XercesDefs.hpp> 00090 #include <xercesc/util/KeyValuePair.hpp> 00091 #include <xercesc/util/HashBase.hpp> 00092 #include <xercesc/util/IllegalArgumentException.hpp> 00093 #include <xercesc/util/NoSuchElementException.hpp> 00094 #include <xercesc/util/RuntimeException.hpp> 00095 #include <xercesc/util/XMLExceptMsgs.hpp> 00096 #include <xercesc/util/XMLEnumerator.hpp> 00097 #include <xercesc/util/XMLString.hpp> 00098 #include <xercesc/util/HashBase.hpp> 00099 #include <xercesc/util/HashXMLCh.hpp> 00100 00101 // This hash table is similar to RefHashTableOf with an additional integer as key2 00102 00103 // 00104 // Forward declare the enumerator so he can be our friend. Can you say 00105 // friend? Sure... 00106 // 00107 template <class TVal> class RefHash2KeysTableOfEnumerator; 00108 template <class TVal> struct RefHash2KeysTableBucketElem; 00109 00110 00111 // 00112 // This should really be a nested class, but some of the compilers we 00113 // have to support cannot deal with that! 00114 // 00115 template <class TVal> struct RefHash2KeysTableBucketElem 00116 { 00117 RefHash2KeysTableBucketElem(void* key1, int key2, TVal* const value, RefHash2KeysTableBucketElem<TVal>* next) 00118 : fData(value), fNext(next), fKey1(key1), fKey2(key2) 00119 { 00120 } 00121 00122 TVal* fData; 00123 RefHash2KeysTableBucketElem<TVal>* fNext; 00124 void* fKey1; 00125 int fKey2; 00126 }; 00127 00128 00129 template <class TVal> class RefHash2KeysTableOf 00130 { 00131 public: 00132 // ----------------------------------------------------------------------- 00133 // Constructors and Destructor 00134 // ----------------------------------------------------------------------- 00135 // backwards compatability - default hasher is HashXMLCh 00136 RefHash2KeysTableOf(const unsigned int modulus); 00137 // backwards compatability - default hasher is HashXMLCh 00138 RefHash2KeysTableOf(const unsigned int modulus, const bool adoptElems); 00139 // if a hash function is passed in, it will be deleted when the hashtable is deleted. 00140 // use a new instance of the hasher class for each hashtable, otherwise one hashtable 00141 // may delete the hasher of a different hashtable if both use the same hasher. 00142 RefHash2KeysTableOf(const unsigned int modulus, const bool adoptElems, HashBase* hashBase); 00143 ~RefHash2KeysTableOf(); 00144 00145 00146 // ----------------------------------------------------------------------- 00147 // Element management 00148 // ----------------------------------------------------------------------- 00149 bool isEmpty() const; 00150 bool containsKey(const void* const key1, const int key2) const; 00151 void removeKey(const void* const key1, const int key2); 00152 void removeAll(); 00153 00154 00155 // ----------------------------------------------------------------------- 00156 // Getters 00157 // ----------------------------------------------------------------------- 00158 TVal* get(const void* const key1, const int key2); 00159 const TVal* get(const void* const key1, const int key2) const; 00160 00161 00162 // ----------------------------------------------------------------------- 00163 // Putters 00164 // ----------------------------------------------------------------------- 00165 void put(void* key1, int key2, TVal* const valueToAdopt); 00166 00167 00168 private : 00169 // ----------------------------------------------------------------------- 00170 // Declare our friends 00171 // ----------------------------------------------------------------------- 00172 friend class RefHash2KeysTableOfEnumerator<TVal>; 00173 00174 private: 00175 00176 // ----------------------------------------------------------------------- 00177 // Private methods 00178 // ----------------------------------------------------------------------- 00179 RefHash2KeysTableBucketElem<TVal>* findBucketElem(const void* const key1, const int key2, unsigned int& hashVal); 00180 const RefHash2KeysTableBucketElem<TVal>* findBucketElem(const void* const key1, const int key2, unsigned int& hashVal) const; 00181 void removeBucketElem(const void* const key1, const int key2, unsigned int& hashVal); 00182 void initialize(const unsigned int modulus); 00183 00184 00185 // ----------------------------------------------------------------------- 00186 // Data members 00187 // 00188 // fAdoptedElems 00189 // Indicates whether the values added are adopted or just referenced. 00190 // If adopted, then they are deleted when they are removed from the 00191 // hash table. 00192 // 00193 // fBucketList 00194 // This is the array that contains the heads of all of the list 00195 // buckets, one for each possible hash value. 00196 // 00197 // fHashModulus 00198 // The modulus used for this hash table, to hash the keys. This is 00199 // also the number of elements in the bucket list. 00200 // 00201 // fHash 00202 // The hasher for the key1 data type. 00203 // ----------------------------------------------------------------------- 00204 bool fAdoptedElems; 00205 RefHash2KeysTableBucketElem<TVal>** fBucketList; 00206 unsigned int fHashModulus; 00207 HashBase* fHash; 00208 }; 00209 00210 00211 00212 // 00213 // An enumerator for a value array. It derives from the basic enumerator 00214 // class, so that value vectors can be generically enumerated. 00215 // 00216 template <class TVal> class RefHash2KeysTableOfEnumerator : public XMLEnumerator<TVal> 00217 { 00218 public : 00219 // ----------------------------------------------------------------------- 00220 // Constructors and Destructor 00221 // ----------------------------------------------------------------------- 00222 RefHash2KeysTableOfEnumerator(RefHash2KeysTableOf<TVal>* const toEnum, const bool adopt = false); 00223 virtual ~RefHash2KeysTableOfEnumerator(); 00224 00225 00226 // ----------------------------------------------------------------------- 00227 // Enum interface 00228 // ----------------------------------------------------------------------- 00229 bool hasMoreElements() const; 00230 TVal& nextElement(); 00231 void Reset(); 00232 00233 00234 private : 00235 // ----------------------------------------------------------------------- 00236 // Private methods 00237 // ----------------------------------------------------------------------- 00238 void findNext(); 00239 00240 00241 // ----------------------------------------------------------------------- 00242 // Data Members 00243 // 00244 // fAdopted 00245 // Indicates whether we have adopted the passed vector. If so then 00246 // we delete the vector when we are destroyed. 00247 // 00248 // fCurElem 00249 // This is the current bucket bucket element that we are on. 00250 // 00251 // fCurHash 00252 // The is the current hash buck that we are working on. Once we hit 00253 // the end of the bucket that fCurElem is in, then we have to start 00254 // working this one up to the next non-empty bucket. 00255 // 00256 // fToEnum 00257 // The value array being enumerated. 00258 // ----------------------------------------------------------------------- 00259 bool fAdopted; 00260 RefHash2KeysTableBucketElem<TVal>* fCurElem; 00261 unsigned int fCurHash; 00262 RefHash2KeysTableOf<TVal>* fToEnum; 00263 }; 00264 00265 #if !defined(XERCES_TMPLSINC) 00266 #include <xercesc/util/RefHash2KeysTableOf.c> 00267 #endif 00268 00269 #endif