source: NonGTP/Xerces/xercesc/util/ValueHashTableOf.hpp @ 188

Revision 188, 10.8 KB checked in by mattausch, 19 years ago (diff)

added xercesc to support

Line 
1/*
2 * The Apache Software License, Version 1.1
3 *
4 * Copyright (c) 2002 The Apache Software Foundation.  All rights
5 * reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in
16 *    the documentation and/or other materials provided with the
17 *    distribution.
18 *
19 * 3. The end-user documentation included with the redistribution,
20 *    if any, must include the following acknowledgment:
21 *       "This product includes software developed by the
22 *        Apache Software Foundation (http://www.apache.org/)."
23 *    Alternately, this acknowledgment may appear in the software itself,
24 *    if and wherever such third-party acknowledgments normally appear.
25 *
26 * 4. The names "Xerces" and "Apache Software Foundation" must
27 *    not be used to endorse or promote products derived from this
28 *    software without prior written permission. For written
29 *    permission, please contact apache\@apache.org.
30 *
31 * 5. Products derived from this software may not be called "Apache",
32 *    nor may "Apache" appear in their name, without prior written
33 *    permission of the Apache Software Foundation.
34 *
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 * SUCH DAMAGE.
47 * ====================================================================
48 *
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Software Foundation, and was
51 * originally based on software copyright (c) 1999, International
52 * Business Machines, Inc., http://www.ibm.com .  For more information
53 * on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57/*
58 * $Id: ValueHashTableOf.hpp,v 1.11 2004/01/29 11:48:46 cargilld Exp $
59 */
60
61
62#if !defined(VALUEHASHTABLEOF_HPP)
63#define VALUEHASHTABLEOF_HPP
64
65
66#include <xercesc/util/HashBase.hpp>
67#include <xercesc/util/IllegalArgumentException.hpp>
68#include <xercesc/util/NoSuchElementException.hpp>
69#include <xercesc/util/RuntimeException.hpp>
70#include <xercesc/util/PlatformUtils.hpp>
71#include <xercesc/util/XMLString.hpp>
72#include <xercesc/util/HashXMLCh.hpp>
73
74XERCES_CPP_NAMESPACE_BEGIN
75
76//
77//  Forward declare the enumerator so he can be our friend. Can you say
78//  friend? Sure...
79//
80template <class TVal> class ValueHashTableOfEnumerator;
81template <class TVal> struct ValueHashTableBucketElem;
82
83
84//
85//  This should really be a nested class, but some of the compilers we
86//  have to support cannot deal with that!
87//
88template <class TVal> struct ValueHashTableBucketElem : public XMemory
89{
90    ValueHashTableBucketElem(void* key, const TVal& value, ValueHashTableBucketElem<TVal>* next)
91                : fData(value), fNext(next), fKey(key)
92        {
93        }
94    ValueHashTableBucketElem(){};
95
96    TVal                           fData;
97    ValueHashTableBucketElem<TVal>* fNext;
98        void*                          fKey;
99
100private:
101    // -----------------------------------------------------------------------
102    //  Unimplemented constructors and operators
103    // -----------------------------------------------------------------------   
104    ValueHashTableBucketElem(const ValueHashTableBucketElem<TVal>&);
105    ValueHashTableBucketElem<TVal>& operator=(const ValueHashTableBucketElem<TVal>&);
106};
107
108
109template <class TVal> class ValueHashTableOf : public XMemory
110{
111public:
112    // -----------------------------------------------------------------------
113    //  Constructors and Destructor
114    // -----------------------------------------------------------------------
115        // backwards compatability - default hasher is HashXMLCh
116    ValueHashTableOf
117    (
118          const unsigned int   modulus
119        , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
120    );
121        // if a hash function is passed in, it will be deleted when the hashtable is deleted.
122        // use a new instance of the hasher class for each hashtable, otherwise one hashtable
123        // may delete the hasher of a different hashtable if both use the same hasher.
124    ValueHashTableOf
125    (
126          const unsigned int   modulus
127        , HashBase*            hashBase
128        , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
129    );
130    ~ValueHashTableOf();
131
132
133    // -----------------------------------------------------------------------
134    //  Element management
135    // -----------------------------------------------------------------------
136    bool isEmpty() const;
137    bool containsKey(const void* const key) const;
138    void removeKey(const void* const key);
139    void removeAll();
140
141
142    // -----------------------------------------------------------------------
143    //  Getters
144    // -----------------------------------------------------------------------
145    TVal& get(const void* const key, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
146    const TVal& get(const void* const key) const;
147
148
149    // -----------------------------------------------------------------------
150    //  Putters
151    // -----------------------------------------------------------------------
152        void put(void* key, const TVal& valueToAdopt);
153
154
155private :
156    // -----------------------------------------------------------------------
157    //  Declare our friends
158    // -----------------------------------------------------------------------
159    friend class ValueHashTableOfEnumerator<TVal>;
160
161private:
162    // -----------------------------------------------------------------------
163    //  Unimplemented constructors and operators
164    // -----------------------------------------------------------------------   
165    ValueHashTableOf(const ValueHashTableOf<TVal>&);
166    ValueHashTableOf<TVal>& operator=(const ValueHashTableOf<TVal>&);
167
168    // -----------------------------------------------------------------------
169    //  Private methods
170    // -----------------------------------------------------------------------
171    ValueHashTableBucketElem<TVal>* findBucketElem(const void* const key, unsigned int& hashVal);
172    const ValueHashTableBucketElem<TVal>* findBucketElem(const void* const key, unsigned int& hashVal) const;
173    void removeBucketElem(const void* const key, unsigned int& hashVal);
174        void initialize(const unsigned int modulus);
175
176
177    // -----------------------------------------------------------------------
178    //  Data members
179    //
180    //  fBucketList
181    //      This is the array that contains the heads of all of the list
182    //      buckets, one for each possible hash value.
183    //
184    //  fHashModulus
185    //      The modulus used for this hash table, to hash the keys. This is
186    //      also the number of elements in the bucket list.
187        //
188        //  fHash
189        //      The hasher for the key data type.
190    // -----------------------------------------------------------------------
191    MemoryManager*                   fMemoryManager;
192    ValueHashTableBucketElem<TVal>** fBucketList;
193    unsigned int                     fHashModulus;
194        HashBase*                        fHash;
195};
196
197
198
199//
200//  An enumerator for a value array. It derives from the basic enumerator
201//  class, so that value vectors can be generically enumerated.
202//
203template <class TVal> class ValueHashTableOfEnumerator : public XMLEnumerator<TVal>, public XMemory
204{
205public :
206    // -----------------------------------------------------------------------
207    //  Constructors and Destructor
208    // -----------------------------------------------------------------------
209    ValueHashTableOfEnumerator(ValueHashTableOf<TVal>* const toEnum
210        , const bool adopt = false
211        , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
212    virtual ~ValueHashTableOfEnumerator();
213
214
215    // -----------------------------------------------------------------------
216    //  Enum interface
217    // -----------------------------------------------------------------------
218    bool hasMoreElements() const;
219    TVal& nextElement();
220    void Reset();
221
222    // -----------------------------------------------------------------------
223    //  New interface specific for key used in ValueHashable
224    // -----------------------------------------------------------------------
225    void* nextElementKey();
226
227
228private :
229    // -----------------------------------------------------------------------
230    //  Unimplemented constructors and operators
231    // -----------------------------------------------------------------------   
232    ValueHashTableOfEnumerator(const ValueHashTableOfEnumerator<TVal>&);
233    ValueHashTableOfEnumerator<TVal>& operator=(const ValueHashTableOfEnumerator<TVal>&);
234
235    // -----------------------------------------------------------------------
236    //  Private methods
237    // -----------------------------------------------------------------------
238    void findNext();
239
240
241    // -----------------------------------------------------------------------
242    //  Data Members
243    //
244    //  fAdopted
245    //      Indicates whether we have adopted the passed vector. If so then
246    //      we delete the vector when we are destroyed.
247    //
248    //  fCurElem
249    //      This is the current bucket bucket element that we are on.
250    //
251    //  fCurHash
252    //      The is the current hash buck that we are working on. Once we hit
253    //      the end of the bucket that fCurElem is in, then we have to start
254    //      working this one up to the next non-empty bucket.
255    //
256    //  fToEnum
257    //      The value array being enumerated.
258    // -----------------------------------------------------------------------
259    bool                            fAdopted;
260    ValueHashTableBucketElem<TVal>* fCurElem;
261    unsigned int                    fCurHash;
262    ValueHashTableOf<TVal>*         fToEnum;
263    MemoryManager* const            fMemoryManager;
264};
265
266XERCES_CPP_NAMESPACE_END
267
268#if !defined(XERCES_TMPLSINC)
269#include <xercesc/util/ValueHashTableOf.c>
270#endif
271
272#endif
Note: See TracBrowser for help on using the repository browser.