Implementation of the hash bucket (zipper method)

Source: Internet
Author: User
Tags prev
Brief Introduction

In order to solve the phenomenon that data heap is gathered after the hash collision occurs in the linear detection implementation hash table, we have taken another structure to deal with hash conflicts, hash buckets (zipper method), and the method of zipper resolution is to store all hash addresses that are computed from the hash function in a single linked list.
The principle of implementation is to define a hash table as an array of pointers consisting of an n-head pointer, with the same data that is computed by the hash function, all of which are attached under the header pointer. It inherits the features of an array of easy lookups and linked lists to facilitate deletion of inserts.

Complexity of Time

Zipper method Because of the addition of the linked list, so the time complexity in the search for deletion is also degraded to O (n), but if the hash function given reasonable, and the hash table capacity is sufficient, the time complexity of the zipper method is certainly far less than O (n). advantages and disadvantages of zipper method

Advantages :
The clustering phenomenon of hash table is solved, and the average lookup length is reduced. Deleting nodes is easier to implement than linear probes, and it is only necessary to find the corresponding nodes and then delete them, which cannot be done in the open address method because the nodes in the hash table that empty the array will cause subsequent data to be inaccessible.

defects :
When the amount of data is small, the open address method does not need to open up space, if the relative to the zipper method to save the space used to expand the hash table can be the loading factor (node number and the ratio of the meter), which reduces the open address law conflict, improve the average lookup speed. Code Implementation

Code in the overall framework of the design and linear detection is basically consistent, but in the insertion of the deletion of the operation of the difference, instead of using the change state to express the deletion, but to use the list of delete and insert operations. Because of the combination of the linked list and vector, the _status (state) is deleted from the node design, adding a pointer to the next node _next.

Template<class k,class v> struct Hashtablebucketnode {K _key;

    V _value;

    Hashtablebucketnode<k, v>* _next; 
    Hashtablebucketnode (const k& key, const v& value): _key (Key), _value (value), _next (NULL)

{}
};
    Template<class k> struct _hashfunc {//The imitation function used to compute the key size_t operator () (const k& key) {return key;

}
}; template<> struct _hashfunc<string> {//Special string version static size_t Bkdrhash (const CHAR*STR) {UN
        Signed int seed = 131;//131 1313 13131 131313 unsigned int hash = 0;
        while (*STR) {hash = Hash*seed + (*str++);
    Return (hash & 0x7fffffff);
    } size_t operator () (const string& key) {return Bkdrhash (Key.c_str ());

}
};
const int _primesize = 28; static const unsigned long _primelist[_primesize] = {//Prime number table 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}; Template<class k,class V,class hashfunc = _hashfunc<k>> class Hashtablebucket {typedef HashTableBucketN
Ode<k,v> Node;
        Public:hashtablebucket (size_t size): _size (0) {size_t newsize = getprime (size);
    _v.resize (newsize);
        } size_t Hashfunc (const k& key) {//hash function Hashfunc HS;
        size_t hash = HS (key);
    Return hash% _v.size ();
        pair<node*, bool> Insert (const k& key, const v& value) {//Insert checkcapacity ();

        size_t index = Hashfunc (key);
            if (!_v[index]) {//header pointer is empty node* NewNode = new Node (key, value);
            _v[index] = NewNode;
            ++_size; Return Make_pair (NewNode, tRue);
        ///Find insertion position in the list node* prev = NULL;
        node* cur = _v[index];

            while (cur) {if (Cur->_key = = key) return Make_pair (cur, false);
            prev = cur;
        Cur = cur->_next;
        node* newNode =new Node (key, value);
        Prev->_next = NewNode;
        ++_size;
    Return Make_pair (NewNode, true);

    //node* Find (const k& key)//{//lookup of the first implementation, directly by location Lookup//size_t index = Hashfunc (key);
    node* cur = _v[index];

    while (cur)//{//if (Cur->_key = key)//return cur;
    Cur = cur->_next;
    }//return NULL;
            } node* Find (const k& key) {//Traversal lookup for (size_t index = 0; index < _size; ++index) {
            node* cur = _v[index];

                while (cur) {if (Cur->_key = key) return cur; Cur = cur->_next;
    } return NULL;

        BOOL Remove (const k& key) {//delete size_t index = Hashfunc (key);
        node* prev = NULL;

        node* cur = _v[index];
                    while (cur) {if (Cur->_key = = key) {if (prev = NULL)   
                _v[index] = NULL;

                else Prev->_next = cur->_next;
                Delete cur;
            return true;
    return false;
        protected:size_t getprime (size_t size) {//Get prime number of primes for (size_t i = 0; i < _primesize; ++i)
            {if (_primelist[i] > size) {return _primelist[i];
    } return _primelist[_primesize-1];
        } void Swap (hashtablebucket<k, v> tmp) {//Exchange Std::swap (_v, Tmp._v);
    Std::swap (_size, tmp._size);
} void Checkcapacity ()    {//capacity detection if (_size = = _v.size ()) {hashtablebucket<k, v> tmp (_v.size ());
                for (size_t index = 0; index < _size; ++_size) {node* cur = _v[index];
                    while (cur) {node* cur = _v[index]; while (cur) {tmp.
                        Insert (Cur->_key, Cur->_value);
                    Cur = cur->_next;
        }} Swap (TMP);
    }} private:vector<node*> _v;
size_t _size; };

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.