|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 /* |
|
18 * Licensed to the Apache Software Foundation (ASF) under one or more |
|
19 * contributor license agreements. See the NOTICE file distributed with |
|
20 * this work for additional information regarding copyright ownership. |
|
21 * The ASF licenses this file to You under the Apache License, Version 2.0 |
|
22 * (the "License"); you may not use this file except in compliance with |
|
23 * the License. You may obtain a copy of the License at |
|
24 * |
|
25 * http://www.apache.org/licenses/LICENSE-2.0 |
|
26 * |
|
27 * Unless required by applicable law or agreed to in writing, software |
|
28 * distributed under the License is distributed on an "AS IS" BASIS, |
|
29 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
30 * See the License for the specific language governing permissions and |
|
31 * limitations under the License. |
|
32 */ |
|
33 |
|
34 /* |
|
35 * $Id: ValueHashTableOf.hpp 568078 2007-08-21 11:43:25Z amassari $ |
|
36 */ |
|
37 |
|
38 |
|
39 #if !defined(VALUEHASHTABLEOF_HPP) |
|
40 #define VALUEHASHTABLEOF_HPP |
|
41 |
|
42 |
|
43 #include <xercesc/util/HashBase.hpp> |
|
44 #include <xercesc/util/IllegalArgumentException.hpp> |
|
45 #include <xercesc/util/NoSuchElementException.hpp> |
|
46 #include <xercesc/util/RuntimeException.hpp> |
|
47 #include <xercesc/util/PlatformUtils.hpp> |
|
48 #include <xercesc/util/XMLString.hpp> |
|
49 #include <xercesc/util/HashXMLCh.hpp> |
|
50 |
|
51 XERCES_CPP_NAMESPACE_BEGIN |
|
52 |
|
53 // |
|
54 // Forward declare the enumerator so he can be our friend. Can you say |
|
55 // friend? Sure... |
|
56 // |
|
57 template <class TVal> class ValueHashTableOfEnumerator; |
|
58 template <class TVal> struct ValueHashTableBucketElem; |
|
59 |
|
60 |
|
61 // |
|
62 // This should really be a nested class, but some of the compilers we |
|
63 // have to support cannot deal with that! |
|
64 // |
|
65 template <class TVal> struct ValueHashTableBucketElem |
|
66 { |
|
67 ValueHashTableBucketElem(void* key, const TVal& value, ValueHashTableBucketElem<TVal>* next) |
|
68 : fData(value), fNext(next), fKey(key) |
|
69 { |
|
70 } |
|
71 ValueHashTableBucketElem(){}; |
|
72 ~ValueHashTableBucketElem(){}; |
|
73 |
|
74 TVal fData; |
|
75 ValueHashTableBucketElem<TVal>* fNext; |
|
76 void* fKey; |
|
77 |
|
78 private: |
|
79 // ----------------------------------------------------------------------- |
|
80 // Unimplemented constructors and operators |
|
81 // ----------------------------------------------------------------------- |
|
82 ValueHashTableBucketElem(const ValueHashTableBucketElem<TVal>&); |
|
83 ValueHashTableBucketElem<TVal>& operator=(const ValueHashTableBucketElem<TVal>&); |
|
84 }; |
|
85 |
|
86 |
|
87 template <class TVal> class ValueHashTableOf : public XMemory |
|
88 { |
|
89 public: |
|
90 // ----------------------------------------------------------------------- |
|
91 // Constructors and Destructor |
|
92 // ----------------------------------------------------------------------- |
|
93 // backwards compatability - default hasher is HashXMLCh |
|
94 ValueHashTableOf |
|
95 ( |
|
96 const unsigned int modulus |
|
97 , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager |
|
98 ); |
|
99 // if a hash function is passed in, it will be deleted when the hashtable is deleted. |
|
100 // use a new instance of the hasher class for each hashtable, otherwise one hashtable |
|
101 // may delete the hasher of a different hashtable if both use the same hasher. |
|
102 ValueHashTableOf |
|
103 ( |
|
104 const unsigned int modulus |
|
105 , HashBase* hashBase |
|
106 , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager |
|
107 ); |
|
108 ~ValueHashTableOf(); |
|
109 |
|
110 |
|
111 // ----------------------------------------------------------------------- |
|
112 // Element management |
|
113 // ----------------------------------------------------------------------- |
|
114 bool isEmpty() const; |
|
115 bool containsKey(const void* const key) const; |
|
116 void removeKey(const void* const key); |
|
117 void removeAll(); |
|
118 |
|
119 |
|
120 // ----------------------------------------------------------------------- |
|
121 // Getters |
|
122 // ----------------------------------------------------------------------- |
|
123 TVal& get(const void* const key, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager); |
|
124 const TVal& get(const void* const key) const; |
|
125 |
|
126 |
|
127 // ----------------------------------------------------------------------- |
|
128 // Putters |
|
129 // ----------------------------------------------------------------------- |
|
130 void put(void* key, const TVal& valueToAdopt); |
|
131 |
|
132 |
|
133 private : |
|
134 // ----------------------------------------------------------------------- |
|
135 // Declare our friends |
|
136 // ----------------------------------------------------------------------- |
|
137 friend class ValueHashTableOfEnumerator<TVal>; |
|
138 |
|
139 private: |
|
140 // ----------------------------------------------------------------------- |
|
141 // Unimplemented constructors and operators |
|
142 // ----------------------------------------------------------------------- |
|
143 ValueHashTableOf(const ValueHashTableOf<TVal>&); |
|
144 ValueHashTableOf<TVal>& operator=(const ValueHashTableOf<TVal>&); |
|
145 |
|
146 // ----------------------------------------------------------------------- |
|
147 // Private methods |
|
148 // ----------------------------------------------------------------------- |
|
149 ValueHashTableBucketElem<TVal>* findBucketElem(const void* const key, unsigned int& hashVal); |
|
150 const ValueHashTableBucketElem<TVal>* findBucketElem(const void* const key, unsigned int& hashVal) const; |
|
151 void removeBucketElem(const void* const key, unsigned int& hashVal); |
|
152 void initialize(const unsigned int modulus); |
|
153 |
|
154 |
|
155 // ----------------------------------------------------------------------- |
|
156 // Data members |
|
157 // |
|
158 // fBucketList |
|
159 // This is the array that contains the heads of all of the list |
|
160 // buckets, one for each possible hash value. |
|
161 // |
|
162 // fHashModulus |
|
163 // The modulus used for this hash table, to hash the keys. This is |
|
164 // also the number of elements in the bucket list. |
|
165 // |
|
166 // fHash |
|
167 // The hasher for the key data type. |
|
168 // ----------------------------------------------------------------------- |
|
169 MemoryManager* fMemoryManager; |
|
170 ValueHashTableBucketElem<TVal>** fBucketList; |
|
171 unsigned int fHashModulus; |
|
172 HashBase* fHash; |
|
173 }; |
|
174 |
|
175 |
|
176 |
|
177 // |
|
178 // An enumerator for a value array. It derives from the basic enumerator |
|
179 // class, so that value vectors can be generically enumerated. |
|
180 // |
|
181 template <class TVal> class ValueHashTableOfEnumerator : public XMLEnumerator<TVal>, public XMemory |
|
182 { |
|
183 public : |
|
184 // ----------------------------------------------------------------------- |
|
185 // Constructors and Destructor |
|
186 // ----------------------------------------------------------------------- |
|
187 ValueHashTableOfEnumerator(ValueHashTableOf<TVal>* const toEnum |
|
188 , const bool adopt = false |
|
189 , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager); |
|
190 virtual ~ValueHashTableOfEnumerator(); |
|
191 |
|
192 |
|
193 // ----------------------------------------------------------------------- |
|
194 // Enum interface |
|
195 // ----------------------------------------------------------------------- |
|
196 bool hasMoreElements() const; |
|
197 TVal& nextElement(); |
|
198 void Reset(); |
|
199 |
|
200 // ----------------------------------------------------------------------- |
|
201 // New interface specific for key used in ValueHashable |
|
202 // ----------------------------------------------------------------------- |
|
203 void* nextElementKey(); |
|
204 |
|
205 |
|
206 private : |
|
207 // ----------------------------------------------------------------------- |
|
208 // Unimplemented constructors and operators |
|
209 // ----------------------------------------------------------------------- |
|
210 ValueHashTableOfEnumerator(const ValueHashTableOfEnumerator<TVal>&); |
|
211 ValueHashTableOfEnumerator<TVal>& operator=(const ValueHashTableOfEnumerator<TVal>&); |
|
212 |
|
213 // ----------------------------------------------------------------------- |
|
214 // Private methods |
|
215 // ----------------------------------------------------------------------- |
|
216 void findNext(); |
|
217 |
|
218 |
|
219 // ----------------------------------------------------------------------- |
|
220 // Data Members |
|
221 // |
|
222 // fAdopted |
|
223 // Indicates whether we have adopted the passed vector. If so then |
|
224 // we delete the vector when we are destroyed. |
|
225 // |
|
226 // fCurElem |
|
227 // This is the current bucket bucket element that we are on. |
|
228 // |
|
229 // fCurHash |
|
230 // The is the current hash buck that we are working on. Once we hit |
|
231 // the end of the bucket that fCurElem is in, then we have to start |
|
232 // working this one up to the next non-empty bucket. |
|
233 // |
|
234 // fToEnum |
|
235 // The value array being enumerated. |
|
236 // ----------------------------------------------------------------------- |
|
237 bool fAdopted; |
|
238 ValueHashTableBucketElem<TVal>* fCurElem; |
|
239 unsigned int fCurHash; |
|
240 ValueHashTableOf<TVal>* fToEnum; |
|
241 MemoryManager* const fMemoryManager; |
|
242 }; |
|
243 |
|
244 XERCES_CPP_NAMESPACE_END |
|
245 |
|
246 #if !defined(XERCES_TMPLSINC) |
|
247 #include <xercesc/util/ValueHashTableOf.c> |
|
248 #endif |
|
249 |
|
250 #endif |