C + + implementation of HashMap

Source: Internet
Author: User
Tags rehash

#include <iostream>#include<cstring>#include<string>typedef unsignedintsize_t;using namespacestd;/** C + + implementation of HashMap template, using zipper method to resolve conflicts * Note: Requires two functor for each keytype: Hashfunc and Equalkey*/Template<typename KeyType, TypeName ValueType, TypeName Hashfunc, TypeName equalkey>classhashmap{#defineDefault_capacity 43//hash Table initialization capacity#defineLoadfactor 0.7//load Factor    classkeynode{//Store key value on     Public: KeyType key;        ValueType value; Keynode*Next; Keynode () {} keynode (ConstKeynode &RHS)        {Assign (RHS); }        ConstKeynode &operator=(ConstKeynode &RHS)            {Assign (RHS); return* This; }        voidAssign (ConstKeynode &RHS) {             This->key =Rhs.key;  This->value =Rhs.value;  This->next =Rhs.next; }    }; Public: HashMap () {table=Newkeynode*[default_capacity]; :: memset (table,0, Default_capacity *sizeof(keynode*)); Size=0; Capacity=default_capacity; Hash=Hashfunc (); Equal=Equalkey (); }    BOOLPutConstKeyType & Key,ConstValueType &value) {size_t index= hash (key)%capacity; if(Table[index] = =NULL) {Keynode*temp =NewKeynode (); Temp->key =key; Temp->value =value; Temp->next =NULL; Table[index]=temp; }Else{Keynode* pre=Table[index];  while(Pre->next! =NULL) {                if(Equal (Pre->key, key))return false;//RepeatPre = Pre->Next; }            if(Equal (Pre->key, key))return false;//RepeatKeynode *temp =NewKeynode (); Temp->key =key; Temp->value =value; Temp->next =NULL; Pre->next =temp; } size++; if(size*1.0/capacity > Loadfactor) This-rehash (); return true; }    BOOLRemoveConstKeyType &key) {size_t index= hash (key)%capacity; if(Table[index] = = NULL)return false; Keynode*temp =Table[index]; if(Equal (temp->Key,key)) {Table[index]= temp->Next; Deletetemp; Size--; return true; }         while(Temp->next! =NULL) {            if(Equal (temp->next->key, Key)) {Keynode* del = temp->Next; Temp->next = del->Next; Deletedel; Size--; return true; }Else{Temp= temp->Next; }        }        return false; }    BOOLExistConstKeyType & Key)Const{size_t index= hash (key)%capacity; if(Table[index] = = NULL)return false; Keynode*temp =Table[index];  while(temp!=NULL) {            if(Equal (Temp->key,key))return true; Temp= temp->Next; }        return false; }    /*Keynode Find (const KeyType & key) {size_t index = hash (key)% capacity;        if (table[index] = = NULL) return null;        Keynode *temp = Table[index];            while (temp!= NULL) {if (equal (Temp->key,key)) return *temp;        temp = temp->next;    } return NULL; }    */ValueType&operator[](ConstKeyType &key) {        if(!exist (key)) put (Key,valuetype ()); return Get(key); } size_t SIZE () {returnsize; }    voiddisplay () {cout<<"Size:"<<size<<Endl;  for(size_t i=0;i< This->capacity;i++) {Keynode* Temp =Table[i];  while(Temp! =NULL) {cout<<"("<<temp->key<<","<<temp->value<<") "; Temp= temp->Next; }            if(table[i]!=null) cout <<Endl; }    }    ~HashMap () { This-destroy (table); }Private: Keynode**table;    size_t capacity;    size_t size;    Hashfunc Hash;     Equalkey equal;    Keynode Erroe; ValueType&Get(ConstKeyType Key) {size_t index= hash (key)%capacity; if(Table[index] = = NULL)returnErroe.value; Keynode*temp =Table[index];  while(temp!=NULL) {            if(Equal (Temp->key,key))returnTemp->value; Temp= temp->Next; }        returnErroe.value; }    //not implemented    voidrehash () {//get a capacity of about twice times the size (preferably prime number), re-hash    }    voidDestroy (Keynode * *Hashtable) {         for(size_t i=0;i< This->capacity;i++) {Keynode* Temp =Hashtable[i];  while(Temp! =NULL) {Keynode*del =temp; Temp= temp->Next; Deletedel; }        }        Delete[]hashtable; }};classhashfuncint{ Public: size_toperator()(intKeyConst{        returnkey; }};classequalfuncint{ Public:    BOOL operator()(intKeya,intKeyB)Const{        returnKeya = =KeyB; }}; intMain () {Const intScale = -; //memory leak performance test     while(false) {HashMap<int,int,hashfuncint,equalfuncint>HM;  for(intI=1; i<scale;i++) {hm.put (i,i* (i+1));        } hm.display ();  for(intI=1; i<scale;i+=2) Hm.remove (i);        Hm.display ();  for(intI=0; i<scale;i+=3) Hm[i]=i*3;    Hm.display (); }    //Template Usability Test         return 0;}

Reference documents:

    • http://blog.csdn.net/anialy/article/details/7620469
    • http://blog.csdn.net/luxiaoxun/article/details/7786073

C + + implementation of HashMap

Related Article

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.