结合redis设计与实现的redis源码学习-3-链表

在上一篇中,我们了解了redis的基础数据结构SDS,在redis中,基本上所有的数据都是以SDS的形式存储的,无论是long还是int。
在有了基础数据类型后,我们就要了解它是怎样在redis中使用的,今天我们来分析redis链表的实现。
在学习的过程中基本是复习对链表的操作,只是有一些实现方法和理念解决了运行速度和适用环境的问题。
redis的链表具有以下特点:
1、双端:获取某个节点的前后节点的复杂度都是O(1);
2、无环:表头的prev和表尾的next都指向NULL,对链表的访问以NULL终结;
3、携带表头和表尾指针:通过head和tail获取的复杂度为O(1);
4、带链表长度计数器:使用len获取节点数数量复杂度O(1);
5、多态:使用void*指向数据,通过dup,free,match设置节点的函数,可以保存各种不同类型的值。
上代码:
.h文件中定义了链表的子节点,迭代器,和链表结构体。

/* adlist.h - A generic doubly linked list implementation */

#ifndef __ADLIST_H__
#define __ADLIST_H__

/* Node, List, and Iterator are the only data structures used currently. */
//链表的子节点,是一个双向链表,void*可以指向任何结构
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 listGetFree(l) ((l)->free)
#define listGetMatchMethod(l) ((l)->match)

/* Prototypes */
list *listCreate(void);//创建链表
void listRelease(list *list);//释放链表
list *listAddNodeHead(list *list, void *value);//头插
list *listAddNodeTail(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);//链表旋转
//这里定义了迭代器的两个方向,这里我奇怪既然要效率为什么不直接定义两种迭代器类型,这样的话就不用在迭代器中判断方向了
/* Directions for iterators */
#define AL_START_HEAD 0
#define AL_START_TAIL 1
#endif /* __ADLIST_H__ */

下面是.c文件关于链表的操作:

// adlist.c - A generic doubly linked list implementation

#include <stdlib.h>
#include "adlist.h"
#include "zmalloc.h"
//在堆中创建一个链表节点,并将各个数据成员初始化,放回地址
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;
}
//释放每个节点,最后释放链表节点
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);
}
//头插法,如果链表没有节点,那么头尾都指向该节点
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;
}
//尾插,如果长度为0,那么头尾都指向该节点
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;
}
//在指定节点之前或者之后插入数据,after表示前后true为齐纳
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;//将老节点的next指向新节点
    }
    if (node->next != NULL) {
        node->next->prev = node;//将老节点的下一个节点的prev指向新节点
    }
    list->len++;
    return list;
}
//删除节点,头尾几点是特殊情况,其他可以直接让前一节点的next指向后一节点,后一节点的prev指向前一节点,然后释放该节点
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--;
}
//获取链表的迭代器,要指明是正向还是逆向的
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;
}
void listReleaseIterator(listIter *iter) {
    zfree(iter);
}
//重置迭代器的位置
void listRewind(list *list, listIter *li) {
    li->next = list->head;
    li->direction = AL_START_HEAD;//0
}
void listRewindTail(list *list, listIter *li) {
    li->next = list->tail;
    li->direction = AL_START_TAIL;//1
}
//让迭代器指向下一个节点
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;
}
//复制链表,创建一个新链表,将链表的所有成员复制过来,然后利用迭代器和复制函数指针指向的复制函数复制每个节点
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;
}
//查找某一值的链表节点
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;
}
//返回第几个节点(从头开始数),如果是负值就从尾开始数
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 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;
}

因为链表操作比较简单且可以适用个中数据类型,在以后的工作中可能会用到,所以将所有的链表操作都写了进来。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章