source: NonGTP/Xerces/xerces-c_2_8_0/include/xercesc/util/RefHashTableOf.hpp @ 2674

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