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