1 | /*
|
---|
2 | * Copyright 1999-2000,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: NameIdPool.hpp,v $
|
---|
19 | * Revision 1.11 2004/09/23 07:10:13 amassari
|
---|
20 | * Removed const from variable declaration (jira#1259)
|
---|
21 | *
|
---|
22 | * Revision 1.10 2004/09/08 13:56:22 peiyongz
|
---|
23 | * Apache License Version 2.0
|
---|
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/10/29 16:17:48 peiyongz
|
---|
32 | * size() added
|
---|
33 | *
|
---|
34 | * Revision 1.6 2003/05/16 06:01:52 knoaman
|
---|
35 | * Partial implementation of the configurable memory manager.
|
---|
36 | *
|
---|
37 | * Revision 1.5 2003/05/15 19:04:35 knoaman
|
---|
38 | * Partial implementation of the configurable memory manager.
|
---|
39 | *
|
---|
40 | * Revision 1.4 2003/03/07 18:11:54 tng
|
---|
41 | * Return a reference instead of void for operator=
|
---|
42 | *
|
---|
43 | * Revision 1.3 2002/12/04 02:32:43 knoaman
|
---|
44 | * #include cleanup.
|
---|
45 | *
|
---|
46 | * Revision 1.2 2002/11/04 15:22:04 tng
|
---|
47 | * C++ Namespace Support.
|
---|
48 | *
|
---|
49 | * Revision 1.1.1.1 2002/02/01 22:22:11 peiyongz
|
---|
50 | * sane_include
|
---|
51 | *
|
---|
52 | * Revision 1.6 2000/09/09 00:11:48 andyh
|
---|
53 | * Virtual Destructor Patch, submitted by Kirk Wylie
|
---|
54 | *
|
---|
55 | * Revision 1.5 2000/07/19 18:47:26 andyh
|
---|
56 | * More Macintosh port tweaks, submitted by James Berry.
|
---|
57 | *
|
---|
58 | * Revision 1.4 2000/03/02 19:54:42 roddey
|
---|
59 | * This checkin includes many changes done while waiting for the
|
---|
60 | * 1.1.0 code to be finished. I can't list them all here, but a list is
|
---|
61 | * available elsewhere.
|
---|
62 | *
|
---|
63 | * Revision 1.3 2000/02/24 20:05:24 abagchi
|
---|
64 | * Swat for removing Log from API docs
|
---|
65 | *
|
---|
66 | * Revision 1.2 2000/02/06 07:48:02 rahulj
|
---|
67 | * Year 2K copyright swat.
|
---|
68 | *
|
---|
69 | * Revision 1.1.1.1 1999/11/09 01:04:48 twl
|
---|
70 | * Initial checkin
|
---|
71 | *
|
---|
72 | * Revision 1.3 1999/11/08 20:45:10 rahul
|
---|
73 | * Swat for adding in Product name and CVS comment log variable.
|
---|
74 | *
|
---|
75 | */
|
---|
76 |
|
---|
77 |
|
---|
78 | #if !defined(NAMEIDPOOL_HPP)
|
---|
79 | #define NAMEIDPOOL_HPP
|
---|
80 |
|
---|
81 | #include <xercesc/util/XMemory.hpp>
|
---|
82 | #include <xercesc/util/XMLString.hpp>
|
---|
83 | #include <xercesc/util/PlatformUtils.hpp>
|
---|
84 |
|
---|
85 | XERCES_CPP_NAMESPACE_BEGIN
|
---|
86 |
|
---|
87 | //
|
---|
88 | // Forward declare the enumerator so he can be our friend. Can you say
|
---|
89 | // friend? Sure...
|
---|
90 | //
|
---|
91 | template <class TElem> class NameIdPoolEnumerator;
|
---|
92 |
|
---|
93 |
|
---|
94 | //
|
---|
95 | // This class is provided to serve as the basis of many of the pools that
|
---|
96 | // are used by the scanner and validators. They often need to be able to
|
---|
97 | // store objects in such a way that they can be quickly accessed by the
|
---|
98 | // name field of the object, and such that each element added is assigned
|
---|
99 | // a unique id via which it can be accessed almost instantly.
|
---|
100 | //
|
---|
101 | // Object names are enforced as being unique, since that's what all these
|
---|
102 | // pools require. So its effectively a hash table in conjunction with an
|
---|
103 | // array of references into the hash table by id. Ids are assigned such that
|
---|
104 | // id N can be used to get the Nth element from the array of references.
|
---|
105 | // This provides very fast access by id.
|
---|
106 | //
|
---|
107 | // The way these pools are used, elements are never removed except when the
|
---|
108 | // whole thing is flushed. This makes it very easy to maintain the two
|
---|
109 | // access methods in sync.
|
---|
110 | //
|
---|
111 | // For efficiency reasons, the id refererence array is never flushed until
|
---|
112 | // the dtor. This way, it does not have to be regrown every time its reused.
|
---|
113 | //
|
---|
114 | // All elements are assumed to be owned by the pool!
|
---|
115 | //
|
---|
116 | // We have to have a bucket element structure to use to maintain the linked
|
---|
117 | // lists for each bucket. Because some of the compilers we have to support
|
---|
118 | // are totally brain dead, it cannot be a nested class as it should be.
|
---|
119 | //
|
---|
120 | template <class TElem> struct NameIdPoolBucketElem : public XMemory
|
---|
121 | {
|
---|
122 | public :
|
---|
123 | NameIdPoolBucketElem
|
---|
124 | (
|
---|
125 | TElem* const value
|
---|
126 | , NameIdPoolBucketElem<TElem>* const next
|
---|
127 | );
|
---|
128 | ~NameIdPoolBucketElem();
|
---|
129 |
|
---|
130 | TElem* fData;
|
---|
131 | NameIdPoolBucketElem<TElem>* fNext;
|
---|
132 | private:
|
---|
133 | // -----------------------------------------------------------------------
|
---|
134 | // Unimplemented constructors and operators
|
---|
135 | // -----------------------------------------------------------------------
|
---|
136 | NameIdPoolBucketElem(const NameIdPoolBucketElem<TElem>&);
|
---|
137 | NameIdPoolBucketElem<TElem>& operator=(const NameIdPoolBucketElem<TElem>&);
|
---|
138 | };
|
---|
139 |
|
---|
140 |
|
---|
141 | template <class TElem> class NameIdPool : public XMemory
|
---|
142 | {
|
---|
143 | public :
|
---|
144 | // -----------------------------------------------------------------------
|
---|
145 | // Contructors and Destructor
|
---|
146 | // -----------------------------------------------------------------------
|
---|
147 | NameIdPool
|
---|
148 | (
|
---|
149 | const unsigned int hashModulus
|
---|
150 | , const unsigned int initSize = 128
|
---|
151 | , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
|
---|
152 | );
|
---|
153 |
|
---|
154 | ~NameIdPool();
|
---|
155 |
|
---|
156 |
|
---|
157 | // -----------------------------------------------------------------------
|
---|
158 | // Element management
|
---|
159 | // -----------------------------------------------------------------------
|
---|
160 | bool containsKey(const XMLCh* const key) const;
|
---|
161 | void removeAll();
|
---|
162 |
|
---|
163 |
|
---|
164 | // -----------------------------------------------------------------------
|
---|
165 | // Getters
|
---|
166 | // -----------------------------------------------------------------------
|
---|
167 | TElem* getByKey(const XMLCh* const key);
|
---|
168 | const TElem* getByKey(const XMLCh* const key) const;
|
---|
169 | TElem* getById(const unsigned elemId);
|
---|
170 | const TElem* getById(const unsigned elemId) const;
|
---|
171 |
|
---|
172 | MemoryManager* getMemoryManager() const;
|
---|
173 | // -----------------------------------------------------------------------
|
---|
174 | // Putters
|
---|
175 | //
|
---|
176 | // Dups are not allowed and cause an IllegalArgumentException. The id
|
---|
177 | // of the new element is returned.
|
---|
178 | // -----------------------------------------------------------------------
|
---|
179 | unsigned int put(TElem* const valueToAdopt);
|
---|
180 |
|
---|
181 |
|
---|
182 | protected :
|
---|
183 | // -----------------------------------------------------------------------
|
---|
184 | // Declare the enumerator our friend so he can see our members
|
---|
185 | // -----------------------------------------------------------------------
|
---|
186 | friend class NameIdPoolEnumerator<TElem>;
|
---|
187 |
|
---|
188 |
|
---|
189 | private :
|
---|
190 | // -----------------------------------------------------------------------
|
---|
191 | // Unused constructors and operators
|
---|
192 | // -----------------------------------------------------------------------
|
---|
193 | NameIdPool(const NameIdPool<TElem>&);
|
---|
194 | NameIdPool<TElem>& operator=(const NameIdPool<TElem>&);
|
---|
195 |
|
---|
196 |
|
---|
197 | // -----------------------------------------------------------------------
|
---|
198 | // Private helper methods
|
---|
199 | // -----------------------------------------------------------------------
|
---|
200 | NameIdPoolBucketElem<TElem>* findBucketElem
|
---|
201 | (
|
---|
202 | const XMLCh* const key
|
---|
203 | , unsigned int& hashVal
|
---|
204 | );
|
---|
205 | const NameIdPoolBucketElem<TElem>* findBucketElem
|
---|
206 | (
|
---|
207 | const XMLCh* const key
|
---|
208 | , unsigned int& hashVal
|
---|
209 | ) const;
|
---|
210 |
|
---|
211 |
|
---|
212 | // -----------------------------------------------------------------------
|
---|
213 | // Data members
|
---|
214 | //
|
---|
215 | // fBucketList
|
---|
216 | // This is the array that contains the heads of all of the list
|
---|
217 | // buckets, one for each possible hash value.
|
---|
218 | //
|
---|
219 | // fIdPtrs
|
---|
220 | // fIdPtrsCount
|
---|
221 | // This is the array of pointers to the bucket elements in order of
|
---|
222 | // their assigned ids. So taking id N and referencing this array
|
---|
223 | // gives you the element with that id. The count field indicates
|
---|
224 | // the current size of this list. When fIdCounter+1 reaches this
|
---|
225 | // value the list must be expanded.
|
---|
226 | //
|
---|
227 | // fIdCounter
|
---|
228 | // This is used to give out unique ids to added elements. It starts
|
---|
229 | // at zero (which means empty), and is bumped up for each newly added
|
---|
230 | // element. So the first element is 1, the next is 2, etc... This
|
---|
231 | // means that this value is set to the top index of the fIdPtrs array.
|
---|
232 | //
|
---|
233 | // fHashModulus
|
---|
234 | // This is the modulus to use in this pool. The fBucketList array
|
---|
235 | // is of this size. It should be a prime number.
|
---|
236 | // -----------------------------------------------------------------------
|
---|
237 | MemoryManager* fMemoryManager;
|
---|
238 | NameIdPoolBucketElem<TElem>** fBucketList;
|
---|
239 | TElem** fIdPtrs;
|
---|
240 | unsigned int fIdPtrsCount;
|
---|
241 | unsigned int fIdCounter;
|
---|
242 | unsigned int fHashModulus;
|
---|
243 | };
|
---|
244 |
|
---|
245 |
|
---|
246 | //
|
---|
247 | // An enumerator for a name id pool. It derives from the basic enumerator
|
---|
248 | // class, so that pools can be generically enumerated.
|
---|
249 | //
|
---|
250 | template <class TElem> class NameIdPoolEnumerator : public XMLEnumerator<TElem>, public XMemory
|
---|
251 | {
|
---|
252 | public :
|
---|
253 | // -----------------------------------------------------------------------
|
---|
254 | // Constructors and Destructor
|
---|
255 | // -----------------------------------------------------------------------
|
---|
256 | NameIdPoolEnumerator
|
---|
257 | (
|
---|
258 | NameIdPool<TElem>* const toEnum
|
---|
259 | , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
|
---|
260 | );
|
---|
261 |
|
---|
262 | NameIdPoolEnumerator
|
---|
263 | (
|
---|
264 | const NameIdPoolEnumerator<TElem>& toCopy
|
---|
265 | );
|
---|
266 |
|
---|
267 | virtual ~NameIdPoolEnumerator();
|
---|
268 |
|
---|
269 | // -----------------------------------------------------------------------
|
---|
270 | // Public operators
|
---|
271 | // -----------------------------------------------------------------------
|
---|
272 | NameIdPoolEnumerator<TElem>& operator=
|
---|
273 | (
|
---|
274 | const NameIdPoolEnumerator<TElem>& toAssign
|
---|
275 | );
|
---|
276 |
|
---|
277 | // -----------------------------------------------------------------------
|
---|
278 | // Enum interface
|
---|
279 | // -----------------------------------------------------------------------
|
---|
280 | bool hasMoreElements() const;
|
---|
281 | TElem& nextElement();
|
---|
282 | void Reset();
|
---|
283 | int size() const;
|
---|
284 |
|
---|
285 | private :
|
---|
286 | // -----------------------------------------------------------------------
|
---|
287 | // Data Members
|
---|
288 | //
|
---|
289 | // fCurIndex
|
---|
290 | // This is the current index into the pool's id mapping array. This
|
---|
291 | // is now we enumerate it.
|
---|
292 | //
|
---|
293 | // fToEnum
|
---|
294 | // The name id pool that is being enumerated.
|
---|
295 | // -----------------------------------------------------------------------
|
---|
296 | unsigned int fCurIndex;
|
---|
297 | NameIdPool<TElem>* fToEnum;
|
---|
298 | MemoryManager* fMemoryManager;
|
---|
299 | };
|
---|
300 |
|
---|
301 | XERCES_CPP_NAMESPACE_END
|
---|
302 |
|
---|
303 | #if !defined(XERCES_TMPLSINC)
|
---|
304 | #include <xercesc/util/NameIdPool.c>
|
---|
305 | #endif
|
---|
306 |
|
---|
307 | #endif
|
---|