📄 6.6.3c.htm
字号:
<html>
<head>
<title>编译原理</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link type="text/css" rel="stylesheet" href="../css/specification.css">
</head>
<body>
<table align=right width=300>
<tr>
<td><img src="../images/previous.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='6.6.3b.htm'"></td>
<td><img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='6.6.4.htm'"></img></td>
</tr>
</table>
<br><br>
<table><tr><td>    </td>
<td class="content">
<P>
下面要介绍的是P.J.Weinberger的C编译程序中实际用到的散列函数hashpjw,如图6.29所示。
</p>
</td></tr></table>
<table><tr><td>    </td>
<td class="content">
<P>
(1) <font color="#0000FF">#define</font> PRIME 211 <br>
(2) <font color="#0000FF">#define</font> EOS '\0'<br>
(3) <font color="#0000FF"> int</font> hashpjw(s) <br>
(4) <font color="#0000FF"> char</font> *s; <br>
(5){ <br>
(6) <font color="#0000FF">char</font> *p; <br>
(7) <font color="#0000FF">unsigned</font> h=0,g; <br>
(8) <font color="#0000FF">for</font>(p=s;*p!=EOS;p=p+1){ <br>
(9) h=(h<<4)+(*p); <br>
(10) <font color="#0000FF">if</font>(g=h&0xf0000000){
<br>
(11) h= h^(g>>24); <br>
(12) h=h^g;
<br>
(13) } <br>
(14) } <br>
(15) <font color="#0000FF">return</font> h%PRIME; <br>
(16) } </p>
<p>
<b>图6.29</b> 散列函数hashpjw </p>
</td></tr></table>
<table><tr><td>    </td>
<td class="content">
<P>
函数hashpjw是从h=0开始计算的。对每一个字符c,将h左移4位,并加上c,得到新的h。如果在32位的h的四位高序位(最左边的四位)中有1存在的话,那么把这四位提取出来并右移24位,把它与h进行模2相加,然后将四位高序位中曾是1的那些位置为0。经试验证明散列函数hashpiw对于用它建立的各种长度不同的符号表都取得了很好的效果。即在这样的符号表中字符串能均匀的落人到各个链表中去。不但如此,haShpjw本身的计算既快速又简洁。
</p>
</td></tr></table>
<br>
<table align=right width=300>
<tr>
<td><img src="../images/previous.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='6.6.3b.htm'"></td>
<td><img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='6.6.4.htm'"></img></td>
</tr>
</table>
</BODY>
<html><script language="JavaScript">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -