通俗的說,程序是能夠完成既定目標的具有特定邏輯組織形式的指令集序列。既然有現實的需求,那麼我們知道外界環境必然會給予程序某些特定形式的“輸入”,然而在機器的內部,這種“輸入”將轉換爲數據的形式,繼而這就要求我們爲用以描述現實世界需求的數據建立一個結構化的模型,使其能夠被機器指令高效的處理。通常,對於數據的處理無外乎以下幾種:讀取/更新/刪除數據項,或者插入新項,其中除插入外其他幾種操作均要求對集合進行搜索。而結構化的模型可以通過數組、鏈表或者樹形結構等建立,不同的建模方式對於數據處理中的各種操作有不同的性能表現。由於本文是偏向介紹算法,所以對於數組、鏈表等數據結構只做簡單介紹,並將由這兩種結構引出本文的主題——散列,而對樹結構的介紹及對其進行操作的算法將會寫在後續文章中。一般來講,數據結構將直接影響對其處理的算法的選擇,在本文中的散列函數算法又會反過來影響散列表這種結構之於數據的存貯效率,可以說,數據結構與算法的關係就好比是一卵雙生。
我們先簡單看一下數組與鏈表這兩種數據結構存儲數據的方式:
圖1
由上圖,我們可以得出以下幾點:
- 通常總是會爲動態集合的字典操作預留一定的資源,並且通過額外的空間記錄現有元素集合的大小或是鏈表的表頭/尾,因此對於插入操作來說其運行時間爲O(1)。
- 因爲讀取/更新操作均建立在搜索的基礎上,因此運行效率取決於定位待操作的數據項的時間。我們首先來看數組,如果所存儲的元素集是無序的,那麼搜索集合中的任一元素的期望時間E=(1+2+…+n)/n=O(n),因此搜索某個數據項幾乎總是需要遍歷整個集合。如果需要大量的搜索操作,那麼對於現有集合可以通過排序執行適當的預處理,並在此之後調用運行時間爲O(㏒n)的二分搜索以獲得較好的整體性能。但這幾乎總是受到外界環境不穩定因素的限制,例如動態插入操作將導致現有結構的序性質遭到破壞,並且這種預處理將會消耗O(n)或是O(n㏒n)的運行時間。再來看鏈表結構,因爲其無法提供隨機訪問能力,因此這決定了即使對鏈表進行排序也無法運用二分搜索快速查找具有某個固定值的元素,並且同未排序的普通數組一樣,鏈表的搜索操作的期望時間也爲O(n)。
- 對於數據的刪除操作,我們發現在有序數組中還需要將已刪除數據項所佔位置之後的元素逐個前移,使得在維持序關係時整個數組仍然保持緊湊,而在鏈表中,雖然只需改變待刪除數據項的前驅的指向,但高效利用空間的前提條件將會要求我們能夠維護一個空閒鏈表(freelist),這些都需要額外的時間開銷。
- 最後我們知道這兩種結構對存儲空間的利用率都很高,誰說這不是一個優點呢,至少對於散列來說是的。
需要注意的兩點是,在實際情況中還可以運用循環數組/鏈表,雙向鏈表等變形,數據結構的合理選擇總是需要考慮特定的應用需求和外部環境。
什麼是散列
在解釋散列的含義之前,我們先來深入的觀察一下普通數組這種數據結構,由圖1所示,將集合{a,b,c,d,e,f}存儲在數組中時佔據了索引爲0~5的6個存儲空間,我們可以形式化的表示爲有序對(An ordered pair)的集合,即{(a,0),(b,1),(c,2),(d,3),(e,4),(f,5)}。但其實這個集合中任意一個有序對的元素之間的關係是未定義的(undefined),因此這樣的有序對集合可以有6!個。然而我們是否可以構造一個關係,使得待存儲的集合{a,b,c,d,e,f}中的任意一個元素通過這個關係都能找到一個指定的索引值,由下圖更形象的表示:
圖2
根據上圖我們知道原有集合中的元素被散列之後所得到的有序對的集合爲{(d,0),(b,2),(c,2),(a,3),(f,4),(e,5)}。其中,散列表中的1號槽爲空,表示經過散列函數作用之後,原集合中沒有任何元素被散列至該位置,而2號槽中則存在兩個元素,將兩個不同元素散列至相同位置的情形我們稱爲碰撞(Collision)。如上圖所示,我們通過使用鏈表將不同元素鏈接起來解決碰撞,在後文中還將介紹另外一種稱爲開放尋址(Open
addressing)的解決方法。這裏更具體的說明一下鏈接法的鏈接形式:因爲散列至同一個槽的元素並無順序上的先後要求,因此爲效率計,我們將總是採用在鏈表頭插入碰撞元素的做法,最終導致的結果是,在一個鏈表中,越靠近表頭的節點,在原數組中被散列的次序越靠後。綜上所述,可以對散列函數hash進行如下形式化定義:
設在大小爲N的集合中存在元素elem,存儲原集合中元素的散列表共有m個槽位,則有
hash: elem→γ∈{0,1,2,…,m-1}
散列分析
這裏主要是對鏈接散列進行分析,其中的問題分析與解決思路同樣適用於開放尋址。因爲通過設計某個散列函數hash我們定義了集合元素與散列表下標之間一一對應的關係,從而使得搜索操作只需在常量時間內即可完成,即假定存在某個待搜索的關鍵字key,那麼該關鍵字必定在槽hash(key)中。唯一需要關心的問題是,集合中的元素在被散列之後的分佈狀況如何?在最壞情況下,有可能選取的散列函數將所有元素均散列至同一個槽中,使得該散列表實際已退化爲鏈表,在這種情況下的搜索操作將花費O(n)的運行開銷。顯然,我們並非因爲散列具有這種最壞情況纔去使用它,而能在多數情況下避免這種“退化”,將要求我們更深入的思考以下兩個問題:
- 如何設計一個好的散列函數?
- 散列表的大小爲多少較好,是否正如圖2所示,使用與原集合大小相同的散列表?我們將通過量化證明。
首先針對第二個問題進行分析,即散列表的大小爲多少較好?
設所有元素之間均相互獨立,且每個元素被散列至每個槽的概率均相等 ------------> 假設①
並設原集合的尺寸爲n,散列表的大小爲m,裝載因子α=n/m -------------->假設②
最重要的是,我們並不考慮散列函數所消耗的計算開銷 ---------------->假設③
由假設①,元素被散列至每個槽的概率相等,再根據假設②中散列表的大小爲m,有P{elem→γ∈{0,1,2,…,m-1}}=1/m
設待查找的關鍵字爲k,因此存在兩種情況:Ⅰ、待查找關鍵字k不存在; Ⅱ、關鍵字k存在
然而其實情況Ⅰ的搜索開銷即爲情況Ⅱ的最壞情況運行時間,因爲關鍵字k不存在時將查找整個鏈表
以下是更詳細的分析:
------>Ⅰ、由假設①,並且根據假設②有裝載因子α=n/m,所以α表示每個槽的結點數。因爲當待查找的關鍵字k不存在時,我們必將搜索到該槽中的最後一個結點處,因此在這種情況下的搜索開銷爲O(α)。
------>Ⅱ、對於關鍵字k存在的情況,其實不做詳細分析我們就已經知道,這種情況下的運行時間必然不超過O(α),然而單單分析本身就是一件很有趣的事情,爲何不做得徹底一些?更重要的是,下面所用到的這種方法,其體現的思想對於分析隨機過程將具有極大的益處。
綜上可知,如果我們將散列表設定爲與原集合大小相同時,裝載因子α=1,此時的搜索時間爲O(1),但其實設置爲原集合尺寸的1/2或是2倍並沒有多大區別。需要注意的是雖然散列表越大導致搜索時間減少,但其所佔內存空間將會增大。
設計散列函數
如何設計一個好的散列函數?需要一定的數論功底才能回答,因此這一部分不會去詳細解釋爲什麼這樣做。儘管脫離了能夠對其解釋的理論,工程實踐將會變得“矮”很多截,但這裏所用的一些東西在實際生活中具有廣泛應用,爲了應用而去了解並積累他們仍是有趣的。此外,無論是字符或是數據在機器內部總是以數值的形式表現,所以總能設置一種機制將任何輸入轉換爲數值,例如對字符'a'進行散列,若將'a'解釋爲與其對應的ASCII碼97,則hash('a')=hash(97),只是當輸入變得複雜時這種設計也將變得更加困難,這裏我們將避開這些複雜性而直接假設所有的輸入均爲某一個數。
- 除餘散列法
設 hash(key)=key mod m,其中key表示被散列的關鍵字,而m則表示散列表的大小,mod則爲取餘操作。
這是一種比較簡單的散列函數,但簡單並不意味着高效。當待散列的元素之間存在某種模式時,這種散列法會有相當糟糕的性能表現。對該函數一個有用的指導原則是將m選取爲接近待散列集合大小的質數。
- 乘法散列法
設 hash(key)=floor(m×(A×key mod 1)),其中floor()表示對表達式進行下取整,A∈(0,1),m如上同樣表示散列表的大小,且在這種方法中對m並無任何特殊的要求。
[A×key mod 1]表示將key乘上某個在0~1之間的數並取乘積的小數部分,該表達式等價於A×key-floor(A×key)
這裏最重要的是A的值應該如何設定,Don•Knuth老大認爲A=(√5-1)/2 [黃金分割點]
比較好。
- 全域散列法(universal hashing)
設 hasha,b(key)=(a×key+b) mod m,如同除餘散列法中一樣,m的值應爲質數,而a∈{1,2,3,…,m-1},b∈{0,1,2,…,m-1}且a,b的值應在運行時動態確定。
全域散列的基本思想是給出hash函數的基本“骨架”,而其中的某些參數通過運行時在指定範圍內隨機選取確定,從而實際上形成了一個函數簇。根據上述a,b的取值範圍,我們知道這個函數簇中存在m×(m-1)個函數。因爲對於同一個輸入,每次執行時選取不同的參數將擁有不同的性能表現,因此設計的函數實際上獨立於任意被散列的關鍵字。只有當一個相對糟糕的輸入——即該輸入不構成隨機分佈,遇到一個選取的相對糟糕的散列函數時,纔將導致較差的性能,在多數情況下這類散列往往具有較好的運行時性能表現。
- 完全散列法(perfect hashing)
完全散列法與其說是一種函數設計方法,倒不如說是對碰撞(Collision)情況的另一種解決策略。具體來講,存在待查找的元素key,經過一次散列,該元素被存放在槽hash(key)處,然而其他元素也有可能被散列至該處,因此對散列至該槽的元素繼續進行散列,所得到的值hash'(hash(key))即爲元素key的確切存儲位置。其中外層hash過程稱爲一級散列,內層hash'過程稱爲二級散列。
這個策略會存在幾個問題,首先二級散列之後可能還會存在碰撞問題,是否需要進行三級散列甚至更多級的散列?其次,某個槽對應的二級散列函數應如何選取?最後,一級散列中某個槽所對應的二級散列表的尺寸應該如何設置?
這三個問題其實可以一併解決。首先對於是否需要更多級的散列,因爲外層的一級散列已經將原集合分爲一系列子集,若我們將一級散列表設置爲與原集合尺寸相近的大小,同時合理的選取一個散列函數,那麼在每個槽中發生碰撞的元素個數將在一個可控範圍內,此時如果爲了少部分元素再設置三級散列的話,不僅增加了整個結構的複雜性以及對於內存的要求,而且還要再次爲三級散列選取合理的散列函數,成本相對較大。接着來看解決二級散列函數的選取問題,我們發現在全域散列法中,只需設置不同的參數,即能生成特定於某個槽的散列函數,若生成的散列函數導致二級散列表中發生碰撞,那麼重新從全域散列函數簇中選取,直至不發生碰撞爲止即可。另外,只需合理設置二級散列表的大小,這種重新選取的概率將變得極低,一個可行的指導原則是將二級散列表的大小設置爲外層散列至該槽的元素個數的平方。
圖3
完全散列的分析更形象化的說明如圖3所示。最後要說明的一點是,這種通過兩次散列的方法雖然提供了高效的搜索,但代價是花費了更多的內存空間,同時因爲插入操作有可能導致在二級散列表中發生碰撞,因此這種方法只適用於靜態關鍵字集合中,“靜態”意指該集合一旦確定,便不再發生動態變化,即不發生插入或是刪除操作。
開放尋址(open addressing)
同鏈接法一樣,開放尋址是一種用於解決碰撞的策略。不同之處在於,在鏈接法中,每個關鍵字只能對應一個固定的槽,而在開放尋址中,每個關鍵字可以對應散列表中的多個槽,因爲有可能在首次尋址過程中,該槽已被先前的關鍵字所佔據,因而接下來我們根據事先所制定的某個規則繼續試探下一個槽是否可用,直至找到一個可用的槽並將關鍵字存儲在其中爲止。這裏存在的一個問題是,如果僅將待散列的關鍵字作爲函數的輸入,那麼最終必定只有與其對應的單個輸出,因此我們必須尋找一個誘導因子,使得在關鍵字不變的情況下誘使函數的輸出發生變化。因爲所有的關鍵字放在一個散列表中,因而將該誘導因子的值域設置成與散列表的大小相同,能夠更充分的利用整個散列表:
設原集合中存在元素elem,誘導因子爲i,散列表的大小爲m,則有
hash: (elem, i)→γ,其中 i,γ∈{0,1,2,…,m-1}
並且該函數所形成的探查序列爲 <hash(elem,0), hash(elem,1), …, hash(elem,m-1)>
這裏誘導因子i被形式化地定義爲區間[0,m)中的某個值,其取值序列被固定爲<0,1,…,m-1>只是爲了方便散列函數的實現,實際可根據原集合中的元素所存在的模式制定更具針對性的值域及取值序列,這裏我們將重點放在開放尋址中散列函數的設計上。通常在開放尋址中所用的散列函數主要由以下幾種:
- 線性試探法
設 hash(key, i)=(hash'(key)+i) mod m,其中 i=0,1,…,m-1,hash'爲輔助散列函數
在線性試探中,首次探查位置取決於hash'(key)的值,若發生碰撞,則接下來所探查的位置爲(hash'(key)+1) mod m,從而所形成的探查序列爲<hash'(key), hash'(key)+1,…, m-1, 0,…, hash'(key)-1>。因爲散列表的大小爲m,所以整個散列表總共提供m種不同的探查序列。並且由於線性試探使用連續探查的方式,因此隨着散列表中元素的增加,所佔用的槽逐漸呈連續分佈狀況,從而增加插入/搜索操作的期望時間。
- 二次試探法
設 hash(key,i)=(hash'(key)+c1i+c2i2) mod m,如上所述,i=0,1,…,m-1,hash'爲輔助散列函數,c1,c2爲輔助參數
可以發現,線性試探法實際是二次試探的特殊情況,若取參數c1=1,c2=0,那麼二次試探將“退化”爲線性試探。同樣的,整個探查序列也是由hash'(key)所決定的,因爲雖然探查序列中各元素之間的增量[c1i+c2i2]不再以線性的方式進行,但對於每個元素來說,引導因子i總是以步長1的方式遞增,這就導致所有元素的探查序列的增加方式是相同的,因此整個散列表同樣提供m種不同的探查序列。但隨着散列表中元素的增加,這種跳躍式的增量方式使得插入/搜索操作的運行時間受到的影響較小。
- 雙重試探法
設 hash(key,i)=(hash'(key)+i×hash''(key)) mod m,其中i=0,1,…,m-1,hash',hash''均爲輔助散列函數
雙重試探法的首個探查位置爲hash'(key),當產生碰撞之後,接下來的探查位置爲(hash'(key)+hash''(key)) mod m,因此我們發現在雙重試探法中,不僅初始探查位置依賴於關鍵字key,探查序列中的增量hash''(key)同樣依賴於關鍵字key,因而整個散列表提供了m2種不同的探查序列,較之於前兩種開放尋址具備了更多的靈活性。這裏還要注意的是應保證hash''(key)與m互質,因爲根據固定的偏移量所尋址的所有槽將形成一個羣,若最大公約數p=gcd(m, hash''(key))>1,那麼所能尋址的槽的個數爲m/p<m,使得對於一個關鍵字來說無法充分利用整個散列表。舉例來說,若初始探查位置爲1,偏移量爲3,整個散列表大小爲12,那麼所能尋址的槽爲<1,
4, 7, 10>,尋址個數爲12/gcd(12,3)=4。
最後要注意的一點是,因爲開放尋址將所有的關鍵字均存放在散列表中,因此我們必須保證散列表的大小始終大於原集合的元素規模,使得在任何時刻都能將元素存放在散列表中。但動態集合的插入操作有可能使得散列表被填滿,這種情況下如果還需進行插入操作,那麼我們就必須擴大散列表,由於散列表的大小m發生變化,考慮到m是散列函數中的參數,因此能保證m發生變化之後,同樣一個關鍵字能被散列至相同的槽中,所以存放在原先散列表中的關鍵字必須被重新散列,這就帶來了額外的開銷。因此我們首先應充分挖掘待存儲的數據的特點,比如作用在該集合上的操作是否在大多數情況下爲搜索,而動態的插入/刪除操作較少發生,或者是預留一個較大的散列表,能夠保證原集合必定不超過該散列表的大小,在這之後才考慮使用開放尋址。
以下是鏈接法[除餘散列,乘法散列,全域散列]和開放尋址[線性試探,二次試探,雙重試探]的性能測試,由於所使用的數據集爲隨機生成數,因而可以預知對於鏈接法中的三種散列方法來說,散列之後的元素分佈狀況並不會存在多大差別,但開放尋址中的三種散列方法仍具有測試價值,這裏我們一併進行測試:
-
#include <iostream>
-
#include <cmath>
-
#include <ctime>
-
#include <string>
-
#include <exception>
-
#include <Windows.h>
-
#include <engine.h>
-
using namespace std;
-
-
-
#pragma warning(disable:4244)
-
#pragma warning(disable:4996)
-
#pragma warning(disable:4290)
-
const size_t KEY_SCALE
= 600;
-
const size_t SLOTS_IN_CHAIN
= 45;
-
const size_t SLOTS_IN_OPENADDR
= KEY_SCALE;
-
const size_t COUNT_OF_EXECUTE
= 60;
-
-
class MatDraw{
-
public:
-
MatDraw(const mwSize&);
-
~MatDraw()
-
{
-
mxDestroyArray(paForXOrder);
-
mxDestroyArray(paForYOrder);
-
engClose(ep);
-
}
-
void mdPlot(double **, const size_t&);
-
void EvalStr(string
str)
-
{ engEvalString(ep, str.c_str()); }
-
private:
-
MatDraw(const MatDraw&);
-
MatDraw& operator=(const MatDraw&);
-
private:
-
Engine *ep;
-
mxArray *paForXOrder;
-
mxArray *paForYOrder;
-
size_t ArrLength;
-
};
-
-
class Hash{
-
public:
-
virtual void plot()
= 0;
-
virtual void RunAndGetCollision()
= 0;
-
Hash(const size_t&, const size_t&);
-
virtual ~Hash()
-
{ if(SrcSet) delete []
SrcSet; }
-
protected:
-
typedef size_t index;
-
void GeneRand(const size_t&);
-
size_t GeneFrom(size_t left, size_t right) const
-
{ return (rand()%(right-left+1))+left;
}
-
size_t getClosePrime() const;
-
void Exit() const {
system("pause"); exit(0); }
-
size_t *SrcSet;
-
size_t szSet;
-
size_t approSlot;
-
size_t nSlot;
-
protected:
-
-
-
index unihash(const size_t &key, const size_t &arg_a, const size_t &arg_b)
-
{
-
-
return (arg_a*key+arg_b)
% nSlot;
-
}
-
};
-
-
class chainHash
: public Hash{
-
private:
-
typedef index
(chainHash::*ChHsh)(const size_t&);
-
struct ChainNode{
-
size_t key;
-
ChainNode *next;
-
};
-
struct HashTable{
-
ChainNode *head;
-
size_t szCnt;
-
};
-
public:
-
typedef enum {
REMAIN, MULTI, UNIVER } HashFunc;
-
chainHash(const size_t&, const size_t&);
-
~chainHash();
-
void getCorresIndex(const HashFunc&);
-
void IterEveryChain(const HashFunc
&) const;
-
void InsertIntoHashTable(const HashFunc&);
-
void RunAndGetCollision();
-
void plot()
-
{
-
pmd->mdPlot(ColliMatrix,chainHash::NumOfHashF);
-
pmd->EvalStr(string("legend('remainder','multiplicate','univerhash');").c_str());
-
}
-
private:
-
chainHash(const chainHash&);
-
chainHash& operator=(const chainHash&);
-
-
index remainder(const size_t &key)
{ return key%nSlot; }
-
index multiplicate(const size_t &key)
-
{
-
-
float A
= (sqrt(5.0)-1)/2;
-
float temp
= A*key;
-
return (temp-static_cast<size_t>(temp))*nSlot;
-
}
-
index univerhash(const size_t &key)
-
{
-
-
return unihash(key,
a, b);
-
}
-
private:
-
-
size_t a,
b;
-
private:
-
HashTable *pht;
-
ChainNode *pcn;
-
static ChHsh
hfTable[];
-
static size_t NumOfHashF;
-
double **ColliMatrix;
-
class MatDraw
*pmd;
-
};
-
-
class openAddrHash
: public Hash {
-
private:
-
typedef index
(openAddrHash::*OAHashF)(const size_t&, const size_t&);
-
struct HashTable
{
-
size_t szCnt;
-
bool IsNotEmpty;
-
};
-
public:
-
typedef enum {
LINEAR, QUADRATIC, DBHASH} HashFunc;
-
openAddrHash(const size_t&, const size_t&);
-
~openAddrHash();
-
void plot()
-
{
-
pmd->EvalStr(string("figure;").c_str());
-
pmd->mdPlot(ColliMatrix, openAddrHash::NumOfHashF);
-
pmd->EvalStr(string("legend('linear','quadratic','doublehash');").c_str());
-
}
-
void RunAndGetCollision();
-
private:
-
-
-
index linearprob(const size_t &key, const size_t &ix)
-
{ return (unihash(key,
arg[0], arg[1])+ix)%nSlot; }
-
-
index quadprob(const size_t &key, const size_t &ix)
-
{ return (unihash(key,
arg[2], arg[3])+c1*ix+c2*ix*ix)%nSlot; }
-
-
index dbhashprob(const size_t &key, const size_t &ix)
-
{ return (unihash(key,
arg[4], arg[5])+ix*unihash(key, arg[6], arg[7]))%nSlot; }
-
void getCorresIndex(const HashFunc&) throw(exception);
-
void printInfo(const HashFunc&);
-
private:
-
-
-
-
-
-
#define ARG_IN_HASHFUNC 8
-
size_t arg[ARG_IN_HASHFUNC];
-
void GeneArg(const HashFunc&);
-
-
#define START 1
-
#define END 5
-
-
size_t c1,
c2;
-
private:
-
HashTable *pht;
-
double **ColliMatrix;
-
class MatDraw
*pmd;
-
static OAHashF
hfTable[];
-
static size_t NumOfHashF;
-
};
-
-
chainHash::ChHsh chainHash::hfTable[] = {&chainHash::remainder,
-
&chainHash::multiplicate,
-
&chainHash::univerhash};
-
size_t chainHash::NumOfHashF
= sizeof(chainHash::hfTable)/sizeof(chainHash::hfTable[0]);
-
-
openAddrHash::OAHashF openAddrHash::hfTable[] = {&openAddrHash::linearprob,
-
&openAddrHash::quadprob,
-
&openAddrHash::dbhashprob};
-
size_t openAddrHash::NumOfHashF
= sizeof(openAddrHash::hfTable)/
-
sizeof(openAddrHash::hfTable[0]);
-
-
int main(int argc, char *argv[])
-
{
-
srand(static_cast<unsigned>(time(NULL)));
-
Hash *ph = new chainHash(KEY_SCALE,SLOTS_IN_CHAIN);
-
ph->RunAndGetCollision();
-
ph->plot();
-
delete ph;
-
ph = new openAddrHash(KEY_SCALE,SLOTS_IN_OPENADDR);
-
ph->RunAndGetCollision();
-
ph->plot();
-
delete ph;
-
system("pause");
-
return EXIT_SUCCESS;
-
}
-
-
Hash::Hash(const size_t &szKey, const size_t &slot)
: szSet(szKey),
-
approSlot(slot),nSlot(getClosePrime())
-
{
-
if(!(SrcSet
= new size_t[szSet]))
-
{
-
cout<<"Fail to Allocate Memory!"<<endl;
-
Exit();
-
}
-
GeneRand(szSet);
-
}
-
-
inline void Hash::GeneRand(const size_t &nSize)
-
{
-
for(size_t index
= 0; index != nSize; index++)
-
SrcSet[index] = static_cast<size_t>(rand());
-
}
-
-
inline size_t Hash::getClosePrime() const
-
{
-
size_t start
= approSlot;
-
for(
; ; )
-
{
-
size_t index
= 2, bound = sqrt(static_cast<double>(start));
-
for(;
index <= bound; index++ )
-
if(start%index
== 0) break;
-
if(index
> bound) break;
-
else ++start;
-
}
-
return start;
-
}
-
-
chainHash::chainHash(const size_t &nSize, const size_t &slot)
: Hash(nSize, slot)
-
{
-
if(
!(pht = new HashTable[nSlot]) ||
-
!(pcn = new ChainNode[nSize])
||
-
!(ColliMatrix = new double*[chainHash::NumOfHashF]))
-
{
-
cout<<"Fail to Allocate Memory!"<<endl;
-
Exit();
-
}
-
if(!(pmd
= new MatDraw(nSlot)))
-
{
-
cout<<"Fail to start initialize matlab engine!"<<endl;
-
Exit();
-
}
-
for(size_t index
= 0; index != chainHash::NumOfHashF; index++)
-
{
-
ColliMatrix[index] = new double[nSlot];
-
if(!ColliMatrix[index])
{
-
cout<<"Fail to Allocate Memory!"<<endl;
-
Exit();
-
}
-
memset(ColliMatrix[index], 0, nSlot*sizeof(double));
-
}
-
a = GeneFrom(1,nSlot-1);
-
b = GeneFrom(0,nSlot-1);
-
memset(pht, 0, nSlot*sizeof(HashTable));
-
memset(pcn, 0, nSize*sizeof(ChainNode));
-
for(size_t index
= 0; index != szSet; index++)
-
pcn[index].key = SrcSet[index];
-
}
-
-
chainHash::~chainHash()
-
{
-
if(pht) delete []
pht;
-
if(pcn) delete []
pcn;
-
if(pmd) delete pmd;
-
for(size_t index
= 0; index != chainHash::NumOfHashF; index++)
-
delete []
ColliMatrix[index];
-
delete []
ColliMatrix;
-
}
-
-
void chainHash::getCorresIndex(const HashFunc
&hf)
-
{
-
#ifdef __TEST__
-
ChHsh phf = hfTable[hf];
-
for(size_t iter
= 0; iter != szSet; iter++)
-
cout<<SrcSet[iter]<<' '<<(this->*phf)(SrcSet[iter])<<endl;
-
cout<<endl;
-
#endif
-
}
-
-
void chainHash::InsertIntoHashTable(const HashFunc
&hf)
-
{
-
ChHsh phf = hfTable[hf];
-
size_t szLabel;
-
for(size_t index
= 0; index != szSet; index++)
-
{
-
szLabel = (this->*phf)(pcn[index].key);
-
pcn[index].next = pht[szLabel].head;
-
pht[szLabel].head = &pcn[index];
-
++pht[szLabel].szCnt;
-
}
-
}
-
-
void chainHash::IterEveryChain(const HashFunc
&hf) const
-
{
-
#ifdef __TEST__
-
if(hf
== chainHash::REMAIN)
-
cout<<"Iterate the chain hashed by <remainder>"<<endl;
-
else if(hf
== chainHash::MULTI)
-
cout<<"Iterate the chain hashed by <multiplicate>"<<endl;
-
else if(hf
== chainHash::UNIVER)
-
cout<<"Iterate the chain hashed by <univerhash>"<<endl;
-
else {
-
cout<<"Wrong : Iterate the chain hashed by NULL function"<<endl;
-
Exit();
-
}
-
#endif
-
for(size_t index
= 0; index != nSlot; index++)
-
{
-
size_t szCnt
= pht[index].szCnt;
-
*(ColliMatrix[static_cast<size_t>(hf)]+index)
= szCnt;
-
#ifdef __TEST__
-
cout<<"the number of nodes in the chain : "<<szCnt
-
<<'\t'<<"the
node :";
-
for(ChainNode
*p=pht[index].head; p != NULL; p = p->next)
-
cout<<p->key<<"-->";
-
cout<<endl;
-
#endif
-
}
-
}
-
-
void chainHash::RunAndGetCollision()
-
{
-
size_t limit
= sizeof(chainHash::hfTable)/sizeof(chainHash::hfTable[0]);
-
for(size_t index
= 0; index != limit; ++index)
-
{
-
getCorresIndex(static_cast<HashFunc>(index));
-
InsertIntoHashTable(static_cast<HashFunc>(index));
-
IterEveryChain(static_cast<HashFunc>(index));
-
memset(pht, 0, nSlot*sizeof(HashTable));
-
for(size_t ix
= 0; ix != szSet; ix++)
-
pcn[ix].next = NULL;
-
#ifdef __TEST__
-
for(size_t ix
= 0; ix !=nSlot; ix++)
-
cout<<*(ColliMatrix[index]+ix)<<' ';
-
cout<<endl<<endl;
-
#endif
-
}
-
}
-
-
void openAddrHash::GeneArg(const HashFunc
&hf)
-
{
-
size_t start,
end;
-
switch(hf)
{
-
case openAddrHash::LINEAR:
-
start = 0; end = 1+1; break;
-
case openAddrHash::QUADRATIC:
-
start = 2; end = 3+1; break;
-
case openAddrHash::DBHASH:
-
start = 4; end = 7+1; break;
-
default:
-
cout<<"Wrong : No existing hash function !"<<endl;
-
Exit();
-
}
-
for(size_t index
= start; index != end; index += 2)
-
{
-
arg[index] = GeneFrom(1, nSlot-1);
-
arg[index+1] = GeneFrom(0, nSlot-1);
-
}
-
}
-
-
openAddrHash::openAddrHash(const size_t &nSize, const size_t &slot)
: Hash(nSize, slot)
-
{
-
if(!(pht
= new HashTable[nSlot]) ||
-
!(ColliMatrix = new double*[openAddrHash::NumOfHashF])
)
-
{
-
cout<<"Fail to Allocate Memory!"<<endl;
-
Exit();
-
}
-
if(!(pmd
= new MatDraw(COUNT_OF_EXECUTE)))
-
{
-
cout<<"Fail to start initialize matlab engine!"<<endl;
-
Exit();
-
}
-
for(size_t index
= 0; index != openAddrHash::NumOfHashF; index++)
-
{
-
if(!(ColliMatrix[index]
= new double[COUNT_OF_EXECUTE]))
-
{
-
cout<<"Fail to Allcate Memory!"<<endl;
-
Exit();
-
}
-
memset(ColliMatrix[index], 0, COUNT_OF_EXECUTE*sizeof(double));
-
}
-
memset(pht, 0, nSlot*sizeof(HashTable));
-
for(size_t index
= 0; index != openAddrHash::NumOfHashF; index++)
-
GeneArg(static_cast<HashFunc>(index));
-
c1 = GeneFrom(START, END);
-
c2 = GeneFrom(START, END);
-
}
-
-
openAddrHash::~openAddrHash()
-
{
-
if(pht) delete []
pht;
-
if(pmd) delete pmd;
-
for(size_t index
= 0; index != openAddrHash::NumOfHashF; index++)
-
delete []
ColliMatrix[index];
-
delete []
ColliMatrix;
-
}
-
-
void openAddrHash::getCorresIndex(const HashFunc
&hf) throw(exception)
-
{
-
#ifdef __TEST__
-
printInfo(hf);
-
#endif
-
OAHashF pOpenAddrHf = hfTable[hf];
-
size_t szLabel,
ix;
-
for(size_t index
= 0; index != szSet; index++)
-
{
-
for(ix
= 0; ix != nSlot; ix++)
-
{
-
szLabel = (this->*pOpenAddrHf)(SrcSet[index],
ix);
-
++pht[szLabel].szCnt;
-
#ifdef __TEST__
-
cout<<SrcSet[index]<<'\t'<<szLabel<<endl;
-
#endif
-
if(!pht[szLabel].IsNotEmpty)
{
-
pht[szLabel].IsNotEmpty = true;
-
break;
-
}
-
}
-
if(ix
== nSlot)
-
throw exception("hash
Failure, restart ...\n");
-
}
-
#ifdef __TEST__
-
for(size_t index
= 0; index != nSlot; index++)
-
cout<<"Slot : "<<index<<"\tCounts
of Collision : "<<pht[index].szCnt<<endl;
-
#endif
-
}
-
-
void openAddrHash::RunAndGetCollision()
-
{
-
size_t totalCollision,
totalKeyInHashTable;
-
for(size_t index
= 0; index != COUNT_OF_EXECUTE; index++)
-
{
-
for(size_t ix
= 0; ix != openAddrHash::NumOfHashF; ix++)
-
{
-
try{
-
memset(pht, 0, nSlot*sizeof(HashTable));
-
totalCollision = totalKeyInHashTable = 0;
-
getCorresIndex(static_cast<HashFunc>(ix));
-
for(size_t j
= 0; j != nSlot; j++)
-
totalCollision += pht[j].szCnt;
-
} catch(exception
&e) {
-
cout<<e.what();
-
GeneArg(static_cast<HashFunc>(ix));
-
--ix; continue;
-
}
-
ColliMatrix[ix][index] = totalCollision;
-
}
-
cout<<index<<" test has been finished!"<<endl;
-
GeneRand(szSet);
-
}
-
#ifdef __TEST__
-
for(size_t index
= 0; index != openAddrHash::NumOfHashF; index++)
-
{
-
for(size_t ix
= 0; ix != COUNT_OF_EXECUTE; ix++)
-
cout<<ColliMatrix[index][ix]<<' ';
-
cout<<endl;
-
}
-
#endif
-
}
-
-
void openAddrHash::printInfo(const HashFunc
&hf)
-
{
-
cout<<"hash' & hash'' assist hash function --->"
-
" (a*key+b) mod m, here m="<<nSlot<<endl;
-
switch(hf)
{
-
case openAddrHash::LINEAR:
-
cout<<"Linear Probing : \n"<<"a1="<<arg[0]<<"\tb1="<<arg[1]<<'\n';
-
cout<<"hash(key,i)=(hash'(key)+i) mod m"<<endl;
-
break;
-
case openAddrHash::QUADRATIC:
-
cout<<"Quadratic Probing : \n"<<"a1="<<arg[2]<<"\tb1="<<arg[3]<<'\n';
-
cout<<"c1="<<c1<<"\tc2="<<c2<<'\n';
-
cout<<"hash(key,i)=(hash'(key)+c1*i+c2*i*i) mod m"<<endl;
-
break;
-
case openAddrHash::DBHASH:
-
cout<<"DoubleHash Probing : \n"<<"a1="<<arg[4]<<"\tb1="<<arg[5]<<'\n';
-
cout<<"a2="<<arg[6]<<"\tb2="<<arg[7]<<'\n';
-
cout<<"hash(key,i)=(hash'(key)+i*hash''(key)) mod m"<<endl;
-
break;
-
default:
-
cout<<"Wrong : There has no More Hash Function!"<<endl;
-
Exit();
-
}
-
}
-
-
MatDraw::MatDraw(const mwSize
&n_Length) : ArrLength(n_Length)
-
{
-
double *
InitForXOrder = new double[n_Length];
-
-
if(!(ep
= engOpen(NULL))) {
-
cout<<"Fail to start Matlab!"<<endl;
-
exit(0);
-
}
-
engSetVisible(ep, false);
-
-
if(!(paForXOrder
= mxCreateDoubleMatrix(1, n_Length, mxREAL)) ||
-
!(paForYOrder = mxCreateDoubleMatrix(1, n_Length, mxREAL)))
-
{
-
cout<<"Fail to create matrix!"<<endl;
-
exit(0);
-
}
-
for(size_t index
= 0; index != n_Length; index++)
-
InitForXOrder[index] = index;
-
-
memmove(mxGetPr(paForXOrder), InitForXOrder, ArrLength*sizeof(double));
-
-
engPutVariable(ep, "paForXOrder",
paForXOrder);
-
delete InitForXOrder;
-
}
-
-
-
void MatDraw::mdPlot(double **pd, const size_t &RowsOfMatrix)
-
{
-
char temp[1024];
-
string EvalStr("title('collision statistics');"),
colStr("rgbykmc");
-
sprintf(temp, "xlabel('x=[0 : %d]');",
ArrLength);
-
EvalStr += string(temp);
-
EvalStr += string("ylabel('the count ofs collision');");
-
EvalStr += string("hold on;");
-
for(size_t index
= 0; index != RowsOfMatrix; index++)
-
{
-
memmove(mxGetPr(paForYOrder), pd[index],
-
ArrLength*sizeof(double));
-
engPutVariable(ep, "paForYOrder",
paForYOrder);
-
EvalStr += string("plot(paForXOrder,paForYOrder,\'")
-
+colStr[index]+string("*-\');");
-
engEvalString(ep, EvalStr.c_str());
-
EvalStr.clear();
-
}
-
}
-
#include <iostream>
-
#include <cmath>
-
#include <ctime>
-
#include <string>
-
#include <exception>
-
#include <Windows.h>
-
#include <engine.h>
-
using namespace std;
-
-
-
#pragma warning(disable:4244)
-
#pragma warning(disable:4996)
-
#pragma warning(disable:4290)
-
const size_t KEY_SCALE = 600;
-
const size_t SLOTS_IN_CHAIN = 45;
-
const size_t SLOTS_IN_OPENADDR = KEY_SCALE;
-
const size_t COUNT_OF_EXECUTE = 60;
-
-
class MatDraw{
-
public:
-
MatDraw(const mwSize&);
-
~MatDraw()
-
{
-
mxDestroyArray(paForXOrder);
-
mxDestroyArray(paForYOrder);
-
engClose(ep);
-
}
-
void mdPlot(double **, const size_t&);
-
void EvalStr(string str)
-
{ engEvalString(ep, str.c_str()); }
-
private:
-
MatDraw(const MatDraw&);
-
MatDraw& operator=(const MatDraw&);
-
private:
-
Engine *ep;
-
mxArray *paForXOrder;
-
mxArray *paForYOrder;
-
size_t ArrLength;
-
};
-
-
class Hash{
-
public:
-
virtual void plot() = 0;
-
virtual void RunAndGetCollision() = 0;
-
Hash(const size_t&, const size_t&);
-
virtual ~Hash()
-
{ if(SrcSet) delete [] SrcSet; }
-
protected:
-
typedef size_t index;
-
void GeneRand(const size_t&);
-
size_t GeneFrom(size_t left, size_t right) const
-
{ return (rand()%(right-left+1))+left; }
-
size_t getClosePrime() const;
-
void Exit() const { system("pause"); exit(0); }
-
size_t *SrcSet;
-
size_t szSet;
-
size_t approSlot;
-
size_t nSlot;
-
protected:
-
-
-
index unihash(const size_t &key, const size_t &arg_a, const size_t &arg_b)
-
{
-
-
return (arg_a*key+arg_b) % nSlot;
-
}
-
};
-
-
class chainHash : public Hash{
-
private:
-
typedef index (chainHash::*ChHsh)(const size_t&);
-
struct ChainNode{
-
size_t key;
-
ChainNode *next;
-
};
-
struct HashTable{
-
ChainNode *head;
-
size_t szCnt;
-
};
-
public:
-
typedef enum { REMAIN, MULTI, UNIVER } HashFunc;
-
chainHash(const size_t&, const size_t&);
-
~chainHash();
-
void getCorresIndex(const HashFunc&);
-
void IterEveryChain(const HashFunc &) const;
-
void InsertIntoHashTable(const HashFunc&);
-
void RunAndGetCollision();
-
void plot()
-
{
-
pmd->mdPlot(ColliMatrix,chainHash::NumOfHashF);
-
pmd->EvalStr(string("legend('remainder','multiplicate','univerhash');").c_str());
-
}
-
private:
-
chainHash(const chainHash&);
-
chainHash& operator=(const chainHash&);
-
-
index remainder(const size_t &key) { return key%nSlot; }
-
index multiplicate(const size_t &key)
-
{
-
-
float A = (sqrt(5.0)-1)/2;
-
float temp = A*key;
-
return (temp-static_cast<size_t>(temp))*nSlot;
-
}
-
index univerhash(const size_t &key)
-
{
-
-
return unihash(key, a, b);
-
}
-
private:
-
-
size_t a, b;
-
private:
-
HashTable *pht;
-
ChainNode *pcn;
-
static ChHsh hfTable[];
-
static size_t NumOfHashF;
-
double **ColliMatrix;
-
class MatDraw *pmd;
-
};
-
-
class openAddrHash : public Hash {
-
private:
-
typedef index (openAddrHash::*OAHashF)(const size_t&, const size_t&);
-
struct HashTable {
-
size_t szCnt;
-
bool IsNotEmpty;
-
};
-
public:
-
typedef enum { LINEAR, QUADRATIC, DBHASH} HashFunc;
-
openAddrHash(const size_t&, const size_t&);
-
~openAddrHash();
-
void plot()
-
{
-
pmd->EvalStr(string("figure;").c_str());
-
pmd->mdPlot(ColliMatrix, openAddrHash::NumOfHashF);
-
pmd->EvalStr(string("legend('linear','quadratic','doublehash');").c_str());
-
}
-
void RunAndGetCollision();
-
private:
-
-
-
index linearprob(const size_t &key, const size_t &ix)
-
{ return (unihash(key, arg[0], arg[1])+ix)%nSlot; }
-
-
index quadprob(const size_t &key, const size_t &ix)
-
{ return (unihash(key, arg[2], arg[3])+c1*ix+c2*ix*ix)%nSlot; }
-
-
index dbhashprob(const size_t &key, const size_t &ix)
-
{ return (unihash(key, arg[4], arg[5])+ix*unihash(key, arg[6], arg[7]))%nSlot; }
-
void getCorresIndex(const HashFunc&) throw(exception);
-
void printInfo(const HashFunc&);
-
private:
-
-
-
-
-
-
#define ARG_IN_HASHFUNC 8
-
size_t arg[ARG_IN_HASHFUNC];
-
void GeneArg(const HashFunc&);
-
-
#define START 1
-
#define END 5
-
-
size_t c1, c2;
-
private:
-
HashTable *pht;
-
double **ColliMatrix;
-
class MatDraw *pmd;
-
static OAHashF hfTable[];
-
static size_t NumOfHashF;
-
};
-
-
chainHash::ChHsh chainHash::hfTable[] = {&chainHash::remainder,
-
&chainHash::multiplicate,
-
&chainHash::univerhash};
-
size_t chainHash::NumOfHashF = sizeof(chainHash::hfTable)/sizeof(chainHash::hfTable[0]);
-
-
openAddrHash::OAHashF openAddrHash::hfTable[] = {&openAddrHash::linearprob,
-
&openAddrHash::quadprob,
-
&openAddrHash::dbhashprob};
-
size_t openAddrHash::NumOfHashF = sizeof(openAddrHash::hfTable)/
-
sizeof(openAddrHash::hfTable[0]);
-
-
int main(int argc, char *argv[])
-
{
-
srand(static_cast<unsigned>(time(NULL)));
-
Hash *ph = new chainHash(KEY_SCALE,SLOTS_IN_CHAIN);
-
ph->RunAndGetCollision();
-
ph->plot();
-
delete ph;
-
ph = new openAddrHash(KEY_SCALE,SLOTS_IN_OPENADDR);
-
ph->RunAndGetCollision();
-
ph->plot();
-
delete ph;
-
system("pause");
-
return EXIT_SUCCESS;
-
}
-
-
Hash::Hash(const size_t &szKey, const size_t &slot) : szSet(szKey),
-
approSlot(slot),nSlot(getClosePrime())
-
{
-
if(!(SrcSet = new size_t[szSet]))
-
{
-
cout<<"Fail to Allocate Memory!"<<endl;
-
Exit();
-
}
-
GeneRand(szSet);
-
}
-
-
inline void Hash::GeneRand(const size_t &nSize)
-
{
-
for(size_t index = 0; index != nSize; index++)
-
SrcSet[index] = static_cast<size_t>(rand());
-
}
-
-
inline size_t Hash::getClosePrime() const
-
{
-
size_t start = approSlot;
-
for( ; ; )
-
{
-
size_t index = 2, bound = sqrt(static_cast<double>(start));
-
for(; index <= bound; index++ )
-
if(start%index == 0) break;
-
if(index > bound) break;
-
else ++start;
-
}
-
return start;
-
}
-
-
chainHash::chainHash(const size_t &nSize, const size_t &slot) : Hash(nSize, slot)
-
{
-
if( !(pht = new HashTable[nSlot]) ||
-
!(pcn = new ChainNode[nSize]) ||
-
!(ColliMatrix = new double*[chainHash::NumOfHashF]))
-
{
-
cout<<"Fail to Allocate Memory!"<<endl;
-
Exit();
-
}
-
if(!(pmd = new MatDraw(nSlot)))
-
{
-
cout<<"Fail to start initialize matlab engine!"<<endl;
-
Exit();
-
}
-
for(size_t index = 0; index != chainHash::NumOfHashF; index++)
-
{
-
ColliMatrix[index] = new double[nSlot];
-
if(!ColliMatrix[index]) {
-
cout<<"Fail to Allocate Memory!"<<endl;
-
Exit();
-
}
-
memset(ColliMatrix[index], 0, nSlot*sizeof(double));
-
}
-
a = GeneFrom(1,nSlot-1);
-
b = GeneFrom(0,nSlot-1);
-
memset(pht, 0, nSlot*sizeof(HashTable));
-
memset(pcn, 0, nSize*sizeof(ChainNode));
-
for(size_t index = 0; index != szSet; index++)
-
pcn[index].key = SrcSet[index];
-
}
-
-
chainHash::~chainHash()
-
{
-
if(pht) delete [] pht;
-
if(pcn) delete [] pcn;
-
if(pmd) delete pmd;
-
for(size_t index = 0; index != chainHash::NumOfHashF; index++)
-
delete [] ColliMatrix[index];
-
delete [] ColliMatrix;
-
}
-
-
void chainHash::getCorresIndex(const HashFunc &hf)
-
{
-
#ifdef __TEST__
-
ChHsh phf = hfTable[hf];
-
for(size_t iter = 0; iter != szSet; iter++)
-
cout<<SrcSet[iter]<<' '<<(this->*phf)(SrcSet[iter])<<endl;
-
cout<<endl;
-
#endif
-
}
-
-
void chainHash::InsertIntoHashTable(const HashFunc &hf)
-
{
-
ChHsh phf = hfTable[hf];
-
size_t szLabel;
-
for(size_t index = 0; index != szSet; index++)
-
{
-
szLabel = (this->*phf)(pcn[index].key);
-
pcn[index].next = pht[szLabel].head;
-
pht[szLabel].head = &pcn[index];
-
++pht[szLabel].szCnt;
-
}
-
}
-
-
void chainHash::IterEveryChain(const HashFunc &hf) const
-
{
-
#ifdef __TEST__
-
if(hf == chainHash::REMAIN)
-
cout<<"Iterate the chain hashed by <remainder>"<<endl;
-
else if(hf == chainHash::MULTI)
-
cout<<"Iterate the chain hashed by <multiplicate>"<<endl;
-
else if(hf == chainHash::UNIVER)
-
cout<<"Iterate the chain hashed by <univerhash>"<<endl;
-
else {
-
cout<<"Wrong : Iterate the chain hashed by NULL function"<<endl;
-
Exit();
-
}
-
#endif
-
for(size_t index = 0; index != nSlot; index++)
-
{
-
size_t szCnt = pht[index].szCnt;
-
*(ColliMatrix[static_cast<size_t>(hf)]+index) = szCnt;
-
#ifdef __TEST__
-
cout<<"the number of nodes in the chain : "<<szCnt
-
<<'\t'<<"the node :";
-
for(ChainNode *p=pht[index].head; p != NULL; p = p->next)
-
cout<<p->key<<"-->";
-
cout<<endl;
-
#endif
-
}
-
}
-
-
void chainHash::RunAndGetCollision()
-
{
-
size_t limit = sizeof(chainHash::hfTable)/sizeof(chainHash::hfTable[0]);
-
for(size_t index = 0; index != limit; ++index)
-
{
-
getCorresIndex(static_cast<HashFunc>(index));
-
InsertIntoHashTable(static_cast<HashFunc>(index));
-
IterEveryChain(static_cast<HashFunc>(index));
-
memset(pht, 0, nSlot*sizeof(HashTable));
-
for(size_t ix = 0; ix != szSet; ix++)
-
pcn[ix].next = NULL;
-
#ifdef __TEST__
-
for(size_t ix = 0; ix !=nSlot; ix++)
-
cout<<*(ColliMatrix[index]+ix)<<' ';
-
cout<<endl<<endl;
-
#endif
-
}
-
}
-
-
void openAddrHash::GeneArg(const HashFunc &hf)
-
{
-
size_t start, end;
-
switch(hf) {
-
case openAddrHash::LINEAR:
-
start = 0; end = 1+1; break;
-
case openAddrHash::QUADRATIC:
-
start = 2; end = 3+1; break;
-
case openAddrHash::DBHASH:
-
start = 4; end = 7+1; break;
-
default:
-
cout<<"Wrong : No existing hash function !"<<endl;
-
Exit();
-
}
-
for(size_t index = start; index != end; index += 2)
-
{
-
arg[index] = GeneFrom(1, nSlot-1);
-
arg[index+1] = GeneFrom(0, nSlot-1);
-
}
-
}
-
-
openAddrHash::openAddrHash(const size_t &nSize, const size_t &slot) : Hash(nSize, slot)
-
{
-
if(!(pht = new HashTable[nSlot]) ||
-
!(ColliMatrix = new double*[openAddrHash::NumOfHashF]) )
-
{
-
cout<<"Fail to Allocate Memory!"<<endl;
-
Exit();
-
}
-
if(!(pmd = new MatDraw(COUNT_OF_EXECUTE)))
-
{
-
cout<<"Fail to start initialize matlab engine!"<<endl;
-
Exit();
-
}
-
for(size_t index = 0; index != openAddrHash::NumOfHashF; index++)
-
{
-
if(!(ColliMatrix[index] = new double[COUNT_OF_EXECUTE]))
-
{
-
cout<<"Fail to Allcate Memory!"<<endl;
-
Exit();
-
}
-
memset(ColliMatrix[index], 0, COUNT_OF_EXECUTE*sizeof(double));
-
}
-
memset(pht, 0, nSlot*sizeof(HashTable));
-
for(size_t index = 0; index != openAddrHash::NumOfHashF; index++)
-
GeneArg(static_cast<HashFunc>(index));
-
c1 = GeneFrom(START, END);
-
c2 = GeneFrom(START, END);
-
}
-
-
openAddrHash::~openAddrHash()
-
{
-
if(pht) delete [] pht;
-
if(pmd) delete pmd;
-
for(size_t index = 0; index != openAddrHash::NumOfHashF; index++)
-
delete [] ColliMatrix[index];
-
delete [] ColliMatrix;
-
}
-
-
void openAddrHash::getCorresIndex(const HashFunc &hf) throw(exception)
-
{
-
#ifdef __TEST__
-
printInfo(hf);
-
#endif
-
OAHashF pOpenAddrHf = hfTable[hf];
-
size_t szLabel, ix;
-
for(size_t index = 0; index != szSet; index++)
-
{
-
for(ix = 0; ix != nSlot; ix++)
-
{
-
szLabel = (this->*pOpenAddrHf)(SrcSet[index], ix);
-
++pht[szLabel].szCnt;
-
#ifdef __TEST__
-
cout<<SrcSet[index]<<'\t'<<szLabel<<endl;
-
#endif
-
if(!pht[szLabel].IsNotEmpty) {
-
pht[szLabel].IsNotEmpty = true;
-
break;
-
}
-
}
-
if(ix == nSlot)
-
throw exception("hash Failure, restart ...\n");
-
}
-
#ifdef __TEST__
-
for(size_t index = 0; index != nSlot; index++)
-
cout<<"Slot : "<<index<<"\tCounts of Collision : "<<pht[index].szCnt<<endl;
-
#endif
-
}
-
-
void openAddrHash::RunAndGetCollision()
-
{
-
size_t totalCollision, totalKeyInHashTable;
-
for(size_t index = 0; index != COUNT_OF_EXECUTE; index++)
-
{
-
for(size_t ix = 0; ix != openAddrHash::NumOfHashF; ix++)
-
{
-
try{
-
memset(pht, 0, nSlot*sizeof(HashTable));
-
totalCollision = totalKeyInHashTable = 0;
-
getCorresIndex(static_cast<HashFunc>(ix));
-
for(size_t j = 0; j != nSlot; j++)
-
totalCollision += pht[j].szCnt;
-
} catch(exception &e) {
-
cout<<e.what();
-
GeneArg(static_cast<HashFunc>(ix));
-
--ix; continue;
-
}
-
ColliMatrix[ix][index] = totalCollision;
-
}
-
cout<<index<<" test has been finished!"<<endl;
-
GeneRand(szSet);
-
}
-
#ifdef __TEST__
-
for(size_t index = 0; index != openAddrHash::NumOfHashF; index++)
-
{
-
for(size_t ix = 0; ix != COUNT_OF_EXECUTE; ix++)
-
cout<<ColliMatrix[index][ix]<<' ';
-
cout<<endl;
-
}
-
#endif
-
}
-
-
void openAddrHash::printInfo(const HashFunc &hf)
-
{
-
cout<<"hash' & hash'' assist hash function --->"
-
" (a*key+b) mod m, here m="<<nSlot<<endl;
-
switch(hf) {
-
case openAddrHash::LINEAR:
-
cout<<"Linear Probing : \n"<<"a1="<<arg[0]<<"\tb1="<<arg[1]<<'\n';
-
cout<<"hash(key,i)=(hash'(key)+i) mod m"<<endl;
-
break;
-
case openAddrHash::QUADRATIC:
-
cout<<"Quadratic Probing : \n"<<"a1="<<arg[2]<<"\tb1="<<arg[3]<<'\n';
-
cout<<"c1="<<c1<<"\tc2="<<c2<<'\n';
-
cout<<"hash(key,i)=(hash'(key)+c1*i+c2*i*i) mod m"<<endl;
-
break;
-
case openAddrHash::DBHASH:
-
cout<<"DoubleHash Probing : \n"<<"a1="<<arg[4]<<"\tb1="<<arg[5]<<'\n';
-
cout<<"a2="<<arg[6]<<"\tb2="<<arg[7]<<'\n';
-
cout<<"hash(key,i)=(hash'(key)+i*hash''(key)) mod m"<<endl;
-
break;
-
default:
-
cout<<"Wrong : There has no More Hash Function!"<<endl;
-
Exit();
-
}
-
}
-
-
MatDraw::MatDraw(const mwSize &n_Length) : ArrLength(n_Length)
-
{
-
double * InitForXOrder = new double[n_Length];
-
-
if(!(ep = engOpen(NULL))) {
-
cout<<"Fail to start Matlab!"<<endl;
-
exit(0);
-
}
-
engSetVisible(ep, false);
-
-
if(!(paForXOrder = mxCreateDoubleMatrix(1, n_Length, mxREAL)) ||
-
!(paForYOrder = mxCreateDoubleMatrix(1, n_Length, mxREAL)))
-
{
-
cout<<"Fail to create matrix!"<<endl;
-
exit(0);
-
}
-
for(size_t index = 0; index != n_Length; index++)
-
InitForXOrder[index] = index;
-
-
memmove(mxGetPr(paForXOrder), InitForXOrder, ArrLength*sizeof(double));
-
-
engPutVariable(ep, "paForXOrder", paForXOrder);
-
delete InitForXOrder;
-
}
-
-
-
void MatDraw::mdPlot(double **pd, const size_t &RowsOfMatrix)
-
{
-
char temp[1024];
-
string EvalStr("title('collision statistics');"), colStr("rgbykmc");
-
sprintf(temp, "xlabel('x=[0 : %d]');", ArrLength);
-
EvalStr += string(temp);
-
EvalStr += string("ylabel('the count ofs collision');");
-
EvalStr += string("hold on;");
-
for(size_t index = 0; index != RowsOfMatrix; index++)
-
{
-
memmove(mxGetPr(paForYOrder), pd[index],
-
ArrLength*sizeof(double));
-
engPutVariable(ep, "paForYOrder", paForYOrder);
-
EvalStr += string("plot(paForXOrder,paForYOrder,\'")
-
+colStr[index]+string("*-\');");
-
engEvalString(ep, EvalStr.c_str());
-
EvalStr.clear();
-
}
-
}
上述測試不再生成matlab腳本,而是直接在vc中調用其函數庫,因此需要進行如下設置(VS2010):
- 右擊解決方案 --> 打開屬性窗口 --> 點擊"VC++目錄"選項卡。
- 在“包含目錄”選項中添加頭文件目錄,如“d:\Program Files\MATLAB\R2009a\extern\include;”。
- 在“庫目錄”中添加庫文件目錄,如”d:\Program Files\MATLAB\R2009a\extern\lib\win32\microsoft;“。
- 最後選中”鏈接器“ --> "輸入"選項卡,在”附加依賴項“中添加添加如下庫"libeng.lib;libmx.lib;libmex.lib;"。
上述最後一步也可以換成在源代碼中添加預處理指令,格式如#pragma comment(lib,"libeng.lib"),將上述三個庫顯式命令鏈接器加載。
與測試用例相關的配置介紹完了,我們最後簡單分析執行所得結果:
在我們的測試用例中,測試函數散列狀況的方法是將同一個待散列集合中的每一個元素根據某個散列函數得到與其對應的槽,並將所有元素以鏈表的形式鏈接在該槽中,最終統計各個槽中存儲的元素個數用來表示碰撞的次數。可以發現,集合被除餘散列,乘法散列以及全域散列作用之後的散列狀況大致相同,這是因爲在我們的測試中,使用的待散列元素爲隨機生成數。這裏不再精確計算每個散列函數下的碰撞次數的方差,因爲如果重新生成新的隨機散列集合,方差也將隨之改變,因此在隨機生成的集合下判定到底哪個函數的散列性能較好沒有意義,一個重要的原則是首先分析待散列的集合具有什麼樣的特點,之後再具體選擇有針對性的散列函數。
上述執行結果爲開放尋址下的三種函數的散列性能,測試所用的方法是爲每個槽設置一個計數值,並且元素在探查到某個空槽之前將探查序列中的每個槽所對應的計數值均自增1(包括該空槽),最終統計槽被該散列集合中的元素碰撞的總次數,以此表示該散列函數性能,之後多次重新生成隨機數的集合進行測試。我們開放尋址下的三種函數中,線性試探法的散列性能最差,而二次試探與雙重散列的性能相當,這是因爲線性試探法中存在嚴重的羣集現象。
後記
散列表這種數據結構僅僅只是散列函數一個較爲簡單的應用。試想一下散列函數將輸入映射爲某個輸出,既然可以將該輸出解釋爲所要尋址的槽,那麼自然也可以具有其他的解釋,一個具有重大意義的應用領域即爲密碼學。這裏簡單提一下王小云教授對MD5所採用的破解方式,準確來說她所採用的方法不叫破解,而是構造了一種能夠快速產生碰撞的方法,即給出一個p1,通過該方法可以很快算出一個不等於p1的p2使得
MD5(p1)=MD5(p2),根據這一點就足以把MD5槍斃掉了。這種方法並不意味着能根據MD5的Hash函數反算出明文來,亦即這是一種單向加密函數——不存在逆函數可以將密文重新變爲明文。最後要提的一點是,現有的加密方式也就那麼幾種,並且數據/字符所組合而成的輸入也是有限的,那麼自然可以通過窮舉所有輸入,並且根據現有的散列函數預先計算出所有的輸出用以加速破解,詳細見彩虹表。
轉自 http://blog.csdn.net/jn1158359135/article/details/7205688