source: icXML/icXML-devel/src/icxercesc/util/BaseRefVectorOf.c

Last change on this file was 3564, checked in by cameron, 6 years ago

Changes to icxercesc files

File size: 9.9 KB
Line 
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//  Includes
19// ---------------------------------------------------------------------------
20#if defined(XERCES_TMPLSINC)
21#include <icxercesc/util/BaseRefVectorOf.hpp>
22#endif
23
24#include <icxmlc/backtrace.h>
25
26XERCES_CPP_NAMESPACE_BEGIN
27
28// ---------------------------------------------------------------------------
29//  BaseRefVectorOf: Constructors and Destructor
30// ---------------------------------------------------------------------------
31template <class TElem>
32BaseRefVectorOf<TElem>::BaseRefVectorOf( const XMLSize_t maxElems
33                                       , const bool adoptElems
34                                       , MemoryManager* const manager) :
35
36    fAdoptedElems(adoptElems)
37    , fCurCount(0)
38    , fMaxCount(maxElems)
39    , fElemList(0)
40    , fMemoryManager(manager)
41{
42    // Allocate and initialize the array
43    fElemList = (TElem**) fMemoryManager->allocate(maxElems * sizeof(TElem*));//new TElem*[maxElems];
44    for (XMLSize_t index = 0; index < maxElems; index++)
45        fElemList[index] = 0;
46}
47
48
49//implemented so code will link
50template <class TElem> BaseRefVectorOf<TElem>::~BaseRefVectorOf()
51{
52}
53
54
55// ---------------------------------------------------------------------------
56//  BaseRefVectorOf: Element management
57// ---------------------------------------------------------------------------
58template <class TElem> void BaseRefVectorOf<TElem>::addElement(TElem* const toAdd)
59{
60    ensureExtraCapacity(1);
61    fElemList[fCurCount] = toAdd;
62    fCurCount++;
63}
64
65
66template <class TElem> void
67BaseRefVectorOf<TElem>::setElementAt(TElem* const toSet, const XMLSize_t setAt)
68{
69    if (setAt >= fCurCount)
70    {
71        ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
72    }
73
74    if (fAdoptedElems)
75        delete fElemList[setAt];
76    fElemList[setAt] = toSet;
77}
78
79template <class TElem> void BaseRefVectorOf<TElem>::
80insertElementAt(TElem* const toInsert, const XMLSize_t insertAt)
81{
82    if (insertAt == fCurCount)
83    {
84        addElement(toInsert);
85        return;
86    }
87
88    if (insertAt > fCurCount)
89        ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
90
91    ensureExtraCapacity(1);
92
93    // Make room for the newbie
94    for (XMLSize_t index = fCurCount; index > insertAt; index--)
95        fElemList[index] = fElemList[index-1];
96
97    // And stick it in and bump the count
98    fElemList[insertAt] = toInsert;
99    fCurCount++;
100}
101
102template <class TElem> TElem* BaseRefVectorOf<TElem>::
103orphanElementAt(const XMLSize_t orphanAt)
104{
105    if (orphanAt >= fCurCount)
106        ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
107
108    // Get the element we are going to orphan
109    TElem* retVal = fElemList[orphanAt];
110
111    // Optimize if its the last element
112    if (orphanAt == fCurCount-1)
113    {
114        fElemList[orphanAt] = 0;
115        fCurCount--;
116        return retVal;
117    }
118
119    // Copy down every element above orphan point
120    for (XMLSize_t index = orphanAt; index < fCurCount-1; index++)
121        fElemList[index] = fElemList[index+1];
122
123    // Keep unused elements zero for sanity's sake
124    fElemList[fCurCount-1] = 0;
125
126    // And bump down count
127    fCurCount--;
128
129    return retVal;
130}
131
132template <class TElem> void BaseRefVectorOf<TElem>::removeAllElements()
133{
134    for (XMLSize_t index = 0; index < fCurCount; index++)
135    {
136        if (fAdoptedElems)
137          delete fElemList[index];
138
139        // Keep unused elements zero for sanity's sake
140        fElemList[index] = 0;
141    }
142    fCurCount = 0;
143}
144
145template <class TElem> void BaseRefVectorOf<TElem>::
146removeElementAt(const XMLSize_t removeAt)
147{
148    if (removeAt >= fCurCount)
149        ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
150
151    if (fAdoptedElems)
152        delete fElemList[removeAt];
153
154    // Optimize if its the last element
155    if (removeAt == fCurCount-1)
156    {
157        fElemList[removeAt] = 0;
158        fCurCount--;
159        return;
160    }
161
162    // Copy down every element above remove point
163    for (XMLSize_t index = removeAt; index < fCurCount-1; index++)
164        fElemList[index] = fElemList[index+1];
165
166    // Keep unused elements zero for sanity's sake
167    fElemList[fCurCount-1] = 0;
168
169    // And bump down count
170    fCurCount--;
171}
172
173template <class TElem> void BaseRefVectorOf<TElem>::removeLastElement()
174{
175    if (!fCurCount)
176        return;
177    fCurCount--;
178
179    if (fAdoptedElems)
180        delete fElemList[fCurCount];
181}
182
183template <class TElem>
184bool BaseRefVectorOf<TElem>::containsElement(const TElem* const toCheck) {
185
186    for (XMLSize_t i = 0; i < fCurCount; i++) {
187        if (fElemList[i] == toCheck) {
188            return true;
189        }
190    }
191
192    return false;
193}
194
195//
196// cleanup():
197//   similar to destructor
198//   called to cleanup the memory, in case destructor cannot be called
199//
200template <class TElem> void BaseRefVectorOf<TElem>::cleanup()
201{
202    if (fAdoptedElems)
203    {
204        for (XMLSize_t index = 0; index < fCurCount; index++)
205            delete fElemList[index];
206    }
207    fMemoryManager->deallocate(fElemList);//delete [] fElemList;
208}
209
210//
211// reinitialize():
212//   similar to constructor
213//   called to re-construct the fElemList from scratch again
214//
215template <class TElem> void BaseRefVectorOf<TElem>::reinitialize()
216{
217    // reinitialize the array
218    if (fElemList)
219        cleanup();
220
221    fElemList = (TElem**) fMemoryManager->allocate(fMaxCount * sizeof(TElem*));//new TElem*[fMaxCount];
222    for (XMLSize_t index = 0; index < fMaxCount; index++)
223        fElemList[index] = 0;
224
225}
226
227template <class TElem>
228MemoryManager* BaseRefVectorOf<TElem>::getMemoryManager() const
229{
230    return fMemoryManager;
231}
232
233
234// ---------------------------------------------------------------------------
235//  BaseRefVectorOf: Getter methods
236// ---------------------------------------------------------------------------
237template <class TElem> inline XMLSize_t BaseRefVectorOf<TElem>::curCapacity() const
238{
239    return fMaxCount;
240}
241
242template <class TElem> inline const TElem* BaseRefVectorOf<TElem>::elementAt(const XMLSize_t getAt) const
243{
244    if (getAt >= fCurCount)
245        ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
246    return fElemList[getAt];
247}
248
249template <class TElem> inline TElem* BaseRefVectorOf<TElem>::elementAt(const XMLSize_t getAt)
250{
251    if (getAt >= fCurCount)
252        ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
253    return fElemList[getAt];
254}
255
256template <class TElem> inline XMLSize_t BaseRefVectorOf<TElem>::size() const
257{
258    return fCurCount;
259}
260
261
262// ---------------------------------------------------------------------------
263//  BaseRefVectorOf: Miscellaneous
264// ---------------------------------------------------------------------------
265template <class TElem> void BaseRefVectorOf<TElem>::
266ensureExtraCapacity(const XMLSize_t length)
267{
268    XMLSize_t newMax = fCurCount + length;
269
270    if (newMax <= fMaxCount)
271        return;
272
273    // Choose how much bigger based on the current size.
274    // This will grow half as much again.
275    if (newMax < fMaxCount + fMaxCount/2)
276        newMax = fMaxCount + fMaxCount/2;
277
278    // Allocate the new array and copy over the existing stuff
279    TElem** newList = (TElem**) fMemoryManager->allocate(newMax * sizeof(TElem*));
280    XMLSize_t index = 0;
281    for (; index < fCurCount; index++)
282        newList[index] = fElemList[index];
283
284    // Zero out the rest of them
285    for (; index < newMax; index++)
286        newList[index] = 0;
287
288    // Clean up the old array and update our members
289    fMemoryManager->deallocate(fElemList);//delete [] fElemList;
290    fElemList = newList;
291    fMaxCount = newMax;
292}
293
294// ---------------------------------------------------------------------------
295//  AbstractBaseRefVectorEnumerator: Constructors and Destructor
296// ---------------------------------------------------------------------------
297template <class TElem> BaseRefVectorEnumerator<TElem>::
298BaseRefVectorEnumerator(        BaseRefVectorOf<TElem>* const   toEnum
299                    , const bool                        adopt) :
300    fAdopted(adopt)
301    , fCurIndex(0)
302    , fToEnum(toEnum)
303{
304}
305
306template <class TElem> BaseRefVectorEnumerator<TElem>::~BaseRefVectorEnumerator()
307{
308    if (fAdopted)
309        delete fToEnum;
310}
311
312template <class TElem> BaseRefVectorEnumerator<TElem>::
313BaseRefVectorEnumerator(const BaseRefVectorEnumerator<TElem>& toCopy) :
314    XMLEnumerator<TElem>(toCopy)
315    , XMemory(toCopy)
316    , fAdopted(toCopy.fAdopted)
317    , fCurIndex(toCopy.fCurIndex)
318    , fToEnum(toCopy.fToEnum)
319{
320}
321// ---------------------------------------------------------------------------
322//  RefBaseRefVectorEnumerator: Enum interface
323// ---------------------------------------------------------------------------
324template <class TElem> bool BaseRefVectorEnumerator<TElem>::hasMoreElements() const
325{
326    if (fCurIndex >= fToEnum->size())
327        return false;
328    return true;
329}
330
331template <class TElem> TElem& BaseRefVectorEnumerator<TElem>::nextElement()
332{
333    return *(fToEnum->elementAt(fCurIndex++));
334}
335
336template <class TElem> void BaseRefVectorEnumerator<TElem>::Reset()
337{
338    fCurIndex = 0;
339}
340
341XERCES_CPP_NAMESPACE_END
Note: See TracBrowser for help on using the repository browser.