source: icXML/icXML-devel/src/icxercesc/util/NameIdPool.c

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

Initial check-in of icXML 0.8 source files

File size: 8.9 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: NameIdPool.c 883368 2009-11-23 15:28:19Z amassari $
20 */
21
22
23// ---------------------------------------------------------------------------
24//  Includes
25// ---------------------------------------------------------------------------
26#if defined(XERCES_TMPLSINC)
27#include <xercesc/util/NameIdPool.hpp>
28#endif
29
30#include <xercesc/util/IllegalArgumentException.hpp>
31#include <xercesc/util/NoSuchElementException.hpp>
32#include <xercesc/util/RuntimeException.hpp>
33#include <new>
34#include <assert.h>
35
36XERCES_CPP_NAMESPACE_BEGIN
37
38// ---------------------------------------------------------------------------
39//  NameIdPool: Constructors and Destructor
40// ---------------------------------------------------------------------------
41template <class TElem>
42NameIdPool<TElem>::NameIdPool( const XMLSize_t      hashModulus
43                             , const XMLSize_t      initSize
44                             , MemoryManager* const manager) :
45    fMemoryManager(manager)
46    , fIdPtrs(0)
47    , fIdPtrsCount(initSize)
48    , fIdCounter(0)
49    , fBucketList(hashModulus, manager)
50{
51    if (!hashModulus)
52        ThrowXMLwithMemMgr(IllegalArgumentException, XMLExcepts::Pool_ZeroModulus, fMemoryManager);
53
54    //
55    //  Allocate the initial id pointers array. We don't have to zero them
56    //  out since the fIdCounter value tells us which ones are valid. The
57    //  zeroth element is never used (and represents an invalid pool id.)
58    //
59    if (!fIdPtrsCount)
60        fIdPtrsCount = 256;
61    fIdPtrs = (TElem**) fMemoryManager->allocate
62    (
63        fIdPtrsCount * sizeof(TElem*)
64    );
65    fIdPtrs[0] = 0;
66}
67
68template <class TElem> NameIdPool<TElem>::~NameIdPool()
69{
70    //
71    //  Delete the id pointers list. The stuff it points to will be cleaned
72    //  up when we clean the bucket lists.
73    //
74    fMemoryManager->deallocate(fIdPtrs); //delete [] fIdPtrs;
75}
76
77
78// ---------------------------------------------------------------------------
79//  NameIdPool: Element management
80// ---------------------------------------------------------------------------
81template <class TElem>
82inline bool NameIdPool<TElem>::
83containsKey(const XMLCh* const key) const
84{
85    if (fIdCounter == 0) return false;
86    return fBucketList.containsKey(key);
87}
88
89
90template <class TElem> void NameIdPool<TElem>::removeAll()
91{
92    if (fIdCounter == 0) return;
93
94    fBucketList.removeAll();
95
96    // Reset the id counter
97    fIdCounter = 0;
98}
99
100
101// ---------------------------------------------------------------------------
102//  NameIdPool: Getters
103// ---------------------------------------------------------------------------
104template <class TElem>
105inline TElem* NameIdPool<TElem>::
106getByKey(const XMLCh* const key)
107{
108    if (fIdCounter == 0) return 0;
109    return fBucketList.get(key);
110}
111
112template <class TElem>
113inline const TElem* NameIdPool<TElem>::
114getByKey(const XMLCh* const key) const
115{
116    if (fIdCounter == 0) return 0;
117    return fBucketList.get(key);
118}
119
120template <class TElem>
121inline TElem* NameIdPool<TElem>::
122getById(const XMLSize_t elemId)
123{
124    // If its either zero or beyond our current id, its an error
125    if (!elemId || (elemId > fIdCounter))
126        ThrowXMLwithMemMgr(IllegalArgumentException, XMLExcepts::Pool_InvalidId, fMemoryManager);
127
128    return fIdPtrs[elemId];
129}
130
131template <class TElem>
132inline const TElem* NameIdPool<TElem>::
133getById(const XMLSize_t elemId) const
134{
135    // If its either zero or beyond our current id, its an error
136    if (!elemId || (elemId > fIdCounter))
137        ThrowXMLwithMemMgr(IllegalArgumentException, XMLExcepts::Pool_InvalidId, fMemoryManager);
138
139    return fIdPtrs[elemId];
140}
141
142template <class TElem>
143inline MemoryManager* NameIdPool<TElem>::getMemoryManager() const
144{
145    return fMemoryManager;
146}
147
148// ---------------------------------------------------------------------------
149//  NameIdPool: Setters
150// ---------------------------------------------------------------------------
151template <class TElem>
152XMLSize_t NameIdPool<TElem>::put(TElem* const elemToAdopt)
153{
154#pragma warning determine how safe it is to replace "if (containsKey(...))" with "assert(!containsKey(...))"
155
156    // First see if the key exists already. If so, its an error
157    if (containsKey(elemToAdopt->getKey()))
158    {
159        ThrowXMLwithMemMgr1
160        (
161            IllegalArgumentException
162            , XMLExcepts::Pool_ElemAlreadyExists
163            , elemToAdopt->getKey()
164            , fMemoryManager
165        );
166    }
167
168    fBucketList.put((void*)elemToAdopt->getKey(), elemToAdopt);
169
170#pragma warning fIdPtrs are only used by the iterators; do we really need them?
171
172    //
173    //  Give this new one the next available id and add to the pointer list.
174    //  Expand the list if that is now required.
175    //
176    if (fIdCounter + 1 == fIdPtrsCount)
177    {
178        // Create a new count 1.5 times larger and allocate a new array
179        XMLSize_t newCount = (XMLSize_t)(fIdPtrsCount * 1.5);
180        TElem** newArray = (TElem**) fMemoryManager->allocate(newCount * sizeof(TElem*));
181
182        // Copy over the old contents to the new array
183        memcpy(newArray, fIdPtrs, fIdPtrsCount * sizeof(TElem*));
184
185        // Ok, toss the old array and store the new data
186        fMemoryManager->deallocate(fIdPtrs); //delete [] fIdPtrs;
187        fIdPtrs = newArray;
188        fIdPtrsCount = newCount;
189    }
190    const XMLSize_t retId = ++fIdCounter;
191    fIdPtrs[retId] = elemToAdopt;
192
193    // Set the id on the passed element
194    elemToAdopt->setId(retId);
195
196    // Return the id that we gave to this element
197    return retId;
198}
199
200
201// ---------------------------------------------------------------------------
202//  NameIdPoolEnumerator: Constructors and Destructor
203// ---------------------------------------------------------------------------
204template <class TElem> NameIdPoolEnumerator<TElem>::
205NameIdPoolEnumerator(NameIdPool<TElem>* const toEnum
206                     , MemoryManager* const manager) :
207
208    XMLEnumerator<TElem>()
209    , fCurIndex(0)
210    , fToEnum(toEnum)
211    , fMemoryManager(manager)
212{
213    Reset();
214}
215
216template <class TElem> NameIdPoolEnumerator<TElem>::
217NameIdPoolEnumerator(const NameIdPoolEnumerator<TElem>& toCopy) :
218    XMLEnumerator<TElem>(toCopy)
219    , XMemory(toCopy)
220    , fCurIndex(toCopy.fCurIndex)
221    , fToEnum(toCopy.fToEnum)
222    , fMemoryManager(toCopy.fMemoryManager)
223{
224}
225
226template <class TElem> NameIdPoolEnumerator<TElem>::~NameIdPoolEnumerator()
227{
228    // We don't own the pool being enumerated, so no cleanup required
229}
230
231
232// ---------------------------------------------------------------------------
233//  NameIdPoolEnumerator: Public operators
234// ---------------------------------------------------------------------------
235template <class TElem> NameIdPoolEnumerator<TElem>& NameIdPoolEnumerator<TElem>::
236operator=(const NameIdPoolEnumerator<TElem>& toAssign)
237{
238    if (this == &toAssign)
239        return *this;
240    fMemoryManager = toAssign.fMemoryManager;
241    fCurIndex      = toAssign.fCurIndex;
242    fToEnum        = toAssign.fToEnum;
243    return *this;
244}
245
246// ---------------------------------------------------------------------------
247//  NameIdPoolEnumerator: Enum interface
248// ---------------------------------------------------------------------------
249template <class TElem> bool NameIdPoolEnumerator<TElem>::
250hasMoreElements() const
251{
252    // If our index is zero or past the end, then we are done
253    return ((fCurIndex > 0) && (fCurIndex <= fToEnum->fIdCounter));
254}
255
256template <class TElem> TElem& NameIdPoolEnumerator<TElem>::nextElement()
257{
258    // If our index is zero or past the end, then we are done
259    if (!fCurIndex || (fCurIndex > fToEnum->fIdCounter))
260        ThrowXMLwithMemMgr(NoSuchElementException, XMLExcepts::Enum_NoMoreElements, fMemoryManager);
261
262    // Return the current element and bump the index
263    return *fToEnum->fIdPtrs[fCurIndex++];
264}
265
266
267template <class TElem> void NameIdPoolEnumerator<TElem>::Reset()
268{
269    //
270    //  Find the next available bucket element in the pool. We use the id
271    //  array since its very easy to enumerator through by just maintaining
272    //  an index. If the id counter is zero, then its empty and we leave the
273    //  current index to zero.
274    //
275    fCurIndex = fToEnum->fIdCounter ? 1:0;
276}
277
278template <class TElem> XMLSize_t NameIdPoolEnumerator<TElem>::size() const
279{
280    return fToEnum->fIdCounter;
281}
282
283XERCES_CPP_NAMESPACE_END
Note: See TracBrowser for help on using the repository browser.