Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:15:13

0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one or more
0003  * contributor license agreements.  See the NOTICE file distributed with
0004  * this work for additional information regarding copyright ownership.
0005  * The ASF licenses this file to You under the Apache License, Version 2.0
0006  * (the "License"); you may not use this file except in compliance with
0007  * the License.  You may obtain a copy of the License at
0008  *
0009  *      http://www.apache.org/licenses/LICENSE-2.0
0010  *
0011  * Unless required by applicable law or agreed to in writing, software
0012  * distributed under the License is distributed on an "AS IS" BASIS,
0013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014  * See the License for the specific language governing permissions and
0015  * limitations under the License.
0016  */
0017 
0018 /*
0019  * $Id$
0020  */
0021 
0022 #if !defined(XERCESC_INCLUDE_GUARD_REFHASH2KEYSTABLEOF_HPP)
0023 #define XERCESC_INCLUDE_GUARD_REFHASH2KEYSTABLEOF_HPP
0024 
0025 
0026 #include <xercesc/util/Hashers.hpp>
0027 #include <xercesc/util/IllegalArgumentException.hpp>
0028 #include <xercesc/util/NoSuchElementException.hpp>
0029 #include <xercesc/util/RuntimeException.hpp>
0030 #include <xercesc/util/PlatformUtils.hpp>
0031 
0032 XERCES_CPP_NAMESPACE_BEGIN
0033 
0034 // This hash table is similar to RefHashTableOf with an additional integer as key2
0035 
0036 //  Forward declare the enumerator so it can be our friend.
0037 //
0038 template <class TVal, class THasher = StringHasher>
0039 class RefHash2KeysTableOfEnumerator;
0040 
0041 //
0042 //  This should really be a nested class, but some of the compilers we
0043 //  have to support cannot deal with that!
0044 //
0045 template <class TVal>
0046 struct RefHash2KeysTableBucketElem
0047 {
0048     RefHash2KeysTableBucketElem(void* key1, int key2, TVal* const value, RefHash2KeysTableBucketElem<TVal>* next)
0049         : fData(value), fNext(next), fKey1(key1), fKey2(key2)
0050         {
0051         }
0052     ~RefHash2KeysTableBucketElem() {};
0053 
0054     TVal*                                fData;
0055     RefHash2KeysTableBucketElem<TVal>*   fNext;
0056     void*                                fKey1;
0057     int                                  fKey2;
0058 
0059 private:
0060     // -----------------------------------------------------------------------
0061     //  Unimplemented constructors and operators
0062     // -----------------------------------------------------------------------
0063     RefHash2KeysTableBucketElem(const RefHash2KeysTableBucketElem<TVal>&);
0064     RefHash2KeysTableBucketElem<TVal>& operator=(const RefHash2KeysTableBucketElem<TVal>&);
0065 };
0066 
0067 
0068 template <class TVal, class THasher = StringHasher>
0069 class RefHash2KeysTableOf : public XMemory
0070 {
0071 public:
0072     // -----------------------------------------------------------------------
0073     //  Constructors and Destructor
0074     // -----------------------------------------------------------------------
0075 
0076     RefHash2KeysTableOf(
0077       const XMLSize_t modulus,
0078       MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
0079 
0080     RefHash2KeysTableOf(
0081       const XMLSize_t modulus,
0082       const THasher& hasher,
0083       MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
0084 
0085     RefHash2KeysTableOf(
0086       const XMLSize_t modulus,
0087       const bool adoptElems,
0088       MemoryManager* const manager =  XMLPlatformUtils::fgMemoryManager);
0089 
0090     RefHash2KeysTableOf(
0091       const XMLSize_t modulus,
0092       const bool adoptElems,
0093       const THasher& hasher,
0094       MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
0095 
0096     ~RefHash2KeysTableOf();
0097 
0098 
0099     // -----------------------------------------------------------------------
0100     //  Element management
0101     // -----------------------------------------------------------------------
0102     bool isEmpty() const;
0103     bool containsKey(const void* const key1, const int key2) const;
0104     void removeKey(const void* const key1, const int key2);
0105     void removeKey(const void* const key1);
0106     void removeAll();
0107     void transferElement(const void* const key1, void* key2);
0108 
0109     // -----------------------------------------------------------------------
0110     //  Getters
0111     // -----------------------------------------------------------------------
0112     TVal* get(const void* const key1, const int key2);
0113     const TVal* get(const void* const key1, const int key2) const;
0114 
0115     MemoryManager* getMemoryManager() const;
0116     XMLSize_t      getHashModulus()   const;
0117 
0118     // -----------------------------------------------------------------------
0119     //  Putters
0120     // -----------------------------------------------------------------------
0121     void put(void* key1, int key2, TVal* const valueToAdopt);
0122 
0123 private :
0124     // -----------------------------------------------------------------------
0125     //  Declare our friends
0126     // -----------------------------------------------------------------------
0127     friend class RefHash2KeysTableOfEnumerator<TVal, THasher>;
0128 
0129 
0130 private:
0131     // -----------------------------------------------------------------------
0132     //  Unimplemented constructors and operators
0133     // -----------------------------------------------------------------------
0134     RefHash2KeysTableOf(const RefHash2KeysTableOf<TVal, THasher>&);
0135     RefHash2KeysTableOf<TVal>& operator=(const RefHash2KeysTableOf<TVal, THasher>&);
0136 
0137     // -----------------------------------------------------------------------
0138     //  Private methods
0139     // -----------------------------------------------------------------------
0140     RefHash2KeysTableBucketElem<TVal>* findBucketElem(const void* const key1, const int key2, XMLSize_t& hashVal);
0141     const RefHash2KeysTableBucketElem<TVal>* findBucketElem(const void* const key1, const int key2, XMLSize_t& hashVal) const;
0142     void initialize(const XMLSize_t modulus);
0143     void rehash();
0144 
0145 
0146     // -----------------------------------------------------------------------
0147     //  Data members
0148     //
0149     //  fAdoptedElems
0150     //      Indicates whether the values added are adopted or just referenced.
0151     //      If adopted, then they are deleted when they are removed from the
0152     //      hash table.
0153     //
0154     //  fBucketList
0155     //      This is the array that contains the heads of all of the list
0156     //      buckets, one for each possible hash value.
0157     //
0158     //  fHashModulus
0159     //      The modulus used for this hash table, to hash the keys. This is
0160     //      also the number of elements in the bucket list.
0161     //
0162     //  fCount
0163     //      The number of elements currently in the map
0164     //
0165     //  fHash
0166     //      The hasher for the key1 data type.
0167     // -----------------------------------------------------------------------
0168     MemoryManager*                      fMemoryManager;
0169     bool                                fAdoptedElems;
0170     RefHash2KeysTableBucketElem<TVal>** fBucketList;
0171     XMLSize_t                           fHashModulus;
0172     XMLSize_t                           fCount;
0173     THasher                             fHasher;
0174 };
0175 
0176 
0177 
0178 //
0179 //  An enumerator for a value array. It derives from the basic enumerator
0180 //  class, so that value vectors can be generically enumerated.
0181 //
0182 template <class TVal, class THasher>
0183 class RefHash2KeysTableOfEnumerator : public XMLEnumerator<TVal>, public XMemory
0184 {
0185 public :
0186     // -----------------------------------------------------------------------
0187     //  Constructors and Destructor
0188     // -----------------------------------------------------------------------
0189     RefHash2KeysTableOfEnumerator(RefHash2KeysTableOf<TVal, THasher>* const toEnum
0190                                   , const bool adopt = false
0191                                   , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
0192     virtual ~RefHash2KeysTableOfEnumerator();
0193 
0194 
0195     // -----------------------------------------------------------------------
0196     //  Enum interface
0197     // -----------------------------------------------------------------------
0198     bool hasMoreElements() const;
0199     TVal& nextElement();
0200     void Reset();
0201 
0202     // -----------------------------------------------------------------------
0203     //  New interface
0204     // -----------------------------------------------------------------------
0205     void nextElementKey(void*&, int&);
0206     void setPrimaryKey(const void* key);
0207 
0208 private :
0209     // -----------------------------------------------------------------------
0210     //  Unimplemented constructors and operators
0211     // -----------------------------------------------------------------------
0212     RefHash2KeysTableOfEnumerator(const RefHash2KeysTableOfEnumerator<TVal, THasher>&);
0213     RefHash2KeysTableOfEnumerator<TVal, THasher>& operator=(const RefHash2KeysTableOfEnumerator<TVal, THasher>&);
0214 
0215     // -----------------------------------------------------------------------
0216     //  Private methods
0217     // -----------------------------------------------------------------------
0218     void findNext();
0219 
0220 
0221     // -----------------------------------------------------------------------
0222     //  Data Members
0223     //
0224     //  fAdopted
0225     //      Indicates whether we have adopted the passed vector. If so then
0226     //      we delete the vector when we are destroyed.
0227     //
0228     //  fCurElem
0229     //      This is the current bucket bucket element that we are on.
0230     //
0231     //  fCurHash
0232     //      The is the current hash buck that we are working on. Once we hit
0233     //      the end of the bucket that fCurElem is in, then we have to start
0234     //      working this one up to the next non-empty bucket.
0235     //
0236     //  fToEnum
0237     //      The value array being enumerated.
0238     //
0239     //  fLockPrimaryKey
0240     //      Indicates that we are requested to iterate over the secondary keys
0241     //      associated with the given primary key
0242     //
0243     // -----------------------------------------------------------------------
0244     bool                                    fAdopted;
0245     RefHash2KeysTableBucketElem<TVal>*      fCurElem;
0246     XMLSize_t                               fCurHash;
0247     RefHash2KeysTableOf<TVal, THasher>*     fToEnum;
0248     MemoryManager* const                    fMemoryManager;
0249     const void*                             fLockPrimaryKey;
0250 };
0251 
0252 XERCES_CPP_NAMESPACE_END
0253 
0254 #if !defined(XERCES_TMPLSINC)
0255 #include <xercesc/util/RefHash2KeysTableOf.c>
0256 #endif
0257 
0258 #endif