source: icXML/icXML-devel/src/xercesc/util/RefHash3KeysIdPool.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: 10.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: RefHash3KeysIdPool.hpp 883368 2009-11-23 15:28:19Z amassari $
20 */
21
22#if !defined(XERCESC_INCLUDE_GUARD_REFHASH3KEYSIDPOOL_HPP)
23#define XERCESC_INCLUDE_GUARD_REFHASH3KEYSIDPOOL_HPP
24
25
26#include <icxercesc/util/Hashers.hpp>
27#include <xercesc/util/IllegalArgumentException.hpp>
28#include <xercesc/util/NoSuchElementException.hpp>
29#include <xercesc/util/RuntimeException.hpp>
30#include <icxercesc/util/PlatformUtils.hpp>
31
32XERCES_CPP_NAMESPACE_BEGIN
33
34// This hash table is a combination of RefHash2KeyTableOf (with an additional integer as key3)
35// and NameIdPool with an id as index
36
37//  Forward declare the enumerator so it can be our friend.
38//
39template <class TVal, class THasher = StringHasher>
40class RefHash3KeysIdPoolEnumerator;
41
42
43//
44//  This should really be a nested class, but some of the compilers we
45//  have to support cannot deal with that!
46//
47template <class TVal>
48struct RefHash3KeysTableBucketElem
49{
50    RefHash3KeysTableBucketElem(
51              void* key1
52              , int key2
53              , int key3
54              , TVal* const value
55              , RefHash3KeysTableBucketElem<TVal>* next) :
56                fData(value)
57    , fNext(next)
58    , fKey1(key1)
59    , fKey2(key2)
60    , fKey3(key3)
61    {
62    }
63
64    RefHash3KeysTableBucketElem() {};
65    ~RefHash3KeysTableBucketElem() {};
66
67    TVal*  fData;
68    RefHash3KeysTableBucketElem<TVal>*   fNext;
69    void*  fKey1;
70    int    fKey2;
71    int    fKey3;
72
73private:
74    // -----------------------------------------------------------------------
75    //  Unimplemented constructors and operators
76    // -----------------------------------------------------------------------
77    RefHash3KeysTableBucketElem(const RefHash3KeysTableBucketElem<TVal>&);
78    RefHash3KeysTableBucketElem<TVal>& operator=(const RefHash3KeysTableBucketElem<TVal>&);
79};
80
81
82template <class TVal, class THasher = StringHasher>
83class RefHash3KeysIdPool : public XMemory
84{
85public:
86    // -----------------------------------------------------------------------
87    //  Constructors and Destructor
88    // -----------------------------------------------------------------------
89    RefHash3KeysIdPool(
90      const XMLSize_t modulus,
91      const XMLSize_t initSize = 128,
92      MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
93
94    RefHash3KeysIdPool(
95      const XMLSize_t modulus,
96      const THasher& hasher,
97      const XMLSize_t initSize = 128,
98      MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
99
100    RefHash3KeysIdPool(
101      const XMLSize_t modulus,
102      const bool adoptElems,
103      const XMLSize_t initSize = 128,
104      MemoryManager* const manager =  XMLPlatformUtils::fgMemoryManager);
105
106    RefHash3KeysIdPool(
107      const XMLSize_t modulus,
108      const bool adoptElems,
109      const THasher& hasher,
110      const XMLSize_t initSize = 128,
111      MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
112
113    ~RefHash3KeysIdPool();
114
115    // -----------------------------------------------------------------------
116    //  Element management
117    // -----------------------------------------------------------------------
118    bool isEmpty() const;
119    bool containsKey(const void* const key1, const int key2, const int key3) const;
120    void removeAll();
121
122
123    // -----------------------------------------------------------------------
124    //  Getters
125    // -----------------------------------------------------------------------
126    TVal* getByKey(const void* const key1, const int key2, const int key3);
127    const TVal* getByKey(const void* const key1, const int key2, const int key3) const;
128
129    TVal* getById(const unsigned int elemId);
130    const TVal* getById(const unsigned int elemId) const;
131
132    MemoryManager* getMemoryManager() const;
133    XMLSize_t      getHashModulus()   const;
134
135    // -----------------------------------------------------------------------
136    //  Putters
137    // -----------------------------------------------------------------------
138    XMLSize_t put(void* key1, int key2, int key3, TVal* const valueToAdopt);
139
140
141private :
142    // -----------------------------------------------------------------------
143    //  Declare our friends
144    // -----------------------------------------------------------------------
145    friend class RefHash3KeysIdPoolEnumerator<TVal, THasher>;
146
147private:
148    // -----------------------------------------------------------------------
149    //  Unimplemented constructors and operators
150    // -----------------------------------------------------------------------
151    RefHash3KeysIdPool(const RefHash3KeysIdPool<TVal, THasher>&);
152    RefHash3KeysIdPool<TVal, THasher>& operator=(const RefHash3KeysIdPool<TVal, THasher>&);
153
154    // -----------------------------------------------------------------------
155    //  Private methods
156    // -----------------------------------------------------------------------
157    RefHash3KeysTableBucketElem<TVal>* findBucketElem(const void* const key1, const int key2, const int key3, XMLSize_t& hashVal);
158    const RefHash3KeysTableBucketElem<TVal>* findBucketElem(const void* const key1, const int key2, const int key3, XMLSize_t& hashVal) const;
159    void initialize(const XMLSize_t modulus);
160
161
162    // -----------------------------------------------------------------------
163    //  Data members
164    //
165    //  fAdoptedElems
166    //      Indicates whether the values added are adopted or just referenced.
167    //      If adopted, then they are deleted when they are removed from the
168    //      hash table.
169    //
170    //  fBucketList
171    //      This is the array that contains the heads of all of the list
172    //      buckets, one for each possible hash value.
173    //
174    //  fHashModulus
175    //      The modulus used for this hash table, to hash the keys. This is
176    //      also the number of elements in the bucket list.
177    //
178    //  fHash
179    //      The hasher for the key1 data type.
180    //
181    //  fIdPtrs
182    //  fIdPtrsCount
183    //      This is the array of pointers to the bucket elements in order of
184    //      their assigned ids. So taking id N and referencing this array
185    //      gives you the element with that id. The count field indicates
186    //      the current size of this list. When fIdCounter+1 reaches this
187    //      value the list must be expanded.
188    //
189    //  fIdCounter
190    //      This is used to give out unique ids to added elements. It starts
191    //      at zero (which means empty), and is bumped up for each newly added
192    //      element. So the first element is 1, the next is 2, etc... This
193    //      means that this value is set to the top index of the fIdPtrs array.
194    // -----------------------------------------------------------------------
195    MemoryManager*                      fMemoryManager;
196    bool                                fAdoptedElems;
197    RefHash3KeysTableBucketElem<TVal>** fBucketList;
198    XMLSize_t                           fHashModulus;
199    TVal**                              fIdPtrs;
200    XMLSize_t                           fIdPtrsCount;
201    XMLSize_t                           fIdCounter;
202    THasher                             fHasher;
203};
204
205
206
207//
208//  An enumerator for a value array. It derives from the basic enumerator
209//  class, so that value vectors can be generically enumerated.
210//
211template <class TVal, class THasher>
212class RefHash3KeysIdPoolEnumerator : public XMLEnumerator<TVal>, public XMemory
213{
214public :
215    // -----------------------------------------------------------------------
216    //  Constructors and Destructor
217    // -----------------------------------------------------------------------
218    RefHash3KeysIdPoolEnumerator(RefHash3KeysIdPool<TVal, THasher>* const toEnum
219        , const bool adopt = false
220        , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
221    virtual ~RefHash3KeysIdPoolEnumerator();
222
223    RefHash3KeysIdPoolEnumerator(const RefHash3KeysIdPoolEnumerator<TVal, THasher>&);
224    // -----------------------------------------------------------------------
225    //  Enum interface
226    // -----------------------------------------------------------------------
227    bool hasMoreElements() const;
228    TVal& nextElement();
229    void Reset();
230    XMLSize_t size() const;
231
232    // -----------------------------------------------------------------------
233    //  New interface
234    // -----------------------------------------------------------------------
235    void resetKey();
236    void nextElementKey(void*&, int&, int&);
237    bool hasMoreKeys()   const;
238
239private :
240    // -----------------------------------------------------------------------
241    //  Unimplemented constructors and operators
242    // -----------------------------------------------------------------------
243    RefHash3KeysIdPoolEnumerator<TVal, THasher>&
244    operator=(const RefHash3KeysIdPoolEnumerator<TVal, THasher>&);
245
246    // -----------------------------------------------------------------------
247    //  Private methods
248    // -----------------------------------------------------------------------
249    void findNext();
250
251    // -----------------------------------------------------------------------
252    //  Data Members
253    //  fAdoptedElems
254    //      Indicates whether the values added are adopted or just referenced.
255    //      If adopted, then they are deleted when they are removed from the
256    //      hash table
257    //
258    //  fCurIndex
259    //      This is the current index into the pool's id mapping array. This
260    //      is now we enumerate it.
261    //
262    //  fToEnum
263    //      The name id pool that is being enumerated.
264    // -----------------------------------------------------------------------
265    bool                                fAdoptedElems;
266    XMLSize_t                           fCurIndex;
267    RefHash3KeysIdPool<TVal, THasher>*  fToEnum;
268    RefHash3KeysTableBucketElem<TVal>*  fCurElem;
269    XMLSize_t                           fCurHash;
270    MemoryManager* const                fMemoryManager;
271};
272
273XERCES_CPP_NAMESPACE_END
274
275#if !defined(XERCES_TMPLSINC)
276#include <xercesc/util/RefHash3KeysIdPool.c>
277#endif
278
279#endif
Note: See TracBrowser for help on using the repository browser.