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

Revision 188, 14.3 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) 2001 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) 2001, 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 * $Log: RefHash3KeysIdPool.hpp,v $
59 * Revision 1.9  2004/01/29 11:48:46  cargilld
60 * Code cleanup changes to get rid of various compiler diagnostic messages.
61 *
62 * Revision 1.8  2003/12/17 00:18:35  cargilld
63 * Update to memory management so that the static memory manager (one used to call Initialize) is only for static data.
64 *
65 * Revision 1.7  2003/11/03 22:00:31  peiyongz
66 * RefHashTable-like enumeration accessing added
67 *
68 * Revision 1.6  2003/10/29 16:17:48  peiyongz
69 * size() added
70 *
71 * Revision 1.5  2003/05/16 06:01:52  knoaman
72 * Partial implementation of the configurable memory manager.
73 *
74 * Revision 1.4  2003/05/15 19:04:35  knoaman
75 * Partial implementation of the configurable memory manager.
76 *
77 * Revision 1.3  2002/11/04 15:22:04  tng
78 * C++ Namespace Support.
79 *
80 * Revision 1.2  2002/06/12 17:15:12  tng
81 * Remove redundant include header file.
82 *
83 * Revision 1.1.1.1  2002/02/01 22:22:12  peiyongz
84 * sane_include
85 *
86 * Revision 1.4  2001/12/22 01:06:08  jasons
87 * Made the destructors virtual for:
88 *
89 * * ~RefHash2KeysTableOfEnumerator
90 * * ~RefHash3KeysIdPoolEnumerator
91 *
92 * This fixes bug #5514
93 *
94 * Revision 1.3  2001/06/04 13:45:04  tng
95 * The "hash" argument clashes with STL hash.  Fixed by Pei Yong Zhang.
96 *
97 * Revision 1.2  2001/05/11 13:26:29  tng
98 * Copyright update.
99 *
100 * Revision 1.1  2001/03/21 21:56:12  tng
101 * Schema: Add Schema Grammar, Schema Validator, and split the DTDValidator into DTDValidator, DTDScanner, and DTDGrammar.
102 *
103 */
104
105
106#if !defined(REFHASH3KEYSIDPOOL_HPP)
107#define REFHASH3KEYSIDPOOL_HPP
108
109
110#include <xercesc/util/HashBase.hpp>
111#include <xercesc/util/IllegalArgumentException.hpp>
112#include <xercesc/util/NoSuchElementException.hpp>
113#include <xercesc/util/RuntimeException.hpp>
114#include <xercesc/util/PlatformUtils.hpp>
115#include <xercesc/util/XMLString.hpp>
116#include <xercesc/util/HashXMLCh.hpp>
117
118XERCES_CPP_NAMESPACE_BEGIN
119
120// This hash table is a combination of RefHash2KeyTableOf (with an additional integer as key3)
121// and NameIdPool with an id as index
122
123//
124//  Forward declare the enumerator so he can be our friend. Can you say
125//  friend? Sure...
126//
127template <class TVal> class RefHash3KeysIdPoolEnumerator;
128template <class TVal> struct RefHash3KeysTableBucketElem;
129
130
131//
132//  This should really be a nested class, but some of the compilers we
133//  have to support cannot deal with that!
134//
135template <class TVal> struct RefHash3KeysTableBucketElem : public XMemory
136{
137    RefHash3KeysTableBucketElem(
138              void* key1
139              , int key2
140              , int key3
141              , TVal* const value
142              , RefHash3KeysTableBucketElem<TVal>* next) :
143                fData(value)
144    , fNext(next)
145    , fKey1(key1)
146    , fKey2(key2)
147    , fKey3(key3)
148    {
149    }
150   
151    RefHash3KeysTableBucketElem() {};
152
153    TVal*  fData;
154    RefHash3KeysTableBucketElem<TVal>*   fNext;
155    void*  fKey1;
156    int    fKey2;
157    int    fKey3;
158
159private:
160    // -----------------------------------------------------------------------
161    //  Unimplemented constructors and operators
162    // -----------------------------------------------------------------------
163    RefHash3KeysTableBucketElem(const RefHash3KeysTableBucketElem<TVal>&);
164    RefHash3KeysTableBucketElem<TVal>& operator=(const RefHash3KeysTableBucketElem<TVal>&);
165};
166
167
168template <class TVal> class RefHash3KeysIdPool : public XMemory
169{
170public:
171    // -----------------------------------------------------------------------
172    //  Constructors and Destructor
173    // -----------------------------------------------------------------------
174    // backwards compatability - default hasher is HashXMLCh
175    RefHash3KeysIdPool
176    (
177          const unsigned int   modulus
178        , const unsigned int   initSize = 128
179        , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
180    );
181
182    // backwards compatability - default hasher is HashXMLCh
183    RefHash3KeysIdPool
184    (
185          const unsigned int   modulus
186        , const bool           adoptElems
187        , const unsigned int   initSize = 128
188        , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
189    );
190
191    // if a hash function is passed in, it will be deleted when the hashtable is deleted.
192    // use a new instance of the hasher class for each hashtable, otherwise one hashtable
193    // may delete the hasher of a different hashtable if both use the same hasher.
194    RefHash3KeysIdPool
195    (
196          const unsigned int   modulus
197        , const bool           adoptElems
198        , HashBase* hashBase
199        , const unsigned int initSize = 128
200        , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
201    );
202
203    ~RefHash3KeysIdPool();
204
205    // -----------------------------------------------------------------------
206    //  Element management
207    // -----------------------------------------------------------------------
208    bool isEmpty() const;
209    bool containsKey(const void* const key1, const int key2, const int key3) const;
210    void removeAll();
211
212
213    // -----------------------------------------------------------------------
214    //  Getters
215    // -----------------------------------------------------------------------
216    TVal* getByKey(const void* const key1, const int key2, const int key3);
217    const TVal* getByKey(const void* const key1, const int key2, const int key3) const;
218
219    TVal* getById(const unsigned elemId);
220    const TVal* getById(const unsigned elemId) const;
221
222    MemoryManager* getMemoryManager() const;
223    // -----------------------------------------------------------------------
224    //  Putters
225    // -----------------------------------------------------------------------
226        unsigned int put(void* key1, int key2, int key3, TVal* const valueToAdopt);
227
228
229private :
230    // -----------------------------------------------------------------------
231    //  Declare our friends
232    // -----------------------------------------------------------------------
233    friend class RefHash3KeysIdPoolEnumerator<TVal>;
234
235private:
236    // -----------------------------------------------------------------------
237    //  Unimplemented constructors and operators
238    // -----------------------------------------------------------------------
239    RefHash3KeysIdPool(const RefHash3KeysIdPool<TVal>&);
240    RefHash3KeysIdPool<TVal>& operator=(const RefHash3KeysIdPool<TVal>&);
241
242    // -----------------------------------------------------------------------
243    //  Private methods
244    // -----------------------------------------------------------------------
245    RefHash3KeysTableBucketElem<TVal>* findBucketElem(const void* const key1, const int key2, const int key3, unsigned int& hashVal);
246    const RefHash3KeysTableBucketElem<TVal>* findBucketElem(const void* const key1, const int key2, const int key3, unsigned int& hashVal) const;
247    void initialize(const unsigned int modulus);
248
249
250    // -----------------------------------------------------------------------
251    //  Data members
252    //
253    //  fAdoptedElems
254    //      Indicates whether the values added are adopted or just referenced.
255    //      If adopted, then they are deleted when they are removed from the
256    //      hash table.
257    //
258    //  fBucketList
259    //      This is the array that contains the heads of all of the list
260    //      buckets, one for each possible hash value.
261    //
262    //  fHashModulus
263    //      The modulus used for this hash table, to hash the keys. This is
264    //      also the number of elements in the bucket list.
265    //
266    //  fHash
267    //      The hasher for the key1 data type.
268    //
269    //  fIdPtrs
270    //  fIdPtrsCount
271    //      This is the array of pointers to the bucket elements in order of
272    //      their assigned ids. So taking id N and referencing this array
273    //      gives you the element with that id. The count field indicates
274    //      the current size of this list. When fIdCounter+1 reaches this
275    //      value the list must be expanded.
276    //
277    //  fIdCounter
278    //      This is used to give out unique ids to added elements. It starts
279    //      at zero (which means empty), and is bumped up for each newly added
280    //      element. So the first element is 1, the next is 2, etc... This
281    //      means that this value is set to the top index of the fIdPtrs array.
282    // -----------------------------------------------------------------------
283    MemoryManager*                      fMemoryManager;
284    bool                                fAdoptedElems;
285    RefHash3KeysTableBucketElem<TVal>** fBucketList;
286    unsigned int                        fHashModulus;
287    HashBase*                           fHash;
288    TVal**                              fIdPtrs;
289    unsigned int                        fIdPtrsCount;
290    unsigned int                        fIdCounter;
291};
292
293
294
295//
296//  An enumerator for a value array. It derives from the basic enumerator
297//  class, so that value vectors can be generically enumerated.
298//
299template <class TVal> class RefHash3KeysIdPoolEnumerator : public XMLEnumerator<TVal>, public XMemory
300{
301public :
302    // -----------------------------------------------------------------------
303    //  Constructors and Destructor
304    // -----------------------------------------------------------------------
305    RefHash3KeysIdPoolEnumerator(RefHash3KeysIdPool<TVal>* const toEnum
306        , const bool adopt = false
307        , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
308    virtual ~RefHash3KeysIdPoolEnumerator();
309
310    RefHash3KeysIdPoolEnumerator(const RefHash3KeysIdPoolEnumerator<TVal>&);
311    // -----------------------------------------------------------------------
312    //  Enum interface
313    // -----------------------------------------------------------------------
314    bool hasMoreElements() const;
315    TVal& nextElement();
316    void Reset();
317    int  size() const;
318
319    // -----------------------------------------------------------------------
320    //  New interface
321    // -----------------------------------------------------------------------
322    void resetKey();
323    void nextElementKey(void*&, int&, int&);
324    bool hasMoreKeys()   const;
325
326private :
327    // -----------------------------------------------------------------------
328    //  Unimplemented constructors and operators
329    // -----------------------------------------------------------------------   
330    RefHash3KeysIdPoolEnumerator<TVal>& operator=(const RefHash3KeysIdPoolEnumerator<TVal>&);
331
332    // -----------------------------------------------------------------------
333    //  Private methods
334    // -----------------------------------------------------------------------
335    void findNext();
336
337    // -----------------------------------------------------------------------
338    //  Data Members
339    //  fAdoptedElems
340    //      Indicates whether the values added are adopted or just referenced.
341    //      If adopted, then they are deleted when they are removed from the
342    //      hash table
343    //
344    //  fCurIndex
345    //      This is the current index into the pool's id mapping array. This
346    //      is now we enumerate it.
347    //
348    //  fToEnum
349    //      The name id pool that is being enumerated.
350    // -----------------------------------------------------------------------
351    bool                                fAdoptedElems;
352    unsigned int                        fCurIndex;
353    RefHash3KeysIdPool<TVal>*           fToEnum;
354    RefHash3KeysTableBucketElem<TVal>*  fCurElem;
355    unsigned int                        fCurHash;
356    MemoryManager* const                fMemoryManager;
357};
358
359XERCES_CPP_NAMESPACE_END
360
361#if !defined(XERCES_TMPLSINC)
362#include <xercesc/util/RefHash3KeysIdPool.c>
363#endif
364
365#endif
Note: See TracBrowser for help on using the repository browser.