zoukankan      html  css  js  c++  java
  • hashmap C++实现

    hashmap.h

    #ifndef _HASHMAP_H_
    #define _HASHMAP_H_
    
    template<class Key, class Value>
    class HashNode
    {
    public:
        Key    _key;
        Value  _value;
        HashNode *next;
    
        HashNode(Key key, Value value)
        {
            _key = key;
            _value = value;
            next = NULL;
        }
    
        ~HashNode(){}
    
        HashNode& operator=(const HashNode& node)
        {
            _key  = node._key;
            _value = node._value;
            next = node.next;
            return *this;
        }
    };
    
    template <class Key, class Value, class HashFunc, class EqualKey>
    class HashMap
    {
    public:
        HashMap(int size);
        ~HashMap();
        bool insert(const Key& key, const Value& value);
        bool del(const Key& key);
        Value& find(const Key& key);
        Value& operator [](const Key& key);
    
    private:
        HashFunc hash;
        EqualKey equal;
        HashNode<Key, Value> **table;
        unsigned int _size;
        Value ValueNULL;
    };
    
    template <class Key, class Value, class HashFunc, class EqualKey>
    HashMap<Key, Value, HashFunc, EqualKey>::HashMap(int size) : _size(size),hash(),equal()
    {
        table = new HashNode<Key, Value>*[_size];
        for (unsigned i = 0; i < _size; i++)
            table[i] = NULL;
    }
    
    template <class Key, class Value, class HashFunc, class EqualKey>
    HashMap<Key, Value, HashFunc, EqualKey>::~HashMap()
    {
        for (unsigned i = 0; i < _size; i++)
        {
            HashNode<Key, Value> *currentNode = table[i];
            while (currentNode)
            {
                HashNode<Key, Value> *temp = currentNode;
                currentNode = currentNode->next;
                delete temp;
            }
        }
        delete table;
    }
    
    template <class Key, class Value, class HashFunc, class EqualKey>
    bool HashMap<Key, Value, HashFunc, EqualKey>::insert(const Key& key, const Value& value)
    {
        int index = hash(key)%_size;
        HashNode<Key, Value> * node = new HashNode<Key, Value>(key,value);
        node->next = table[index];
        table[index] = node;
        return true;
    }
    
    template <class Key, class Value, class HashFunc, class EqualKey>
    bool HashMap<Key, Value, HashFunc, EqualKey>::del(const Key& key)
    {
        unsigned index = hash(key) % _size;
        HashNode<Key, Value> * node = table[index];
        HashNode<Key, Value> * prev = NULL;
        while (node)
        {
            if (node->_key == key)
            {
                if (prev == NULL)
                {
                    table[index] = node->next;
                }
                else
                {
                    prev->next = node->next;
                }
                delete node;
                return true;
            }
            prev = node;
            node = node->next;
        }
        return false;
    }
    
    template <class Key, class Value, class HashFunc, class EqualKey>
    Value& HashMap<Key, Value, HashFunc, EqualKey>::find(const Key& key)
    {
        unsigned  index = hash(key) % _size;
        if (table[index] == NULL)
            return ValueNULL;
        else
        {
            HashNode<Key, Value> * node = table[index];
            while (node)
            {
                if (node->_key == key)
                    return node->_value;
                node = node->next;
            }
        }
    }
    
    template <class Key, class Value, class HashFunc, class EqualKey>
    Value& HashMap<Key, Value, HashFunc, EqualKey>::operator [](const Key& key)
    {
        return find(key);
    }
    
    #endif

    测试:

    //首先要定义hash函数与比较函数
    class HashFunc
    {
    public:
        int operator()(const std::string& key )
        {
            int hash = 0;
            for(int i = 0; i < key.length(); ++i)
            {
                hash = hash << 7 ^ key[i];
            }
            return (hash & 0x7FFFFFFF);
        }
    };
    
    
    class EqualKey
    {
    public:
        bool operator()(const std::string& A, const std::string& B)
        {
            if (A.compare(B) == 0)
                return true;
            else
                return false;
        }
    };
    
    //测试用例
    int main()
    {
        HashMap<std::string, std::string, HashFunc, EqualKey> hashmap(100);
    
        hashmap.insert("hello", "world");
        hashmap.insert("why", "dream");
        hashmap.insert("c++", "good");
        hashmap.insert("welcome", "haha");
    
        
        std::cout << "after insert:" << std::endl;
        std::cout << hashmap.find("welcome").c_str() << std::endl;
        std::cout << hashmap.find("c++").c_str() << std::endl;
        std::cout << hashmap["why"].c_str() << std::endl;
        std::cout << hashmap["hello"].c_str() << std::endl;
    
        if (hashmap.del("hello"))
            std::cout << "remove is ok" << std::endl;    //remove is ok
        std::cout << hashmap.find("hello").c_str() << std::endl; //not exist print NULL
    
        hashmap["why"] = "love";
        std::cout << hashmap["why"].c_str() << std::endl;
        return 0;
    }

    原作者:https://www.cnblogs.com/myd620/p/6349552.html

  • 相关阅读:
    Jmeter(七)Jmeter脚本优化(数据与脚本分离)
    Jmeter(六)Jmeter脚本包含要素及书写习惯
    Redis的持久化之AOF方式
    Redis的持久化之RDB方式
    Redis持久化介绍
    Redis Keys的通用操作
    Redis的数据结构之sorted-set
    Redis的数据结构之哈希
    Redis的数据结构之字符串
    Jedis 连接池实例
  • 原文地址:https://www.cnblogs.com/evenleee/p/11345974.html
Copyright © 2011-2022 走看看