source: NonGTP/Xerces/xerces-c_2_8_0/include/xercesc/util/RefHash2KeysTableOf.hpp @ 2674

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