GME  13
DOMNodeVector.cpp
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: DOMNodeVector.cpp 678709 2008-07-22 10:56:56Z borisk $
00020  */
00021 
00022 //
00023 //      file:   DOMNodeVector.cpp
00024 //                      Implementation of class DOMNodeVector.
00025 //                      (Use of STL vector, or equivalent, would have been nice,
00026 //                      but is not available.  'DOMNode *' is the only type
00027 //                      kept in Vectors in this DOM implementation, so this is
00028 //                      a hardwired implementation for that type.
00029 //
00030 
00031 #include "DOMNodeVector.hpp"
00032 #include "DOMDocumentImpl.hpp"
00033 #include <assert.h>
00034 
00035 XERCES_CPP_NAMESPACE_BEGIN
00036 
00037 
00038 DOMNodeVector::DOMNodeVector(DOMDocument *doc)
00039 {
00040         init(doc, 10);
00041 }
00042 
00043 DOMNodeVector::DOMNodeVector(DOMDocument *doc, XMLSize_t size) {
00044         init(doc, size);
00045 }
00046 
00047 
00048 void DOMNodeVector::init(DOMDocument *doc, XMLSize_t size) {
00049     assert(size > 0);
00050     data = (DOMNode**) ((DOMDocumentImpl *)doc)->allocate(sizeof(DOMNode*) * size);
00051     assert(data != 0);
00052     for (XMLSize_t i=0; i<size; i++)
00053         data[i] = 0;
00054     allocatedSize = size;
00055     nextFreeSlot = 0;
00056 }
00057 
00058 
00059 DOMNodeVector::~DOMNodeVector() {
00060 }
00061 
00062 
00063 void DOMNodeVector::addElement(DOMNode *elem) {
00064         checkSpace();
00065         data[nextFreeSlot] = elem;
00066         ++nextFreeSlot;
00067 }
00068 
00069 
00070 void DOMNodeVector::checkSpace() {
00071     if (nextFreeSlot == allocatedSize) {
00072         XMLSize_t grow = allocatedSize/2;
00073         if (grow < 10) grow = 10;
00074         const XMLSize_t newAllocatedSize = allocatedSize + grow;
00075         DOMDocument *doc = data[0]->getOwnerDocument();
00076 
00077         //DOMNode **newData = new (doc) DOMNode *[newAllocatedSize];
00078         DOMNode **newData = (DOMNode**) ((DOMDocumentImpl *)doc)->allocate(sizeof(DOMNode*) * newAllocatedSize);
00079 
00080         assert(newData != 0);
00081         for (XMLSize_t i=0; i<allocatedSize; i++) {
00082             newData[i] = data[i];
00083         }
00084         // delete [] data;  // revisit.  Can't delete!  Recycle?
00085         allocatedSize = newAllocatedSize;
00086         data = newData;
00087     }
00088 }
00089 
00090 
00091 void DOMNodeVector::insertElementAt(DOMNode *elem, XMLSize_t index) {
00092 
00093         assert(index <= nextFreeSlot);
00094 
00095         checkSpace();
00096         for (XMLSize_t i=nextFreeSlot; i>index; --i) {
00097                 data[i] = data[i-1];
00098         }
00099         data[index] = elem;
00100         ++nextFreeSlot;
00101 
00102 }
00103 
00104 
00105 void DOMNodeVector::removeElementAt(XMLSize_t index) {
00106         assert(index < nextFreeSlot);
00107         for (XMLSize_t i=index; i<nextFreeSlot-1; ++i) {
00108                 data[i] = data[i+1];
00109         }
00110         --nextFreeSlot;
00111 }
00112 
00113 void DOMNodeVector::reset() {
00114         nextFreeSlot = 0;
00115 }
00116 
00117 void DOMNodeVector::setElementAt(DOMNode *elem, XMLSize_t index) {
00118         assert(index < nextFreeSlot);
00119         data[index] = elem;
00120 }
00121 
00122 
00123 XERCES_CPP_NAMESPACE_END