hashtable.cpp
来自「一个面向对像语言的编译器」· C++ 代码 · 共 101 行
CPP
101 行
/*
* 文件hashTable.cpp:Hashtable类的实现
* 说明:不要修改此文件
*/
#include "hashtable.h"
#include "declaration.h"
/*
* 成员函数Enter()
*/
void Hashtable::Enter(const char *identifier, Declaration *decl, bool overwrite)
{
Declaration *prev;
if (overwrite && (prev = Lookup(identifier)))
{
Remove(identifier,prev);
}
mmap.insert(make_pair(strdup(identifier), decl));
return;
}
/*
* 成员函数Remove()
*/
void Hashtable::Remove(const char *identifier, Declaration *decl)
{
if (mmap.count(identifier) == 0)
return;
multimap<const char *, Declaration *, ltstr>::iterator itr;
itr = mmap.find(identifier);
while (itr != mmap.upper_bound(identifier))
{
if (itr->second == decl)
{
mmap.erase(itr);
break;
}
++itr;
}
}
/*
* 成员函数Lookup()
*/
Declaration *Hashtable::Lookup(const char *identifier)
{
Declaration *found = NULL;
if (mmap.count(identifier) > 0)
{
//如果表中有同名表项,则返回最后插入表中的那一项
multimap<const char *, Declaration *, ltstr>::iterator cur, last, prev;
cur = mmap.find(identifier);
last = mmap.upper_bound(identifier);
while (cur != last)
{
prev = cur;
cur++;
}
found = prev->second;
}
return found;
}
/*
* 成员函数NumElements()
*/
int Hashtable::NumElements()
{
return mmap.size();
}
/*
* 成员函数FirstDecla()
*/
Declaration * Hashtable::FirstDecla()
{
mmapIterator = mmap.begin();
return mmapIterator->second;
}
/*
* 成员函数NextDecla()
*/
Declaration * Hashtable::NextDecla()
{
if(mmapIterator != mmap.end())
{
mmapIterator++;
}
return (mmapIterator == mmap.end()) ? NULL : (mmapIterator->second);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?