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

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

xerces added

Line 
1/*
2 * Copyright 1999-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: RefHashTableOf.hpp,v $
19 * Revision 1.15  2004/09/08 13:56:23  peiyongz
20 * Apache License Version 2.0
21 *
22 * Revision 1.14  2004/03/01 15:03:08  peiyongz
23 * new getter: getHashModulus
24 *
25 * Revision 1.13  2004/01/29 11:48:46  cargilld
26 * Code cleanup changes to get rid of various compiler diagnostic messages.
27 *
28 * Revision 1.12  2003/12/17 00:18:35  cargilld
29 * Update to memory management so that the static memory manager (one used to call Initialize) is only for static data.
30 *
31 * Revision 1.11  2003/10/20 11:45:06  gareth
32 * Made enumerators inherit from XMemory.
33 *
34 * Revision 1.10  2003/05/18 14:02:05  knoaman
35 * Memory manager implementation: pass per instance manager.
36 *
37 * Revision 1.9  2003/05/16 21:36:59  knoaman
38 * Memory manager implementation: Modify constructors to pass in the memory manager.
39 *
40 * Revision 1.8  2003/05/15 19:04:35  knoaman
41 * Partial implementation of the configurable memory manager.
42 *
43 * Revision 1.7  2003/05/15 10:37:08  gareth
44 * Optimization. We now resize the hash when appropriate. Patch by Nathan Codding.
45 *
46 * Revision 1.6  2002/11/04 15:22:04  tng
47 * C++ Namespace Support.
48 *
49 * Revision 1.5  2002/08/21 17:45:00  tng
50 * [Bug 7087] compiler warnings when using gcc.
51 *
52 * Revision 1.4  2002/07/11 18:49:53  knoaman
53 * Add setAdoptElements method.
54 * Rename removeBucketElemSafe to orphanKey.
55 *
56 * Revision 1.3  2002/07/04 15:24:57  tng
57 * DOM L3: add transferElement and removeBucketElemSafe for use in DOMDocument::renameNode.
58 *
59 * Revision 1.2  2002/06/12 17:14:03  tng
60 * Add function cleanup, reinitialize and nextElementKey for ease of use.
61 *
62 * Revision 1.1.1.1  2002/02/01 22:22:12  peiyongz
63 * sane_include
64 *
65 * Revision 1.9  2001/06/04 13:45:04  tng
66 * The "hash" argument clashes with STL hash.  Fixed by Pei Yong Zhang.
67 *
68 * Revision 1.8  2000/07/07 22:16:51  jpolast
69 * remove old put(value) function.  use put(key,value) instead.
70 *
71 * Revision 1.7  2000/06/29 18:27:09  jpolast
72 * bug fix for passing hasher class references to constructor
73 *
74 * Revision 1.6  2000/06/27 22:11:12  jpolast
75 * added more general functionality to hashtables.
76 * able to specify which hasher to use.
77 * default: HashXMLCh [hashes XMLCh* strings]
78 *
79 * future todo: make hasher class references static so only
80 * one instance of a hasher is ever created.
81 *
82 * Revision 1.5  2000/03/02 19:54:44  roddey
83 * This checkin includes many changes done while waiting for the
84 * 1.1.0 code to be finished. I can't list them all here, but a list is
85 * available elsewhere.
86 *
87 * Revision 1.4  2000/02/24 20:05:25  abagchi
88 * Swat for removing Log from API docs
89 *
90 * Revision 1.3  2000/02/06 07:48:03  rahulj
91 * Year 2K copyright swat.
92 *
93 * Revision 1.2  1999/12/18 00:18:10  roddey
94 * More changes to support the new, completely orthagonal support for
95 * intrinsic encodings.
96 *
97 * Revision 1.1.1.1  1999/11/09 01:05:01  twl
98 * Initial checkin
99 *
100 * Revision 1.2  1999/11/08 20:45:12  rahul
101 * Swat for adding in Product name and CVS comment log variable.
102 *
103 */
104
105
106#if !defined(REFHASHTABLEOF_HPP)
107#define REFHASHTABLEOF_HPP
108
109
110#include <xercesc/util/HashBase.hpp>
111#include <xercesc/util/IllegalArgumentException.hpp>
112#include <xercesc/util/NoSuchElementException.hpp>
113#include <xercesc/util/RuntimeException.hpp>
114#include <xercesc/util/XMLString.hpp>
115#include <xercesc/util/HashXMLCh.hpp>
116#include <xercesc/util/PlatformUtils.hpp>
117#include <xercesc/framework/MemoryManager.hpp>
118
119XERCES_CPP_NAMESPACE_BEGIN
120
121//
122//  Forward declare the enumerator so he can be our friend. Can you say
123//  friend? Sure...
124//
125template <class TVal> class RefHashTableOfEnumerator;
126template <class TVal> struct RefHashTableBucketElem;
127
128
129//
130//  This should really be a nested class, but some of the compilers we
131//  have to support cannot deal with that!
132//
133template <class TVal> struct RefHashTableBucketElem : public XMemory
134{
135    RefHashTableBucketElem(void* key, TVal* const value, RefHashTableBucketElem<TVal>* next)
136                : fData(value), fNext(next), fKey(key)
137        {
138        }
139
140    RefHashTableBucketElem(){};
141
142    TVal*                           fData;
143    RefHashTableBucketElem<TVal>*   fNext;
144        void*                                                   fKey;
145
146private:
147    // -----------------------------------------------------------------------
148    //  Unimplemented constructors and operators
149    // -----------------------------------------------------------------------
150    RefHashTableBucketElem(const RefHashTableBucketElem<TVal>&);
151    RefHashTableBucketElem<TVal>& operator=(const RefHashTableBucketElem<TVal>&);
152};
153
154
155template <class TVal> class RefHashTableOf : public XMemory
156{
157public:
158    // -----------------------------------------------------------------------
159    //  Constructors and Destructor
160    // -----------------------------------------------------------------------
161        // backwards compatability - default hasher is HashXMLCh
162    RefHashTableOf
163    (
164        const unsigned int modulus
165        , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
166    );
167        // backwards compatability - default hasher is HashXMLCh
168    RefHashTableOf
169    (
170        const unsigned int modulus
171        , const bool adoptElems
172        , MemoryManager* const manager =  XMLPlatformUtils::fgMemoryManager
173    );
174        // if a hash function is passed in, it will be deleted when the hashtable is deleted.
175        // use a new instance of the hasher class for each hashtable, otherwise one hashtable
176        // may delete the hasher of a different hashtable if both use the same hasher.
177    RefHashTableOf
178    (
179        const unsigned int modulus
180        , const bool adoptElems
181        , HashBase* hashBase
182        , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
183    );
184    ~RefHashTableOf();
185
186
187    // -----------------------------------------------------------------------
188    //  Element management
189    // -----------------------------------------------------------------------
190    bool isEmpty() const;
191    bool containsKey(const void* const key) const;
192    void removeKey(const void* const key);
193    void removeAll();
194    void cleanup();
195    void reinitialize(HashBase* hashBase);
196    void transferElement(const void* const key1, void* key2);
197    TVal* orphanKey(const void* const key);
198
199    // -----------------------------------------------------------------------
200    //  Getters
201    // -----------------------------------------------------------------------
202    TVal* get(const void* const key);
203    const TVal* get(const void* const key) const;
204    MemoryManager* getMemoryManager() const;   
205    unsigned int   getHashModulus()   const;
206
207    // -----------------------------------------------------------------------
208    //  Setters
209    // -----------------------------------------------------------------------
210    void setAdoptElements(const bool aValue);
211
212
213    // -----------------------------------------------------------------------
214    //  Putters
215    // -----------------------------------------------------------------------
216        void put(void* key, TVal* const valueToAdopt);
217
218
219private :
220    // -----------------------------------------------------------------------
221    //  Declare our friends
222    // -----------------------------------------------------------------------
223    friend class RefHashTableOfEnumerator<TVal>;
224
225private:
226    // -----------------------------------------------------------------------
227    //  Unimplemented constructors and operators
228    // -----------------------------------------------------------------------
229    RefHashTableOf(const RefHashTableOf<TVal>&);
230    RefHashTableOf<TVal>& operator=(const RefHashTableOf<TVal>&);
231
232    // -----------------------------------------------------------------------
233    //  Private methods
234    // -----------------------------------------------------------------------
235    RefHashTableBucketElem<TVal>* findBucketElem(const void* const key, unsigned int& hashVal);
236    const RefHashTableBucketElem<TVal>* findBucketElem(const void* const key, unsigned int& hashVal) const;
237    void removeBucketElem(const void* const key, unsigned int& hashVal);
238    void initialize(const unsigned int modulus);
239    void rehash();
240
241
242    // -----------------------------------------------------------------------
243    //  Data members
244    //
245    //  fAdoptedElems
246    //      Indicates whether the values added are adopted or just referenced.
247    //      If adopted, then they are deleted when they are removed from the
248    //      hash table.
249    //
250    //  fBucketList
251    //      This is the array that contains the heads of all of the list
252    //      buckets, one for each possible hash value.
253    //
254    //  fHashModulus
255    //      The modulus used for this hash table, to hash the keys. This is
256    //      also the number of elements in the bucket list.
257        //
258        //  fHash
259        //      The hasher for the key data type.
260    // -----------------------------------------------------------------------
261    MemoryManager*                 fMemoryManager;
262    bool                           fAdoptedElems;
263    RefHashTableBucketElem<TVal>** fBucketList;
264    unsigned int                   fHashModulus;
265    unsigned int                   fInitialModulus;
266    unsigned int                   fCount;
267    HashBase*                      fHash;
268};
269
270
271
272//
273//  An enumerator for a value array. It derives from the basic enumerator
274//  class, so that value vectors can be generically enumerated.
275//
276template <class TVal> class RefHashTableOfEnumerator : public XMLEnumerator<TVal>, public XMemory
277{
278public :
279    // -----------------------------------------------------------------------
280    //  Constructors and Destructor
281    // -----------------------------------------------------------------------
282    RefHashTableOfEnumerator(RefHashTableOf<TVal>* const toEnum
283        , const bool adopt = false
284        , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
285    virtual ~RefHashTableOfEnumerator();
286
287    RefHashTableOfEnumerator(const RefHashTableOfEnumerator<TVal>&);
288    // -----------------------------------------------------------------------
289    //  Enum interface
290    // -----------------------------------------------------------------------
291    bool hasMoreElements() const;
292    TVal& nextElement();
293    void Reset();
294
295    // -----------------------------------------------------------------------
296    //  New interface specific for key used in RefHashable
297    // -----------------------------------------------------------------------
298    void* nextElementKey();
299
300private :
301    // -----------------------------------------------------------------------
302    //  Unimplemented constructors and operators
303    // -----------------------------------------------------------------------
304    RefHashTableOfEnumerator<TVal>& operator=(const RefHashTableOfEnumerator<TVal>&);
305
306    // -----------------------------------------------------------------------
307    //  Private methods
308    // -----------------------------------------------------------------------
309    void findNext();
310
311
312    // -----------------------------------------------------------------------
313    //  Data Members
314    //
315    //  fAdopted
316    //      Indicates whether we have adopted the passed vector. If so then
317    //      we delete the vector when we are destroyed.
318    //
319    //  fCurElem
320    //      This is the current bucket bucket element that we are on.
321    //
322    //  fCurHash
323    //      The is the current hash buck that we are working on. Once we hit
324    //      the end of the bucket that fCurElem is in, then we have to start
325    //      working this one up to the next non-empty bucket.
326    //
327    //  fToEnum
328    //      The value array being enumerated.
329    // -----------------------------------------------------------------------
330    bool                                  fAdopted;
331    RefHashTableBucketElem<TVal>*         fCurElem;
332    unsigned int                          fCurHash;
333    RefHashTableOf<TVal>*                 fToEnum;
334    MemoryManager* const                  fMemoryManager;
335};
336
337XERCES_CPP_NAMESPACE_END
338
339#if !defined(XERCES_TMPLSINC)
340#include <xercesc/util/RefHashTableOf.c>
341#endif
342
343#endif
344
Note: See TracBrowser for help on using the repository browser.