HashMap的C++實現

來源:互聯網
上載者:User

標籤:

#include <iostream>#include <cstring>#include <string>typedef  unsigned int SIZE_T;using namespace std;/** HashMap模板的C++實現,用拉鏈法解決衝突*注意:需要為每一種KeyType兩個仿函數:HashFunc and EqualKey*/ template<typename KeyType, typename ValueType, typename HashFunc, typename EqualKey>class HashMap{#define DEFAULT_CAPACITY  43 // hash表初始化容量#define LOADFACTOR 0.7   //負載因子    class KeyNode{ //存放key value對    public:        KeyType key;        ValueType value;        KeyNode *next;        KeyNode(){}        KeyNode(const KeyNode & rhs){            Assign(rhs);        }        const KeyNode & operator =(const KeyNode & rhs){            Assign(rhs);            return *this;        }        void Assign(const KeyNode & rhs){            this->key = rhs.key;            this->value = rhs.value;            this->next = rhs.next;        }    };public:    HashMap(){        table = new KeyNode* [DEFAULT_CAPACITY];        ::memset(table,0,DEFAULT_CAPACITY * sizeof(KeyNode*));        size  =0;        capacity = DEFAULT_CAPACITY;        hash = HashFunc();        equal = EqualKey();    }    bool put(const KeyType & key, const ValueType & value){        SIZE_T index = hash(key) % capacity;        if(table[index] == NULL){            KeyNode *temp = new KeyNode();            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;  //重複                pre = pre->next;            }            if(equal(pre->key, key)) return false;  //重複            KeyNode *temp = new KeyNode();            temp->key = key;            temp->value = value;            temp->next =NULL;            pre->next = temp;        }        size ++;        if(size*1.0/capacity > LOADFACTOR) this->rehash();        return true;    }    bool remove(const KeyType & 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;            delete temp;            size --;            return true;        }        while(temp->next != NULL){            if(equal(temp->next->key, key)){                KeyNode * del = temp->next;                temp->next = del->next;                delete del;                size --;                return true;            }else{                temp = temp->next;            }        }        return false;    }    bool exist(const KeyType & 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[](const KeyType & key){        if(!exist(key))    put(key,ValueType());        return get(key);    }    SIZE_T Size(){        return size;    }    void display(){        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(const KeyType key){        SIZE_T index = hash(key) % capacity;        if(table[index] == NULL) return ERROE.value;        KeyNode *temp = table[index];        while(temp!= NULL){            if(equal(temp->key,key)) return temp->value;            temp = temp->next;        }        return ERROE.value;    }    //未實現    void rehash(){         //得到一個兩倍左右大小的capacity(最好為素數),重新散列            }    void destroy(KeyNode ** hashtable){        for(SIZE_T i=0;i<this->capacity;i++){            KeyNode * temp = hashtable[i];            while(temp != NULL){                KeyNode *del = temp;                 temp = temp->next;                 delete del;            }        }        delete []hashtable;    }};class HashFuncInt{public:    SIZE_T operator()(  int key)const{        return key;    }};class EqualFuncInt{public:    bool operator()(int keyA, int keyB)const{        return keyA == keyB;    }}; int main(){   const int scale = 100;    //記憶體泄露效能測試    while(false){        HashMap<int,int,HashFuncInt,EqualFuncInt> hm;        for(int i=1;i<scale;i++){            hm.put(i,i*(i+1));        }        hm.display();        for(int i=1;i<scale;i+=2)            hm.remove(i);        hm.display();        for(int i=0;i<scale;i+=3)            hm[i]=i*3;        hm.display();    }    //模板實用性測試         return 0;}

 參考文獻:

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

HashMap的C++實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.