source: NonGTP/Xerces/xercesc/util/BaseRefVectorOf.c @ 188

Revision 188, 12.0 KB checked in by mattausch, 19 years ago (diff)

added xercesc to support

Line 
1/*
2 * The Apache Software License, Version 1.1
3 *
4 * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
5 * reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in
16 *    the documentation and/or other materials provided with the
17 *    distribution.
18 *
19 * 3. The end-user documentation included with the redistribution,
20 *    if any, must include the following acknowledgment:
21 *       "This product includes software developed by the
22 *        Apache Software Foundation (http://www.apache.org/)."
23 *    Alternately, this acknowledgment may appear in the software itself,
24 *    if and wherever such third-party acknowledgments normally appear.
25 *
26 * 4. The names "Xerces" and "Apache Software Foundation" must
27 *    not be used to endorse or promote products derived from this
28 *    software without prior written permission. For written
29 *    permission, please contact apache\@apache.org.
30 *
31 * 5. Products derived from this software may not be called "Apache",
32 *    nor may "Apache" appear in their name, without prior written
33 *    permission of the Apache Software Foundation.
34 *
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 * SUCH DAMAGE.
47 * ====================================================================
48 *
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Software Foundation, and was
51 * originally based on software copyright (c) 1999, International
52 * Business Machines, Inc., http://www.ibm.com .  For more information
53 * on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56// ---------------------------------------------------------------------------
57//  Includes
58// ---------------------------------------------------------------------------
59#if defined(XERCES_TMPLSINC)
60#include <xercesc/util/BaseRefVectorOf.hpp>
61#endif
62
63XERCES_CPP_NAMESPACE_BEGIN
64
65// ---------------------------------------------------------------------------
66//  BaseRefVectorOf: Constructors and Destructor
67// ---------------------------------------------------------------------------
68template <class TElem>
69BaseRefVectorOf<TElem>::BaseRefVectorOf( const unsigned int maxElems
70                                       , const bool adoptElems
71                                       , MemoryManager* const manager) :
72
73    fAdoptedElems(adoptElems)
74    , fCurCount(0)
75    , fMaxCount(maxElems)
76    , fElemList(0)
77    , fMemoryManager(manager)
78{
79    // Allocate and initialize the array
80    fElemList = (TElem**) fMemoryManager->allocate(maxElems * sizeof(TElem*));//new TElem*[maxElems];
81    for (unsigned int index = 0; index < maxElems; index++)
82        fElemList[index] = 0;
83}
84
85
86//implemented so code will link
87template <class TElem> BaseRefVectorOf<TElem>::~BaseRefVectorOf()
88{
89}
90
91
92// ---------------------------------------------------------------------------
93//  BaseRefVectorOf: Element management
94// ---------------------------------------------------------------------------
95template <class TElem> void BaseRefVectorOf<TElem>::addElement(TElem* const toAdd)
96{
97    ensureExtraCapacity(1);
98    fElemList[fCurCount] = toAdd;
99    fCurCount++;
100}
101
102
103template <class TElem> void
104BaseRefVectorOf<TElem>::setElementAt(TElem* const toSet, const unsigned int setAt)
105{
106    if (setAt >= fCurCount)
107        ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
108
109    if (fAdoptedElems)
110        delete fElemList[setAt];
111    fElemList[setAt] = toSet;
112}
113
114template <class TElem> void BaseRefVectorOf<TElem>::
115insertElementAt(TElem* const toInsert, const unsigned int insertAt)
116{
117    if (insertAt == fCurCount)
118    {
119        addElement(toInsert);
120        return;
121    }
122
123    if (insertAt > fCurCount)
124        ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
125
126    ensureExtraCapacity(1);
127
128    // Make room for the newbie
129    for (unsigned int index = fCurCount; index > insertAt; index--)
130        fElemList[index] = fElemList[index-1];
131
132    // And stick it in and bump the count
133    fElemList[insertAt] = toInsert;
134    fCurCount++;
135}
136
137template <class TElem> TElem* BaseRefVectorOf<TElem>::
138orphanElementAt(const unsigned int orphanAt)
139{
140    if (orphanAt >= fCurCount)
141        ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
142
143    // Get the element we are going to orphan
144    TElem* retVal = fElemList[orphanAt];
145
146    // Optimize if its the last element
147    if (orphanAt == fCurCount-1)
148    {
149        fElemList[orphanAt] = 0;
150        fCurCount--;
151        return retVal;
152    }
153
154    // Copy down every element above orphan point
155    for (unsigned int index = orphanAt; index < fCurCount-1; index++)
156        fElemList[index] = fElemList[index+1];
157
158    // Keep unused elements zero for sanity's sake
159    fElemList[fCurCount-1] = 0;
160
161    // And bump down count
162    fCurCount--;
163
164    return retVal;
165}
166
167template <class TElem> void BaseRefVectorOf<TElem>::removeAllElements()
168{
169    for (unsigned int index = 0; index < fCurCount; index++)
170    {
171        if (fAdoptedElems)
172          delete fElemList[index];
173
174        // Keep unused elements zero for sanity's sake
175        fElemList[index] = 0;
176    }
177    fCurCount = 0;
178}
179
180template <class TElem> void BaseRefVectorOf<TElem>::
181removeElementAt(const unsigned int removeAt)
182{
183    if (removeAt >= fCurCount)
184        ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
185
186    if (fAdoptedElems)
187        delete fElemList[removeAt];
188
189    // Optimize if its the last element
190    if (removeAt == fCurCount-1)
191    {
192        fElemList[removeAt] = 0;
193        fCurCount--;
194        return;
195    }
196
197    // Copy down every element above remove point
198    for (unsigned int index = removeAt; index < fCurCount-1; index++)
199        fElemList[index] = fElemList[index+1];
200
201    // Keep unused elements zero for sanity's sake
202    fElemList[fCurCount-1] = 0;
203
204    // And bump down count
205    fCurCount--;
206}
207
208template <class TElem> void BaseRefVectorOf<TElem>::removeLastElement()
209{
210    if (!fCurCount)
211        return;
212    fCurCount--;
213
214    if (fAdoptedElems)
215        delete fElemList[fCurCount];
216}
217
218template <class TElem>
219bool BaseRefVectorOf<TElem>::containsElement(const TElem* const toCheck) {
220
221    for (unsigned int i = 0; i < fCurCount; i++) {
222        if (fElemList[i] == toCheck) {
223            return true;
224        }
225    }
226
227    return false;
228}
229
230//
231// cleanup():
232//   similar to destructor
233//   called to cleanup the memory, in case destructor cannot be called
234//
235template <class TElem> void BaseRefVectorOf<TElem>::cleanup()
236{
237    if (fAdoptedElems)
238    {
239        for (unsigned int index = 0; index < fCurCount; index++)
240            delete fElemList[index];
241    }
242    fMemoryManager->deallocate(fElemList);//delete [] fElemList;
243}
244
245//
246// reinitialize():
247//   similar to constructor
248//   called to re-construct the fElemList from scratch again
249//
250template <class TElem> void BaseRefVectorOf<TElem>::reinitialize()
251{
252    // reinitialize the array
253    if (fElemList)
254        cleanup();
255
256    fElemList = (TElem**) fMemoryManager->allocate(fMaxCount * sizeof(TElem*));//new TElem*[fMaxCount];
257    for (unsigned int index = 0; index < fMaxCount; index++)
258        fElemList[index] = 0;
259
260}
261
262template <class TElem>
263MemoryManager* BaseRefVectorOf<TElem>::getMemoryManager() const
264{
265    return fMemoryManager;
266}
267
268
269// ---------------------------------------------------------------------------
270//  BaseRefVectorOf: Getter methods
271// ---------------------------------------------------------------------------
272template <class TElem> unsigned int BaseRefVectorOf<TElem>::curCapacity() const
273{
274    return fMaxCount;
275}
276
277template <class TElem> const TElem* BaseRefVectorOf<TElem>::
278elementAt(const unsigned int getAt) const
279{
280    if (getAt >= fCurCount)
281        ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
282    return fElemList[getAt];
283}
284
285template <class TElem> TElem*
286BaseRefVectorOf<TElem>::elementAt(const unsigned int getAt)
287{
288    if (getAt >= fCurCount)
289        ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
290    return fElemList[getAt];
291}
292
293template <class TElem> unsigned int BaseRefVectorOf<TElem>::size() const
294{
295    return fCurCount;
296}
297
298
299// ---------------------------------------------------------------------------
300//  BaseRefVectorOf: Miscellaneous
301// ---------------------------------------------------------------------------
302template <class TElem> void BaseRefVectorOf<TElem>::
303ensureExtraCapacity(const unsigned int length)
304{
305    unsigned int newMax = fCurCount + length;
306
307    if (newMax < fMaxCount)
308        return;
309
310    // Avoid too many reallocations by providing a little more space
311    if (newMax < fMaxCount + 32)
312        newMax = fMaxCount + 32;
313
314    // Allocate the new array and copy over the existing stuff
315    TElem** newList = (TElem**) fMemoryManager->allocate
316    (
317        newMax * sizeof(TElem*)
318    );//new TElem*[newMax];
319    unsigned int index = 0;
320    for (; index < fCurCount; index++)
321        newList[index] = fElemList[index];
322
323    // Zero out the rest of them
324    for (; index < newMax; index++)
325        newList[index] = 0;
326
327    // Clean up the old array and update our members
328    fMemoryManager->deallocate(fElemList);//delete [] fElemList;
329    fElemList = newList;
330    fMaxCount = newMax;
331}
332
333
334
335// ---------------------------------------------------------------------------
336//  AbstractBaseRefVectorEnumerator: Constructors and Destructor
337// ---------------------------------------------------------------------------
338template <class TElem> BaseRefVectorEnumerator<TElem>::
339BaseRefVectorEnumerator(        BaseRefVectorOf<TElem>* const   toEnum
340                    , const bool                        adopt) :
341    fAdopted(adopt)
342    , fCurIndex(0)
343    , fToEnum(toEnum)
344{
345}
346
347template <class TElem> BaseRefVectorEnumerator<TElem>::~BaseRefVectorEnumerator()
348{
349    if (fAdopted)
350        delete fToEnum;
351}
352
353template <class TElem> BaseRefVectorEnumerator<TElem>::
354BaseRefVectorEnumerator(const BaseRefVectorEnumerator<TElem>& toCopy) :
355    fAdopted(toCopy.fAdopted)
356    , fCurIndex(toCopy.fCurIndex)
357    , fToEnum(toCopy.fToEnum)   
358{
359}
360// ---------------------------------------------------------------------------
361//  RefBaseRefVectorEnumerator: Enum interface
362// ---------------------------------------------------------------------------
363template <class TElem> bool BaseRefVectorEnumerator<TElem>::hasMoreElements() const
364{
365    if (fCurIndex >= fToEnum->size())
366        return false;
367    return true;
368}
369
370template <class TElem> TElem& BaseRefVectorEnumerator<TElem>::nextElement()
371{
372    return *(fToEnum->elementAt(fCurIndex++));
373}
374
375template <class TElem> void BaseRefVectorEnumerator<TElem>::Reset()
376{
377    fCurIndex = 0;
378}
379
380XERCES_CPP_NAMESPACE_END
Note: See TracBrowser for help on using the repository browser.