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