source: icXML/icXML-devel/src/xercesc/dom/impl/DOMNodeVector.cpp @ 2777

Last change on this file since 2777 was 2777, checked in by cameron, 7 years ago

Set up to use xercesc/icxercesc root-relative paths for all includes

File size: 3.2 KB
Line 
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: DOMNodeVector.cpp 678709 2008-07-22 10:56:56Z borisk $
20 */
21
22//
23//      file:   DOMNodeVector.cpp
24//                      Implementation of class DOMNodeVector.
25//                      (Use of STL vector, or equivalent, would have been nice,
26//                      but is not available.  'DOMNode *' is the only type
27//                      kept in Vectors in this DOM implementation, so this is
28//                      a hardwired implementation for that type.
29//
30
31#include <xercesc/dom/impl/DOMNodeVector.hpp>
32#include <xercesc/dom/impl/DOMDocumentImpl.hpp>
33#include <assert.h>
34
35XERCES_CPP_NAMESPACE_BEGIN
36
37
38DOMNodeVector::DOMNodeVector(DOMDocument *doc)
39{
40        init(doc, 10);
41}
42
43DOMNodeVector::DOMNodeVector(DOMDocument *doc, XMLSize_t size) {
44        init(doc, size);
45}
46
47
48void DOMNodeVector::init(DOMDocument *doc, XMLSize_t size) {
49    assert(size > 0);
50    data = (DOMNode**) ((DOMDocumentImpl *)doc)->allocate(sizeof(DOMNode*) * size);
51    assert(data != 0);
52    for (XMLSize_t i=0; i<size; i++)
53        data[i] = 0;
54    allocatedSize = size;
55    nextFreeSlot = 0;
56}
57
58
59DOMNodeVector::~DOMNodeVector() {
60}
61
62
63void DOMNodeVector::addElement(DOMNode *elem) {
64        checkSpace();
65        data[nextFreeSlot] = elem;
66        ++nextFreeSlot;
67}
68
69
70void DOMNodeVector::checkSpace() {
71    if (nextFreeSlot == allocatedSize) {
72        XMLSize_t grow = allocatedSize/2;
73        if (grow < 10) grow = 10;
74        const XMLSize_t newAllocatedSize = allocatedSize + grow;
75        DOMDocument *doc = data[0]->getOwnerDocument();
76
77        //DOMNode **newData = new (doc) DOMNode *[newAllocatedSize];
78        DOMNode **newData = (DOMNode**) ((DOMDocumentImpl *)doc)->allocate(sizeof(DOMNode*) * newAllocatedSize);
79
80        assert(newData != 0);
81        for (XMLSize_t i=0; i<allocatedSize; i++) {
82            newData[i] = data[i];
83        }
84        // delete [] data;  // revisit.  Can't delete!  Recycle?
85        allocatedSize = newAllocatedSize;
86        data = newData;
87    }
88}
89
90
91void DOMNodeVector::insertElementAt(DOMNode *elem, XMLSize_t index) {
92
93        assert(index <= nextFreeSlot);
94
95        checkSpace();
96        for (XMLSize_t i=nextFreeSlot; i>index; --i) {
97                data[i] = data[i-1];
98        }
99        data[index] = elem;
100        ++nextFreeSlot;
101
102}
103
104
105void DOMNodeVector::removeElementAt(XMLSize_t index) {
106        assert(index < nextFreeSlot);
107        for (XMLSize_t i=index; i<nextFreeSlot-1; ++i) {
108                data[i] = data[i+1];
109        }
110        --nextFreeSlot;
111}
112
113void DOMNodeVector::reset() {
114        nextFreeSlot = 0;
115}
116
117void DOMNodeVector::setElementAt(DOMNode *elem, XMLSize_t index) {
118        assert(index < nextFreeSlot);
119        data[index] = elem;
120}
121
122
123XERCES_CPP_NAMESPACE_END
Note: See TracBrowser for help on using the repository browser.