<html>

<head><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- MyFirstUnitAd -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:250px"
     data-ad-client="ca-pub-5778386704669218"
     data-ad-slot="1503492166"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>CFGReader</title>
</head>

<body>



<p align="left"><font size="6" color="#FF0000"><span lang="en-ca"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </b></span>
<b>H<span lang="en-ca">ash Table</span></b></font></p>

<div align="left">
  <pre><b><font color="#ff0000" size="5">A. <span lang="en-ca">First </span>Edition</font></b></pre>
</div>
<div align="left">
  <pre><b>Th<span lang="en-ca">is is the first edition of my hash-table class with template. I want to make it as generic as possible. However,</span></b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>it is not so easy as for hash table user needs some specific functions which has to be passed by template.</b></span></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">B</span>.<span lang="en-ca"><a name="problem"></a>The problem</span></font></b></pre>
</div>
<div align="left">
  <pre><b>How to <span lang="en-ca">implement a hash table with data type stored inside independently? Or in other words, can you make a </span></b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>generic hash table? My conflict resolution is separate chaining.</b></span></pre>
</div>
<div align="left">
  <b><font color="#ff0000" size="5"><span lang="en-ca"><a name="explain"></a>C</span>.<span lang="en-ca">The
  </span></font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>idea of 
  program</b></font></span></div>
<div align="left">
  　</div>
<div align="left">
  <pre><b>Th<span lang="en-ca">is</span> class<span lang="en-ca"> is for comp442 compiler design and our symbol table needs a hash table. I force user to pass in</span></b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>his method of initialization, remove, assignment, comparison method all by a template parameter which is a class:</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>&quot;hashFun&quot;.</b></span></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5">D.<span lang="en-ca"><a name="Method"></a>The </span>major functions</font></b></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">E</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>Further improvement</b></font></span></pre>
</div>
<div align="left">
  <pre><b>The most important part of hash-table is hash function. You can see my hash table is only about 35% full of </b></pre>
</div>
<div align="left">
  <pre><b>total space and there has been quite a few conflict. This simply means that my hash function is not good enough.</b></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">F</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>File listing</b></font></span></pre>
</div>
<div align="left">
  <pre><font size="3"><b>1<span lang="en-ca">. hash.h</span></b></font></pre>
</div>
<div align="left">
  <pre><font size="3"><b>2. <span lang="en-ca">main.cpp (main)</span></b></font></pre>
</div>
<p>　</p>
<p><span lang="en-ca"><font size="3" color="#FF0000"><b>file name: hash.h</span></b></font></p>
<pre>const int SHIFT=8;
const int TableLength=211;

struct Node
{
	char* data;
	Node* next;
};




class HashFun
{
public:
	static int fun(char* in);
	static Node* empty;
	static Node* conflictFun(Node* oldNode, Node* newNode);
	static Node* remove(Node* ptr);
	static bool eq(Node* l, Node* r);
	static void assign(Node*&amp; target, Node* source);

};


template&lt;class inElem, class outElem, class hashFun, int tableLength&gt;
class Hash
{
protected:
	outElem table[tableLength];
	int count;
public:
	Hash();
	bool search(inElem&amp; in, outElem&amp; out);
	outElem  insert(inElem&amp; in, outElem&amp; out);
	bool purge(inElem&amp; in);
	void print();
	int loadFactor(){ return count*100/tableLength;}
};


template&lt;class inElem, class outElem, class hashFun, int tableLength&gt;
Hash&lt;inElem, outElem, hashFun, tableLength&gt;::Hash()
{
	count=0;
	for (int i=0; i&lt;tableLength; i++)
	{
		hashFun::assign(table[i], hashFun::empty);//even pointer can be 0
	}
}

template&lt;class inElem, class outElem, class hashFun, int tableLength&gt;
bool Hash&lt;inElem, outElem, hashFun, tableLength&gt;
	::search(inElem&amp; in, outElem&amp; out)
{
	int index=hashFun::fun(in);
	if (!hashFun::eq(table[index], hashFun::empty))
	{
		//outElem must overload &quot;=&quot; operator!!
		hashFun::assign(out, table[index]);
		return true;
	}
	return false;
}

template&lt;class inElem, class outElem, class hashFun, int tableLength&gt;
outElem Hash&lt;inElem, outElem, hashFun, tableLength&gt;
	::insert(inElem&amp; in, outElem&amp; out)
{
	int index=hashFun::fun(in);
	outElem temp;
	if (hashFun::eq(table[index], hashFun::empty))
	{
		hashFun::assign(table[index], out);
		count++;
		return hashFun::empty;
	}
	else
	{
		hashFun::assign(temp, table[index]);
		hashFun::assign(table[index], hashFun::conflictFun(table[index], out));	
		return temp;
	}
}


template&lt;class inElem, class outElem, class hashFun, int tableLength&gt;
bool Hash&lt;inElem, outElem, hashFun, tableLength&gt;
	::purge(inElem&amp; in)
{
	int index=hashFun::fun(in);
	
	if (!(hashFun::eq(table[index], hashFun::empty)))
	{
		hashFun::remove(table[index]);
		hashFun::assign(table[index], hashFun::empty);
		return true;
	}
	else
	{
		return false;
	}
}

template&lt;class inElem, class outElem, class hashFun, int tableLength&gt;
void Hash&lt;inElem, outElem, hashFun, tableLength&gt;::print()
{
	for (int i=0; i&lt;tableLength; i++)
	{
		if (!(hashFun::eq(table[i], hashFun::empty)))
		{
			cout&lt;&lt;&quot;No. &quot;&lt;&lt;i&lt;&lt;&quot; is: &quot;&lt;&lt;table[i]&lt;&lt;endl;
		}
	}
}


Node* HashFun::empty=NULL;

int HashFun::fun(char* in)
{
	char* ptr=in;
	int result=0;
	while (*ptr!='\0')
	{
		result = ((result&lt;&lt;SHIFT) + *ptr)%TableLength;
		//result+= *ptr;
		ptr++;
	}
	return result;
}

Node* HashFun::conflictFun(Node* oldNode, Node* newNode)
{
	newNode-&gt;next=oldNode;
	return newNode;
}

Node* HashFun::remove(Node* ptr)
{
	Node* temp;
	while (ptr!=NULL)
	{
		temp=ptr;
		ptr=ptr-&gt;next;
		delete[]temp-&gt;data;
		delete temp;
	}
	return NULL;
}

bool HashFun::eq(Node* l, Node* r)
{
	return l==r;//maybe wrong!!!
}

void HashFun::assign(Node*&amp; target, Node* source)
{
	target=source;
}




</pre>
<p><font size="3" color="#FF0000"><b><span lang="en-ca">file name: main.cpp 
(main)</span></b></font></p>
<pre>#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &quot;hash.h&quot;

using namespace std;

char* myStr[5]={&quot;first&quot;, &quot;second&quot;, &quot;third&quot;, &quot;fourth&quot;, &quot;fifth&quot;};

ostream&amp; operator&lt;&lt;(ostream&amp; out, Node*ptr);

Node* createNode(char* str);

int main()
{
	Hash&lt;char*, Node*, HashFun, TableLength&gt; H;
	char buffer[20];
	char* pStr=buffer;
	Node* ptr;
	ifstream f(&quot;c:\\myinputFile.txt&quot;);
	while (!f.eof())
	{
		f&gt;&gt;buffer;
		ptr=createNode(buffer);
		if ((ptr=H.insert(pStr, ptr))!=NULL)
		{
			cout&lt;&lt;&quot; conflict for: &quot;&lt;&lt;buffer&lt;&lt;&quot;  and &quot;&lt;&lt;ptr-&gt;data&lt;&lt;endl;
		}
	}
	H.print();
	//cout&lt;&lt;&quot;now remove &quot;&lt;&lt;myStr[3]&lt;&lt;endl;
	//H.purge(myStr[3]);
	//H.print();
	cout&lt;&lt;&quot;load factor is &quot;&lt;&lt;H.loadFactor()&lt;&lt;&quot;%&quot;&lt;&lt;endl;

	return 0;
}


Node* createNode(char* str)
{
	Node* ptr=new Node;
	ptr-&gt;data=new  char[strlen(str)+1];
	strcpy(ptr-&gt;data, str);
	ptr-&gt;next=NULL;
	return ptr;
}


ostream&amp; operator&lt;&lt;(ostream&amp; out, Node* ptr)
{
	Node* temp=ptr;
	while (temp!=NULL)
	{
		out&lt;&lt;temp-&gt;data&lt;&lt;&quot;,&quot;;
		temp=temp-&gt;next;
	}
	return out;
}

</pre>
<p>　</p>
<pre>
</pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre><font color="#0000FF"><b><span lang="en-ca">The input is something like following:</span></b></font></pre>
<p>a A the1 following templatef iss provided for use with the rational unified 
process. text enclosed in square brackets and displayed in blue italics style=infoBlue) 
isn't included to provide guidance to the author and should be deleted before 
publishing the document. a paragraph entered following this style will 
automatically be set to normal </p>
<p>this template is directly inspired by the templates provided in the Rational 
Unified Process, and are all available for free on the Rational Software web 
site. All credit go to Rational Software, except for the edition that we made, 
for which Rational Software is not responsible for<br>
<br>
<br>
</p>
<p>ialog, automatic fields may be updated throughout the document by selecting 
Edit&gt;Select All (or Ctrl-A) and pressing F9, or simply<br>
<br>
</p>
<p>between displaying the field names and the </p>
<p>A paragraph entered following this style a</p>
<pre>　</pre>
<pre><font color="#0000FF"><b>Here is the <a name="result"></a><a name="result" href="picture/Console.JPG">result:</a></b></font></pre>

<pre>　</pre>

<pre> conflict for: rational  and the
 conflict for: in  and in
 conflict for: guidance  and provide
 conflict for: to  and to
 conflict for: the  and rational
 conflict for: and  and and
 conflict for: publishing  and brackets
 conflict for: the  and the
 conflict for: a  and a
 conflict for: paragraph  and for
 conflict for: following  and following
 conflict for: style  and unified
 conflict for: be  and be
 conflict for: to  and to
 conflict for: this  and this
 conflict for: by  and included
 conflict for: the  and the
 conflict for: provided  and provided
 conflict for: in  and in
 conflict for: the  and the
 conflict for: Rational  and set
 conflict for: Process,  and deleted
 conflict for: and  and and
 conflict for: available  and style=infoBlue)
 conflict for: for  and paragraph
 conflict for: free  and following
 conflict for: on  and publishing
 conflict for: the  and the
 conflict for: Rational  and Rational
 conflict for: All  and before
 conflict for: to  and to
 conflict for: Rational  and Rational
 conflict for: except  and templates
 conflict for: for  and for
 conflict for: the  and the
 conflict for: we  and and
 conflict for: for  and for
 conflict for: which  and Rational
 conflict for: Rational  and which
 conflict for: Software  and Software
 conflict for: is  and is
 conflict for: for  and for
 conflict for: ialog,  and guidance
 conflict for: be  and be
 conflict for: updated  and by
 conflict for: the  and the
 conflict for: document  and may
 conflict for: by  and updated
 conflict for: All  and All
 conflict for: and  and we
 conflict for: the  and the
 conflict for: field  and Software,
 conflict for: names  and document
 conflict for: and  and and
 conflict for: the  and the
 conflict for: A  and A
 conflict for: paragraph  and for
 conflict for: entered  and entered
 conflict for: following  and free
 conflict for: this  and this
 conflict for: style  and style
 conflict for: a  and a
No. 0 is: document.,
No. 4 is: will,
No. 7 is: the1,
No. 8 is: field,Software,,
No. 9 is: following,free,following,following,
No. 13 is: edition,
No. 14 is: web,
No. 18 is: provided,provided,
No. 19 is: credit,
No. 21 is: (or,
No. 26 is: paragraph,for,for,for,for,paragraph,for,
No. 29 is: should,
No. 31 is: ialog,,guidance,provide,
No. 34 is: Unified,
No. 35 is: F9,,
No. 41 is: on,publishing,brackets,
No. 42 is: this,this,this,
No. 45 is: or,
No. 46 is: isn't,
No. 49 is: pressing,
No. 53 is: fields,
No. 56 is: to,to,to,to,
No. 64 is: enclosed,
No. 65 is: A,A,
No. 69 is: between,
No. 71 is: automatic,
No. 74 is: names,document,may,
No. 76 is: All,All,before,
No. 77 is: inspired,
No. 80 is: be,be,be,
No. 81 is: blue,
No. 82 is: site.,
No. 84 is: made,,
No. 97 is: a,a,a,
No. 98 is: italics,
No. 99 is: all,
No. 100 is: by,updated,by,included,
No. 103 is: Edit&gt;Select,
No. 104 is: go,
No. 105 is: that,
No. 108 is: throughout,
No. 109 is: Process,,deleted,
No. 114 is: responsible,
No. 119 is: available,style=infoBlue),
No. 122 is: templatef,
No. 127 is: entered,entered,
No. 129 is: text,
No. 135 is: except,templates,
No. 140 is: displaying,
No. 146 is: process.,
No. 149 is: with,
No. 151 is: are,
No. 152 is: style,style,unified,
No. 156 is: directly,
No. 158 is: simply,
No. 161 is: Rational,which,Rational,Rational,Rational,set,
No. 163 is: iss,
No. 168 is: Software,Software,
No. 169 is: author,
No. 179 is: automatically,
No. 180 is: Ctrl-A),
No. 181 is: and,and,we,and,and,and,
No. 184 is: use,
No. 187 is: square,
No. 188 is: template,
No. 190 is: selecting,
No. 192 is: not,
No. 193 is: in,in,in,
No. 196 is: the,the,the,the,the,the,the,the,the,rational,the,
No. 198 is: is,is,
No. 200 is: displayed,
No. 206 is: normal,
load factor is 34%
Press any key to continue</pre>
<pre>　</pre>

<p>　</p>

<pre></pre>

<pre></pre>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                                   
&nbsp;&nbsp;&nbsp; <a href="WhoAmI.htm">                  







                       <img src="picture/back.gif" style="border: medium none" alt="back.gif (341 bytes)" width="32" height="35"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<a href="index.htm">
<img src="picture/up.gif" style="border: medium none" alt="up.gif (335 bytes)" width="35" height="32"></a>       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                         
<img src="picture/next.gif" style="border: medium none" alt="next.gif (337 bytes)" width="32" height="35">          


</p>

</body>

</html>