|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 /* |
|
18 * Licensed to the Apache Software Foundation (ASF) under one or more |
|
19 * contributor license agreements. See the NOTICE file distributed with |
|
20 * this work for additional information regarding copyright ownership. |
|
21 * The ASF licenses this file to You under the Apache License, Version 2.0 |
|
22 * (the "License"); you may not use this file except in compliance with |
|
23 * the License. You may obtain a copy of the License at |
|
24 * |
|
25 * http://www.apache.org/licenses/LICENSE-2.0 |
|
26 * |
|
27 * Unless required by applicable law or agreed to in writing, software |
|
28 * distributed under the License is distributed on an "AS IS" BASIS, |
|
29 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
30 * See the License for the specific language governing permissions and |
|
31 * limitations under the License. |
|
32 */ |
|
33 |
|
34 /* |
|
35 * $Id: ValueVectorOf.c 568078 2007-08-21 11:43:25Z amassari $ |
|
36 */ |
|
37 |
|
38 |
|
39 // --------------------------------------------------------------------------- |
|
40 // Includes |
|
41 // --------------------------------------------------------------------------- |
|
42 #if defined(XERCES_TMPLSINC) |
|
43 #include <xercesc/util/ValueVectorOf.hpp> |
|
44 #endif |
|
45 #include <string.h> |
|
46 |
|
47 XERCES_CPP_NAMESPACE_BEGIN |
|
48 |
|
49 // --------------------------------------------------------------------------- |
|
50 // ValueVectorOf: Constructors and Destructor |
|
51 // --------------------------------------------------------------------------- |
|
52 template <class TElem> |
|
53 ValueVectorOf<TElem>::ValueVectorOf(const unsigned int maxElems, |
|
54 MemoryManager* const manager, |
|
55 const bool toCallDestructor) : |
|
56 |
|
57 fCallDestructor(toCallDestructor) |
|
58 , fCurCount(0) |
|
59 , fMaxCount(maxElems) |
|
60 , fElemList(0) |
|
61 , fMemoryManager(manager) |
|
62 { |
|
63 fElemList = (TElem*) fMemoryManager->allocate |
|
64 ( |
|
65 fMaxCount * sizeof(TElem) |
|
66 ); //new TElem[fMaxCount]; |
|
67 |
|
68 memset(fElemList, 0, fMaxCount * sizeof(TElem)); |
|
69 } |
|
70 |
|
71 template <class TElem> |
|
72 ValueVectorOf<TElem>::ValueVectorOf(const ValueVectorOf<TElem>& toCopy) : |
|
73 XMemory(toCopy) |
|
74 , fCallDestructor(toCopy.fCallDestructor) |
|
75 , fCurCount(toCopy.fCurCount) |
|
76 , fMaxCount(toCopy.fMaxCount) |
|
77 , fElemList(0) |
|
78 , fMemoryManager(toCopy.fMemoryManager) |
|
79 { |
|
80 fElemList = (TElem*) fMemoryManager->allocate |
|
81 ( |
|
82 fMaxCount * sizeof(TElem) |
|
83 ); //new TElem[fMaxCount]; |
|
84 |
|
85 memset(fElemList, 0, fMaxCount * sizeof(TElem)); |
|
86 for (unsigned int index = 0; index < fCurCount; index++) |
|
87 fElemList[index] = toCopy.fElemList[index]; |
|
88 } |
|
89 |
|
90 template <class TElem> ValueVectorOf<TElem>::~ValueVectorOf() |
|
91 { |
|
92 if (fCallDestructor) { |
|
93 for (int index= fMaxCount - 1; index >= 0; index--) |
|
94 fElemList[index].~TElem(); |
|
95 } |
|
96 fMemoryManager->deallocate(fElemList); //delete [] fElemList; |
|
97 } |
|
98 |
|
99 |
|
100 |
|
101 // --------------------------------------------------------------------------- |
|
102 // ValueVectorOf: Operators |
|
103 // --------------------------------------------------------------------------- |
|
104 template <class TElem> ValueVectorOf<TElem>& |
|
105 ValueVectorOf<TElem>::operator=(const ValueVectorOf<TElem>& toAssign) |
|
106 { |
|
107 if (this == &toAssign) |
|
108 return *this; |
|
109 |
|
110 // Reallocate if required |
|
111 if (fMaxCount < toAssign.fCurCount) |
|
112 { |
|
113 fMemoryManager->deallocate(fElemList); //delete [] fElemList; |
|
114 fElemList = (TElem*) fMemoryManager->allocate |
|
115 ( |
|
116 toAssign.fMaxCount * sizeof(TElem) |
|
117 ); //new TElem[toAssign.fMaxCount]; |
|
118 fMaxCount = toAssign.fMaxCount; |
|
119 } |
|
120 |
|
121 fCurCount = toAssign.fCurCount; |
|
122 for (unsigned int index = 0; index < fCurCount; index++) |
|
123 fElemList[index] = toAssign.fElemList[index]; |
|
124 |
|
125 return *this; |
|
126 } |
|
127 |
|
128 |
|
129 // --------------------------------------------------------------------------- |
|
130 // ValueVectorOf: Element management |
|
131 // --------------------------------------------------------------------------- |
|
132 template <class TElem> void ValueVectorOf<TElem>::addElement(const TElem& toAdd) |
|
133 { |
|
134 ensureExtraCapacity(1); |
|
135 fElemList[fCurCount] = toAdd; |
|
136 fCurCount++; |
|
137 } |
|
138 |
|
139 template <class TElem> void ValueVectorOf<TElem>:: |
|
140 setElementAt(const TElem& toSet, const unsigned int setAt) |
|
141 { |
|
142 if (setAt >= fCurCount) |
|
143 ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager); |
|
144 fElemList[setAt] = toSet; |
|
145 } |
|
146 |
|
147 template <class TElem> void ValueVectorOf<TElem>:: |
|
148 insertElementAt(const TElem& toInsert, const unsigned int insertAt) |
|
149 { |
|
150 if (insertAt == fCurCount) |
|
151 { |
|
152 addElement(toInsert); |
|
153 return; |
|
154 } |
|
155 |
|
156 if (insertAt > fCurCount) |
|
157 ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager); |
|
158 |
|
159 // Make room for the newbie |
|
160 ensureExtraCapacity(1); |
|
161 for (unsigned int index = fCurCount; index > insertAt; index--) |
|
162 fElemList[index] = fElemList[index-1]; |
|
163 |
|
164 // And stick it in and bump the count |
|
165 fElemList[insertAt] = toInsert; |
|
166 fCurCount++; |
|
167 } |
|
168 |
|
169 template <class TElem> void ValueVectorOf<TElem>:: |
|
170 removeElementAt(const unsigned int removeAt) |
|
171 { |
|
172 if (removeAt >= fCurCount) |
|
173 ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager); |
|
174 |
|
175 if (removeAt == fCurCount-1) |
|
176 { |
|
177 fCurCount--; |
|
178 return; |
|
179 } |
|
180 |
|
181 // Copy down every element above remove point |
|
182 for (unsigned int index = removeAt; index < fCurCount-1; index++) |
|
183 fElemList[index] = fElemList[index+1]; |
|
184 |
|
185 // And bump down count |
|
186 fCurCount--; |
|
187 } |
|
188 |
|
189 template <class TElem> void ValueVectorOf<TElem>::removeAllElements() |
|
190 { |
|
191 fCurCount = 0; |
|
192 } |
|
193 |
|
194 template <class TElem> |
|
195 bool ValueVectorOf<TElem>::containsElement(const TElem& toCheck, |
|
196 const unsigned int startIndex) { |
|
197 |
|
198 for (unsigned int i = startIndex; i < fCurCount; i++) { |
|
199 if (fElemList[i] == toCheck) { |
|
200 return true; |
|
201 } |
|
202 } |
|
203 |
|
204 return false; |
|
205 } |
|
206 |
|
207 |
|
208 // --------------------------------------------------------------------------- |
|
209 // ValueVectorOf: Getter methods |
|
210 // --------------------------------------------------------------------------- |
|
211 template <class TElem> const TElem& ValueVectorOf<TElem>:: |
|
212 elementAt(const unsigned int getAt) const |
|
213 { |
|
214 if (getAt >= fCurCount) |
|
215 ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager); |
|
216 return fElemList[getAt]; |
|
217 } |
|
218 |
|
219 template <class TElem> TElem& ValueVectorOf<TElem>:: |
|
220 elementAt(const unsigned int getAt) |
|
221 { |
|
222 if (getAt >= fCurCount) |
|
223 ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager); |
|
224 return fElemList[getAt]; |
|
225 } |
|
226 |
|
227 template <class TElem> unsigned int ValueVectorOf<TElem>::curCapacity() const |
|
228 { |
|
229 return fMaxCount; |
|
230 } |
|
231 |
|
232 template <class TElem> unsigned int ValueVectorOf<TElem>::size() const |
|
233 { |
|
234 return fCurCount; |
|
235 } |
|
236 |
|
237 template <class TElem> |
|
238 MemoryManager* ValueVectorOf<TElem>::getMemoryManager() const |
|
239 { |
|
240 return fMemoryManager; |
|
241 } |
|
242 |
|
243 // --------------------------------------------------------------------------- |
|
244 // ValueVectorOf: Miscellaneous |
|
245 // --------------------------------------------------------------------------- |
|
246 template <class TElem> void ValueVectorOf<TElem>:: |
|
247 ensureExtraCapacity(const unsigned int length) |
|
248 { |
|
249 unsigned int newMax = fCurCount + length; |
|
250 |
|
251 if (newMax <= fMaxCount) |
|
252 return; |
|
253 |
|
254 // Avoid too many reallocations by expanding by a percentage |
|
255 unsigned int minNewMax = (unsigned int)((double)fCurCount * 1.25); |
|
256 if (newMax < minNewMax) |
|
257 newMax = minNewMax; |
|
258 |
|
259 TElem* newList = (TElem*) fMemoryManager->allocate |
|
260 ( |
|
261 newMax * sizeof(TElem) |
|
262 ); //new TElem[newMax]; |
|
263 for (unsigned int index = 0; index < fCurCount; index++) |
|
264 newList[index] = fElemList[index]; |
|
265 |
|
266 fMemoryManager->deallocate(fElemList); //delete [] fElemList; |
|
267 fElemList = newList; |
|
268 fMaxCount = newMax; |
|
269 } |
|
270 |
|
271 template <class TElem> const TElem* ValueVectorOf<TElem>::rawData() const |
|
272 { |
|
273 return fElemList; |
|
274 } |
|
275 |
|
276 |
|
277 |
|
278 // --------------------------------------------------------------------------- |
|
279 // ValueVectorEnumerator: Constructors and Destructor |
|
280 // --------------------------------------------------------------------------- |
|
281 template <class TElem> ValueVectorEnumerator<TElem>:: |
|
282 ValueVectorEnumerator( ValueVectorOf<TElem>* const toEnum |
|
283 , const bool adopt) : |
|
284 fAdopted(adopt) |
|
285 , fCurIndex(0) |
|
286 , fToEnum(toEnum) |
|
287 { |
|
288 } |
|
289 |
|
290 template <class TElem> ValueVectorEnumerator<TElem>::~ValueVectorEnumerator() |
|
291 { |
|
292 if (fAdopted) |
|
293 delete fToEnum; |
|
294 } |
|
295 |
|
296 |
|
297 // --------------------------------------------------------------------------- |
|
298 // ValueVectorEnumerator: Enum interface |
|
299 // --------------------------------------------------------------------------- |
|
300 template <class TElem> bool |
|
301 ValueVectorEnumerator<TElem>::hasMoreElements() const |
|
302 { |
|
303 if (fCurIndex >= fToEnum->size()) |
|
304 return false; |
|
305 return true; |
|
306 } |
|
307 |
|
308 template <class TElem> TElem& ValueVectorEnumerator<TElem>::nextElement() |
|
309 { |
|
310 return fToEnum->elementAt(fCurIndex++); |
|
311 } |
|
312 |
|
313 template <class TElem> void ValueVectorEnumerator<TElem>::Reset() |
|
314 { |
|
315 fCurIndex = 0; |
|
316 } |
|
317 |
|
318 XERCES_CPP_NAMESPACE_END |