hash table(开放寻址法-线性探查实现的哈希表)

hash table(开放寻址法-线性探查实现的哈希表)

//
// Created by 许加权 on 2021/7/17.
//

#ifndef C11LEARN_HASHLINER_H
#define C11LEARN_HASHLINER_H
#include "KeyNode.h"
template<typename T>
class HashLiner
{
public:
    HashLiner();
    HashLiner(const HashLiner<T> & hashLiner);
    ~HashLiner();
    const HashLiner<T>& operator=(const HashLiner<T> & hashLiner);
    T & operator[](int key);
protected:
    long long capacity;
    KeyNode<T>** array;
    long long w;
    long long p;
    long long s;
    long long two_32;
protected:
    virtual int auxiliary_hashing(int key);
    virtual int hashing(int key,int index);
    void insert(KeyNode<T>* node);
    KeyNode<T>*search(int key);
    void clear();
    void copy(const HashLiner<T> & hashLiner);
};

template<typename T>
HashLiner<T>::HashLiner(){
    s = 2654435769;
    w = 32;
    p = 14;
    two_32 = 1;
    two_32 = two_32<<32;
    capacity = 1<<p;
    array = new KeyNode<T>*[capacity];
}
template<typename T>
HashLiner<T>::HashLiner(const HashLiner<T> & hashLiner){
    s = 2654435769;
    w = 32;
    p = 14;
    two_32 = 1;
    two_32 = two_32<<32;
    capacity = 1<<p;
    array = new KeyNode<T>*[capacity];
    copy(hashLiner);
}
template<typename T>
HashLiner<T>::~HashLiner(){
    clear();
    if(array!= nullptr)
    {
        delete [] array;
        array = nullptr;
    }
}
template<typename T>
void HashLiner<T>::copy(const HashLiner<T> & hashLiner){
    for (int i = 0; i < capacity; ++i) {
        if(hashLiner.array[i]!= nullptr)
        {
            array[i] = new KeyNode<T>(hashLiner.array[i]->key,hashLiner.array[i]->value);
        }
    }
}
template<typename T>
const HashLiner<T>& HashLiner<T>::operator=(const HashLiner<T> & hashLiner){
    if(this == &hashLiner) return *this;
    clear();
    copy(hashLiner);
    return *this;
}
template<typename T>
T & HashLiner<T>::operator[](int key){
    KeyNode<T>* node = search(key);
    if(node == nullptr)
    {
        node = new KeyNode<T>();
        node->key = key;
        insert(node);
    }
    return node->value;
}
template<typename T>
int HashLiner<T>::auxiliary_hashing(int key){
    return ((key*s)%two_32)>>(w-p);
}
template<typename T>
int HashLiner<T>::hashing(int key,int index){
    return (auxiliary_hashing(key)+index) % capacity;
}
template<typename T>
void HashLiner<T>::insert(KeyNode<T>* node){
    int i = -1;
    int j;
    while (++i<capacity)
    {
        j = hashing(node->key,i);
        if(array[j] == nullptr) {
            array[j] = node;
            return;
        }
    }
    throw "hash table overflow";
}
template<typename T>
KeyNode<T>* HashLiner<T>::search(int key){
    int i = -1;
    int j;
    while (++i<capacity)
    {
       j = hashing(key,i);
       if(array[j] == nullptr)
           return nullptr;
       if(array[j]->key == key)
           return array[j];
    }
    return nullptr;
}
template<typename T>
void HashLiner<T>::clear(){
    for (int i = 0; i < capacity; ++i) {
        if(array[i]!= nullptr)
        {
            delete array[i];
            array[i] = nullptr;
        }
    }
}
#endif //C11LEARN_HASHLINER_H

测试代码

    HashLiner<string> hashLiner;
    hashLiner[2] = "hello";
    hashLiner[123456] = "world";
    cout << hashLiner[2] << endl;
    cout << hashLiner[123456] << endl;
    HashLiner<string> hashLiner1 = hashLiner;
    cout << hashLiner1[2] << endl;
    cout << hashLiner1[123456] << endl;
    HashLiner<string> hashLiner2;
    hashLiner2 = hashLiner;
    cout << hashLiner2[2] << endl;
    cout << hashLiner2[123456] << endl;

辅助类
KeyNode地址
辅助auxiliary_hashing函数是利用乘法散列法实现的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值