/******************************************************************************
*****作者:李文嘉 如有转帖,请表出处
*****CHash.h
*****
*****
*******************************************************************************/
#ifndef __HASHCACHE__H
#define __HASHCACHE__H
#include<iostream>
#include<string>
using namespace std;
class CHashTableFull
{
public:
CHashTableFull(string theMessage="The hash table is full")
{
message =theMessage;
}
void outputMessage(){cout<<message<<endl;}
private:
string message;
};
template<class K,class E> class CHashTable;
template<class K,class E>
class CHashNode
{
private:
CHashNode(){}
CHashNode(const K &Host,const K&Path,const E Port):host(Host),path(Path),port(Port),Next(0){}
K host;
K path;
E port;
CHashNode<K,E> *Next;
public:
friend class CHashTable<K,E>;
//void print(){cout<<host<<endl<<path<<endl<<port<<endl;}
};
//没有设置复制构造函数,未来可以添加
template<class K,class E>
class CHashTable
{
public:
CHashTable(int Size=1024);
~CHashTable(){delete [] CHashTables;}
bool empty()const{return HashSize==0;}
int size()const{return HashSize;}
//int IdleSize()const{return HashSize-FullSize;}
int HashId(K urls)
{
unsigned long hashValue=0;
int length=(int)urls.length();
for(int i=0;i<length;i++)
hashValue=5*hashValue+urls.at(i);
return int(hashValue);
}
void Insert(const K Urls,const K Host,const K Path,const E Port); /*MD5*/
void OutPut(ostream&out)const;
protected:
//int search(const K&)const;
protected:
CHashNode<K,E>**CHashTables;
int HashSize;
//int FullSize;
};
template<class K, class E>
CHashTable<K,E>::CHashTable(int Size)
{
HashSize=Size;
//FullSize=0;
CHashTables=new CHashNode<K,E>*[HashSize]; //暂不加入new异常处理
}
template<class K,class E>
void CHashTable<K,E>::Insert(const K Urls,const K Host,const K Path,const E Port)//不能随便使用引用,否则会发生难以预想的问题
{
int id=(int)HashId(Urls)%HashSize;
//CHashNode<K,E> *p=CHashTables[id];
//CHashNode<K,E> *node=new CHashNode<K,E>(Host,Path,Port);
//cout<<p<<endl<<CHashTables[id]<<endl;
if(CHashTables[id])
{
CHashNode<K,E> *p=CHashTables[id];
CHashNode<K,E> *node=new CHashNode<K,E>(Host,Path,Port);
node->Next=p->Next;
p->Next=node;
return;
}
else
{
CHashTables[id]=new CHashNode<K,E>(Host,Path,Port);
cout<<id<<endl<<endl<<CHashTables[id]<<endl;
//cout<<Host<<endl<<Path<<endl;
//p.print();
//p=node;
return;
}
}
template<class K,class E>
void CHashTable<K,E>::OutPut(ostream&out)const
{
for(int i=0;i<HashSize;i++)
{
CHashNode<K,E> *p=CHashTables[i];
while(p)
{
out<<"111"<<endl;
out<<p->host<<":"<<p->path<<":"<<p->port<<":"<<endl;
p=p->Next;
}
}
}
template <class K,class E>
ostream& operator<<(ostream& out, const CHashTable<K,E> &x)
{
x.OutPut(out);
return out;
}
#endif
/***********************************************************************
*****
*****
***********************************************************************/
#include"Chash.h"
#include<iostream>
#include<string>
using namespace std;
//using namespace crawler;
int main()
{
CHashTable<string,int>z;
z.Insert("www.baidu.com","www.baidu.com","192.124,343,3434",80);
z.Insert("www.google.com","www.google.com","192.124,343,3434",80);
z.Insert("www.baidu.com","www.baidu.com/index.html","192.124,343,3434",80);
cout<<z<<endl;
}