source: icXML/icXML-devel/src/icxercesc/util/RefHashTableOf.hpp @ 2721

Last change on this file since 2721 was 2721, checked in by cameron, 6 years ago

Fix imports in icXML modified Xerces files

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