dict, hash

dict: 

  dictKey -- > dictVal

example:

     dictEntry *dictFind(dict *d, const void *key)
     Key is like a index which to find the real entry.

how to find:

  depend on the construction of dict.

       if dict is constructed by arr then we get arr[i]. key is not integer usual and here the array is a map.

  if dict is constructed by hash table, then we get hash table value.

 

redis code dictFind:

  

typedef struct dict {
    dictType *type;
    void *privdata;
    dictht ht[2];
    long rehashidx; /* rehashing not in progress if rehashidx == -1 */
    int iterators; /* number of iterators currently running */
} dict;

typedef struct dictht {
    dictEntry **table;
    unsigned long size;
    unsigned long sizemask;
    unsigned long used;
} dictht;

  

dictEntry *dictFind(dict *d, const void *key)
{
    dictEntry *he;
    unsigned int h, idx, table;

    if (d->ht[0].size == 0) return NULL; /* We don't have a table at all */
    if (dictIsRehashing(d)) _dictRehashStep(d);
    h = dictHashKey(d, key);
    for (table = 0; table <= 1; table++) {
        idx = h & d->ht[table].sizemask;
        he = d->ht[table].table[idx];
        while(he) {
            if (dictCompareKeys(d, key, he->key))
                return he;
            he = he->next;
        }
        if (!dictIsRehashing(d)) return NULL;
    }
    return NULL;
}
 
 

  key -> hashValue -> &hashMask -> hashIndex -> hashTable[hashIndex] (hashTableEleList[idx]) -> hashTableEleList[idx][j]

  dictKey ----------------------------------------------------------------------------------------------------------------------> dictVal

why hash:

   

     no  hash table , store m * n elems. 

  hash, store m list head.

    seraching, storing...

 

    比如 dict 是个管理仓库的,仓库管理员按客人的姓名笔划来分架子。李一一来取货,dict算下李的在8号架子上,但是是8号架上的哪个柜子,要一个个来查看,只能根据姓名来一一对比了,最终找到客人的柜子,那个大的东西,肯定不好拿了,直接告诉李一一你的柜子地址就好了,让李一一去自己去对这个柜子中的东西操作。
  

 

转载于:https://www.cnblogs.com/ashen/p/11301828.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值