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

Revision 188, 12.6 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-2000 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/**
58 * $Log: ValueVectorOf.c,v $
59 * Revision 1.9  2003/12/17 00:18:35  cargilld
60 * Update to memory management so that the static memory manager (one used to call Initialize) is only for static data.
61 *
62 * Revision 1.8  2003/11/21 15:44:12  amassari
63 * insertElementAt was not checking if there was room for the new element (bug#24714)
64 *
65 * Revision 1.7  2003/05/29 13:26:44  knoaman
66 * Fix memory leak when using deprecated dom.
67 *
68 * Revision 1.6  2003/05/20 21:06:30  knoaman
69 * Set values to 0.
70 *
71 * Revision 1.5  2003/05/16 21:37:00  knoaman
72 * Memory manager implementation: Modify constructors to pass in the memory manager.
73 *
74 * Revision 1.4  2003/05/16 06:01:52  knoaman
75 * Partial implementation of the configurable memory manager.
76 *
77 * Revision 1.3  2003/05/15 19:07:46  knoaman
78 * Partial implementation of the configurable memory manager.
79 *
80 * Revision 1.2  2002/11/04 15:22:05  tng
81 * C++ Namespace Support.
82 *
83 * Revision 1.1.1.1  2002/02/01 22:22:13  peiyongz
84 * sane_include
85 *
86 * Revision 1.5  2002/01/10 17:44:49  knoaman
87 * Fix for bug 5786.
88 *
89 * Revision 1.4  2001/08/09 15:24:37  knoaman
90 * add support for <anyAttribute> declaration.
91 *
92 * Revision 1.3  2000/03/02 19:54:47  roddey
93 * This checkin includes many changes done while waiting for the
94 * 1.1.0 code to be finished. I can't list them all here, but a list is
95 * available elsewhere.
96 *
97 * Revision 1.2  2000/02/06 07:48:05  rahulj
98 * Year 2K copyright swat.
99 *
100 * Revision 1.1.1.1  1999/11/09 01:05:31  twl
101 * Initial checkin
102 *
103 * Revision 1.2  1999/11/08 20:45:18  rahul
104 * Swat for adding in Product name and CVS comment log variable.
105 *
106 */
107
108
109// ---------------------------------------------------------------------------
110//  Includes
111// ---------------------------------------------------------------------------
112#if defined(XERCES_TMPLSINC)
113#include <xercesc/util/ValueVectorOf.hpp>
114#endif
115#include <string.h>
116
117XERCES_CPP_NAMESPACE_BEGIN
118
119// ---------------------------------------------------------------------------
120//  ValueVectorOf: Constructors and Destructor
121// ---------------------------------------------------------------------------
122template <class TElem>
123ValueVectorOf<TElem>::ValueVectorOf(const unsigned int maxElems,
124                                    MemoryManager* const manager,
125                                    const bool toCallDestructor) :
126
127    fCallDestructor(toCallDestructor)
128    , fCurCount(0)
129    , fMaxCount(maxElems)
130    , fElemList(0)
131    , fMemoryManager(manager)
132{
133    fElemList = (TElem*) fMemoryManager->allocate
134    (
135        fMaxCount * sizeof(TElem)
136    ); //new TElem[fMaxCount];
137
138    memset(fElemList, 0, fMaxCount * sizeof(TElem));
139}
140
141template <class TElem>
142ValueVectorOf<TElem>::ValueVectorOf(const ValueVectorOf<TElem>& toCopy) :
143
144    fCallDestructor(toCopy.fCallDestructor)
145    , fCurCount(toCopy.fCurCount)
146    , fMaxCount(toCopy.fMaxCount)
147    , fElemList(0)
148    , fMemoryManager(toCopy.fMemoryManager)
149{
150    fElemList = (TElem*) fMemoryManager->allocate
151    (
152        fMaxCount * sizeof(TElem)
153    ); //new TElem[fMaxCount];
154
155    memset(fElemList, 0, fMaxCount * sizeof(TElem));
156    for (unsigned int index = 0; index < fCurCount; index++)
157        fElemList[index] = toCopy.fElemList[index];
158}
159
160template <class TElem> ValueVectorOf<TElem>::~ValueVectorOf()
161{
162    if (fCallDestructor) {
163        for (int index= fMaxCount - 1; index >= 0; index--)
164            fElemList[index].~TElem();
165    }
166    fMemoryManager->deallocate(fElemList); //delete [] fElemList;
167}
168
169
170
171// ---------------------------------------------------------------------------
172//  ValueVectorOf: Operators
173// ---------------------------------------------------------------------------
174template <class TElem> ValueVectorOf<TElem>&
175ValueVectorOf<TElem>::operator=(const ValueVectorOf<TElem>& toAssign)
176{
177    if (this == &toAssign)
178        return *this;
179
180    // Reallocate if required
181    if (fMaxCount < toAssign.fCurCount)
182    {
183        fMemoryManager->deallocate(fElemList); //delete [] fElemList;
184        fElemList = (TElem*) fMemoryManager->allocate
185        (
186            toAssign.fMaxCount * sizeof(TElem)
187        ); //new TElem[toAssign.fMaxCount];
188        fMaxCount = toAssign.fMaxCount;
189    }
190
191    fCurCount = toAssign.fCurCount;
192    for (unsigned int index = 0; index < fCurCount; index++)
193        fElemList[index] = toAssign.fElemList[index];
194
195    return *this;
196}
197
198
199// ---------------------------------------------------------------------------
200//  ValueVectorOf: Element management
201// ---------------------------------------------------------------------------
202template <class TElem> void ValueVectorOf<TElem>::addElement(const TElem& toAdd)
203{
204    ensureExtraCapacity(1);
205    fElemList[fCurCount] = toAdd;
206    fCurCount++;
207}
208
209template <class TElem> void ValueVectorOf<TElem>::
210setElementAt(const TElem& toSet, const unsigned int setAt)
211{
212    if (setAt >= fCurCount)
213        ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
214    fElemList[setAt] = toSet;
215}
216
217template <class TElem> void ValueVectorOf<TElem>::
218insertElementAt(const TElem& toInsert, const unsigned int insertAt)
219{
220    if (insertAt == fCurCount)
221    {
222        addElement(toInsert);
223        return;
224    }
225
226    if (insertAt > fCurCount)
227        ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
228
229    // Make room for the newbie
230    ensureExtraCapacity(1);
231    for (unsigned int index = fCurCount; index > insertAt; index--)
232        fElemList[index] = fElemList[index-1];
233
234    // And stick it in and bump the count
235    fElemList[insertAt] = toInsert;
236    fCurCount++;
237}
238
239template <class TElem> void ValueVectorOf<TElem>::
240removeElementAt(const unsigned int removeAt)
241{
242    if (removeAt >= fCurCount)
243        ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
244
245    if (removeAt == fCurCount-1)
246    {
247        fCurCount--;
248        return;
249    }
250
251    // Copy down every element above remove point
252    for (unsigned int index = removeAt; index < fCurCount-1; index++)
253        fElemList[index] = fElemList[index+1];
254
255    // And bump down count
256    fCurCount--;
257}
258
259template <class TElem> void ValueVectorOf<TElem>::removeAllElements()
260{
261    fCurCount = 0;
262}
263
264template <class TElem>
265bool ValueVectorOf<TElem>::containsElement(const TElem& toCheck,
266                                           const unsigned int startIndex) {
267
268    for (unsigned int i = startIndex; i < fCurCount; i++) {
269        if (fElemList[i] == toCheck) {
270            return true;
271        }
272    }
273
274    return false;
275}
276
277
278// ---------------------------------------------------------------------------
279//  ValueVectorOf: Getter methods
280// ---------------------------------------------------------------------------
281template <class TElem> const TElem& ValueVectorOf<TElem>::
282elementAt(const unsigned int getAt) const
283{
284    if (getAt >= fCurCount)
285        ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
286    return fElemList[getAt];
287}
288
289template <class TElem> TElem& ValueVectorOf<TElem>::
290elementAt(const unsigned int getAt)
291{
292    if (getAt >= fCurCount)
293        ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
294    return fElemList[getAt];
295}
296
297template <class TElem> unsigned int ValueVectorOf<TElem>::curCapacity() const
298{
299    return fMaxCount;
300}
301
302template <class TElem> unsigned int ValueVectorOf<TElem>::size() const
303{
304    return fCurCount;
305}
306
307template <class TElem>
308MemoryManager* ValueVectorOf<TElem>::getMemoryManager() const
309{
310    return fMemoryManager;
311}
312
313// ---------------------------------------------------------------------------
314//  ValueVectorOf: Miscellaneous
315// ---------------------------------------------------------------------------
316template <class TElem> void ValueVectorOf<TElem>::
317ensureExtraCapacity(const unsigned int length)
318{
319    unsigned int newMax = fCurCount + length;
320
321    if (newMax < fMaxCount)
322        return;
323
324    // Avoid too many reallocations by expanding by a percentage
325    unsigned int minNewMax = (unsigned int)((double)fCurCount * 1.25);
326    if (newMax < minNewMax)
327        newMax = minNewMax;
328
329    TElem* newList = (TElem*) fMemoryManager->allocate
330    (
331        newMax * sizeof(TElem)
332    ); //new TElem[newMax];
333    for (unsigned int index = 0; index < fCurCount; index++)
334        newList[index] = fElemList[index];
335
336    fMemoryManager->deallocate(fElemList); //delete [] fElemList;
337    fElemList = newList;
338    fMaxCount = newMax;
339}
340
341template <class TElem> const TElem* ValueVectorOf<TElem>::rawData() const
342{
343    return fElemList;
344}
345
346
347
348// ---------------------------------------------------------------------------
349//  ValueVectorEnumerator: Constructors and Destructor
350// ---------------------------------------------------------------------------
351template <class TElem> ValueVectorEnumerator<TElem>::
352ValueVectorEnumerator(       ValueVectorOf<TElem>* const toEnum
353                     , const bool                        adopt) :
354    fAdopted(adopt)
355    , fCurIndex(0)
356    , fToEnum(toEnum)
357{
358}
359
360template <class TElem> ValueVectorEnumerator<TElem>::~ValueVectorEnumerator()
361{
362    if (fAdopted)
363        delete fToEnum;
364}
365
366
367// ---------------------------------------------------------------------------
368//  ValueVectorEnumerator: Enum interface
369// ---------------------------------------------------------------------------
370template <class TElem> bool
371ValueVectorEnumerator<TElem>::hasMoreElements() const
372{
373    if (fCurIndex >= fToEnum->size())
374        return false;
375    return true;
376}
377
378template <class TElem> TElem& ValueVectorEnumerator<TElem>::nextElement()
379{
380    return fToEnum->elementAt(fCurIndex++);
381}
382
383template <class TElem> void ValueVectorEnumerator<TElem>::Reset()
384{
385    fCurIndex = 0;
386}
387
388XERCES_CPP_NAMESPACE_END
Note: See TracBrowser for help on using the repository browser.