⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hash.h

📁 此代码运行于visual c++ 6.0的环境下
💻 H
字号:
// Hash.h: interface for the CHash class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_HASH_H__76B4260C_FEF6_45ED_AC24_0095B7F4F910__INCLUDED_)
#define AFX_HASH_H__76B4260C_FEF6_45ED_AC24_0095B7F4F910__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "stdafx.h"
#include "Data.h"
#include "HashElem.h"
#include "Deque.h"
#include <deque>
using std::deque;

#define Max 100000

class CHash  //散列表类
{
public:

	CDeque<CHashElem> * Index;					//CDeque<CHashElem>数组的头指针
	int ELFHash( const char * key ){			//计算散列地址的函数
		unsigned long h = 0;
		while( * key ){
			h = ( h << 4 ) + *key++;
			unsigned long g = h & 0xF0000000L;
			if(g) h^=g>>24;
			h &= ~g;
		}
		return h%Max;
	}

	void insert( const CHashElem & elem ){					//在散列中插入一个新元素
		int num = ELFHash( elem.Key.c_str() );				//找到相应的槽
		Index[num].push_back( elem );						//插入队列末尾
	}
	

	void visit( CDeque<CHashElem> & Deque, fstream & out ){//遍历槽中的队列,将内容写入文件
		for( int i = 0; i < Deque.size(); i++ )
			out<<Deque.at(i);
	}
	void VisitAll(fstream & out){							//遍历一遍链表将散列内容写入文件
		for( int i = 0; i < Max; i++ )
			visit( Index[i],out);
	}

	void find( const char * key, CDeque<long> & Deque ){	//查找与参数中所给关键码相同的元素
		//CData data;										//将偏移两插入参数传入的Deque中
		int num = ELFHash ( key );
		for( int i = 0; i < Index[num].size();i++ ){
			if( Index[num].at(i).Key.compare( key) == 0 ){
				Deque.push_back( Index[num].at(i).Pos );
			}
		}
	}
	CHash(){ Index = new CDeque<CHashElem>[Max]; }			//构造函数
	virtual ~CHash(){ delete [] Index; }					//析构函数

};

#endif // !defined(AFX_HASH_H__76B4260C_FEF6_45ED_AC24_0095B7F4F910__INCLUDED_)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -