00001 /* 00002 * Licensed to the Apache Software Foundation (ASF) under one or more 00003 * contributor license agreements. See the NOTICE file distributed with 00004 * this work for additional information regarding copyright ownership. 00005 * The ASF licenses this file to You under the Apache License, Version 2.0 00006 * (the "License"); you may not use this file except in compliance with 00007 * the License. You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 00018 /* 00019 * $Id$ 00020 */ 00021 00022 #if !defined(XERCESC_INCLUDE_GUARD_HASH2KEYSSETOF_HPP) 00023 #define XERCESC_INCLUDE_GUARD_HASH2KEYSSETOF_HPP 00024 00025 00026 #include <xercesc/util/Hashers.hpp> 00027 #include <xercesc/util/IllegalArgumentException.hpp> 00028 #include <xercesc/util/NoSuchElementException.hpp> 00029 #include <xercesc/util/RuntimeException.hpp> 00030 #include <xercesc/util/PlatformUtils.hpp> 00031 00032 XERCES_CPP_NAMESPACE_BEGIN 00033 00034 // This hash table is similar to Hash2KeysSetOf with an additional integer as key2 00035 00036 // Forward declare the enumerator so it can be our friend. 00037 // 00038 template <class THasher> 00039 class Hash2KeysSetOfEnumerator; 00040 00041 // 00042 // This should really be a nested class, but some of the compilers we 00043 // have to support cannot deal with that! 00044 // 00045 struct Hash2KeysSetBucketElem 00046 { 00047 Hash2KeysSetBucketElem* fNext; 00048 const void* fKey1; 00049 int fKey2; 00050 }; 00051 00052 00053 template <class THasher> 00054 class Hash2KeysSetOf : public XMemory 00055 { 00056 public: 00057 // ----------------------------------------------------------------------- 00058 // Constructors and Destructor 00059 // ----------------------------------------------------------------------- 00060 00061 Hash2KeysSetOf( 00062 const XMLSize_t modulus, 00063 MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager); 00064 00065 Hash2KeysSetOf( 00066 const XMLSize_t modulus, 00067 const THasher& hasher, 00068 MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager); 00069 00070 ~Hash2KeysSetOf(); 00071 00072 00073 // ----------------------------------------------------------------------- 00074 // Element management 00075 // ----------------------------------------------------------------------- 00076 bool isEmpty() const; 00077 bool containsKey(const void* const key1, const int key2) const; 00078 void removeKey(const void* const key1, const int key2); 00079 void removeKey(const void* const key1); 00080 void removeAll(); 00081 00082 // ----------------------------------------------------------------------- 00083 // Getters 00084 // ----------------------------------------------------------------------- 00085 MemoryManager* getMemoryManager() const; 00086 XMLSize_t getHashModulus() const; 00087 00088 // ----------------------------------------------------------------------- 00089 // Putters 00090 // ----------------------------------------------------------------------- 00091 void put(const void* key1, int key2); 00092 bool putIfNotPresent(const void* key1, int key2); 00093 00094 private : 00095 // ----------------------------------------------------------------------- 00096 // Declare our friends 00097 // ----------------------------------------------------------------------- 00098 friend class Hash2KeysSetOfEnumerator<THasher>; 00099 00100 00101 private: 00102 // ----------------------------------------------------------------------- 00103 // Unimplemented constructors and operators 00104 // ----------------------------------------------------------------------- 00105 Hash2KeysSetOf(const Hash2KeysSetOf<THasher>&); 00106 Hash2KeysSetOf<THasher>& operator=(const Hash2KeysSetOf<THasher>&); 00107 00108 // ----------------------------------------------------------------------- 00109 // Private methods 00110 // ----------------------------------------------------------------------- 00111 Hash2KeysSetBucketElem* findBucketElem(const void* const key1, const int key2, XMLSize_t& hashVal); 00112 const Hash2KeysSetBucketElem* findBucketElem(const void* const key1, const int key2, XMLSize_t& hashVal) const; 00113 void initialize(const XMLSize_t modulus); 00114 void rehash(); 00115 00116 00117 // ----------------------------------------------------------------------- 00118 // Data members 00119 // 00120 // fBucketList 00121 // This is the array that contains the heads of all of the list 00122 // buckets, one for each possible hash value. 00123 // 00124 // fHashModulus 00125 // The modulus used for this hash table, to hash the keys. This is 00126 // also the number of elements in the bucket list. 00127 // 00128 // fCount 00129 // The number of elements currently in the map 00130 // 00131 // fHash 00132 // The hasher for the key1 data type. 00133 // ----------------------------------------------------------------------- 00134 MemoryManager* fMemoryManager; 00135 Hash2KeysSetBucketElem** fBucketList; 00136 XMLSize_t fHashModulus; 00137 XMLSize_t fCount; 00138 Hash2KeysSetBucketElem* fAvailable; 00139 THasher fHasher; 00140 }; 00141 00142 00143 00144 // 00145 // An enumerator for a value array. It derives from the basic enumerator 00146 // class, so that value vectors can be generically enumerated. 00147 // 00148 template <class THasher> 00149 class Hash2KeysSetOfEnumerator : public XMemory 00150 { 00151 public : 00152 // ----------------------------------------------------------------------- 00153 // Constructors and Destructor 00154 // ----------------------------------------------------------------------- 00155 Hash2KeysSetOfEnumerator(Hash2KeysSetOf<THasher>* const toEnum 00156 , const bool adopt = false 00157 , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager); 00158 virtual ~Hash2KeysSetOfEnumerator(); 00159 00160 00161 // ----------------------------------------------------------------------- 00162 // Enum interface 00163 // ----------------------------------------------------------------------- 00164 bool hasMoreElements() const; 00165 void Reset(); 00166 00167 // ----------------------------------------------------------------------- 00168 // New interface 00169 // ----------------------------------------------------------------------- 00170 void nextElementKey(const void*&, int&); 00171 void setPrimaryKey(const void* key); 00172 00173 private : 00174 // ----------------------------------------------------------------------- 00175 // Unimplemented constructors and operators 00176 // ----------------------------------------------------------------------- 00177 Hash2KeysSetOfEnumerator(const Hash2KeysSetOfEnumerator<THasher>&); 00178 Hash2KeysSetOfEnumerator<THasher>& operator=(const Hash2KeysSetOfEnumerator<THasher>&); 00179 00180 // ----------------------------------------------------------------------- 00181 // Private methods 00182 // ----------------------------------------------------------------------- 00183 void findNext(); 00184 00185 00186 // ----------------------------------------------------------------------- 00187 // Data Members 00188 // 00189 // fAdopted 00190 // Indicates whether we have adopted the passed vector. If so then 00191 // we delete the vector when we are destroyed. 00192 // 00193 // fCurElem 00194 // This is the current bucket bucket element that we are on. 00195 // 00196 // fCurHash 00197 // The is the current hash buck that we are working on. Once we hit 00198 // the end of the bucket that fCurElem is in, then we have to start 00199 // working this one up to the next non-empty bucket. 00200 // 00201 // fToEnum 00202 // The value array being enumerated. 00203 // 00204 // fLockPrimaryKey 00205 // Indicates that we are requested to iterate over the secondary keys 00206 // associated with the given primary key 00207 // 00208 // ----------------------------------------------------------------------- 00209 bool fAdopted; 00210 Hash2KeysSetBucketElem* fCurElem; 00211 XMLSize_t fCurHash; 00212 Hash2KeysSetOf<THasher>* fToEnum; 00213 MemoryManager* const fMemoryManager; 00214 const void* fLockPrimaryKey; 00215 }; 00216 00217 XERCES_CPP_NAMESPACE_END 00218 00219 #if !defined(XERCES_TMPLSINC) 00220 #include <xercesc/util/Hash2KeysSetOf.c> 00221 #endif 00222 00223 #endif