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: RefHash3KeysIdPool.hpp,v $ 00059 * Revision 1.1 2002/05/11 21:09:05 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:04 tng 00074 * The "hash" argument clashes with STL hash. Fixed by Pei Yong Zhang. 00075 * 00076 * Revision 1.2 2001/05/11 13:26:29 tng 00077 * Copyright update. 00078 * 00079 * Revision 1.1 2001/03/21 21:56:12 tng 00080 * Schema: Add Schema Grammar, Schema Validator, and split the DTDValidator into DTDValidator, DTDScanner, and DTDGrammar. 00081 * 00082 */ 00083 00084 00085 #if !defined(REFHASH3KEYSIDPOOL_HPP) 00086 #define REFHASH3KEYSIDPOOL_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 a combination of RefHash2KeyTableOf (with an additional integer as key3) 00102 // and NameIdPool with an id as index 00103 00104 // 00105 // Forward declare the enumerator so he can be our friend. Can you say 00106 // friend? Sure... 00107 // 00108 template <class TVal> class RefHash3KeysIdPoolEnumerator; 00109 template <class TVal> struct RefHash3KeysTableBucketElem; 00110 00111 00112 // 00113 // This should really be a nested class, but some of the compilers we 00114 // have to support cannot deal with that! 00115 // 00116 template <class TVal> struct RefHash3KeysTableBucketElem 00117 { 00118 RefHash3KeysTableBucketElem( 00119 void* key1 00120 , int key2 00121 , int key3 00122 , TVal* const value 00123 , RefHash3KeysTableBucketElem<TVal>* next) : 00124 fData(value) 00125 , fNext(next) 00126 , fKey1(key1) 00127 , fKey2(key2) 00128 , fKey3(key3) 00129 { 00130 } 00131 00132 TVal* fData; 00133 RefHash3KeysTableBucketElem<TVal>* fNext; 00134 void* fKey1; 00135 int fKey2; 00136 int fKey3; 00137 }; 00138 00139 00140 template <class TVal> class RefHash3KeysIdPool 00141 { 00142 public: 00143 // ----------------------------------------------------------------------- 00144 // Constructors and Destructor 00145 // ----------------------------------------------------------------------- 00146 // backwards compatability - default hasher is HashXMLCh 00147 RefHash3KeysIdPool 00148 ( 00149 const unsigned int modulus 00150 , const unsigned int initSize = 128 00151 ); 00152 00153 // backwards compatability - default hasher is HashXMLCh 00154 RefHash3KeysIdPool 00155 ( 00156 const unsigned int modulus 00157 , const bool adoptElems 00158 , const unsigned int initSize = 128 00159 ); 00160 00161 // if a hash function is passed in, it will be deleted when the hashtable is deleted. 00162 // use a new instance of the hasher class for each hashtable, otherwise one hashtable 00163 // may delete the hasher of a different hashtable if both use the same hasher. 00164 RefHash3KeysIdPool 00165 ( 00166 const unsigned int modulus 00167 , const bool adoptElems, HashBase* hashBase 00168 , const unsigned int initSize = 128 00169 ); 00170 00171 ~RefHash3KeysIdPool(); 00172 00173 // ----------------------------------------------------------------------- 00174 // Element management 00175 // ----------------------------------------------------------------------- 00176 bool isEmpty() const; 00177 bool containsKey(const void* const key1, const int key2, const int key3) const; 00178 void removeAll(); 00179 00180 00181 // ----------------------------------------------------------------------- 00182 // Getters 00183 // ----------------------------------------------------------------------- 00184 TVal* getByKey(const void* const key1, const int key2, const int key3); 00185 const TVal* getByKey(const void* const key1, const int key2, const int key3) const; 00186 00187 TVal* getById(const unsigned elemId); 00188 const TVal* getById(const unsigned elemId) const; 00189 00190 // ----------------------------------------------------------------------- 00191 // Putters 00192 // ----------------------------------------------------------------------- 00193 unsigned int put(void* key1, int key2, int key3, TVal* const valueToAdopt); 00194 00195 00196 private : 00197 // ----------------------------------------------------------------------- 00198 // Declare our friends 00199 // ----------------------------------------------------------------------- 00200 friend class RefHash3KeysIdPoolEnumerator<TVal>; 00201 00202 private: 00203 00204 // ----------------------------------------------------------------------- 00205 // Private methods 00206 // ----------------------------------------------------------------------- 00207 RefHash3KeysTableBucketElem<TVal>* findBucketElem(const void* const key1, const int key2, const int key3, unsigned int& hashVal); 00208 const RefHash3KeysTableBucketElem<TVal>* findBucketElem(const void* const key1, const int key2, const int key3, unsigned int& hashVal) const; 00209 void initialize(const unsigned int modulus); 00210 00211 00212 // ----------------------------------------------------------------------- 00213 // Data members 00214 // 00215 // fAdoptedElems 00216 // Indicates whether the values added are adopted or just referenced. 00217 // If adopted, then they are deleted when they are removed from the 00218 // hash table. 00219 // 00220 // fBucketList 00221 // This is the array that contains the heads of all of the list 00222 // buckets, one for each possible hash value. 00223 // 00224 // fHashModulus 00225 // The modulus used for this hash table, to hash the keys. This is 00226 // also the number of elements in the bucket list. 00227 // 00228 // fHash 00229 // The hasher for the key1 data type. 00230 // 00231 // fIdPtrs 00232 // fIdPtrsCount 00233 // This is the array of pointers to the bucket elements in order of 00234 // their assigned ids. So taking id N and referencing this array 00235 // gives you the element with that id. The count field indicates 00236 // the current size of this list. When fIdCounter+1 reaches this 00237 // value the list must be expanded. 00238 // 00239 // fIdCounter 00240 // This is used to give out unique ids to added elements. It starts 00241 // at zero (which means empty), and is bumped up for each newly added 00242 // element. So the first element is 1, the next is 2, etc... This 00243 // means that this value is set to the top index of the fIdPtrs array. 00244 // ----------------------------------------------------------------------- 00245 bool fAdoptedElems; 00246 RefHash3KeysTableBucketElem<TVal>** fBucketList; 00247 unsigned int fHashModulus; 00248 HashBase* fHash; 00249 TVal** fIdPtrs; 00250 unsigned int fIdPtrsCount; 00251 unsigned int fIdCounter; 00252 }; 00253 00254 00255 00256 // 00257 // An enumerator for a value array. It derives from the basic enumerator 00258 // class, so that value vectors can be generically enumerated. 00259 // 00260 template <class TVal> class RefHash3KeysIdPoolEnumerator : public XMLEnumerator<TVal> 00261 { 00262 public : 00263 // ----------------------------------------------------------------------- 00264 // Constructors and Destructor 00265 // ----------------------------------------------------------------------- 00266 RefHash3KeysIdPoolEnumerator(RefHash3KeysIdPool<TVal>* const toEnum, const bool adopt = false); 00267 virtual ~RefHash3KeysIdPoolEnumerator(); 00268 00269 00270 // ----------------------------------------------------------------------- 00271 // Enum interface 00272 // ----------------------------------------------------------------------- 00273 bool hasMoreElements() const; 00274 TVal& nextElement(); 00275 void Reset(); 00276 00277 00278 private : 00279 // ----------------------------------------------------------------------- 00280 // Data Members 00281 // fAdoptedElems 00282 // Indicates whether the values added are adopted or just referenced. 00283 // If adopted, then they are deleted when they are removed from the 00284 // hash table 00285 // 00286 // fCurIndex 00287 // This is the current index into the pool's id mapping array. This 00288 // is now we enumerate it. 00289 // 00290 // fToEnum 00291 // The name id pool that is being enumerated. 00292 // ----------------------------------------------------------------------- 00293 bool fAdoptedElems; 00294 unsigned int fCurIndex; 00295 RefHash3KeysIdPool<TVal>* fToEnum; 00296 }; 00297 00298 #if !defined(XERCES_TMPLSINC) 00299 #include <xercesc/util/RefHash3KeysIdPool.c> 00300 #endif 00301 00302 #endif