source: icXML/icXML-devel/src/xercesc/util/NameIdPool.hpp @ 2732

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

Original Xerces files with import mods for icxercesc

File size: 7.6 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.hpp 932887 2010-04-11 13:04:59Z borisk $
20 */
21
22#if !defined(XERCESC_INCLUDE_GUARD_NAMEIDPOOL_HPP)
23#define XERCESC_INCLUDE_GUARD_NAMEIDPOOL_HPP
24
25#include <xercesc/util/XMemory.hpp>
26#include <icxercesc/util/XMLString.hpp>
27#include <icxercesc/util/PlatformUtils.hpp>
28#include <icxercesc/util/RefHashTableOf.hpp>
29
30XERCES_CPP_NAMESPACE_BEGIN
31
32//
33//  Forward declare the enumerator so he can be our friend. Can you say
34//  friend? Sure...
35//
36template <class TElem> class NameIdPoolEnumerator;
37
38
39//
40//  This class is provided to serve as the basis of many of the pools that
41//  are used by the scanner and validators. They often need to be able to
42//  store objects in such a way that they can be quickly accessed by the
43//  name field of the object, and such that each element added is assigned
44//  a unique id via which it can be accessed almost instantly.
45//
46//  Object names are enforced as being unique, since that's what all these
47//  pools require. So its effectively a hash table in conjunction with an
48//  array of references into the hash table by id. Ids are assigned such that
49//  id N can be used to get the Nth element from the array of references.
50//  This provides very fast access by id.
51//
52//  The way these pools are used, elements are never removed except when the
53//  whole thing is flushed. This makes it very easy to maintain the two
54//  access methods in sync.
55//
56//  For efficiency reasons, the id reference array is never flushed until
57//  the dtor. This way, it does not have to be regrown every time its reused.
58//
59//  All elements are assumed to be owned by the pool!
60//
61
62template <class TElem> class NameIdPool : public XMemory
63{
64public :
65    // -----------------------------------------------------------------------
66    //  Constructors and Destructor
67    // -----------------------------------------------------------------------
68    NameIdPool
69    (
70        const   XMLSize_t       hashModulus
71        , const XMLSize_t       initSize = 128
72        , MemoryManager* const  manager = XMLPlatformUtils::fgMemoryManager
73    );
74
75    ~NameIdPool();
76
77
78    // -----------------------------------------------------------------------
79    //  Element management
80    // -----------------------------------------------------------------------
81    bool containsKey(const XMLCh* const key) const;
82    void removeAll();
83
84
85    // -----------------------------------------------------------------------
86    //  Getters
87    // -----------------------------------------------------------------------
88    TElem* getByKey(const XMLCh* const key);
89    const TElem* getByKey(const XMLCh* const key) const;
90    TElem* getById(const XMLSize_t elemId);
91    const TElem* getById(const XMLSize_t elemId) const;
92
93    MemoryManager* getMemoryManager() const;
94    // -----------------------------------------------------------------------
95    //  Putters
96    //
97    //  Dups are not allowed and cause an IllegalArgumentException. The id
98    //  of the new element is returned.
99    // -----------------------------------------------------------------------
100    XMLSize_t put(TElem* const valueToAdopt);
101
102
103protected :
104    // -----------------------------------------------------------------------
105    //  Declare the enumerator our friend so he can see our members
106    // -----------------------------------------------------------------------
107    friend class NameIdPoolEnumerator<TElem>;
108
109
110private :
111    // -----------------------------------------------------------------------
112    //  Unused constructors and operators
113    // -----------------------------------------------------------------------
114    NameIdPool(const NameIdPool<TElem>&);
115    NameIdPool<TElem>& operator=(const NameIdPool<TElem>&);
116
117    // -----------------------------------------------------------------------
118    //  Data members
119    //
120    //  fBucketList
121    //      This is the hash table that contains the values.
122    //
123    //  fIdPtrs
124    //  fIdPtrsCount
125    //      This is the array of pointers to the bucket elements in order of
126    //      their assigned ids. So taking id N and referencing this array
127    //      gives you the element with that id. The count field indicates
128    //      the current size of this list. When fIdCounter+1 reaches this
129    //      value the list must be expanded.
130    //
131    //  fIdCounter
132    //      This is used to give out unique ids to added elements. It starts
133    //      at zero (which means empty), and is bumped up for each newly added
134    //      element. So the first element is 1, the next is 2, etc... This
135    //      means that this value is set to the top index of the fIdPtrs array.
136    //
137    // -----------------------------------------------------------------------
138    MemoryManager*                  fMemoryManager;
139    TElem**                         fIdPtrs;
140    XMLSize_t                       fIdPtrsCount;
141    XMLSize_t                       fIdCounter;
142    RefHashTableOf<TElem>           fBucketList;
143};
144
145
146//
147//  An enumerator for a name id pool. It derives from the basic enumerator
148//  class, so that pools can be generically enumerated.
149//
150template <class TElem> class NameIdPoolEnumerator : public XMLEnumerator<TElem>, public XMemory
151{
152public :
153    // -----------------------------------------------------------------------
154    //  Constructors and Destructor
155    // -----------------------------------------------------------------------
156    NameIdPoolEnumerator
157    (
158                NameIdPool<TElem>* const    toEnum
159                , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
160    );
161
162    NameIdPoolEnumerator
163    (
164        const   NameIdPoolEnumerator<TElem>& toCopy
165    );
166
167    virtual ~NameIdPoolEnumerator();
168
169    // -----------------------------------------------------------------------
170    //  Public operators
171    // -----------------------------------------------------------------------
172    NameIdPoolEnumerator<TElem>& operator=
173    (
174        const   NameIdPoolEnumerator<TElem>& toAssign
175    );
176
177    // -----------------------------------------------------------------------
178    //  Enum interface
179    // -----------------------------------------------------------------------
180    bool hasMoreElements() const;
181    TElem& nextElement();
182    void Reset();
183    XMLSize_t size()  const;
184
185private :
186    // -----------------------------------------------------------------------
187    //  Data Members
188    //
189    //  fCurIndex
190    //      This is the current index into the pool's id mapping array. This
191    //      is now we enumerate it.
192    //
193    //  fToEnum
194    //      The name id pool that is being enumerated.
195    // -----------------------------------------------------------------------
196    XMLSize_t               fCurIndex;
197    NameIdPool<TElem>*      fToEnum;
198    MemoryManager*          fMemoryManager;
199};
200
201XERCES_CPP_NAMESPACE_END
202
203#if !defined(XERCES_TMPLSINC)
204#include <icxercesc/util/NameIdPool.c>
205#endif
206
207#endif
Note: See TracBrowser for help on using the repository browser.