zoukankan      html  css  js  c++  java
  • HashTable 哈希表 C++

    What’s a Hash Table? Why we need a Hash Table?

    By Using a Hash Table we can find element very quickly. For example, There are 20 random number in an array below.

    It’s not a sorted array, So We can not use Binary Search to finding a number, When we need to find  118, We need 12 comparisons! Finding number like 270, 198, we need even more comparison.

    We change the way the 20 number store. We store them in 10 linked lists. There is a rule, If the number’s last digit is 0, so we insert it in a  linked list which  index is 0. Look at  the Figure 1 for more detail. like number 118, it’s last digit is 8, so we  insert it in ninth linked list.

    Hash Function

    Note that the basic hash table which we explained above used a modulo function. There are many way to hash data, but modulo is the most common. What’s more, the modulo function often use a prime number. That was because you get fewer collisions when you modulo a key  by a prime number. Having fewer collisions makes your table easier to work and more efficient. There is a completed mathmatical reasoning behind this, but it is okay for us to assume that this is true for us.

    C++ Code of Hash Table

    /* Class : HastTable
     * Function : A simple Hash table class
     * Author : Jason
     * Date : 2014.03.16
     */
    class HashTable
    {
    public:
        typedef list<int>::iterator ListIter;
    private:
        list<int> m_Container[10];
        int HashFunction(const int& v) const;
        int m_Count;
    public:
        HashTable();
        ~HashTable();
        void Insert(const int& v);
        bool Find(const int& v);
        bool Delete(const int& v);    
        int Count() const;
    };
    
    HashTable::HashTable()
    {
        m_Count = 0;
    }
    
    HashTable::~HashTable()
    {
        m_Count = 0;
        for (int i = 0; i < 10; i++)
        {
            m_Container[i].clear();
        }
    }
    
    // This Hash Function is very simple
    int HashTable::HashFunction(const int& v) const
    {
        return v%10;
    }
    
    int HashTable::Count() const
    {
        return m_Count;
    }
    
    void HashTable::Insert(const int& v)
    {
        int hashRes = HashFunction(v);
        m_Container[hashRes].push_back(v);
        ++m_Count;
    }
    
    bool HashTable::Find(const int& v)
    {
        int hashRes = HashFunction(v);
        //typedef list<int>::iterator ListIter;
        //ListIter FindIter = find(m_Container[hashRes].begin(), m_Container[hashRes].end(), v);
        //if (FindIter != m_Container[hashRes].end())
        //{
        //    return true;
        //} 
        //else
        //{
        //    return false;
        //}
        for (ListIter FindIter = m_Container[hashRes].begin(); FindIter != m_Container[hashRes].end(); ++FindIter)
        {
            if (*FindIter == v)
            {
                return true;
            }
        }
        return false;
    }
    
    bool HashTable::Delete(const int& v)
    {
        int hashRes = HashFunction(v);
        //ListIter DelIter = find(m_Container[hashRes].begin(), m_Container[hashRes].end(), v);
        //if (DelIter != m_Container[hashRes].end())
        //{
        //    m_Container[hashRes].erase(DelIter);
        //    --m_Count;
        //    return true;
        //}
        //return false;
        for (ListIter DelIter = m_Container[hashRes].begin(); DelIter != m_Container[hashRes].end(); ++DelIter)
        {
            if (*DelIter == v)
            {
                m_Container[hashRes].erase(DelIter);
                m_Count--;
                return true;
            } 
        }
        return false;
    }

    下面是是一个稍微复杂一些的C++版本的代码

    /* Class : HastTable
     * Function : A completed Hash table class
     * Author : Jason
     * Date : 2014.03.16
     */
    class Entry
    {
    private:
        int m_nKey;
        string m_strData;
    public:
        friend bool operator==(const Entry& E1, const Entry& E2);
        Entry();
        Entry(const int& nKey, const string& strData);
        ~Entry();
        void SetKey(int nKey);
        void SetData(const string& strData);
        int GetKey() const;
        string GetData() const;
    };
    
    bool operator==(const Entry& E1, const Entry& E2)
    {
        return E1.m_nKey == E2.m_nKey ? true : false;
    }
    
    Entry::Entry()
    {}
    Entry::Entry(const int& nKey, const string& strData)
    {
        m_nKey = nKey;
        m_strData = strData;
    }
    Entry::~Entry()
    {
        m_nKey = 0;
        m_strData = '';
    }
    void Entry::SetKey(int nKey)
    {
        m_nKey = nKey;
    }
    void Entry::SetData(const string& strData)
    {
        m_strData = strData;
    }
    int Entry::GetKey() const
    {
        return m_nKey;
    }
    string Entry::GetData() const
    {
        return m_strData;
    }
    
    class EntryHashTable
    {
    public:
        typedef list<Entry>::const_iterator ListEntryIter;
    private:
        list<Entry> m_Container[10];
        int HashFunction(const int& entry) const;
        int m_Count;
    public:
        EntryHashTable();
        ~EntryHashTable();
        void Insert(const Entry& entry);
        bool Find(const Entry& entry) const;
        bool Delete(const Entry& entry);
        int Count() const;
    };
    
    int EntryHashTable::HashFunction(const int& v) const
    {
        return v%10;
    }
    
    EntryHashTable::EntryHashTable()
    {
        
    }
    
    EntryHashTable::~EntryHashTable()
    {
        for (int i = 0; i < 10; i++)
        {
            m_Container[i].clear();
        }
        m_Count = 0;
    }
    
    void EntryHashTable::Insert(const Entry& entry)
    {
        int HashRes = HashFunction(entry.GetKey());
        m_Container[HashRes].push_back(entry);
        ++m_Count;
    }
    
    bool EntryHashTable::Find(const Entry& entry) const
    {
        int HashRes = HashFunction(entry.GetKey());
        ListEntryIter FindIter;
        for (FindIter = m_Container[HashRes].begin(); FindIter != m_Container[HashRes].end(); ++FindIter)
        {
            if (*FindIter == entry)
            {
                return true;
            }
        }
        return false;
    }
    
    bool EntryHashTable::Delete(const Entry& entry)
    {
        int HashRes = HashFunction(entry.GetKey());
        ListEntryIter DelIter;
        for (DelIter = m_Container[HashRes].begin(); DelIter != m_Container[HashRes].end(); ++DelIter)
        {
            if (*DelIter == entry)
            {
                m_Container[HashRes].erase(entry);
                --m_Count;
                return true;
            }
        }
        return false;
    }
    
    int EntryHashTable::Count() const
    {
        return m_Count;
    }
  • 相关阅读:
    时间类型:datetime,timestamp,date,time,year
    字符串类型:char,varchar,text,enum,set
    RHEL7安装ZABBIX 3.2
    Go-06-数据类型、常量、运算符
    GO-05-数据类型
    GO-04-变量
    GO-03-基础
    GO-02-helloworld
    Ambassador-09-prefix正则表达式
    Ambassador-08-跨域
  • 原文地址:https://www.cnblogs.com/Jasonscor/p/3604336.html
Copyright © 2011-2022 走看看