zoukankan      html  css  js  c++  java
  • Redis源码系列(二)

    Redis源码系列——双链表

    redis底层的数据结构使用了双链表,其实现很简洁,值得阅读。

    原型 src/adlist.h

    /*list node*/
    typedef struct listNode{
            struct listNode *prev;
            struct listNode *next;
            /*generic value*/
            void *value;
    }listNode;
    
    

    提供了迭代器

    /*list iterator*/
    typedef struct listIter{
            listNode *next;
            int direction;
    }listIter;
    

    一个链表结构的定义如下:

    typedef struct list{
            /*head and tail node*/
            listNode *head;
            listNode *tail;
            /*copy a node*/
            void * (*dup)(void *ptr);
            /*free a node*/
            void (*free)(void *ptr);
            /*matching function*/
            int (*match)(void *ptr,void *key);
            /*node number*/
            unsigned long len;
    }list;
    

    所以根据这几个结构体,链表应该是这个样子

    下面是几个宏定义,提供了一些方便的功能

    #define listLength(l) ((l)->len)
    #define listFirst(l) ((l)->head)
    #define listLast(l) ((l)->last)
    
    #define listPrevNode(n) ((n)->prev)
    #define listNextNode(n) ((n)->next)
    #define listNodeValue(n)((n)->value)
    
    /*set the dup function pointer*/
    #define listSetDupMethod(l,m) ((l)->dup=(m))
    /*set the free function pointer*/
    #define listSetFreeMethod(l,m) ((l)->free=(m))
    /*set the matching function pointer*/
    #define listSetMatchMethod(l,m) ((l)->match=(m))
    
    /*return function pointer method of list*/
    #define listGetDupMethod(l) ((l)->dup)
    #define listGetFreeMethod(l) ((l)->free)
    #define listGetMatchMethod(l) ((l)->match)
    

    下面是一些API接口

    list *listCreate(void);
    void listRelease(list *list);
    list *listAddNodeHead(list *list, void *value);
    list *listAddNodLeTail(list *list, void *value);
    list *listInsertNode(list *list, listNode *old_node, void *value, int after);
    void listDelNode(list *list, listNode *node);
    listIter *listGetIterator(list *list, int direction);
    listNode *listNext(listIter *iter);
    void listReleaseIterator(listIter *iter);
    list *listDup(list *orig);
    listNode *listSearchKey(list *list, void *key);
    listNode *listIndex(list *list, long index);
    void listRewind(list *list, listIter *li);
    void listRewindTail(list *list, listIter *li);
    void listRotate(list *list);
    

    因为有迭代器,还有两个宏定义来对迭代器的方向进行控制

    /*from head to tail*/
    #define AL_START_HEAD 0
    /*from tail to head*/
    #define AL_START_TAIL 1
    

    实现 src/adlist.c

    1.listCreate

    创建一个空的双链表

    /*
     * 创建一个新的链表 
     * 创建成功返回链表,失败返回 NULL 。
     */
    list *listCreate(void)
    {
        struct list *list;
    
        // 分配内存
        if ((list = zmalloc(sizeof(*list))) == NULL)
            return NULL;
    
        // 初始化属性
        list->head = list->tail = NULL;
        list->len = 0;
        list->dup = NULL;
        list->free = NULL;
        list->match = NULL;
        return list;
    }
    

    2.listRelease

    释放整个链表

    void listRelease(list *list)
    {
        unsigned long len;
        listNode *current, *next;
        // 指向头指针
        current = list->head;
        // 遍历整个链表
        len = list->len;
        while(len--) {
            next = current->next;
            // 如果有设置值释放函数,那么调用它
            if (list->free) list->free(current->value);
            // 释放节点结构
            zfree(current);
            current = next;
        }
        // 释放链表结构
        zfree(list);
    }
    

    3.listAddNodeHead

    在表头插入一个节点,该节点成为新的表头.在各种操作中,由于headtail这两个属性,所以要考虑边界条件,即会改变这俩属性的地方.

    /*
     * 将一个包含有给定值指针 value 的新节点添加到链表的表头
     * 如果为新节点分配内存出错,那么返回 NULL
     * 如果执行成功,返回传入的链表指针
     */
    list *listAddNodeHead(list *list, void *value)
    {
        listNode *node;
    
        // 为节点分配内存
        if ((node = zmalloc(sizeof(*node))) == NULL)
            return NULL;
    
        // 保存值指针
        node->value = value;
    
        // !添加节点到空链表
        if (list->len == 0) {
            list->head = list->tail = node;
            node->prev = node->next = NULL;
        // 添加节点到非空链表
        } else {
            node->prev = NULL;
            node->next = list->head;
            list->head->prev = node;
            list->head = node;
        }
        // 更新链表节点数
        list->len++;
        return list;
    }
    

    4.listAddNodeTail

    在尾部插入节点,新的节点变成尾节点

    /*
     * 将一个包含有给定值指针 value 的新节点添加到链表的表尾
     * 如果为新节点分配内存出错,那么返回 NULL
     * 如果执行成功,返回传入的链表指针
     */
    list *listAddNodeTail(list *list, void *value)
    {
        listNode *node;
    
        // 为新节点分配内存
        if ((node = zmalloc(sizeof(*node))) == NULL)
            return NULL;
    
        // 保存值指针
        node->value = value;
    
        // 目标链表为空
        if (list->len == 0) {
            list->head = list->tail = node;
            node->prev = node->next = NULL;
        // 目标链表非空
        } else {
            node->prev = list->tail;
            node->next = NULL;
            list->tail->next = node;
            list->tail = node;
        }
    
        // 更新链表节点数
        list->len++;
    
        return list;
    }
    
    

    5.listInsertNode

    在特定的节点前后插入节点

    /*
     * 创建一个包含值 value 的新节点,并将它插入到 old_node 的之前或之后
     * after 为 0 ,插入到 old_node 之前。
     * after 为 1 ,插入到 old_node 之后。
     */
    list *listInsertNode(list *list, listNode *old_node, void *value, int after) {
        listNode *node;
        // 创建新节点
        if ((node = zmalloc(sizeof(*node))) == NULL)
            return NULL;
        // 保存值
        node->value = value;
        // 将新节点添加到给定节点之后
        if (after) {
            node->prev = old_node;
            node->next = old_node->next;
            // 给定节点是原表尾节点
            if (list->tail == old_node) {
                list->tail = node;
            }
        // 将新节点添加到给定节点之前
        } else {
            node->next = old_node;
            node->prev = old_node->prev;
            // 给定节点是原表头节点
            if (list->head == old_node) {
                list->head = node;
            }
        }
        if (node->prev != NULL) {
            node->prev->next = node;
        }
        if (node->next != NULL) {
            node->next->prev = node;
        }
        // 更新链表节点数
        list->len++;
        return list;
    }
    

    6. listDelNode

    从链表中删除节点

    /*
     * 从链表 list 中删除给定节点 node 
     */
    void listDelNode(list *list, listNode *node)
    {
        // 调整前置节点的指针
        if (node->prev)
            node->prev->next = node->next;
        else
            list->head = node->next;
        // 调整后置节点的指针
        if (node->next)
            node->next->prev = node->prev;
        else
            list->tail = node->prev;
        // 释放值
        if (list->free) list->free(node->value);
        // 释放节点
        zfree(node);
        list->len--;
    }
    

    7.listGetIterator

    /*
     * 为给定链表创建一个迭代器,
     * 之后每次对这个迭代器调用 listNext 都返回被迭代到的链表节点
     * direction 参数决定了迭代器的迭代方向:
     *  AL_START_HEAD :从表头向表尾迭代
     *  AL_START_TAIL :从表尾想表头迭代
     */
    listIter *listGetIterator(list *list, int direction)
    {
        // 为迭代器分配内存
        listIter *iter;
        if ((iter = zmalloc(sizeof(*iter))) == NULL) return NULL;
    
        // 根据迭代方向,设置迭代器的起始节点
        if (direction == AL_START_HEAD)
            iter->next = list->head;
        else
            iter->next = list->tail;
        // 记录迭代方向
        iter->direction = direction;
        return iter;
    }
    

    8.listReleaseIterator

    void listReleaseIterator(listIter *iter) {
        zfree(iter);
    }
    

    9.listRewind

    /*
     * 将迭代器的方向设置为 AL_START_HEAD ,
     * 并将迭代指针重新指向表头节点。
     */
    void listRewind(list *list, listIter *li) {
        li->next = list->head;
        li->direction = AL_START_HEAD;
    }
    /*
     * 将迭代器的方向设置为 AL_START_TAIL ,
     * 并将迭代指针重新指向表尾节点。
     */
    void listRewindTail(list *list, listIter *li) {
        li->next = list->tail;
        li->direction = AL_START_TAIL;
    }
    

    10.listNext

    迭代器的使用,返回当前迭代到的元素

    /*
     * 返回迭代器当前所指向的节点。
     * !删除当前节点是允许的,但不能修改链表里的其他节点。
     * 函数要么返回一个节点,要么返回 NULL ,常见的用法是:
     */
    listNode *listNext(listIter *iter)
    {
        listNode *current = iter->next;
    
        if (current != NULL) {
            // 根据方向选择下一个节点
            if (iter->direction == AL_START_HEAD)
                // 保存下一个节点,防止当前节点被删除而造成指针丢失
                iter->next = current->next;
            else
                // 保存下一个节点,防止当前节点被删除而造成指针丢失
                iter->next = current->prev;
        }
    
        return current;
    }
    

    11.listDup

    复制整个链表,然后返回一个副本

    list *listDup(list *orig)
    {
        list *copy;
        listIter *iter;
        listNode *node;
    
        // 创建新链表
        if ((copy = listCreate()) == NULL)
            return NULL;
    
        // 设置节点值处理函数
        copy->dup = orig->dup;
        copy->free = orig->free;
        copy->match = orig->match;
    
        // 迭代整个输入链表
        iter = listGetIterator(orig, AL_START_HEAD);
        while((node = listNext(iter)) != NULL) {
            void *value;
    
            // 复制节点值到新节点
            if (copy->dup) {
                value = copy->dup(node->value);
                if (value == NULL) {
                    listRelease(copy);
                    listReleaseIterator(iter);
                    return NULL;
                }
            } else
                value = node->value;
    
            // 将节点添加到链表
            if (listAddNodeTail(copy, value) == NULL) {
                listRelease(copy);
                listReleaseIterator(iter);
                return NULL;
            }
        }
        // 释放迭代器
        listReleaseIterator(iter);
        return copy;
    }
    

    12.listSearchKey

    匹配list中的值与传入的key,对比由match或者直接对比指针实现

    /* 
     * 查找链表 list 中值和 key 匹配的节点。
     * 对比操作由链表的 match 函数负责进行,
     * 如果没有match 函数,
     * 那么直接通过对比值的指针来决定是否匹配。
     * 如果匹配成功,那么第一个匹配的节点会被返回。
     * 否则返回 NULL 。
     */
    listNode *listSearchKey(list *list, void *key)
    {
        listIter *iter;
        listNode *node;
    
        // 迭代整个链表
        iter = listGetIterator(list, AL_START_HEAD);
        while((node = listNext(iter)) != NULL) {
            
            // 对比
            if (list->match) {
                if (list->match(node->value, key)) {
                    listReleaseIterator(iter);
                    // 找到
                    return node;
                }
            } else {
                if (key == node->value) {
                    listReleaseIterator(iter);
                    // 找到
                    return node;
                }
            }
        }
        
        listReleaseIterator(iter);
        return NULL;
    }
    
    

    13.listIndex

    返回指定索引上的值,可以为负数,表示从尾部开始

    /*
     * 返回链表在给定索引上的值。
     * 索引以 0 为起始,也可以是负数, -1 表示链表最后一个节点
     * 如果索引超出范围,返回 NULL 。
     */
    listNode *listIndex(list *list, long index) {
        listNode *n;
    
        // 如果索引为负数,从表尾开始查找
        if (index < 0) {
            index = (-index)-1;
            n = list->tail;
            while(index-- && n) n = n->prev;
        // 如果索引为正数,从表头开始查找
        } else {
            n = list->head;
            while(index-- && n) n = n->next;
        }
    
        return n;
    }
    

    14.listRotate

    将表尾变成表头

    void listRotate(list *list) {
        listNode *tail = list->tail;
    
        if (listLength(list) <= 1) return;
    
        /* Detach current tail */
        // 取出表尾节点
        list->tail = tail->prev;
        list->tail->next = NULL;
    
        /* Move it as head */
        // 插入到表头
        list->head->prev = tail;
        tail->prev = NULL;
        tail->next = list->head;
        list->head = tail;
    }
    
  • 相关阅读:
    IO模型
    函数第一类对象,闭包,迭代器
    admin里面的注册模型类的写法
    升级pip
    Windows部署superset操作手册
    Python命名空间和作用域窥探
    使用CSS3画出一个叮当猫
    D
    [java]说说 JRE , JDK , JVM 三者之间的区别与联系
    H~N皇后问题
  • 原文地址:https://www.cnblogs.com/oasisyang/p/14299121.html
Copyright © 2011-2022 走看看