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: ValueVectorOf.c,v $
|
---|
19 | * Revision 1.10 2004/09/08 13:56:23 peiyongz
|
---|
20 | * Apache License Version 2.0
|
---|
21 | *
|
---|
22 | * Revision 1.9 2003/12/17 00:18:35 cargilld
|
---|
23 | * Update to memory management so that the static memory manager (one used to call Initialize) is only for static data.
|
---|
24 | *
|
---|
25 | * Revision 1.8 2003/11/21 15:44:12 amassari
|
---|
26 | * insertElementAt was not checking if there was room for the new element (bug#24714)
|
---|
27 | *
|
---|
28 | * Revision 1.7 2003/05/29 13:26:44 knoaman
|
---|
29 | * Fix memory leak when using deprecated dom.
|
---|
30 | *
|
---|
31 | * Revision 1.6 2003/05/20 21:06:30 knoaman
|
---|
32 | * Set values to 0.
|
---|
33 | *
|
---|
34 | * Revision 1.5 2003/05/16 21:37:00 knoaman
|
---|
35 | * Memory manager implementation: Modify constructors to pass in the memory manager.
|
---|
36 | *
|
---|
37 | * Revision 1.4 2003/05/16 06:01:52 knoaman
|
---|
38 | * Partial implementation of the configurable memory manager.
|
---|
39 | *
|
---|
40 | * Revision 1.3 2003/05/15 19:07:46 knoaman
|
---|
41 | * Partial implementation of the configurable memory manager.
|
---|
42 | *
|
---|
43 | * Revision 1.2 2002/11/04 15:22:05 tng
|
---|
44 | * C++ Namespace Support.
|
---|
45 | *
|
---|
46 | * Revision 1.1.1.1 2002/02/01 22:22:13 peiyongz
|
---|
47 | * sane_include
|
---|
48 | *
|
---|
49 | * Revision 1.5 2002/01/10 17:44:49 knoaman
|
---|
50 | * Fix for bug 5786.
|
---|
51 | *
|
---|
52 | * Revision 1.4 2001/08/09 15:24:37 knoaman
|
---|
53 | * add support for <anyAttribute> declaration.
|
---|
54 | *
|
---|
55 | * Revision 1.3 2000/03/02 19:54:47 roddey
|
---|
56 | * This checkin includes many changes done while waiting for the
|
---|
57 | * 1.1.0 code to be finished. I can't list them all here, but a list is
|
---|
58 | * available elsewhere.
|
---|
59 | *
|
---|
60 | * Revision 1.2 2000/02/06 07:48:05 rahulj
|
---|
61 | * Year 2K copyright swat.
|
---|
62 | *
|
---|
63 | * Revision 1.1.1.1 1999/11/09 01:05:31 twl
|
---|
64 | * Initial checkin
|
---|
65 | *
|
---|
66 | * Revision 1.2 1999/11/08 20:45:18 rahul
|
---|
67 | * Swat for adding in Product name and CVS comment log variable.
|
---|
68 | *
|
---|
69 | */
|
---|
70 |
|
---|
71 |
|
---|
72 | // ---------------------------------------------------------------------------
|
---|
73 | // Includes
|
---|
74 | // ---------------------------------------------------------------------------
|
---|
75 | #if defined(XERCES_TMPLSINC)
|
---|
76 | #include <xercesc/util/ValueVectorOf.hpp>
|
---|
77 | #endif
|
---|
78 | #include <string.h>
|
---|
79 |
|
---|
80 | XERCES_CPP_NAMESPACE_BEGIN
|
---|
81 |
|
---|
82 | // ---------------------------------------------------------------------------
|
---|
83 | // ValueVectorOf: Constructors and Destructor
|
---|
84 | // ---------------------------------------------------------------------------
|
---|
85 | template <class TElem>
|
---|
86 | ValueVectorOf<TElem>::ValueVectorOf(const unsigned int maxElems,
|
---|
87 | MemoryManager* const manager,
|
---|
88 | const bool toCallDestructor) :
|
---|
89 |
|
---|
90 | fCallDestructor(toCallDestructor)
|
---|
91 | , fCurCount(0)
|
---|
92 | , fMaxCount(maxElems)
|
---|
93 | , fElemList(0)
|
---|
94 | , fMemoryManager(manager)
|
---|
95 | {
|
---|
96 | fElemList = (TElem*) fMemoryManager->allocate
|
---|
97 | (
|
---|
98 | fMaxCount * sizeof(TElem)
|
---|
99 | ); //new TElem[fMaxCount];
|
---|
100 |
|
---|
101 | memset(fElemList, 0, fMaxCount * sizeof(TElem));
|
---|
102 | }
|
---|
103 |
|
---|
104 | template <class TElem>
|
---|
105 | ValueVectorOf<TElem>::ValueVectorOf(const ValueVectorOf<TElem>& toCopy) :
|
---|
106 |
|
---|
107 | fCallDestructor(toCopy.fCallDestructor)
|
---|
108 | , fCurCount(toCopy.fCurCount)
|
---|
109 | , fMaxCount(toCopy.fMaxCount)
|
---|
110 | , fElemList(0)
|
---|
111 | , fMemoryManager(toCopy.fMemoryManager)
|
---|
112 | {
|
---|
113 | fElemList = (TElem*) fMemoryManager->allocate
|
---|
114 | (
|
---|
115 | fMaxCount * sizeof(TElem)
|
---|
116 | ); //new TElem[fMaxCount];
|
---|
117 |
|
---|
118 | memset(fElemList, 0, fMaxCount * sizeof(TElem));
|
---|
119 | for (unsigned int index = 0; index < fCurCount; index++)
|
---|
120 | fElemList[index] = toCopy.fElemList[index];
|
---|
121 | }
|
---|
122 |
|
---|
123 | template <class TElem> ValueVectorOf<TElem>::~ValueVectorOf()
|
---|
124 | {
|
---|
125 | if (fCallDestructor) {
|
---|
126 | for (int index= fMaxCount - 1; index >= 0; index--)
|
---|
127 | fElemList[index].~TElem();
|
---|
128 | }
|
---|
129 | fMemoryManager->deallocate(fElemList); //delete [] fElemList;
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 |
|
---|
134 | // ---------------------------------------------------------------------------
|
---|
135 | // ValueVectorOf: Operators
|
---|
136 | // ---------------------------------------------------------------------------
|
---|
137 | template <class TElem> ValueVectorOf<TElem>&
|
---|
138 | ValueVectorOf<TElem>::operator=(const ValueVectorOf<TElem>& toAssign)
|
---|
139 | {
|
---|
140 | if (this == &toAssign)
|
---|
141 | return *this;
|
---|
142 |
|
---|
143 | // Reallocate if required
|
---|
144 | if (fMaxCount < toAssign.fCurCount)
|
---|
145 | {
|
---|
146 | fMemoryManager->deallocate(fElemList); //delete [] fElemList;
|
---|
147 | fElemList = (TElem*) fMemoryManager->allocate
|
---|
148 | (
|
---|
149 | toAssign.fMaxCount * sizeof(TElem)
|
---|
150 | ); //new TElem[toAssign.fMaxCount];
|
---|
151 | fMaxCount = toAssign.fMaxCount;
|
---|
152 | }
|
---|
153 |
|
---|
154 | fCurCount = toAssign.fCurCount;
|
---|
155 | for (unsigned int index = 0; index < fCurCount; index++)
|
---|
156 | fElemList[index] = toAssign.fElemList[index];
|
---|
157 |
|
---|
158 | return *this;
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
162 | // ---------------------------------------------------------------------------
|
---|
163 | // ValueVectorOf: Element management
|
---|
164 | // ---------------------------------------------------------------------------
|
---|
165 | template <class TElem> void ValueVectorOf<TElem>::addElement(const TElem& toAdd)
|
---|
166 | {
|
---|
167 | ensureExtraCapacity(1);
|
---|
168 | fElemList[fCurCount] = toAdd;
|
---|
169 | fCurCount++;
|
---|
170 | }
|
---|
171 |
|
---|
172 | template <class TElem> void ValueVectorOf<TElem>::
|
---|
173 | setElementAt(const TElem& toSet, const unsigned int setAt)
|
---|
174 | {
|
---|
175 | if (setAt >= fCurCount)
|
---|
176 | ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
|
---|
177 | fElemList[setAt] = toSet;
|
---|
178 | }
|
---|
179 |
|
---|
180 | template <class TElem> void ValueVectorOf<TElem>::
|
---|
181 | insertElementAt(const TElem& toInsert, const unsigned int insertAt)
|
---|
182 | {
|
---|
183 | if (insertAt == fCurCount)
|
---|
184 | {
|
---|
185 | addElement(toInsert);
|
---|
186 | return;
|
---|
187 | }
|
---|
188 |
|
---|
189 | if (insertAt > fCurCount)
|
---|
190 | ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
|
---|
191 |
|
---|
192 | // Make room for the newbie
|
---|
193 | ensureExtraCapacity(1);
|
---|
194 | for (unsigned int index = fCurCount; index > insertAt; index--)
|
---|
195 | fElemList[index] = fElemList[index-1];
|
---|
196 |
|
---|
197 | // And stick it in and bump the count
|
---|
198 | fElemList[insertAt] = toInsert;
|
---|
199 | fCurCount++;
|
---|
200 | }
|
---|
201 |
|
---|
202 | template <class TElem> void ValueVectorOf<TElem>::
|
---|
203 | removeElementAt(const unsigned int removeAt)
|
---|
204 | {
|
---|
205 | if (removeAt >= fCurCount)
|
---|
206 | ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
|
---|
207 |
|
---|
208 | if (removeAt == fCurCount-1)
|
---|
209 | {
|
---|
210 | fCurCount--;
|
---|
211 | return;
|
---|
212 | }
|
---|
213 |
|
---|
214 | // Copy down every element above remove point
|
---|
215 | for (unsigned int index = removeAt; index < fCurCount-1; index++)
|
---|
216 | fElemList[index] = fElemList[index+1];
|
---|
217 |
|
---|
218 | // And bump down count
|
---|
219 | fCurCount--;
|
---|
220 | }
|
---|
221 |
|
---|
222 | template <class TElem> void ValueVectorOf<TElem>::removeAllElements()
|
---|
223 | {
|
---|
224 | fCurCount = 0;
|
---|
225 | }
|
---|
226 |
|
---|
227 | template <class TElem>
|
---|
228 | bool ValueVectorOf<TElem>::containsElement(const TElem& toCheck,
|
---|
229 | const unsigned int startIndex) {
|
---|
230 |
|
---|
231 | for (unsigned int i = startIndex; i < fCurCount; i++) {
|
---|
232 | if (fElemList[i] == toCheck) {
|
---|
233 | return true;
|
---|
234 | }
|
---|
235 | }
|
---|
236 |
|
---|
237 | return false;
|
---|
238 | }
|
---|
239 |
|
---|
240 |
|
---|
241 | // ---------------------------------------------------------------------------
|
---|
242 | // ValueVectorOf: Getter methods
|
---|
243 | // ---------------------------------------------------------------------------
|
---|
244 | template <class TElem> const TElem& ValueVectorOf<TElem>::
|
---|
245 | elementAt(const unsigned int getAt) const
|
---|
246 | {
|
---|
247 | if (getAt >= fCurCount)
|
---|
248 | ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
|
---|
249 | return fElemList[getAt];
|
---|
250 | }
|
---|
251 |
|
---|
252 | template <class TElem> TElem& ValueVectorOf<TElem>::
|
---|
253 | elementAt(const unsigned int getAt)
|
---|
254 | {
|
---|
255 | if (getAt >= fCurCount)
|
---|
256 | ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
|
---|
257 | return fElemList[getAt];
|
---|
258 | }
|
---|
259 |
|
---|
260 | template <class TElem> unsigned int ValueVectorOf<TElem>::curCapacity() const
|
---|
261 | {
|
---|
262 | return fMaxCount;
|
---|
263 | }
|
---|
264 |
|
---|
265 | template <class TElem> unsigned int ValueVectorOf<TElem>::size() const
|
---|
266 | {
|
---|
267 | return fCurCount;
|
---|
268 | }
|
---|
269 |
|
---|
270 | template <class TElem>
|
---|
271 | MemoryManager* ValueVectorOf<TElem>::getMemoryManager() const
|
---|
272 | {
|
---|
273 | return fMemoryManager;
|
---|
274 | }
|
---|
275 |
|
---|
276 | // ---------------------------------------------------------------------------
|
---|
277 | // ValueVectorOf: Miscellaneous
|
---|
278 | // ---------------------------------------------------------------------------
|
---|
279 | template <class TElem> void ValueVectorOf<TElem>::
|
---|
280 | ensureExtraCapacity(const unsigned int length)
|
---|
281 | {
|
---|
282 | unsigned int newMax = fCurCount + length;
|
---|
283 |
|
---|
284 | if (newMax < fMaxCount)
|
---|
285 | return;
|
---|
286 |
|
---|
287 | // Avoid too many reallocations by expanding by a percentage
|
---|
288 | unsigned int minNewMax = (unsigned int)((double)fCurCount * 1.25);
|
---|
289 | if (newMax < minNewMax)
|
---|
290 | newMax = minNewMax;
|
---|
291 |
|
---|
292 | TElem* newList = (TElem*) fMemoryManager->allocate
|
---|
293 | (
|
---|
294 | newMax * sizeof(TElem)
|
---|
295 | ); //new TElem[newMax];
|
---|
296 | for (unsigned int index = 0; index < fCurCount; index++)
|
---|
297 | newList[index] = fElemList[index];
|
---|
298 |
|
---|
299 | fMemoryManager->deallocate(fElemList); //delete [] fElemList;
|
---|
300 | fElemList = newList;
|
---|
301 | fMaxCount = newMax;
|
---|
302 | }
|
---|
303 |
|
---|
304 | template <class TElem> const TElem* ValueVectorOf<TElem>::rawData() const
|
---|
305 | {
|
---|
306 | return fElemList;
|
---|
307 | }
|
---|
308 |
|
---|
309 |
|
---|
310 |
|
---|
311 | // ---------------------------------------------------------------------------
|
---|
312 | // ValueVectorEnumerator: Constructors and Destructor
|
---|
313 | // ---------------------------------------------------------------------------
|
---|
314 | template <class TElem> ValueVectorEnumerator<TElem>::
|
---|
315 | ValueVectorEnumerator( ValueVectorOf<TElem>* const toEnum
|
---|
316 | , const bool adopt) :
|
---|
317 | fAdopted(adopt)
|
---|
318 | , fCurIndex(0)
|
---|
319 | , fToEnum(toEnum)
|
---|
320 | {
|
---|
321 | }
|
---|
322 |
|
---|
323 | template <class TElem> ValueVectorEnumerator<TElem>::~ValueVectorEnumerator()
|
---|
324 | {
|
---|
325 | if (fAdopted)
|
---|
326 | delete fToEnum;
|
---|
327 | }
|
---|
328 |
|
---|
329 |
|
---|
330 | // ---------------------------------------------------------------------------
|
---|
331 | // ValueVectorEnumerator: Enum interface
|
---|
332 | // ---------------------------------------------------------------------------
|
---|
333 | template <class TElem> bool
|
---|
334 | ValueVectorEnumerator<TElem>::hasMoreElements() const
|
---|
335 | {
|
---|
336 | if (fCurIndex >= fToEnum->size())
|
---|
337 | return false;
|
---|
338 | return true;
|
---|
339 | }
|
---|
340 |
|
---|
341 | template <class TElem> TElem& ValueVectorEnumerator<TElem>::nextElement()
|
---|
342 | {
|
---|
343 | return fToEnum->elementAt(fCurIndex++);
|
---|
344 | }
|
---|
345 |
|
---|
346 | template <class TElem> void ValueVectorEnumerator<TElem>::Reset()
|
---|
347 | {
|
---|
348 | fCurIndex = 0;
|
---|
349 | }
|
---|
350 |
|
---|
351 | XERCES_CPP_NAMESPACE_END
|
---|