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

📄 hashtable.cpp

📁 一个面向对像语言的编译器
💻 CPP
字号:
/*
 * 文件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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -