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