简单的闭散列(hashtable)实现(c++)

用c++实现的一个简单的闭散列,使用链表来解决slot冲突,使用移位与的方式来作为hash函数(hash函数摘自Redis源码)

#include <list>
#include <iostream>
#include <algorithm>
#include <string>
//#include <google/profiler.h>

extern "C" {
#include <ctype.h>
#include <string.h>
}

template <typename T>
class HashTable {
    private:
        std::list< std::pair<std::string, T> >* ht;
        static const int dict_hash_function_seed = 5381;
        int size;
    public:
        HashTable(int s)
            : size(s)
        {
            ht = new std::list< std::pair<std::string, T> >[size];
        }

        /* And a caseinsensitive hash function (based on djb hash) */
        //来源于Redis
        unsigned int dictGenCaseHashFunction(const std::string& key) {
            const char* buf = key.c_str();
            int len = key.length();
            unsigned int hash = (unsigned int)dict_hash_function_seed;

            while (len--)
                hash = ((hash << 5) + hash) +(tolower(*buf++)); /* hash * 33 + c */
            return hash % size;
        }

        bool hash(const std::string &key, const T& t) {
            auto slot = &ht[dictGenCaseHashFunction(key)];
            for(auto it = slot->begin(); it != slot->end(); it++) {
                if(key == it->first) {
                    it->second = t;
                    return true;
                }
            }
            slot->push_back(std::pair<std::string, T>(key, t));
            return true;
            //std::cout<<ht[slotPos].size()<<std::endl;
        }

        bool get(const std::string& key, T& t) {
            auto slot = &ht[dictGenCaseHashFunction(key)];
            auto it = slot->begin();
            for(it = slot->begin(); it != slot->end(); it++) {
                if(key == it->first) {
                    t = it->second;
                    //std::cout<<t<<std::endl;
                    return true;
                }
            }
            return false;
        }

        bool remove(const std::string& key) {
            auto slot = &ht[dictGenCaseHashFunction(key)];
            auto it = slot->begin();
            for(it = slot->begin(); it != slot->end(); it++) {
                if(key == it->first) {
                    //std::cout<<it->second<<std::endl;
                    return true;
                }
            }
            return false;
        }

        ~HashTable() {
            //ProfilerStart("profiler");
            delete []ht;
            //ProfilerStop();
        }

};

int main(int argc, const char** argv) {
    HashTable<int> hashTable(10000000);
    /*
    for(int i = 0; i < 10000000; i++) {
        if(i%2 == 0) {
            continue;
        }
        std::string key = std::to_string(i);
        hashTable.hash(key, i*10);
    }
    //ProfilerStart("profiler");
    for(int i = 8999999; i < 10000000; i++) {
        std::string key = std::to_string(i);
        int a = 0;
        hashTable.get(key, a);
    }
    //ProfilerStop();
    int a = 0;
    hashTable.get("127", a);
    hashTable.hash("127", 127);
    hashTable.get("127", a);*/
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++语言中的二次探测散列法是一种常见的散列表实现方法,用于解决哈希冲突问题。其基本思想是,当哈希函数计算出的位置已经被占用时,按照一定的规则在散列表中查找下一个空闲位置,直到找到为止。 具体实现过程如下: 1. 对于给定的关键字,通过哈希函数计算出它在散列表中的位置。 2. 如果该位置已经被占用,则按照二次探测法的规则,在散列表中查找下一个空闲位置。具体规则为,从当前位置开始,依次查找 $(1^2, 2^2, 3^2, \ldots)$ 个位置,直到找到一个空闲位置为止。 3. 如果散列表已经满了,则无法插入新的关键字。 4. 在查找时,如果找到了与待查找关键字相等的关键字,则说明该关键字已经存在于散列表中。 下面是二次探测散列法的C++代码实现: ```cpp const int MAX_SIZE = 10007; // 哈希表中的每个元素结构体 struct HashNode { int key; // 关键字 int value; // 值 }; // 哈希表类 class HashTable { public: HashTable(); ~HashTable(); void Insert(int key, int value); void Remove(int key); int Find(int key); private: HashNode* data; // 哈希表数据 int size; // 哈希表大小 int count; // 哈希表中元素个数 int HashFunc(int key); // 哈希函数 int GetNextPos(int pos, int i); // 获取下一个空闲位置 }; HashTable::HashTable() { size = MAX_SIZE; count = 0; data = new HashNode[size]; } HashTable::~HashTable() { delete[] data; } void HashTable::Insert(int key, int value) { int pos = HashFunc(key); int i = 0; while (data[pos].key != -1 && i < size) { pos = GetNextPos(pos, i); i++; } if (i >= size) { cout << "HashTable is full." << endl; return; } data[pos].key = key; data[pos].value = value; count++; } void HashTable::Remove(int key) { int pos = HashFunc(key); int i = 0; while (data[pos].key != key && i < size) { pos = GetNextPos(pos, i); i++; } if (i >= size) { cout << "Key not found." << endl; return; } data[pos].key = -1; data[pos].value = -1; count--; } int HashTable::Find(int key) { int pos = HashFunc(key); int i = 0; while (data[pos].key != key && i < size) { pos = GetNextPos(pos, i); i++; } if (i >= size) { cout << "Key not found." << endl; return -1; } return data[pos].value; } int HashTable::HashFunc(int key) { return key % size; } int HashTable::GetNextPos(int pos, int i) { return (pos + i * i) % size; } ``` 在上面的代码中,`HashTable` 类封装了二次探测散列法的实现细节,提供了插入、删除和查找操作。其中,`data` 数组存储了哈希表中的元素,`size` 表示哈希表的大小,`count` 表示哈希表中元素的个数。`HashFunc` 函数实现了哈希函数,`GetNextPos` 函数实现了获取下一个空闲位置的规则。在插入、删除和查找操作中,都需要通过哈希函数计算出待操作关键字在哈希表中的位置,然后按照二次探测法的规则查找下一个空闲位置或者待操作关键字所在的位置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值