source: NonGTP/Xerces/xerces/include/xercesc/util/RefHash2KeysTableOf.hpp @ 358

Revision 358, 11.6 KB checked in by bittner, 19 years ago (diff)

xerces added

Line 
1/*
2 * Copyright 2001-2004 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * $Log: RefHash2KeysTableOf.hpp,v $
19 * Revision 1.12  2004/09/08 13:56:22  peiyongz
20 * Apache License Version 2.0
21 *
22 * Revision 1.11  2004/08/30 15:18:35  amassari
23 * - Added transferElement API
24 * - The iterator class now can iterate over the items having the same primary key
25 *
26 * Revision 1.10  2004/03/01 15:03:08  peiyongz
27 * new getter: getHashModulus
28 *
29 * Revision 1.9  2004/01/29 11:48:46  cargilld
30 * Code cleanup changes to get rid of various compiler diagnostic messages.
31 *
32 * Revision 1.8  2003/12/17 00:18:35  cargilld
33 * Update to memory management so that the static memory manager (one used to call Initialize) is only for static data.
34 *
35 * Revision 1.7  2003/10/20 11:45:06  gareth
36 * Made enumerators inherit from XMemory.
37 *
38 * Revision 1.6  2003/10/17 21:10:40  peiyongz
39 * nextElementKey() added
40 *
41 * Revision 1.5  2003/05/18 14:02:05  knoaman
42 * Memory manager implementation: pass per instance manager.
43 *
44 * Revision 1.4  2003/05/15 19:04:35  knoaman
45 * Partial implementation of the configurable memory manager.
46 *
47 * Revision 1.3  2002/11/04 15:22:04  tng
48 * C++ Namespace Support.
49 *
50 * Revision 1.2  2002/06/12 17:15:12  tng
51 * Remove redundant include header file.
52 *
53 * Revision 1.1.1.1  2002/02/01 22:22:12  peiyongz
54 * sane_include
55 *
56 * Revision 1.4  2001/12/22 01:06:08  jasons
57 * Made the destructors virtual for:
58 *
59 * * ~RefHash2KeysTableOfEnumerator
60 * * ~RefHash3KeysIdPoolEnumerator
61 *
62 * This fixes bug #5514
63 *
64 * Revision 1.3  2001/06/04 13:45:03  tng
65 * The "hash" argument clashes with STL hash.  Fixed by Pei Yong Zhang.
66 *
67 * Revision 1.2  2001/05/11 13:26:28  tng
68 * Copyright update.
69 *
70 * Revision 1.1  2001/02/27 18:24:01  tng
71 * Schema: Add utility RefHash2KeysTableOf.
72 *
73 */
74
75
76#if !defined(REFHASH2KEYSTABLEOF_HPP)
77#define REFHASH2KEYSTABLEOF_HPP
78
79
80#include <xercesc/util/HashBase.hpp>
81#include <xercesc/util/IllegalArgumentException.hpp>
82#include <xercesc/util/NoSuchElementException.hpp>
83#include <xercesc/util/RuntimeException.hpp>
84#include <xercesc/util/XMLString.hpp>
85#include <xercesc/util/HashXMLCh.hpp>
86#include <xercesc/util/PlatformUtils.hpp>
87
88XERCES_CPP_NAMESPACE_BEGIN
89
90// This hash table is similar to RefHashTableOf with an additional integer as key2
91
92//
93//  Forward declare the enumerator so he can be our friend. Can you say
94//  friend? Sure...
95//
96template <class TVal> class RefHash2KeysTableOfEnumerator;
97template <class TVal> struct RefHash2KeysTableBucketElem;
98
99
100//
101//  This should really be a nested class, but some of the compilers we
102//  have to support cannot deal with that!
103//
104template <class TVal> struct RefHash2KeysTableBucketElem : public XMemory
105{
106    RefHash2KeysTableBucketElem(void* key1, int key2, TVal* const value, RefHash2KeysTableBucketElem<TVal>* next)
107                : fData(value), fNext(next), fKey1(key1), fKey2(key2)
108        {
109        }
110    ~RefHash2KeysTableBucketElem() {};
111
112    TVal*                           fData;
113    RefHash2KeysTableBucketElem<TVal>*   fNext;
114        void*                                                   fKey1;
115        int                                                     fKey2;
116
117private:
118    // -----------------------------------------------------------------------
119    //  Unimplemented constructors and operators
120    // -----------------------------------------------------------------------
121    RefHash2KeysTableBucketElem(const RefHash2KeysTableBucketElem<TVal>&);
122    RefHash2KeysTableBucketElem<TVal>& operator=(const RefHash2KeysTableBucketElem<TVal>&);
123};
124
125
126template <class TVal> class RefHash2KeysTableOf : public XMemory
127{
128public:
129    // -----------------------------------------------------------------------
130    //  Constructors and Destructor
131    // -----------------------------------------------------------------------
132        // backwards compatability - default hasher is HashXMLCh
133    RefHash2KeysTableOf
134    (
135        const unsigned int modulus
136                , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
137    );
138        // backwards compatability - default hasher is HashXMLCh
139    RefHash2KeysTableOf
140    (
141        const unsigned int modulus
142        , const bool adoptElems
143        , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
144    );
145        // if a hash function is passed in, it will be deleted when the hashtable is deleted.
146        // use a new instance of the hasher class for each hashtable, otherwise one hashtable
147        // may delete the hasher of a different hashtable if both use the same hasher.
148    RefHash2KeysTableOf
149    (
150        const unsigned int modulus
151        , const bool adoptElems
152        , HashBase* hashBase
153        , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
154    );
155    ~RefHash2KeysTableOf();
156
157
158    // -----------------------------------------------------------------------
159    //  Element management
160    // -----------------------------------------------------------------------
161    bool isEmpty() const;
162    bool containsKey(const void* const key1, const int key2) const;
163    void removeKey(const void* const key1, const int key2);
164    void removeAll();
165    void transferElement(const void* const key1, void* key2);
166
167    // -----------------------------------------------------------------------
168    //  Getters
169    // -----------------------------------------------------------------------
170    TVal* get(const void* const key1, const int key2);
171    const TVal* get(const void* const key1, const int key2) const;
172
173    MemoryManager* getMemoryManager() const;
174    unsigned int   getHashModulus()   const;
175
176    // -----------------------------------------------------------------------
177    //  Putters
178    // -----------------------------------------------------------------------
179        void put(void* key1, int key2, TVal* const valueToAdopt);
180
181private :
182    // -----------------------------------------------------------------------
183    //  Declare our friends
184    // -----------------------------------------------------------------------
185    friend class RefHash2KeysTableOfEnumerator<TVal>;
186
187   
188private:
189    // -----------------------------------------------------------------------
190    //  Unimplemented constructors and operators
191    // -----------------------------------------------------------------------
192    RefHash2KeysTableOf(const RefHash2KeysTableOf<TVal>&);
193    RefHash2KeysTableOf<TVal>& operator=(const RefHash2KeysTableOf<TVal>&);
194
195    // -----------------------------------------------------------------------
196    //  Private methods
197    // -----------------------------------------------------------------------
198    RefHash2KeysTableBucketElem<TVal>* findBucketElem(const void* const key1, const int key2, unsigned int& hashVal);
199    const RefHash2KeysTableBucketElem<TVal>* findBucketElem(const void* const key1, const int key2, unsigned int& hashVal) const;
200    void removeBucketElem(const void* const key1, const int key2, unsigned int& hashVal);
201        void initialize(const unsigned int modulus);
202
203
204    // -----------------------------------------------------------------------
205    //  Data members
206    //
207    //  fAdoptedElems
208    //      Indicates whether the values added are adopted or just referenced.
209    //      If adopted, then they are deleted when they are removed from the
210    //      hash table.
211    //
212    //  fBucketList
213    //      This is the array that contains the heads of all of the list
214    //      buckets, one for each possible hash value.
215    //
216    //  fHashModulus
217    //      The modulus used for this hash table, to hash the keys. This is
218    //      also the number of elements in the bucket list.
219        //
220        //  fHash
221        //      The hasher for the key1 data type.
222    // -----------------------------------------------------------------------
223    MemoryManager*                      fMemoryManager;
224    bool                                fAdoptedElems;
225    RefHash2KeysTableBucketElem<TVal>** fBucketList;
226    unsigned int                        fHashModulus;
227        HashBase*                                                       fHash;
228};
229
230
231
232//
233//  An enumerator for a value array. It derives from the basic enumerator
234//  class, so that value vectors can be generically enumerated.
235//
236template <class TVal> class RefHash2KeysTableOfEnumerator : public XMLEnumerator<TVal>, public XMemory
237{
238public :
239    // -----------------------------------------------------------------------
240    //  Constructors and Destructor
241    // -----------------------------------------------------------------------
242    RefHash2KeysTableOfEnumerator(RefHash2KeysTableOf<TVal>* const toEnum
243        , const bool adopt = false
244        , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
245    virtual ~RefHash2KeysTableOfEnumerator();
246
247
248    // -----------------------------------------------------------------------
249    //  Enum interface
250    // -----------------------------------------------------------------------
251    bool hasMoreElements() const;
252    TVal& nextElement();
253    void Reset();
254
255    // -----------------------------------------------------------------------
256    //  New interface
257    // -----------------------------------------------------------------------
258    void nextElementKey(void*&, int&);
259    void setPrimaryKey(const void* key);
260
261private :
262    // -----------------------------------------------------------------------
263    //  Unimplemented constructors and operators
264    // -----------------------------------------------------------------------
265    RefHash2KeysTableOfEnumerator(const RefHash2KeysTableOfEnumerator<TVal>&);
266    RefHash2KeysTableOfEnumerator<TVal>& operator=(const RefHash2KeysTableOfEnumerator<TVal>&);
267
268    // -----------------------------------------------------------------------
269    //  Private methods
270    // -----------------------------------------------------------------------
271    void findNext();
272
273
274    // -----------------------------------------------------------------------
275    //  Data Members
276    //
277    //  fAdopted
278    //      Indicates whether we have adopted the passed vector. If so then
279    //      we delete the vector when we are destroyed.
280    //
281    //  fCurElem
282    //      This is the current bucket bucket element that we are on.
283    //
284    //  fCurHash
285    //      The is the current hash buck that we are working on. Once we hit
286    //      the end of the bucket that fCurElem is in, then we have to start
287    //      working this one up to the next non-empty bucket.
288    //
289    //  fToEnum
290    //      The value array being enumerated.
291    //
292    //  fLockPrimaryKey
293    //      Indicates that we are requested to iterate over the secondary keys
294    //      associated with the given primary key
295    //
296    // -----------------------------------------------------------------------
297    bool                                    fAdopted;
298    RefHash2KeysTableBucketElem<TVal>*      fCurElem;
299    unsigned int                            fCurHash;
300    RefHash2KeysTableOf<TVal>*              fToEnum;
301    MemoryManager* const                    fMemoryManager;
302    const void*                             fLockPrimaryKey;
303};
304
305XERCES_CPP_NAMESPACE_END
306
307#if !defined(XERCES_TMPLSINC)
308#include <xercesc/util/RefHash2KeysTableOf.c>
309#endif
310
311#endif
Note: See TracBrowser for help on using the repository browser.