source: icXML/icXML-devel/src/xercesc/util/BaseRefVectorOf.c @ 2722

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

Original Xerces files with import mods for icxercesc

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