1 | /*
|
---|
2 | * Copyright 2001,2004 The Apache Software Foundation.
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | */
|
---|
16 |
|
---|
17 | /*
|
---|
18 | * $Log: RefHash3KeysIdPool.hpp,v $
|
---|
19 | * Revision 1.11 2004/09/08 13:56:22 peiyongz
|
---|
20 | * Apache License Version 2.0
|
---|
21 | *
|
---|
22 | * Revision 1.10 2004/03/01 15:03:08 peiyongz
|
---|
23 | * new getter: getHashModulus
|
---|
24 | *
|
---|
25 | * Revision 1.9 2004/01/29 11:48:46 cargilld
|
---|
26 | * Code cleanup changes to get rid of various compiler diagnostic messages.
|
---|
27 | *
|
---|
28 | * Revision 1.8 2003/12/17 00:18:35 cargilld
|
---|
29 | * Update to memory management so that the static memory manager (one used to call Initialize) is only for static data.
|
---|
30 | *
|
---|
31 | * Revision 1.7 2003/11/03 22:00:31 peiyongz
|
---|
32 | * RefHashTable-like enumeration accessing added
|
---|
33 | *
|
---|
34 | * Revision 1.6 2003/10/29 16:17:48 peiyongz
|
---|
35 | * size() added
|
---|
36 | *
|
---|
37 | * Revision 1.5 2003/05/16 06:01:52 knoaman
|
---|
38 | * Partial implementation of the configurable memory manager.
|
---|
39 | *
|
---|
40 | * Revision 1.4 2003/05/15 19:04:35 knoaman
|
---|
41 | * Partial implementation of the configurable memory manager.
|
---|
42 | *
|
---|
43 | * Revision 1.3 2002/11/04 15:22:04 tng
|
---|
44 | * C++ Namespace Support.
|
---|
45 | *
|
---|
46 | * Revision 1.2 2002/06/12 17:15:12 tng
|
---|
47 | * Remove redundant include header file.
|
---|
48 | *
|
---|
49 | * Revision 1.1.1.1 2002/02/01 22:22:12 peiyongz
|
---|
50 | * sane_include
|
---|
51 | *
|
---|
52 | * Revision 1.4 2001/12/22 01:06:08 jasons
|
---|
53 | * Made the destructors virtual for:
|
---|
54 | *
|
---|
55 | * * ~RefHash2KeysTableOfEnumerator
|
---|
56 | * * ~RefHash3KeysIdPoolEnumerator
|
---|
57 | *
|
---|
58 | * This fixes bug #5514
|
---|
59 | *
|
---|
60 | * Revision 1.3 2001/06/04 13:45:04 tng
|
---|
61 | * The "hash" argument clashes with STL hash. Fixed by Pei Yong Zhang.
|
---|
62 | *
|
---|
63 | * Revision 1.2 2001/05/11 13:26:29 tng
|
---|
64 | * Copyright update.
|
---|
65 | *
|
---|
66 | * Revision 1.1 2001/03/21 21:56:12 tng
|
---|
67 | * Schema: Add Schema Grammar, Schema Validator, and split the DTDValidator into DTDValidator, DTDScanner, and DTDGrammar.
|
---|
68 | *
|
---|
69 | */
|
---|
70 |
|
---|
71 |
|
---|
72 | #if !defined(REFHASH3KEYSIDPOOL_HPP)
|
---|
73 | #define REFHASH3KEYSIDPOOL_HPP
|
---|
74 |
|
---|
75 |
|
---|
76 | #include <xercesc/util/HashBase.hpp>
|
---|
77 | #include <xercesc/util/IllegalArgumentException.hpp>
|
---|
78 | #include <xercesc/util/NoSuchElementException.hpp>
|
---|
79 | #include <xercesc/util/RuntimeException.hpp>
|
---|
80 | #include <xercesc/util/PlatformUtils.hpp>
|
---|
81 | #include <xercesc/util/XMLString.hpp>
|
---|
82 | #include <xercesc/util/HashXMLCh.hpp>
|
---|
83 |
|
---|
84 | XERCES_CPP_NAMESPACE_BEGIN
|
---|
85 |
|
---|
86 | // This hash table is a combination of RefHash2KeyTableOf (with an additional integer as key3)
|
---|
87 | // and NameIdPool with an id as index
|
---|
88 |
|
---|
89 | //
|
---|
90 | // Forward declare the enumerator so he can be our friend. Can you say
|
---|
91 | // friend? Sure...
|
---|
92 | //
|
---|
93 | template <class TVal> class RefHash3KeysIdPoolEnumerator;
|
---|
94 | template <class TVal> struct RefHash3KeysTableBucketElem;
|
---|
95 |
|
---|
96 |
|
---|
97 | //
|
---|
98 | // This should really be a nested class, but some of the compilers we
|
---|
99 | // have to support cannot deal with that!
|
---|
100 | //
|
---|
101 | template <class TVal> struct RefHash3KeysTableBucketElem : public XMemory
|
---|
102 | {
|
---|
103 | RefHash3KeysTableBucketElem(
|
---|
104 | void* key1
|
---|
105 | , int key2
|
---|
106 | , int key3
|
---|
107 | , TVal* const value
|
---|
108 | , RefHash3KeysTableBucketElem<TVal>* next) :
|
---|
109 | fData(value)
|
---|
110 | , fNext(next)
|
---|
111 | , fKey1(key1)
|
---|
112 | , fKey2(key2)
|
---|
113 | , fKey3(key3)
|
---|
114 | {
|
---|
115 | }
|
---|
116 |
|
---|
117 | RefHash3KeysTableBucketElem() {};
|
---|
118 |
|
---|
119 | TVal* fData;
|
---|
120 | RefHash3KeysTableBucketElem<TVal>* fNext;
|
---|
121 | void* fKey1;
|
---|
122 | int fKey2;
|
---|
123 | int fKey3;
|
---|
124 |
|
---|
125 | private:
|
---|
126 | // -----------------------------------------------------------------------
|
---|
127 | // Unimplemented constructors and operators
|
---|
128 | // -----------------------------------------------------------------------
|
---|
129 | RefHash3KeysTableBucketElem(const RefHash3KeysTableBucketElem<TVal>&);
|
---|
130 | RefHash3KeysTableBucketElem<TVal>& operator=(const RefHash3KeysTableBucketElem<TVal>&);
|
---|
131 | };
|
---|
132 |
|
---|
133 |
|
---|
134 | template <class TVal> class RefHash3KeysIdPool : public XMemory
|
---|
135 | {
|
---|
136 | public:
|
---|
137 | // -----------------------------------------------------------------------
|
---|
138 | // Constructors and Destructor
|
---|
139 | // -----------------------------------------------------------------------
|
---|
140 | // backwards compatability - default hasher is HashXMLCh
|
---|
141 | RefHash3KeysIdPool
|
---|
142 | (
|
---|
143 | const unsigned int modulus
|
---|
144 | , const unsigned int initSize = 128
|
---|
145 | , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
|
---|
146 | );
|
---|
147 |
|
---|
148 | // backwards compatability - default hasher is HashXMLCh
|
---|
149 | RefHash3KeysIdPool
|
---|
150 | (
|
---|
151 | const unsigned int modulus
|
---|
152 | , const bool adoptElems
|
---|
153 | , const unsigned int initSize = 128
|
---|
154 | , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
|
---|
155 | );
|
---|
156 |
|
---|
157 | // if a hash function is passed in, it will be deleted when the hashtable is deleted.
|
---|
158 | // use a new instance of the hasher class for each hashtable, otherwise one hashtable
|
---|
159 | // may delete the hasher of a different hashtable if both use the same hasher.
|
---|
160 | RefHash3KeysIdPool
|
---|
161 | (
|
---|
162 | const unsigned int modulus
|
---|
163 | , const bool adoptElems
|
---|
164 | , HashBase* hashBase
|
---|
165 | , const unsigned int initSize = 128
|
---|
166 | , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
|
---|
167 | );
|
---|
168 |
|
---|
169 | ~RefHash3KeysIdPool();
|
---|
170 |
|
---|
171 | // -----------------------------------------------------------------------
|
---|
172 | // Element management
|
---|
173 | // -----------------------------------------------------------------------
|
---|
174 | bool isEmpty() const;
|
---|
175 | bool containsKey(const void* const key1, const int key2, const int key3) const;
|
---|
176 | void removeAll();
|
---|
177 |
|
---|
178 |
|
---|
179 | // -----------------------------------------------------------------------
|
---|
180 | // Getters
|
---|
181 | // -----------------------------------------------------------------------
|
---|
182 | TVal* getByKey(const void* const key1, const int key2, const int key3);
|
---|
183 | const TVal* getByKey(const void* const key1, const int key2, const int key3) const;
|
---|
184 |
|
---|
185 | TVal* getById(const unsigned elemId);
|
---|
186 | const TVal* getById(const unsigned elemId) const;
|
---|
187 |
|
---|
188 | MemoryManager* getMemoryManager() const;
|
---|
189 | unsigned int getHashModulus() const;
|
---|
190 |
|
---|
191 | // -----------------------------------------------------------------------
|
---|
192 | // Putters
|
---|
193 | // -----------------------------------------------------------------------
|
---|
194 | unsigned int put(void* key1, int key2, int key3, TVal* const valueToAdopt);
|
---|
195 |
|
---|
196 |
|
---|
197 | private :
|
---|
198 | // -----------------------------------------------------------------------
|
---|
199 | // Declare our friends
|
---|
200 | // -----------------------------------------------------------------------
|
---|
201 | friend class RefHash3KeysIdPoolEnumerator<TVal>;
|
---|
202 |
|
---|
203 | private:
|
---|
204 | // -----------------------------------------------------------------------
|
---|
205 | // Unimplemented constructors and operators
|
---|
206 | // -----------------------------------------------------------------------
|
---|
207 | RefHash3KeysIdPool(const RefHash3KeysIdPool<TVal>&);
|
---|
208 | RefHash3KeysIdPool<TVal>& operator=(const RefHash3KeysIdPool<TVal>&);
|
---|
209 |
|
---|
210 | // -----------------------------------------------------------------------
|
---|
211 | // Private methods
|
---|
212 | // -----------------------------------------------------------------------
|
---|
213 | RefHash3KeysTableBucketElem<TVal>* findBucketElem(const void* const key1, const int key2, const int key3, unsigned int& hashVal);
|
---|
214 | const RefHash3KeysTableBucketElem<TVal>* findBucketElem(const void* const key1, const int key2, const int key3, unsigned int& hashVal) const;
|
---|
215 | void initialize(const unsigned int modulus);
|
---|
216 |
|
---|
217 |
|
---|
218 | // -----------------------------------------------------------------------
|
---|
219 | // Data members
|
---|
220 | //
|
---|
221 | // fAdoptedElems
|
---|
222 | // Indicates whether the values added are adopted or just referenced.
|
---|
223 | // If adopted, then they are deleted when they are removed from the
|
---|
224 | // hash table.
|
---|
225 | //
|
---|
226 | // fBucketList
|
---|
227 | // This is the array that contains the heads of all of the list
|
---|
228 | // buckets, one for each possible hash value.
|
---|
229 | //
|
---|
230 | // fHashModulus
|
---|
231 | // The modulus used for this hash table, to hash the keys. This is
|
---|
232 | // also the number of elements in the bucket list.
|
---|
233 | //
|
---|
234 | // fHash
|
---|
235 | // The hasher for the key1 data type.
|
---|
236 | //
|
---|
237 | // fIdPtrs
|
---|
238 | // fIdPtrsCount
|
---|
239 | // This is the array of pointers to the bucket elements in order of
|
---|
240 | // their assigned ids. So taking id N and referencing this array
|
---|
241 | // gives you the element with that id. The count field indicates
|
---|
242 | // the current size of this list. When fIdCounter+1 reaches this
|
---|
243 | // value the list must be expanded.
|
---|
244 | //
|
---|
245 | // fIdCounter
|
---|
246 | // This is used to give out unique ids to added elements. It starts
|
---|
247 | // at zero (which means empty), and is bumped up for each newly added
|
---|
248 | // element. So the first element is 1, the next is 2, etc... This
|
---|
249 | // means that this value is set to the top index of the fIdPtrs array.
|
---|
250 | // -----------------------------------------------------------------------
|
---|
251 | MemoryManager* fMemoryManager;
|
---|
252 | bool fAdoptedElems;
|
---|
253 | RefHash3KeysTableBucketElem<TVal>** fBucketList;
|
---|
254 | unsigned int fHashModulus;
|
---|
255 | HashBase* fHash;
|
---|
256 | TVal** fIdPtrs;
|
---|
257 | unsigned int fIdPtrsCount;
|
---|
258 | unsigned int fIdCounter;
|
---|
259 | };
|
---|
260 |
|
---|
261 |
|
---|
262 |
|
---|
263 | //
|
---|
264 | // An enumerator for a value array. It derives from the basic enumerator
|
---|
265 | // class, so that value vectors can be generically enumerated.
|
---|
266 | //
|
---|
267 | template <class TVal> class RefHash3KeysIdPoolEnumerator : public XMLEnumerator<TVal>, public XMemory
|
---|
268 | {
|
---|
269 | public :
|
---|
270 | // -----------------------------------------------------------------------
|
---|
271 | // Constructors and Destructor
|
---|
272 | // -----------------------------------------------------------------------
|
---|
273 | RefHash3KeysIdPoolEnumerator(RefHash3KeysIdPool<TVal>* const toEnum
|
---|
274 | , const bool adopt = false
|
---|
275 | , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
|
---|
276 | virtual ~RefHash3KeysIdPoolEnumerator();
|
---|
277 |
|
---|
278 | RefHash3KeysIdPoolEnumerator(const RefHash3KeysIdPoolEnumerator<TVal>&);
|
---|
279 | // -----------------------------------------------------------------------
|
---|
280 | // Enum interface
|
---|
281 | // -----------------------------------------------------------------------
|
---|
282 | bool hasMoreElements() const;
|
---|
283 | TVal& nextElement();
|
---|
284 | void Reset();
|
---|
285 | int size() const;
|
---|
286 |
|
---|
287 | // -----------------------------------------------------------------------
|
---|
288 | // New interface
|
---|
289 | // -----------------------------------------------------------------------
|
---|
290 | void resetKey();
|
---|
291 | void nextElementKey(void*&, int&, int&);
|
---|
292 | bool hasMoreKeys() const;
|
---|
293 |
|
---|
294 | private :
|
---|
295 | // -----------------------------------------------------------------------
|
---|
296 | // Unimplemented constructors and operators
|
---|
297 | // -----------------------------------------------------------------------
|
---|
298 | RefHash3KeysIdPoolEnumerator<TVal>& operator=(const RefHash3KeysIdPoolEnumerator<TVal>&);
|
---|
299 |
|
---|
300 | // -----------------------------------------------------------------------
|
---|
301 | // Private methods
|
---|
302 | // -----------------------------------------------------------------------
|
---|
303 | void findNext();
|
---|
304 |
|
---|
305 | // -----------------------------------------------------------------------
|
---|
306 | // Data Members
|
---|
307 | // fAdoptedElems
|
---|
308 | // Indicates whether the values added are adopted or just referenced.
|
---|
309 | // If adopted, then they are deleted when they are removed from the
|
---|
310 | // hash table
|
---|
311 | //
|
---|
312 | // fCurIndex
|
---|
313 | // This is the current index into the pool's id mapping array. This
|
---|
314 | // is now we enumerate it.
|
---|
315 | //
|
---|
316 | // fToEnum
|
---|
317 | // The name id pool that is being enumerated.
|
---|
318 | // -----------------------------------------------------------------------
|
---|
319 | bool fAdoptedElems;
|
---|
320 | unsigned int fCurIndex;
|
---|
321 | RefHash3KeysIdPool<TVal>* fToEnum;
|
---|
322 | RefHash3KeysTableBucketElem<TVal>* fCurElem;
|
---|
323 | unsigned int fCurHash;
|
---|
324 | MemoryManager* const fMemoryManager;
|
---|
325 | };
|
---|
326 |
|
---|
327 | XERCES_CPP_NAMESPACE_END
|
---|
328 |
|
---|
329 | #if !defined(XERCES_TMPLSINC)
|
---|
330 | #include <xercesc/util/RefHash3KeysIdPool.c>
|
---|
331 | #endif
|
---|
332 |
|
---|
333 | #endif
|
---|