[2674] | 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 568078 2007-08-21 11:43:25Z 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 | |
---|
| 31 | XERCES_CPP_NAMESPACE_BEGIN |
---|
| 32 | |
---|
| 33 | // --------------------------------------------------------------------------- |
---|
| 34 | // ValueVectorOf: Constructors and Destructor |
---|
| 35 | // --------------------------------------------------------------------------- |
---|
| 36 | template <class TElem> |
---|
| 37 | ValueVectorOf<TElem>::ValueVectorOf(const unsigned int 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 | |
---|
| 55 | template <class TElem> |
---|
| 56 | ValueVectorOf<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 (unsigned int index = 0; index < fCurCount; index++) |
---|
| 71 | fElemList[index] = toCopy.fElemList[index]; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | template <class TElem> ValueVectorOf<TElem>::~ValueVectorOf() |
---|
| 75 | { |
---|
| 76 | if (fCallDestructor) { |
---|
| 77 | for (int index= fMaxCount - 1; index >= 0; index--) |
---|
| 78 | fElemList[index].~TElem(); |
---|
| 79 | } |
---|
| 80 | fMemoryManager->deallocate(fElemList); //delete [] fElemList; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | |
---|
| 84 | |
---|
| 85 | // --------------------------------------------------------------------------- |
---|
| 86 | // ValueVectorOf: Operators |
---|
| 87 | // --------------------------------------------------------------------------- |
---|
| 88 | template <class TElem> ValueVectorOf<TElem>& |
---|
| 89 | ValueVectorOf<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 (unsigned int index = 0; index < fCurCount; index++) |
---|
| 107 | fElemList[index] = toAssign.fElemList[index]; |
---|
| 108 | |
---|
| 109 | return *this; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | |
---|
| 113 | // --------------------------------------------------------------------------- |
---|
| 114 | // ValueVectorOf: Element management |
---|
| 115 | // --------------------------------------------------------------------------- |
---|
| 116 | template <class TElem> void ValueVectorOf<TElem>::addElement(const TElem& toAdd) |
---|
| 117 | { |
---|
| 118 | ensureExtraCapacity(1); |
---|
| 119 | fElemList[fCurCount] = toAdd; |
---|
| 120 | fCurCount++; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | template <class TElem> void ValueVectorOf<TElem>:: |
---|
| 124 | setElementAt(const TElem& toSet, const unsigned int setAt) |
---|
| 125 | { |
---|
| 126 | if (setAt >= fCurCount) |
---|
| 127 | ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager); |
---|
| 128 | fElemList[setAt] = toSet; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | template <class TElem> void ValueVectorOf<TElem>:: |
---|
| 132 | insertElementAt(const TElem& toInsert, const unsigned int insertAt) |
---|
| 133 | { |
---|
| 134 | if (insertAt == fCurCount) |
---|
| 135 | { |
---|
| 136 | addElement(toInsert); |
---|
| 137 | return; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | if (insertAt > fCurCount) |
---|
| 141 | ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager); |
---|
| 142 | |
---|
| 143 | // Make room for the newbie |
---|
| 144 | ensureExtraCapacity(1); |
---|
| 145 | for (unsigned int index = fCurCount; index > insertAt; index--) |
---|
| 146 | fElemList[index] = fElemList[index-1]; |
---|
| 147 | |
---|
| 148 | // And stick it in and bump the count |
---|
| 149 | fElemList[insertAt] = toInsert; |
---|
| 150 | fCurCount++; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | template <class TElem> void ValueVectorOf<TElem>:: |
---|
| 154 | removeElementAt(const unsigned int removeAt) |
---|
| 155 | { |
---|
| 156 | if (removeAt >= fCurCount) |
---|
| 157 | ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager); |
---|
| 158 | |
---|
| 159 | if (removeAt == fCurCount-1) |
---|
| 160 | { |
---|
| 161 | fCurCount--; |
---|
| 162 | return; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | // Copy down every element above remove point |
---|
| 166 | for (unsigned int index = removeAt; index < fCurCount-1; index++) |
---|
| 167 | fElemList[index] = fElemList[index+1]; |
---|
| 168 | |
---|
| 169 | // And bump down count |
---|
| 170 | fCurCount--; |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | template <class TElem> void ValueVectorOf<TElem>::removeAllElements() |
---|
| 174 | { |
---|
| 175 | fCurCount = 0; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | template <class TElem> |
---|
| 179 | bool ValueVectorOf<TElem>::containsElement(const TElem& toCheck, |
---|
| 180 | const unsigned int startIndex) { |
---|
| 181 | |
---|
| 182 | for (unsigned int i = startIndex; i < fCurCount; i++) { |
---|
| 183 | if (fElemList[i] == toCheck) { |
---|
| 184 | return true; |
---|
| 185 | } |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | return false; |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | |
---|
| 192 | // --------------------------------------------------------------------------- |
---|
| 193 | // ValueVectorOf: Getter methods |
---|
| 194 | // --------------------------------------------------------------------------- |
---|
| 195 | template <class TElem> const TElem& ValueVectorOf<TElem>:: |
---|
| 196 | elementAt(const unsigned int getAt) const |
---|
| 197 | { |
---|
| 198 | if (getAt >= fCurCount) |
---|
| 199 | ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager); |
---|
| 200 | return fElemList[getAt]; |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | template <class TElem> TElem& ValueVectorOf<TElem>:: |
---|
| 204 | elementAt(const unsigned int getAt) |
---|
| 205 | { |
---|
| 206 | if (getAt >= fCurCount) |
---|
| 207 | ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager); |
---|
| 208 | return fElemList[getAt]; |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | template <class TElem> unsigned int ValueVectorOf<TElem>::curCapacity() const |
---|
| 212 | { |
---|
| 213 | return fMaxCount; |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | template <class TElem> unsigned int ValueVectorOf<TElem>::size() const |
---|
| 217 | { |
---|
| 218 | return fCurCount; |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | template <class TElem> |
---|
| 222 | MemoryManager* ValueVectorOf<TElem>::getMemoryManager() const |
---|
| 223 | { |
---|
| 224 | return fMemoryManager; |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | // --------------------------------------------------------------------------- |
---|
| 228 | // ValueVectorOf: Miscellaneous |
---|
| 229 | // --------------------------------------------------------------------------- |
---|
| 230 | template <class TElem> void ValueVectorOf<TElem>:: |
---|
| 231 | ensureExtraCapacity(const unsigned int length) |
---|
| 232 | { |
---|
| 233 | unsigned int newMax = fCurCount + length; |
---|
| 234 | |
---|
| 235 | if (newMax <= fMaxCount) |
---|
| 236 | return; |
---|
| 237 | |
---|
| 238 | // Avoid too many reallocations by expanding by a percentage |
---|
| 239 | unsigned int minNewMax = (unsigned int)((double)fCurCount * 1.25); |
---|
| 240 | if (newMax < minNewMax) |
---|
| 241 | newMax = minNewMax; |
---|
| 242 | |
---|
| 243 | TElem* newList = (TElem*) fMemoryManager->allocate |
---|
| 244 | ( |
---|
| 245 | newMax * sizeof(TElem) |
---|
| 246 | ); //new TElem[newMax]; |
---|
| 247 | for (unsigned int index = 0; index < fCurCount; index++) |
---|
| 248 | newList[index] = fElemList[index]; |
---|
| 249 | |
---|
| 250 | fMemoryManager->deallocate(fElemList); //delete [] fElemList; |
---|
| 251 | fElemList = newList; |
---|
| 252 | fMaxCount = newMax; |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | template <class TElem> const TElem* ValueVectorOf<TElem>::rawData() const |
---|
| 256 | { |
---|
| 257 | return fElemList; |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | |
---|
| 261 | |
---|
| 262 | // --------------------------------------------------------------------------- |
---|
| 263 | // ValueVectorEnumerator: Constructors and Destructor |
---|
| 264 | // --------------------------------------------------------------------------- |
---|
| 265 | template <class TElem> ValueVectorEnumerator<TElem>:: |
---|
| 266 | ValueVectorEnumerator( ValueVectorOf<TElem>* const toEnum |
---|
| 267 | , const bool adopt) : |
---|
| 268 | fAdopted(adopt) |
---|
| 269 | , fCurIndex(0) |
---|
| 270 | , fToEnum(toEnum) |
---|
| 271 | { |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | template <class TElem> ValueVectorEnumerator<TElem>::~ValueVectorEnumerator() |
---|
| 275 | { |
---|
| 276 | if (fAdopted) |
---|
| 277 | delete fToEnum; |
---|
| 278 | } |
---|
| 279 | |
---|
| 280 | |
---|
| 281 | // --------------------------------------------------------------------------- |
---|
| 282 | // ValueVectorEnumerator: Enum interface |
---|
| 283 | // --------------------------------------------------------------------------- |
---|
| 284 | template <class TElem> bool |
---|
| 285 | ValueVectorEnumerator<TElem>::hasMoreElements() const |
---|
| 286 | { |
---|
| 287 | if (fCurIndex >= fToEnum->size()) |
---|
| 288 | return false; |
---|
| 289 | return true; |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | template <class TElem> TElem& ValueVectorEnumerator<TElem>::nextElement() |
---|
| 293 | { |
---|
| 294 | return fToEnum->elementAt(fCurIndex++); |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | template <class TElem> void ValueVectorEnumerator<TElem>::Reset() |
---|
| 298 | { |
---|
| 299 | fCurIndex = 0; |
---|
| 300 | } |
---|
| 301 | |
---|
| 302 | XERCES_CPP_NAMESPACE_END |
---|