redis链表
- 作用:实现list命令
- 作为redis定时事件的实现方式
- 服务器保存客户端列表等
数据结构
// 链表节点
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;
相关宏定义
/* 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);
/* 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().
*
* 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;
list->dup = NULL;
list->free = NULL;
list->match = NULL;
return list;
}
/* Free the whole list.
*
* This function can't fail. */
// 释放链表
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); // 释放整个链表管理节点
}
/* 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 *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;
}
/* 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 *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) { // 插入节点后,非头节点
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.
*
* 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)// 头部开始的迭代器
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.
*
* 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.
*
* 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.
*
* 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;
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;
}
/* 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.
*
* 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;
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;
}
/* 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. */
// 获取索引上的链表节点
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;
}