📄 hashtable.cpp
字号:
/******************************************************************************** hashtable.cpp: Hashtable manipulation*-------------------------------------------------------------------------------* (c)1999-2001 VideoLAN* $Id: hashtable.cpp,v 1.1 2001/10/06 21:23:36 bozo Exp $** Authors: Benoit Steiner <benny@via.ecp.fr>** This program is free software; you can redistribute it and/or* modify it under the terms of the GNU General Public License* as published by the Free Software Foundation; either version 2* of the License, or (at your option) any later version.** This program is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the* GNU General Public License for more details.** You should have received a copy of the GNU General Public License* along with this program; if not, write to the Free Software* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.**-------------------------------------------------------------------------------* Notice: This file must be included in the source file with its header* TO DO:* Warning: This class is not thread safe********************************************************************************///------------------------------------------------------------------------------// Preamble//------------------------------------------------------------------------------// There is no preamble since this file is to be included in the files which// use the template: look at vector.h for further explanation//******************************************************************************// class C_HashMethod//******************************************************************************// This class has a specialisation for u16 and u32 to be able to use integers// as key. Negative integers are not supported yet, they shouldn't be usefull//******************************************************************************//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// //------------------------------------------------------------------------------template <class T> C_HashMethod<T>::C_HashMethod(u32 uiMaxHash){ m_uiMaxHash = uiMaxHash;}C_HashMethod<u32>::C_HashMethod(u32 uiMaxHash){ m_uiMaxHash = uiMaxHash;}C_HashMethod<u16>::C_HashMethod(u32 uiMaxHash){ m_uiMaxHash = uiMaxHash;}C_HashMethod<handle>::C_HashMethod(u32 uiMaxHash){ m_uiMaxHash = uiMaxHash;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// //------------------------------------------------------------------------------template <class T> u32 C_HashMethod<T>::Hash(const T& cKey) const{ return cKey.GetHashCode() % m_uiMaxHash;}u32 C_HashMethod<u32>::Hash(u32 iKey) const{ return iKey % m_uiMaxHash;}u32 C_HashMethod<u16>::Hash(u16 iKey) const{ return iKey % m_uiMaxHash;}u32 C_HashMethod<handle>::Hash(handle hKey) const{ return (u32)hKey % m_uiMaxHash;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// //------------------------------------------------------------------------------template <class T> u32 C_HashMethod<T>::GetMaxHash() const{ return m_uiMaxHash;}u32 C_HashMethod<u32>::GetMaxHash() const{ return m_uiMaxHash;}u32 C_HashMethod<u16>::GetMaxHash() const{ return m_uiMaxHash;}u32 C_HashMethod<handle>::GetMaxHash() const{ return m_uiMaxHash;}//******************************************************************************// class C_Predicate//******************************************************************************// //******************************************************************************//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// //------------------------------------------------------------------------------template <class T> C_Predicate<T>::C_Predicate(){ // Nothing to do}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// //------------------------------------------------------------------------------template <class T> bool C_Predicate<T>::Compare(const T& cArg1, const T& cArg2) const{ return cArg1 == cArg2;}//******************************************************************************// class C_HashTableNode//******************************************************************************// //******************************************************************************//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// //------------------------------------------------------------------------------template <class Key, class Value> C_HashTableNode<Key, Value>::C_HashTableNode(const Key& cKey, Value* pValue) : m_cKey(cKey){ ASSERT(pValue); m_pValue = pValue;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// //------------------------------------------------------------------------------template <class Key, class Value>C_HashTableNode<Key, Value>::C_HashTableNode(const C_HashTableNode<Key, Value>& cNode) : m_cKey(cNode.m_cKey){ // Copy the object stored in the node. Since the copy constructor cannot // be virtual, it wouldn't work to call new Value(*cNode.m_pValue) with // virtual classes: we therefore use the Clone() method ASSERT(m_pValue); m_pValue = cNode.m_pValue->Clone();}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// //------------------------------------------------------------------------------template <class Key, class Value>C_HashTableNode<Key, Value>::~C_HashTableNode(){ if(m_pValue) delete m_pValue;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// //------------------------------------------------------------------------------template <class Key, class Value>const Key& C_HashTableNode<Key, Value>::GetKey() const{ return m_cKey;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// //------------------------------------------------------------------------------template <class Key, class Value>Value* C_HashTableNode<Key, Value>::GetValue() const{ return m_pValue;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// //------------------------------------------------------------------------------template <class Key, class Value> void C_HashTableNode<Key, Value>::Empty(){ m_pValue = NULL;}//******************************************************************************// class C_HashTable//******************************************************************************////******************************************************************************//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// //------------------------------------------------------------------------------template <class Key, class Value>C_HashTable<Key, Value>::C_HashTable(u32 uiBuckets) : m_cHashMethod(uiBuckets), m_cPredicate(){ m_uiArraySize = m_cHashMethod.GetMaxHash(); m_avData = new C_Vector< C_HashTableNode<Key, Value> >[m_uiArraySize];}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// //------------------------------------------------------------------------------template <class Key, class Value>C_HashTable<Key, Value>::C_HashTable(const C_HashTable<Key, Value>& cHashTable) : m_cHashMethod(cHashTable.m_cHashMethod), m_cPredicate(cHashTable.m_cPredicate){ 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); }}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// //------------------------------------------------------------------------------template <class Key, class Value> C_HashTable<Key, Value>::~C_HashTable(){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -