zoukankan      html  css  js  c++  java
  • 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的定义

    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方法。

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

    HashTable::create方法

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

    HashTable* HashTable::create(int keyType) {
      return new BasicHashTable(keyType);
    }
    

    HashTable::RemoveNext方法

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

    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;
    }
    
  • 相关阅读:
    npm install 报错问题解决
    搭建VUE脚手架流程
    gitLad创建新仓库、上传项目、拉取项目方法
    vscode打开终端失败,重新安装其它版本也没用,想知道这是为什么
    Vue组件 父组件使用子组件中的值(子组件给父组件传值的方法)
    Element-ui 上传el-upload组件的使用方法
    Vue 计算属性
    JQ+Layui弹窗 实现支付功能
    Vue+ElementUI 验证手机号发送验证码倒计时功能,先清除定时器然后创建定时器
    生命陪伴生命,生命唤醒生命
  • 原文地址:https://www.cnblogs.com/oloroso/p/4599526.html
Copyright © 2011-2022 走看看