📄 hashtable.cpp
字号:
delete[] m_avData;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// //------------------------------------------------------------------------------template <class Key, class Value>C_HashTable<Key, Value>& C_HashTable<Key, Value>::operator = (const C_HashTable<Key, Value>& cHashTable){ delete[] m_avData; m_uiArraySize = m_cHashMethod.GetMaxHash(); m_avData = new C_Vector< C_HashTableNode<Key, Value> >[m_uiArraySize]; C_HashTableIterator<Key, Value> cIterator = cHashTable.CreateIterator(); while(cIterator.HasNext()) { C_HashTableNode<Key, Value>* pNode = cIterator.GetNext(); Value* pValue = pNode->GetValue()->Clone(); Add(pNode->GetKey(), pValue); } return *this;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// //------------------------------------------------------------------------------template <class Key, class Value>void C_HashTable<Key, Value>::Add(const Key& cKey, Value* pValue){ u32 uiHashCode = m_cHashMethod.Hash(cKey); C_Vector< C_HashTableNode<Key, Value> >& cVector = m_avData[uiHashCode];#ifdef DEBUG bool bFound = 0; unsigned int iIndex; for(iIndex = 0; iIndex < cVector.Size(); iIndex++) { bFound = m_cPredicate.Compare(cVector[iIndex].m_cKey, cKey); if(bFound) break; } ASSERT(!bFound);#endif C_HashTableNode<Key, Value>* pNewNode = new C_HashTableNode<Key, Value> (cKey, pValue); cVector.Add(pNewNode);} //------------------------------------------------------------------------------// //------------------------------------------------------------------------------// //------------------------------------------------------------------------------template <class Key, class Value>Value* C_HashTable<Key, Value>::Remove(const Key& cKey){ ASSERT(Get(cKey)); u32 uiHashCode = m_cHashMethod.Hash(cKey); C_Vector< C_HashTableNode<Key, Value> >& cVector = m_avData[uiHashCode]; Value* pItem = NULL; for(unsigned int iIndex = 0; iIndex < cVector.Size(); iIndex++) { bool bFound = m_cPredicate.Compare(cVector[iIndex].m_cKey, cKey); if(bFound) { C_HashTableNode<Key, Value>* pNode = cVector.Remove(iIndex); pItem = pNode->GetValue(); pNode->Empty(); delete pNode; break; } } ASSERT(pItem); return pItem;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// //------------------------------------------------------------------------------template <class Key, class Value>void C_HashTable<Key, Value>::Delete(const Key& cKey){ ASSERT(Get(cKey)); u32 uiHashCode = m_cHashMethod.Hash(cKey); C_Vector< C_HashTableNode<Key, Value> >& cVector = m_avData[uiHashCode]; for(unsigned int iIndex = 0; iIndex < cVector.Size(); iIndex++) { bool bFound = m_cPredicate.Compare(cVector[iIndex].m_cKey, cKey); if(bFound) { cVector.Delete(iIndex); break; } }}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// Update or create the value stored under Key, and destroy the old one if// it existed//------------------------------------------------------------------------------template <class Key, class Value>void C_HashTable<Key, Value>::Update(const Key& cKey, Value* pNewValue){ Value* pOldValue = Modify(cKey, pNewValue); if(pOldValue) delete pOldValue;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// Return the value or NULL if it does not exist//------------------------------------------------------------------------------template <class Key, class Value>Value* C_HashTable<Key, Value>::Get(const Key& cKey) const{ u32 uiHashCode = m_cHashMethod.Hash(cKey); C_Vector< C_HashTableNode<Key, Value> >& cVector = m_avData[uiHashCode]; Value* pResult = NULL; for(unsigned int iIndex = 0; iIndex < cVector.Size(); iIndex++) { bool bFound = m_cPredicate.Compare(cVector[iIndex].m_cKey, cKey); if(bFound) { pResult = cVector[iIndex].m_pValue; break; } } return pResult; }//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// Return the old value or null if the entry does not exist, and set the entry// to the new one. If the entry did not exist, it is added//------------------------------------------------------------------------------template <class Key, class Value>Value* C_HashTable<Key, Value>::Modify(const Key& cKey, Value* pNewValue){ u32 uiHashCode = m_cHashMethod.Hash(cKey); C_Vector< C_HashTableNode<Key, Value> >& cVector = m_avData[uiHashCode]; Value* pResult = NULL; for(unsigned int iIndex = 0; iIndex < cVector.Size(); iIndex++) { bool bFound = m_cPredicate.Compare(cVector[iIndex].m_cKey, cKey); if(bFound) { pResult = cVector[iIndex].m_pValue; cVector[iIndex].m_pValue = pNewValue; break; } } if(pResult == NULL) Add(cKey, pNewValue); return pResult; }//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// Returns the number of values stored in the hashtable//------------------------------------------------------------------------------template <class Key, class Value>unsigned int C_HashTable<Key, Value>::Size() const{ unsigned int uiResult = 0; for(unsigned int i = 0; i < m_cHashMethod.GetMaxHash(); i++) { uiResult += m_avData[i].Size(); } return uiResult;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// Creates an iterator to browse the values contained in the hashtable.//------------------------------------------------------------------------------template <class Key, class Value>C_HashTableIterator<Key, Value> C_HashTable<Key, Value>::CreateIterator() const{ return C_HashTableIterator<Key, Value>(*this);}//******************************************************************************// class C_HashTableIterator//******************************************************************************////******************************************************************************//------------------------------------------------------------------------------// //------------------------------------------------------------------------------////------------------------------------------------------------------------------template <class Key, class Value>C_HashTableIterator<Key, Value>::C_HashTableIterator(const C_HashTable<Key, Value>& cHashTable) : m_cHashTable(cHashTable){ Reset();}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------////------------------------------------------------------------------------------template <class Key, class Value>bool C_HashTableIterator<Key, Value>::HasNext(){ bool bResult = false; while(m_iCurrentVector < m_cHashTable.m_uiArraySize) { if(m_iCurrentVectorItem < m_cHashTable.m_avData[m_iCurrentVector].Size()) { // We found an item bResult = true; break; } else { // Go to the beginning of the next vector in the buckets array m_iCurrentVector++; m_iCurrentVectorItem = 0; } } return bResult;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------////------------------------------------------------------------------------------template <class Key, class Value>C_HashTableNode<Key, Value>* C_HashTableIterator<Key, Value>::GetNext(){ ASSERT(HasNext()); // Read the value at the current position C_Vector< C_HashTableNode<Key, Value> >& cVector = m_cHashTable.m_avData[m_iCurrentVector]; C_HashTableNode<Key, Value>& cResult = cVector[m_iCurrentVectorItem]; // Prepare the next iteration m_iCurrentVectorItem++; return &cResult;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------////------------------------------------------------------------------------------template <class Key, class Value> void C_HashTableIterator<Key, Value>::Reset(){ m_iCurrentVector = 0; m_iCurrentVectorItem = 0; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -