当前页面链接:https://www.cnblogs.com/oloroso/p/4599526.html

13 HashTable抽象哈希表类——Live555源码阅读(一)基本组件类

这是Live555源码阅读的第一部分,包括了时间类,延时队列类,处理程序描述类,哈希表类这四个大类。

本文由乌合之众 lym瞎编,欢迎转载 http://www.cnblogs.com/oloroso/
本文由乌合之众 lym瞎编,欢迎转载 my.oschina.net/oloroso

HashTable抽象哈希表类#

HashTable类内部嵌套定义了一个迭代器类Iterator,这个迭代器类用于循环访问表的成员。这也是一个抽象类,但是其有一个静态的方法static Iterator* create(HashTable& hashTable);这个方法用于创建一个BasicHashTable::Iterator对象,并返回其地址。

HashTable的定义#

Copy Highlighter-hljs
class HashTable {
public:
virtual ~HashTable();
// The following must be implemented by a particular
// implementation (subclass):
static HashTable* create(int keyType);
virtual void* Add(char const* key, void* value) = 0;
// Returns the old value if different, otherwise 0
virtual Boolean Remove(char const* key) = 0;
virtual void* Lookup(char const* key) const = 0;
// Returns 0 if not found
virtual unsigned numEntries() const = 0;
Boolean IsEmpty() const { return numEntries() == 0; }
// Used to iterate through the members of the table:
class Iterator {
public:
// The following must be implemented by a particular
// implementation (subclass):
static Iterator* create(HashTable& hashTable);
virtual ~Iterator();
virtual void* next(char const*& key) = 0; // returns 0 if none
protected:
Iterator(); // abstract base class
};
// A shortcut that can be used to successively remove each of
// the entries in the table (e.g., so that their values can be
// deleted, if they happen to be pointers to allocated memory).
void* RemoveNext();
protected:
HashTable(); // abstract base class
};

迭代器HashTable:: Iterator ::create方法#

这个就不说了,代码很简单。要注意的是,其创建的是BasicHashTable::Iterator对象。BasicHashTable::IteratorHashTable::Iterator的派生类。还有,这是一个static方法。

Copy Highlighter-hljs
HashTable::Iterator* HashTable::Iterator::create(HashTable& hashTable) {
// "hashTable" is assumed to be a BasicHashTable
return new BasicHashTable::Iterator((BasicHashTable&)hashTable);
}

HashTable::create方法#

我们前面说了HashTable是一个抽象了,其定义的create方法是一个静态方法,实质上是调用的派生类的构造函数来创建的对象并返回。

Copy Highlighter-hljs
HashTable* HashTable::create(int keyType) {
return new BasicHashTable(keyType);
}

HashTable::RemoveNext方法#

RemoveNext方法不是纯虚接口。其先创建了一个绑定到自身的迭代器,然后获取迭代器当前指向的节点(因为这里刚创建,所以就是第一个),然后将其从哈希表中移除(并销毁)。

Copy Highlighter-hljs
void* HashTable::RemoveNext() {
Iterator* iter = Iterator::create(*this);
char const* key;
void* removedValue = iter->next(key);
if (removedValue != 0) Remove(key);
delete iter;
return removedValue;
}
posted @   乌合之众  阅读(1139)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
clear
点击右上角即可分享
微信分享提示
CONTENTS