哈希表

· · 算法·理论

写着玩儿,卡常用用啥的,没有在犇犇里引流,应该没人看吧(

综述

随机数据大概比 unordered_map 快五倍。哈希方式来自此处,应该卡不掉。学着 unordered_map 实现了一部分其中的常用函数,但功能与 unordered_map 可能有一些差异。

定义 n 表示上一次调用 clear() 后插入的元素个数,注意 erase 不会减小 n。

该数据结构用于维护一些键-值二元组。空间复杂度 O(n+p)。

函数 bool empty()const

返回容器中是否没有元素。

时间复杂度:O(1)。

函数 unsigned size()const

返回容器中的元素个数。

时间复杂度:O(1)。

函数 void clear()

删除容器中的所有元素。

时间复杂度:O(n)。

函数 Q*find(const Key&x)

寻找容器中是否有键等于 x 的二元组。若有,则返回指向存储该元组的 Q 类型的指针,否则返回空指针。

时间复杂度:期望 O(1),最坏 O(n)。由于目前没有发现卡该哈希的方法,可以认为是 O(1)。

函数 std::pair<Q*,bool>insert(const Key&x,const T&y)

寻找容器中是否有键等于 x 的二元组。若有,则返回指向存储该元组的 Q 类型的指针和 false 构成的二元组;否则向容器中插入键等于 x、值等于 y 的二元组,然后返回指向存储该元组的 Q 类型的指针和 true 构成的二元组。

时间复杂度:期望 O(1),最坏 O(n)。由于目前没有发现卡该哈希的方法,可以认为是 O(1)。

函数 bool erase(const Key&x)

寻找容器中是否有键等于 x 的二元组。若有,则删除之,返回 true;否则返回 false。

时间复杂度:期望 O(1),最坏 O(n)。由于目前没有发现卡该哈希的方法,可以认为是 O(1)。

函数 T&operator[](const Key&x)

寻找容器中是否有键等于 x 的二元组。若有,则返回其值的引用;否则向容器中插入键等于 x、值默认的二元组,然后返回其值的引用。

这里默认值指在堆空间里开 T 这种类型时其值。就是直接拎出来 e 这个数组里一个没有被访问过的元素的值。

时间复杂度:期望 O(1),最坏 O(n)。由于目前没有发现卡该哈希的方法,可以认为是 O(1)。

代码

template<typename Key,typename T,unsigned n,unsigned p>struct UNORDERED_MAP{
// Key : key type (signed/unsigned int/long long)
// T : value type
// n : the maximum total number of times insert() and operator[] is called
// p : number of buckets
    unsigned hsh(unsigned long long x)const{
        static const unsigned long long t=
        std::chrono::steady_clock::now().time_since_epoch().count();
        x+=t+0x9e3779b97f4a7c15;
        x=(x^(x>>30))*0xbf58476d1ce4e5b9;
        x=(x^(x>>27))*0x94d049bb133111eb;
        return(x^(x>>31))%p;
    }
    struct Q{Key x;T y;unsigned z;}e[n+1];
    unsigned h[p],idx,sz;
    bool empty()const{return!sz;}
    unsigned size()const{return sz;}
    void clear(){
        for(unsigned i=1;i<=idx;++i) h[hsh(e[i].x)]=0,e[i]=e[i-1];
        idx=sz=0;
    }
    Q*find(const Key&x){
        auto z=hsh(x);
        for(auto i=h[z];i;i=e[i].z)
            if(e[i].x==x)
                return&e[i];
        return 0;
    }
    std::pair<Q*,bool>insert(const Key&x,const T&y){
        auto z=hsh(x);
        for(auto i=h[z];i;i=e[i].z)
            if(e[i].x==x)
                return std::make_pair(&e[i],0);
        ++idx,++sz,e[idx].x=x,e[idx].y=y,e[idx].z=h[z],h[z]=idx;
        return std::make_pair(&e[idx],1);
    }
    bool erase(const Key&x){
        auto z=hsh(x);
        for(auto i=h[z],j=-1;i;j=i,i=e[i].z)
            if(e[i].x==x)
                return(~j?e[j].z:h[z])=e[i].z,--sz,1;
        return 0;
    }
    T&operator[](const Key&x){
        auto z=hsh(x);
        for(auto i=h[z];i;i=e[i].z)
            if(e[i].x==x)
                return e[i].y;
        ++idx,++sz,e[idx].x=x,e[idx].z=h[z],h[z]=idx;
        return e[idx].y;
    }
};

示例

#include<cstdio>
UNORDERED_MAP<int,int,10000000,100000007>mp;
int main(){
    mp[5]-=6;
    printf("mp[5]=%d\n",mp[5]);
    mp[5]=114;
    printf("mp[5]=%d\n",mp[5]);
    printf("insert(5).second=%d\n",int(mp.insert(5,514).second));
    printf("insert(-1).second=%d\n",int(mp.insert(-1,514).second));
    if(mp.find(5)) printf("++mp[5]=%d\n",++(mp.find(5)->y));
    else printf("mp[5] does not exist\n");
    printf("mp.empty()=%d\n",mp.empty());
    printf("mp.erase(5)=%d\n",mp.erase(5));
    if(mp.find(5)) printf("++mp[5]=%d\n",++(mp.find(5)->y));
    else printf("mp[5] does not exist\n");
    printf("mp.size()=%d\n",mp.size());
    printf("mp[999]=%d\n",mp[999]);
    printf("mp.size()=%d\n",mp.size());
    mp.clear();
    printf("mp.empty()=%d\n",mp.empty());
    return 0;
}

示例输出

mp[5]=-6
mp[5]=114
insert(5).second=0
insert(-1).second=1
++mp[5]=115
mp.empty()=0
mp.erase(5)=1
mp[5] does not exist
mp.size()=1
mp[999]=0
mp.size()=2
mp.empty()=1