Data Structure---Hash table (KV mode) (except remainder method)

Source: Internet
Author: User

Enables quick location lookup of data

thought one : Open an array of an appropriate size, speaking of the number of _capacity that need to be deposited in the array as his location, if the position is accounted for, then store the data in his next position (will not find the empty location, which is said below).

thought two: stored in the array is a struct, the struct contains an index value key, the value of the storage values, and a storage state (enumeration type, exist,empty,delete) can use these three states to judge and lazy deletion method (without purging the data, Set to delete state).

thought three: open up the problem of space, the initial space through the data query, open a size of 53 (prime) size space, load factor is about 0.7 when the capacity expansion, then need to re-storage, because the space has changed so the position has changed.

* Load factor : The space is used divided by the total space size.

#include   <iostream> #include <vector>using namespace std;enum signtable{ exist,empty,delete};template<class t,class k>struct package{t _myvalue; k _key; signtable _sign;}; Template <class t, class k> class hashtable{public:hashtable (): _capacity (0 ), _size (0) {_newcapacity ();} Void insert (t t,k k) {double j =  (double) _size /  (double) _capacity;  if  (j>= 0.7) {_newcapacity ();} Size_t key = _returnkey (k);while  (_hashtable[key]._sign == exist) {key++;if  (key > _capacity - 1) {key = 0;}} _hashtable[key]._myvalue = t;_hashtable[key]._key = k;_hashtable[key]._sign =  exist;_size++;} Bool find (k k, t t) {Size_ flag = 0;size_t key = _returnkey (K );while  (_hashtable[key]._key != empty) {If  (_hashtable[key]._myvalue == t) {return  true;} key++;if  (key > _capacity - 1) {key = 0;flag++;} if  (flag > 1) {break;}} Return false;} Bool remove (k k,t t) {Size_t flag = 0;size_t key = _returnkey (K); while  (_hashtable[key]._key != empty) {if  (_hashtable[key]._myvalue == t& &_hashtable[key]._sign==exist) {_hashtable[key]._sign = delete;return  true;} key++;if  (key > _capacity - 1) {key = 0;flag ++ ;} if  (flag > 1) {break;} Return false;}} Protected:void _newcapacity () {if  (_capacity == 0) {_hashtable.resize); _capacity =  10;size_t i = 0;for  (i = 0; i < _capacity; i++) {_ hashtable[i]._myvalue = 0;_hashtable[i]._key = 0;_hashtable[i]._sign = EMPTY;}} Else{hashtable<t,k> newtable;newtable._hashtable.resize (_capacity * 2); newTable._capacity  = _capacity*2;for  (size_t i = 0; i < newtable._capacity;  i++) {Newtable._hashtable[i]._myvalue = 0;newtable._hashtable[i]._key = 0;newtable._ Hashtable[i]._sign = empty;} size_t k = 0;while  (k<_capacity) {if  (_hashtable[k]._sign != empty) { Newtable.insert (_hashtable[k]._myvalue, _hashtable[k]._key);} k++;} newtable._size = _size;;;;; *this = newtable;}} Size_t _returnkey (k k) {return k%_capacity;} private:vector<package<t, k>> _hashtable;size_t _capacity;size_t _size;}; Void test1 () {Hashtable<int, int> hash;hash. Insert (2, 2); hash. Insert (3, 2); hash. Insert (4, 2); hash. Insert (4, 2); hash. Insert (3, 2); hash. Insert (4, 2); hash. Insert (4, 2); hash.Insert (2, 2); hash. Insert (3, 2); hash. Insert (4, 2); hash. Insert (4, 2); Cout << hash. Remove (4, 2)  << endl;cout << hash. Find (4,&NBSP;2)  << endl;} Int main () {Test1 (); return 0;}


This article is from the "Traces" blog, be sure to keep this source http://wpfbcr.blog.51cto.com/10696766/1759937

Data Structure---Hash table (KV mode) (except remainder method)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.