现在的位置: 首页 > 综合 > 正文

要想进行二次开发,先进行分析之一:

2017年11月15日 ⁄ 综合 ⁄ 共 2863字 ⁄ 字号 评论关闭

我们开始介绍第一个类:HashTable

class HashTable {
public:
  virtual ~HashTable();
  //以下必须由一个特定子类实现
  static HashTable* create(int keyType);//静态方法主要是为了实现单例模式,一种数据只能实现一种特定的HashTable
  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 {//HashTable内的指针
  public:
    static Iterator* create(HashTable const& hashTable);
    virtual ~Iterator();    
    virtual void* next(char const*& key) = 0; // returns 0 if none  
  protected:
    Iterator(); // 子类能够使用
  };
  //可用于先后删除每个表中的条目
  void* RemoveNext();
  //返回表中的第一个条目。
  void* getFirst();  
protected:
  HashTable(); // abstract base class
};

第二个介绍 BasicHashTable

class BasicHashTable: public HashTable {
private:
	class TableEntry; // forward

public:
  BasicHashTable(int keyType);//创建基本的HashTable 有字符型也有字符串型的 还有别的类型,对最小表的清空
  virtual ~BasicHashTable();//删除每个key对应的TableEntry,如果新建竖着的TableEntry*,就需要删除新建的大小

  // Used to iterate through the members of the table:
  class Iterator; friend class Iterator; // to make Sun's C++ compiler happy
  class Iterator: public HashTable::Iterator {
  public:
    Iterator(BasicHashTable const& table);
  private:
    void* next(char const*& key); // returns 0 if none
	//初始时TableEntry*为空,index++找到下一个不为空的TableEntry,然后给下一个给fNextEntry,返回当前value
  private:
    BasicHashTable const& fTable;
    unsigned fNextIndex; // 记录下一个的index
    TableEntry* fNextEntry; // 在当前表中下一个TableEntry
  };

private:
  virtual void* Add(char const* key, void* value);//向竖着的某key对应的TableEntry*中添加一个value,
  //因多个key估计对应一个index,所以要遍历整个TableEntry*找到对应的key
  // 如果在这HashTable,找到该key,返回以前数据,更新表;没有找到返回0,并在结尾添加新的TableEntry
  virtual Boolean Remove(char const* key);//删除TableEntry
  virtual void* Lookup(char const* key) const;//返回key的TableEntry的value
  // Returns 0 if not found
  virtual unsigned numEntries() const;

private:
  class TableEntry {
  public:
    TableEntry* fNext;//这是个链表,相当于HashTable里面的一个key所对应的离岸边
    char const* key;
    void* value;
  };

  TableEntry* lookupKey(char const* key, unsigned& index) const;
    // returns entry matching "key", or NULL if none
  Boolean keyMatches(char const* key1, char const* key2) const;
    // used to implement "lookupKey()"

  TableEntry* insertNewEntry(unsigned index, char const* key);
    // creates a new entry, and inserts it in the table
  void assignKey(TableEntry* entry, char const* key);
    // used to implement "insertNewEntry()"

  void deleteEntry(unsigned index, TableEntry* entry);
  void deleteKey(TableEntry* entry);
    // used to implement "deleteEntry()"

  void rebuild(); // rebuilds the table as its size increases

  unsigned hashIndexFromKey(char const* key) const;
    // used to implement many of the routines above

  unsigned randomIndex(uintptr_t i) const {
    return (unsigned)(((i*1103515245) >> fDownShift) & fMask);
  }

private:
  TableEntry** fBuckets; //形象化描述如下
  // 2-3-4
  // |
  // 5-4-4-6
  // |
  // 7-2
  TableEntry* fStaticBuckets[SMALL_HASH_TABLE_SIZE];// 最小的HashTable
  unsigned fNumBuckets/*竖着的TableEntry链表长度*/, fNumEntries/*已存在实体个数*/, fRebuildSize/*重建竖着的TableEntry链表长度*/, fDownShift, fMask;
  int fKeyType/*该元素数据类型*/;
};

抱歉!评论已关闭.