live555 哈希表

转载于:https://www.acodelife.cn/index.php/category/live555/

1.创建
static HashTable* create(int keyType);

//参数:key类型
HashTable* HashTable::create(int keyType)
{
//new一个HashTable的派生类BasicHashTable的对象并返回
//调用BasicHashTable的构造函数
  return new BasicHashTable(keyType);
}

调用BasicHashTable的构造函数

2.增
HashTable的Add函数是纯虚函数,实现在派生的类BasicHashTable中实现

//如果不同value则返回旧值,否则返回NULL
void* BasicHashTable::Add(char const* key, void* value)
{
  void* oldValue;
  unsigned index;
  //通过传入key指针和index引用,得到index值,并返回一个TableEntry指针
  TableEntry* entry = lookupKey(key, index);
  //TableEntry已经存在
  if (entry != NULL)
  {
    oldValue = entry->value;
  } else 
  {
    // entry不存在,创建
    entry = insertNewEntry(index, key);
    oldValue = NULL;
  }
  //插入值
  entry->value = value;

  // 如果TableEntry的个数太大,就重现构建更多的buckets
  if (fNumEntries >= fRebuildSize)
      rebuild();

  return oldValue;
}

3.删
HashTable的Remove函数是纯虚函数,实现在派生的类BasicHashTable中实现
virtual Boolean Remove(char const* key);

//是否删除
Boolean BasicHashTable::Remove(char const* key)
{
  unsigned index;
  //通过传入key指针和index引用,得到index值,并返回一个TableEntry指针
  TableEntry* entry = lookupKey(key, index);
  //没有TableEntry,返回False
  if (entry == NULL)
      return False; // no such entry

 //删除entry
  deleteEntry(index, entry);

  return True;
}

4.查
HashTable的Lookup函数是纯虚函数,实现在派生的类BasicHashTable中实现
virtual void* Lookup(char const* key) const;

//通过键值key,查找value,不存在返回NULL
void* BasicHashTable::Lookup(char const* key) const
{
  unsigned index;
  TableEntry* entry = lookupKey(key, index);
  if (entry == NULL) return NULL; // no such entry

  return entry->value;
}

你可能感兴趣的:(live555)