#pragma once#include<iostream>//with one probe two probes #include<vector> #include <math.h> Using namespace std;enum status{exist,empty,delet,};template <class k,class v > //key/value form Structure Body struct kv{k _key; v _value; KV (Const k& key = k (), const v& value = v ()): _key (Key), _value (value) {}};template <class k, class v>class hashtable{public:kv< k,v>* _tables;size_t _size;size_t _capacity; Status* _status;public:hashtable (): _tables (null), _size (0), _capacity (0), _status (null) {}void insert (const k& key , const v& value ) {if (_ capacity == 0 | | &NBSP;_SIZE&NBSP;*&NBSP;10&NBSP;/&NBSP;_CAPACITY&NBSP;>&NBSP;7) {if (!_increasecapacity ()) cout<< " Linear detection Hash list is full "<<ENDL;} Size_t i = _hashfanction1 (Key);d o{if (Key == _tables[i]._key&&_status[i]==exist) break;if (_status[i] != exist) {_tables[i]. _key = key;_tables[i]._value = value;_status[i] = exist;++_size;break;} else if (++i >= _capacity) i = 0;} while (1);} Void insert2 (Const k& key, const v& value) {if (_capacity == 0 | | _size * 10 / _capacity > 7) {if (!_increasecapacity ()) cout << "Linear detection hash list is full" &NBSP;<<&NBSP;ENDL;} int count = 0;//counter for two probes Size_t i = _hashfanction2 (key,count);d o{if (i<0) i = _capacity + i;if (key == _tables[i]._key&&_status[i] == exist) break;if (_status[i] != exist) {_tables[i]._key = key;_tables[i]._value = value;_status[i] = exist;++_size;break;} Else i = _hasHFanction2 (key, i);} while (1);} Int find (Const k& key) {size_t i = _hashfanction1 (key); size_t j = i;do{if (_status[i] == exist&&key==_tables[i]._key) {return i;} else if (++i >= _capacity) i = 0;} while (i!=j); return -1;} Void remove (Const k& key) {size_t i = _hashfanction1 (key);size_t j = i;do{if (_status[i] == exist&&key == _tables[i]._key) {_status[i] = delet;break;} else if (++i >= _capacity) i = 0;} while (I&NBSP;!=&NBSP;J);} Void print () {size_t i = 0;for (; i < _capacity; ++i) {printf ("[ %d] k: %d v: %d s: %d \n ", I, _tables[i]._key, _tables[i]._ Value, _status[i]);}} Protected:size_t _hashfanction1 (const k&&Nbsp;key)//One-time detection (linear detection) {return key%_capacity;} Int _hashfanction2 (Const k& key,const int i)//two times probe (prevent data accumulation in one piece of memory) {return (key+ (int) pow ( -1,i) *i*i)%_capacity;} Bool _increasecapacity () {const int _primesize = 28;//Prime table 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};for (Int i = 0; i < _primesize; ++i) {if (_ primelist[i]>_capacity) {size_t newcapacity = _primelist[i]; Hashtable<k, v> newhash;newhash._tables = new kv&lT k,v>[newcapacity];newhash._status = new status[newcapacity];newhash._capacity=_primelist[i]; for (size_t j = 0; j < newcapacity; ++j) {if (_status&& J<_capacity&&_status[j] == exist) {//newhash.insert (_tables[j]._key, _tables[j]._ Value)/////the insertion function Newhash.insert2 (_tables[j]._key, _tables[j]._value) of the one-time detection of an increase in the volume of a probe;//Two probes ... }else{newhash._status[j] = empty;newhash._tables[j]._key = 0;newhash._tables[j]._value = 0;}} Swap (_tables, newhash._tables); swap (newhash._status,_status); swap (newhash._capacity, _capacity); Return true;}} return false;}}; #pragma once#include<iostream>//hash bucket #include<vector> #include <math.h> #include <string >using namespace std;template <class K>class GetKey{public:size_t Operator () (Const k& key) {return key;}}; Static size_t bkdrhash (consT&NBSP;CHAR&NBSP;*&NBSP;STR) {unsigned int seed = 131; // 31 131 1313 13131 131313unsigned int hash = 0;while (*STR) {hash = hash * seed + (*str++);} return (HASH&NBSP;&&NBSP;0X7FFFFFFF);} Template <>class getkey<string>{public:size_t operator () (const string & key) {Return bkdrhash (Key.c_str ());}}; Template <class k,class v> //key/value form Structure Body struct keyvalue{k _key; v _value; keyvalue<k, v>* _next; KeyValue (Const k& key = k (), const v& value = v ()): _key ( Key), _value (value), _next (NULL) {}};template <class k, class v>class Hashbucket{public:vector<keyvalue<k, v>*> _tables;size_t _size;public:hashbucket (): _size (0) {}void insert (Const k& key , const v& value ) {if (_tables.capacity () ==0| | _size * 10 / _tables.capacity () > 7) {if (!_increasecapacity ()) cout< < "Linear detection hash list is full" <<ENDL;} Size_t i = _hashfanction1 (key); keyvalue<k, v>* cur = _tables[i];if (cur == null) _tables[i] = new KeyValue<K, V> (key, value);else{while (cur) {if (cur->_key == key) return;else if (Cur->_next == null) Break;elsecur = cur->_next;} Cur->_next = new keyvalue<k, v> (Key, value);} ++_size;} Int find (Const k& key) {size_t i = _hashfanction1 (key); keyvalue<k, v>* cur = _tables[i];while (cur) {if (cur->_key == key) Return i;cur = cur->_next;} Return -1;} Void remove (const k& key) {Size_t i = _hashfanction1 (key); keyvalue<k, v> * cur =_tables[i]; keyvalue<k, v> * prev = null;while (cur) {if (cur->_key == key) {if (prev) {prev->_next = cur->_next;} Else_tables[i] = null;delete cur;return;} Prev = cur;cur = cur->_next;}} Void print () {size_t i = 0;for (; i < _tables.capacity (); ++i) { keyvalue<k, v>* cur = _tables[i];if (cur == null)//printf ("[%d]:K= %d V= %d -> ", i, -1,-1);cout<< " [" << i < < "] " << -1 << " " << -1 << " << ;while (cur) {//printf ("[%d]:k= %d v= %d ", i, cur->_key,cur->_value);cout << "[" << i << "] " << _tables[i]->_key << " " << _tables[i]->_value << " " << ";cout << "; Cur = cur->_next;} cout << " null" <<endl;}} Protected:size_t _getkey (Const k& key) {return getkey<k> () (key);} Size_t _hashfanction1 (Const k& key)//One-time detection (linear detection) {Size_t k = _getkey (key ); Return k%_tables.capacity ();} Bool _increasecapacity () {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};for (int i = 0; i < _primesize; ++i) {if (_primelist[i]>_tables.size ()) {size_t Newcapacity = _primelist[i];vector<keyvalue<k, v>*> newtables;newtables.resize (newcapacity, 0);for (Size_t j = 0; j < _tables.capacity (); ++ j) {keyvalue<k, v>* cur = _tables[j];while (cur) {size_t k = _ HashFanction1 (Cur->_key); keyvalue<k, v>* tmp = cur; keyvalue<k, v>* tmp2 = newtables[k];if (tmp2 = null) {newtables[i] = cur;break;} while (tmp2->_next) {tmp2->_next = cur;} tmp->_next = cur;cur->_next = null; _tables[j] = tmp; cur = tmp;}} Swap (newtables, _tables);} Return true;} Return false;}}; #include "hashtable.hpp" //test Case # include "HASHBUCKET.HPP" Using namespace std;void test () { hashtable<int, int> h1;for (Int i = 0; i <30;++i) H1. Insert (i, i); H1. Insert (100,1); H1. Insert (170, 3); H1. Insert (223, 5); H1. Insert (56, 7); H1. Insert (550,9); H1.print (); cout<
C + + hash table linear detection two-time probe hash bucket