GME  13
ValueVectorOf.c
Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one or more
00003  * contributor license agreements.  See the NOTICE file distributed with
00004  * this work for additional information regarding copyright ownership.
00005  * The ASF licenses this file to You under the Apache License, Version 2.0
00006  * (the "License"); you may not use this file except in compliance with
00007  * the License.  You may obtain a copy of the License at
00008  * 
00009  *      http://www.apache.org/licenses/LICENSE-2.0
00010  * 
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 /*
00019  * $Id: ValueVectorOf.c 676911 2008-07-15 13:27:32Z amassari $
00020  */
00021 
00022 
00023 // ---------------------------------------------------------------------------
00024 //  Includes
00025 // ---------------------------------------------------------------------------
00026 #if defined(XERCES_TMPLSINC)
00027 #include <xercesc/util/ValueVectorOf.hpp>
00028 #endif
00029 #include <string.h>
00030 
00031 XERCES_CPP_NAMESPACE_BEGIN
00032 
00033 // ---------------------------------------------------------------------------
00034 //  ValueVectorOf: Constructors and Destructor
00035 // ---------------------------------------------------------------------------
00036 template <class TElem>
00037 ValueVectorOf<TElem>::ValueVectorOf(const XMLSize_t maxElems,
00038                                     MemoryManager* const manager,
00039                                     const bool toCallDestructor) :
00040 
00041     fCallDestructor(toCallDestructor)
00042     , fCurCount(0)
00043     , fMaxCount(maxElems)
00044     , fElemList(0)
00045     , fMemoryManager(manager)
00046 {
00047     fElemList = (TElem*) fMemoryManager->allocate
00048     (
00049         fMaxCount * sizeof(TElem)
00050     ); //new TElem[fMaxCount];
00051 
00052     memset(fElemList, 0, fMaxCount * sizeof(TElem));
00053 }
00054 
00055 template <class TElem>
00056 ValueVectorOf<TElem>::ValueVectorOf(const ValueVectorOf<TElem>& toCopy) :
00057     XMemory(toCopy)
00058     , fCallDestructor(toCopy.fCallDestructor)
00059     , fCurCount(toCopy.fCurCount)
00060     , fMaxCount(toCopy.fMaxCount)
00061     , fElemList(0)
00062     , fMemoryManager(toCopy.fMemoryManager)
00063 {
00064     fElemList = (TElem*) fMemoryManager->allocate
00065     (
00066         fMaxCount * sizeof(TElem)
00067     ); //new TElem[fMaxCount];
00068 
00069     memset(fElemList, 0, fMaxCount * sizeof(TElem));
00070     for (XMLSize_t index = 0; index < fCurCount; index++)
00071         fElemList[index] = toCopy.fElemList[index];
00072 }
00073 
00074 template <class TElem> ValueVectorOf<TElem>::~ValueVectorOf()
00075 {
00076     if (fCallDestructor) {
00077         for (XMLSize_t index=fMaxCount; index > 0; index--)
00078             fElemList[index-1].~TElem();
00079     }
00080     fMemoryManager->deallocate(fElemList); //delete [] fElemList;
00081 }
00082 
00083 
00084 
00085 // ---------------------------------------------------------------------------
00086 //  ValueVectorOf: Operators
00087 // ---------------------------------------------------------------------------
00088 template <class TElem> ValueVectorOf<TElem>&
00089 ValueVectorOf<TElem>::operator=(const ValueVectorOf<TElem>& toAssign)
00090 {
00091     if (this == &toAssign)
00092         return *this;
00093 
00094     // Reallocate if required
00095     if (fMaxCount < toAssign.fCurCount)
00096     {
00097         fMemoryManager->deallocate(fElemList); //delete [] fElemList;
00098         fElemList = (TElem*) fMemoryManager->allocate
00099         (
00100             toAssign.fMaxCount * sizeof(TElem)
00101         ); //new TElem[toAssign.fMaxCount];
00102         fMaxCount = toAssign.fMaxCount;
00103     }
00104 
00105     fCurCount = toAssign.fCurCount;
00106     for (XMLSize_t index = 0; index < fCurCount; index++)
00107         fElemList[index] = toAssign.fElemList[index];
00108 
00109     return *this;
00110 }
00111 
00112 
00113 // ---------------------------------------------------------------------------
00114 //  ValueVectorOf: Element management
00115 // ---------------------------------------------------------------------------
00116 template <class TElem> void ValueVectorOf<TElem>::addElement(const TElem& toAdd)
00117 {
00118     ensureExtraCapacity(1);
00119     fElemList[fCurCount++] = toAdd;
00120 }
00121 
00122 template <class TElem> void ValueVectorOf<TElem>::
00123 setElementAt(const TElem& toSet, const XMLSize_t setAt)
00124 {
00125     if (setAt >= fCurCount)
00126         ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
00127     fElemList[setAt] = toSet;
00128 }
00129 
00130 template <class TElem> void ValueVectorOf<TElem>::
00131 insertElementAt(const TElem& toInsert, const XMLSize_t insertAt)
00132 {
00133     if (insertAt == fCurCount)
00134     {
00135         addElement(toInsert);
00136         return;
00137     }
00138 
00139     if (insertAt > fCurCount)
00140         ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
00141 
00142     // Make room for the newbie
00143     ensureExtraCapacity(1);
00144     for (XMLSize_t index = fCurCount; index > insertAt; index--)
00145         fElemList[index] = fElemList[index-1];
00146 
00147     // And stick it in and bump the count
00148     fElemList[insertAt] = toInsert;
00149     fCurCount++;
00150 }
00151 
00152 template <class TElem> void ValueVectorOf<TElem>::
00153 removeElementAt(const XMLSize_t removeAt)
00154 {
00155     if (removeAt >= fCurCount)
00156         ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
00157 
00158     // Copy down every element above remove point
00159     for (XMLSize_t index = removeAt; index < fCurCount-1; index++)
00160         fElemList[index] = fElemList[index+1];
00161 
00162     // And bump down count
00163     fCurCount--;
00164 }
00165 
00166 template <class TElem> void ValueVectorOf<TElem>::removeAllElements()
00167 {
00168     fCurCount = 0;
00169 }
00170 
00171 template <class TElem>
00172 bool ValueVectorOf<TElem>::containsElement(const TElem& toCheck,
00173                                            const XMLSize_t startIndex) {
00174 
00175     for (XMLSize_t i = startIndex; i < fCurCount; i++) {
00176         if (fElemList[i] == toCheck) {
00177             return true;
00178         }
00179     }
00180 
00181     return false;
00182 }
00183 
00184 
00185 // ---------------------------------------------------------------------------
00186 //  ValueVectorOf: Getter methods
00187 // ---------------------------------------------------------------------------
00188 template <class TElem> const TElem& ValueVectorOf<TElem>::
00189 elementAt(const XMLSize_t getAt) const
00190 {
00191     if (getAt >= fCurCount)
00192         ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
00193     return fElemList[getAt];
00194 }
00195 
00196 template <class TElem> TElem& ValueVectorOf<TElem>::
00197 elementAt(const XMLSize_t getAt)
00198 {
00199     if (getAt >= fCurCount)
00200         ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
00201     return fElemList[getAt];
00202 }
00203 
00204 template <class TElem> XMLSize_t ValueVectorOf<TElem>::curCapacity() const
00205 {
00206     return fMaxCount;
00207 }
00208 
00209 template <class TElem> XMLSize_t ValueVectorOf<TElem>::size() const
00210 {
00211     return fCurCount;
00212 }
00213 
00214 template <class TElem>
00215 MemoryManager* ValueVectorOf<TElem>::getMemoryManager() const
00216 {
00217     return fMemoryManager;
00218 }
00219 
00220 // ---------------------------------------------------------------------------
00221 //  ValueVectorOf: Miscellaneous
00222 // ---------------------------------------------------------------------------
00223 template <class TElem> void ValueVectorOf<TElem>::
00224 ensureExtraCapacity(const XMLSize_t length)
00225 {
00226     XMLSize_t newMax = fCurCount + length;
00227 
00228     if (newMax > fMaxCount)
00229     {
00230         // Avoid too many reallocations by expanding by a percentage
00231         XMLSize_t minNewMax = (XMLSize_t)((double)fCurCount * 1.25);
00232         if (newMax < minNewMax)
00233             newMax = minNewMax;
00234 
00235         TElem* newList = (TElem*) fMemoryManager->allocate
00236         (
00237             newMax * sizeof(TElem)
00238         ); //new TElem[newMax];
00239         for (XMLSize_t index = 0; index < fCurCount; index++)
00240             newList[index] = fElemList[index];
00241 
00242         fMemoryManager->deallocate(fElemList); //delete [] fElemList;
00243         fElemList = newList;
00244         fMaxCount = newMax;
00245     }
00246 }
00247 
00248 template <class TElem> const TElem* ValueVectorOf<TElem>::rawData() const
00249 {
00250     return fElemList;
00251 }
00252 
00253 
00254 
00255 // ---------------------------------------------------------------------------
00256 //  ValueVectorEnumerator: Constructors and Destructor
00257 // ---------------------------------------------------------------------------
00258 template <class TElem> ValueVectorEnumerator<TElem>::
00259 ValueVectorEnumerator(       ValueVectorOf<TElem>* const toEnum
00260                      , const bool                        adopt) :
00261     fAdopted(adopt)
00262     , fCurIndex(0)
00263     , fToEnum(toEnum)
00264 {
00265 }
00266 
00267 template <class TElem> ValueVectorEnumerator<TElem>::~ValueVectorEnumerator()
00268 {
00269     if (fAdopted)
00270         delete fToEnum;
00271 }
00272 
00273 
00274 // ---------------------------------------------------------------------------
00275 //  ValueVectorEnumerator: Enum interface
00276 // ---------------------------------------------------------------------------
00277 template <class TElem> bool
00278 ValueVectorEnumerator<TElem>::hasMoreElements() const
00279 {
00280     if (fCurIndex >= fToEnum->size())
00281         return false;
00282     return true;
00283 }
00284 
00285 template <class TElem> TElem& ValueVectorEnumerator<TElem>::nextElement()
00286 {
00287     return fToEnum->elementAt(fCurIndex++);
00288 }
00289 
00290 template <class TElem> void ValueVectorEnumerator<TElem>::Reset()
00291 {
00292     fCurIndex = 0;
00293 }
00294 
00295 XERCES_CPP_NAMESPACE_END