📄 refhash2keystableof.c
字号:
template <class TVal>MemoryManager* RefHash2KeysTableOf<TVal>::getMemoryManager() const{ return fMemoryManager;}// ---------------------------------------------------------------------------// RefHash2KeysTableOf: Putters// ---------------------------------------------------------------------------template <class TVal> void RefHash2KeysTableOf<TVal>::put(void* key1, int key2, TVal* const valueToAdopt){ // First see if the key exists already unsigned int hashVal; RefHash2KeysTableBucketElem<TVal>* newBucket = findBucketElem(key1, key2, hashVal); // // If so,then update its value. If not, then we need to add it to // the right bucket // if (newBucket) { if (fAdoptedElems) delete newBucket->fData; newBucket->fData = valueToAdopt; newBucket->fKey1 = key1; newBucket->fKey2 = key2; } else { newBucket = new (fMemoryManager) RefHash2KeysTableBucketElem<TVal>(key1, key2, valueToAdopt, fBucketList[hashVal]); fBucketList[hashVal] = newBucket; }}// ---------------------------------------------------------------------------// RefHash2KeysTableOf: Private methods// ---------------------------------------------------------------------------template <class TVal> RefHash2KeysTableBucketElem<TVal>* RefHash2KeysTableOf<TVal>::findBucketElem(const void* const key1, const int key2, unsigned int& hashVal){ // Hash the key hashVal = fHash->getHashVal(key1, fHashModulus, fMemoryManager); if (hashVal > fHashModulus) ThrowXMLwithMemMgr(RuntimeException, XMLExcepts::HshTbl_BadHashFromKey, fMemoryManager); // Search that bucket for the key RefHash2KeysTableBucketElem<TVal>* curElem = fBucketList[hashVal]; while (curElem) { if (fHash->equals(key1, curElem->fKey1) && (key2==curElem->fKey2)) return curElem; curElem = curElem->fNext; } return 0;}template <class TVal> const RefHash2KeysTableBucketElem<TVal>* RefHash2KeysTableOf<TVal>::findBucketElem(const void* const key1, const int key2, unsigned int& hashVal) const{ // Hash the key hashVal = fHash->getHashVal(key1, fHashModulus, fMemoryManager); if (hashVal > fHashModulus) ThrowXMLwithMemMgr(RuntimeException, XMLExcepts::HshTbl_BadHashFromKey, fMemoryManager); // Search that bucket for the key const RefHash2KeysTableBucketElem<TVal>* curElem = fBucketList[hashVal]; while (curElem) { if (fHash->equals(key1, curElem->fKey1) && (key2==curElem->fKey2)) return curElem; curElem = curElem->fNext; } return 0;}template <class TVal> void RefHash2KeysTableOf<TVal>::removeBucketElem(const void* const key1, const int key2, unsigned int& hashVal){ // Hash the key hashVal = fHash->getHashVal(key1, fHashModulus); if (hashVal > fHashModulus) ThrowXMLwithMemMgr(RuntimeException, XMLExcepts::HshTbl_BadHashFromKey, fMemoryManager); // // Search the given bucket for this key. Keep up with the previous // element so we can patch around it. // RefHash2KeysTableBucketElem<TVal>* curElem = fBucketList[hashVal]; RefHash2KeysTableBucketElem<TVal>* lastElem = 0; while (curElem) { if (fHash->equals(key1, curElem->fKey1) && (key2==curElem->fKey2)) { if (!lastElem) { // It was the first in the bucket fBucketList[hashVal] = curElem->fNext; } else { // Patch around the current element lastElem->fNext = curElem->fNext; } // If we adopted the elements, then delete the data if (fAdoptedElems) delete curElem->fData; // Delete the current element delete curElem; return; } // Move both pointers upwards lastElem = curElem; curElem = curElem->fNext; } // We never found that key ThrowXMLwithMemMgr(NoSuchElementException, XMLExcepts::HshTbl_NoSuchKeyExists, fMemoryManager);}// ---------------------------------------------------------------------------// RefHash2KeysTableOfEnumerator: Constructors and Destructor// ---------------------------------------------------------------------------template <class TVal> RefHash2KeysTableOfEnumerator<TVal>::RefHash2KeysTableOfEnumerator(RefHash2KeysTableOf<TVal>* const toEnum , const bool adopt , MemoryManager* const manager) : fAdopted(adopt), fCurElem(0), fCurHash((unsigned int)-1), fToEnum(toEnum) , fMemoryManager(manager){ if (!toEnum) ThrowXMLwithMemMgr(NullPointerException, XMLExcepts::CPtr_PointerIsZero, fMemoryManager); // // Find the next available bucket element in the hash table. If it // comes back zero, that just means the table is empty. // // Note that the -1 in the current hash tells it to start from the // beginning. // findNext();}template <class TVal> RefHash2KeysTableOfEnumerator<TVal>::~RefHash2KeysTableOfEnumerator(){ if (fAdopted) delete fToEnum;}// ---------------------------------------------------------------------------// RefHash2KeysTableOfEnumerator: Enum interface// ---------------------------------------------------------------------------template <class TVal> bool RefHash2KeysTableOfEnumerator<TVal>::hasMoreElements() const{ // // If our current has is at the max and there are no more elements // in the current bucket, then no more elements. // if (!fCurElem && (fCurHash == fToEnum->fHashModulus)) return false; return true;}template <class TVal> TVal& RefHash2KeysTableOfEnumerator<TVal>::nextElement(){ // Make sure we have an element to return if (!hasMoreElements()) ThrowXMLwithMemMgr(NoSuchElementException, XMLExcepts::Enum_NoMoreElements, fMemoryManager); // // Save the current element, then move up to the next one for the // next time around. // RefHash2KeysTableBucketElem<TVal>* saveElem = fCurElem; findNext(); return *saveElem->fData;}template <class TVal> void RefHash2KeysTableOfEnumerator<TVal>::nextElementKey(void*& retKey1, int& retKey2){ // Make sure we have an element to return if (!hasMoreElements()) ThrowXMLwithMemMgr(NoSuchElementException, XMLExcepts::Enum_NoMoreElements, fMemoryManager); // // Save the current element, then move up to the next one for the // next time around. // RefHash2KeysTableBucketElem<TVal>* saveElem = fCurElem; findNext(); retKey1 = saveElem->fKey1; retKey2 = saveElem->fKey2; return;}template <class TVal> void RefHash2KeysTableOfEnumerator<TVal>::Reset(){ fCurHash = (unsigned int)-1; fCurElem = 0; findNext();}// ---------------------------------------------------------------------------// RefHash2KeysTableOfEnumerator: Private helper methods// ---------------------------------------------------------------------------template <class TVal> void RefHash2KeysTableOfEnumerator<TVal>::findNext(){ // // If there is a current element, move to its next element. If this // hits the end of the bucket, the next block will handle the rest. // if (fCurElem) fCurElem = fCurElem->fNext; // // If the current element is null, then we have to move up to the // next hash value. If that is the hash modulus, then we cannot // go further. // if (!fCurElem) { fCurHash++; if (fCurHash == fToEnum->fHashModulus) return; // Else find the next non-empty bucket while (true) { if (fToEnum->fBucketList[fCurHash]) break; // Bump to the next hash value. If we max out return fCurHash++; if (fCurHash == fToEnum->fHashModulus) return; } fCurElem = fToEnum->fBucketList[fCurHash]; }}XERCES_CPP_NAMESPACE_END
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -