今天我们来学习一下 redis 中 通用双向链表 list 的实现,涉及到的代码有 src/adlist.h src/adlist.c
首先来看一下主要结构体的定义,有 node,iterator、list 三个
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;
构造与析构
// 构造
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);
}
构造与析构函数的逻辑都很简单,就不额外说明了。值得注意的是,通过添加函数指针成员,我们可以定制化例如:dup、free 等行为
插入
双向链表的一个好处就是可以以 O(1) 的复杂度在头部和尾部插入节点,看一下 redis 中的实现:
// 在头部插入
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;
}
// 在尾部插入
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;
}
// 在 old_node 前/后插入节点, after==1 在节点后插入反之节点前
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;
}
链表插入逻辑也是很简单的,每个例程不过 20 行代码。只要正确更新影响到的节点的前驱或者后继指针指向即可。
特别注意的一点是,如果链表本来是空链表,我们记得要将 list->tail,list->head 都指向新节点。在节点
前后插入新节点时,则要注意如果新节点成为头、尾节点,也要更新 list 中的指针。
删除
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--;
}
因为同时保存了前驱和后继节点,在双向链表上可以以 O(1) 复杂度删除某节点。在单向列表中的话,有一个小窍门,可以将 node->next 的值
复制到 node 中,然后将 node->next 指向 node->next->next(本方法只适用于非尾节点的删除,尾节点的删除需要遍历链表,复杂度为 O(n))
迭代器
// 构造
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;
}
// 将迭代器设置为从尾节点开始迭代的逆向迭代器
void listRewindTail(list *list, listIter *li) {
li->next = list->tail;
li->direction = AL_START_TAIL;
}
// advance iterator
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;
}
查找
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;
}
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;
}
在查找函数中,redis 使用了迭代器来实现,但是在 index 函数,则直接操作的是底层数据,其实笔者本人并不太喜欢,觉得
代码并不具有一致性。既然定义了迭代器,也许可以将更多的函数实现使用迭代器来实现。
总结
redis 的通用双向链表的实现比较常规,代码简洁易懂
本文深入探讨Redis中双向链表的实现细节,包括其结构定义、构造与析构过程、节点插入与删除逻辑,以及迭代器和查找功能的实现。

被折叠的 条评论
为什么被折叠?



