adlist
adlist
为Redis基本数据结构之一,为双向链表,记录了链表长度,adlist的迭代器记录了迭代节点和方向,个人觉得实现优于STL的list
几个重要结构
adlist实现比较精简,基本上写过链表相关的代码就能很快写出所有实现函数
/*
* 双端链表节点
*/
typedef struct listNode {
// 前置节点
struct listNode *prev;
// 后置节点
struct listNode *next;
// 节点的值
void *value;
} listNode;
/*
* 双端链表迭代器
*/
typedef struct listIter {
// 当前迭代到的节点
listNode *next;
// 迭代的方向
int direction;
} listIter;
// 从表头向表尾进行迭代
#define AL_START_HEAD 0
// 从表尾到表头进行迭代
#define AL_START_TAIL 1
/*
* 双端链表结构
*/
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;
基本函数总览
函数 | 功能 | 复杂度 |
---|---|---|
listCreate | 创建链表 | O(1) |
listRelease | 释放,回收空间 | O(N) |
listInsertNode | 插入节点 | O(1) |
listDelNode | 删除节点 | O(1) |
listSearchKey | 查找节点 | O(N) |
listGetIterator | 获取迭代器 | O(1) |
… | … | … |
其它的宏/函数实现都很简单。不做介绍了
listCreate
// 创建一个无节点的空链表
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;
}
listRelease
// 释放所有节点再释放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);
}
listInsertNode
// 在所给节点的前/后插入新节点
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;
}
listDelNode
// 删除节点
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--;
}
listSearchKey
// 根据给定Key值查找节点,找到则返回第一个
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;
}
listGetIterator
// 根据方向获取迭代器
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;
}
小结
adlist
实现比较简单,与STL的list相比复杂性小了很多,但基本的功能都有。STL提供了合并排序等功能,adlist记录了链表长度,能O(1)得到长度