ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
### 2.3.1 字典的结构 字典所使用的哈希表(dict hash table)由`dict.h/dictht`结构表示: ```c typedef struct dictht { dictEntry **table; // 哈希表数组 unsigned long size; // 哈希表大小 unsigned long sizemask; // 掩码,用于计算索引值,等于size-1 unsigned long used; // 该哈希表已有结点数量 } dictht; typedef struct dictEntry { void *key; // 键 union { void *val; uint64_tu64; int64_ts64; } v; struct dictEntry *next; // 指向下一结点,形成链表 } dictEntry; ``` 字典由`dict.h/dict`结构表示: ```c typedef struct dict { dictType *type; // 特定类型数据 void *privdata; // 私有类型数据 dictht ht[2]; // 哈希表,一般情况下只使用ht[0],ht[0]在rehash时使用ht[1] int rehashidx; // rehash索引,当rehash不在进行时,值为-1 }; struct dictType { unsigned int (*hashFunction)(const void *key); // 计算哈希值的函数 void *(*keyDup) (void *privdata, const void *key); // 复制键的函数 void *(*valDup) (void *privdata, const void *obj); // 复制值的函数 int (*keyCompare) (void *privdata, const void *k1, const void *k2); // 对比键的函数 void (*keyDetructor) (void *privdata, const void *key); // 销毁键的函数 void (*valDetructor) (void *privdata, const void *obj); // 销毁值的函数 } dictType; ```