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