?? p382.cpp
字號:
const int PageSize = 2; //一個頁塊的最大容量
const int CharNum = 2; //關(guān)鍵碼中的字符數(shù)(包括串結(jié)束符)
struct TwoChars
{
char str[CharNum];
}; //每個關(guān)鍵碼兩個字符
struct page { //頁塊構(gòu)造
int PgDepth; //區(qū)分關(guān)鍵碼的二進位位數(shù), 即局部深度
TwoChars Names[PageSize]; //關(guān)鍵碼數(shù)組, 每頁最大可容納的關(guān)鍵碼個數(shù)
int Count; //在本頁塊中的實際關(guān)鍵碼數(shù)目
};
typedef page *paddr; //頁塊指針
int DicDepth; //目錄深度, 即最大局部深度, 不超過WordSize
paddr *Directory;
int hash ( TwoChars key ) {
//使用一個均勻分布的散列函數(shù)對關(guān)鍵字key進行計算, 函數(shù)返回一個隨機化的地址
int Sum = 0, j = 0, len = 0;
for ( int i=0; i<=CharNum; i++ )
if ( key.str[i] == '\0' || key.str[i] == '\n') break;
else len++; //計算關(guān)鍵碼的字符數(shù)
if ( len % 2 == 1) //如果len是奇數(shù), 在關(guān)鍵碼尾加一空白, 使len成為偶數(shù)
{ key.str[len+1] = key.str[len]; key.str[len] = ' '; len++; }
while ( j < len ) {
Sum = ( Sum + 100 * key.str[j] + key.str[j+1] ) % 19937; j += 2;
}
return Sum;
}
unsigned int makeAddress ( const TwoChars & key , const int depth ) {
//將由關(guān)鍵碼key轉(zhuǎn)換成的散列值按指定的低階二進位數(shù)depth再轉(zhuǎn)換成二進位序列, 作為頁塊地址返回。
int hashval = hash ( key ); //將關(guān)鍵碼轉(zhuǎn)換成為一個整型的散列值
unsigned int retval = 0, mask = 1; //累加結(jié)果位串置初值及抽取最低位的屏蔽單元
for ( int j=1; j<=depth; j++ ) { //按照指定二進位數(shù)循環(huán), 逐位轉(zhuǎn)換
retval = retval << 1; //結(jié)果左移一位
retval = retval | mask; //將最低位拼入結(jié)果
}
retval=retval&hashval;
return retval;
}
int operator ==(const TwoChars& left,const TwoChars& right)
{
return ((left.str[0]==right.str[0])&&(left.str[1]==right.str[1]));
}
inline unsigned int Power2(unsigned int n)
{
return 1<<n;
}
void InitHash(void)
{
DicDepth=0;
Directory=new paddr[1];
Directory[0]=new page;
Directory[0]->Count=0;
Directory[0]->PgDepth=0;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -