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

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

xerces added

Line 
1/*
2 * Copyright 2002,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 * $Id: ValueHashTableOf.hpp,v 1.12 2004/09/08 13:56:23 peiyongz Exp $
19 */
20
21
22#if !defined(VALUEHASHTABLEOF_HPP)
23#define VALUEHASHTABLEOF_HPP
24
25
26#include <xercesc/util/HashBase.hpp>
27#include <xercesc/util/IllegalArgumentException.hpp>
28#include <xercesc/util/NoSuchElementException.hpp>
29#include <xercesc/util/RuntimeException.hpp>
30#include <xercesc/util/PlatformUtils.hpp>
31#include <xercesc/util/XMLString.hpp>
32#include <xercesc/util/HashXMLCh.hpp>
33
34XERCES_CPP_NAMESPACE_BEGIN
35
36//
37//  Forward declare the enumerator so he can be our friend. Can you say
38//  friend? Sure...
39//
40template <class TVal> class ValueHashTableOfEnumerator;
41template <class TVal> struct ValueHashTableBucketElem;
42
43
44//
45//  This should really be a nested class, but some of the compilers we
46//  have to support cannot deal with that!
47//
48template <class TVal> struct ValueHashTableBucketElem : public XMemory
49{
50    ValueHashTableBucketElem(void* key, const TVal& value, ValueHashTableBucketElem<TVal>* next)
51                : fData(value), fNext(next), fKey(key)
52        {
53        }
54    ValueHashTableBucketElem(){};
55
56    TVal                           fData;
57    ValueHashTableBucketElem<TVal>* fNext;
58        void*                          fKey;
59
60private:
61    // -----------------------------------------------------------------------
62    //  Unimplemented constructors and operators
63    // -----------------------------------------------------------------------   
64    ValueHashTableBucketElem(const ValueHashTableBucketElem<TVal>&);
65    ValueHashTableBucketElem<TVal>& operator=(const ValueHashTableBucketElem<TVal>&);
66};
67
68
69template <class TVal> class ValueHashTableOf : public XMemory
70{
71public:
72    // -----------------------------------------------------------------------
73    //  Constructors and Destructor
74    // -----------------------------------------------------------------------
75        // backwards compatability - default hasher is HashXMLCh
76    ValueHashTableOf
77    (
78          const unsigned int   modulus
79        , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
80    );
81        // if a hash function is passed in, it will be deleted when the hashtable is deleted.
82        // use a new instance of the hasher class for each hashtable, otherwise one hashtable
83        // may delete the hasher of a different hashtable if both use the same hasher.
84    ValueHashTableOf
85    (
86          const unsigned int   modulus
87        , HashBase*            hashBase
88        , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
89    );
90    ~ValueHashTableOf();
91
92
93    // -----------------------------------------------------------------------
94    //  Element management
95    // -----------------------------------------------------------------------
96    bool isEmpty() const;
97    bool containsKey(const void* const key) const;
98    void removeKey(const void* const key);
99    void removeAll();
100
101
102    // -----------------------------------------------------------------------
103    //  Getters
104    // -----------------------------------------------------------------------
105    TVal& get(const void* const key, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
106    const TVal& get(const void* const key) const;
107
108
109    // -----------------------------------------------------------------------
110    //  Putters
111    // -----------------------------------------------------------------------
112        void put(void* key, const TVal& valueToAdopt);
113
114
115private :
116    // -----------------------------------------------------------------------
117    //  Declare our friends
118    // -----------------------------------------------------------------------
119    friend class ValueHashTableOfEnumerator<TVal>;
120
121private:
122    // -----------------------------------------------------------------------
123    //  Unimplemented constructors and operators
124    // -----------------------------------------------------------------------   
125    ValueHashTableOf(const ValueHashTableOf<TVal>&);
126    ValueHashTableOf<TVal>& operator=(const ValueHashTableOf<TVal>&);
127
128    // -----------------------------------------------------------------------
129    //  Private methods
130    // -----------------------------------------------------------------------
131    ValueHashTableBucketElem<TVal>* findBucketElem(const void* const key, unsigned int& hashVal);
132    const ValueHashTableBucketElem<TVal>* findBucketElem(const void* const key, unsigned int& hashVal) const;
133    void removeBucketElem(const void* const key, unsigned int& hashVal);
134        void initialize(const unsigned int modulus);
135
136
137    // -----------------------------------------------------------------------
138    //  Data members
139    //
140    //  fBucketList
141    //      This is the array that contains the heads of all of the list
142    //      buckets, one for each possible hash value.
143    //
144    //  fHashModulus
145    //      The modulus used for this hash table, to hash the keys. This is
146    //      also the number of elements in the bucket list.
147        //
148        //  fHash
149        //      The hasher for the key data type.
150    // -----------------------------------------------------------------------
151    MemoryManager*                   fMemoryManager;
152    ValueHashTableBucketElem<TVal>** fBucketList;
153    unsigned int                     fHashModulus;
154        HashBase*                        fHash;
155};
156
157
158
159//
160//  An enumerator for a value array. It derives from the basic enumerator
161//  class, so that value vectors can be generically enumerated.
162//
163template <class TVal> class ValueHashTableOfEnumerator : public XMLEnumerator<TVal>, public XMemory
164{
165public :
166    // -----------------------------------------------------------------------
167    //  Constructors and Destructor
168    // -----------------------------------------------------------------------
169    ValueHashTableOfEnumerator(ValueHashTableOf<TVal>* const toEnum
170        , const bool adopt = false
171        , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
172    virtual ~ValueHashTableOfEnumerator();
173
174
175    // -----------------------------------------------------------------------
176    //  Enum interface
177    // -----------------------------------------------------------------------
178    bool hasMoreElements() const;
179    TVal& nextElement();
180    void Reset();
181
182    // -----------------------------------------------------------------------
183    //  New interface specific for key used in ValueHashable
184    // -----------------------------------------------------------------------
185    void* nextElementKey();
186
187
188private :
189    // -----------------------------------------------------------------------
190    //  Unimplemented constructors and operators
191    // -----------------------------------------------------------------------   
192    ValueHashTableOfEnumerator(const ValueHashTableOfEnumerator<TVal>&);
193    ValueHashTableOfEnumerator<TVal>& operator=(const ValueHashTableOfEnumerator<TVal>&);
194
195    // -----------------------------------------------------------------------
196    //  Private methods
197    // -----------------------------------------------------------------------
198    void findNext();
199
200
201    // -----------------------------------------------------------------------
202    //  Data Members
203    //
204    //  fAdopted
205    //      Indicates whether we have adopted the passed vector. If so then
206    //      we delete the vector when we are destroyed.
207    //
208    //  fCurElem
209    //      This is the current bucket bucket element that we are on.
210    //
211    //  fCurHash
212    //      The is the current hash buck that we are working on. Once we hit
213    //      the end of the bucket that fCurElem is in, then we have to start
214    //      working this one up to the next non-empty bucket.
215    //
216    //  fToEnum
217    //      The value array being enumerated.
218    // -----------------------------------------------------------------------
219    bool                            fAdopted;
220    ValueHashTableBucketElem<TVal>* fCurElem;
221    unsigned int                    fCurHash;
222    ValueHashTableOf<TVal>*         fToEnum;
223    MemoryManager* const            fMemoryManager;
224};
225
226XERCES_CPP_NAMESPACE_END
227
228#if !defined(XERCES_TMPLSINC)
229#include <xercesc/util/ValueHashTableOf.c>
230#endif
231
232#endif
Note: See TracBrowser for help on using the repository browser.