日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2023/12/4 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hash table(开放寻址法-线性探查实现的哈希表) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

hash table(開(kāi)放尋址法-線性探查實(shí)現(xiàn)的哈希表)

// // Created by 許加權(quán) 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

測(cè)試代碼

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函數(shù)是利用乘法散列法實(shí)現(xiàn)的

總結(jié)

以上是生活随笔為你收集整理的hash table(开放寻址法-线性探查实现的哈希表)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。