00001 /* 00002 * The Apache Software License, Version 1.1 00003 * 00004 * Copyright (c) 2001-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) 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: IDDeepNodeListPool.hpp,v $ 00059 * Revision 1.1 2002/05/11 20:07:11 bhavani 00060 * CR#CR062582# adding xercesc 1.7 file 00061 * 00062 * Revision 1.2 2002/02/04 21:50:38 tng 00063 * [Bug 6114] Memory leaks on iDOM getElementsByTagName(). 00064 * 00065 * Revision 1.1.1.1 2002/02/01 22:21:53 peiyongz 00066 * sane_include 00067 * 00068 * Revision 1.1 2001/06/04 14:55:32 tng 00069 * IDOM: Add IRange and IDeepNodeList Support. 00070 * 00071 */ 00072 00073 00074 #if !defined(IDDEEPNODELISTPOOL_HPP) 00075 #define IDDEEPNODELISTPOOL_HPP 00076 00077 00078 #include <xercesc/util/XercesDefs.hpp> 00079 #include <xercesc/util/KeyValuePair.hpp> 00080 #include <xercesc/util/HashBase.hpp> 00081 #include <xercesc/util/IllegalArgumentException.hpp> 00082 #include <xercesc/util/NoSuchElementException.hpp> 00083 #include <xercesc/util/RuntimeException.hpp> 00084 #include <xercesc/util/XMLExceptMsgs.hpp> 00085 #include <xercesc/util/XMLEnumerator.hpp> 00086 #include <xercesc/util/XMLString.hpp> 00087 #include <xercesc/util/HashBase.hpp> 00088 #include <xercesc/util/HashXMLCh.hpp> 00089 #include <xercesc/util/HashPtr.hpp> 00090 00091 // This hash table is modified from RefHash3KeysIdPool with first key as object ptr (IDOM_Node), 00092 // second and third keys are both XMLCh* string 00093 00094 template <class TVal> struct IDDeepNodeListPoolTableBucketElem; 00095 00096 00097 // 00098 // This should really be a nested class, but some of the compilers we 00099 // have to support cannot deal with that! 00100 // 00101 template <class TVal> struct IDDeepNodeListPoolTableBucketElem 00102 { 00103 IDDeepNodeListPoolTableBucketElem( 00104 void* key1 00105 , XMLCh* key2 00106 , XMLCh* key3 00107 , TVal* const value 00108 , IDDeepNodeListPoolTableBucketElem<TVal>* next) : 00109 fData(value) 00110 , fNext(next) 00111 , fKey1(key1) 00112 , fKey2(0) 00113 , fKey3(0) 00114 { 00115 if (key2) 00116 fKey2 = XMLString::replicate(key2); 00117 00118 if (key3) 00119 fKey3 = XMLString::replicate(key3); 00120 } 00121 00122 TVal* fData; 00123 IDDeepNodeListPoolTableBucketElem<TVal>* fNext; 00124 void* fKey1; 00125 XMLCh* fKey2; 00126 XMLCh* fKey3; 00127 }; 00128 00129 00130 template <class TVal> class IDDeepNodeListPool 00131 { 00132 public: 00133 // ----------------------------------------------------------------------- 00134 // Constructors and Destructor 00135 // ----------------------------------------------------------------------- 00136 // backwards compatability - default hasher is HashXMLCh 00137 IDDeepNodeListPool 00138 ( 00139 const unsigned int modulus 00140 , const unsigned int initSize = 128 00141 ); 00142 00143 // backwards compatability - default hasher is HashXMLCh 00144 IDDeepNodeListPool 00145 ( 00146 const unsigned int modulus 00147 , const bool adoptElems 00148 , const unsigned int initSize = 128 00149 ); 00150 00151 // if a hash function is passed in, it will be deleted when the hashtable is deleted. 00152 // use a new instance of the hasher class for each hashtable, otherwise one hashtable 00153 // may delete the hasher of a different hashtable if both use the same hasher. 00154 IDDeepNodeListPool 00155 ( 00156 const unsigned int modulus 00157 , const bool adoptElems, HashBase* hash 00158 , const unsigned int initSize = 128 00159 ); 00160 00161 ~IDDeepNodeListPool(); 00162 00163 // ----------------------------------------------------------------------- 00164 // Element management 00165 // ----------------------------------------------------------------------- 00166 bool isEmpty() const; 00167 bool containsKey(const void* const key1, const XMLCh* const key2, const XMLCh* const key3) const; 00168 void removeAll(); 00169 void cleanup(); 00170 00171 00172 // ----------------------------------------------------------------------- 00173 // Getters 00174 // ----------------------------------------------------------------------- 00175 TVal* getByKey(const void* const key1, const XMLCh* const key2, const XMLCh* const key3); 00176 const TVal* getByKey(const void* const key1, const XMLCh* const key2, const XMLCh* const key3) const; 00177 00178 TVal* getById(const unsigned elemId); 00179 const TVal* getById(const unsigned elemId) const; 00180 00181 // ----------------------------------------------------------------------- 00182 // Putters 00183 // ----------------------------------------------------------------------- 00184 unsigned int put(void* key1, XMLCh* key2, XMLCh* key3, TVal* const valueToAdopt); 00185 00186 private: 00187 00188 // ----------------------------------------------------------------------- 00189 // Private methods 00190 // ----------------------------------------------------------------------- 00191 IDDeepNodeListPoolTableBucketElem<TVal>* findBucketElem(const void* const key1, const XMLCh* const key2, const XMLCh* const key3, unsigned int& hashVal); 00192 const IDDeepNodeListPoolTableBucketElem<TVal>* findBucketElem(const void* const key1, const XMLCh* const key2, const XMLCh* const key3, unsigned int& hashVal) const; 00193 void initialize(const unsigned int modulus); 00194 00195 00196 // ----------------------------------------------------------------------- 00197 // Data members 00198 // 00199 // fAdoptedElems 00200 // Indicates whether the values added are adopted or just referenced. 00201 // If adopted, then they are deleted when they are removed from the 00202 // hash table. 00203 // 00204 // fBucketList 00205 // This is the array that contains the heads of all of the list 00206 // buckets, one for each possible hash value. 00207 // 00208 // fHashModulus 00209 // The modulus used for this hash table, to hash the keys. This is 00210 // also the number of elements in the bucket list. 00211 // 00212 // fHash 00213 // The hasher for the key1 data type. 00214 // 00215 // fIdPtrs 00216 // fIdPtrsCount 00217 // This is the array of pointers to the bucket elements in order of 00218 // their assigned ids. So taking id N and referencing this array 00219 // gives you the element with that id. The count field indicates 00220 // the current size of this list. When fIdCounter+1 reaches this 00221 // value the list must be expanded. 00222 // 00223 // fIdCounter 00224 // This is used to give out unique ids to added elements. It starts 00225 // at zero (which means empty), and is bumped up for each newly added 00226 // element. So the first element is 1, the next is 2, etc... This 00227 // means that this value is set to the top index of the fIdPtrs array. 00228 // ----------------------------------------------------------------------- 00229 bool fAdoptedElems; 00230 IDDeepNodeListPoolTableBucketElem<TVal>** fBucketList; 00231 unsigned int fHashModulus; 00232 HashBase* fHash; 00233 TVal** fIdPtrs; 00234 unsigned int fIdPtrsCount; 00235 unsigned int fIdCounter; 00236 }; 00237 #if !defined(XERCES_TMPLSINC) 00238 #include <xercesc/idom/IDDeepNodeListPool.c> 00239 #endif 00240 00241 #endif