source: icXML/icXML-devel/src/xercesc/util/ValueVectorOf.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: 8.8 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/*
19 * $Id: ValueVectorOf.c 676911 2008-07-15 13:27:32Z amassari $
20 */
21
22
23// ---------------------------------------------------------------------------
24//  Includes
25// ---------------------------------------------------------------------------
26#if defined(XERCES_TMPLSINC)
27#include <xercesc/util/ValueVectorOf.hpp>
28#endif
29#include <string.h>
30
31XERCES_CPP_NAMESPACE_BEGIN
32
33// ---------------------------------------------------------------------------
34//  ValueVectorOf: Constructors and Destructor
35// ---------------------------------------------------------------------------
36template <class TElem>
37ValueVectorOf<TElem>::ValueVectorOf(const XMLSize_t maxElems,
38                                    MemoryManager* const manager,
39                                    const bool toCallDestructor) :
40
41    fCallDestructor(toCallDestructor)
42    , fCurCount(0)
43    , fMaxCount(maxElems)
44    , fElemList(0)
45    , fMemoryManager(manager)
46{
47    fElemList = (TElem*) fMemoryManager->allocate
48    (
49        fMaxCount * sizeof(TElem)
50    ); //new TElem[fMaxCount];
51
52    memset(fElemList, 0, fMaxCount * sizeof(TElem));
53}
54
55template <class TElem>
56ValueVectorOf<TElem>::ValueVectorOf(const ValueVectorOf<TElem>& toCopy) :
57    XMemory(toCopy)
58    , fCallDestructor(toCopy.fCallDestructor)
59    , fCurCount(toCopy.fCurCount)
60    , fMaxCount(toCopy.fMaxCount)
61    , fElemList(0)
62    , fMemoryManager(toCopy.fMemoryManager)
63{
64    fElemList = (TElem*) fMemoryManager->allocate
65    (
66        fMaxCount * sizeof(TElem)
67    ); //new TElem[fMaxCount];
68
69    memset(fElemList, 0, fMaxCount * sizeof(TElem));
70    for (XMLSize_t index = 0; index < fCurCount; index++)
71        fElemList[index] = toCopy.fElemList[index];
72}
73
74template <class TElem> ValueVectorOf<TElem>::~ValueVectorOf()
75{
76    if (fCallDestructor) {
77        for (XMLSize_t index=fMaxCount; index > 0; index--)
78            fElemList[index-1].~TElem();
79    }
80    fMemoryManager->deallocate(fElemList); //delete [] fElemList;
81}
82
83
84
85// ---------------------------------------------------------------------------
86//  ValueVectorOf: Operators
87// ---------------------------------------------------------------------------
88template <class TElem> ValueVectorOf<TElem>&
89ValueVectorOf<TElem>::operator=(const ValueVectorOf<TElem>& toAssign)
90{
91    if (this == &toAssign)
92        return *this;
93
94    // Reallocate if required
95    if (fMaxCount < toAssign.fCurCount)
96    {
97        fMemoryManager->deallocate(fElemList); //delete [] fElemList;
98        fElemList = (TElem*) fMemoryManager->allocate
99        (
100            toAssign.fMaxCount * sizeof(TElem)
101        ); //new TElem[toAssign.fMaxCount];
102        fMaxCount = toAssign.fMaxCount;
103    }
104
105    fCurCount = toAssign.fCurCount;
106    for (XMLSize_t index = 0; index < fCurCount; index++)
107        fElemList[index] = toAssign.fElemList[index];
108
109    return *this;
110}
111
112
113// ---------------------------------------------------------------------------
114//  ValueVectorOf: Element management
115// ---------------------------------------------------------------------------
116template <class TElem> void ValueVectorOf<TElem>::addElement(const TElem& toAdd)
117{
118    ensureExtraCapacity(1);
119    fElemList[fCurCount++] = toAdd;
120}
121
122template <class TElem> void ValueVectorOf<TElem>::
123setElementAt(const TElem& toSet, const XMLSize_t setAt)
124{
125    if (setAt >= fCurCount)
126        ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
127    fElemList[setAt] = toSet;
128}
129
130template <class TElem> void ValueVectorOf<TElem>::
131insertElementAt(const TElem& toInsert, const XMLSize_t insertAt)
132{
133    if (insertAt == fCurCount)
134    {
135        addElement(toInsert);
136        return;
137    }
138
139    if (insertAt > fCurCount)
140        ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
141
142    // Make room for the newbie
143    ensureExtraCapacity(1);
144    for (XMLSize_t index = fCurCount; index > insertAt; index--)
145        fElemList[index] = fElemList[index-1];
146
147    // And stick it in and bump the count
148    fElemList[insertAt] = toInsert;
149    fCurCount++;
150}
151
152template <class TElem> void ValueVectorOf<TElem>::
153removeElementAt(const XMLSize_t removeAt)
154{
155    if (removeAt >= fCurCount)
156        ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
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    // And bump down count
163    fCurCount--;
164}
165
166template <class TElem> void ValueVectorOf<TElem>::removeAllElements()
167{
168    fCurCount = 0;
169}
170
171template <class TElem>
172bool ValueVectorOf<TElem>::containsElement(const TElem& toCheck,
173                                           const XMLSize_t startIndex) {
174
175    for (XMLSize_t i = startIndex; i < fCurCount; i++) {
176        if (fElemList[i] == toCheck) {
177            return true;
178        }
179    }
180
181    return false;
182}
183
184
185// ---------------------------------------------------------------------------
186//  ValueVectorOf: Getter methods
187// ---------------------------------------------------------------------------
188template <class TElem> const TElem& ValueVectorOf<TElem>::
189elementAt(const XMLSize_t getAt) const
190{
191    if (getAt >= fCurCount)
192        ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
193    return fElemList[getAt];
194}
195
196template <class TElem> TElem& ValueVectorOf<TElem>::
197elementAt(const XMLSize_t getAt)
198{
199    if (getAt >= fCurCount)
200        ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
201    return fElemList[getAt];
202}
203
204template <class TElem> XMLSize_t ValueVectorOf<TElem>::curCapacity() const
205{
206    return fMaxCount;
207}
208
209template <class TElem> XMLSize_t ValueVectorOf<TElem>::size() const
210{
211    return fCurCount;
212}
213
214template <class TElem>
215MemoryManager* ValueVectorOf<TElem>::getMemoryManager() const
216{
217    return fMemoryManager;
218}
219
220// ---------------------------------------------------------------------------
221//  ValueVectorOf: Miscellaneous
222// ---------------------------------------------------------------------------
223template <class TElem> void ValueVectorOf<TElem>::
224ensureExtraCapacity(const XMLSize_t length)
225{
226    XMLSize_t newMax = fCurCount + length;
227
228    if (newMax > fMaxCount)
229    {
230        // Avoid too many reallocations by expanding by a percentage
231        XMLSize_t minNewMax = (XMLSize_t)((double)fCurCount * 1.25);
232        if (newMax < minNewMax)
233            newMax = minNewMax;
234
235        TElem* newList = (TElem*) fMemoryManager->allocate
236        (
237            newMax * sizeof(TElem)
238        ); //new TElem[newMax];
239        for (XMLSize_t index = 0; index < fCurCount; index++)
240            newList[index] = fElemList[index];
241
242        fMemoryManager->deallocate(fElemList); //delete [] fElemList;
243        fElemList = newList;
244        fMaxCount = newMax;
245    }
246}
247
248template <class TElem> const TElem* ValueVectorOf<TElem>::rawData() const
249{
250    return fElemList;
251}
252
253
254
255// ---------------------------------------------------------------------------
256//  ValueVectorEnumerator: Constructors and Destructor
257// ---------------------------------------------------------------------------
258template <class TElem> ValueVectorEnumerator<TElem>::
259ValueVectorEnumerator(       ValueVectorOf<TElem>* const toEnum
260                     , const bool                        adopt) :
261    fAdopted(adopt)
262    , fCurIndex(0)
263    , fToEnum(toEnum)
264{
265}
266
267template <class TElem> ValueVectorEnumerator<TElem>::~ValueVectorEnumerator()
268{
269    if (fAdopted)
270        delete fToEnum;
271}
272
273
274// ---------------------------------------------------------------------------
275//  ValueVectorEnumerator: Enum interface
276// ---------------------------------------------------------------------------
277template <class TElem> bool
278ValueVectorEnumerator<TElem>::hasMoreElements() const
279{
280    if (fCurIndex >= fToEnum->size())
281        return false;
282    return true;
283}
284
285template <class TElem> TElem& ValueVectorEnumerator<TElem>::nextElement()
286{
287    return fToEnum->elementAt(fCurIndex++);
288}
289
290template <class TElem> void ValueVectorEnumerator<TElem>::Reset()
291{
292    fCurIndex = 0;
293}
294
295XERCES_CPP_NAMESPACE_END
Note: See TracBrowser for help on using the repository browser.