




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、 按照hashmap的基本原理用C實(shí)現(xiàn)了簡(jiǎn)單的基本功能,復(fù)雜的實(shí)現(xiàn)參考C 庫(kù)的源碼。 /* * HashMap.h * 2012-7-25 * Author: luxiaoxun */ #ifndef HASHMAP_H #define HASHMAP_H #include iostream using namespace std; /List all the integer number no less than 57 total number is 28 /And each number is about half of its next number static int prime28
2、 = 57, 97, 1543, 193, 3079, 389, 6151, 12289, 769, 24593, 98317, 3145739, 6291469, 49157, 1572869, 50331653, 100663319, 196613, 393241, 12582917, 786433, 25165843, 201326611, 402653189, 805306457, 1610612741 ; class HashMapUtil public: static int find_NextPrimeNumber( int current ) /Find the next pr
3、ime number by searching in the prime number list int i = 0; for( ; i 28 ; i+ ) if ( current primei ) break; return primei; /return the next larger prime. ; template class Key, class Value, class HashFunc, class EqualKey class Hash_Map private: template class _Key, class _Value class KeyNode /Store t
4、he value /Store the keyword public: _Value value; _Key key; int used; of Value/Key is your own class, make sure they can handle copy /if the type constructor and operator = KeyNode():used(0) KeyNode(const KeyNode kn) value = kn.value; key = kn.key; used = kn.used; KeyNode operator=(const KeyNode kn)
5、 if(this = kn) return *this; value = kn.value; key = kn.key; used = kn.used; return *this; ; public: Hash_Map(); void insert( const Key hashKey , const Value value ); void remove( const Key hashKey ); void clear(); /use it when rehashing Value find( const Key hashKey ); Value operator ( const Key ha
6、shKey ); Hash_Map(); private: HashFunc* hf; EqualKey* ek; HashMapUtil* hml; KeyNode Key ,Value *table; int size;/current number of itmesint capacity; /capacity of the array const static double loadingFactor = 0.8; /find the index of a key int findKey( const Key hashKey ); ; templateclass Key , class
7、 Value , class HashFunc , class EqualKey Hash_MapKey, Value, HashFunc, EqualKey:Hash_Map() /initialize the capacity with hf = new HashFunc; ek = new EqualKey; hml = new HashMapUtil; capacity = hml-find_NextPrimeNumber(0); first primer 57 /resize the table with capacity because an extra one is used /
8、to return the NULL type of Value in the function find table = new KeyNodeKey , Valuecapacity+1; for( int i = 0 ; i capacity ; i+ )/initialize the table tablei.used = 0; size = 0; templateclass Key, class Value, class HashFunc, class EqualKey Hash_MapKey, Value, HashFunc, EqualKey:Hash_Map() delete t
9、able; delete hf; delete ek; delete hml; templateclass Key, class Value, class HashFunc, class EqualKey Value Hash_MapKey, Value, HashFunc, EqualKey:find( const Key hashKey ) int index = findKey( hashKey ); if ( index 0 ) /if index 0 ,not found,else return the index coutcan not find the key!endl; ret
10、urn tablecapacity.value; /return NULL else return tableindex.value; templateclass Key, class Value, class HashFunc, class EqualKey void Hash_MapKey, Value, HashFunc, EqualKey:insert( const Key hashKey, const Value value ) int hash_value = ( hf-cal_hash( hashKey ) ) % capacity; while( tablehash_value
11、.used =1 )/search an empty place,and then insert into it if ( hash_value = capacity - 1 ) hash_value = 0; else hash_value+; /modify the KeyNode tablehash_value.used = 1; tablehash_value.key = hashKey; tablehash_value.value = value; size+; /if the tables size is too large ,then rehash it if ( size =
12、capacity * loadingFactor ) clear(); templateclass Key, class Value, class HashFunc, class EqualKey void Hash_MapKey, Value, HashFunc, EqualKey:clear() int pastsize = capacity; /create a new array to copy the information in the old table capacity = hml-find_NextPrimeNumber( capacity ); KeyNodeKey,Val
13、ue* tmp = new KeyNodeKey,Valuecapacity; /copy the KeyNode into the tmp array for( int i = 0 ; i pastsize ; i+ ) if ( tablei.used = 1 ) tmpi = tablei; delete table; /release the memory of the old table /resize the table table = new KeyNodeKey,Valuecapacity+1; for( int i = 0 ; i capacity ; i+ ) /initi
14、alize the table tablei.used = 0; for( int i = 0 ; i pastsize ; i+) /insert the item into the table one by one if ( tmpi.used = 1 ) insert( tmpi.key, tmpi.value ); delete tmp;/delete the tmp array templateclass Key, class Value, class HashFunc, class EqualKey void Hash_MapKey, Value, HashFunc, EqualK
15、ey:remove( const Key hashKey ) int index = findKey( hashKey ); /find the index of the key if ( index 0 ) /if find modify the flag with 0,else print out no such key! coutNo such Key!endl; else tableindex.used = 0; size-; templateclass Key, class Value, class HashFunc, class EqualKey Value Hash_MapKey
16、, Value, HashFunc, EqualKey:operator(const Key hashKey) return find( hashKey ); /overload the operation to return the value of the element const Key templateclass Key, class Value, class HashFunc, class EqualKey int Hash_MapKey, Value, HashFunc, EqualKey:findKey( hashKey ) int index = ( hf-cal_hash(
17、 hashKey ) ) % capacity; /if the used flag equals 1 and the key equals to hashkey, find it for(int i = 0 ; (tableindex.used = 1) !(ek-Check_equal(tableindex.key,hashKey) i = capacity ; i+ ) if ( index = capacity - 1 ) /if index=capacity-1,return to the head of the array index = 0; else index+; if (
18、(tableindex.used | !(ek-Check_equal( tableindex.key,hashKey ) ) return -1; else return index; != 1) #endif /* HASHMAP_H_ */ C 編譯器不支持模板頭文件和實(shí)現(xiàn)代碼分離的編譯, 如果的類實(shí)現(xiàn)和類聲明分別放 在 cpp 文件和 h 頭文件里,那么在測(cè)試代碼里 include 要加上實(shí)現(xiàn)的代碼,比如加上 #includeHashMap.cpp 。 使用hashmap,需要自己提供針對(duì) key的hash仿函數(shù)類和equal仿函數(shù)類,測(cè)試代碼: /* * main.cpp * 201
19、2-7-25 * Author: luxiaoxun */ #include HashMap.h #include string #include iostream using namespace std; /Hash function you provided must be correspond to the type of the Key class HashFunc public: int cal_hash(const string key ) unsigned int hash = 0; for(int i = 0; i key.length(); +i) / equivalent to: hash = 65599*hash + (*str+); hash = keyi + (hash 6) + (hash 16) - hash; return (hash 0 x7FFFFFFF); ;
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年中國(guó)低壓角式調(diào)節(jié)閥行業(yè)投資前景及策略咨詢研究報(bào)告
- 2025年中國(guó)丁腈縫制手套行業(yè)投資前景及策略咨詢研究報(bào)告
- 德潤(rùn)高中分班考試題及答案
- 大學(xué)生機(jī)電考試題及答案
- 初三下冊(cè)考試題及答案
- 赤峰市輔警招聘考試題及答案
- 出版發(fā)行行業(yè)大數(shù)據(jù)商業(yè)應(yīng)用淺析
- 2025年金融機(jī)構(gòu)風(fēng)險(xiǎn)管理數(shù)字化轉(zhuǎn)型與風(fēng)險(xiǎn)管理團(tuán)隊(duì)優(yōu)化報(bào)告
- 2025年金融風(fēng)險(xiǎn)管理:信用保險(xiǎn)在供應(yīng)鏈金融中的應(yīng)用策略報(bào)告
- 2025至2030谷物烘干機(jī)行業(yè)發(fā)展趨勢(shì)分析與未來(lái)投資戰(zhàn)略咨詢研究報(bào)告
- 社區(qū)矯正人員日常行為規(guī)范
- 農(nóng)村自建房業(yè)主培訓(xùn)課件
- 財(cái)產(chǎn)申報(bào)表-被執(zhí)行人用
- 一例肝硬化患者的護(hù)理查房課件
- 2025-2030中國(guó)光伏建筑一體化(BIPV)市場(chǎng)規(guī)模預(yù)測(cè)與競(jìng)爭(zhēng)格局分析研究報(bào)告
- 《2025年普通高校在陜招生計(jì)劃》
- 2025年廣西壯族自治區(qū)三支一扶考試真題
- 宿舍管理員述職報(bào)告
- 2025年徐州市專業(yè)技術(shù)人員公需課程 - 心理調(diào)適
- 企業(yè)內(nèi)部保密工作流程制度
- 《云南教育強(qiáng)省建設(shè)規(guī)劃綱要(2024-2035年)》解讀培訓(xùn)
評(píng)論
0/150
提交評(píng)論