📄 hashtable.h
字号:
/*
* 文件hashtable.h:Hashtable类的定义
* 说明:Hashtable类是存储标识符语义属性的表结构,
不要修改此文件。
*/
#ifndef _H_hashtable
#define _H_hashtable
// disable warning C4786: symbol greater than 255 character,
// okay to ignore
#pragma warning(disable:4786)
#include <map>
#include <string>
#include "declaration.h"
// never forget it when using STL
using namespace std;
// 函数对象Itstr:提供一个比较字符串大小的函数
struct ltstr {
bool operator()(const char* s1, const char* s2) const
{ return strcmp(s1, s2) < 0; }
};
// Hashtable类的定义
class Hashtable {
private:
multimap<const char*, Declaration *, ltstr> mmap; //multimap模板类mmap存储符号表
//每个表项都包含标识符名和标识符属性两部分
//表项按照符号名升序排列
multimap<const char*, Declaration *, ltstr>::iterator mmapIterator; //引用表项的指针
public:
/*
* 成员函数NextDecla()
* 功能:访问下一个表项,与FirstDecla()配合使用,可以遍历符号表
* 参数:无
* 返回值:Declaration 类的指针
*/
Declaration * NextDecla();
/*
* 成员函数FirstDecla()
* 功能:访问第一个表项,与NextDecla()配合使用,可以遍历符号表
* 参数:无
* 返回值:Declaration 类的指针
*/
Declaration * FirstDecla();
/*
* 构造函数Hashtable()
* 功能:构造一个空的符号表,mmapIterator指向表头
* 参数:无
* 返回值:无
*/
Hashtable() {mmapIterator = mmap.begin();}
/*
* 成员函数NumElements()
* 功能:返回表的长度
* 参数:无
* 返回值:整型,表的长度
*/
int NumElements();
/*
* 成员函数Enter()
* 功能:插入一个表项
* 参数:identifier:字符串类型,标识符的名
decl:Declaration类指针,指向标识符的语言属性
overwriteInsteadOfShadow:布尔型,缺省值为true,
若为真,则覆盖原来的表项;若为假,增加一个同名表项
* 返回值:无
*/
void Enter(const char *identifier, Declaration *decl,
bool overwriteInsteadOfShadow = true);
/*
* 成员函数Remove()
* 功能:删除表项
* 参数:identifier:字符串类型,标识符的名
decl:Declaration类指针,指向标识符的语言属性
* 返回值:无
*/
void Remove(const char *identifier, Declaration *decl);
/*
* 成员函数Lookup()
* 功能:查找表项
* 参数:identifier:字符串类型,标识符的名
* 返回值:Declaration类指针,指向标识符的语言属性
*/
Declaration *Lookup(const char *identifier);
};
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -