redis6.0.5之adlist阅读笔记--双向链表

redis6.0.5之adlist阅读笔记--双向链表
***********************************************************************
双向链表的数据结构
/* Node, List, and Iterator are the only data structures used currently. */
typedef struct listNode {
    struct listNode *prev;   前指针,指向前面的节点
    struct listNode *next;   后指针,指向后面的节点
    void *value;             当前节点的值
} listNode;

typedef struct listIter {
    listNode *next;          指向下一个节点的指针
    int direction;           迭代方向,从前往后或者从后往前
} listIter;

typedef struct list {         
    listNode *head;        头节点指针
    listNode *tail;        尾节点指针
    void *(*dup)(void *ptr);   复制函数指针
    void (*free)(void *ptr);   释放函数指针
    int (*match)(void *ptr, void *key);   匹配函数指针
    unsigned long len;     链表长度
} list;

***********************************************************************
宏定义函数
/* Functions implemented as macros */
#define listLength(l) ((l)->len)          获取链表长度
#define listFirst(l) ((l)->head)          获取链表的头节点
#define listLast(l) ((l)->tail)           获取链表的尾节点
#define listPrevNode(n) ((n)->prev)       获取前面的节点
#define listNextNode(n) ((n)->next)       获取后面的节点
#define listNodeValue(n) ((n)->value)     获取当前节点的值

#define listSetDupMethod(l,m) ((l)->dup = (m))      赋值复制函数
#define listSetFreeMethod(l,m) ((l)->free = (m))    赋值释放函数
#define listSetMatchMethod(l,m) ((l)->match = (m))  赋值匹配函数

#define listGetDupMethod(l) ((l)->dup)        获取复制函数
#define listGetFreeMethod(l) ((l)->free)      获取释放函数
#define listGetMatchMethod(l) ((l)->match)    获取匹配函数

***********************************************************************
/* Create a new list. The created list can be freed with
 * AlFreeList(), but private value of every node need to be freed
 * by the user before to call AlFreeList().
创建一个新链表。创建的链表能够被函数AlFreeList释放,
但是每个节点的私有值必须由创建者自己在调用AlFreeList之前释放
 * On error, NULL is returned. Otherwise the pointer to the new list. */
 创建成功返回新的链表,失败就返回空
list *listCreate(void)
{
    struct list *list;

    if ((list = zmalloc(sizeof(*list))) == NULL)  开辟链表结构体大小的空间
        return NULL;
    list->head = list->tail = NULL;   初始化都为空,没有元素
    list->len = 0;     长度为0
    list->dup = NULL;  没有函数
    list->free = NULL;
    list->match = NULL;
    return list;
}
***********************************************************************
/* Remove all the elements from the list without destroying the list itself. */
释放链表上所有的节点,但是不释放链表本身
void listEmpty(list *list)
{
    unsigned long len;
    listNode *current, *next;

    current = list->head; 获取头节点
    len = list->len;  获取长度
    while(len--) {  长度不为0
        next = current->next;  保存下一个节点
        if (list->free) list->free(current->value); 存在释放函数,就对节点值进行释放处理
        zfree(current);  释放当前节点
        current = next;  迭代下一个节点
    }
    list->head = list->tail = NULL;   全部释放完毕,就将头和尾全部置成空
    list->len = 0; 长度为0
}
***********************************************************************
/* Free the whole list.  释放整个链表
 * This function can't fail. */ 这个函数不会失败,如果机器挂掉呢?
void listRelease(list *list)
{
    listEmpty(list);  释放所有节点
    zfree(list);      释放链表本身
}
***********************************************************************
/* Add a new node to the list, to head, containing the specified 'value'
 * pointer as value.
添加一个新节点道链表头部,包括节点中指向的值
 * On error, NULL is returned and no operation is performed (i.e. the list remains unaltered).
失败的情况下,返回空,对链表不做任何改变(即链表保持原样) 
 * On success the 'list' pointer you pass to the function is returned. */
成功情况下,传入函数的参数list指针就会返回新链表
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++;   元素个数增加1
    return list;   返回链表指针
}
***********************************************************************
/* Add a new node to the list, to tail, containing the specified 'value'
 * pointer as value.
在链表尾部添加一个新节点,包括节点中指向的值
 * On error, NULL is returned and no operation is performed (i.e. the list remains unaltered).
 失败的情况下,返回空,对链表不做任何改变(即链表保持原样) 
 * On success the 'list' pointer you pass to the function is returned. */
 成功情况下,传入函数的参数list指针就会返回新链表
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;
}
***********************************************************************
链表中间加入一个节点,在某个特定节点前面或者后面
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) { 
       如果新节点的前节点不为空,那么它的后节点就是这个新节点,因为是双向链表,有4个方向,
       之前只是处理了新节点指向的两个方向了,还剩下两个从前后指向新节点的两个方向,
       这里是处理从前面指向这个新节点的方向
        node->prev->next = node; 
    }
    if (node->next != NULL) {
        这里是处理从后面指向这个新节点的方向
        node->next->prev = node;
    }
    list->len++;
    return list;
}
***********************************************************************
/* Remove the specified node from the specified list.
 * It's up to the caller to free the private value of the node.
从指定链表移除指定节点,由调用者来释放节点的私有值
 * This function can't fail. */ 这个函数不会失败
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--; 节点数目减1
}
***********************************************************************
/* Returns a list iterator 'iter'. After the initialization every
 * call to listNext() will return the next element of the list.
返回一个链表的迭代器iter。经过初始化之后,
每个调用函数listNext都会返回链表的下一个节点
 * This function can't fail. */
listIter *listGetIterator(list *list, int direction)
{
    listIter *iter;

    if ((iter = zmalloc(sizeof(*iter))) == NULL) return NULL; 开辟一个迭代器内存空间
    if (direction == AL_START_HEAD)    //  #define AL_START_HEAD 0  如果从头开始迭代
        iter->next = list->head;       //将头节点赋给迭代器
    else
        iter->next = list->tail;  //从尾节点开始迭代,将尾节点赋给迭代器
    iter->direction = direction;  //确定方向不变
    return iter;
}
***********************************************************************
/* Release the iterator memory */
释放迭代器的内存空间
void listReleaseIterator(listIter *iter) {
    zfree(iter);
}
***********************************************************************
/* Create an iterator in the list private iterator structure */
在列表私有迭代器结构中创建迭代器
void listRewind(list *list, listIter *li) {
    li->next = list->head;
    li->direction = AL_START_HEAD;
}
***********************************************************************
void listRewindTail(list *list, listIter *li) {
    li->next = list->tail;
    li->direction = AL_START_TAIL;
}
***********************************************************************
/* Return the next element of an iterator.
 * It's valid to remove the currently returned element using
 * listDelNode(), but not to remove other elements.
返回迭代器的下一个元素
可以使用listDelNode移除当前返回的元素,但是不能移除另外的元素
 * The function returns a pointer to the next element of the list,
 * or NULL if there are no more elements, so the classical usage patter
 * is:
函数返回一个指向链表下一个元素的指针,或者空如果没有元素了。
所以使用的经典模式如下
 * iter = listGetIterator(list,<direction>);  获取迭代器
 * while ((node = listNext(iter)) != NULL) {   遍历链表
 *     doSomethingWith(listNodeValue(node));  针对每个元素做操作
 * }
 *
 * */
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;
}
***********************************************************************
/* Duplicate the whole list. On out of memory NULL is returned.
 * On success a copy of the original list is returned.
复制整个链表,如果OOM就返回空,如果成功就返回一个原始链表的拷贝
 * The 'Dup' method set with listSetDupMethod() function is used
 * to copy the node value. Otherwise the same pointer value of
 * the original node is used as value of the copied node.
如果复制函数由函数listSetDupMethod设置过,那么复制函数就被用来拷贝这个节点的值,
否则的话,复制节点使用原节点值同样的指针
 * The original list both on success or error is never modified. */
原始的链表不受这个函数成功失败的影响保持不变
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;
    listRewind(orig, &iter);  //用链表本身构建迭代器
    while((node = listNext(&iter)) != NULL) {  //遍历链表
        void *value;

        if (copy->dup) {  如果存在拷贝函数
            value = copy->dup(node->value);  通过存在的函数做节点值的复制
            if (value == NULL) { 复制值失败
                listRelease(copy); 将列表释放,返回空
                return NULL;
            }
        } else
            value = node->value;  不存在拷贝函数,直接用原来的指针指向的值
            
        if (listAddNodeTail(copy, value) == NULL) {  在链表末尾添加节点
            listRelease(copy);
            return NULL;
        }
    }
    return copy;
}
***********************************************************************
/* Search the list for a node matching a given key.
 * The match is performed using the 'match' method
 * set with listSetMatchMethod(). If no 'match' method
 * is set, the 'value' pointer of every node is directly
 * compared with the 'key' pointer.
对于给定键搜索链表中的一个节点。匹配的动作由函数listSetMatchMethod设置的match方法执行,
如果没有设定match方法,那么直接使用节点值的指针和键的指针进行比较
 * On success the first matching node pointer is returned
 * (search starts from head). If no matching node exists
 * NULL is returned. */
返回成功匹配的第一个节点(从头部开始),如果不存在匹配的节点,那么返回空
listNode *listSearchKey(list *list, void *key)
{
    listIter iter;
    listNode *node;

    listRewind(list, &iter); 通过链表本身构建迭代器
    while((node = listNext(&iter)) != NULL) { 遍历链表
        if (list->match) {  存在匹配函数
            if (list->match(node->value, key)) {  成功匹配
                return node;
            }
        } else {不存在匹配函数
            if (key == node->value) {
                return node;
            }
        }
    }
    return NULL;
}
***********************************************************************
/* Return the element at the specified zero-based index
 * where 0 is the head, 1 is the element next to head
 * and so on. Negative integers are used in order to count
 * from the tail, -1 is the last element, -2 the penultimate
 * and so on. If the index is out of range NULL is returned. */
返回一个基于0为基准位置的节点,0表示头节点,1表示头节点的下一个节点,以此类推。
负数表示从尾部开始计数,-1表示尾节点,-2表示倒数第二个节点,以此类推。
如果索引的位置超出了链表的范围,那么返回空
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;
}
***********************************************************************
/* Rotate the list removing the tail node and inserting it to the head. */
翻转链表的尾节点成头节点
void listRotateTailToHead(list *list) {
    if (listLength(list) <= 1) return;  当一个节点时候,既是头又是尾,无需操作

    /* Detach current tail */
    listNode *tail = list->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;        原头结点前面是新头节点
}
***********************************************************************
/* Rotate the list removing the head node and inserting it to the tail. */
翻转链表的头节点成尾节点
void listRotateHeadToTail(list *list) {
    if (listLength(list) <= 1) return;

    listNode *head = list->head;
    /* Detach current head */
    list->head = head->next;  将头节点的下一个节点设置为头节点
    list->head->prev = NULL;  新头结点前无节点
    
    /* Move it as tail */
    list->tail->next = head;  放在原尾节点后面成为新的尾节点
    head->next = NULL;        新尾节点后面没有节点
    head->prev = list->tail;  原尾节点成为倒数第二个节点
    list->tail = head;        设置新尾节点
}
***********************************************************************
/* Add all the elements of the list 'o' at the end of the
 * list 'l'. The list 'other' remains empty but otherwise valid. */
添加链表o的所有元素在链表l的结尾。清空链表o,但是不释放
void listJoin(list *l, list *o) {
    if (o->head)  o非空
        o->head->prev = l->tail;  将0的首节点的前节点设置为l的尾节点

    if (l->tail)  l非空
        l->tail->next = o->head; 将l的尾节点设置为o头节点的后继
    else
        l->head = o->head; 如果l为空,那么o的首节点就是l的首节点

    if (o->tail) l->tail = o->tail; 设置新的尾节点
    
    l->len += o->len;  将两个链表的元素加到一起
    
    /* Setup other as an empty list. */
    o->head = o->tail = NULL; 将链表o清空
    o->len = 0;
}
***********************************************************************

 

posted on 2020-08-21 12:28  子虚乌有  阅读(202)  评论(0)    收藏  举报