The open-chain method (hash bucket) is a common technique for resolving the conflict in Kazakhstan and is structured as follows:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/80/11/wKiom1c1z0jiqWopAAAtDyd3Kyk688.png "title=" Qq20160513205751.png "alt=" Wkiom1c1z0jiqwopaaatdyd3kyk688.png "/>
The design of the data structure is like this, define a k-v chain node (node), storing the nodes pointer in an array way
The implementation code is as follows:
#include <vector> #include "HashTable.h" size_t getsize () {static size_t index = 0; Const int _primesize = 28;static const unsigned long _primelist[_ primesize] ={53ul, 97ul, 193ul, 389ul, 769ul,1543ul, 3079ul, 6151ul, 12289ul, 24593ul,49157ul, 98317ul, 196613ul, 393241ul, 786433ul,1572869ul, 3145739ul, 6291469ul, 12582917ul, 25165843ul,50331653ul, 100663319ul, 201326611ul, 402653189ul, 805306457ul,1610612741ul, 3221225473ul, 4294967291ul};return _primelist [index++];} Template<class k,class v>struct hashbucketnode{hashbucketnode (Const K& key, const v& value): _key (Key), _value (value), _next (NULL) {}k _key; v _value; hashbucketnode* _next;}; template<class k, class v, class hashfunc = defaulthash&Lt k> >class hashbucket{public:typedef hashbucketnode<k,v> node; Hashbucket (): _size (0) {_tables.resize (0);} Bool push (const k& key, const v& value) {_CheckCapacity ();size_t Index = hashfunc () (key) % _tables.size (); node*cur = _tables[index];while (cur) {if (cur->_key == key&&cur->_ Value == value) Return false;cur = cur->_next;} Node*tmp = new node (Key, value);if (_tables[index]) {_tables[index]->_next = tmp->_next;} _tables[index] = tmp;++_size;return true;} Void swap (hashbucket & h) {Swap (_size, h._size); _tables.swap (h._tables);} Node* find (Const k& key, const v& value) {size_t index = Hashfunc () (key) % _tables.size (); node*cur = _tables[index];while (cur) {if (cur->_key&nbSp;== key&&cur->_value == value) Return cur;cur = cur->_next;} Return null;} Bool remove (Const k& key) {Size_t index = hashfunc () (key) % _ Tables.size ();if (_tables[index]) {if (_tables[index]->key == key) {Node*tmp = _tables[index];_tables[index] = tmp->_next;delete tmp;return true;} else{node*cur = _tables[index];while (Cur->_next) {if (cur->_next->_key == key) {node*tmp = cur->_next;cur->_next = cur->_next->_next;delete Tmp;return true;} Cur = cur->_next;}}} Return false;} Protected:void _checkcapacity () {if (_size >= _tables.size ()) {HashBucket tmp;tmp._ Tables.resize (GetSize ());for (Size_t i = 0; i < tmp._tables.size (); ++i) {node*cur = tmp._tables[i];while (cur) {tmp. PuSH (cur->_key, cur->_value); cur = cur->_next;}} Swap (TMP);}} protected:vector<node*> _tables;size_t _size;};
If there is any shortage of hope to correct, there is doubt to raise
The implementation of the "dry" C + + hash Bucket (open chain method for solving the conflict of the SID) class