和字符串一样,Redis同样自己实现了链表数据结构,但自己实现的理由很简单,就是C语言没有提供链表数据结构,那么接下来我们将通过阅读源码了解到Redis实现的链表和其他语言如Java或C++实现的链表有什么不同。
1. 链表和链表节点的实现
typedef struct listNode {
// 前置节点
struct listNode *prev;
// 后置节点
struct listNode *next;
// 节点的值
void *value;
} listNode;
从链表节点的定义可以看出,Redis的链表实现属于双向链表。
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;
学过数据结构的我们都知道,仅需要链表节点就能构造一个链表,但是为了更方便操作链表,Redis提供了list结构体。list提供了表头节点head、表尾节点tail、节点数量len等属性,同时还提供了节点复制函数dup用于复制节点的值、节点值释放函数free用于释放节点空间、节点值对比函数match用于对比两个节点值是否相等。
2. 创建一个新的链表
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;
}
一个空链表的长度为0,表头指针和表尾指针为NULL。相关函数也被初始化为NULL。
3. 设置或获取函数指针
// 将链表 l 的值复制函数设置为 m
#define listSetDupMethod(l,m) ((l)->dup = (m))
// 将链表 l 的值释放函数设置为 m
#define listSetFreeMethod(l,m) ((l)->free = (m))
// 将链表的对比函数设置为 m
#define listSetMatchMethod(l,m) ((l)->match = (m))
// 返回给定链表的值复制函数
#define listGetDupMethod(l) ((l)->dup)
// 返回给定链表的值释放函数
#define listGetFree(l) ((l)->free)
// 返回给定链表的值对比函数
#define listGetMatchMethod(l) ((l)->match)
这个应该是根据链表中存储的元素内容动态设置的。
4. 添加新节点到表头
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;
// list的表头节点的prev和表尾节点的next指向NULL,因此不会产生环形节点
node->prev = node->next = NULL;
// 添加节点到非空链表
} else {
node->prev = NULL;
node->next = list->head;
list->head->prev = node;
list->head = node;
}
// 更新链表节点数
list->len++;
return list;
}
5. 添加新节点到表尾部
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代码块中的内容不同。
} else {
node->prev = list->tail;
node->next = NULL;
list->tail->next = node;
list->tail = node;
}
// 更新链表节点数
list->len++;
return list;
}
6. 插入新节点到某个节点的后面
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;
}
7. 获取链表长度
#define listLength(l) ((l)->len)
8. 返回表头节点
#define listFirst(l) ((l)->head)
9. 返回表尾节点
#define listLast(l) ((l)->tail)
10. 返回一个节点的前置节点
#define listPrevNode(n) ((n)->prev)
11. 返回一个节点的后置节点
#define listNextNode(n) ((n)->next)
12. 返回一个节点的值
#define listNodeValue(n) ((n)->value)
13. 返回链表迭代器
// 从表头向表尾进行迭代
#define AL_START_HEAD 0
// 从表尾到表头进行迭代
#define AL_START_TAIL 1
typedef struct listIter {
// 当前迭代到的节点
listNode *next;
// 迭代的方向
int direction;
} listIter;
上面是迭代器的定义,next表示下一个被访问的节点,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;
}
获取迭代器时需要指定链表和迭代器的方向,如果从表头开始迭代,则迭代器对象中的next为表头节点,反之为表尾节点。
14. 释放迭代器内存
void listReleaseIterator(listIter *iter) {
zfree(iter);
}
15. 查找第一个匹配的节点 O(n)
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;
}
首先获取一个从表头开始遍历的迭代器,使用match方法进行节点值匹配,返回第一个节点值相等的节点,然后释放迭代器空间。
16. 返回指定索引上的节点 O(n)
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;
}
索引值可以为负数,正数表示从表头开始查找,负数表示从表尾开始查找。
17. 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;
}
18. 链表复制
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;
}
链表复制包括设置处理函数和拷贝节点值。
19. 释放节点内存
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);
}
20. 调整迭代器方向
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;
}